git ssb

0+

Dominic / ssb-peer-invites



Commit e106db19b5757a1b96390c4ef9bc5603e3075aa1

split up tests so that happy tests are easy, and invalid tests can be througher

Dominic Tarr committed on 3/26/2018, 3:39:23 AM
Parent: ec3e8d2e2ef215491071a80801e6a0e2887abf2d

Files changed

test/invite.jsdeleted
test/happy.jsadded
test/invalid.jsadded
test/invite.jsView
@@ -1,45 +1,0 @@
1-
2-var tape = require('tape')
3-var ssbKeys = require('ssb-keys')
4-var v = require('ssb-validate')
5-var i = require('../')
6-
7-var crypto = require('crypto')
8-
9-function hash (s) {
10- return crypto.createHash('sha256').update(s).digest()
11-}
12-
13-var alice = ssbKeys.generate(null, hash('ALICE'))
14-var bob = ssbKeys.generate(null, hash('BOB'))
15-
16-tape('happy', function (t) {
17-
18- var seed = hash('seed')
19-
20- var invite_content = i.createInvite(seed, bob.id, {name: 'bob'}, {text: 'welcome to ssb!'})
21-
22- console.log(invite_content)
23-
24- var msg = v.create(null, alice, null, invite_content, new Date('2018-03-14T06:14:18.377Z'))
25-
26- var message = i.verifyInvitePrivate(msg, seed)
27-
28- t.deepEqual({
29- reveal: {name: 'bob'},
30- private: {text: 'welcome to ssb!'}
31- }, message)
32-
33- var accept_content = i.createAccept(msg, seed, bob.id)
34-
35- console.log('accept:', accept_content)
36-
37- var msg2 = v.create(null, bob, null, accept_content, new Date('2018-03-14T06:32:18.377Z'))
38-
39- var revealed = i.verifyAccept(msg2, msg)
40-
41- t.deepEqual(revealed, {name: 'bob'})
42-
43- t.end()
44-})
45-
test/happy.jsView
@@ -1,0 +1,142 @@
1 +
2 +var tape = require('tape')
3 +var ssbKeys = require('ssb-keys')
4 +var v = require('ssb-validate')
5 +var i = require('../')
6 +var u = require('../util')
7 +
8 +var invite_key = require('../cap')
9 +
10 +var hash = u.hash
11 +
12 +var alice = ssbKeys.generate(null, hash('ALICE'))
13 +var bob = ssbKeys.generate(null, hash('BOB'))
14 +
15 +tape('happy', function (t) {
16 +
17 + var seed = hash('seed1')
18 +
19 + var tmp = ssbKeys.generate(null, seed)
20 +
21 + var invite_content = i.createInvite(seed, alice.id, {name: 'bob'}, {text: 'welcome to ssb!'})
22 +
23 + var msg = v.create(null, alice, null, invite_content, new Date('2018-03-14T06:14:18.377Z'))
24 +
25 + var message = i.verifyInvitePrivate(msg, seed)
26 +
27 + t.deepEqual({
28 + reveal: {name: 'bob'},
29 + private: {text: 'welcome to ssb!'}
30 + }, message)
31 +
32 + var accept_content = i.createAccept(msg, seed, bob.id)
33 +
34 + var msg2 = v.create(null, bob, null, accept_content, new Date('2018-03-14T06:32:18.377Z'))
35 +
36 + var revealed = i.verifyAccept(msg2, msg)
37 +
38 + t.deepEqual(revealed, {name: 'bob'})
39 +
40 + t.end()
41 +})
42 +
43 +tape('happy 2, without private', function (t) {
44 +
45 + var seed = hash('seed2')
46 +
47 + var tmp = ssbKeys.generate(null, seed)
48 +
49 + var invite_content = i.createInvite(seed, alice.id, {name: 'bob'}, null)
50 +
51 + var msg = v.create(null, alice, null, invite_content, new Date('2018-03-14T06:14:18.377Z'))
52 +
53 + var message = i.verifyInvitePrivate(msg, seed)
54 +
55 + t.deepEqual({
56 + reveal: {name: 'bob'},
57 + private: undefined
58 + }, message)
59 +
60 + var accept_content = i.createAccept(msg, seed, bob.id)
61 +
62 + var msg2 = v.create(null, bob, null, accept_content, new Date('2018-03-14T06:32:18.377Z'))
63 +
64 + var revealed = i.verifyAccept(msg2, msg)
65 +
66 + t.deepEqual(revealed, {name: 'bob'})
67 +
68 + t.end()
69 +})
70 +
71 +
72 +
73 +tape('happy 3, without reveal', function (t) {
74 +
75 + var seed = hash('seed3')
76 +
77 + var tmp = ssbKeys.generate(null, seed)
78 +
79 + var invite_content = i.createInvite(seed, alice.id, null, {name: 'bob'})
80 +
81 + var msg = v.create(null, alice, null, invite_content, new Date('2018-03-14T06:14:18.377Z'))
82 +
83 + var message = i.verifyInvitePrivate(msg, seed)
84 +
85 + t.deepEqual({
86 + reveal: undefined,
87 + private: {name: 'bob'}
88 + }, message)
89 +
90 + var accept_content = i.createAccept(msg, seed, bob.id)
91 +
92 + var msg2 = v.create(null, bob, null, accept_content, new Date('2018-03-14T06:32:18.377Z'))
93 +
94 + var revealed = i.verifyAccept(msg2, msg)
95 +
96 + t.equal(revealed, true)
97 +
98 + t.end()
99 +})
100 +
101 +
102 +tape('happy 4, neither private or reveal', function (t) {
103 +
104 + var seed = hash('seed4')
105 +
106 + var tmp = ssbKeys.generate(null, seed)
107 +
108 + var invite_content = i.createInvite(seed, alice.id)
109 +
110 + var msg = v.create(null, alice, null, invite_content, new Date('2018-03-14T06:14:18.377Z'))
111 +
112 + var message = i.verifyInvitePrivate(msg, seed)
113 +
114 + t.deepEqual({
115 + reveal: undefined,
116 + private: undefined
117 + }, message)
118 +
119 + var accept_content = i.createAccept(msg, seed, bob.id)
120 +
121 + var msg2 = v.create(null, bob, null, accept_content, new Date('2018-03-14T06:32:18.377Z'))
122 +
123 + var revealed = i.verifyAccept(msg2, msg)
124 +
125 + t.equal(revealed, true)
126 +
127 + t.end()
128 +})
129 +
130 +tape('safety', function (t) {
131 + t.throws(function () {
132 + //do now give away your own private key!
133 + i.createInvite(hash("ALICE"), alice.id, {name: 'bob'}, {text: 'welcome to ssb!'})
134 + })
135 + t.end()
136 +})
137 +
138 +
139 +
140 +
141 +
142 +
test/invalid.jsView
@@ -1,0 +1,95 @@
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 +})

Built with git-ssb-web