From be6c093c9f192f6c9ab0dcf21fe7e37e2a3709e3 Mon Sep 17 00:00:00 2001 From: armfazh Date: Tue, 2 Jul 2024 12:54:30 -0700 Subject: [PATCH] Adding type assertions for non-nullable variables. --- test/oprf.test.ts | 5 ++--- test/util.ts | 4 ++++ test/vectors.test.ts | 8 ++++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/test/oprf.test.ts b/test/oprf.test.ts index 9da2b38..85c19e2 100644 --- a/test/oprf.test.ts +++ b/test/oprf.test.ts @@ -21,7 +21,7 @@ import { } from '../src/index.js' import { describeCryptoTests } from './describeCryptoTests.js' -import { serdeClass } from './util.js' +import { expectToBeDefined, serdeClass } from './util.js' async function testBadProof( client: OPRFClient, @@ -30,10 +30,9 @@ async function testBadProof( crypto: CryptoProvider, suiteID: SuiteID ) { - if (!evaluation.proof) throw new Error('no evaluation exists') - const badEval = Evaluation.deserialize(suiteID, evaluation.serialize(), crypto) + expectToBeDefined(evaluation.proof) Object.assign(badEval, { proof: { s: evaluation.proof.c, c: evaluation.proof.c } }) await expect(client.finalize(finData, badEval)).rejects.toThrow(/proof failed/) } diff --git a/test/util.ts b/test/util.ts index aa9fed9..04b2d20 100644 --- a/test/util.ts +++ b/test/util.ts @@ -35,3 +35,7 @@ export function serdesEquals< const deser = deserializer.deserialize(ser) return t.isEqual(deser) } + +export function expectToBeDefined(v?: T): asserts v is NonNullable { + expect(v).toBeDefined() +} diff --git a/test/vectors.test.ts b/test/vectors.test.ts index adffe46..941b9aa 100644 --- a/test/vectors.test.ts +++ b/test/vectors.test.ts @@ -22,6 +22,7 @@ import { describeCryptoTests } from './describeCryptoTests.js' // Test vectors taken from reference implementation at https://github.com/cfrg/draft-irtf-cfrg-voprf import allVectors from './testdata/allVectors_v20.json' import { jest } from '@jest/globals' +import { expectToBeDefined } from './util.js' function fromHex(x: string): Uint8Array { return Uint8Array.from(Buffer.from(x, 'hex')) @@ -80,9 +81,7 @@ describeCryptoTests(({ provider, supportedSuites: supported }) => { describeOrSkip(`${txtMode}, ${id}`, () => { const suiteParams = supported.at(index) - if (!suiteParams) { - return - } + expectToBeDefined(suiteParams) let skSm: Uint8Array let server: OPRFServer | VOPRFServer | POPRFServer @@ -139,7 +138,8 @@ describeCryptoTests(({ provider, supportedSuites: supported }) => { let info: Uint8Array | undefined = undefined if (testVector.mode === Oprf.Mode.POPRF) { - info = fromHex(vi.Info ?? '') + expectToBeDefined(vi.Info) + info = fromHex(vi.Info) } const input = fromHexList(vi.Input)