@@ -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)' , ( ) => {
19
33
let ipfs
20
34
let fc
@@ -59,6 +73,25 @@ describe('.files (the MFS API part)', () => {
59
73
} )
60
74
} )
61
75
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
+
62
95
it ( 'files.mkdir' , ( done ) => {
63
96
ipfs . files . mkdir ( '/test-folder' , done )
64
97
} )
@@ -196,6 +229,24 @@ describe('.files (the MFS API part)', () => {
196
229
} )
197
230
} )
198
231
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
+
199
250
it ( 'files.mkdir' , ( ) => {
200
251
return ipfs . files . mkdir ( '/test-folder' )
201
252
} )
0 commit comments