github.com/aitimate-0/go-ethereum@v1.9.7/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/ethereum/go-ethereum/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(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 params.TTL = 1 469 470 messagesCount := 5 471 472 // Send a few messages one after another. Due to low PoW and expiration buckets 473 // with one second resolution, it covers a case when there are multiple items 474 // in a single expiration bucket. 475 for i := 0; i < messagesCount; i++ { 476 msg, err := NewSentMessage(params) 477 if err != nil { 478 t.Fatalf("failed to create new message with seed %d: %s.", seed, err) 479 } 480 env, err := msg.Wrap(params) 481 if err != nil { 482 t.Fatalf("failed Wrap with seed %d: %s.", seed, err) 483 } 484 485 err = w.Send(env) 486 if err != nil { 487 t.Fatalf("failed to send envelope with seed %d: %s.", seed, err) 488 } 489 } 490 491 // wait till received or timeout 492 var received, expired bool 493 for j := 0; j < 20; j++ { 494 time.Sleep(100 * time.Millisecond) 495 if len(w.Envelopes()) == messagesCount { 496 received = true 497 break 498 } 499 } 500 501 if !received { 502 t.Fatalf("did not receive the sent envelope, seed: %d.", seed) 503 } 504 505 // wait till expired or timeout 506 for j := 0; j < 20; j++ { 507 time.Sleep(100 * time.Millisecond) 508 if len(w.Envelopes()) == 0 { 509 expired = true 510 break 511 } 512 } 513 514 if !expired { 515 t.Fatalf("expire failed, seed: %d.", seed) 516 } 517 } 518 519 func TestCustomization(t *testing.T) { 520 InitSingleTest() 521 522 w := New(&DefaultConfig) 523 defer w.SetMinimumPowTest(DefaultMinimumPoW) 524 defer w.SetMaxMessageSize(DefaultMaxMessageSize) 525 w.Start(nil) 526 defer w.Stop() 527 528 const smallPoW = 0.00001 529 530 f, err := generateFilter(t, true) 531 if err != nil { 532 t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 533 } 534 params, err := generateMessageParams() 535 if err != nil { 536 t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 537 } 538 539 params.KeySym = f.KeySym 540 params.Topic = BytesToTopic(f.Topics[2]) 541 params.PoW = smallPoW 542 params.TTL = 3600 * 24 // one day 543 msg, err := NewSentMessage(params) 544 if err != nil { 545 t.Fatalf("failed to create new message with seed %d: %s.", seed, err) 546 } 547 env, err := msg.Wrap(params) 548 if err != nil { 549 t.Fatalf("failed Wrap with seed %d: %s.", seed, err) 550 } 551 552 err = w.Send(env) 553 if err == nil { 554 t.Fatalf("successfully sent envelope with PoW %.06f, false positive (seed %d).", env.PoW(), seed) 555 } 556 557 w.SetMinimumPowTest(smallPoW / 2) 558 err = w.Send(env) 559 if err != nil { 560 t.Fatalf("failed to send envelope with seed %d: %s.", seed, err) 561 } 562 563 params.TTL++ 564 msg, err = NewSentMessage(params) 565 if err != nil { 566 t.Fatalf("failed to create new message with seed %d: %s.", seed, err) 567 } 568 env, err = msg.Wrap(params) 569 if err != nil { 570 t.Fatalf("failed Wrap with seed %d: %s.", seed, err) 571 } 572 w.SetMaxMessageSize(uint32(env.size() - 1)) 573 err = w.Send(env) 574 if err == nil { 575 t.Fatalf("successfully sent oversized envelope (seed %d): false positive.", seed) 576 } 577 578 w.SetMaxMessageSize(DefaultMaxMessageSize) 579 err = w.Send(env) 580 if err != nil { 581 t.Fatalf("failed to send second envelope with seed %d: %s.", seed, err) 582 } 583 584 // wait till received or timeout 585 var received bool 586 for j := 0; j < 20; j++ { 587 time.Sleep(100 * time.Millisecond) 588 if len(w.Envelopes()) > 1 { 589 received = true 590 break 591 } 592 } 593 594 if !received { 595 t.Fatalf("did not receive the sent envelope, seed: %d.", seed) 596 } 597 598 // check w.messages() 599 _, err = w.Subscribe(f) 600 if err != nil { 601 t.Fatalf("failed subscribe with seed %d: %s.", seed, err) 602 } 603 time.Sleep(5 * time.Millisecond) 604 mail := f.Retrieve() 605 if len(mail) > 0 { 606 t.Fatalf("received premature mail") 607 } 608 } 609 610 func TestSymmetricSendCycle(t *testing.T) { 611 InitSingleTest() 612 613 w := New(&DefaultConfig) 614 defer w.SetMinimumPowTest(DefaultMinimumPoW) 615 defer w.SetMaxMessageSize(DefaultMaxMessageSize) 616 w.Start(nil) 617 defer w.Stop() 618 619 filter1, err := generateFilter(t, true) 620 if err != nil { 621 t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 622 } 623 filter1.PoW = DefaultMinimumPoW 624 625 // Copy the first filter since some of its fields 626 // are randomly gnerated. 627 filter2 := &Filter{ 628 KeySym: filter1.KeySym, 629 Topics: filter1.Topics, 630 PoW: filter1.PoW, 631 AllowP2P: filter1.AllowP2P, 632 Messages: make(map[common.Hash]*ReceivedMessage), 633 } 634 635 params, err := generateMessageParams() 636 if err != nil { 637 t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 638 } 639 640 filter1.Src = ¶ms.Src.PublicKey 641 filter2.Src = ¶ms.Src.PublicKey 642 643 params.KeySym = filter1.KeySym 644 params.Topic = BytesToTopic(filter1.Topics[2]) 645 params.PoW = filter1.PoW 646 params.WorkTime = 10 647 params.TTL = 50 648 msg, err := NewSentMessage(params) 649 if err != nil { 650 t.Fatalf("failed to create new message with seed %d: %s.", seed, err) 651 } 652 env, err := msg.Wrap(params) 653 if err != nil { 654 t.Fatalf("failed Wrap with seed %d: %s.", seed, err) 655 } 656 657 _, err = w.Subscribe(filter1) 658 if err != nil { 659 t.Fatalf("failed subscribe 1 with seed %d: %s.", seed, err) 660 } 661 662 _, err = w.Subscribe(filter2) 663 if err != nil { 664 t.Fatalf("failed subscribe 2 with seed %d: %s.", seed, err) 665 } 666 667 err = w.Send(env) 668 if err != nil { 669 t.Fatalf("Failed sending envelope with PoW %.06f (seed %d): %s", env.PoW(), seed, err) 670 } 671 672 // wait till received or timeout 673 var received bool 674 for j := 0; j < 200; j++ { 675 time.Sleep(10 * time.Millisecond) 676 if len(w.Envelopes()) > 0 { 677 received = true 678 break 679 } 680 } 681 682 if !received { 683 t.Fatalf("did not receive the sent envelope, seed: %d.", seed) 684 } 685 686 // check w.messages() 687 time.Sleep(5 * time.Millisecond) 688 mail1 := filter1.Retrieve() 689 mail2 := filter2.Retrieve() 690 if len(mail2) == 0 { 691 t.Fatalf("did not receive any email for filter 2") 692 } 693 if len(mail1) == 0 { 694 t.Fatalf("did not receive any email for filter 1") 695 } 696 697 } 698 699 func TestSymmetricSendWithoutAKey(t *testing.T) { 700 InitSingleTest() 701 702 w := New(&DefaultConfig) 703 defer w.SetMinimumPowTest(DefaultMinimumPoW) 704 defer w.SetMaxMessageSize(DefaultMaxMessageSize) 705 w.Start(nil) 706 defer w.Stop() 707 708 filter, err := generateFilter(t, true) 709 if err != nil { 710 t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 711 } 712 filter.PoW = DefaultMinimumPoW 713 714 params, err := generateMessageParams() 715 if err != nil { 716 t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 717 } 718 719 filter.Src = nil 720 721 params.KeySym = filter.KeySym 722 params.Topic = BytesToTopic(filter.Topics[2]) 723 params.PoW = filter.PoW 724 params.WorkTime = 10 725 params.TTL = 50 726 msg, err := NewSentMessage(params) 727 if err != nil { 728 t.Fatalf("failed to create new message with seed %d: %s.", seed, err) 729 } 730 env, err := msg.Wrap(params) 731 if err != nil { 732 t.Fatalf("failed Wrap with seed %d: %s.", seed, err) 733 } 734 735 _, err = w.Subscribe(filter) 736 if err != nil { 737 t.Fatalf("failed subscribe 1 with seed %d: %s.", seed, err) 738 } 739 740 err = w.Send(env) 741 if err != nil { 742 t.Fatalf("Failed sending envelope with PoW %.06f (seed %d): %s", env.PoW(), seed, err) 743 } 744 745 // wait till received or timeout 746 var received bool 747 for j := 0; j < 200; j++ { 748 time.Sleep(10 * time.Millisecond) 749 if len(w.Envelopes()) > 0 { 750 received = true 751 break 752 } 753 } 754 755 if !received { 756 t.Fatalf("did not receive the sent envelope, seed: %d.", seed) 757 } 758 759 // check w.messages() 760 time.Sleep(5 * time.Millisecond) 761 mail := filter.Retrieve() 762 if len(mail) == 0 { 763 t.Fatalf("did not receive message in spite of not setting a public key") 764 } 765 } 766 767 func TestSymmetricSendKeyMismatch(t *testing.T) { 768 InitSingleTest() 769 770 w := New(&DefaultConfig) 771 defer w.SetMinimumPowTest(DefaultMinimumPoW) 772 defer w.SetMaxMessageSize(DefaultMaxMessageSize) 773 w.Start(nil) 774 defer w.Stop() 775 776 filter, err := generateFilter(t, true) 777 if err != nil { 778 t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 779 } 780 filter.PoW = DefaultMinimumPoW 781 782 params, err := generateMessageParams() 783 if err != nil { 784 t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 785 } 786 787 params.KeySym = filter.KeySym 788 params.Topic = BytesToTopic(filter.Topics[2]) 789 params.PoW = filter.PoW 790 params.WorkTime = 10 791 params.TTL = 50 792 msg, err := NewSentMessage(params) 793 if err != nil { 794 t.Fatalf("failed to create new message with seed %d: %s.", seed, err) 795 } 796 env, err := msg.Wrap(params) 797 if err != nil { 798 t.Fatalf("failed Wrap with seed %d: %s.", seed, err) 799 } 800 801 _, err = w.Subscribe(filter) 802 if err != nil { 803 t.Fatalf("failed subscribe 1 with seed %d: %s.", seed, err) 804 } 805 806 err = w.Send(env) 807 if err != nil { 808 t.Fatalf("Failed sending envelope with PoW %.06f (seed %d): %s", env.PoW(), seed, err) 809 } 810 811 // wait till received or timeout 812 var received bool 813 for j := 0; j < 200; j++ { 814 time.Sleep(10 * time.Millisecond) 815 if len(w.Envelopes()) > 0 { 816 received = true 817 break 818 } 819 } 820 821 if !received { 822 t.Fatalf("did not receive the sent envelope, seed: %d.", seed) 823 } 824 825 // check w.messages() 826 time.Sleep(5 * time.Millisecond) 827 mail := filter.Retrieve() 828 if len(mail) > 0 { 829 t.Fatalf("received a message when keys weren't matching") 830 } 831 } 832 833 func TestBloom(t *testing.T) { 834 topic := TopicType{0, 0, 255, 6} 835 b := TopicToBloom(topic) 836 x := make([]byte, BloomFilterSize) 837 x[0] = byte(1) 838 x[32] = byte(1) 839 x[BloomFilterSize-1] = byte(128) 840 if !BloomFilterMatch(x, b) || !BloomFilterMatch(b, x) { 841 t.Fatalf("bloom filter does not match the mask") 842 } 843 844 _, err := mrand.Read(b) 845 if err != nil { 846 t.Fatalf("math rand error") 847 } 848 _, err = mrand.Read(x) 849 if err != nil { 850 t.Fatalf("math rand error") 851 } 852 if !BloomFilterMatch(b, b) { 853 t.Fatalf("bloom filter does not match self") 854 } 855 x = addBloom(x, b) 856 if !BloomFilterMatch(x, b) { 857 t.Fatalf("bloom filter does not match combined bloom") 858 } 859 if !isFullNode(nil) { 860 t.Fatalf("isFullNode did not recognize nil as full node") 861 } 862 x[17] = 254 863 if isFullNode(x) { 864 t.Fatalf("isFullNode false positive") 865 } 866 for i := 0; i < BloomFilterSize; i++ { 867 b[i] = byte(255) 868 } 869 if !isFullNode(b) { 870 t.Fatalf("isFullNode false negative") 871 } 872 if BloomFilterMatch(x, b) { 873 t.Fatalf("bloomFilterMatch false positive") 874 } 875 if !BloomFilterMatch(b, x) { 876 t.Fatalf("bloomFilterMatch false negative") 877 } 878 879 w := New(&DefaultConfig) 880 f := w.BloomFilter() 881 if f != nil { 882 t.Fatalf("wrong bloom on creation") 883 } 884 err = w.SetBloomFilter(x) 885 if err != nil { 886 t.Fatalf("failed to set bloom filter: %s", err) 887 } 888 f = w.BloomFilter() 889 if !BloomFilterMatch(f, x) || !BloomFilterMatch(x, f) { 890 t.Fatalf("retireved wrong bloom filter") 891 } 892 }