Skip to content

Commit b4c0785

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

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
function get (done) {
1812
send({
1913
path: 'object/get',
20-
args: multihash
14+
args: hash
2115
}, done)
2216
},
2317

@@ -27,7 +21,7 @@ module.exports = function (send, hash, callback) {
2721
// See https://github.com/ipfs/go-ipfs/issues/1582 for more details.
2822
send({
2923
path: 'object/data',
30-
args: multihash
24+
args: hash
3125
}, done)
3226
}],
3327

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)', () => {
1933
let ipfs
2034
let fc
@@ -59,6 +73,25 @@ describe('.files (the MFS API part)', () => {
5973
})
6074
})
6175

76+
HASH_ALGS.forEach((name) => {
77+
it(`files.add with hash=${name} and raw-leaves=false`, (done) => {
78+
const content = String(Math.random() + Date.now())
79+
const file = {
80+
path: content + '.txt',
81+
content: Buffer.from(content)
82+
}
83+
const options = { hash: name, 'raw-leaves': false }
84+
85+
ipfs.files.add([file], options, (err, res) => {
86+
if (err) return done(err)
87+
expect(res).to.have.length(1)
88+
const cid = new CID(res[0].hash)
89+
expect(mh.decode(cid.multihash).name).to.equal(name)
90+
done()
91+
})
92+
})
93+
})
94+
6295
it('files.mkdir', (done) => {
6396
ipfs.files.mkdir('/test-folder', done)
6497
})
@@ -196,6 +229,24 @@ describe('.files (the MFS API part)', () => {
196229
})
197230
})
198231

232+
HASH_ALGS.forEach((name) => {
233+
it(`files.add with hash=${name} and raw-leaves=false`, () => {
234+
const content = String(Math.random() + Date.now())
235+
const file = {
236+
path: content + '.txt',
237+
content: Buffer.from(content)
238+
}
239+
const options = { hash: name, 'raw-leaves': false }
240+
241+
return ipfs.files.add([file], options)
242+
.then((res) => {
243+
expect(res).to.have.length(1)
244+
const cid = new CID(res[0].hash)
245+
expect(mh.decode(cid.multihash).name).to.equal(name)
246+
})
247+
})
248+
})
249+
199250
it('files.mkdir', () => {
200251
return ipfs.files.mkdir('/test-folder')
201252
})

0 commit comments

Comments
 (0)