@@ -8,13 +8,27 @@ const expect = chai.expect
8
8
chai . use ( dirtyChai )
9
9
const isNode = require ( 'detect-node' )
10
10
const loadFixture = require ( 'aegir/fixtures' )
11
+ const mh = require ( 'multihashes' )
12
+ const CID = require ( 'cids' )
11
13
12
14
const FactoryClient = require ( './ipfs-factory/client' )
13
15
14
16
const testfile = isNode
15
17
? loadFixture ( __dirname , '/fixtures/testfile.txt' )
16
18
: loadFixture ( __dirname , 'fixtures/testfile.txt' )
17
19
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
+
18
32
describe ( '.files (the MFS API part)' , function ( ) {
19
33
this . timeout ( 120 * 1000 )
20
34
@@ -73,6 +87,25 @@ describe('.files (the MFS API part)', function () {
73
87
} )
74
88
} )
75
89
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
+
76
109
it ( 'files.mkdir' , ( done ) => {
77
110
ipfs . files . mkdir ( '/test-folder' , done )
78
111
} )
@@ -230,6 +263,24 @@ describe('.files (the MFS API part)', function () {
230
263
} )
231
264
} )
232
265
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
+
233
284
it ( 'files.mkdir' , ( ) => {
234
285
return ipfs . files . mkdir ( '/test-folder' )
235
286
} )
0 commit comments