git ssb

1+

Dominic / secure-scuttlebutt



Tree: 6dde1c5e1d32a7427848bfad8a4641577bd0d1ab

Files: 6dde1c5e1d32a7427848bfad8a4641577bd0d1ab / test / validation.js

3582 bytesRaw
1'use strict'
2var tape = require('tape')
3var level = require('level-test')()
4var sublevel = require('level-sublevel/bytewise')
5var pull = require('pull-stream')
6var explain = require('explain-error')
7var generate = require('ssb-keys').generate
8var hash = require('ssb-keys').hash
9
10var codec = require('../codec')
11
12module.exports = function (opts) {
13
14 var db = sublevel(level('test-ssb-validate', {
15 valueEncoding: codec
16 }))
17
18 var create = require('ssb-feed/util').create
19 var ssb = require('../')(db, opts)
20
21 var validate = require('ssb-feed/validator')(ssb)
22
23 tape('simple', function (t) {
24 var keys = generate()
25 var prev
26 var messages = [
27 prev = create(keys, null, {type: 'init', public: keys.public}),
28 prev = create(keys, 'msg', 'hello', prev),
29 prev = create(keys, 'msg', 'hello2', prev)
30 ]
31
32 console.log(messages)
33
34 var _msg = null
35 messages.forEach(function (msg) {
36 validate(msg, function (err) {
37 console.log('HELLO', hash('HELLLO'))
38 if(_msg)
39 t.deepEqual('%'+hash(codec.encode(_msg)), msg.previous)
40 _msg = msg
41 if(err) throw err
42 if(msg.sequence === 3)
43 t.end()
44 })
45 })
46 })
47
48 tape('add & validate', function (t) {
49 var keys = generate()
50 var prev
51 ssb.add(
52 prev = create(keys, null, {type: 'init', public: keys.public}),
53 function (err) {
54 if(err) throw explain(err, 'init failed')
55
56 ssb.add(
57 prev = create(keys, 'msg', 'hello', prev),
58 function (err) {
59 if(err) throw explain(err, 'hello failed')
60
61 ssb.add(
62 prev = create(keys, 'msg', 'hello2', prev),
63 function (err) {
64 if(err) throw explain(err, 'hello2 failed')
65 pull(
66 db.createFeedStream({ keys: false }),
67 pull.collect(function (err, ary) {
68 if(err) throw explain(err, 'createFeedStream failed')
69 t.deepEqual(ary.pop(), prev)
70 t.end()
71 })
72 )
73 }
74 )
75 }
76 )
77 }
78 )
79 })
80
81 tape('race: should queue', function (t) {
82 var keys = generate()
83 var prev, calls = 0
84 ssb.add(
85 prev = create(keys,null, {type: 'init', public: keys.public}),
86 function (err) {
87 if(err) throw err
88 calls ++
89 }
90 )
91 ssb.add(
92 prev = create(keys, 'msg', 'hello', prev),
93 function (err) {
94 if(err) throw err
95 calls ++
96 }
97 )
98 ssb.add(
99 prev = create(keys, 'msg', 'hello2', prev),
100 function (err) {
101 if(err) throw err
102 calls ++
103 }
104 )
105 setTimeout(function () {
106 ssb.add(
107 prev = create(keys, 'msg', 'hello3', prev),
108 function (err) {
109 if(err) throw err
110 calls ++
111 t.equal(calls, 4)
112 t.end()
113 }
114 )
115 })
116
117
118 })
119
120 //when an add fails, you should still be able to add another
121 //message if you wait until it has returned.
122
123 tape('too big', function (t) {
124 var keys = generate()
125 var feed = ssb.createFeed(keys)
126 var str = ''
127 for (var i=0; i < 808; i++) str += '1234567890'
128 feed.add({ type: 'msg', value: str }, function (err, msg) {
129 if(!err) throw new Error('too big was allowed')
130 console.log(err)
131 feed.add({ type: 'msg', value: 'this ones cool tho' }, function (err) {
132 if (err) throw err
133 t.end()
134 })
135 })
136 })
137}
138
139if(!module.parent)
140 module.exports(require('../defaults'))
141
142
143
144

Built with git-ssb-web