-
Notifications
You must be signed in to change notification settings - Fork 2
/
v2.test.js
242 lines (197 loc) · 7.59 KB
/
v2.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
const { describe } = require('tape-plus')
const { unbox } = require('ssb-keys')
const getContent = require('ssb-msg-content')
const Server = require('../../../../testbot')
const Share = require('../../../../../share/async/share')
const Request = require('../../../../../recover/async/request')
const Recombine = require('../../../../../recover/async/recombine')
// mix: These tests may be redundent as recombine is now "fetch" + "mend" which are each tested
// left in place because they still pass and don't (currently) cost anything
describe('recover.async.recombine (request v2 shards)', context => {
let server, recombine, request, alice, bob, carol
let custodians, name, secret, quorum, share, label
context.beforeEach(c => {
server = Server()
recombine = Recombine(server)
request = Request(server)
share = Share(server)
alice = server.createFeed()
bob = server.createFeed()
carol = server.createFeed()
name = 'My SSB Dark Crystal'
label = 'Give this key to your nearest and dearest'
secret = Math.random().toString(36)
custodians = [alice, bob, carol]
quorum = 2
})
context.afterEach(c => {
server.close()
})
context('Returns the recombined secret', (assert, next) => {
share({ name, secret, quorum, label, recps: custodians.map(id) }, (err, data) => {
if (err) console.error(err)
var rootId = data.root.key
request(rootId, (err, invites) => {
if (err) console.error(err)
var replies = invites.reduce((collection, invite) => {
return buildReplies(collection, invite, data, rootId)
}, {})
alice.publish(replies[alice.id], (err, aliceReply) => {
if (err) console.error(err)
bob.publish(replies[bob.id], (err, bobReply) => {
if (err) console.error(err)
recombine(rootId, (err, returnedSecret) => {
assert.notOk(err, 'error is null')
assert.equal(secret, returnedSecret.secret, 'returns the correct secret')
assert.equal(label, returnedSecret.label, 'returns the correct label')
next()
})
})
})
})
})
})
context('calls back with an error if quorum is not reached', (assert, next) => {
share({ name, secret, quorum, recps: custodians.map(id) }, (err, data) => {
if (err) console.error(err)
var rootId = data.root.key
request(rootId, (err, invites) => {
if (err) console.error(err)
var replies = invites.reduce((collection, invite) => {
return buildReplies(collection, invite, data, rootId)
}, {})
alice.publish(replies[alice.id], (err, aliceReply) => {
if (err) console.error(err)
recombine(rootId, (err, returnedSecret) => {
assert.ok(err, 'an error')
assert.notOk(returnedSecret, 'Does not return a secret')
next()
})
})
})
})
})
context('calls back with an error and returns no secret if an invalid shard is found', (assert, next) => {
share({ name, secret, quorum, recps: custodians.map(id) }, (err, data) => {
if (err) console.error(err)
var rootId = data.root.key
request(rootId, (err, invites) => {
if (err) console.error(err)
var replies = invites.reduce((collection, invite) => {
var content = getContent(invite)
var custodianId = content.recps.find(notMe)
collection[custodianId] = {
type: 'invite-reply',
root: rootId,
branch: invite.key,
accept: true,
version: '1',
body: 'This is not a valid shard',
recps: content.recps
}
return collection
}, {})
alice.publish(replies[alice.id], (err, aliceReply) => {
if (err) console.error(err)
bob.publish(replies[bob.id], (err, bobReply) => {
if (err) console.error(err)
recombine(rootId, (err, returnedSecret) => {
assert.ok(err, 'an error')
assert.notOk(returnedSecret, 'Does not return a secret')
next()
})
})
})
})
})
})
context('calls back with an error and returns no secret when reply refers to the wrong root message', (assert, next) => {
share({ name, secret, quorum, recps: custodians.map(id) }, (err, data) => {
if (err) console.error(err)
var rootId = data.root.key
share({ name, secret: 'another secret', quorum, recps: custodians.map(id) }, (err, otherData) => {
if (err) console.error(err)
var otherRootId = otherData.root.key
request(rootId, (err, invites) => {
if (err) console.error(err)
var replies = invites.reduce((collection, invite) => {
return buildReplies(collection, invite, data, otherRootId)
}, {})
alice.publish(replies[alice.keys.id], (err, aliceReply) => {
if (err) console.error(err)
bob.publish(replies[bob.keys.id], (err, bobReply) => {
if (err) console.error(err)
recombine(rootId, (err, returnedSecret) => {
assert.ok(err, 'an error')
assert.notOk(returnedSecret, 'Does not return a secret')
next()
})
})
})
})
})
})
})
context('calls back with an error and returns no secret if a reply message has wrong version', (assert, next) => {
share({ name, secret, quorum, recps: custodians.map(id) }, (err, data) => {
if (err) console.error(err)
var rootId = data.root.key
request(rootId, (err, invites) => {
if (err) console.error(err)
var replies = invites.reduce((collection, invite) => (
buildReplies(collection, invite, data, rootId)
), {})
replies[alice.id].shareVersion = '1111.0.0'
alice.publish(replies[alice.id], (err, aliceReply) => {
if (err) console.error(err)
bob.publish(replies[bob.id], (err, bobReply) => {
if (err) console.error(err)
recombine(rootId, (err, returnedSecret) => {
assert.ok(err, 'an error')
assert.notOk(returnedSecret, 'Does not return a secret')
next()
})
})
})
})
})
})
context('calls back with an error when given a rootId for which there is no root message', (assert, next) => {
const rootId = '%g1gbRKarJT4au9De2r4aJ+MghFSAyQzjfVnnxtJNBBw=.sha256'
recombine(rootId, (err, returnedSecret) => {
assert.ok(err, 'has error')
assert.notOk(returnedSecret, 'Does not return a secret')
next()
})
})
function id (feed) {
return feed.id
}
function notMe (recp) {
return recp !== server.id
}
function findCustodian (custodianId) {
return custodians.find(c => c.id === custodianId)
}
function buildReplies (collection, invite, data, rootId) {
var content = getContent(invite)
var custodianId = content.recps.find(notMe)
var shard = data.shards.map(getContent)
.find(shard => shard.recps.find(recp => recp === custodianId))
.shard
const unboxedShard = unbox(shard, findCustodian(custodianId).keys)
const contextMessage = JSON.stringify({ rootId, recp: custodianId })
const boxedShard = server.ephemeral.boxMessage(unboxedShard, content.ephPublicKey, contextMessage)
collection[custodianId] = {
type: 'invite-reply',
root: rootId,
branch: invite.key,
accept: true,
version: '1',
shareVersion: '2.0.0',
body: boxedShard,
recps: content.recps
}
return collection
}
})