-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.spec.js
More file actions
535 lines (511 loc) · 18.7 KB
/
index.spec.js
File metadata and controls
535 lines (511 loc) · 18.7 KB
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
const should = require('chai').should();
const AggregionBundle = require('./index');
const path = require('path');
const crypto = require('crypto');
const temp = require('temp');
const fs = require('fs');
const fsExtra = require('fs-extra');
const exec = require('child_process').exec;
describe('AggregionBundle', () => {
const testBundlePath = temp.path() + '.agb';
const bundleInfo = new Buffer(JSON.stringify({
contentId: 1
}), 'UTF-8');
const bundleProperties = new Buffer(JSON.stringify({
main_file: 'test.dat'
}), 'UTF-8');
const createBundle = () => {
return new AggregionBundle({
path: testBundlePath
});
};
const fillBundle = (bundle) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(256, (err, buff) => {
if (err) {
return reject(err);
}
let testFiles = [];
for (let i = 0; i < 100; i++) {
let depth = i % 4;
let path = '';
for (let j = 0; j < depth; j++) {
path += `dir${i}_${j}/`;
}
path += `file${i}.dat`;
let testFile = {
name: path,
data: buff,
props: new Buffer(path, 'UTF-8')
};
testFiles.push(testFile);
}
let testNames = testFiles.map((f) => f.name);
Promise.resolve()
.then(() => {
return bundle.setBundleInfoData(bundleInfo);
})
.then(() => {
return bundle.setBundlePropertiesData(bundleProperties);
})
.then(() => {
let filePromises = [];
testFiles.forEach((f) => {
filePromises.push(Promise.resolve()
.then(() => {
return bundle.createFile(f.name);
})
.then((fd) => {
return bundle
.writeFileBlock(fd, f.data)
.then(() => {
return bundle.writeFilePropertiesData(fd, f.props);
});
})
);
});
return Promise.all(filePromises);
})
.then(() => {
resolve({
testNames
});
})
.catch(reject);
});
});
};
describe('#constructor', () => {
it('should open bundle', () => {
try {
let bundle;
should.not.throw(() => {
bundle = createBundle();
bundle.close();
});
} finally {
fs.unlinkSync(testBundlePath);
}
});
it('should throw if invalid arguments passed', () => {
should.throw(() => new AggregionBundle());
});
});
describe('#getFiles', () => {
it('should return valid list of files', (done) => {
let bundle = createBundle();
fillBundle(bundle)
.then(({testNames}) => {
return bundle.getFiles()
.then((bundleFiles) => {
bundleFiles.should.include.members(testNames);
testNames.should.include.members(bundleFiles);
bundle.close();
done();
});
})
.catch(done);
});
});
describe('#getBundleInfoData', () => {
it('should return valid info', (done) => {
let bundle = createBundle();
bundle
.getBundleInfoData()
.then((data) => {
bundleInfo.compare(data).should.equal(0);
bundle.close();
done();
});
});
});
describe('#setBundleInfoData', () => {
it('should set bundle info', (done) => {
let tempPath = temp.path() + '.agb';
let bundle = new AggregionBundle({
path: tempPath
});
bundle
.setBundleInfoData(bundleInfo)
.then(() => {
bundle.close();
let bundle2 = new AggregionBundle({
path: tempPath
});
return bundle2
.getBundleInfoData()
.then((data) => {
bundleInfo.compare(data).should.equal(0);
bundle2.close();
});
})
.catch(done)
.then(() => {
fs.unlinkSync(tempPath);
done();
});
});
});
describe('#getBundlePropertiesData', () => {
it('should return valid properties', (done) => {
let bundle = createBundle();
bundle
.getBundlePropertiesData()
.then((data) => {
bundleProperties.compare(data).should.equal(0);
bundle.close();
fs.unlinkSync(testBundlePath);
done();
});
});
});
describe('#setBundlePropertiesData', () => {
it('should set bundle properties', (done) => {
let tempPath = temp.path() + '.agb';
let bundle = new AggregionBundle({
path: tempPath
});
bundle
.setBundlePropertiesData(bundleProperties)
.then(() => {
bundle.close();
let bundle2 = new AggregionBundle({
path: tempPath
});
return bundle2
.getBundlePropertiesData()
.then((data) => {
bundleProperties.compare(data).should.equal(0);
bundle2.close();
});
})
.catch(done)
.then(() => {
fs.unlinkSync(tempPath);
done();
});
});
});
describe('#createFile', () => {
it('should create file in the new bundle', (done) => {
let tempPath = temp.path() + '.agb';
let bundle = new AggregionBundle({
path: tempPath
});
const filePath = 'dir1/dir2/file.dat';
bundle
.createFile(filePath)
.then(() => {
bundle.close();
let bundle2 = new AggregionBundle({
path: tempPath
});
return bundle2
.getFiles()
.then((files) => {
files.should.have.lengthOf(1);
files[0].should.equal(filePath);
bundle2.close();
});
})
.catch(done)
.then(() => {
fs.unlinkSync(tempPath);
done();
});
});
it('should append file to the existing bundle', (done) => {
let tempPath = temp.path() + '.agb';
let bundle = new AggregionBundle({
path: tempPath
});
fillBundle(bundle)
.then(() => {
bundle.close();
return new AggregionBundle({
path: tempPath
});
})
.then((bundle) => {
const filePath = 'dir1/dir2/file.dat';
return bundle
.createFile(filePath)
.then(() => {
bundle.close();
let bundle2 = new AggregionBundle({
path: tempPath
});
return bundle2
.getFiles()
.then((files) => {
files.should.have.lengthOf(101);
bundle2.close();
});
});
})
.catch((e) => {
done(e);
})
.then(() => {
fs.unlinkSync(tempPath);
done();
});
});
});
describe('#openFile', () => {
it('should open all files in the bundle', (done) => {
let bundle = createBundle();
fillBundle(bundle)
.then(({testNames}) => {
let fds = [];
testNames.forEach((path, i) => {
let fd = bundle.openFile(path);
fd.should.not.be.undefined;
fds.should.not.include(fd);
fds.push(fd);
});
bundle.close();
fs.unlinkSync(testBundlePath);
done();
})
.catch(done);
});
});
describe('#deleteFile', () => {
it('should delete file', (done) => {
let tempPath = temp.path() + '.agb';
let bundle = new AggregionBundle({
path: tempPath
});
fillBundle(bundle)
.then(() => {
let filesCount;
return bundle
.getFiles()
.then((files) => {
filesCount = files.length;
bundle.deleteFile(files[0]);
return bundle.getFiles();
})
.then((files) => {
files.length.should.equal(filesCount - 1);
bundle.close();
})
.catch(done)
.then(() => {
fs.unlinkSync(tempPath);
done();
});
})
.catch(done);
});
});
describe('#getFileSize', () => {
it('should return valid file size', (done) => {
let tempPath = temp.path() + '.agb';
let bundle = new AggregionBundle({
path: tempPath
});
const filePath = 'dir1/dir2/file.dat';
const data = new Buffer('testString', 'UTF-8');
bundle
.createFile(filePath)
.then((fd) => {
return bundle.writeFileBlock(fd, data);
})
.then(() => {
bundle.getFileSize(filePath).should.equal(data.length);
bundle.close();
})
.catch(done)
.then(() => {
fs.unlinkSync(tempPath);
done();
});
});
});
describe('#seekFile', () => {
it('should seek file and return valid block', (done) => {
let tempPath = temp.path() + '.agb';
let bundle = new AggregionBundle({
path: tempPath
});
const filePath = 'dir1/dir2/file.dat';
const data = new Buffer('1234567890', 'ascii');
const expectedData = new Buffer('67890', 'ascii');
bundle
.createFile(filePath)
.then((fd) => {
return bundle.writeFileBlock(fd, data);
})
.then(() => {
let fd2 = bundle.openFile(filePath);
bundle.seekFile(fd2, 5);
return bundle.readFileBlock(fd2, expectedData.length);
})
.then((readData) => {
expectedData.compare(readData).should.equal(0);
bundle.close();
})
.catch(done)
.then(() => {
fs.unlinkSync(tempPath);
done();
});
});
});
describe('#readFileBlock', () => {
it('should read file block with real size', (done) => {
let tempPath = temp.path() + '.agb';
let bundle = new AggregionBundle({
path: tempPath
});
const filePath = 'dir1/dir2/file.dat';
const data = new Buffer('1234567890', 'ascii');
bundle
.createFile(filePath)
.then((fd) => {
return bundle.writeFileBlock(fd, data);
})
.then(() => {
let fd2 = bundle.openFile(filePath);
const bufSize = 1024;
return bundle.readFileBlock(fd2, bufSize);
})
.then((readData) => {
data.compare(readData).should.equal(0);
bundle.close();
})
.catch(done)
.then(() => {
fs.unlinkSync(tempPath);
done();
});
});
});
describe('#readFilePropertiesData', () => {
it('should read file properties', (done) => {
let bundle = createBundle();
fillBundle(bundle)
.then(() => {
let testProps;
return bundle
.getFiles()
.then((files) => {
let file = files[0];
testProps = new Buffer(file, 'UTF-8');
let fd = bundle.openFile(file);
return bundle.readFilePropertiesData(fd);
})
.then((readProps) => {
testProps.compare(readProps).should.equal(0);
bundle.close();
fs.unlinkSync(testBundlePath);
done();
});
})
.catch(done);
});
});
describe('#writeFile', () => {
it('should write block that then will be readable and equal to wrote', (done) => {
let tempPath = temp.path() + '.agb';
let bundle = new AggregionBundle({
path: tempPath
});
const filePath = 'dir1/dir2/file.dat';
const data = new Buffer('testString', 'UTF-8');
bundle
.createFile(filePath)
.then((fd) => {
return bundle.writeFileBlock(fd, data);
})
.then(() => {
let fd2 = bundle.openFile(filePath);
return bundle.readFileBlock(fd2, data.length);
})
.then((readData) => {
data.compare(readData).should.equal(0);
bundle.close();
})
.catch(done)
.then(() => {
fs.unlinkSync(tempPath);
done();
});
});
});
describe('#writeFilePropertiesData', () => {
it('should write properties that then will be readable and equal to wrote', (done) => {
let tempPath = temp.path() + '.agb';
let bundle = new AggregionBundle({
path: tempPath
});
const filePath = 'dir1/dir2/file.dat';
const data = new Buffer('testString', 'utf8');
let bundle2;
bundle
.createFile(filePath)
.then((fd) => {
return bundle.writeFilePropertiesData(fd, data);
})
.then(() => {
bundle.close();
bundle2 = new AggregionBundle({
path: tempPath,
readonly: true
});
let fd2 = bundle2.openFile(filePath);
return bundle2.readFilePropertiesData(fd2);
})
.then((readData) => {
data.compare(readData).should.equal(0);
bundle2.close();
})
.catch(done)
.then(() => {
fs.unlinkSync(tempPath);
done();
});
});
it('should really write all data to the disk', (done) => {
let tempPath = temp.path() + '.agb';
let bundle = new AggregionBundle({
path: tempPath
});
const filePath = 'dir1/dir2/file.dat';
const data = new Buffer('testString', 'UTF-8');
bundle
.createFile(filePath)
.then((fd) => {
return bundle.writeFilePropertiesData(fd, data);
})
.then(() => {
bundle.close();
let script = path.join(__dirname, './utils/readbundle.js');
return new Promise((resolve, reject) => {
let run = `node ${script} fileprops -p ${filePath} ${tempPath}`;
exec(run, (err, stdout, stderr) => {
fs.unlinkSync(tempPath);
if (err) {
return reject(err);
}
let hex = stdout.replace('\n', '');
hex.should.not.be.empty;
try {
let buf = new Buffer(hex, 'hex');
resolve(buf);
} catch (e) {
reject(e);
}
});
});
})
.then((readData) => {
data.compare(readData).should.equal(0);
done();
})
.catch(done);
});
});
});