Skip to content

Commit 426741a

Browse files
committed
Fix unrar unit tests. Remove RAR5 test files. Properly error on RAR5 files for issue #25
1 parent ede9c6a commit 426741a

File tree

8 files changed

+39
-21
lines changed

8 files changed

+39
-21
lines changed

archive/decompress.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,19 +352,20 @@ export function getUnarchiver(ab, options = {}) {
352352

353353
// import * as fs from 'node:fs';
354354
// async function main() {
355-
// const nodeBuf = fs.readFileSync(`./action-1.cbz`);
355+
// const nodeBuf = fs.readFileSync(`./tests/archive-testfiles/archive-rar-store.rar`);
356356
// const ab = nodeBuf.buffer.slice(nodeBuf.byteOffset, nodeBuf.byteOffset + nodeBuf.length);
357357
// const then = Date.now();
358-
// const zipper = new Unzipper(ab, {debug: true});
359-
// zipper.addEventListener('extract', evt => {
358+
// const unarchiver = getUnarchiver(ab, {debug: true})
359+
// unarchiver.addEventListener('extract', evt => {
360+
// console.dir(evt);
360361
// const f = evt.unarchivedFile;
361362
// fs.writeFileSync(f.filename, Buffer.from(f.fileData));
362363
// });
363-
// zipper.addEventListener('finish', evt => {
364+
// unarchiver.addEventListener('finish', evt => {
364365
// console.dir(evt);
365366
// console.log(`Took ${(Date.now() - then)}ms`);
366367
// });
367-
// await zipper.start();
368+
// await unarchiver.start();
368369
// }
369370

370371
// main();

archive/unrar.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,19 +1343,20 @@ class RarLocalFile {
13431343
function unrar_start() {
13441344
let bstream = bytestream.tee();
13451345
const header = new RarVolumeHeader(bstream);
1346-
if (header.crc == 0x6152 &&
1347-
header.headType == 0x72 &&
1348-
header.flags.value == 0x1A21 &&
1349-
header.headSize == 7) {
1350-
if (logToConsole) {
1351-
info('Found RAR signature');
1352-
}
1346+
if (header.crc == 0x6152 && header.headType == 0x72 && header.flags.value == 0x1A21) {
1347+
if (header.headSize == 7) {
1348+
if (logToConsole) {
1349+
info('Found RAR signature');
1350+
}
13531351

1354-
const mhead = new RarVolumeHeader(bstream);
1355-
if (mhead.headType != MAIN_HEAD) {
1356-
info('Error! RAR did not include a MAIN_HEAD header');
1357-
} else {
1358-
bytestream = bstream.tee();
1352+
const mhead = new RarVolumeHeader(bstream);
1353+
if (mhead.headType != MAIN_HEAD) {
1354+
info('Error! RAR did not include a MAIN_HEAD header');
1355+
} else {
1356+
bytestream = bstream.tee();
1357+
}
1358+
} else if (header.headSize === 0x107) {
1359+
throw 'Error! RAR5 files not supported yet. See https://github.com/codedread/bitjs/issues/25';
13591360
}
13601361
}
13611362
}

tests/archive-decompress.spec.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ const INPUT_FILES = [
1313
];
1414

1515
const ARCHIVE_FILES = [
16+
// rar a -m0 -ma4 archive-rar-store.rar sample*
1617
'archive-rar-store.rar',
18+
// rar a -m3 -ma4 archive-rar-default.rar sample*
1719
'archive-rar-default.rar',
20+
// rar a -m5 -ma4 archive-rar-smaller.rar sample*
1821
'archive-rar-smaller.rar',
19-
'archive-rar-ma4.rar',
20-
'archive-rar-ma5.rar',
2122
'archive-tar.tar',
2223
'archive-zip-store.zip',
2324
'archive-zip-faster.zip',
@@ -36,15 +37,23 @@ describe('bitjs.archive.decompress', () => {
3637
}
3738
});
3839

40+
afterEach(() => {
41+
// Delete the unarchived files that were written to disk.
42+
INPUT_FILES.forEach(fn => fs.unlink(fn, () => {}));
43+
});
44+
3945
for (const outFile of ARCHIVE_FILES) {
40-
it(outFile, (done) => {
46+
it(outFile, async () => {
4147
const bufs = new Map(inputArrayBuffers);
4248
const nodeBuf = fs.readFileSync(`${PATH}${outFile}`);
4349
const ab = nodeBuf.buffer.slice(nodeBuf.byteOffset, nodeBuf.byteOffset + nodeBuf.length);
4450
let unarchiver = getUnarchiver(ab);
4551
expect(unarchiver instanceof Unarchiver).equals(true);
52+
let extractEvtFiredForAddEventListener = false;
53+
let extractEvtFiredForOnExtract = false;
4654

4755
unarchiver.addEventListener('extract', evt => {
56+
extractEvtFiredForAddEventListener = true;
4857
const {filename, fileData} = evt.unarchivedFile;
4958
expect(bufs.has(filename)).equals(true);
5059
const ab = bufs.get(filename);
@@ -55,7 +64,14 @@ describe('bitjs.archive.decompress', () => {
5564
// Remove the value from the map so that it is only used once.
5665
bufs.delete(filename);
5766
});
58-
unarchiver.start().then(() => { done() });
67+
unarchiver.onExtract(evt => {
68+
extractEvtFiredForOnExtract = true;
69+
expect(evt.unarchivedFile.filename.length > 0).equals(true);
70+
})
71+
72+
await unarchiver.start();
73+
expect(extractEvtFiredForAddEventListener).equals(true);
74+
expect(extractEvtFiredForOnExtract).equals(true);
5975
});
6076
}
6177
});
3 Bytes
Binary file not shown.
-509 Bytes
Binary file not shown.
-506 Bytes
Binary file not shown.
3 Bytes
Binary file not shown.
1 Byte
Binary file not shown.

0 commit comments

Comments
 (0)