forked from berty/weshnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_marshaler_test.go
210 lines (153 loc) · 4.99 KB
/
message_marshaler_test.go
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
package weshnet
import (
"testing"
"time"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"berty.tech/go-ipfs-log/enc"
"berty.tech/go-ipfs-log/entry"
"berty.tech/go-orbit-db/iface"
"berty.tech/weshnet/v2/pkg/rendezvous"
"berty.tech/weshnet/v2/pkg/secretstore"
)
var (
testSeed1 = []byte("secretsecretsecretsecretsecretse") // 32 bytes seed
testSeed2 = []byte("badbadbadbadbadbadbadbadbadbadba") // 32 bytes seed
)
func TestRotationMessageMarshaler(t *testing.T) {
key, err := enc.NewSecretbox(testSeed1)
require.NoError(t, err)
msg := &iface.MessageExchangeHeads{
Address: "address_1",
Heads: []*entry.Entry{},
}
mn := mocknet.New()
defer mn.Close()
p, err := mn.GenPeer()
require.NoError(t, err)
// generate keystore
acc1, err := secretstore.NewInMemSecretStore(nil)
require.NoError(t, err)
rp := rendezvous.NewStaticRotationInterval()
m := NewOrbitDBMessageMarshaler(p.ID(), acc1, rp, false)
g, _, err := NewGroupMultiMember()
require.NoError(t, err)
m.RegisterGroup(msg.Address, g)
rp.RegisterRotation(time.Now(), msg.Address, testSeed1)
m.RegisterSharedKeyForTopic(msg.Address, key)
// marshal with register topic, should succeed
payload, err := m.Marshal(msg)
require.NoError(t, err)
require.NotNil(t, payload)
ret := iface.MessageExchangeHeads{}
err = m.Unmarshal(payload, &ret)
require.NoError(t, err)
assert.Equal(t, msg.Address, ret.Address)
}
func TestRotationMessageMarshalUnknownTopic(t *testing.T) {
mn := mocknet.New()
defer mn.Close()
msg := &iface.MessageExchangeHeads{
Address: "address_1",
Heads: []*entry.Entry{},
}
p, err := mn.GenPeer()
require.NoError(t, err)
// generate keystore
acc, err := secretstore.NewInMemSecretStore(nil)
require.NoError(t, err)
rp := rendezvous.NewStaticRotationInterval()
m := NewOrbitDBMessageMarshaler(p.ID(), acc, rp, false)
// marshal without register topic, should fail
payload, err := m.Marshal(msg)
require.Error(t, err)
require.Nil(t, payload)
}
func TestRotationMessageUnmarshalUnknownTopic(t *testing.T) {
mn := mocknet.New()
defer mn.Close()
msg := &iface.MessageExchangeHeads{
Address: "address_1",
Heads: []*entry.Entry{},
}
key1, err := enc.NewSecretbox(testSeed1)
require.NoError(t, err)
key2, err := enc.NewSecretbox(testSeed2)
require.NoError(t, err)
p1, err := mn.GenPeer()
require.NoError(t, err)
p2, err := mn.GenPeer()
require.NoError(t, err)
// generate keystore
acc1, err := secretstore.NewInMemSecretStore(nil)
require.NoError(t, err)
acc2, err := secretstore.NewInMemSecretStore(nil)
require.NoError(t, err)
g1, _, err := NewGroupMultiMember()
require.NoError(t, err)
g2, _, err := NewGroupMultiMember()
require.NoError(t, err)
rp1 := rendezvous.NewStaticRotationInterval()
rp1.RegisterRotation(time.Now(), msg.Address, testSeed1)
rp2 := rendezvous.NewStaticRotationInterval()
m1 := NewOrbitDBMessageMarshaler(p1.ID(), acc1, rp1, false)
m1.RegisterSharedKeyForTopic(msg.Address, key1)
m1.RegisterGroup(msg.Address, g1)
payload, err := m1.Marshal(msg)
require.NoError(t, err)
m2 := NewOrbitDBMessageMarshaler(p2.ID(), acc2, rp2, false)
m2.RegisterSharedKeyForTopic(msg.Address, key2)
m2.RegisterGroup(msg.Address, g2)
var ret iface.MessageExchangeHeads
// marshal with wrong key should fail
err = m2.Unmarshal(payload, &ret)
require.Error(t, err)
assert.NotEqual(t, ret.Address, msg.Address)
}
func TestRotationMessageMarshalWrongKey(t *testing.T) {
mn := mocknet.New()
defer mn.Close()
msg := &iface.MessageExchangeHeads{
Address: "address_1",
Heads: []*entry.Entry{},
}
key1, err := enc.NewSecretbox(testSeed1)
require.NoError(t, err)
key2, err := enc.NewSecretbox(testSeed2)
require.NoError(t, err)
p1, err := mn.GenPeer()
require.NoError(t, err)
p2, err := mn.GenPeer()
require.NoError(t, err)
g1, _, err := NewGroupMultiMember()
require.NoError(t, err)
g2, _, err := NewGroupMultiMember()
require.NoError(t, err)
// generate keystore
acc1, err := secretstore.NewInMemSecretStore(nil)
require.NoError(t, err)
acc2, err := secretstore.NewInMemSecretStore(nil)
require.NoError(t, err)
rp1 := rendezvous.NewStaticRotationInterval()
rp1.RegisterRotation(time.Now(), msg.Address, testSeed1)
rp2 := rendezvous.NewStaticRotationInterval()
rp2.RegisterRotation(time.Now(), msg.Address, testSeed2)
m1 := NewOrbitDBMessageMarshaler(p1.ID(), acc1, rp1, false)
m1.RegisterSharedKeyForTopic(msg.Address, key1)
m1.RegisterGroup(msg.Address, g1)
payload, err := m1.Marshal(msg)
require.NoError(t, err)
m2 := NewOrbitDBMessageMarshaler(p2.ID(), acc2, rp2, false)
m2.RegisterSharedKeyForTopic(msg.Address, key2)
m2.RegisterGroup(msg.Address, g2)
var ret iface.MessageExchangeHeads
// marshal with wrong key should fail
err = m2.Unmarshal(payload, &ret)
require.Error(t, err)
assert.NotEqual(t, ret.Address, msg.Address)
// marshal with good key should succeed
err = m1.Unmarshal(payload, &ret)
require.NoError(t, err)
assert.Equal(t, ret.Address, msg.Address)
}