aboutsummaryrefslogtreecommitdiffstats
path: root/whisper/whisperv5/whisper_test.go
blob: 1db26265a5fff5da5fdddaf8f4b9aa8d2646ffc5 (plain) (blame)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package whisperv5

import (
    "bytes"
    "testing"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/crypto"
)

func TestWhisperBasic(x *testing.T) {
    w := NewWhisper(nil)
    p := w.Protocols()
    shh := p[0]
    if shh.Name != ProtocolName {
        x.Errorf("failed Protocol Name: %v.", shh.Name)
        return
    }
    if uint64(shh.Version) != ProtocolVersion {
        x.Errorf("failed Protocol Version: %v.", shh.Version)
        return
    }
    if shh.Length != NumberOfMessageCodes {
        x.Errorf("failed Protocol Length: %v.", shh.Length)
        return
    }
    if shh.Run == nil {
        x.Errorf("failed shh.Run.")
        return
    }
    if uint64(w.Version()) != ProtocolVersion {
        x.Errorf("failed whisper Version: %v.", shh.Version)
        return
    }
    if w.GetFilter(0) != nil {
        x.Errorf("failed GetFilter.")
        return
    }

    peerID := make([]byte, 64)
    randomize(peerID)
    peer, err := w.getPeer(peerID)
    if peer != nil {
        x.Errorf("failed GetPeer.")
        return
    }
    err = w.MarkPeerTrusted(peerID)
    if err == nil {
        x.Errorf("failed MarkPeerTrusted.")
        return
    }
    err = w.RequestHistoricMessages(peerID, peerID)
    if err == nil {
        x.Errorf("failed RequestHistoricMessages.")
        return
    }
    err = w.SendP2PMessage(peerID, nil)
    if err == nil {
        x.Errorf("failed SendP2PMessage.")
        return
    }
    exist := w.HasSymKey("non-existing")
    if exist {
        x.Errorf("failed HasSymKey.")
        return
    }
    key := w.GetSymKey("non-existing")
    if key != nil {
        x.Errorf("failed GetSymKey.")
        return
    }
    mail := w.Envelopes()
    if len(mail) != 0 {
        x.Errorf("failed w.Envelopes().")
        return
    }
    m := w.Messages(0)
    if len(m) != 0 {
        x.Errorf("failed w.Messages.")
        return
    }

    var derived []byte
    ver := uint64(0xDEADBEEF)
    derived, err = deriveKeyMaterial(peerID, ver)
    if err != unknownVersionError(ver) {
        x.Errorf("failed deriveKeyMaterial 1 with param = %v: %s.", peerID, err)
        return
    }
    derived, err = deriveKeyMaterial(peerID, 0)
    if err != nil {
        x.Errorf("failed deriveKeyMaterial 2 with param = %v: %s.", peerID, err)
        return
    }
    if !validateSymmetricKey(derived) {
        x.Errorf("failed validateSymmetricKey with param = %v.", derived)
        return
    }
    if containsOnlyZeros(derived) {
        x.Errorf("failed containsOnlyZeros with param = %v.", derived)
        return
    }

    buf := []byte{0xFF, 0xE5, 0x80, 0x2, 0}
    le := bytesToIntLittleEndian(buf)
    be := BytesToIntBigEndian(buf)
    if le != uint64(0x280e5ff) {
        x.Errorf("failed bytesToIntLittleEndian: %d.", le)
        return
    }
    if be != uint64(0xffe5800200) {
        x.Errorf("failed BytesToIntBigEndian: %d.", be)
        return
    }

    pk := w.NewIdentity()
    if !validatePrivateKey(pk) {
        x.Errorf("failed validatePrivateKey: %v.", pk)
        return
    }
    if !ValidatePublicKey(&pk.PublicKey) {
        x.Errorf("failed ValidatePublicKey: %v.", pk)
        return
    }
}

func TestWhisperIdentityManagement(x *testing.T) {
    w := NewWhisper(nil)
    id1 := w.NewIdentity()
    id2 := w.NewIdentity()
    pub1 := common.ToHex(crypto.FromECDSAPub(&id1.PublicKey))
    pub2 := common.ToHex(crypto.FromECDSAPub(&id2.PublicKey))
    pk1 := w.GetIdentity(pub1)
    pk2 := w.GetIdentity(pub2)
    if !w.HasIdentity(pub1) {
        x.Errorf("failed HasIdentity 1.")
        return
    }
    if !w.HasIdentity(pub2) {
        x.Errorf("failed HasIdentity 2.")
        return
    }
    if pk1 != id1 {
        x.Errorf("failed GetIdentity 3.")
        return
    }
    if pk2 != id2 {
        x.Errorf("failed GetIdentity 4.")
        return
    }

    // Delete one identity
    w.DeleteIdentity(pub1)
    pk1 = w.GetIdentity(pub1)
    pk2 = w.GetIdentity(pub2)
    if w.HasIdentity(pub1) {
        x.Errorf("failed HasIdentity 11.")
        return
    }
    if !w.HasIdentity(pub2) {
        x.Errorf("failed HasIdentity 12.")
        return
    }
    if pk1 != nil {
        x.Errorf("failed GetIdentity 13.")
        return
    }
    if pk2 != id2 {
        x.Errorf("failed GetIdentity 14.")
        return
    }

    // Delete again non-existing identity
    w.DeleteIdentity(pub1)
    pk1 = w.GetIdentity(pub1)
    pk2 = w.GetIdentity(pub2)
    if w.HasIdentity(pub1) {
        x.Errorf("failed HasIdentity 21.")
        return
    }
    if !w.HasIdentity(pub2) {
        x.Errorf("failed HasIdentity 22.")
        return
    }
    if pk1 != nil {
        x.Errorf("failed GetIdentity 23.")
        return
    }
    if pk2 != id2 {
        x.Errorf("failed GetIdentity 24.")
        return
    }

    // Delete second identity
    w.DeleteIdentity(pub2)
    pk1 = w.GetIdentity(pub1)
    pk2 = w.GetIdentity(pub2)
    if w.HasIdentity(pub1) {
        x.Errorf("failed HasIdentity 31.")
        return
    }
    if w.HasIdentity(pub2) {
        x.Errorf("failed HasIdentity 32.")
        return
    }
    if pk1 != nil {
        x.Errorf("failed GetIdentity 33.")
        return
    }
    if pk2 != nil {
        x.Errorf("failed GetIdentity 34.")
        return
    }
}

func TestWhisperSymKeyManagement(x *testing.T) {
    InitSingleTest()

    var k1, k2 []byte
    w := NewWhisper(nil)
    id1 := string("arbitrary-string-1")
    id2 := string("arbitrary-string-2")

    err := w.GenerateSymKey(id1)
    if err != nil {
        x.Errorf("failed test case 1 with seed %d: %s.", seed, err)
        return
    }

    k1 = w.GetSymKey(id1)
    k2 = w.GetSymKey(id2)
    if !w.HasSymKey(id1) {
        x.Errorf("failed HasIdentity 2.")
        return
    }
    if w.HasSymKey(id2) {
        x.Errorf("failed HasIdentity 3.")
        return
    }
    if k1 == nil {
        x.Errorf("failed GetIdentity 4.")
        return
    }
    if k2 != nil {
        x.Errorf("failed GetIdentity 5.")
        return
    }

    // add existing id, nothing should change
    randomKey := make([]byte, 16)
    randomize(randomKey)
    err = w.AddSymKey(id1, randomKey)
    if err == nil {
        x.Errorf("failed test case 10 with seed %d.", seed)
        return
    }

    k1 = w.GetSymKey(id1)
    k2 = w.GetSymKey(id2)
    if !w.HasSymKey(id1) {
        x.Errorf("failed HasIdentity 12.")
        return
    }
    if w.HasSymKey(id2) {
        x.Errorf("failed HasIdentity 13.")
        return
    }
    if k1 == nil {
        x.Errorf("failed GetIdentity 14.")
        return
    }
    if bytes.Compare(k1, randomKey) == 0 {
        x.Errorf("failed GetIdentity 15: k1 == randomKey.")
        return
    }
    if k2 != nil {
        x.Errorf("failed GetIdentity 16.")
        return
    }

    err = w.AddSymKey(id2, randomKey) // add non-existing (yet)
    if err != nil {
        x.Errorf("failed test case 21 with seed %d: %s.", seed, err)
        return
    }
    k1 = w.GetSymKey(id1)
    k2 = w.GetSymKey(id2)
    if !w.HasSymKey(id1) {
        x.Errorf("failed HasIdentity 22.")
        return
    }
    if !w.HasSymKey(id2) {
        x.Errorf("failed HasIdentity 23.")
        return
    }
    if k1 == nil {
        x.Errorf("failed GetIdentity 24.")
        return
    }
    if k2 == nil {
        x.Errorf("failed GetIdentity 25.")
        return
    }
    if bytes.Compare(k1, k2) == 0 {
        x.Errorf("failed GetIdentity 26.")
        return
    }
    if bytes.Compare(k1, randomKey) == 0 {
        x.Errorf("failed GetIdentity 27.")
        return
    }
    if len(k1) != aesKeyLength {
        x.Errorf("failed GetIdentity 28.")
        return
    }
    if len(k2) != aesKeyLength {
        x.Errorf("failed GetIdentity 29.")
        return
    }

    w.DeleteSymKey(id1)
    k1 = w.GetSymKey(id1)
    k2 = w.GetSymKey(id2)
    if w.HasSymKey(id1) {
        x.Errorf("failed HasIdentity 31.")
        return
    }
    if !w.HasSymKey(id2) {
        x.Errorf("failed HasIdentity 32.")
        return
    }
    if k1 != nil {
        x.Errorf("failed GetIdentity 33.")
        return
    }
    if k2 == nil {
        x.Errorf("failed GetIdentity 34.")
        return
    }

    w.DeleteSymKey(id1)
    w.DeleteSymKey(id2)
    k1 = w.GetSymKey(id1)
    k2 = w.GetSymKey(id2)
    if w.HasSymKey(id1) {
        x.Errorf("failed HasIdentity 41.")
        return
    }
    if w.HasSymKey(id2) {
        x.Errorf("failed HasIdentity 42.")
        return
    }
    if k1 != nil {
        x.Errorf("failed GetIdentity 43.")
        return
    }
    if k2 != nil {
        x.Errorf("failed GetIdentity 44.")
        return
    }
}