Skip to content

Commit

Permalink
feat: header tests (#139)
Browse files Browse the repository at this point in the history
* test scrambling of a buffer and then decoding it

* export `encodeBlockPrefix` from `encode.js`. more tests

* hardcode randomBytes to avoid randomly generated but correct headers

* remove unused import (cenc)

* remove wrong import (throws from node:test)

* remove plan (it just leads to unnecessary errors)

---------

Co-authored-by: Tomás Ciccola <[email protected]>
  • Loading branch information
tomasciccola and Tomás Ciccola authored Aug 31, 2023
1 parent 5e13870 commit 0a0a988
Showing 1 changed file with 98 additions and 2 deletions.
100 changes: 98 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
// @ts-check
import test from 'tape'
import { encode, decode } from '../dist/index.js'
import { parseVersionId } from '../dist/index.js'
import { randomBytes } from 'node:crypto'
import {
encode,
decode,
decodeBlockPrefix,
parseVersionId,
} from '../dist/index.js'
import { encodeBlockPrefix } from '../dist/encode.js'
import { dataTypeIds, currentSchemaVersions } from '../dist/config.js'
import { DATA_TYPE_ID_BYTES, SCHEMA_VERSION_BYTES } from '../dist/constants.js'
import {
goodDocsMinimal,
goodDocsCompleted,
Expand Down Expand Up @@ -61,6 +69,94 @@ then decoding and comparing the two objects - extra values shouldn't be present`
}
})

test(`testing decoding of header that should match the dataTypeId and version`, async (t) => {
for (const [schemaName, dataTypeId] of Object.entries(dataTypeIds)) {
// TODO: test also schemaVersions greater than the current, for foward compat
const schemaVersion = currentSchemaVersions[schemaName]
/** @type { import('../src/types.js').ValidSchemaDef } */
const schemaDef = { schemaName, schemaVersion }
const buf = encodeBlockPrefix(schemaDef)
t.deepEqual(
decodeBlockPrefix(buf),
schemaDef,
`test equality of schema definition with the decoded block prefix for ${schemaName}`
)
t.equal(
buf.subarray(0, DATA_TYPE_ID_BYTES).toString('hex'),
dataTypeId,
`test equality of dataTypeId for ${schemaName}`
)
}
})

test(`test encoding and decoding of block prefix, ignoring data that comes after`, async (t) => {
for (let [schemaName, schemaVersion] of Object.entries(
currentSchemaVersions
)) {
/** @type { import('../src/types.js').ValidSchemaDef } */
const schemaDef = {
schemaName,
schemaVersion,
}
const blockPrefix = encodeBlockPrefix(schemaDef)
const prefixLength = DATA_TYPE_ID_BYTES + SCHEMA_VERSION_BYTES
const buf = Buffer.concat([blockPrefix, randomBytes(50)])
t.equals(
blockPrefix.length,
prefixLength,
`test proper length of block prefix`
)
t.deepEqual(
decodeBlockPrefix(buf),
schemaDef,
`test equality of schema definition for ${schemaName}`
)
}
})

test(`test encoding of wrongly formatted header`, async (t) => {
/** @type { import('../src/types.js').ValidSchemaDef } */
let schemaDef = {
schemaName: 'presot',
schemaVersion: 2,
}
t.throws(() => {
encodeBlockPrefix(schemaDef)
}, `when encoding a prefix with wrong schema name`)

/** @type { import('../src/types.js').ValidSchemaDef } */
schemaDef = {
schemaName: 'observation',
schemaVersion: 5,
}
let buf = encodeBlockPrefix(schemaDef).subarray(0, 7)
t.throws(() => {
decodeBlockPrefix(buf)
}, `when decoding a header with the wrong length`)

// hardcoded since there's a slim chance we produce a correct header
const randomBuf = Buffer.from(
'806a8dbb56e1994c8ea6887cda1d21b441eb9122',
'hex'
)
t.throws(() => {
decodeBlockPrefix(randomBuf)
}, `when trying to decode a header that is random data`)

t.throws(() => {
decodeBlockPrefix(Buffer.alloc(50))
}, `when trying to decode a header that is empty`)

schemaDef = { schemaName: 'projectSettings', schemaVersion: 2 }
buf = encodeBlockPrefix(schemaDef)
// hardcoded since there's a slim chance we produce a correct header
const unknownDataTypeId = Buffer.from('7a79b8b773b2', 'hex')
unknownDataTypeId.copy(buf)
t.throws(() => {
decodeBlockPrefix(buf)
})
})

/**
* Remove undefined properties (deeply) from an object, by round-tripping to
* JSON. Also handles Buffers via JSON.parse reviver
Expand Down

0 comments on commit 0a0a988

Please sign in to comment.