Skip to content

Commit

Permalink
test: add CID version agnostic tests (ipfs#413)
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
  • Loading branch information
Alan Shaw authored Dec 12, 2018
1 parent f54422d commit b7fad99
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 7 deletions.
38 changes: 37 additions & 1 deletion js/src/block/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,42 @@ module.exports = (createCommon, options) => {
})
})

// TODO it.skip('Promises support', (done) => {})
it('should get a block added as CIDv0 with a CIDv1', done => {
const input = Buffer.from(`TEST${Date.now()}`)

ipfs.block.put(input, { version: 0 }, (err, res) => {
expect(err).to.not.exist()

const cidv0 = res.cid
expect(cidv0.version).to.equal(0)

const cidv1 = cidv0.toV1()

ipfs.block.get(cidv1, (err, output) => {
expect(err).to.not.exist()
expect(output.data).to.eql(input)
done()
})
})
})

it('should get a block added as CIDv1 with a CIDv0', done => {
const input = Buffer.from(`TEST${Date.now()}`)

ipfs.block.put(input, { version: 1 }, (err, res) => {
expect(err).to.not.exist()

const cidv1 = res.cid
expect(cidv1.version).to.equal(1)

const cidv0 = cidv1.toV0()

ipfs.block.get(cidv0, (err, output) => {
expect(err).to.not.exist()
expect(output.data).to.eql(input)
done()
})
})
})
})
}
42 changes: 42 additions & 0 deletions js/src/dag/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const { series, eachSeries } = require('async')
const dagPB = require('ipld-dag-pb')
const DAGNode = dagPB.DAGNode
const dagCBOR = require('ipld-dag-cbor')
const Unixfs = require('ipfs-unixfs')
const CID = require('cids')
const { spawnNodeWithId } = require('../utils/spawn')
const { getDescribe, getIt, expect } = require('../utils/mocha')

Expand Down Expand Up @@ -211,5 +213,45 @@ module.exports = (createCommon, options) => {
done()
})
})

it('should get a node added as CIDv0 with a CIDv1', done => {
const input = Buffer.from(`TEST${Date.now()}`)

dagPB.DAGNode.create(input, (err, node) => {
expect(err).to.not.exist()

ipfs.dag.put(node, { format: 'dag-pb', hashAlg: 'sha2-256' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(0)

const cidv1 = cid.toV1()

ipfs.dag.get(cidv1, (err, output) => {
expect(err).to.not.exist()
expect(output.value.data).to.eql(input)
done()
})
})
})
})

it('should get a node added as CIDv1 with a CIDv0', done => {
const input = Buffer.from(`TEST${Date.now()}`)

ipfs.add(input, { cidVersion: 1, rawLeaves: false }, (err, res) => {
expect(err).to.not.exist()

const cidv1 = new CID(res[0].hash)
expect(cidv1.version).to.equal(1)

const cidv0 = cidv1.toV0()

ipfs.dag.get(cidv0, (err, output) => {
expect(err).to.not.exist()
expect(Unixfs.unmarshal(output.value.data).data).to.eql(input)
done()
})
})
})
})
}
3 changes: 1 addition & 2 deletions js/src/dht/findprovs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
'use strict'

const multihashing = require('multihashing-async')
const Crypto = require('crypto')
const waterfall = require('async/waterfall')
const CID = require('cids')
const { spawnNodesWithId } = require('../utils/spawn')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const { connect } = require('../utils/swarm')

function fakeCid (cb) {
const bytes = Crypto.randomBytes(Math.round(Math.random() * 1000))
const bytes = Buffer.from(`TEST${Date.now()}`)
multihashing(bytes, 'sha2-256', (err, mh) => {
if (err) {
cb(err)
Expand Down
38 changes: 38 additions & 0 deletions js/src/files-regular/cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,44 @@ module.exports = (createCommon, options) => {
})
})

it('should cat a file added as CIDv0 with a CIDv1', done => {
const input = Buffer.from(`TEST${Date.now()}`)

ipfs.add(input, { cidVersion: 0 }, (err, res) => {
expect(err).to.not.exist()

const cidv0 = new CID(res[0].hash)
expect(cidv0.version).to.equal(0)

const cidv1 = cidv0.toV1()

ipfs.cat(cidv1, (err, output) => {
expect(err).to.not.exist()
expect(output).to.eql(input)
done()
})
})
})

it('should cat a file added as CIDv1 with a CIDv0', done => {
const input = Buffer.from(`TEST${Date.now()}`)

ipfs.add(input, { cidVersion: 1, rawLeaves: false }, (err, res) => {
expect(err).to.not.exist()

const cidv1 = new CID(res[0].hash)
expect(cidv1.version).to.equal(1)

const cidv0 = cidv1.toV0()

ipfs.cat(cidv0, (err, output) => {
expect(err).to.not.exist()
expect(output).to.eql(input)
done()
})
})
})

it('should cat a BIG file', (done) => {
ipfs.cat(fixtures.bigFile.cid, (err, data) => {
expect(err).to.not.exist()
Expand Down
39 changes: 39 additions & 0 deletions js/src/files-regular/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { fixtures } = require('./utils')
const bs58 = require('bs58')
const parallel = require('async/parallel')
const series = require('async/series')
const CID = require('cids')
const { getDescribe, getIt, expect } = require('../utils/mocha')

module.exports = (createCommon, options) => {
Expand Down Expand Up @@ -73,6 +74,44 @@ module.exports = (createCommon, options) => {
})
})

it('should get a file added as CIDv0 with a CIDv1', done => {
const input = Buffer.from(`TEST${Date.now()}`)

ipfs.add(input, { cidVersion: 0 }, (err, res) => {
expect(err).to.not.exist()

const cidv0 = new CID(res[0].hash)
expect(cidv0.version).to.equal(0)

const cidv1 = cidv0.toV1()

ipfs.get(cidv1, (err, output) => {
expect(err).to.not.exist()
expect(output[0].content).to.eql(input)
done()
})
})
})

it('should get a file added as CIDv1 with a CIDv0', done => {
const input = Buffer.from(`TEST${Date.now()}`)

ipfs.add(input, { cidVersion: 1, rawLeaves: false }, (err, res) => {
expect(err).to.not.exist()

const cidv1 = new CID(res[0].hash)
expect(cidv1.version).to.equal(1)

const cidv0 = cidv1.toV0()

ipfs.get(cidv0, (err, output) => {
expect(err).to.not.exist()
expect(output[0].content).to.eql(input)
done()
})
})
})

it('should get a BIG file', (done) => {
ipfs.get(fixtures.bigFile.cid, (err, files) => {
expect(err).to.not.exist()
Expand Down
57 changes: 57 additions & 0 deletions js/src/files-regular/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const { fixtures } = require('./utils')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const CID = require('cids')

module.exports = (createCommon, options) => {
const describe = getDescribe(options)
Expand Down Expand Up @@ -104,6 +105,62 @@ module.exports = (createCommon, options) => {
})
})

it('should ls files added as CIDv0 with a CIDv1', done => {
const randomName = prefix => `${prefix}${Math.round(Math.random() * 1000)}`
const dir = randomName('DIR')

const input = [
{ path: `${dir}/${randomName('F0')}`, content: Buffer.from(randomName('D0')) },
{ path: `${dir}/${randomName('F1')}`, content: Buffer.from(randomName('D1')) }
]

ipfs.add(input, { cidVersion: 0 }, (err, res) => {
expect(err).to.not.exist()

const cidv0 = new CID(res[res.length - 1].hash)
expect(cidv0.version).to.equal(0)

const cidv1 = cidv0.toV1()

ipfs.ls(cidv1, (err, output) => {
expect(err).to.not.exist()
expect(output.length).to.equal(input.length)
output.forEach(({ hash }) => {
expect(res.find(file => file.hash === hash)).to.exist()
})
done()
})
})
})

it('should ls files added as CIDv1 with a CIDv0', done => {
const randomName = prefix => `${prefix}${Math.round(Math.random() * 1000)}`
const dir = randomName('DIR')

const input = [
{ path: `${dir}/${randomName('F0')}`, content: Buffer.from(randomName('D0')) },
{ path: `${dir}/${randomName('F1')}`, content: Buffer.from(randomName('D1')) }
]

ipfs.add(input, { cidVersion: 1, rawLeaves: false }, (err, res) => {
expect(err).to.not.exist()

const cidv1 = new CID(res[res.length - 1].hash)
expect(cidv1.version).to.equal(1)

const cidv0 = cidv1.toV1()

ipfs.ls(cidv0, (err, output) => {
expect(err).to.not.exist()
expect(output.length).to.equal(input.length)
output.forEach(({ hash }) => {
expect(res.find(file => file.hash === hash)).to.exist()
})
done()
})
})
})

it('should correctly handle a non existing hash', (done) => {
ipfs.ls('surelynotavalidhashheh?', (err, res) => {
expect(err).to.exist()
Expand Down
4 changes: 2 additions & 2 deletions js/src/object/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const series = require('async/series')
const hat = require('hat')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const UnixFs = require('ipfs-unixfs')
const crypto = require('crypto')
const randomBytes = require('randombytes')
const { asDAGLink } = require('./utils')

module.exports = (createCommon, options) => {
Expand Down Expand Up @@ -326,7 +326,7 @@ module.exports = (createCommon, options) => {
let next = maxBytes

while (data.length !== required) {
data = Buffer.concat([data, crypto.randomBytes(next)])
data = Buffer.concat([data, randomBytes(next)])
next = maxBytes

if (data.length + maxBytes > required) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"chai": "^4.2.0",
"cids": "~0.5.5",
"concat-stream": "^1.6.2",
"crypto": "^1.0.1",
"dirty-chai": "^2.0.1",
"es6-promisify": "^6.0.1",
"hat": "0.0.3",
Expand All @@ -62,7 +61,8 @@
"peer-id": "~0.12.0",
"peer-info": "~0.15.0",
"pull-stream": "^3.6.9",
"pump": "^3.0.0"
"pump": "^3.0.0",
"randombytes": "^2.0.6"
},
"devDependencies": {
"aegir": "^17.0.1"
Expand Down

0 comments on commit b7fad99

Please sign in to comment.