test(check-bundle): Cover invalid archive error with diagnostic location
MODIFY
packages/check-bundle/package.json
packages/check-bundle/package.json
"@endo/bundle-source": "^2.2.0",
"@endo/eslint-config": "^0.4.10",
"@endo/init": "^0.5.41",
+ "@endo/zip": "^0.2.25",
"ava": "^3.12.1",
"babel-eslint": "^10.0.3",
"c8": "^7.7.3",

MODIFY
packages/check-bundle/src/json.js
packages/check-bundle/src/json.js
// @ts-check
+// For enquoting strings
+const q = JSON.stringify;
+
/**
* Parses JSON and, if necessary, throws exceptions that include the location
* of the offending file.
@@ -12,7 +15,7 @@ export const parseLocatedJson = (source, location) => {
return JSON.parse(source);
} catch (error) {
if (error instanceof SyntaxError) {
- throw new SyntaxError(`Cannot parse JSON from ${location}, ${error}`);
+ throw new SyntaxError(`Cannot parse JSON from ${q(location)}, ${error}`);
}
throw error;
}

MODIFY
packages/check-bundle/test/test-check-bundle.js
packages/check-bundle/test/test-check-bundle.js
@@ -6,6 +6,8 @@ import * as fs from 'fs';
import * as url from 'url';
import * as crypto from 'crypto';
import bundleSource from '@endo/bundle-source';
+import { ZipWriter } from '@endo/zip';
+import { encodeBase64 } from '@endo/base64';
import { checkBundle } from '../lite.js';
import {
checkBundleBytes,
@@ -107,3 +109,23 @@ test('bundle and hash bogus package', async t => {
message: `checkBundle cannot determine hash of bundle with unrecognized moduleFormat "bogus"`,
});
});
+
+test('empty bundle is invalid', async t => {
+ const writer = new ZipWriter();
+ const bytes = writer.snapshot();
+ const endoZipBase64 = encodeBase64(bytes);
+ const bundle = Object.freeze({
+ moduleFormat: 'endoZipBase64',
+ endoZipBase64,
+ endoZipBase64Sha512:
+ '00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
+ });
+ await t.throwsAsync(
+ checkBundle(bundle, computeSha512, 'empty.zip'),
+ {
+ message:
+ 'Archive failed sanity check: should contain at least a compartment map file and one module file in "empty.zip"',
+ },
+ 'empty.zip',
+ );
+});

test(check-bundle): Cover invalid archive error with diagnostic location