-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtest.js
47 lines (41 loc) · 1.44 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const test = require('ava')
const fs = require('fs')
const path = require('path')
const os = require('os')
const IPFS = require('ipfs')
const CID = require('cids')
const crypto = require('crypto')
const Hash = require('.')
test('should calculate the IPFS hash of a buffer', async t => {
const data = Buffer.from('hello world\n')
const hash = await Hash.of(data)
t.is(hash, 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o')
})
test('should calculate the IPFS hash of a string', async t => {
const data = 'hello world\n'
const hash = await Hash.of(data)
t.is(hash, 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o')
})
test('should calculate the IPFS hash of an async iterator', async t => {
const stream = fs.createReadStream(path.join(__dirname, 'hello'))
const hash = await Hash.of(stream)
t.is(hash, 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o')
})
test('should produce the same hash as IPFS', async t => {
const datas = [
Buffer.from('TEST' + Date.now()),
crypto.randomBytes(1024 * 1024 * 15)
]
const ipfs = await IPFS.create({ repo: path.join(os.tmpdir(), `${Date.now()}`) })
for (const data of datas) {
const { cid } = await ipfs.add(data)
const hash = await Hash.of(data)
t.is(cid.toString(), hash)
}
})
test('should take CID version option', async t => {
const data = Buffer.from('TEST' + Date.now())
const hash = await Hash.of(data, { cidVersion: 1 })
const cid = new CID(hash)
t.is(cid.version, 1)
})