Skip to content

Commit

Permalink
Added: tests for StringItem with null bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinhammond committed Oct 10, 2023
1 parent c3ea6c0 commit db84980
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions test/zim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,24 @@ describe("Archive", () => {
)
);

// test blobs containing null bytes
const null_blobs = [
new StringItem(
'null_blob_1',

Check failure on line 235 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Jammy (18)

Replace `'null_blob_1'` with `"null_blob_1"`

Check failure on line 235 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Focal (18)

Replace `'null_blob_1'` with `"null_blob_1"`

Check failure on line 235 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Focal (20)

Replace `'null_blob_1'` with `"null_blob_1"`

Check failure on line 235 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / macOS (18)

Replace `'null_blob_1'` with `"null_blob_1"`

Check failure on line 235 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / macOS (20)

Replace `'null_blob_1'` with `"null_blob_1"`
"application/octet-stream",
"null blob title 1",
{},
"\xffabc\x00123",

Check failure on line 239 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Jammy (18)

Delete `,`

Check failure on line 239 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Focal (18)

Delete `,`

Check failure on line 239 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Focal (20)

Delete `,`

Check failure on line 239 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / macOS (18)

Delete `,`

Check failure on line 239 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / macOS (20)

Delete `,`
),
new StringItem(
'null_blob_2',

Check failure on line 242 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Jammy (18)

Replace `'null_blob_2'` with `"null_blob_2"`

Check failure on line 242 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Focal (18)

Replace `'null_blob_2'` with `"null_blob_2"`

Check failure on line 242 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Focal (20)

Replace `'null_blob_2'` with `"null_blob_2"`

Check failure on line 242 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / macOS (18)

Replace `'null_blob_2'` with `"null_blob_2"`

Check failure on line 242 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / macOS (20)

Replace `'null_blob_2'` with `"null_blob_2"`
"application/octet-stream",
"null blob title 2",
{},
"abc 123 \x00",

Check failure on line 246 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Jammy (18)

Delete `,`

Check failure on line 246 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Focal (18)

Delete `,`

Check failure on line 246 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Focal (20)

Delete `,`

Check failure on line 246 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / macOS (18)

Delete `,`

Check failure on line 246 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / macOS (20)

Delete `,`
)

Check failure on line 247 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Jammy (18)

Insert `,`

Check failure on line 247 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Focal (18)

Insert `,`

Check failure on line 247 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Focal (20)

Insert `,`

Check failure on line 247 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / macOS (18)

Insert `,`

Check failure on line 247 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / macOS (20)

Insert `,`
];

// custom item
items.push(
...Array.from(Array(5).keys())
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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) {

Check failure on line 456 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Jammy (18)

Insert `·`

Check failure on line 456 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Focal (18)

Insert `·`

Check failure on line 456 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Focal (20)

Insert `·`

Check failure on line 456 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / macOS (18)

Insert `·`

Check failure on line 456 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / macOS (20)

Insert `·`
const contentProvider = nb.getContentProvider();
let feed = contentProvider.feed();

Check failure on line 458 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Jammy (18)

'feed' is never reassigned. Use 'const' instead

Check failure on line 458 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Focal (18)

'feed' is never reassigned. Use 'const' instead

Check failure on line 458 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Focal (20)

'feed' is never reassigned. Use 'const' instead

Check failure on line 458 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / macOS (18)

'feed' is never reassigned. Use 'const' instead

Check failure on line 458 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / macOS (20)

'feed' is never reassigned. Use 'const' instead
expect(feed).toBeDefined();
expect(feed.size).toBeGreaterThan(0);
if(nb.path === 'null_blob_1') {

Check failure on line 461 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Jammy (18)

Replace `(nb.path·===·'null_blob_1'` with `·(nb.path·===·"null_blob_1"`

Check failure on line 461 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Focal (18)

Replace `(nb.path·===·'null_blob_1'` with `·(nb.path·===·"null_blob_1"`

Check failure on line 461 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Focal (20)

Replace `(nb.path·===·'null_blob_1'` with `·(nb.path·===·"null_blob_1"`

Check failure on line 461 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / macOS (18)

Replace `(nb.path·===·'null_blob_1'` with `·(nb.path·===·"null_blob_1"`

Check failure on line 461 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / macOS (20)

Replace `(nb.path·===·'null_blob_1'` with `·(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') {

Check failure on line 465 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Jammy (18)

Replace `(nb.path·===·'null_blob_2'` with `·(nb.path·===·"null_blob_2"`

Check failure on line 465 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Focal (18)

Replace `(nb.path·===·'null_blob_2'` with `·(nb.path·===·"null_blob_2"`

Check failure on line 465 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / Ubuntu-Focal (20)

Replace `(nb.path·===·'null_blob_2'` with `·(nb.path·===·"null_blob_2"`

Check failure on line 465 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / macOS (18)

Replace `(nb.path·===·'null_blob_2'` with `·(nb.path·===·"null_blob_2"`

Check failure on line 465 in test/zim.test.ts

View workflow job for this annotation

GitHub Actions / macOS (20)

Replace `(nb.path·===·'null_blob_2'` with `·(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);
Expand Down

0 comments on commit db84980

Please sign in to comment.