Files: 7f5b9989dbc45ee625499416f98f9ce28de20509 / test / validation.js
3455 bytesRaw
1 | |
2 | var tape = require('tape') |
3 | var pull = require('pull-stream') |
4 | var explain = require('explain-error') |
5 | var ssbKeys = require('ssb-keys') |
6 | |
7 | function encode (obj) { |
8 | return JSON.stringify(obj, null, 2) |
9 | } |
10 | |
11 | var u = require('./util') |
12 | |
13 | |
14 | var createFeed = require('../') |
15 | |
16 | var opts = ssbKeys |
17 | opts.keys = opts |
18 | |
19 | |
20 | u.sync(function (async) { |
21 | |
22 | var ssb = require('./mock')(async) |
23 | var create = require('../util').create |
24 | |
25 | var validate = require('../validator')(ssb, opts) |
26 | |
27 | tape('single', function (t) { |
28 | var keys = opts.keys.generate() |
29 | var msg = create(keys, null, {type: 'init', public: keys.public}) |
30 | |
31 | validate(msg, function (err, msg) { |
32 | if(err) throw err |
33 | ssb.get(msg.key, function (err, _msg) { |
34 | t.deepEqual(_msg, msg.value) |
35 | t.end() |
36 | }) |
37 | }) |
38 | }) |
39 | |
40 | tape('simple', function (t) { |
41 | var keys = opts.keys.generate() |
42 | var prev |
43 | var messages = [ |
44 | prev = create(keys, null, {type: 'init', public: keys.public}), |
45 | prev = create(keys, 'msg', 'hello', prev), |
46 | prev = create(keys, 'msg', 'hello2', prev) |
47 | ] |
48 | |
49 | var _msg = null |
50 | messages.forEach(function (msg) { |
51 | validate(msg, function (err) { |
52 | if(_msg) |
53 | t.equal('%'+opts.hash(encode(_msg)), msg.previous) |
54 | _msg = msg |
55 | if(err) throw err |
56 | if(msg.sequence === 3) |
57 | t.end() |
58 | }) |
59 | }) |
60 | }) |
61 | |
62 | tape('add & validate', function (t) { |
63 | var keys = opts.keys.generate() |
64 | var prev |
65 | ssb.add( |
66 | prev = create(keys, null, {type: 'init', public: keys.public}), |
67 | function (err) { |
68 | if(err) throw explain(err, 'init failed') |
69 | |
70 | ssb.add( |
71 | prev = create(keys, 'msg', 'hello', prev), |
72 | function (err) { |
73 | if(err) throw explain(err, 'hello failed') |
74 | |
75 | ssb.add( |
76 | prev = create(keys, 'msg', 'hello2', prev), |
77 | function (err) { |
78 | if(err) throw explain(err, 'hello2 failed') |
79 | pull( |
80 | ssb.createFeedStream({ keys: false }), |
81 | pull.collect(function (err, ary) { |
82 | if(err) throw explain(err, 'createFeedStream failed') |
83 | t.deepEqual(ary.pop(), prev) |
84 | t.end() |
85 | }) |
86 | ) |
87 | } |
88 | ) |
89 | } |
90 | ) |
91 | } |
92 | ) |
93 | }) |
94 | |
95 | tape('race: should queue', function (t) { |
96 | var keys = opts.keys.generate() |
97 | var prev, calls = 0 |
98 | ssb.add( |
99 | prev = create(keys,null, {type: 'init', public: keys.public}), |
100 | function (err) { |
101 | if(err) throw err |
102 | calls ++ |
103 | } |
104 | ) |
105 | ssb.add( |
106 | prev = create(keys, 'msg', 'hello', prev), |
107 | function (err) { |
108 | if(err) throw err |
109 | calls ++ |
110 | } |
111 | ) |
112 | ssb.add( |
113 | prev = create(keys, 'msg', 'hello2', prev), |
114 | function (err) { |
115 | if(err) throw err |
116 | calls ++ |
117 | } |
118 | ) |
119 | setTimeout(function () { |
120 | ssb.add( |
121 | prev = create(keys, 'msg', 'hello3', prev), |
122 | function (err) { |
123 | if(err) throw err |
124 | calls ++ |
125 | t.equal(calls, 4) |
126 | t.end() |
127 | } |
128 | ) |
129 | }) |
130 | }) |
131 | |
132 | tape('too big', function (t) { |
133 | var keys = opts.keys.generate() |
134 | var str = '' |
135 | for (var i=0; i < 808; i++) str += '1234567890' |
136 | |
137 | var msg = create(keys, null, {type: 'msg', value: str}) |
138 | |
139 | validate(msg, function (err, msg) { |
140 | if(!err) throw new Error('too big was allowed') |
141 | t.end() |
142 | }) |
143 | }) |
144 | |
145 | }) |
146 |
Built with git-ssb-web