From db849801e0e2cf12073ce09bc12d53174c777301 Mon Sep 17 00:00:00 2001 From: Kelvin Hammond Date: Tue, 10 Oct 2023 19:17:26 -0400 Subject: [PATCH] Added: tests for StringItem with null bytes --- test/zim.test.ts | 49 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/test/zim.test.ts b/test/zim.test.ts index 6eec2d4..ffda0c0 100644 --- a/test/zim.test.ts +++ b/test/zim.test.ts @@ -229,6 +229,24 @@ describe("Archive", () => { ) ); + // test blobs containing null bytes + const null_blobs = [ + new StringItem( + 'null_blob_1', + "application/octet-stream", + "null blob title 1", + {}, + "\xffabc\x00123", + ), + new StringItem( + 'null_blob_2', + "application/octet-stream", + "null blob title 2", + {}, + "abc 123 \x00", + ) + ]; + // custom item items.push( ...Array.from(Array(5).keys()) @@ -256,7 +274,7 @@ describe("Archive", () => { ); // all entries - const entries = items.concat(blobs); + const entries = items.concat(blobs, null_blobs); const meta = { test1: "test string 1", @@ -284,7 +302,7 @@ describe("Archive", () => { .configIndexing(true, "en") .startZimCreation(outFile); - for (const item of items.concat(blobs)) { + for (const item of entries) { await creator.addItem(item); } @@ -431,6 +449,33 @@ describe("Archive", () => { } }); + it("verifies that blobs containing and ending in null were stored correctly from the archive", () => { + const archive = new Archive(outFile); + expect(archive).toBeDefined(); + + for(const nb of null_blobs) { + const contentProvider = nb.getContentProvider(); + let feed = contentProvider.feed(); + expect(feed).toBeDefined(); + expect(feed.size).toBeGreaterThan(0); + if(nb.path === 'null_blob_1') { + // 4th byte is null, last byte is not + expect(feed.data[5]).toEqual(0); + expect(feed.data[feed.data.length - 1]).not.toEqual(0); + } else if(nb.path === 'null_blob_2') { + // last byte is null + expect(feed.data[feed.data.length - 1]).toEqual(0); + } + + const entry = Array.from(archive.findByPath(nb.path))[0]; + expect(entry).toBeDefined(); + expect(entry.title).toEqual(nb.title); + + expect(entry.item.data.size).toEqual(feed.data.length); + expect(entry.item.data.data).toEqual(feed.data); + } + }); + describe("Searcher", () => { it("searches the archive", () => { const archive = new Archive(outFile);