Skip to content

Commit 1b5accc

Browse files
committed
Add test for add with --hash option. Pass raw cid/multihash to ipfs object get/data
1 parent eb1f356 commit 1b5accc

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

src/files/add.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = (send) => {
3030
}
3131

3232
if (opts['raw-leaves'] != null) {
33-
qs['raw-leaves'] = opts['raw-version']
33+
qs['raw-leaves'] = opts['raw-leaves']
3434
} else if (opts.rawLeaves != null) {
3535
qs['raw-leaves'] = opts.rawLeaves
3636
}

src/utils/get-dagnode.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,16 @@
22

33
const DAGNode = require('ipld-dag-pb').DAGNode
44
const parallel = require('async/parallel')
5-
const CID = require('cids')
6-
const mh = require('multihashes')
75
const streamToValue = require('./stream-to-value')
86

97
module.exports = function (send, hash, callback) {
10-
// Until js-ipfs supports object/get and object/data by CID
11-
// we need to convert our CID or multihash hash into a multihash
12-
const multihash = mh.toB58String(new CID(hash).multihash)
13-
148
// Retrieve the object and its data in parallel, then produce a DAGNode
159
// instance using this information.
1610
parallel([
1711
(done) => {
1812
send({
1913
path: 'object/get',
20-
args: multihash
14+
args: hash
2115
}, done)
2216
},
2317
(done) => {
@@ -26,7 +20,7 @@ module.exports = function (send, hash, callback) {
2620
// See https://github.com/ipfs/go-ipfs/issues/1582 for more details.
2721
send({
2822
path: 'object/data',
29-
args: multihash
23+
args: hash
3024
}, done)
3125
}
3226
], (err, res) => {

test/files.spec.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,27 @@ const expect = chai.expect
88
chai.use(dirtyChai)
99
const isNode = require('detect-node')
1010
const loadFixture = require('aegir/fixtures')
11+
const mh = require('multihashes')
12+
const CID = require('cids')
1113

1214
const FactoryClient = require('./ipfs-factory/client')
1315

1416
const testfile = isNode
1517
? loadFixture(__dirname, '/fixtures/testfile.txt')
1618
: loadFixture(__dirname, 'fixtures/testfile.txt')
1719

20+
// TODO: Test against all algorithms Object.keys(mh.names)
21+
// This subset is known to work with both go-ipfs and js-ipfs as of 2017-09-05
22+
const HASH_ALGS = [
23+
'sha1',
24+
'sha2-256',
25+
'sha2-512',
26+
'keccak-224',
27+
'keccak-256',
28+
'keccak-384',
29+
'keccak-512'
30+
]
31+
1832
describe('.files (the MFS API part)', function () {
1933
this.timeout(120 * 1000)
2034

@@ -73,6 +87,25 @@ describe('.files (the MFS API part)', function () {
7387
})
7488
})
7589

90+
HASH_ALGS.forEach((name) => {
91+
it(`files.add with hash=${name} and raw-leaves=false`, (done) => {
92+
const content = String(Math.random() + Date.now())
93+
const file = {
94+
path: content + '.txt',
95+
content: Buffer.from(content)
96+
}
97+
const options = { hash: name, 'raw-leaves': false }
98+
99+
ipfs.files.add([file], options, (err, res) => {
100+
if (err) return done(err)
101+
expect(res).to.have.length(1)
102+
const cid = new CID(res[0].hash)
103+
expect(mh.decode(cid.multihash).name).to.equal(name)
104+
done()
105+
})
106+
})
107+
})
108+
76109
it('files.mkdir', (done) => {
77110
ipfs.files.mkdir('/test-folder', done)
78111
})
@@ -230,6 +263,24 @@ describe('.files (the MFS API part)', function () {
230263
})
231264
})
232265

266+
HASH_ALGS.forEach((name) => {
267+
it(`files.add with hash=${name} and raw-leaves=false`, () => {
268+
const content = String(Math.random() + Date.now())
269+
const file = {
270+
path: content + '.txt',
271+
content: Buffer.from(content)
272+
}
273+
const options = { hash: name, 'raw-leaves': false }
274+
275+
return ipfs.files.add([file], options)
276+
.then((res) => {
277+
expect(res).to.have.length(1)
278+
const cid = new CID(res[0].hash)
279+
expect(mh.decode(cid.multihash).name).to.equal(name)
280+
})
281+
})
282+
})
283+
233284
it('files.mkdir', () => {
234285
return ipfs.files.mkdir('/test-folder')
235286
})

0 commit comments

Comments
 (0)