github.com/luckypickle/go-ethereum-vet@v1.14.2/whisper/whisperv6/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 whisperv6 18 19 import ( 20 "bytes" 21 "crypto/ecdsa" 22 "crypto/sha256" 23 mrand "math/rand" 24 "testing" 25 "time" 26 27 "github.com/luckypickle/go-ethereum-vet/common" 28 "golang.org/x/crypto/pbkdf2" 29 ) 30 31 func TestWhisperBasic(t *testing.T) { 32 w := New(&DefaultConfig) 33 p := w.Protocols() 34 shh := p[0] 35 if shh.Name != ProtocolName { 36 t.Fatalf("failed Protocol Name: %v.", shh.Name) 37 } 38 if uint64(shh.Version) != ProtocolVersion { 39 t.Fatalf("failed Protocol Version: %v.", shh.Version) 40 } 41 if shh.Length != NumberOfMessageCodes { 42 t.Fatalf("failed Protocol Length: %v.", shh.Length) 43 } 44 if shh.Run == nil { 45 t.Fatalf("failed shh.Run.") 46 } 47 if uint64(w.Version()) != ProtocolVersion { 48 t.Fatalf("failed whisper Version: %v.", shh.Version) 49 } 50 if w.GetFilter("non-existent") != nil { 51 t.Fatalf("failed GetFilter.") 52 } 53 54 peerID := make([]byte, 64) 55 mrand.Read(peerID) 56 peer, _ := w.getPeer(peerID) 57 if peer != nil { 58 t.Fatal("found peer for random key.") 59 } 60 if err := w.AllowP2PMessagesFromPeer(peerID); err == nil { 61 t.Fatalf("failed MarkPeerTrusted.") 62 } 63 exist := w.HasSymKey("non-existing") 64 if exist { 65 t.Fatalf("failed HasSymKey.") 66 } 67 key, err := w.GetSymKey("non-existing") 68 if err == nil { 69 t.Fatalf("failed GetSymKey(non-existing): false positive.") 70 } 71 if key != nil { 72 t.Fatalf("failed GetSymKey: false positive.") 73 } 74 mail := w.Envelopes() 75 if len(mail) != 0 { 76 t.Fatalf("failed w.Envelopes().") 77 } 78 79 derived := pbkdf2.Key([]byte(peerID), nil, 65356, aesKeyLength, sha256.New) 80 if !validateDataIntegrity(derived, aesKeyLength) { 81 t.Fatalf("failed validateSymmetricKey with param = %v.", derived) 82 } 83 if containsOnlyZeros(derived) { 84 t.Fatalf("failed containsOnlyZeros with param = %v.", derived) 85 } 86 87 buf := []byte{0xFF, 0xE5, 0x80, 0x2, 0} 88 le := bytesToUintLittleEndian(buf) 89 be := BytesToUintBigEndian(buf) 90 if le != uint64(0x280e5ff) { 91 t.Fatalf("failed bytesToIntLittleEndian: %d.", le) 92 } 93 if be != uint64(0xffe5800200) { 94 t.Fatalf("failed BytesToIntBigEndian: %d.", be) 95 } 96 97 id, err := w.NewKeyPair() 98 if err != nil { 99 t.Fatalf("failed to generate new key pair: %s.", err) 100 } 101 pk, err := w.GetPrivateKey(id) 102 if err != nil { 103 t.Fatalf("failed to retrieve new key pair: %s.", err) 104 } 105 if !validatePrivateKey(pk) { 106 t.Fatalf("failed validatePrivateKey: %v.", pk) 107 } 108 if !ValidatePublicKey(&pk.PublicKey) { 109 t.Fatalf("failed ValidatePublicKey: %v.", pk) 110 } 111 } 112 113 func TestWhisperAsymmetricKeyImport(t *testing.T) { 114 var ( 115 w = New(&DefaultConfig) 116 privateKeys []*ecdsa.PrivateKey 117 ) 118 119 for i := 0; i < 50; i++ { 120 id, err := w.NewKeyPair() 121 if err != nil { 122 t.Fatalf("could not generate key: %v", err) 123 } 124 125 pk, err := w.GetPrivateKey(id) 126 if err != nil { 127 t.Fatalf("could not export private key: %v", err) 128 } 129 130 privateKeys = append(privateKeys, pk) 131 132 if !w.DeleteKeyPair(id) { 133 t.Fatalf("could not delete private key") 134 } 135 } 136 137 for _, pk := range privateKeys { 138 if _, err := w.AddKeyPair(pk); err != nil { 139 t.Fatalf("could not import private key: %v", err) 140 } 141 } 142 } 143 144 func TestWhisperIdentityManagement(t *testing.T) { 145 w := New(&DefaultConfig) 146 id1, err := w.NewKeyPair() 147 if err != nil { 148 t.Fatalf("failed to generate new key pair: %s.", err) 149 } 150 id2, err := w.NewKeyPair() 151 if err != nil { 152 t.Fatalf("failed to generate new key pair: %s.", err) 153 } 154 pk1, err := w.GetPrivateKey(id1) 155 if err != nil { 156 t.Fatalf("failed to retrieve the key pair: %s.", err) 157 } 158 pk2, err := w.GetPrivateKey(id2) 159 if err != nil { 160 t.Fatalf("failed to retrieve the key pair: %s.", err) 161 } 162 163 if !w.HasKeyPair(id1) { 164 t.Fatalf("failed HasIdentity(pk1).") 165 } 166 if !w.HasKeyPair(id2) { 167 t.Fatalf("failed HasIdentity(pk2).") 168 } 169 if pk1 == nil { 170 t.Fatalf("failed GetIdentity(pk1).") 171 } 172 if pk2 == nil { 173 t.Fatalf("failed GetIdentity(pk2).") 174 } 175 176 if !validatePrivateKey(pk1) { 177 t.Fatalf("pk1 is invalid.") 178 } 179 if !validatePrivateKey(pk2) { 180 t.Fatalf("pk2 is invalid.") 181 } 182 183 // Delete one identity 184 done := w.DeleteKeyPair(id1) 185 if !done { 186 t.Fatalf("failed to delete id1.") 187 } 188 pk1, err = w.GetPrivateKey(id1) 189 if err == nil { 190 t.Fatalf("retrieve the key pair: false positive.") 191 } 192 pk2, err = w.GetPrivateKey(id2) 193 if err != nil { 194 t.Fatalf("failed to retrieve the key pair: %s.", err) 195 } 196 if w.HasKeyPair(id1) { 197 t.Fatalf("failed DeleteIdentity(pub1): still exist.") 198 } 199 if !w.HasKeyPair(id2) { 200 t.Fatalf("failed DeleteIdentity(pub1): pub2 does not exist.") 201 } 202 if pk1 != nil { 203 t.Fatalf("failed DeleteIdentity(pub1): first key still exist.") 204 } 205 if pk2 == nil { 206 t.Fatalf("failed DeleteIdentity(pub1): second key does not exist.") 207 } 208 209 // Delete again non-existing identity 210 done = w.DeleteKeyPair(id1) 211 if done { 212 t.Fatalf("delete id1: false positive.") 213 } 214 pk1, err = w.GetPrivateKey(id1) 215 if err == nil { 216 t.Fatalf("retrieve the key pair: false positive.") 217 } 218 pk2, err = w.GetPrivateKey(id2) 219 if err != nil { 220 t.Fatalf("failed to retrieve the key pair: %s.", err) 221 } 222 if w.HasKeyPair(id1) { 223 t.Fatalf("failed delete non-existing identity: exist.") 224 } 225 if !w.HasKeyPair(id2) { 226 t.Fatalf("failed delete non-existing identity: pub2 does not exist.") 227 } 228 if pk1 != nil { 229 t.Fatalf("failed delete non-existing identity: first key exist.") 230 } 231 if pk2 == nil { 232 t.Fatalf("failed delete non-existing identity: second key does not exist.") 233 } 234 235 // Delete second identity 236 done = w.DeleteKeyPair(id2) 237 if !done { 238 t.Fatalf("failed to delete id2.") 239 } 240 pk1, err = w.GetPrivateKey(id1) 241 if err == nil { 242 t.Fatalf("retrieve the key pair: false positive.") 243 } 244 pk2, err = w.GetPrivateKey(id2) 245 if err == nil { 246 t.Fatalf("retrieve the key pair: false positive.") 247 } 248 if w.HasKeyPair(id1) { 249 t.Fatalf("failed delete second identity: first identity exist.") 250 } 251 if w.HasKeyPair(id2) { 252 t.Fatalf("failed delete second identity: still exist.") 253 } 254 if pk1 != nil { 255 t.Fatalf("failed delete second identity: first key exist.") 256 } 257 if pk2 != nil { 258 t.Fatalf("failed delete second identity: second key exist.") 259 } 260 } 261 262 func TestWhisperSymKeyManagement(t *testing.T) { 263 InitSingleTest() 264 265 var err error 266 var k1, k2 []byte 267 w := New(&DefaultConfig) 268 id1 := string("arbitrary-string-1") 269 id2 := string("arbitrary-string-2") 270 271 id1, err = w.GenerateSymKey() 272 if err != nil { 273 t.Fatalf("failed GenerateSymKey with seed %d: %s.", seed, err) 274 } 275 276 k1, err = w.GetSymKey(id1) 277 if err != nil { 278 t.Fatalf("failed GetSymKey(id1).") 279 } 280 k2, err = w.GetSymKey(id2) 281 if err == nil { 282 t.Fatalf("failed GetSymKey(id2): false positive.") 283 } 284 if !w.HasSymKey(id1) { 285 t.Fatalf("failed HasSymKey(id1).") 286 } 287 if w.HasSymKey(id2) { 288 t.Fatalf("failed HasSymKey(id2): false positive.") 289 } 290 if k1 == nil { 291 t.Fatalf("first key does not exist.") 292 } 293 if k2 != nil { 294 t.Fatalf("second key still exist.") 295 } 296 297 // add existing id, nothing should change 298 randomKey := make([]byte, aesKeyLength) 299 mrand.Read(randomKey) 300 id1, err = w.AddSymKeyDirect(randomKey) 301 if err != nil { 302 t.Fatalf("failed AddSymKey with seed %d: %s.", seed, err) 303 } 304 305 k1, err = w.GetSymKey(id1) 306 if err != nil { 307 t.Fatalf("failed w.GetSymKey(id1).") 308 } 309 k2, err = w.GetSymKey(id2) 310 if err == nil { 311 t.Fatalf("failed w.GetSymKey(id2): false positive.") 312 } 313 if !w.HasSymKey(id1) { 314 t.Fatalf("failed w.HasSymKey(id1).") 315 } 316 if w.HasSymKey(id2) { 317 t.Fatalf("failed w.HasSymKey(id2): false positive.") 318 } 319 if k1 == nil { 320 t.Fatalf("first key does not exist.") 321 } 322 if !bytes.Equal(k1, randomKey) { 323 t.Fatalf("k1 != randomKey.") 324 } 325 if k2 != nil { 326 t.Fatalf("second key already exist.") 327 } 328 329 id2, err = w.AddSymKeyDirect(randomKey) 330 if err != nil { 331 t.Fatalf("failed AddSymKey(id2) with seed %d: %s.", seed, err) 332 } 333 k1, err = w.GetSymKey(id1) 334 if err != nil { 335 t.Fatalf("failed w.GetSymKey(id1).") 336 } 337 k2, err = w.GetSymKey(id2) 338 if err != nil { 339 t.Fatalf("failed w.GetSymKey(id2).") 340 } 341 if !w.HasSymKey(id1) { 342 t.Fatalf("HasSymKey(id1) failed.") 343 } 344 if !w.HasSymKey(id2) { 345 t.Fatalf("HasSymKey(id2) failed.") 346 } 347 if k1 == nil { 348 t.Fatalf("k1 does not exist.") 349 } 350 if k2 == nil { 351 t.Fatalf("k2 does not exist.") 352 } 353 if !bytes.Equal(k1, k2) { 354 t.Fatalf("k1 != k2.") 355 } 356 if !bytes.Equal(k1, randomKey) { 357 t.Fatalf("k1 != randomKey.") 358 } 359 if len(k1) != aesKeyLength { 360 t.Fatalf("wrong length of k1.") 361 } 362 if len(k2) != aesKeyLength { 363 t.Fatalf("wrong length of k2.") 364 } 365 366 w.DeleteSymKey(id1) 367 k1, err = w.GetSymKey(id1) 368 if err == nil { 369 t.Fatalf("failed w.GetSymKey(id1): false positive.") 370 } 371 if k1 != nil { 372 t.Fatalf("failed GetSymKey(id1): false positive.") 373 } 374 k2, err = w.GetSymKey(id2) 375 if err != nil { 376 t.Fatalf("failed w.GetSymKey(id2).") 377 } 378 if w.HasSymKey(id1) { 379 t.Fatalf("failed to delete first key: still exist.") 380 } 381 if !w.HasSymKey(id2) { 382 t.Fatalf("failed to delete first key: second key does not exist.") 383 } 384 if k1 != nil { 385 t.Fatalf("failed to delete first key.") 386 } 387 if k2 == nil { 388 t.Fatalf("failed to delete first key: second key is nil.") 389 } 390 391 w.DeleteSymKey(id1) 392 w.DeleteSymKey(id2) 393 k1, err = w.GetSymKey(id1) 394 if err == nil { 395 t.Fatalf("failed w.GetSymKey(id1): false positive.") 396 } 397 k2, err = w.GetSymKey(id2) 398 if err == nil { 399 t.Fatalf("failed w.GetSymKey(id2): false positive.") 400 } 401 if k1 != nil || k2 != nil { 402 t.Fatalf("k1 or k2 is not nil") 403 } 404 if w.HasSymKey(id1) { 405 t.Fatalf("failed to delete second key: first key exist.") 406 } 407 if w.HasSymKey(id2) { 408 t.Fatalf("failed to delete second key: still exist.") 409 } 410 if k1 != nil { 411 t.Fatalf("failed to delete second key: first key is not nil.") 412 } 413 if k2 != nil { 414 t.Fatalf("failed to delete second key: second key is not nil.") 415 } 416 417 randomKey = make([]byte, aesKeyLength+1) 418 mrand.Read(randomKey) 419 _, err = w.AddSymKeyDirect(randomKey) 420 if err == nil { 421 t.Fatalf("added the key with wrong size, seed %d.", seed) 422 } 423 424 const password = "arbitrary data here" 425 id1, err = w.AddSymKeyFromPassword(password) 426 if err != nil { 427 t.Fatalf("failed AddSymKeyFromPassword(id1) with seed %d: %s.", seed, err) 428 } 429 id2, err = w.AddSymKeyFromPassword(password) 430 if err != nil { 431 t.Fatalf("failed AddSymKeyFromPassword(id2) with seed %d: %s.", seed, err) 432 } 433 k1, err = w.GetSymKey(id1) 434 if err != nil { 435 t.Fatalf("failed w.GetSymKey(id1).") 436 } 437 k2, err = w.GetSymKey(id2) 438 if err != nil { 439 t.Fatalf("failed w.GetSymKey(id2).") 440 } 441 if !w.HasSymKey(id1) { 442 t.Fatalf("HasSymKey(id1) failed.") 443 } 444 if !w.HasSymKey(id2) { 445 t.Fatalf("HasSymKey(id2) failed.") 446 } 447 if !validateDataIntegrity(k2, aesKeyLength) { 448 t.Fatalf("key validation failed.") 449 } 450 if !bytes.Equal(k1, k2) { 451 t.Fatalf("k1 != k2.") 452 } 453 } 454 455 func TestExpiry(t *testing.T) { 456 InitSingleTest() 457 458 w := New(&DefaultConfig) 459 w.SetMinimumPowTest(0.0000001) 460 defer w.SetMinimumPowTest(DefaultMinimumPoW) 461 w.Start(nil) 462 defer w.Stop() 463 464 params, err := generateMessageParams() 465 if err != nil { 466 t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 467 } 468 469 params.TTL = 1 470 msg, err := NewSentMessage(params) 471 if err != nil { 472 t.Fatalf("failed to create new message with seed %d: %s.", seed, err) 473 } 474 env, err := msg.Wrap(params) 475 if err != nil { 476 t.Fatalf("failed Wrap with seed %d: %s.", seed, err) 477 } 478 479 err = w.Send(env) 480 if err != nil { 481 t.Fatalf("failed to send envelope with seed %d: %s.", seed, err) 482 } 483 484 // wait till received or timeout 485 var received, expired bool 486 for j := 0; j < 20; j++ { 487 time.Sleep(100 * time.Millisecond) 488 if len(w.Envelopes()) > 0 { 489 received = true 490 break 491 } 492 } 493 494 if !received { 495 t.Fatalf("did not receive the sent envelope, seed: %d.", seed) 496 } 497 498 // wait till expired or timeout 499 for j := 0; j < 20; j++ { 500 time.Sleep(100 * time.Millisecond) 501 if len(w.Envelopes()) == 0 { 502 expired = true 503 break 504 } 505 } 506 507 if !expired { 508 t.Fatalf("expire failed, seed: %d.", seed) 509 } 510 } 511 512 func TestCustomization(t *testing.T) { 513 InitSingleTest() 514 515 w := New(&DefaultConfig) 516 defer w.SetMinimumPowTest(DefaultMinimumPoW) 517 defer w.SetMaxMessageSize(DefaultMaxMessageSize) 518 w.Start(nil) 519 defer w.Stop() 520 521 const smallPoW = 0.00001 522 523 f, err := generateFilter(t, true) 524 if err != nil { 525 t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 526 } 527 params, err := generateMessageParams() 528 if err != nil { 529 t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 530 } 531 532 params.KeySym = f.KeySym 533 params.Topic = BytesToTopic(f.Topics[2]) 534 params.PoW = smallPoW 535 params.TTL = 3600 * 24 // one day 536 msg, err := NewSentMessage(params) 537 if err != nil { 538 t.Fatalf("failed to create new message with seed %d: %s.", seed, err) 539 } 540 env, err := msg.Wrap(params) 541 if err != nil { 542 t.Fatalf("failed Wrap with seed %d: %s.", seed, err) 543 } 544 545 err = w.Send(env) 546 if err == nil { 547 t.Fatalf("successfully sent envelope with PoW %.06f, false positive (seed %d).", env.PoW(), seed) 548 } 549 550 w.SetMinimumPowTest(smallPoW / 2) 551 err = w.Send(env) 552 if err != nil { 553 t.Fatalf("failed to send envelope with seed %d: %s.", seed, err) 554 } 555 556 params.TTL++ 557 msg, err = NewSentMessage(params) 558 if err != nil { 559 t.Fatalf("failed to create new message with seed %d: %s.", seed, err) 560 } 561 env, err = msg.Wrap(params) 562 if err != nil { 563 t.Fatalf("failed Wrap with seed %d: %s.", seed, err) 564 } 565 w.SetMaxMessageSize(uint32(env.size() - 1)) 566 err = w.Send(env) 567 if err == nil { 568 t.Fatalf("successfully sent oversized envelope (seed %d): false positive.", seed) 569 } 570 571 w.SetMaxMessageSize(DefaultMaxMessageSize) 572 err = w.Send(env) 573 if err != nil { 574 t.Fatalf("failed to send second envelope with seed %d: %s.", seed, err) 575 } 576 577 // wait till received or timeout 578 var received bool 579 for j := 0; j < 20; j++ { 580 time.Sleep(100 * time.Millisecond) 581 if len(w.Envelopes()) > 1 { 582 received = true 583 break 584 } 585 } 586 587 if !received { 588 t.Fatalf("did not receive the sent envelope, seed: %d.", seed) 589 } 590 591 // check w.messages() 592 _, err = w.Subscribe(f) 593 if err != nil { 594 t.Fatalf("failed subscribe with seed %d: %s.", seed, err) 595 } 596 time.Sleep(5 * time.Millisecond) 597 mail := f.Retrieve() 598 if len(mail) > 0 { 599 t.Fatalf("received premature mail") 600 } 601 } 602 603 func TestSymmetricSendCycle(t *testing.T) { 604 InitSingleTest() 605 606 w := New(&DefaultConfig) 607 defer w.SetMinimumPowTest(DefaultMinimumPoW) 608 defer w.SetMaxMessageSize(DefaultMaxMessageSize) 609 w.Start(nil) 610 defer w.Stop() 611 612 filter1, err := generateFilter(t, true) 613 if err != nil { 614 t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 615 } 616 filter1.PoW = DefaultMinimumPoW 617 618 // Copy the first filter since some of its fields 619 // are randomly gnerated. 620 filter2 := &Filter{ 621 KeySym: filter1.KeySym, 622 Topics: filter1.Topics, 623 PoW: filter1.PoW, 624 AllowP2P: filter1.AllowP2P, 625 Messages: make(map[common.Hash]*ReceivedMessage), 626 } 627 628 params, err := generateMessageParams() 629 if err != nil { 630 t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 631 } 632 633 filter1.Src = ¶ms.Src.PublicKey 634 filter2.Src = ¶ms.Src.PublicKey 635 636 params.KeySym = filter1.KeySym 637 params.Topic = BytesToTopic(filter1.Topics[2]) 638 params.PoW = filter1.PoW 639 params.WorkTime = 10 640 params.TTL = 50 641 msg, err := NewSentMessage(params) 642 if err != nil { 643 t.Fatalf("failed to create new message with seed %d: %s.", seed, err) 644 } 645 env, err := msg.Wrap(params) 646 if err != nil { 647 t.Fatalf("failed Wrap with seed %d: %s.", seed, err) 648 } 649 650 _, err = w.Subscribe(filter1) 651 if err != nil { 652 t.Fatalf("failed subscribe 1 with seed %d: %s.", seed, err) 653 } 654 655 _, err = w.Subscribe(filter2) 656 if err != nil { 657 t.Fatalf("failed subscribe 2 with seed %d: %s.", seed, err) 658 } 659 660 err = w.Send(env) 661 if err != nil { 662 t.Fatalf("Failed sending envelope with PoW %.06f (seed %d): %s", env.PoW(), seed, err) 663 } 664 665 // wait till received or timeout 666 var received bool 667 for j := 0; j < 200; j++ { 668 time.Sleep(10 * time.Millisecond) 669 if len(w.Envelopes()) > 0 { 670 received = true 671 break 672 } 673 } 674 675 if !received { 676 t.Fatalf("did not receive the sent envelope, seed: %d.", seed) 677 } 678 679 // check w.messages() 680 time.Sleep(5 * time.Millisecond) 681 mail1 := filter1.Retrieve() 682 mail2 := filter2.Retrieve() 683 if len(mail2) == 0 { 684 t.Fatalf("did not receive any email for filter 2") 685 } 686 if len(mail1) == 0 { 687 t.Fatalf("did not receive any email for filter 1") 688 } 689 690 } 691 692 func TestSymmetricSendWithoutAKey(t *testing.T) { 693 InitSingleTest() 694 695 w := New(&DefaultConfig) 696 defer w.SetMinimumPowTest(DefaultMinimumPoW) 697 defer w.SetMaxMessageSize(DefaultMaxMessageSize) 698 w.Start(nil) 699 defer w.Stop() 700 701 filter, err := generateFilter(t, true) 702 if err != nil { 703 t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 704 } 705 filter.PoW = DefaultMinimumPoW 706 707 params, err := generateMessageParams() 708 if err != nil { 709 t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 710 } 711 712 filter.Src = nil 713 714 params.KeySym = filter.KeySym 715 params.Topic = BytesToTopic(filter.Topics[2]) 716 params.PoW = filter.PoW 717 params.WorkTime = 10 718 params.TTL = 50 719 msg, err := NewSentMessage(params) 720 if err != nil { 721 t.Fatalf("failed to create new message with seed %d: %s.", seed, err) 722 } 723 env, err := msg.Wrap(params) 724 if err != nil { 725 t.Fatalf("failed Wrap with seed %d: %s.", seed, err) 726 } 727 728 _, err = w.Subscribe(filter) 729 if err != nil { 730 t.Fatalf("failed subscribe 1 with seed %d: %s.", seed, err) 731 } 732 733 err = w.Send(env) 734 if err != nil { 735 t.Fatalf("Failed sending envelope with PoW %.06f (seed %d): %s", env.PoW(), seed, err) 736 } 737 738 // wait till received or timeout 739 var received bool 740 for j := 0; j < 200; j++ { 741 time.Sleep(10 * time.Millisecond) 742 if len(w.Envelopes()) > 0 { 743 received = true 744 break 745 } 746 } 747 748 if !received { 749 t.Fatalf("did not receive the sent envelope, seed: %d.", seed) 750 } 751 752 // check w.messages() 753 time.Sleep(5 * time.Millisecond) 754 mail := filter.Retrieve() 755 if len(mail) == 0 { 756 t.Fatalf("did not receive message in spite of not setting a public key") 757 } 758 } 759 760 func TestSymmetricSendKeyMismatch(t *testing.T) { 761 InitSingleTest() 762 763 w := New(&DefaultConfig) 764 defer w.SetMinimumPowTest(DefaultMinimumPoW) 765 defer w.SetMaxMessageSize(DefaultMaxMessageSize) 766 w.Start(nil) 767 defer w.Stop() 768 769 filter, err := generateFilter(t, true) 770 if err != nil { 771 t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 772 } 773 filter.PoW = DefaultMinimumPoW 774 775 params, err := generateMessageParams() 776 if err != nil { 777 t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 778 } 779 780 params.KeySym = filter.KeySym 781 params.Topic = BytesToTopic(filter.Topics[2]) 782 params.PoW = filter.PoW 783 params.WorkTime = 10 784 params.TTL = 50 785 msg, err := NewSentMessage(params) 786 if err != nil { 787 t.Fatalf("failed to create new message with seed %d: %s.", seed, err) 788 } 789 env, err := msg.Wrap(params) 790 if err != nil { 791 t.Fatalf("failed Wrap with seed %d: %s.", seed, err) 792 } 793 794 _, err = w.Subscribe(filter) 795 if err != nil { 796 t.Fatalf("failed subscribe 1 with seed %d: %s.", seed, err) 797 } 798 799 err = w.Send(env) 800 if err != nil { 801 t.Fatalf("Failed sending envelope with PoW %.06f (seed %d): %s", env.PoW(), seed, err) 802 } 803 804 // wait till received or timeout 805 var received bool 806 for j := 0; j < 200; j++ { 807 time.Sleep(10 * time.Millisecond) 808 if len(w.Envelopes()) > 0 { 809 received = true 810 break 811 } 812 } 813 814 if !received { 815 t.Fatalf("did not receive the sent envelope, seed: %d.", seed) 816 } 817 818 // check w.messages() 819 time.Sleep(5 * time.Millisecond) 820 mail := filter.Retrieve() 821 if len(mail) > 0 { 822 t.Fatalf("received a message when keys weren't matching") 823 } 824 } 825 826 func TestBloom(t *testing.T) { 827 topic := TopicType{0, 0, 255, 6} 828 b := TopicToBloom(topic) 829 x := make([]byte, BloomFilterSize) 830 x[0] = byte(1) 831 x[32] = byte(1) 832 x[BloomFilterSize-1] = byte(128) 833 if !BloomFilterMatch(x, b) || !BloomFilterMatch(b, x) { 834 t.Fatalf("bloom filter does not match the mask") 835 } 836 837 _, err := mrand.Read(b) 838 if err != nil { 839 t.Fatalf("math rand error") 840 } 841 _, err = mrand.Read(x) 842 if err != nil { 843 t.Fatalf("math rand error") 844 } 845 if !BloomFilterMatch(b, b) { 846 t.Fatalf("bloom filter does not match self") 847 } 848 x = addBloom(x, b) 849 if !BloomFilterMatch(x, b) { 850 t.Fatalf("bloom filter does not match combined bloom") 851 } 852 if !isFullNode(nil) { 853 t.Fatalf("isFullNode did not recognize nil as full node") 854 } 855 x[17] = 254 856 if isFullNode(x) { 857 t.Fatalf("isFullNode false positive") 858 } 859 for i := 0; i < BloomFilterSize; i++ { 860 b[i] = byte(255) 861 } 862 if !isFullNode(b) { 863 t.Fatalf("isFullNode false negative") 864 } 865 if BloomFilterMatch(x, b) { 866 t.Fatalf("bloomFilterMatch false positive") 867 } 868 if !BloomFilterMatch(b, x) { 869 t.Fatalf("bloomFilterMatch false negative") 870 } 871 872 w := New(&DefaultConfig) 873 f := w.BloomFilter() 874 if f != nil { 875 t.Fatalf("wrong bloom on creation") 876 } 877 err = w.SetBloomFilter(x) 878 if err != nil { 879 t.Fatalf("failed to set bloom filter: %s", err) 880 } 881 f = w.BloomFilter() 882 if !BloomFilterMatch(f, x) || !BloomFilterMatch(x, f) { 883 t.Fatalf("retireved wrong bloom filter") 884 } 885 }