From b56c175203ceac9d322d7c2cec00b78859fd48a4 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Sun, 9 Dec 2018 21:23:56 +0000 Subject: [PATCH 1/2] test: add CID version agnostic tests License: MIT Signed-off-by: Alan Shaw --- js/src/block/get.js | 39 ++++++++++++++++++++++++- js/src/dag/get.js | 43 +++++++++++++++++++++++++++ js/src/files-regular/cat.js | 39 +++++++++++++++++++++++++ js/src/files-regular/get.js | 40 +++++++++++++++++++++++++ js/src/files-regular/ls.js | 58 +++++++++++++++++++++++++++++++++++++ 5 files changed, 218 insertions(+), 1 deletion(-) diff --git a/js/src/block/get.js b/js/src/block/get.js index 49bac54f..76fd4924 100644 --- a/js/src/block/get.js +++ b/js/src/block/get.js @@ -4,6 +4,7 @@ const multihash = require('multihashes') const CID = require('cids') const auto = require('async/auto') +const crypto = require('crypto') const { getDescribe, getIt, expect } = require('../utils/mocha') module.exports = (createCommon, options) => { @@ -70,6 +71,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 = crypto.randomBytes(32) + + 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 = crypto.randomBytes(32) + + 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() + }) + }) + }) }) } diff --git a/js/src/dag/get.js b/js/src/dag/get.js index 35f211ac..b602ab1c 100644 --- a/js/src/dag/get.js +++ b/js/src/dag/get.js @@ -5,6 +5,9 @@ const { series, eachSeries } = require('async') const dagPB = require('ipld-dag-pb') const DAGNode = dagPB.DAGNode const dagCBOR = require('ipld-dag-cbor') +const crypto = require('crypto') +const Unixfs = require('ipfs-unixfs') +const CID = require('cids') const { spawnNodeWithId } = require('../utils/spawn') const { getDescribe, getIt, expect } = require('../utils/mocha') @@ -211,5 +214,45 @@ module.exports = (createCommon, options) => { done() }) }) + + it('should get a node added as CIDv0 with a CIDv1', done => { + const input = crypto.randomBytes(32) + + 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 = crypto.randomBytes(32) + + 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() + }) + }) + }) }) } diff --git a/js/src/files-regular/cat.js b/js/src/files-regular/cat.js index 6f1e9f46..c4e4bd1a 100644 --- a/js/src/files-regular/cat.js +++ b/js/src/files-regular/cat.js @@ -5,6 +5,7 @@ const { fixtures } = require('./utils') const bs58 = require('bs58') const parallel = require('async/parallel') const CID = require('cids') +const crypto = require('crypto') const { getDescribe, getIt, expect } = require('../utils/mocha') module.exports = (createCommon, options) => { @@ -76,6 +77,44 @@ module.exports = (createCommon, options) => { }) }) + it('should cat a file added as CIDv0 with a CIDv1', done => { + const input = crypto.randomBytes(32) + + 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 = crypto.randomBytes(32) + + 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() diff --git a/js/src/files-regular/get.js b/js/src/files-regular/get.js index 2fec9718..b93f116a 100644 --- a/js/src/files-regular/get.js +++ b/js/src/files-regular/get.js @@ -5,6 +5,8 @@ const { fixtures } = require('./utils') const bs58 = require('bs58') const parallel = require('async/parallel') const series = require('async/series') +const crypto = require('crypto') +const CID = require('cids') const { getDescribe, getIt, expect } = require('../utils/mocha') module.exports = (createCommon, options) => { @@ -73,6 +75,44 @@ module.exports = (createCommon, options) => { }) }) + it('should get a file added as CIDv0 with a CIDv1', done => { + const input = crypto.randomBytes(32) + + 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 = crypto.randomBytes(32) + + 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() diff --git a/js/src/files-regular/ls.js b/js/src/files-regular/ls.js index 7f441d83..f56448bc 100644 --- a/js/src/files-regular/ls.js +++ b/js/src/files-regular/ls.js @@ -3,6 +3,8 @@ const { fixtures } = require('./utils') const { getDescribe, getIt, expect } = require('../utils/mocha') +const crypto = require('crypto') +const CID = require('cids') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -104,6 +106,62 @@ module.exports = (createCommon, options) => { }) }) + it('should ls files added as CIDv0 with a CIDv1', done => { + const randomName = () => crypto.randomBytes(6).toString('hex') + const dir = randomName() + + const input = [ + { path: `${dir}/${randomName()}`, content: crypto.randomBytes(32) }, + { path: `${dir}/${randomName()}`, content: crypto.randomBytes(32) } + ] + + 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 = () => crypto.randomBytes(6).toString('hex') + const dir = randomName() + + const input = [ + { path: `${dir}/${randomName()}`, content: crypto.randomBytes(32) }, + { path: `${dir}/${randomName()}`, content: crypto.randomBytes(32) } + ] + + 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() From 0d0ae5827319bc4d748aec050374fa75ce9e53ec Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Tue, 11 Dec 2018 16:49:59 +0000 Subject: [PATCH 2/2] refactor: remove crypto License: MIT Signed-off-by: Alan Shaw --- js/src/block/get.js | 5 ++--- js/src/dag/get.js | 5 ++--- js/src/dht/findprovs.js | 3 +-- js/src/files-regular/cat.js | 5 ++--- js/src/files-regular/get.js | 5 ++--- js/src/files-regular/ls.js | 17 ++++++++--------- js/src/object/get.js | 4 ++-- package.json | 4 ++-- 8 files changed, 21 insertions(+), 27 deletions(-) diff --git a/js/src/block/get.js b/js/src/block/get.js index 76fd4924..3238e823 100644 --- a/js/src/block/get.js +++ b/js/src/block/get.js @@ -4,7 +4,6 @@ const multihash = require('multihashes') const CID = require('cids') const auto = require('async/auto') -const crypto = require('crypto') const { getDescribe, getIt, expect } = require('../utils/mocha') module.exports = (createCommon, options) => { @@ -72,7 +71,7 @@ module.exports = (createCommon, options) => { }) it('should get a block added as CIDv0 with a CIDv1', done => { - const input = crypto.randomBytes(32) + const input = Buffer.from(`TEST${Date.now()}`) ipfs.block.put(input, { version: 0 }, (err, res) => { expect(err).to.not.exist() @@ -91,7 +90,7 @@ module.exports = (createCommon, options) => { }) it('should get a block added as CIDv1 with a CIDv0', done => { - const input = crypto.randomBytes(32) + const input = Buffer.from(`TEST${Date.now()}`) ipfs.block.put(input, { version: 1 }, (err, res) => { expect(err).to.not.exist() diff --git a/js/src/dag/get.js b/js/src/dag/get.js index b602ab1c..d5b7415e 100644 --- a/js/src/dag/get.js +++ b/js/src/dag/get.js @@ -5,7 +5,6 @@ const { series, eachSeries } = require('async') const dagPB = require('ipld-dag-pb') const DAGNode = dagPB.DAGNode const dagCBOR = require('ipld-dag-cbor') -const crypto = require('crypto') const Unixfs = require('ipfs-unixfs') const CID = require('cids') const { spawnNodeWithId } = require('../utils/spawn') @@ -216,7 +215,7 @@ module.exports = (createCommon, options) => { }) it('should get a node added as CIDv0 with a CIDv1', done => { - const input = crypto.randomBytes(32) + const input = Buffer.from(`TEST${Date.now()}`) dagPB.DAGNode.create(input, (err, node) => { expect(err).to.not.exist() @@ -237,7 +236,7 @@ module.exports = (createCommon, options) => { }) it('should get a node added as CIDv1 with a CIDv0', done => { - const input = crypto.randomBytes(32) + const input = Buffer.from(`TEST${Date.now()}`) ipfs.add(input, { cidVersion: 1, rawLeaves: false }, (err, res) => { expect(err).to.not.exist() diff --git a/js/src/dht/findprovs.js b/js/src/dht/findprovs.js index 47e36ea8..16d50fde 100644 --- a/js/src/dht/findprovs.js +++ b/js/src/dht/findprovs.js @@ -2,7 +2,6 @@ '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') @@ -10,7 +9,7 @@ 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) diff --git a/js/src/files-regular/cat.js b/js/src/files-regular/cat.js index c4e4bd1a..048d9a9f 100644 --- a/js/src/files-regular/cat.js +++ b/js/src/files-regular/cat.js @@ -5,7 +5,6 @@ const { fixtures } = require('./utils') const bs58 = require('bs58') const parallel = require('async/parallel') const CID = require('cids') -const crypto = require('crypto') const { getDescribe, getIt, expect } = require('../utils/mocha') module.exports = (createCommon, options) => { @@ -78,7 +77,7 @@ module.exports = (createCommon, options) => { }) it('should cat a file added as CIDv0 with a CIDv1', done => { - const input = crypto.randomBytes(32) + const input = Buffer.from(`TEST${Date.now()}`) ipfs.add(input, { cidVersion: 0 }, (err, res) => { expect(err).to.not.exist() @@ -97,7 +96,7 @@ module.exports = (createCommon, options) => { }) it('should cat a file added as CIDv1 with a CIDv0', done => { - const input = crypto.randomBytes(32) + const input = Buffer.from(`TEST${Date.now()}`) ipfs.add(input, { cidVersion: 1, rawLeaves: false }, (err, res) => { expect(err).to.not.exist() diff --git a/js/src/files-regular/get.js b/js/src/files-regular/get.js index b93f116a..334a0c34 100644 --- a/js/src/files-regular/get.js +++ b/js/src/files-regular/get.js @@ -5,7 +5,6 @@ const { fixtures } = require('./utils') const bs58 = require('bs58') const parallel = require('async/parallel') const series = require('async/series') -const crypto = require('crypto') const CID = require('cids') const { getDescribe, getIt, expect } = require('../utils/mocha') @@ -76,7 +75,7 @@ module.exports = (createCommon, options) => { }) it('should get a file added as CIDv0 with a CIDv1', done => { - const input = crypto.randomBytes(32) + const input = Buffer.from(`TEST${Date.now()}`) ipfs.add(input, { cidVersion: 0 }, (err, res) => { expect(err).to.not.exist() @@ -95,7 +94,7 @@ module.exports = (createCommon, options) => { }) it('should get a file added as CIDv1 with a CIDv0', done => { - const input = crypto.randomBytes(32) + const input = Buffer.from(`TEST${Date.now()}`) ipfs.add(input, { cidVersion: 1, rawLeaves: false }, (err, res) => { expect(err).to.not.exist() diff --git a/js/src/files-regular/ls.js b/js/src/files-regular/ls.js index f56448bc..286d02bb 100644 --- a/js/src/files-regular/ls.js +++ b/js/src/files-regular/ls.js @@ -3,7 +3,6 @@ const { fixtures } = require('./utils') const { getDescribe, getIt, expect } = require('../utils/mocha') -const crypto = require('crypto') const CID = require('cids') module.exports = (createCommon, options) => { @@ -107,12 +106,12 @@ module.exports = (createCommon, options) => { }) it('should ls files added as CIDv0 with a CIDv1', done => { - const randomName = () => crypto.randomBytes(6).toString('hex') - const dir = randomName() + const randomName = prefix => `${prefix}${Math.round(Math.random() * 1000)}` + const dir = randomName('DIR') const input = [ - { path: `${dir}/${randomName()}`, content: crypto.randomBytes(32) }, - { path: `${dir}/${randomName()}`, content: crypto.randomBytes(32) } + { 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) => { @@ -135,12 +134,12 @@ module.exports = (createCommon, options) => { }) it('should ls files added as CIDv1 with a CIDv0', done => { - const randomName = () => crypto.randomBytes(6).toString('hex') - const dir = randomName() + const randomName = prefix => `${prefix}${Math.round(Math.random() * 1000)}` + const dir = randomName('DIR') const input = [ - { path: `${dir}/${randomName()}`, content: crypto.randomBytes(32) }, - { path: `${dir}/${randomName()}`, content: crypto.randomBytes(32) } + { 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) => { diff --git a/js/src/object/get.js b/js/src/object/get.js index 14087be5..438459ae 100644 --- a/js/src/object/get.js +++ b/js/src/object/get.js @@ -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) => { @@ -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) { diff --git a/package.json b/package.json index c52f435b..186279ce 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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"