-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
458 lines (412 loc) · 12.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*global it describe before after*/
/*eslint no-undef: "error"*/
'use strict';
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));
const path = require('path');
const assert = require('assert');
const filedisk = Promise.promisifyAll(require('file-disk'), { multiArgs: true });
const constants = require('../lib/constants');
const lkl = Promise.promisifyAll(require('../'));
lkl.fs = Promise.promisifyAll(lkl.fs);
const RAW_FS_PATH = path.join(__dirname, 'fixtures/test.ext4');
const TMP_RAW_FS_PATH = path.join(__dirname, '.tmp-test.ext4');
const DISK_PATH = path.join(__dirname, 'fixtures/disk.img');
describe('node-lkl', function() {
it('should start the kernel', function() {
lkl.startKernelSync(20 * 1024 * 1024);
});
after('stop the kernel', function() {
lkl.haltKernelSync();
});
describe('disks', function() {
describe('raw filesystem image', function() {
const self = this;
before(function() {
return fs.openAsync(RAW_FS_PATH, 'r')
.then(function(fd) {
self.fd = fd;
const disk = new filedisk.FileDisk(fd, true, false);
self.disk = new lkl.disk.DiskWrapper(disk);
return lkl.diskAddAsync(self.disk);
})
.then(function(diskId) {
self.diskId = diskId;
});
});
it('should mount', function() {
return lkl.mountAsync(this.diskId, { readOnly: true, filesystem: 'ext4'})
.then(lkl.umountAsync);
});
after(function(){
return lkl.diskRemoveAsync(this.diskId)
.then(function() {
return fs.closeAsync(self.fd);
});
});
});
describe('MBR disk image', function() {
before(function() {
const self = this;
return fs.openAsync(DISK_PATH, 'r')
.then(function(fd) {
self.fd = fd;
const disk = new filedisk.FileDisk(fd, true, false);
self.disk = new lkl.disk.DiskWrapper(disk);
return lkl.diskAddAsync(self.disk);
})
.then(function(diskId) {
self.diskId = diskId;
});
});
after(function(){
const self = this;
return lkl.diskRemoveAsync(this.diskId)
.then(function() {
return fs.closeAsync(self.fd);
});
});
it('should mount vfat', function() {
return lkl.mountAsync(this.diskId, {readOnly: true, filesystem: 'vfat', partition: 1})
.then(lkl.umountAsync);
});
it('should mount ext2', function() {
return lkl.mountAsync(this.diskId, {readOnly: true, filesystem: 'ext2', partition: 2})
.then(lkl.umountAsync);
});
it('should mount ext4', function() {
return lkl.mountAsync(this.diskId, {readOnly: true, filesystem: 'ext4', partition: 3})
.then(lkl.umountAsync);
});
it('should mount btrfs', function() {
return lkl.mountAsync(this.diskId, {readOnly: true, filesystem: 'btrfs', partition: 5})
.then(lkl.umountAsync);
});
it('should mount xfs', function() {
return lkl.mountAsync(this.diskId, {readOnly: true, filesystem: 'xfs', partition: 6})
.then(lkl.umountAsync);
});
it('should be able to mount 4 partitions', function() {
const promises = [
lkl.mountAsync(this.diskId, { readOnly: true, filesystem: 'vfat', partition: 1 }),
lkl.mountAsync(this.diskId, { readOnly: true, filesystem: 'ext2', partition: 2 }),
lkl.mountAsync(this.diskId, { readOnly: true, filesystem: 'ext4', partition: 3 }),
lkl.mountAsync(this.diskId, { readOnly: true, filesystem: 'btrfs', partition: 5 }),
];
return Promise.all(promises)
.then(function(mountpoints) {
return mountpoints;
})
.then(function(mountpoints) {
const promises = mountpoints.map(function(mountpoint) {
return lkl.umountAsync(mountpoint);
});
return Promise.all(promises);
});
});
it('should be able to mount / umount 200 times', function() {
this.timeout(5000);
return Promise.using(filedisk.openFile(RAW_FS_PATH, 'r'), function(fd) {
const disk = new filedisk.FileDisk(fd, true, false);
return Promise.using(lkl.utils.attachDisk(disk), function(diskId) {
const options = { readOnly: true, filesystem: 'ext4' };
const mountThenUmount = function() {
return Promise.using(lkl.utils.mountPartition(diskId, options), function() {});
};
let c = mountThenUmount();
for(let i=0; i < 200; i++) {
c = c.then(mountThenUmount);
}
return c;
});
});
});
});
});
describe('fs', function() {
const folder = '/some folder';
before(function(done) {
const self = this;
fs.createReadStream(RAW_FS_PATH)
.pipe(fs.createWriteStream(TMP_RAW_FS_PATH))
.on('close', function() {
return fs.openAsync(TMP_RAW_FS_PATH, 'r+')
.then(function(fd) {
self.fd = fd;
const d = new filedisk.FileDisk(fd, false, false);
const disk = new lkl.disk.DiskWrapper(d);
return lkl.diskAddAsync(disk);
})
.then(function(diskId) {
self.diskId = diskId;
return lkl.mountAsync(diskId, {filesystem: 'ext4'});
})
.then(function (mountpoint) {
self.mountpoint = mountpoint;
}).nodeify(done);
});
});
after(function() {
const self = this;
return lkl.umountAsync(this.mountpoint)
.then(function() {
return lkl.diskRemoveAsync(self.diskId);
})
.then(function() {
return fs.closeAsync(self.fd);
})
.then(function() {
return fs.unlinkAsync(TMP_RAW_FS_PATH);
});
});
describe('.mkdir(), .rmdir()', function() {
it('should create / delete folder', function(done) {
lkl.fs.mkdir(folder, function(err) {
assert.strictEqual(err, null);
done();
});
});
after(function(done) {
lkl.fs.rmdir(folder, function(err) {
assert.strictEqual(err, null);
done();
});
});
});
describe('.readdir()', function() {
const files = [];
for (let i = 0; i < 100; i++) {
files.push('file' + i);
}
// Please keep the following filename one character long: it will be
// encoded as utf8 and invalid when read as utf16 (UCS-2).
files.push('x');
files.sort();
function touch(path) {
return lkl.fs.openAsync(
path,
constants.O_WRONLY | constants.O_CREAT | constants.O_TRUNC
)
.then(lkl.fs.closeAsync);
}
function createFiles(path) {
return Promise.each(files, function(f) {
return touch(path + '/' + f);
});
}
function deleteFiles(path) {
return Promise.each(files, function(f) {
return lkl.fs.unlinkAsync(path + '/' + f);
});
}
before(function(done) {
return lkl.fs.mkdirAsync(folder)
.then(function() {
return createFiles(folder);
})
.then(function() {
done();
});
});
after(function (done) {
return deleteFiles(folder)
.then(function() {
return lkl.fs.rmdirAsync(folder);
}).then(function() {
done();
});
});
it('should list files', function(done) {
// ext4 partition
lkl.fs.readdir(folder, {}, function(err, result) {
assert.strictEqual(err, null);
assert.deepEqual(result.sort(), files);
done();
});
});
it('should raise an error for non existent folders', function(done) {
lkl.fs.readdir('/no_such_folder', {}, function(err, result) {
assert.strictEqual(err.errno, 2);
assert.strictEqual(err.code, 'ENOENT');
assert.strictEqual(result, undefined);
done();
});
});
it('should raise an error if no path is given', function(done) {
lkl.fs.readdir(undefined, {}, function(err) {
assert.strictEqual(
err.message,
'TypeError: Path must be a string or a Buffer.'
);
done();
});
});
it('should not accept a Number as path', function(done) {
lkl.fs.readdir(42, {}, function(err, result) {
assert.strictEqual(result, undefined);
assert.strictEqual(
err.message,
'TypeError: Path must be a string or a Buffer.'
);
done();
});
});
it('should accept Buffer objects as path', function(done) {
const buf = new Buffer.from(folder, 'utf8');
lkl.fs.readdir(buf, {}, function(err, result) {
assert.strictEqual(err, null);
assert.deepEqual(result.sort(), files);
done();
});
});
it('should respect the encoding option', function(done) {
lkl.fs.readdir(folder, 'buffer', function(err, result) {
assert.strictEqual(err, null);
assert.deepEqual(
result.sort(),
files.map(function(v) {
return new Buffer.from(v, 'utf8');
})
);
done();
});
});
it(
"should raise an error if it can't read filenames with the requested encoding",
function(done) {
lkl.fs.readdir(folder, 'ucs2', function(err, result) {
assert.strictEqual(result, undefined);
assert.strictEqual(err.errno, 22);
assert.strictEqual(err.code, 'EINVAL');
done();
});
}
);
});
describe('.access()', function() {
const createFileWithPerms = function(file, mode) {
// the catch is here in case the file doesn't exist
return lkl.fs.unlinkAsync(file)
.catch(function(err) {
if (err) {
assert.strictEqual(
err.message,
'ENOENT, No such file or directory'
);
}
}).then(function() {
return lkl.fs.writeFileAsync(file, '');
}).then(function() {
return lkl.fs.chmodAsync(file, mode);
});
};
before(function() {
this.doesNotExist = path.join(this.mountpoint, '__this_should_not_exist');
this.readOnlyFile = path.join(this.mountpoint, 'read_only_file');
this.readWriteFile = path.join(this.mountpoint, 'read_write_file');
return Promise.all([
createFileWithPerms(this.readOnlyFile, 0o444),
createFileWithPerms(this.readWriteFile, 0o666)
]);
});
it('should be able to create 20 files at once', function(done) {
const fname = path.join(this.mountpoint, 'file_number_');
const promises = [];
for (let i=0; i < 20; i++) {
promises.push(createFileWithPerms(fname + i, 0o666));
}
return Promise.all(promises)
.then(function() {
done();
});
});
it('non existent file', function(done) {
lkl.fs.access(this.doesNotExist, function(err) {
assert.notEqual(err, null, 'error should exist');
assert.strictEqual(err.code, 'ENOENT');
done();
});
});
it('should not return an error for readonly files', function(done) {
lkl.fs.access(this.readOnlyFile, fs.F_OK | fs.R_OK, function(err) {
assert.strictEqual(err, null, 'error should not exist');
done();
});
});
});
describe('.fstat()', function() {
it('should return a Stats object', function(done) {
let fd0, fd1;
const self = this;
lkl.fs.openAsync(path.join(self.mountpoint, 'petrosagg2'), constants.O_RDONLY)
.then(function(fd) {
fd0 = fd;
return lkl.fs.fstatAsync(fd0);
})
.then(function(stats) {
assert.strictEqual(stats.isFile(), true);
assert.strictEqual(stats.isDirectory(), false);
assert.strictEqual(stats.isBlockDevice(), false);
assert.strictEqual(stats.isCharacterDevice(), false);
assert.strictEqual(stats.isSymbolicLink(), false);
assert.strictEqual(stats.isFIFO(), false);
assert.strictEqual(stats.isSocket(), false);
assert.strictEqual(stats.mode.toString(8), '100644');
return lkl.fs.closeAsync(fd0);
})
.then(function() {
return lkl.fs.openAsync(path.join(self.mountpoint, 'lost+found'), constants.O_RDONLY);
})
.then(function(fd) {
fd1 = fd;
return lkl.fs.fstatAsync(fd1);
})
.then(function(stats) {
assert.strictEqual(stats.isFile(), false);
assert.strictEqual(stats.isDirectory(), true);
assert.strictEqual(stats.isBlockDevice(), false);
assert.strictEqual(stats.isCharacterDevice(), false);
assert.strictEqual(stats.isSymbolicLink(), false);
assert.strictEqual(stats.isFIFO(), false);
assert.strictEqual(stats.isSocket(), false);
assert.strictEqual(stats.mode.toString(8), '40700');
return lkl.fs.closeAsync(fd1);
})
.then(function() {
done();
});
});
});
describe('.writeFile() and .readFile()', function() {
it('should write and read files', function(done) {
const fpath = path.join(this.mountpoint, 'this_is_a_filename');
const content = 'some content 🗺';
let fd;
lkl.fs.writeFileAsync(fpath, content)
.then(function() {
return lkl.fs.readFileAsync(fpath, 'utf8');
})
.then(function(readContent) {
assert.strictEqual(content, readContent, 'should read what it has written');
})
.then(function() {
return lkl.fs.openAsync(fpath, constants.O_RDONLY);
})
.then(function(_fd) {
fd = _fd;
return lkl.fs.fstatAsync(fd);
})
.then(function(stats) {
const now = Date.now();
assert(now - stats.atime.getTime() < 10000, 'access time is correct');
assert(now - stats.mtime.getTime() < 10000, 'modification time is correct');
assert(now - stats.ctime.getTime() < 10000, 'creation time is correct');
})
.then(function() {
return lkl.fs.closeAsync(fd);
})
.then(done);
});
});
});
});