Files: 68d5803b6db1f8ca946875a36fd4b720cf8f8255 / test.js
1947 bytesRaw
1 | var test = require('tape') |
2 | var hash = require('.') |
3 | var crypto = require('crypto') |
4 | var fs = require('fs') |
5 | var pull = require('pull-stream') |
6 | var toPull = require('stream-to-pull-stream') |
7 | var multicb = require('multicb') |
8 | var path = require('path') |
9 | var gitHash = require('./ext/git') |
10 | var ssbHash = require('./ext/ssb') |
11 | |
12 | function 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 | |
23 | function hashFilePull(filename, type, encoding, cb) { |
24 | pull( |
25 | toPull(fs.createReadStream(filename)), |
26 | hash(type, encoding, cb), |
27 | pull.drain() |
28 | ) |
29 | } |
30 | |
31 | test('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 | |
42 | test('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 | |
54 | test('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 | |
67 | test('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