git ssb

1+

Dominic / secure-scuttlebutt



Tree: 5f4e5cc3e56c96ddb795f9c1966bc83775100b01

Files: 5f4e5cc3e56c96ddb795f9c1966bc83775100b01 / test / validation.js

4811 bytesRaw
1'use strict'
2var tape = require('tape')
3var pull = require('pull-stream')
4var explain = require('explain-error')
5var generate = require('ssb-keys').generate
6var hash = require('ssb-keys').hash
7var v = require('ssb-validate')
8var createSSB = require('./util')
9
10module.exports = function (opts) {
11 var create = require('ssb-feed/util').create
12 var ssb = createSSB('test-ssb-validate')
13
14 tape('simple', function (t) {
15 var keys = generate()
16 console.log('keys', keys)
17 var prev
18 var messages = [
19 prev = create(keys, null, { type: 'init', public: keys.public }),
20 prev = create(keys, 'msg', 'hello', prev),
21 prev = create(keys, 'msg', 'hello2', prev)
22 ]
23 ssb.queue(messages[0], function () {})
24 ssb.queue(messages[1], function () {})
25 ssb.queue(messages[2], function () {
26 t.end()
27 })
28 })
29
30 tape('add & validate', function (t) {
31 var keys = generate()
32 var prev
33 ssb.add(
34 prev = create(keys, null, { type: 'init', public: keys.public }),
35 function (err) {
36 if (err) throw explain(err, 'init failed')
37
38 ssb.add(
39 prev = create(keys, 'msg', 'hello', prev),
40 function (err) {
41 if (err) throw explain(err, 'hello failed')
42
43 ssb.add(
44 prev = create(keys, 'msg', 'hello2', prev),
45 function (err) {
46 if (err) throw explain(err, 'hello2 failed')
47 pull(
48 ssb.createFeedStream({ keys: false }),
49 pull.collect(function (err, ary) {
50 if (err) throw explain(err, 'createFeedStream failed')
51 t.deepEqual(ary.pop(), prev)
52 t.end()
53 })
54 )
55 }
56 )
57 }
58 )
59 }
60 )
61 })
62
63 tape('race: should queue', function (t) {
64 var keys = generate()
65 var prev
66 var calls = 0
67 ssb.add(
68 prev = create(keys, null, { type: 'init', public: keys.public }),
69 function (err) {
70 if (err) throw err
71 calls++
72 }
73 )
74 ssb.add(
75 prev = create(keys, 'msg', 'hello', prev),
76 function (err) {
77 if (err) throw err
78 calls++
79 }
80 )
81 ssb.add(
82 prev = create(keys, 'msg', 'hello2', prev),
83 function (err) {
84 if (err) throw err
85 calls++
86 }
87 )
88 setTimeout(function () {
89 ssb.add(
90 prev = create(keys, 'msg', 'hello3', prev),
91 function (err) {
92 if (err) throw err
93 calls++
94 t.equal(calls, 4)
95 t.end()
96 }
97 )
98 })
99 })
100
101 // when an add fails, you should still be able to add another
102 // message if you wait until it has returned.
103
104 tape('too big', function (t) {
105 var keys = generate()
106 var feed = ssb.createFeed(keys)
107 var str = ''
108 for (var i = 0; i < 808; i++) str += '1234567890'
109 feed.add({ type: 'msg', value: str }, function (err, msg) {
110 if (!err) throw new Error('too big was allowed')
111 console.log(err)
112 feed.add({ type: 'msg', value: 'this ones cool tho' }, function (err) {
113 if (err) throw err
114 t.end()
115 })
116 })
117 })
118
119 // git-ssb is known to change the order of the message
120 tape('dict order', function (t) {
121 var keys = generate()
122 var prev
123 ssb.add(
124 prev = create(keys, null, { type: 'init', public: keys.public }),
125 function (err) {
126 if (err) throw explain(err, 'init failed')
127
128 ssb.add(
129 prev = require('ssb-keys').signObj(keys, null, {
130 previous: ('%' + hash(JSON.stringify(prev, null, 2))),
131 sequence: prev ? prev.sequence + 1 : 1,
132 author: keys.id,
133 timestamp: require('monotonic-timestamp')(),
134 hash: 'sha256',
135 content: { type: 'msg', value: 'hello' }
136 }),
137 function (err) {
138 if (err) throw explain(err, 'hello failed')
139
140 ssb.add(
141 prev = create(keys, 'msg', 'hello2', prev),
142 function (err) {
143 if (err) throw explain(err, 'hello2 failed')
144
145 var state = {
146 feeds: {}, queue: []
147 }
148
149 pull(
150 ssb.createFeedStream({ keys: false }),
151 pull.drain(function (msg) {
152 try {
153 state = v.append(state, null, msg)
154 } catch (ex) {
155 t.fail(ex)
156 return false
157 }
158 }, function () {
159 if (state.queue.length > 0) { t.pass('validate passes') }
160 t.end()
161 })
162 )
163 }
164 )
165 }
166 )
167 }
168 )
169 })
170}
171
172if (!module.parent) { module.exports(require('../')) }
173

Built with git-ssb-web