Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

refactor: tests for CIDv1 base32 #444

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"bl": "^2.1.2",
"bs58": "^4.0.1",
"chai": "^4.2.0",
"cids": "~0.5.5",
"cids": "github:ipld/js-cid#refactor/cidv1base32-default",
"concat-stream": "^2.0.0",
"dirty-chai": "^2.0.1",
"es6-promisify": "^6.0.1",
Expand Down
14 changes: 7 additions & 7 deletions src/block/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,26 @@ module.exports = (createCommon, options) => {
after((done) => common.teardown(done))

it('should put a buffer, using defaults', (done) => {
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const expectedHash = 'bafybeiaxnnnb7qz2focittuqq3ya25q7rcv3bqynnczfzako47346wosmu'
const blob = Buffer.from('blorb')

ipfs.block.put(blob, (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.be.eql(blob)
expect(block.cid.multihash).to.eql(multihash.fromB58String(expectedHash))
expect(block.cid.toString()).to.eql(expectedHash)
done()
})
})

it('should put a buffer, using CID', (done) => {
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const expectedHash = 'bafybeiaxnnnb7qz2focittuqq3ya25q7rcv3bqynnczfzako47346wosmu'
const cid = new CID(expectedHash)
const blob = Buffer.from('blorb')

ipfs.block.put(blob, { cid: cid }, (err, block) => {
ipfs.block.put(blob, { cid }, (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.be.eql(blob)
expect(block.cid.multihash).to.eql(multihash.fromB58String(expectedHash))
expect(block.cid.toString()).to.eql(expectedHash)
done()
})
})
Expand All @@ -74,14 +74,14 @@ module.exports = (createCommon, options) => {
})

it('should put a Block instance', (done) => {
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const expectedHash = 'bafybeiaxnnnb7qz2focittuqq3ya25q7rcv3bqynnczfzako47346wosmu'
const cid = new CID(expectedHash)
const b = new Block(Buffer.from('blorb'), cid)

ipfs.block.put(b, (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.eql(Buffer.from('blorb'))
expect(block.cid.multihash).to.eql(multihash.fromB58String(expectedHash))
expect(block.cid.toString()).to.eql(expectedHash)
done()
})
})
Expand Down
7 changes: 2 additions & 5 deletions src/block/stat.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-env mocha */
'use strict'

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

Expand All @@ -12,7 +11,7 @@ module.exports = (createCommon, options) => {

describe('.block.stat', () => {
const data = Buffer.from('blorb')
let ipfs, hash
let ipfs, cid

before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
Expand All @@ -26,16 +25,14 @@ module.exports = (createCommon, options) => {
}, (err, res) => {
if (err) return done(err)
ipfs = res.ipfs
hash = res.block.cid.multihash
cid = res.block.cid
done()
})
})

after((done) => common.teardown(done))

it('should stat by CID', (done) => {
const cid = new CID(hash)

ipfs.block.stat(cid, (err, stats) => {
expect(err).to.not.exist()
expect(stats).to.have.property('key')
Expand Down
2 changes: 1 addition & 1 deletion src/dag/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ module.exports = (createCommon, options) => {
dagPB.DAGNode.create(input, (err, node) => {
expect(err).to.not.exist()

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

Expand Down
14 changes: 13 additions & 1 deletion src/dag/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ module.exports = (createCommon, options) => {
it('should set defaults when calling put without options', (done) => {
ipfs.dag.put(cborNode, (err, cid) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('dag-cbor')
expect(multihash.decode(cid.multihash).name).to.equal('sha2-256')
done()
Expand All @@ -116,6 +117,7 @@ module.exports = (createCommon, options) => {
it('should set defaults when calling put without options (promised)', () => {
return ipfs.dag.put(cborNode)
.then((cid) => {
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('dag-cbor')
expect(multihash.decode(cid.multihash).name).to.equal('sha2-256')
})
Expand All @@ -133,7 +135,17 @@ module.exports = (createCommon, options) => {
})
})

it.skip('should put by passing the cid instead of format and hashAlg', (done) => {})
it('should put by passing the cid instead of format and hashAlg', (done) => {
dagCBOR.util.cid(cborNode, (err, cid) => {
expect(err).to.not.exist()

ipfs.dag.put(cborNode, { cid }, (err, _cid) => {
expect(err).to.not.exist()
expect(cid.equals(_cid)).to.be.true()
done()
})
})
})

// TODO it.skip('Promises support', (done) => {})
})
Expand Down
2 changes: 1 addition & 1 deletion src/files-regular/add-from-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ module.exports = (createCommon, options) => {
expect(err).to.not.exist()
expect(result.length).to.be.above(10)
expect(result.map(object => object.path)).to.include('hidden-files-folder/.hiddenTest.txt')
expect(result.map(object => object.hash)).to.include('QmdbAjVmLRdpFyi8FFvjPfhTGB2cVXvWLuK7Sbt38HXrtt')
expect(result.map(object => object.hash)).to.include('bafkreieapodbusibovh26gwruowy7rchsgmoxwtwnfxsmjpbpgwh2guazu')
done()
})
})
Expand Down
4 changes: 2 additions & 2 deletions src/files-regular/add-pull-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ module.exports = (createCommon, options) => {
})

it('should add with object chunks and pull stream content', (done) => {
const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm'
const expectedCid = 'bafkreie7q3iidccmpvszul7kudcvvuavuo7u6gzlbobczuk5nqk3b4akba'

pull(
pull.values([{ content: pull.values([Buffer.from('test')]) }]),
ipfs.addPullStream(),
pull.collect((err, res) => {
if (err) return done(err)
expect(res).to.have.length(1)
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 })
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 4 })
done()
})
)
Expand Down
23 changes: 11 additions & 12 deletions src/files-regular/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ module.exports = (createCommon, options) => {
const file = filesAdded[0]
expect(file.hash).to.equal(fixtures.smallFile.cid)
expect(file.path).to.equal(fixtures.smallFile.cid)
// file.size counts the overhead by IPLD nodes and unixfs protobuf
expect(file.size).greaterThan(fixtures.smallFile.data.length)
expect(file.size).to.equal(fixtures.smallFile.data.length)
done()
})
})
Expand Down Expand Up @@ -141,7 +140,7 @@ module.exports = (createCommon, options) => {
})

it('should add readable stream', (done) => {
const expectedCid = 'QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS'
const expectedCid = 'bafkreiata6mq425fzikf5m26temcvg7mizjrxrkn35swuybmpah2ajan5y'

const rs = new Readable()
rs.push(Buffer.from('some data'))
Expand All @@ -153,14 +152,14 @@ module.exports = (createCommon, options) => {
expect(filesAdded).to.be.length(1)
const file = filesAdded[0]
expect(file.path).to.equal(expectedCid)
expect(file.size).to.equal(17)
expect(file.size).to.equal(9)
expect(file.hash).to.equal(expectedCid)
done()
})
})

it('should add array of objects with readable stream content', (done) => {
const expectedCid = 'QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS'
const expectedCid = 'bafkreiata6mq425fzikf5m26temcvg7mizjrxrkn35swuybmpah2ajan5y'

const rs = new Readable()
rs.push(Buffer.from('some data'))
Expand All @@ -174,40 +173,40 @@ module.exports = (createCommon, options) => {
expect(filesAdded).to.be.length(1)
const file = filesAdded[0]
expect(file.path).to.equal('data.txt')
expect(file.size).to.equal(17)
expect(file.size).to.equal(9)
expect(file.hash).to.equal(expectedCid)
done()
})
})

it('should add pull stream', (done) => {
const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm'
const expectedCid = 'bafkreie7q3iidccmpvszul7kudcvvuavuo7u6gzlbobczuk5nqk3b4akba'

ipfs.add(pull.values([Buffer.from('test')]), (err, res) => {
if (err) return done(err)
expect(res).to.have.length(1)
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 })
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 4 })
done()
})
})

it('should add pull stream (promised)', () => {
const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm'
const expectedCid = 'bafkreie7q3iidccmpvszul7kudcvvuavuo7u6gzlbobczuk5nqk3b4akba'

return ipfs.add(pull.values([Buffer.from('test')]))
.then((res) => {
expect(res).to.have.length(1)
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 })
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 4 })
})
})

it('should add array of objects with pull stream content (promised)', () => {
const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm'
const expectedCid = 'bafkreie7q3iidccmpvszul7kudcvvuavuo7u6gzlbobczuk5nqk3b4akba'

return ipfs.add([{ content: pull.values([Buffer.from('test')]) }])
.then((res) => {
expect(res).to.have.length(1)
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 })
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 4 })
})
})

Expand Down
8 changes: 4 additions & 4 deletions src/files-regular/cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ module.exports = (createCommon, options) => {
], done)
})

it('should cat with a base58 string encoded multihash', (done) => {
it('should cat with a base32 string encoded CID', (done) => {
ipfs.cat(fixtures.smallFile.cid, (err, data) => {
expect(err).to.not.exist()
expect(data.toString()).to.contain('Plz add me!')
done()
})
})

it('should cat with a base58 string encoded multihash (promised)', () => {
it('should cat with a base32 string encoded CID (promised)', () => {
return ipfs.cat(fixtures.smallFile.cid)
.then((data) => {
expect(data.toString()).to.contain('Plz add me!')
})
})

it('should cat with a Buffer multihash', (done) => {
const cid = Buffer.from(bs58.decode(fixtures.smallFile.cid))
it('should cat with a Buffer CID', (done) => {
const cid = new CID(fixtures.smallFile.cid).buffer

ipfs.cat(cid, (err, data) => {
expect(err).to.not.exist()
Expand Down
6 changes: 3 additions & 3 deletions src/files-regular/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const loadFixture = require('aegir/fixtures')

exports.fixtures = Object.freeze({
directory: Object.freeze({
cid: 'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP',
cid: 'bafybeiaqxngbqbr5erl2ux2rfuoipy3b6caefjodi3gshpi3u7nsqruvuq',
files: Object.freeze({
'pp.txt': loadFixture('test/fixtures/test-folder/pp.txt', 'interface-ipfs-core'),
'holmes.txt': loadFixture('test/fixtures/test-folder/holmes.txt', 'interface-ipfs-core'),
Expand All @@ -15,11 +15,11 @@ exports.fixtures = Object.freeze({
})
}),
smallFile: Object.freeze({
cid: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP',
cid: 'bafkreidffqfydlguosmmyebv5rp72m45tbpbq6segnkosa45kjfnduix6u',
data: loadFixture('test/fixtures/testfile.txt', 'interface-ipfs-core')
}),
bigFile: Object.freeze({
cid: 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq',
cid: 'bafybeih2sk5etf4biw7mcmzximj4zz5yite4lhqowiq2pfdwiz55qgsiqu',
data: loadFixture('test/fixtures/15mb.random', 'interface-ipfs-core')
}),
sslOpts: Object.freeze({
Expand Down