github.com/ylsgit/go-ethereum@v1.6.5/whisper/whisperv5/whisper_test.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package whisperv5 18 19 import ( 20 "bytes" 21 mrand "math/rand" 22 "testing" 23 "time" 24 ) 25 26 func TestWhisperBasic(t *testing.T) { 27 w := New() 28 p := w.Protocols() 29 shh := p[0] 30 if shh.Name != ProtocolName { 31 t.Fatalf("failed Protocol Name: %v.", shh.Name) 32 } 33 if uint64(shh.Version) != ProtocolVersion { 34 t.Fatalf("failed Protocol Version: %v.", shh.Version) 35 } 36 if shh.Length != NumberOfMessageCodes { 37 t.Fatalf("failed Protocol Length: %v.", shh.Length) 38 } 39 if shh.Run == nil { 40 t.Fatalf("failed shh.Run.") 41 } 42 if uint64(w.Version()) != ProtocolVersion { 43 t.Fatalf("failed whisper Version: %v.", shh.Version) 44 } 45 if w.GetFilter("non-existent") != nil { 46 t.Fatalf("failed GetFilter.") 47 } 48 49 peerID := make([]byte, 64) 50 mrand.Read(peerID) 51 peer, _ := w.getPeer(peerID) 52 if peer != nil { 53 t.Fatal("found peer for random key.") 54 } 55 if err := w.AllowP2PMessagesFromPeer(peerID); err == nil { 56 t.Fatalf("failed MarkPeerTrusted.") 57 } 58 exist := w.HasSymKey("non-existing") 59 if exist { 60 t.Fatalf("failed HasSymKey.") 61 } 62 key, err := w.GetSymKey("non-existing") 63 if err == nil { 64 t.Fatalf("failed GetSymKey(non-existing): false positive.") 65 } 66 if key != nil { 67 t.Fatalf("failed GetSymKey: false positive.") 68 } 69 mail := w.Envelopes() 70 if len(mail) != 0 { 71 t.Fatalf("failed w.Envelopes().") 72 } 73 m := w.Messages("non-existent") 74 if len(m) != 0 { 75 t.Fatalf("failed w.Messages.") 76 } 77 78 var derived []byte 79 ver := uint64(0xDEADBEEF) 80 if _, err := deriveKeyMaterial(peerID, ver); err != unknownVersionError(ver) { 81 t.Fatalf("failed deriveKeyMaterial with param = %v: %s.", peerID, err) 82 } 83 derived, err = deriveKeyMaterial(peerID, 0) 84 if err != nil { 85 t.Fatalf("failed second deriveKeyMaterial with param = %v: %s.", peerID, err) 86 } 87 if !validateSymmetricKey(derived) { 88 t.Fatalf("failed validateSymmetricKey with param = %v.", derived) 89 } 90 if containsOnlyZeros(derived) { 91 t.Fatalf("failed containsOnlyZeros with param = %v.", derived) 92 } 93 94 buf := []byte{0xFF, 0xE5, 0x80, 0x2, 0} 95 le := bytesToUintLittleEndian(buf) 96 be := BytesToUintBigEndian(buf) 97 if le != uint64(0x280e5ff) { 98 t.Fatalf("failed bytesToIntLittleEndian: %d.", le) 99 } 100 if be != uint64(0xffe5800200) { 101 t.Fatalf("failed BytesToIntBigEndian: %d.", be) 102 } 103 104 id, err := w.NewKeyPair() 105 if err != nil { 106 t.Fatalf("failed to generate new key pair: %s.", err) 107 } 108 pk, err := w.GetPrivateKey(id) 109 if err != nil { 110 t.Fatalf("failed to retrieve new key pair: %s.", err) 111 } 112 if !validatePrivateKey(pk) { 113 t.Fatalf("failed validatePrivateKey: %v.", pk) 114 } 115 if !ValidatePublicKey(&pk.PublicKey) { 116 t.Fatalf("failed ValidatePublicKey: %v.", pk) 117 } 118 } 119 120 func TestWhisperIdentityManagement(t *testing.T) { 121 w := New() 122 id1, err := w.NewKeyPair() 123 if err != nil { 124 t.Fatalf("failed to generate new key pair: %s.", err) 125 } 126 id2, err := w.NewKeyPair() 127 if err != nil { 128 t.Fatalf("failed to generate new key pair: %s.", err) 129 } 130 pk1, err := w.GetPrivateKey(id1) 131 if err != nil { 132 t.Fatalf("failed to retrieve the key pair: %s.", err) 133 } 134 pk2, err := w.GetPrivateKey(id2) 135 if err != nil { 136 t.Fatalf("failed to retrieve the key pair: %s.", err) 137 } 138 139 if !w.HasKeyPair(id1) { 140 t.Fatalf("failed HasIdentity(pk1).") 141 } 142 if !w.HasKeyPair(id2) { 143 t.Fatalf("failed HasIdentity(pk2).") 144 } 145 if pk1 == nil { 146 t.Fatalf("failed GetIdentity(pk1).") 147 } 148 if pk2 == nil { 149 t.Fatalf("failed GetIdentity(pk2).") 150 } 151 152 if !validatePrivateKey(pk1) { 153 t.Fatalf("pk1 is invalid.") 154 } 155 if !validatePrivateKey(pk2) { 156 t.Fatalf("pk2 is invalid.") 157 } 158 159 // Delete one identity 160 done := w.DeleteKeyPair(id1) 161 if !done { 162 t.Fatalf("failed to delete id1.") 163 } 164 pk1, err = w.GetPrivateKey(id1) 165 if err == nil { 166 t.Fatalf("retrieve the key pair: false positive.") 167 } 168 pk2, err = w.GetPrivateKey(id2) 169 if err != nil { 170 t.Fatalf("failed to retrieve the key pair: %s.", err) 171 } 172 if w.HasKeyPair(id1) { 173 t.Fatalf("failed DeleteIdentity(pub1): still exist.") 174 } 175 if !w.HasKeyPair(id2) { 176 t.Fatalf("failed DeleteIdentity(pub1): pub2 does not exist.") 177 } 178 if pk1 != nil { 179 t.Fatalf("failed DeleteIdentity(pub1): first key still exist.") 180 } 181 if pk2 == nil { 182 t.Fatalf("failed DeleteIdentity(pub1): second key does not exist.") 183 } 184 185 // Delete again non-existing identity 186 done = w.DeleteKeyPair(id1) 187 if done { 188 t.Fatalf("delete id1: false positive.") 189 } 190 pk1, err = w.GetPrivateKey(id1) 191 if err == nil { 192 t.Fatalf("retrieve the key pair: false positive.") 193 } 194 pk2, err = w.GetPrivateKey(id2) 195 if err != nil { 196 t.Fatalf("failed to retrieve the key pair: %s.", err) 197 } 198 if w.HasKeyPair(id1) { 199 t.Fatalf("failed delete non-existing identity: exist.") 200 } 201 if !w.HasKeyPair(id2) { 202 t.Fatalf("failed delete non-existing identity: pub2 does not exist.") 203 } 204 if pk1 != nil { 205 t.Fatalf("failed delete non-existing identity: first key exist.") 206 } 207 if pk2 == nil { 208 t.Fatalf("failed delete non-existing identity: second key does not exist.") 209 } 210 211 // Delete second identity 212 done = w.DeleteKeyPair(id2) 213 if !done { 214 t.Fatalf("failed to delete id2.") 215 } 216 pk1, err = w.GetPrivateKey(id1) 217 if err == nil { 218 t.Fatalf("retrieve the key pair: false positive.") 219 } 220 pk2, err = w.GetPrivateKey(id2) 221 if err == nil { 222 t.Fatalf("retrieve the key pair: false positive.") 223 } 224 if w.HasKeyPair(id1) { 225 t.Fatalf("failed delete second identity: first identity exist.") 226 } 227 if w.HasKeyPair(id2) { 228 t.Fatalf("failed delete second identity: still exist.") 229 } 230 if pk1 != nil { 231 t.Fatalf("failed delete second identity: first key exist.") 232 } 233 if pk2 != nil { 234 t.Fatalf("failed delete second identity: second key exist.") 235 } 236 } 237 238 func TestWhisperSymKeyManagement(t *testing.T) { 239 InitSingleTest() 240 241 var err error 242 var k1, k2 []byte 243 w := New() 244 id1 := string("arbitrary-string-1") 245 id2 := string("arbitrary-string-2") 246 247 id1, err = w.GenerateSymKey() 248 if err != nil { 249 t.Fatalf("failed GenerateSymKey with seed %d: %s.", seed, err) 250 } 251 252 k1, err = w.GetSymKey(id1) 253 if err != nil { 254 t.Fatalf("failed GetSymKey(id1).") 255 } 256 k2, err = w.GetSymKey(id2) 257 if err == nil { 258 t.Fatalf("failed GetSymKey(id2): false positive.") 259 } 260 if !w.HasSymKey(id1) { 261 t.Fatalf("failed HasSymKey(id1).") 262 } 263 if w.HasSymKey(id2) { 264 t.Fatalf("failed HasSymKey(id2): false positive.") 265 } 266 if k1 == nil { 267 t.Fatalf("first key does not exist.") 268 } 269 if k2 != nil { 270 t.Fatalf("second key still exist.") 271 } 272 273 // add existing id, nothing should change 274 randomKey := make([]byte, aesKeyLength) 275 mrand.Read(randomKey) 276 id1, err = w.AddSymKeyDirect(randomKey) 277 if err != nil { 278 t.Fatalf("failed AddSymKey with seed %d: %s.", seed, err) 279 } 280 281 k1, err = w.GetSymKey(id1) 282 if err != nil { 283 t.Fatalf("failed w.GetSymKey(id1).") 284 } 285 k2, err = w.GetSymKey(id2) 286 if err == nil { 287 t.Fatalf("failed w.GetSymKey(id2): false positive.") 288 } 289 if !w.HasSymKey(id1) { 290 t.Fatalf("failed w.HasSymKey(id1).") 291 } 292 if w.HasSymKey(id2) { 293 t.Fatalf("failed w.HasSymKey(id2): false positive.") 294 } 295 if k1 == nil { 296 t.Fatalf("first key does not exist.") 297 } 298 if !bytes.Equal(k1, randomKey) { 299 t.Fatalf("k1 != randomKey.") 300 } 301 if k2 != nil { 302 t.Fatalf("second key already exist.") 303 } 304 305 id2, err = w.AddSymKeyDirect(randomKey) 306 if err != nil { 307 t.Fatalf("failed AddSymKey(id2) with seed %d: %s.", seed, err) 308 } 309 k1, err = w.GetSymKey(id1) 310 if err != nil { 311 t.Fatalf("failed w.GetSymKey(id1).") 312 } 313 k2, err = w.GetSymKey(id2) 314 if err != nil { 315 t.Fatalf("failed w.GetSymKey(id2).") 316 } 317 if !w.HasSymKey(id1) { 318 t.Fatalf("HasSymKey(id1) failed.") 319 } 320 if !w.HasSymKey(id2) { 321 t.Fatalf("HasSymKey(id2) failed.") 322 } 323 if k1 == nil { 324 t.Fatalf("k1 does not exist.") 325 } 326 if k2 == nil { 327 t.Fatalf("k2 does not exist.") 328 } 329 if !bytes.Equal(k1, k2) { 330 t.Fatalf("k1 != k2.") 331 } 332 if !bytes.Equal(k1, randomKey) { 333 t.Fatalf("k1 != randomKey.") 334 } 335 if len(k1) != aesKeyLength { 336 t.Fatalf("wrong length of k1.") 337 } 338 if len(k2) != aesKeyLength { 339 t.Fatalf("wrong length of k2.") 340 } 341 342 w.DeleteSymKey(id1) 343 k1, err = w.GetSymKey(id1) 344 if err == nil { 345 t.Fatalf("failed w.GetSymKey(id1): false positive.") 346 } 347 if k1 != nil { 348 t.Fatalf("failed GetSymKey(id1): false positive.") 349 } 350 k2, err = w.GetSymKey(id2) 351 if err != nil { 352 t.Fatalf("failed w.GetSymKey(id2).") 353 } 354 if w.HasSymKey(id1) { 355 t.Fatalf("failed to delete first key: still exist.") 356 } 357 if !w.HasSymKey(id2) { 358 t.Fatalf("failed to delete first key: second key does not exist.") 359 } 360 if k1 != nil { 361 t.Fatalf("failed to delete first key.") 362 } 363 if k2 == nil { 364 t.Fatalf("failed to delete first key: second key is nil.") 365 } 366 367 w.DeleteSymKey(id1) 368 w.DeleteSymKey(id2) 369 k1, err = w.GetSymKey(id1) 370 if err == nil { 371 t.Fatalf("failed w.GetSymKey(id1): false positive.") 372 } 373 k2, err = w.GetSymKey(id2) 374 if err == nil { 375 t.Fatalf("failed w.GetSymKey(id2): false positive.") 376 } 377 if k1 != nil || k2 != nil { 378 t.Fatalf("k1 or k2 is not nil") 379 } 380 if w.HasSymKey(id1) { 381 t.Fatalf("failed to delete second key: first key exist.") 382 } 383 if w.HasSymKey(id2) { 384 t.Fatalf("failed to delete second key: still exist.") 385 } 386 if k1 != nil { 387 t.Fatalf("failed to delete second key: first key is not nil.") 388 } 389 if k2 != nil { 390 t.Fatalf("failed to delete second key: second key is not nil.") 391 } 392 393 randomKey = make([]byte, aesKeyLength+1) 394 mrand.Read(randomKey) 395 id1, err = w.AddSymKeyDirect(randomKey) 396 if err == nil { 397 t.Fatalf("added the key with wrong size, seed %d.", seed) 398 } 399 400 const password = "arbitrary data here" 401 id1, err = w.AddSymKeyFromPassword(password) 402 if err != nil { 403 t.Fatalf("failed AddSymKeyFromPassword(id1) with seed %d: %s.", seed, err) 404 } 405 id2, err = w.AddSymKeyFromPassword(password) 406 if err != nil { 407 t.Fatalf("failed AddSymKeyFromPassword(id2) with seed %d: %s.", seed, err) 408 } 409 k1, err = w.GetSymKey(id1) 410 if err != nil { 411 t.Fatalf("failed w.GetSymKey(id1).") 412 } 413 k2, err = w.GetSymKey(id2) 414 if err != nil { 415 t.Fatalf("failed w.GetSymKey(id2).") 416 } 417 if !w.HasSymKey(id1) { 418 t.Fatalf("HasSymKey(id1) failed.") 419 } 420 if !w.HasSymKey(id2) { 421 t.Fatalf("HasSymKey(id2) failed.") 422 } 423 if k1 == nil { 424 t.Fatalf("k1 does not exist.") 425 } 426 if k2 == nil { 427 t.Fatalf("k2 does not exist.") 428 } 429 if !bytes.Equal(k1, k2) { 430 t.Fatalf("k1 != k2.") 431 } 432 if len(k1) != aesKeyLength { 433 t.Fatalf("wrong length of k1.") 434 } 435 if len(k2) != aesKeyLength { 436 t.Fatalf("wrong length of k2.") 437 } 438 if !validateSymmetricKey(k2) { 439 t.Fatalf("key validation failed.") 440 } 441 } 442 443 func TestExpiry(t *testing.T) { 444 InitSingleTest() 445 446 w := New() 447 w.SetMinimumPoW(0.0000001) 448 defer w.SetMinimumPoW(DefaultMinimumPoW) 449 w.Start(nil) 450 defer w.Stop() 451 452 params, err := generateMessageParams() 453 if err != nil { 454 t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 455 } 456 457 params.TTL = 1 458 msg, err := NewSentMessage(params) 459 if err != nil { 460 t.Fatalf("failed to create new message with seed %d: %s.", seed, err) 461 } 462 env, err := msg.Wrap(params) 463 if err != nil { 464 t.Fatalf("failed Wrap with seed %d: %s.", seed, err) 465 } 466 467 err = w.Send(env) 468 if err != nil { 469 t.Fatalf("failed to send envelope with seed %d: %s.", seed, err) 470 } 471 472 // wait till received or timeout 473 var received, expired bool 474 for j := 0; j < 20; j++ { 475 time.Sleep(100 * time.Millisecond) 476 if len(w.Envelopes()) > 0 { 477 received = true 478 break 479 } 480 } 481 482 if !received { 483 t.Fatalf("did not receive the sent envelope, seed: %d.", seed) 484 } 485 486 // wait till expired or timeout 487 for j := 0; j < 20; j++ { 488 time.Sleep(100 * time.Millisecond) 489 if len(w.Envelopes()) == 0 { 490 expired = true 491 break 492 } 493 } 494 495 if !expired { 496 t.Fatalf("expire failed, seed: %d.", seed) 497 } 498 } 499 500 func TestCustomization(t *testing.T) { 501 InitSingleTest() 502 503 w := New() 504 defer w.SetMinimumPoW(DefaultMinimumPoW) 505 defer w.SetMaxMessageLength(DefaultMaxMessageLength) 506 w.Start(nil) 507 defer w.Stop() 508 509 const smallPoW = 0.00001 510 511 f, err := generateFilter(t, true) 512 params, err := generateMessageParams() 513 if err != nil { 514 t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 515 } 516 517 params.KeySym = f.KeySym 518 params.Topic = BytesToTopic(f.Topics[2]) 519 params.PoW = smallPoW 520 params.TTL = 3600 * 24 // one day 521 msg, err := NewSentMessage(params) 522 if err != nil { 523 t.Fatalf("failed to create new message with seed %d: %s.", seed, err) 524 } 525 env, err := msg.Wrap(params) 526 if err != nil { 527 t.Fatalf("failed Wrap with seed %d: %s.", seed, err) 528 } 529 530 err = w.Send(env) 531 if err == nil { 532 t.Fatalf("successfully sent envelope with PoW %.06f, false positive (seed %d).", env.PoW(), seed) 533 } 534 535 w.SetMinimumPoW(smallPoW / 2) 536 err = w.Send(env) 537 if err != nil { 538 t.Fatalf("failed to send envelope with seed %d: %s.", seed, err) 539 } 540 541 params.TTL++ 542 msg, err = NewSentMessage(params) 543 if err != nil { 544 t.Fatalf("failed to create new message with seed %d: %s.", seed, err) 545 } 546 env, err = msg.Wrap(params) 547 if err != nil { 548 t.Fatalf("failed Wrap with seed %d: %s.", seed, err) 549 } 550 w.SetMaxMessageLength(env.size() - 1) 551 err = w.Send(env) 552 if err == nil { 553 t.Fatalf("successfully sent oversized envelope (seed %d): false positive.", seed) 554 } 555 556 w.SetMaxMessageLength(DefaultMaxMessageLength) 557 err = w.Send(env) 558 if err != nil { 559 t.Fatalf("failed to send second envelope with seed %d: %s.", seed, err) 560 } 561 562 // wait till received or timeout 563 var received bool 564 for j := 0; j < 20; j++ { 565 time.Sleep(100 * time.Millisecond) 566 if len(w.Envelopes()) > 1 { 567 received = true 568 break 569 } 570 } 571 572 if !received { 573 t.Fatalf("did not receive the sent envelope, seed: %d.", seed) 574 } 575 576 // check w.messages() 577 id, err := w.Subscribe(f) 578 time.Sleep(5 * time.Millisecond) 579 mail := f.Retrieve() 580 if len(mail) > 0 { 581 t.Fatalf("received premature mail") 582 } 583 584 mail = w.Messages(id) 585 if len(mail) != 2 { 586 t.Fatalf("failed to get whisper messages") 587 } 588 }