Commit a90889a879dea8da10d898d7714c795c81df85e0
test meta()
Dominic Tarr committed on 4/20/2016, 3:33:58 AMParent: cfd64636ed26aacf9abe6a98e786c83caada3dc7
Files changed
test/index.js | changed |
test/index.js | ||
---|---|---|
@@ -20,129 +20,146 @@ | ||
20 | 20 | |
21 | 21 | var l = 100, random2 = [] |
22 | 22 | while(l --) random2.push(crypto.randomBytes(1024)) |
23 | 23 | |
24 | +module.exports = function (alg) { | |
24 | 25 | |
26 | + var start = Date.now() | |
25 | 27 | |
26 | -module.exports = function (alg) { | |
28 | + function hasher (ary) { | |
29 | + var hasher = util.createHash(alg, true) | |
30 | + pull(pull.values(ary), hasher, pull.drain()) | |
31 | + return util.encode(hasher.digest, alg) | |
32 | + } | |
27 | 33 | |
28 | -function hasher (ary) { | |
29 | - var hasher = util.createHash(alg, true) | |
30 | - pull(pull.values(ary), hasher, pull.drain()) | |
31 | - return util.encode(hasher.digest, alg) | |
32 | -} | |
34 | + var hash1 = hasher(random1) | |
35 | + var hash2 = hasher(random2) | |
33 | 36 | |
34 | -var hash1 = hasher(random1) | |
35 | -var hash2 = hasher(random2) | |
37 | + var blobs = Blobs({dir: dirname, hash: alg}) | |
36 | 38 | |
37 | -var blobs = Blobs({dir: dirname, hash: alg}) | |
39 | + tape('add, get, has, ls', function (t) { | |
38 | 40 | |
39 | -tape('add, get, has, ls', function (t) { | |
41 | + pull( | |
42 | + pull.values(random1), | |
43 | + blobs.add(function (err, hash) { | |
44 | + if(err) throw err | |
45 | + t.equal(hash, hash1) | |
46 | + blobs.has(hash, function (_, has) { | |
47 | + t.ok(has) | |
48 | + t.end() | |
49 | + }) | |
50 | + }) | |
51 | + ) | |
40 | 52 | |
41 | - pull( | |
42 | - pull.values(random1), | |
43 | - blobs.add(function (err, hash) { | |
44 | - if(err) throw err | |
45 | - t.equal(hash, hash1) | |
46 | - blobs.has(hash, function (_, has) { | |
47 | - t.ok(has) | |
53 | + }) | |
54 | + | |
55 | + tape('add accepts the correct hash', function (t) { | |
56 | + | |
57 | + pull( | |
58 | + pull.values(random1), | |
59 | + blobs.add(hash1, function (err, hash) { | |
60 | + if(err) throw err | |
61 | + t.equal(hash, hash1) | |
48 | 62 | t.end() |
49 | 63 | }) |
50 | - }) | |
51 | - ) | |
64 | + ) | |
52 | 65 | |
53 | -}) | |
66 | + }) | |
54 | 67 | |
55 | -tape('add accepts the correct hash', function (t) { | |
68 | + tape('add errors with incorrect hash', function (t) { | |
56 | 69 | |
57 | - pull( | |
58 | - pull.values(random1), | |
59 | - blobs.add(hash1, function (err, hash) { | |
60 | - if(err) throw err | |
61 | - t.equal(hash, hash1) | |
70 | + pull( | |
71 | + pull.values(random2), | |
72 | + blobs.add(hash1, function (err, hash) { | |
73 | + t.ok(err) | |
74 | + t.equal(hash, hash2) | |
75 | + //if the hash was wrong, do not add it. | |
76 | + blobs.has(hash, function (_, has) { | |
77 | + console.log(_, has) | |
78 | + t.notOk(has) | |
79 | + t.end() | |
80 | + }) | |
81 | + }) | |
82 | + ) | |
83 | + | |
84 | + }) | |
85 | + | |
86 | + tape('has can take array', function (t) { | |
87 | + blobs.has([hash1, hash2], function (_, ary) { | |
88 | + t.deepEqual(ary, [true, false]) | |
62 | 89 | t.end() |
63 | 90 | }) |
64 | - ) | |
91 | + }) | |
65 | 92 | |
66 | -}) | |
93 | + tape('meta returns {id, size, ts}', function (t) { | |
94 | + blobs.meta(hash1, function (err, meta) { | |
95 | + if(err) throw err | |
96 | + t.ok(meta) | |
97 | + t.equal(meta.id, hash1) | |
98 | + t.equal(meta.size, 100*1024) | |
99 | + t.ok(meta.ts > start) | |
100 | + t.ok(meta.ts < Date.now()) | |
67 | 101 | |
68 | -tape('add errors with incorrect hash', function (t) { | |
69 | - | |
70 | - pull( | |
71 | - pull.values(random2), | |
72 | - blobs.add(hash1, function (err, hash) { | |
73 | - t.ok(err) | |
74 | - t.equal(hash, hash2) | |
75 | - //if the hash was wrong, do not add it. | |
76 | - blobs.has(hash, function (_, has) { | |
77 | - console.log(_, has) | |
78 | - t.notOk(has) | |
79 | - t.end() | |
80 | - }) | |
102 | + t.end() | |
81 | 103 | }) |
82 | - ) | |
104 | + }) | |
83 | 105 | |
84 | -}) | |
85 | 106 | |
86 | -tape('has can take array', function (t) { | |
87 | - blobs.has([hash1, hash2], function (_, ary) { | |
88 | - t.deepEqual(ary, [true, false]) | |
89 | - t.end() | |
90 | - }) | |
91 | -}) | |
107 | + tape('ls streams the list of hashes', function (t) { | |
92 | 108 | |
93 | -tape('ls streams the list of hashes', function (t) { | |
109 | + pull(pull.values(random2), blobs.add(function (err) { | |
110 | + if(err) throw err | |
94 | 111 | |
95 | - pull(pull.values(random2), blobs.add(function (err) { | |
96 | - if(err) throw err | |
112 | + pull( | |
113 | + blobs.ls(), | |
114 | + pull.collect(function (err, ary) { | |
115 | + if(err) throw err | |
116 | + t.deepEqual(ary.sort(), [hash1, hash2].sort()) | |
97 | 117 | |
98 | - pull( | |
99 | - blobs.ls(), | |
100 | - pull.collect(function (err, ary) { | |
101 | - t.deepEqual(ary.sort(), [hash1, hash2].sort()) | |
102 | 118 | |
119 | + pull( | |
120 | + blobs.ls({long: true}), | |
121 | + pull.collect(function (err, ary) { | |
122 | + t.notOk(err) | |
123 | + t.equal(ary.length, 2) | |
124 | + console.log(ary) | |
125 | + t.deepEqual(ary.map(function (e) { | |
126 | + t.ok(e.ts < Date.now()) | |
127 | + t.equal(e.size, 102400) | |
128 | + return e.id | |
129 | + }).sort(), [hash1, hash2].sort()) | |
130 | + t.end() | |
131 | + }) | |
132 | + ) | |
103 | 133 | |
104 | - pull( | |
105 | - blobs.ls({long: true}), | |
106 | - pull.collect(function (err, ary) { | |
107 | - t.notOk(err) | |
108 | - t.equal(ary.length, 2) | |
109 | - console.log(ary) | |
110 | - t.deepEqual(ary.map(function (e) { | |
111 | - t.ok(e.ts < Date.now()) | |
112 | - t.equal(e.size, 102400) | |
113 | - return e.id | |
114 | - }).sort(), [hash1, hash2].sort()) | |
115 | - t.end() | |
116 | - }) | |
117 | - ) | |
134 | + }) | |
135 | + ) | |
118 | 136 | |
137 | + })) | |
138 | + | |
139 | + }) | |
140 | + return | |
141 | + //sometimes there are apis that need direct access | |
142 | + //i.e. in electron. | |
143 | + tape('resolve - direct access to the same file', function (t) { | |
144 | + var filename = blobs.resolve(hash1) | |
145 | + var hasher = util.createHash(alg, true) | |
146 | + pull( | |
147 | + Read(filename), | |
148 | + hasher, | |
149 | + pull.drain(null, function (err) { | |
150 | + t.notOk(err) | |
151 | + t.equal(util.encode(hasher.digest, alg), hash1) | |
152 | + t.end() | |
119 | 153 | }) |
120 | 154 | ) |
155 | + }) | |
121 | 156 | |
122 | - })) | |
123 | - | |
124 | -}) | |
125 | - | |
126 | -//sometimes there are apis that need direct access | |
127 | -//i.e. in electron. | |
128 | -tape('resolve - direct access to the same file', function (t) { | |
129 | - var filename = blobs.resolve(hash1) | |
130 | - var hasher = util.createHash(alg, true) | |
131 | - pull( | |
132 | - Read(filename), | |
133 | - hasher, | |
134 | - pull.drain(null, function (err) { | |
135 | - t.notOk(err) | |
136 | - t.equal(util.encode(hasher.digest, alg), hash1) | |
137 | - t.end() | |
138 | - }) | |
139 | - ) | |
140 | -}) | |
141 | - | |
142 | 157 | } |
143 | 158 | |
144 | 159 | if(!module.parent) |
145 | 160 | module.exports('blake2s') |
146 | 161 | |
147 | 162 | |
148 | 163 | |
164 | + | |
165 | + |
Built with git-ssb-web