|
1 | | -;(function () { |
2 | | - var assert = require('assert'); |
3 | | - var path = require('path'); |
4 | | - var Zip = require('../../adm-zip'); |
5 | | - |
6 | | - testGoodCrc(); |
7 | | - testBadCrc(); |
8 | | - |
9 | | - // Good CRC |
10 | | - function testGoodCrc() { |
11 | | - var goodZip = new Zip(path.join(__dirname, 'good_crc.zip')); |
12 | | - var entries = goodZip.getEntries(); |
| 1 | +const assert = require('assert'); |
| 2 | +const path = require('path'); |
| 3 | +const Zip = require('../../adm-zip'); |
| 4 | +const rimraf = require('rimraf') |
| 5 | + |
| 6 | +describe('crc', () => { |
| 7 | + const destination = __dirname + '/xxx' |
| 8 | + |
| 9 | + beforeEach(done => rimraf(destination, done)) |
| 10 | + |
| 11 | + it('Good CRC', (done) => { |
| 12 | + const goodZip = new Zip(path.join(__dirname, 'good_crc.zip')); |
| 13 | + const entries = goodZip.getEntries(); |
13 | 14 | assert(entries.length === 1, 'Good CRC: Test archive contains exactly 1 file'); |
14 | 15 |
|
15 | | - var testFile = entries.filter(function (entry) { |
| 16 | + const testFile = entries.filter(function (entry) { |
16 | 17 | return entry.entryName === 'lorem_ipsum.txt'; |
17 | 18 | }); |
18 | 19 | assert(testFile.length === 1, 'Good CRC: lorem_ipsum.txt file exists as archive entry'); |
19 | 20 |
|
20 | | - var testFileEntryName = testFile[0].entryName; |
| 21 | + const testFileEntryName = testFile[0].entryName; |
21 | 22 | goodZip.readAsTextAsync(testFileEntryName, function (data, err) { |
22 | 23 | assert(!err, 'Good CRC: error object not present'); |
23 | 24 | assert(data && data.length, 'Good CRC: buffer not empty'); |
| 25 | + done(); |
24 | 26 | }); |
25 | | - } |
| 27 | + }); |
26 | 28 |
|
27 | | - // Bad CRC |
28 | | - function testBadCrc() { |
29 | | - var badZip = new Zip(path.join(__dirname, 'bad_crc.zip')); |
30 | | - var entries = badZip.getEntries(); |
| 29 | + it('Bad CRC', (done) => { |
| 30 | + const badZip = new Zip(path.join(__dirname, 'bad_crc.zip')); |
| 31 | + const entries = badZip.getEntries(); |
31 | 32 | assert(entries.length === 1, 'Bad CRC: Test archive contains exactly 1 file'); |
32 | 33 |
|
33 | | - var testFile = entries.filter(function (entry) { |
| 34 | + const testFile = entries.filter(function (entry) { |
34 | 35 | return entry.entryName === 'lorem_ipsum.txt'; |
35 | 36 | }); |
36 | 37 | assert(testFile.length === 1, 'Bad CRC: lorem_ipsum.txt file exists as archive entry'); |
37 | 38 |
|
38 | | - var testFileEntryName = testFile[0].entryName; |
| 39 | + const testFileEntryName = testFile[0].entryName; |
39 | 40 | badZip.readAsTextAsync(testFileEntryName, function (data, err) { |
40 | 41 | assert(data && data.length, 'Bad CRC: buffer not empty'); |
41 | 42 | assert(err, 'Bad CRC: error object present'); |
| 43 | + done(); |
42 | 44 | }); |
43 | | - } |
44 | | -})(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('CRC is not changed after re-created', () => { |
| 48 | + const goodZip = new Zip(path.join(__dirname, 'good_crc.zip')); |
| 49 | + const original = goodZip.getEntries()[0].header.crc; |
| 50 | + assert.equal(original, 3528145192); |
| 51 | + const newZipPath = destination + '/good_crc_new.zip'; |
| 52 | + goodZip.writeZip(newZipPath); |
| 53 | + const newZip = new Zip(newZipPath); |
| 54 | + const actual = newZip.getEntries()[0].header.crc; |
| 55 | + assert.equal(actual, original); |
| 56 | + }); |
| 57 | +}); |
0 commit comments