Skip to content

Commit

Permalink
Fix unrar unit tests. Remove RAR5 test files. Properly error on RAR5 …
Browse files Browse the repository at this point in the history
…files for issue #25
  • Loading branch information
codedread committed Feb 3, 2024
1 parent ede9c6a commit 426741a
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 21 deletions.
11 changes: 6 additions & 5 deletions archive/decompress.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,19 +352,20 @@ export function getUnarchiver(ab, options = {}) {

// import * as fs from 'node:fs';
// async function main() {
// const nodeBuf = fs.readFileSync(`./action-1.cbz`);
// const nodeBuf = fs.readFileSync(`./tests/archive-testfiles/archive-rar-store.rar`);
// const ab = nodeBuf.buffer.slice(nodeBuf.byteOffset, nodeBuf.byteOffset + nodeBuf.length);
// const then = Date.now();
// const zipper = new Unzipper(ab, {debug: true});
// zipper.addEventListener('extract', evt => {
// const unarchiver = getUnarchiver(ab, {debug: true})
// unarchiver.addEventListener('extract', evt => {
// console.dir(evt);
// const f = evt.unarchivedFile;
// fs.writeFileSync(f.filename, Buffer.from(f.fileData));
// });
// zipper.addEventListener('finish', evt => {
// unarchiver.addEventListener('finish', evt => {
// console.dir(evt);
// console.log(`Took ${(Date.now() - then)}ms`);
// });
// await zipper.start();
// await unarchiver.start();
// }

// main();
25 changes: 13 additions & 12 deletions archive/unrar.js
Original file line number Diff line number Diff line change
Expand Up @@ -1343,19 +1343,20 @@ class RarLocalFile {
function unrar_start() {
let bstream = bytestream.tee();
const header = new RarVolumeHeader(bstream);
if (header.crc == 0x6152 &&
header.headType == 0x72 &&
header.flags.value == 0x1A21 &&
header.headSize == 7) {
if (logToConsole) {
info('Found RAR signature');
}
if (header.crc == 0x6152 && header.headType == 0x72 && header.flags.value == 0x1A21) {
if (header.headSize == 7) {
if (logToConsole) {
info('Found RAR signature');
}

const mhead = new RarVolumeHeader(bstream);
if (mhead.headType != MAIN_HEAD) {
info('Error! RAR did not include a MAIN_HEAD header');
} else {
bytestream = bstream.tee();
const mhead = new RarVolumeHeader(bstream);
if (mhead.headType != MAIN_HEAD) {
info('Error! RAR did not include a MAIN_HEAD header');
} else {
bytestream = bstream.tee();
}
} else if (header.headSize === 0x107) {
throw 'Error! RAR5 files not supported yet. See https://github.com/codedread/bitjs/issues/25';
}
}
}
Expand Down
24 changes: 20 additions & 4 deletions tests/archive-decompress.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ const INPUT_FILES = [
];

const ARCHIVE_FILES = [
// rar a -m0 -ma4 archive-rar-store.rar sample*
'archive-rar-store.rar',
// rar a -m3 -ma4 archive-rar-default.rar sample*
'archive-rar-default.rar',
// rar a -m5 -ma4 archive-rar-smaller.rar sample*
'archive-rar-smaller.rar',
'archive-rar-ma4.rar',
'archive-rar-ma5.rar',
'archive-tar.tar',
'archive-zip-store.zip',
'archive-zip-faster.zip',
Expand All @@ -36,15 +37,23 @@ describe('bitjs.archive.decompress', () => {
}
});

afterEach(() => {
// Delete the unarchived files that were written to disk.
INPUT_FILES.forEach(fn => fs.unlink(fn, () => {}));
});

for (const outFile of ARCHIVE_FILES) {
it(outFile, (done) => {
it(outFile, async () => {
const bufs = new Map(inputArrayBuffers);
const nodeBuf = fs.readFileSync(`${PATH}${outFile}`);
const ab = nodeBuf.buffer.slice(nodeBuf.byteOffset, nodeBuf.byteOffset + nodeBuf.length);
let unarchiver = getUnarchiver(ab);
expect(unarchiver instanceof Unarchiver).equals(true);
let extractEvtFiredForAddEventListener = false;
let extractEvtFiredForOnExtract = false;

unarchiver.addEventListener('extract', evt => {
extractEvtFiredForAddEventListener = true;
const {filename, fileData} = evt.unarchivedFile;
expect(bufs.has(filename)).equals(true);
const ab = bufs.get(filename);
Expand All @@ -55,7 +64,14 @@ describe('bitjs.archive.decompress', () => {
// Remove the value from the map so that it is only used once.
bufs.delete(filename);
});
unarchiver.start().then(() => { done() });
unarchiver.onExtract(evt => {
extractEvtFiredForOnExtract = true;
expect(evt.unarchivedFile.filename.length > 0).equals(true);
})

await unarchiver.start();
expect(extractEvtFiredForAddEventListener).equals(true);
expect(extractEvtFiredForOnExtract).equals(true);
});
}
});
Binary file modified tests/archive-testfiles/archive-rar-default.rar
Binary file not shown.
Binary file removed tests/archive-testfiles/archive-rar-ma4.rar
Binary file not shown.
Binary file removed tests/archive-testfiles/archive-rar-ma5.rar
Binary file not shown.
Binary file modified tests/archive-testfiles/archive-rar-smaller.rar
Binary file not shown.
Binary file modified tests/archive-testfiles/archive-rar-store.rar
Binary file not shown.

0 comments on commit 426741a

Please sign in to comment.