Skip to content

Commit

Permalink
Adding type assertions for non-nullable variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
armfazh committed Jul 2, 2024
1 parent 2e73ed8 commit be6c093
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
5 changes: 2 additions & 3 deletions test/oprf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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/)
}
Expand Down
4 changes: 4 additions & 0 deletions test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ export function serdesEquals<
const deser = deserializer.deserialize(ser)
return t.isEqual(deser)
}

export function expectToBeDefined<T>(v?: T): asserts v is NonNullable<T> {
expect(v).toBeDefined()
}
8 changes: 4 additions & 4 deletions test/vectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit be6c093

Please sign in to comment.