Files: c0e9a969c5c0484a867b4ca5508beaeff462f603 / test / integration.js
5500 bytesRaw
1 | var tape = require('tape') |
2 | var Blobs = require('../inject') |
3 | var pull = require('pull-stream') |
4 | var bitflipper = require('pull-bitflipper') |
5 | var assert = require('assert') |
6 | |
7 | var u = require('./util') |
8 | var Fake = u.fake |
9 | var hash = u.hash |
10 | |
11 | module.exports = function (createBlobs, createAsync) { |
12 | |
13 | |
14 | function log (name) { |
15 | if(LOGGING) |
16 | return pull.through(function (e) { |
17 | console.log(name, e) |
18 | }) |
19 | else |
20 | return pull.through() |
21 | } |
22 | |
23 | tape('want - has', function (t) { |
24 | createAsync(function (async) { |
25 | var alice = createBlobs('alice', async) |
26 | var bob = createBlobs('bob', async) |
27 | var blob = Fake('foobar', 64) |
28 | var h = hash(blob) |
29 | |
30 | u.peers('alice', alice, 'bob', bob)//, async) |
31 | |
32 | alice.want(h, function (err, has) { |
33 | if(err) throw err |
34 | console.log('ALICE has?', h, has) |
35 | alice.has(h, function (err, has) { |
36 | console.log('ALICE has!', h, has) |
37 | if(err) throw err |
38 | assert.ok(has) |
39 | async.done() |
40 | }) |
41 | }) |
42 | |
43 | pull(pull.once(blob), bob.add()) |
44 | }, function (err) { |
45 | if(err) throw err |
46 | t.end() |
47 | }) |
48 | }) |
49 | |
50 | tape('want - has 2', function (t) { |
51 | createAsync(function (async) { |
52 | var alice = createBlobs('alice', async) |
53 | var bob = createBlobs('bob', async) |
54 | var blob = Fake('foobar', 64) |
55 | var h = hash(blob) |
56 | |
57 | u.peers('bob', bob, 'alice', alice) |
58 | pull(pull.once(blob), bob.add()) |
59 | |
60 | alice.want(h, function (err, has) { |
61 | if(err) throw err |
62 | alice.has(h, function (err, has) { |
63 | if(err) throw err |
64 | assert.ok(has) |
65 | async.done() |
66 | }) |
67 | }) |
68 | }, function (err) { |
69 | if(err) throw err |
70 | t.end() |
71 | }) |
72 | }) |
73 | |
74 | tape('want - want -has', function (t) { |
75 | createAsync(function (async) { |
76 | var alice = createBlobs('alice', async) |
77 | var bob = createBlobs('bob', async) |
78 | var carol = createBlobs('carol', async) |
79 | |
80 | var blob = Fake('baz', 64) |
81 | var h = hash(blob) |
82 | |
83 | u.peers('alice', alice, 'bob', bob) |
84 | u.peers('bob', bob, 'carol', carol) |
85 | |
86 | alice.want(h, function (err, has) { |
87 | if(err) throw err |
88 | alice.has(h, function (err, has) { |
89 | if(err) throw err |
90 | assert.ok(has) |
91 | async.done() |
92 | }) |
93 | }) |
94 | |
95 | pull(pull.once(blob), carol.add()) |
96 | }, function (err) { |
97 | if(err) throw err |
98 | t.end() |
99 | }) |
100 | }) |
101 | |
102 | |
103 | tape('peers want what you have', function (t) { |
104 | createAsync(function (async) { |
105 | if(Array.isArray(process._events['exit'])) |
106 | console.log(process._events['exit'].reverse()) |
107 | var alice = createBlobs('alice', async) |
108 | var bob = createBlobs('bob', async) |
109 | var carol = createBlobs('carol', async) |
110 | |
111 | var blob = Fake('baz', 64) |
112 | var h = hash(blob) |
113 | |
114 | u.peers('alice', alice, 'bob', bob) |
115 | u.peers('bob', bob, 'carol', carol) |
116 | |
117 | pull( |
118 | carol.changes(), |
119 | pull.drain(function (_h) { |
120 | assert.equal(_h, h) |
121 | async.done() |
122 | }) |
123 | ) |
124 | |
125 | alice.want(h, function () {}) |
126 | pull(pull.once(blob), alice.add()) |
127 | }, function (err) { |
128 | if(err) throw err |
129 | t.end() |
130 | }) |
131 | }) |
132 | |
133 | |
134 | tape('triangle', function (t) { |
135 | createAsync(function (async) { |
136 | var n = 0 |
137 | var alice = createBlobs('alice', async) |
138 | var bob = createBlobs('bob', async) |
139 | var carol = createBlobs('carol', async) |
140 | |
141 | var blob = Fake('baz', 64) |
142 | var h = hash(blob) |
143 | |
144 | u.peers('alice', alice, 'bob', bob) |
145 | u.peers('bob', bob, 'carol', carol) |
146 | |
147 | pull( |
148 | bob.changes(), |
149 | pull.drain(function (_h) { |
150 | assert.equal(_h, h) |
151 | async.done() |
152 | }) |
153 | ) |
154 | |
155 | pull(pull.once(blob), alice.add()) |
156 | pull(pull.once(blob), carol.add()) |
157 | |
158 | bob.want(h, function () {}) |
159 | }, function (err) { |
160 | if(err) throw err |
161 | t.end() |
162 | }) |
163 | }) |
164 | |
165 | tape('corrupt', function (t) { |
166 | createAsync(function (async) { |
167 | var n = 0 |
168 | var alice = createBlobs('alice', async) |
169 | var bob = createBlobs('bob', async) |
170 | var carol = createBlobs('carol', async) |
171 | |
172 | //everything that comes from bob is corrupt |
173 | var get = alice.get |
174 | alice.get = function (id) { |
175 | return pull(get(id), bitflipper(1)) |
176 | } |
177 | |
178 | var blob = Fake('baz', 64) |
179 | var h = hash(blob) |
180 | |
181 | u.peers('alice', alice, 'bob', bob) |
182 | u.peers('bob', bob, 'carol', carol) |
183 | |
184 | pull( |
185 | bob.changes(), |
186 | pull.drain(function (_h) { |
187 | console.log('HAS', _h) |
188 | assert.equal(_h, h) |
189 | async.done() |
190 | }) |
191 | ) |
192 | |
193 | bob.want(h, function () {}) |
194 | pull(pull.once(blob), alice.add()) |
195 | pull(pull.once(blob), carol.add()) |
196 | }, function (err) { |
197 | if(err) throw err |
198 | t.end() |
199 | }) |
200 | }) |
201 | |
202 | tape('cycle', function (t) { |
203 | createAsync(function (async) { |
204 | var n = 0 |
205 | var alice = createBlobs('alice', async) |
206 | var bob = createBlobs('bob', async) |
207 | var carol = createBlobs('carol', async) |
208 | var dan = createBlobs('dan', async) |
209 | u.peers('alice', alice, 'bob', bob) |
210 | u.peers('bob', bob, 'carol', carol) |
211 | u.peers('carol', carol, 'dan', dan) |
212 | u.peers('dan', dan, 'alice', alice) |
213 | |
214 | var blob = Fake('gurg', 64) |
215 | var h = hash(blob) |
216 | alice.want(h, function (err, has) { |
217 | async.done() |
218 | }) |
219 | |
220 | pull(pull.once(blob), dan.add(h)) |
221 | }, function (err) { |
222 | if(err) throw err |
223 | t.end() |
224 | }) |
225 | |
226 | }) |
227 | } |
228 | |
229 | if(!module.parent) |
230 | u.tests(module.exports) |
231 | |
232 | |
233 | |
234 |
Built with git-ssb-web