git ssb

0+

cryptop / multiblob



forked from Dominic / multiblob

Commit a90889a879dea8da10d898d7714c795c81df85e0

test meta()

Dominic Tarr committed on 4/20/2016, 3:33:58 AM
Parent: cfd64636ed26aacf9abe6a98e786c83caada3dc7

Files changed

test/index.jschanged
test/index.jsView
@@ -20,129 +20,146 @@
2020
2121 var l = 100, random2 = []
2222 while(l --) random2.push(crypto.randomBytes(1024))
2323
24+module.exports = function (alg) {
2425
26+ var start = Date.now()
2527
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+ }
2733
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)
3336
34-var hash1 = hasher(random1)
35-var hash2 = hasher(random2)
37+ var blobs = Blobs({dir: dirname, hash: alg})
3638
37-var blobs = Blobs({dir: dirname, hash: alg})
39+ tape('add, get, has, ls', function (t) {
3840
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+ )
4052
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)
4862 t.end()
4963 })
50- })
51- )
64+ )
5265
53-})
66+ })
5467
55-tape('add accepts the correct hash', function (t) {
68+ tape('add errors with incorrect hash', function (t) {
5669
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])
6289 t.end()
6390 })
64- )
91+ })
6592
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())
67101
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()
81103 })
82- )
104+ })
83105
84-})
85106
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) {
92108
93-tape('ls streams the list of hashes', function (t) {
109+ pull(pull.values(random2), blobs.add(function (err) {
110+ if(err) throw err
94111
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())
97117
98- pull(
99- blobs.ls(),
100- pull.collect(function (err, ary) {
101- t.deepEqual(ary.sort(), [hash1, hash2].sort())
102118
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+ )
103133
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+ )
118136
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()
119153 })
120154 )
155+ })
121156
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-
142157 }
143158
144159 if(!module.parent)
145160 module.exports('blake2s')
146161
147162
148163
164+
165+

Built with git-ssb-web