git ssb

3+

cel / ssb-npm-registry



Tree: d2f2697f296dd39aed6a8b63c6d04a736a7db5b3

Files: d2f2697f296dd39aed6a8b63c6d04a736a7db5b3 / node_modules / pull-hash / test.js

1947 bytesRaw
1var test = require('tape')
2var hash = require('.')
3var crypto = require('crypto')
4var fs = require('fs')
5var pull = require('pull-stream')
6var toPull = require('stream-to-pull-stream')
7var multicb = require('multicb')
8var path = require('path')
9var gitHash = require('./ext/git')
10var ssbHash = require('./ext/ssb')
11
12function hashFile(filename, type, encoding, cb) {
13 var shasum = crypto.createHash(type)
14 fs.createReadStream(filename).on('data', function (d) {
15 shasum.update(d)
16 })
17 .on('error', cb)
18 .on('end', function () {
19 cb(null, shasum.digest(encoding))
20 })
21}
22
23function hashFilePull(filename, type, encoding, cb) {
24 pull(
25 toPull(fs.createReadStream(filename)),
26 hash(type, encoding, cb),
27 pull.drain()
28 )
29}
30
31test('hash a file', function (t) {
32 var done = multicb({ pluck: 1, spread: true })
33 hashFile(__filename, 'md5', 'hex', done())
34 hashFilePull(__filename, 'md5', 'hex', done())
35 done(function (err, digestNodejs, digestPull) {
36 t.error(err, 'hashes')
37 t.equals(digestPull, digestNodejs, 'hash')
38 t.end()
39 })
40})
41
42test('git object hash', function (t) {
43 pull(
44 pull.once('asdf\n'),
45 gitHash({type: 'blob', size: 5}, function (err, digest) {
46 t.error(err, 'git hash')
47 t.equals(digest, '8bd6648ed130ac9ece0f89cd9a8fbbfd2608427a', 'hash')
48 t.end()
49 }),
50 pull.drain()
51 )
52})
53
54test('empty git blob', function (t) {
55 var emptyId = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'
56 pull(
57 pull.empty(),
58 gitHash({type: 'blob', length: 0}, function (err, digest) {
59 t.error(err, 'git hash')
60 t.equals(digest, emptyId, 'blob id')
61 t.end()
62 }),
63 pull.drain()
64 )
65})
66
67test('ssb blob id', function (t) {
68 var emptyId = '&47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=.sha256'
69 pull(
70 pull.empty(),
71 ssbHash(function (err, digest) {
72 t.error(err, 'ssb hash')
73 t.equals(digest, emptyId, 'blob id')
74 t.end()
75 }),
76 pull.drain()
77 )
78})
79

Built with git-ssb-web