Skip to content

Commit

Permalink
chore(carv2): add fixtures and test expectations from go-car
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Mar 4, 2022
1 parent b95a6e2 commit d563acf
Show file tree
Hide file tree
Showing 22 changed files with 4,377 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ node_modules/
coverage/
build/
dist/
types/
types/
examples/node_modules/
12 changes: 12 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
test/fixtures/
test/fixtures.js
test/fixtures-expectations.js
esm/browser-test/fixtures.js
esm/node-test/fixtures.js
cjs/browser-test/fixtures.js
cjs/node-test/fixtures.js
test/fixtures-expectations.js
esm/browser-test/fixtures-expectations.js
esm/node-test/fixtures-expectations.js
cjs/browser-test/fixtures-expectations.js
cjs/node-test/fixtures-expectations.js
5 changes: 4 additions & 1 deletion lib/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const V2_HEADER_LENGTH = /* characteristics */ 16 /* v1 offset */ + 8 /* v1 size
*/
async function readVarint (reader) {
const bytes = await reader.upTo(8)
if (!bytes.length) {
throw new Error('Unexpected end of data')
}
const i = varint.decode(bytes)
reader.seek(varint.decode.bytes)
return i
Expand Down Expand Up @@ -92,7 +95,7 @@ export async function readHeader (reader, strictVersion) {
}
// version 2
const v2Header = await readV2Header(reader)
reader.seek(reader.pos - v2Header.dataOffset)
reader.seek(v2Header.dataOffset - reader.pos)
const v1Header = await readHeader(reader, 1)
return Object.assign(v1Header, v2Header)
/* c8 ignore next 2 */
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint": "standard",
"build": "npm run build:js && npm run build:types",
"build:js": "ipjs build --tests --main && npm run build:copy",
"build:copy": "mkdir -p dist/examples/ && cp -a tsconfig.json *.js *.ts lib test dist/ && cp examples/*.* dist/examples/",
"build:copy": "mkdir -p dist/examples/ && cp -a tsconfig.json .npmignore *.js *.ts lib test dist/ && cp examples/*.* dist/examples/ && rm -rf dist/test/fixtures/",
"build:types": "tsc --build && mv types dist",
"test:cjs": "rm -rf dist && npm run build && cp test/go.car dist/cjs/node-test/ && mocha dist/cjs/node-test/test-*.js && mocha dist/cjs/node-test/node-test-*.js && npm run test:cjs:browser",
"test:esm": "rm -rf dist && npm run build && cp test/go.car dist/esm/node-test/ && mocha dist/esm/node-test/test-*.js && mocha dist/esm/node-test/node-test-*.js && npm run test:esm:browser",
Expand Down Expand Up @@ -211,4 +211,4 @@
"@semantic-release/git"
]
}
}
}
24 changes: 24 additions & 0 deletions test/_fixtures_to_js.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env node

import { readdir, readFile, writeFile } from 'fs/promises'
import { dirname, join } from 'path'

async function main () {
const thisdir = dirname(new URL(import.meta.url).pathname)
const outfile = join(thisdir, 'fixtures.js')
const fixturesdir = join(thisdir, 'fixtures')
const files = await readdir(fixturesdir)
let content = '/** @type {Record<string, string>} */\nexport const data = {\n'
for (const f of files) {
content += ` '${f}': '`
content += (await readFile(join(fixturesdir, f))).toString('base64')
content += '\',\n'
}
content += ' _: \'\'\n}\n'
await writeFile(join(outfile), content, 'utf8')
}

main().catch((err) => {
console.error(err)
process.exit(1)
})
4,264 changes: 4,264 additions & 0 deletions test/fixtures-expectations.js

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions test/fixtures.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test/fixtures/sample-corrupt-pragma.car
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�eroots�gversio
Binary file added test/fixtures/sample-index.carindex
Binary file not shown.
1 change: 1 addition & 0 deletions test/fixtures/sample-rootless-v42.car
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�gversion*
Binary file added test/fixtures/sample-rw-bs-v2.car
Binary file not shown.
Binary file added test/fixtures/sample-unixfs-v2.car
Binary file not shown.
Binary file added test/fixtures/sample-v1-noidentity.car
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added test/fixtures/sample-v1.car
Binary file not shown.
Binary file not shown.
Binary file added test/fixtures/sample-v2-indexless.car
Binary file not shown.
Binary file added test/fixtures/sample-wrapped-v2.car
Binary file not shown.
48 changes: 48 additions & 0 deletions test/test-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import { CarReader } from '@ipld/car/reader'
import { CarWriter } from '@ipld/car/writer'
import { bytesReader, readHeader } from '@ipld/car/decoder'
import * as Block from 'multiformats/block'
import { sha256 } from 'multiformats/hashes/sha2'
import * as raw from 'multiformats/codecs/raw'
import { base64 } from 'multiformats/bases/base64'
import * as dagPb from '@ipld/dag-pb'
import {
carBytes,
Expand All @@ -22,6 +24,8 @@ import {
verifyBlocks,
verifyCids
} from './verify-store-reader.js'
import { data as fixtures } from './fixtures.js'
import { expectations as fixtureExpectations } from './fixtures-expectations.js'

describe('CarReader fromBytes()', () => {
it('complete', async () => {
Expand Down Expand Up @@ -195,3 +199,47 @@ describe('CarReader fromIterable()', () => {
})
})
})

describe('Shared fixtures', () => {
describe('Header', () => {
for (const [name, { version: expectedVersion, err: expectedError }] of Object.entries(fixtureExpectations)) {
it(name, async () => {
const data = base64.baseDecode(fixtures[name])
let header
try {
header = await readHeader(bytesReader(data))
} catch (err) {
if (expectedError != null) {
assert.equal(err.message, expectedError)
return
}
assert.ifError(err)
}
if (expectedError != null) {
assert.fail(`Expected error: ${expectedError}`)
}
assert.isDefined(header, 'did not decode header')
if (expectedVersion != null && header != null) {
assert.strictEqual(header.version, expectedVersion)
}
})
}
})

describe('Contents', () => {
for (const [name, { cids: expectedCids }] of Object.entries(fixtureExpectations)) {
if (expectedCids == null) {
continue
}
it(name, async () => {
const data = base64.baseDecode(fixtures[name])
const reader = await CarReader.fromBytes(data)
let i = 0
for await (const cid of reader.cids()) {
assert.strictEqual(cid.toString(), expectedCids[i++])
}
assert.strictEqual(i, expectedCids.length)
})
}
})
})
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"@ipld/car/reader": [ "./lib/reader.js" ],
"@ipld/car/indexed-reader": [ "./lib/indexed-reader.js" ],
"@ipld/car/iterator": [ "./lib/iterator.js" ],
"@ipld/car/indexer": [ "./lib/indexer.js" ]
"@ipld/car/indexer": [ "./lib/indexer.js" ],
"@ipld/car/decoder": [ "./lib/decoder.js" ],
}
},
"exclude": [
Expand Down

0 comments on commit d563acf

Please sign in to comment.