From 497906a670eebe1c2d7183689d803042673e294f Mon Sep 17 00:00:00 2001 From: Ryan Emery Date: Wed, 21 Aug 2019 14:28:32 -0700 Subject: [PATCH 1/2] fix: integration silent errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The cli’s for the integration packages could silently error. The logic is wrapped in an immediately invoked function expression. The function is async, and if there is an error this results in an unhandled promise rejection. Catch the error, log, and exit with a non-zero code. I also cleaned up some of the path expressions, and removed the ts-ignore. --- .../src/build_decrypt_fixtures.ts | 2 +- .../src/build_encrypt_fixtures.ts | 2 +- modules/integration-browser/src/cli.ts | 24 +++++++++++-------- modules/integration-node/src/cli.ts | 10 ++++---- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/modules/integration-browser/src/build_decrypt_fixtures.ts b/modules/integration-browser/src/build_decrypt_fixtures.ts index cec58f457..f419ecee7 100644 --- a/modules/integration-browser/src/build_decrypt_fixtures.ts +++ b/modules/integration-browser/src/build_decrypt_fixtures.ts @@ -27,7 +27,7 @@ import { DecryptManifestList } from './types' // eslint-disable-line no-unused-v * 1. The code is not tied to a specific copy of the manifest information * 2. The tests can be run on a subset of tests for debugging. */ -export async function buildDecryptFixtures (fixtures: string, vectorFile: string, testName: string, slice: string) { +export async function buildDecryptFixtures (fixtures: string, vectorFile: string, testName?: string, slice?: string) { const [start = 0, end = 9999] = (slice || '').split(':').map(n => parseInt(n, 10)) const centralDirectory = await Open.file(vectorFile) diff --git a/modules/integration-browser/src/build_encrypt_fixtures.ts b/modules/integration-browser/src/build_encrypt_fixtures.ts index a7e4863ff..b091fcd1e 100644 --- a/modules/integration-browser/src/build_encrypt_fixtures.ts +++ b/modules/integration-browser/src/build_encrypt_fixtures.ts @@ -34,7 +34,7 @@ import got from 'got' * 1. The code is not tied to a specific copy of the manifest information * 2. The tests can be run on a subset of tests for debugging. */ -export async function buildEncryptFixtures (fixtures: string, manifestFile: string, keyFile: string, testName: string, slice: string) { +export async function buildEncryptFixtures (fixtures: string, manifestFile: string, keyFile: string, testName?: string, slice?: string) { const [start = 0, end = 9999] = (slice || '').split(':').map(n => parseInt(n, 10)) const { tests, plaintexts }: EncryptManifestList = await getParsedJSON(manifestFile) const { keys }: KeyList = await getParsedJSON(keyFile) diff --git a/modules/integration-browser/src/cli.ts b/modules/integration-browser/src/cli.ts index aace54ad9..a2368e2fc 100644 --- a/modules/integration-browser/src/cli.ts +++ b/modules/integration-browser/src/cli.ts @@ -73,22 +73,22 @@ if (!existsSync(fixtures)) { } ;(async (argv) => { - const { _: [ command ], testName, slice, karma, decryptOracle = '' } = argv + const { _: [ command ], testName, slice, karma } = argv writeFileSync(`${fixtures}/decrypt_tests.json`, JSON.stringify([])) writeFileSync(`${fixtures}/encrypt_tests.json`, JSON.stringify([])) - writeFileSync(`${fixtures}/decrypt_oracle.json`, JSON.stringify(decryptOracle)) if (command === 'decrypt') { - const { vectorFile } = argv - const vectorPath = join(__dirname, vectorFile as string) - if (!existsSync(vectorPath)) throw new Error(`No file found at ${vectorPath}`) - // @ts-ignore - await buildDecryptFixtures(fixtures, vectorFile, testName, slice) + // It is not clear how to get yargs/typescript to play nicely with sub commands + const { vectorFile } = argv as unknown as { vectorFile: string} + if (!existsSync(vectorFile)) throw new Error(`No file found at ${vectorFile}`) + await buildDecryptFixtures(fixtures, vectorFile as string, testName, slice) } else if (command === 'encrypt') { - const { manifestFile, keyFile } = argv - // @ts-ignore - await buildEncryptFixtures(fixtures, manifestFile, keyFile, testName, slice) + // It is not clear how to get yargs/typescript to play nicely with sub commands + const { manifestFile, keyFile, decryptOracle } = argv as unknown as { manifestFile: string, keyFile: string, decryptOracle: string} + writeFileSync(`${fixtures}/decrypt_oracle.json`, JSON.stringify(decryptOracle)) + + await buildEncryptFixtures(fixtures, manifestFile as string, keyFile as string, testName, slice) } else { console.log(`Unknown command ${command}`) cli.showHelp() @@ -101,3 +101,7 @@ if (!existsSync(fixtures)) { }) } })(cli.argv) +.catch(err => { + console.log(err) + process.exit(1) +}) diff --git a/modules/integration-node/src/cli.ts b/modules/integration-node/src/cli.ts index 2c89de560..fcd4c473c 100644 --- a/modules/integration-node/src/cli.ts +++ b/modules/integration-node/src/cli.ts @@ -64,12 +64,10 @@ const cli = yargs /* I set the result to 1 so that if I fall through the exit condition is a failure */ let result = 1 if (command === 'decrypt') { - const { vectorFile } = argv - // @ts-ignore + const { vectorFile } = argv as unknown as { vectorFile: string} result = await integrationDecryptTestVectors(vectorFile, tolerateFailures, testName) } else if (command === 'encrypt') { - const { manifestFile, keyFile, decryptOracle } = argv - // @ts-ignore + const { manifestFile, keyFile, decryptOracle } = argv as unknown as { manifestFile: string, keyFile: string, decryptOracle: string} result = await integrationEncryptTestVectors(manifestFile, keyFile, decryptOracle, tolerateFailures, testName) } else { console.log(`Unknown command ${command}`) @@ -78,3 +76,7 @@ const cli = yargs if (result) process.exit(result) })(cli.argv) +.catch(err => { + console.log(err) + process.exit(1) +}) From 460b47c8cc8b1a409f02754377e83dbbb33b5336 Mon Sep 17 00:00:00 2001 From: Ryan Emery Date: Wed, 21 Aug 2019 14:40:18 -0700 Subject: [PATCH 2/2] lint --- modules/integration-browser/src/cli.ts | 8 ++++---- modules/integration-node/src/cli.ts | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/modules/integration-browser/src/cli.ts b/modules/integration-browser/src/cli.ts index a2368e2fc..c589c7806 100644 --- a/modules/integration-browser/src/cli.ts +++ b/modules/integration-browser/src/cli.ts @@ -101,7 +101,7 @@ if (!existsSync(fixtures)) { }) } })(cli.argv) -.catch(err => { - console.log(err) - process.exit(1) -}) + .catch(err => { + console.log(err) + process.exit(1) + }) diff --git a/modules/integration-node/src/cli.ts b/modules/integration-node/src/cli.ts index fcd4c473c..0186cb873 100644 --- a/modules/integration-node/src/cli.ts +++ b/modules/integration-node/src/cli.ts @@ -76,7 +76,7 @@ const cli = yargs if (result) process.exit(result) })(cli.argv) -.catch(err => { - console.log(err) - process.exit(1) -}) + .catch(err => { + console.log(err) + process.exit(1) + })