Files: d5c6f9521130fcae13928a86e3f39d88e2586342 / test / block.js
7274 bytesRaw
1 | var cont = require('cont') |
2 | var tape = require('tape') |
3 | var pull = require('pull-stream') |
4 | var ssbKeys = require('ssb-keys') |
5 | var u = require('./util') |
6 | |
7 | var createSbot = require('../') |
8 | .use(require('../plugins/replicate')) |
9 | .use(require('ssb-friends')) |
10 | .use(require('ssb-ebt')) |
11 | |
12 | var toAddress = require('../lib/util').toAddress |
13 | |
14 | function once (fn) { |
15 | var called = 0 |
16 | return function () { |
17 | if(called++) throw new Error('called :'+called+' times!') |
18 | return fn.apply(this, arguments) |
19 | } |
20 | } |
21 | |
22 | // alice, bob, and carol all follow each other, |
23 | // but then bob offends alice, and she blocks him. |
24 | // this means that: |
25 | // |
26 | // 1. when bob tries to connect to alice, she refuses. |
27 | // 2. alice never tries to connect to bob. (removed from peers) |
28 | // 3. carol will not give bob any, she will not give him any data from alice. |
29 | |
30 | var crypto = require('crypto') |
31 | function seed (name) { |
32 | return crypto.createHash('sha256').update(name).digest() |
33 | } |
34 | |
35 | var alice = createSbot({ |
36 | temp:'test-block-alice', timeout: 1400, |
37 | keys:ssbKeys.generate(null, seed('alice')), |
38 | replicate: { legacy: false } |
39 | }) |
40 | |
41 | var bob = createSbot({ |
42 | temp: 'test-block-bob', timeout: 600, |
43 | keys:ssbKeys.generate(null, seed('bob')), |
44 | replicate: { legacy: false } |
45 | }) |
46 | |
47 | var carol = createSbot({ |
48 | temp: 'test-block-carol', timeout: 600, |
49 | keys:ssbKeys.generate(null, seed('carol')), |
50 | replicate: { legacy: false } |
51 | |
52 | }) |
53 | |
54 | var names = {} |
55 | names[alice.id] = 'alice' |
56 | names[bob.id] = 'bob' |
57 | names[carol.id] = 'carol' |
58 | |
59 | tape('alice blocks bob, and bob cannot connect to alice', function (t) { |
60 | |
61 | //in the beginning alice and bob follow each other |
62 | cont.para([ |
63 | cont(alice.publish)(u.follow(bob.id)), |
64 | cont(bob .publish)(u.follow(alice.id)), |
65 | cont(carol.publish)(u.follow(alice.id)) |
66 | ]) (function (err) { |
67 | if(err) throw err |
68 | var n = 3, rpc |
69 | |
70 | bob.connect(alice.getAddress(), function (err, _rpc) { |
71 | if(err) throw err |
72 | //replication will begin immediately. |
73 | rpc = _rpc |
74 | next() |
75 | }) |
76 | |
77 | //get the next messages that are replicated to alice and bob, |
78 | //and check that these are the correct follow messages. |
79 | var bobCancel = bob.post(once(function (op) { |
80 | //should be the alice's follow(bob) message. |
81 | t.equal(op.value.author, alice.id, 'bob expected message from alice') |
82 | t.equal(op.value.content.contact, bob.id, 'bob expected message to be about bob') |
83 | next() |
84 | }), false) |
85 | |
86 | var aliceCancel = alice.post(once(function (op) { |
87 | //should be the bob's follow(alice) message. |
88 | t.equal(op.value.author, bob.id, 'alice expected to receive a message from bob') |
89 | t.equal(op.value.content.contact, alice.id, 'alice expected received message to be about alice') |
90 | next() |
91 | }), false) |
92 | function next () { |
93 | if(--n) return |
94 | |
95 | rpc.close(true, function () { |
96 | aliceCancel(); bobCancel() |
97 | alice.publish(u.block(bob.id)) |
98 | (function (err) { |
99 | if(err) throw err |
100 | |
101 | alice.friends.get(null, function (err, g) { |
102 | if(err) throw err |
103 | t.equal(g[alice.id][bob.id], false) |
104 | |
105 | pull( |
106 | alice.links({ |
107 | source: alice.id, |
108 | dest: bob.id, |
109 | rel: 'contact', |
110 | values: true |
111 | }), |
112 | pull.filter(function (op) { |
113 | return op.value.content.flagged != null |
114 | }), |
115 | pull.collect(function (err, ary) { |
116 | if(err) throw err |
117 | console.log(ary) |
118 | t.ok(flagged = ary.pop().value.content.flagged, 'alice did block bob') |
119 | |
120 | //since bob is blocked, he should not be able to connect |
121 | bob.connect(alice.getAddress(), function (err, rpc) { |
122 | t.ok(err, 'bob is blocked, should fail to connect to alice') |
123 | |
124 | |
125 | var carolCancel = carol.post(function (msg) { |
126 | if(msg.author === alice.id) { |
127 | if(msg.sequence == 2) |
128 | t.end() |
129 | } |
130 | }) |
131 | |
132 | //but carol, should, because she is not blocked. |
133 | carol.connect(alice.getAddress(), function (err, rpc) { |
134 | if(err) throw err |
135 | rpc.on('closed', function () { |
136 | carolCancel() |
137 | pull( |
138 | carol.createHistoryStream({id: alice.id, seq: 0, live: false}), |
139 | pull.collect(function (err, ary) { |
140 | if(err) throw err |
141 | |
142 | t.ok(ary.length, 'carol replicated data from alice') |
143 | console.log(alice.id, carol.id, err, ary) |
144 | t.end() |
145 | }) |
146 | ) |
147 | }) |
148 | }) |
149 | }) |
150 | }) |
151 | ) |
152 | }) |
153 | }) |
154 | }) |
155 | } |
156 | }) |
157 | }) |
158 | |
159 | tape('carol does not let bob replicate with alice', function (t) { |
160 | //first, carol should have already replicated with alice. |
161 | //emits this event when did not allow bob to get this data. |
162 | bob.once('replicate:finish', function (vclock) { |
163 | t.equal(vclock[alice.id], 1) |
164 | //t.end() |
165 | }) |
166 | bob.connect(carol.getAddress(), function(err, rpc) { |
167 | if(err) throw err |
168 | rpc.on('closed', function () { |
169 | t.end() |
170 | }) |
171 | }) |
172 | }) |
173 | |
174 | tape('alice does not replicate messages from bob, but carol does', function (t) { |
175 | console.log("**********************************************************") |
176 | var friends = 0 |
177 | carol.friends.get(console.log) |
178 | pull(carol.friends.createFriendStream({meta: true, live: true}), pull.drain(function (v) { |
179 | friends ++ |
180 | console.log('************', v) |
181 | })) |
182 | |
183 | cont.para([ |
184 | cont(alice.publish)(u.follow(carol.id)), |
185 | cont(bob.publish)({type:'post', text: 'hello'}), |
186 | cont(carol.publish)(u.follow(bob.id)) |
187 | ]) (function (err, r) { |
188 | var recv = {alice: 0, carol: 0} |
189 | carol.post(function (msg) { |
190 | recv.carol ++ |
191 | //will receive one message from bob and carol |
192 | }, false) |
193 | |
194 | alice.post(function (msg) { |
195 | recv.alice ++ |
196 | //alice will only receive the message from carol, but not bob. |
197 | t.equal(msg.value.author, carol.id) |
198 | }, false) |
199 | |
200 | carol.friends.get(function (err, g) { |
201 | t.ok(g[carol.id][bob.id]) |
202 | }) |
203 | |
204 | var n = 2 |
205 | carol.connect(alice.getAddress(), cb) |
206 | carol.connect(bob.getAddress(), cb) |
207 | |
208 | function cb (err, rpc) { |
209 | if(err) throw err |
210 | rpc.on('closed', next) |
211 | } |
212 | function next () { |
213 | if(--n) return |
214 | pull( |
215 | carol.createLogStream(), |
216 | pull.collect(function (err, ary) { |
217 | if(err) throw err |
218 | carol.getVectorClock(function (err, vclock) { |
219 | t.equals(vclock[alice.id], 3) |
220 | t.equals(vclock[bob.id], 2) |
221 | t.equals(vclock[carol.id], 2) |
222 | |
223 | t.equal(friends, 3, "carol's createFriendStream has 3 peers") |
224 | t.end() |
225 | }) |
226 | }) |
227 | ) |
228 | } |
229 | }) |
230 | }) |
231 | |
232 | //TODO test that bob is disconnected from alice if he is connected |
233 | // and she blocks him. |
234 | |
235 | //TODO test that blocks work in realtime. if alice blocks him |
236 | // when he is already connected to alice's friend. |
237 | |
238 | tape('cleanup!', function (t) { |
239 | alice.close(true); bob.close(true); carol.close(true) |
240 | t.end() |
241 | }) |
242 | |
243 | |
244 | |
245 | |
246 | |
247 | |
248 | |
249 | |
250 | |
251 | |
252 | |
253 | |
254 | |
255 | |
256 | |
257 | |
258 | |
259 | |
260 | |
261 | |
262 | |
263 | |
264 | |
265 | |
266 |
Built with git-ssb-web