Files: e106db19b5757a1b96390c4ef9bc5603e3075aa1 / test / invalid.js
2605 bytesRaw
1 | var tape = require('tape') |
2 | var ssbKeys = require('ssb-keys') |
3 | var v = require('ssb-validate') |
4 | var i = require('../') |
5 | var u = require('../util') |
6 | |
7 | var invite_key = require('../cap') |
8 | |
9 | var hash = u.hash |
10 | |
11 | var alice = ssbKeys.generate(null, hash('ALICE')) |
12 | var bob = ssbKeys.generate(null, hash('BOB')) |
13 | |
14 | |
15 | function throws(t, test, code) { |
16 | if(!code) throw new Error('error code must be provided') |
17 | try { |
18 | test() |
19 | t.fail('expected:'+test+' to throw code:'+code) |
20 | } catch(err) { |
21 | console.error(err.stack) |
22 | t.ok(err.code, 'errors must have an error code') |
23 | t.equal(err.code, code) |
24 | } |
25 | } |
26 | |
27 | //any bit in invite{invite,host,reveal} is flipped |
28 | tape('invalid - wrong invitee', function (t) { |
29 | |
30 | //construct a message where host does not match |
31 | var seed = hash('seed2') |
32 | var keys = ssbKeys.generate(null, seed) |
33 | var invalid = ssbKeys.signObj(keys, invite_key, { |
34 | type: 'invite', |
35 | invite: ssbKeys.generate(null, hash('seed3')), |
36 | host: alice.id |
37 | }) |
38 | |
39 | var msg = v.create(null, alice, null, invalid, new Date('2018-03-26T06:14:18.377Z')) |
40 | |
41 | throws(t, function () { |
42 | i.verifyInvitePublic(msg) |
43 | }, 'user-invites:invite-signature-failed') |
44 | |
45 | throws(t, function () { |
46 | i.verifyInvitePrivate(msg) |
47 | }, 'user-invites:invite-signature-failed') |
48 | |
49 | t.end() |
50 | }) |
51 | |
52 | //any bit in invite{invite,host,reveal} is flipped |
53 | tape('invalid - wrong invitee', function (t) { |
54 | |
55 | //construct a message where host does not match |
56 | var seed = hash('seed2') |
57 | var keys = ssbKeys.generate(null, seed) |
58 | var invalid = ssbKeys.signObj(keys, invite_key, { |
59 | type: 'invite', |
60 | invite: keys.id, //correct key |
61 | reveal: u.box('cannot be decrypted due to wrong key', u.hash('wrong key')), |
62 | host: alice.id |
63 | }) |
64 | var invite_msg = v.create(null, alice, null, invalid, new Date('2018-03-26T06:14:18.377Z')) |
65 | |
66 | t.ok(i.verifyInvitePublic(invite_msg)) |
67 | |
68 | throws(t, function () { |
69 | i.verifyInvitePrivate(invite_msg, seed) |
70 | }, 'user-invites:decrypt-reveal-failed') |
71 | |
72 | //say if the invitee creates a accept message anyway. |
73 | |
74 | |
75 | |
76 | throws(t, function () { |
77 | i.createAccept(invite_msg, seed, bob.id) |
78 | }, 'user-invites:decrypt-reveal-failed') |
79 | |
80 | var accept = ssbKeys.signObj(ssbKeys.generate(null, seed), invite_key, { |
81 | type: 'invite/accept', |
82 | receipt: '%'+ssbKeys.hash(JSON.stringify(invite_msg, null, 2)), |
83 | id: bob.id, |
84 | key: u.hash(u.hash(seed)) //what the reveal key should be. |
85 | }) |
86 | |
87 | var accept_msg = |
88 | v.create(null, bob, null, accept, new Date('2018-03-26T06:14:18.377Z')) |
89 | |
90 | throws(t, function () { |
91 | i.verifyAccept(accept_msg, invite_msg) |
92 | }, 'user-invites:decrypt-accept-reveal-failed') |
93 | |
94 | t.end() |
95 | }) |
96 |
Built with git-ssb-web