-
Notifications
You must be signed in to change notification settings - Fork 77
/
test.js
498 lines (465 loc) · 17.3 KB
/
test.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
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
var yauzl = require("../");
var zip64 = require("./zip64");
var rangeTest = require("./range-test");
var fs = require("fs");
var path = require("path");
var Pend = require("pend");
var util = require("util");
var child_process = require("child_process");
var Readable = require("stream").Readable;
var Writable = require("stream").Writable;
// this is the date i made the example zip files and their content files,
// so this timestamp will be earlier than all the ones stored in these test zip files
// (and probably all future zip files).
// no timezone awareness, because that's how MS-DOS rolls.
var earliestTimestamp = new Date(2014, 7, 18, 0, 0, 0, 0);
var pend = new Pend();
// 1 thing at a time for better determinism/reproducibility
pend.max = 1;
var args = process.argv.slice(2);
function shouldDoTest(testPath) {
if (args.length === 0) return true;
return args.indexOf(testPath) !== -1;
}
// success tests
listZipFiles([path.join(__dirname, "success"), path.join(__dirname, "wrong-entry-sizes")]).forEach(function(zipfilePath) {
if (!shouldDoTest(zipfilePath)) return;
var optionConfigurations = [
// you can find more options coverage in the failure tests.
{lazyEntries: true},
{lazyEntries: true, decodeStrings: false},
];
if (/\/wrong-entry-sizes\//.test(zipfilePath)) {
optionConfigurations.forEach(function(options) {
options.validateEntrySizes = false;
});
}
var openFunctions = [
function(testId, options, callback) { yauzl.open(zipfilePath, options, callback); },
function(testId, options, callback) { yauzl.fromBuffer(fs.readFileSync(zipfilePath), options, callback); },
function(testId, options, callback) { openWithRandomAccess(zipfilePath, options, true, testId, callback); },
function(testId, options, callback) { openWithRandomAccess(zipfilePath, options, false, testId, callback); },
];
openFunctions.forEach(function(openFunction, i) {
optionConfigurations.forEach(function(options, j) {
var testId = zipfilePath + "(" + ["fd", "buffer", "randomAccess", "minimalRandomAccess"][i] + "," + j + "): ";
var expectedPathPrefix = zipfilePath.replace(/\.zip$/, "");
var expectedArchiveContents = {};
var DIRECTORY = 1; // not a string
recursiveRead(".");
function recursiveRead(name) {
// windows support? whatever.
var name = name.replace(/\\/g, "/");
var key = addUnicodeSupport(name);
var realPath = path.join(expectedPathPrefix, name);
if (fs.statSync(realPath).isFile()) {
switch (path.basename(name)) {
case ".git_please_make_this_directory":
// ignore
break;
case ".dont_expect_an_empty_dir_entry_for_this_dir":
delete expectedArchiveContents[path.dirname(name)];
break;
default:
// normal file
expectedArchiveContents[key] = fs.readFileSync(realPath);
break;
}
} else {
if (name !== ".") expectedArchiveContents[key] = DIRECTORY;
fs.readdirSync(realPath).forEach(function(child) {
recursiveRead(path.join(name, child));
});
}
}
pend.go(function(zipfileCallback) {
openFunction(testId, options, function(err, zipfile) {
if (err) throw err;
zipfile.readEntry();
zipfile.on("entry", function(entry) {
var fileName = entry.fileName;
var fileComment = entry.fileComment;
if (options.decodeStrings === false) {
if (fileName.constructor !== Buffer) throw new Error(testId + "expected fileName to be a Buffer");
fileName = manuallyDecodeFileName(fileName);
fileComment = manuallyDecodeFileName(fileComment);
}
if (fileComment !== "") throw new Error(testId + "expected empty fileComment");
var messagePrefix = testId + fileName + ": ";
var timestamp = entry.getLastModDate();
if (timestamp < earliestTimestamp) throw new Error(messagePrefix + "timestamp too early: " + timestamp);
if (timestamp > new Date()) throw new Error(messagePrefix + "timestamp in the future: " + timestamp);
var fileNameKey = fileName.replace(/\/$/, "");
var expectedContents = expectedArchiveContents[fileNameKey];
if (expectedContents == null) {
throw new Error(messagePrefix + "not supposed to exist");
}
delete expectedArchiveContents[fileNameKey];
if (fileName !== fileNameKey) {
// directory
console.log(messagePrefix + "pass");
zipfile.readEntry();
} else {
var isEncrypted = entry.isEncrypted();
var isCompressed = entry.isCompressed();
if (/traditional-encryption/.test(zipfilePath) !== isEncrypted) {
throw new Error("expected traditional encryption in the traditional encryption test cases");
if (/traditional-encryption-and-compression/.test(zipfilePath) !== isCompressed) {
throw new Error("expected traditional encryption and compression in the traditional encryption and compression test case");
}
}
if (isEncrypted) {
zipfile.openReadStream(entry, {
decrypt: false,
decompress: isCompressed ? false : null,
}, onReadStream);
} else {
zipfile.openReadStream(entry, onReadStream);
}
function onReadStream(err, readStream) {
if (err) throw err;
var buffers = [];
readStream.on("data", function(data) {
buffers.push(data);
});
readStream.on("end", function() {
var actualContents = Buffer.concat(buffers);
// uh. there's no buffer equality check?
var equal = actualContents.toString("binary") === expectedContents.toString("binary");
if (!equal) {
throw new Error(messagePrefix + "wrong contents");
}
console.log(messagePrefix + "pass");
zipfile.readEntry();
});
readStream.on("error", function(err) {
throw err;
});
}
}
});
zipfile.on("end", function() {
for (var fileName in expectedArchiveContents) {
throw new Error(testId + fileName + ": missing file");
}
console.log(testId + "pass");
zipfileCallback();
});
zipfile.on("close", function() {
console.log(testId + "closed");
});
});
});
});
});
});
// failure tests
listZipFiles([path.join(__dirname, "failure")]).forEach(function(zipfilePath) {
if (!shouldDoTest(zipfilePath)) return;
var expectedErrorMessage = path.basename(zipfilePath).replace(/(_\d+)?\.zip$/, "");
var failedYet = false;
var emittedError = false;
pend.go(function(cb) {
var operationsInProgress = 0;
if (/invalid characters in fileName/.test(zipfilePath)) {
// this error can only happen when you specify an option
yauzl.open(zipfilePath, {strictFileNames: true}, onZipFile);
} else {
yauzl.open(zipfilePath, onZipFile);
}
return;
function onZipFile(err, zipfile) {
if (err) return checkErrorMessage(err);
zipfile.on("error", function(err) {
noEventsAllowedAfterError();
emittedError = true;
checkErrorMessage(err);
});
zipfile.on("entry", function(entry) {
noEventsAllowedAfterError();
// let's also try to read directories, cuz whatever.
operationsInProgress += 1;
zipfile.openReadStream(entry, function(err, stream) {
if (err) return checkErrorMessage(err);
stream.on("error", function(err) {
checkErrorMessage(err);
});
stream.on("data", function(data) {
// ignore
});
stream.on("end", function() {
doneWithSomething();
});
});
});
operationsInProgress += 1;
zipfile.on("end", function() {
noEventsAllowedAfterError();
doneWithSomething();
});
function doneWithSomething() {
operationsInProgress -= 1;
if (operationsInProgress !== 0) return;
if (!failedYet) {
throw new Error(zipfilePath + ": expected failure");
}
}
}
function checkErrorMessage(err) {
var actualMessage = err.message.replace(/[^0-9A-Za-z-]+/g, " ").trimRight();
if (actualMessage !== expectedErrorMessage) {
throw new Error(zipfilePath + ": wrong error message: " + actualMessage);
}
console.log(zipfilePath + ": pass");
failedYet = true;
operationsInProgress = -Infinity;
cb();
}
function noEventsAllowedAfterError() {
if (emittedError) throw new Error("events emitted after error event");
}
});
});
// fromRandomAccessReader with errors
pend.go(function(cb) {
util.inherits(TestRandomAccessReader, yauzl.RandomAccessReader);
function TestRandomAccessReader() {
yauzl.RandomAccessReader.call(this);
}
TestRandomAccessReader.prototype._readStreamForRange = function(start, end) {
var brokenator = new Readable();
brokenator._read = function(size) {
brokenator.emit("error", new Error("all reads fail"));
};
return brokenator;
};
var reader = new TestRandomAccessReader();
yauzl.fromRandomAccessReader(reader, 0x1000, function(err, zipfile) {
if (err.message === "all reads fail") {
console.log("fromRandomAccessReader with errors: pass");
cb();
} else {
throw err;
}
});
});
// read some entries, then close.
pend.go(function(cb) {
var prefix = "read some entries then close: ";
// this zip file should have at least 3 entries in it
yauzl.open(path.join(__dirname, "success/unicode.zip"), {lazyEntries: true}, function(err, zipfile) {
if (err) throw err;
var entryCount = 0;
zipfile.readEntry();
zipfile.on("entry", function(entry) {
entryCount += 1;
console.log(prefix + "entryCount: " + entryCount);
if (entryCount < 3) {
zipfile.readEntry();
} else if (entryCount === 3) {
zipfile.close();
console.log(prefix + "close()");
} else {
throw new Error(prefix + "read too many entries");
}
});
zipfile.on("close", function() {
console.log(prefix + "closed");
if (entryCount === 3) {
console.log(prefix + "pass");
cb();
} else {
throw new Error(prefix + "not enough entries read before closed");
}
});
zipfile.on("end", function() {
throw new Error(prefix + "we weren't supposed to get to the end");
});
zipfile.on("error", function(err) {
throw err;
});
});
});
// abort open read stream
pend.go(function(cb) {
var prefix = "abort open read stream: ";
yauzl.open(path.join(__dirname, "big-compression.zip"), {lazyEntries: true}, function(err, zipfile) {
if (err) throw err;
var doneWithStream = false;
zipfile.readEntry();
zipfile.on("entry", function(entry) {
zipfile.openReadStream(entry, function(err, readStream) {
var writer = new Writable();
var bytesSeen = 0;
writer._write = function(chunk, encoding, callback) {
bytesSeen += chunk.length;
if (bytesSeen < entry.uncompressedSize / 10) {
// keep piping a bit longer
callback();
} else {
// alright, i've seen enough.
doneWithStream = true;
console.log(prefix + "destroy()");
readStream.unpipe(writer);
readStream.destroy();
// now keep trying to use the fd
zipfile.readEntry();
}
};
readStream.pipe(writer);
});
});
zipfile.on("end", function() {
console.log(prefix + "end");
});
zipfile.on("close", function() {
console.log(prefix + "closed");
if (doneWithStream) {
console.log(prefix + "pass");
cb();
} else {
throw new Error(prefix + "closed prematurely");
}
});
zipfile.on("error", function(err) {
throw err;
});
});
});
// zip64
pend.go(zip64.runTest);
// openReadStream with range
pend.go(rangeTest.runTest);
// Make sure the examples run with crashing.
pend.go(function(cb) {
var examplesDir = path.join(__dirname, "../examples");
var zipfiles = listZipFiles([path.join(__dirname, "success")]);
var tmpDir = path.join(__dirname, ".tmp");
if (typeof fs.rmSync === "function") fs.rmSync(tmpDir, {recursive: true, force: true});
var parametersToTest = {
"compareCentralAndLocalHeaders.js": zipfiles,
"dump.js": zipfiles,
"promises.js": [null],
"unzip.js": zipfiles,
};
if (JSON.stringify(fs.readdirSync(examplesDir).sort()) !== JSON.stringify(Object.keys(parametersToTest).sort())) throw new Error("unexpected examples/ directory listing");
for (var f in parametersToTest) {
var args = parametersToTest[f];
var script = path.join(examplesDir, f);
if (f === "unzip.js" && typeof fs.rmSync !== "function") {
console.log("WARNING: skipping examples/unzip.js tests for node <14");
continue;
}
args.forEach(function(arg) {
var args = [path.resolve(script)];
var options = {
stdio: ["ignore", "ignore", "inherit"],
timeout: 10_000,
};
var testId;
if (arg != null) {
args.push(path.resolve(arg));
testId = `examples/${f} ${path.basename(arg)}: `;
} else {
testId = `examples/${f}: `;
}
// Handle special cases.
if (f === "dump.js" && /traditional-encryption/.exec(path.basename(arg))) {
args.push("--no-contents");
}
if (f === "unzip.js") {
if (/traditional-encryption/.exec(path.basename(arg))) return; // Can't do these.
// Quaranetine this in a temp directory.
fs.mkdirSync(tmpDir);
options.cwd = tmpDir;
}
process.stdout.write(testId);
var {status, error} = child_process.spawnSync("node", args, options);
if (status) error = new Error("child process return exit code " + status);
if (error) throw error;
if (f === "unzip.js") {
// Quaranetine this in a temp directory.
fs.rmSync(tmpDir, {recursive: true, force: true});
}
process.stdout.write("pass\n");
});
}
cb();
});
var done = false;
pend.wait(function() {
console.log("all done");
done = true;
});
process.on("exit", function(code) {
if (code === 0 && !done) {
throw new Error("premature exit. probably means an event never got fired.");
}
});
function listZipFiles(dirList) {
var zipfilePaths = [];
dirList.forEach(function(dir) {
fs.readdirSync(dir).filter(function(filepath) {
return /\.zip$/.exec(filepath);
}).forEach(function(name) {
zipfilePaths.push(path.relative(".", path.join(dir, name)));
});
});
zipfilePaths.sort();
return zipfilePaths;
}
function addUnicodeSupport(name) {
// reading and writing unicode filenames on mac is broken.
// we keep all our test data ascii, and then swap in the real names here.
// see https://github.com/thejoshwolfe/yauzl/issues/10
name = name.replace(/Turmion Katilot/g, "Turmion Kätilöt");
name = name.replace(/Mista veri pakenee/g, "Mistä veri pakenee");
name = name.replace(/qi ge fangjian/g, "七个房间");
return name;
}
function manuallyDecodeFileName(fileName) {
// file names in this test suite are always utf8 compatible.
fileName = fileName.toString("utf8");
fileName = fileName.replace("\\", "/");
if (fileName === "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f") {
// we're not doing the unicode path extra field decoding outside of yauzl.
// just hardcode this answer.
fileName = "七个房间.txt";
}
return fileName;
}
function openWithRandomAccess(zipfilePath, options, implementRead, testId, callback) {
util.inherits(InefficientRandomAccessReader, yauzl.RandomAccessReader);
function InefficientRandomAccessReader() {
yauzl.RandomAccessReader.call(this);
}
InefficientRandomAccessReader.prototype._readStreamForRange = function(start, end) {
return fs.createReadStream(zipfilePath, {start: start, end: end - 1});
};
if (implementRead) {
InefficientRandomAccessReader.prototype.read = function(buffer, offset, length, position, callback) {
fs.open(zipfilePath, "r", function(err, fd) {
if (err) throw err;
fs.read(fd, buffer, offset, length, position, function(err, bytesRead) {
if (bytesRead < length) throw new Error("unexpected EOF");
fs.close(fd, function(err) {
if (err) throw err;
callback();
});
});
});
};
}
InefficientRandomAccessReader.prototype.close = function(cb) {
console.log(testId + "close hook");
yauzl.RandomAccessReader.prototype.close.call(this, cb);
};
fs.stat(zipfilePath, function(err, stats) {
if (err) throw err;
var reader = new InefficientRandomAccessReader();
yauzl.fromRandomAccessReader(reader, stats.size, options, function(err, zipfile) {
if (err) throw err;
callback(null, zipfile);
});
});
}