git ssb

4+

Dominic / scuttlebot



Tree: ed7e4997927bef14491b40767dbd64f58f2d246e

Files: ed7e4997927bef14491b40767dbd64f58f2d246e / test / block.js

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

Built with git-ssb-web