envelope_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2017 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. // Contains the tests associated with the Whisper protocol Envelope object.
  17. package whisperv6
  18. import (
  19. mrand "math/rand"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/crypto"
  22. )
  23. func TestPoWCalculationsWithNoLeadingZeros(t *testing.T) {
  24. e := Envelope{
  25. TTL: 1,
  26. Data: []byte{0xde, 0xad, 0xbe, 0xef},
  27. Nonce: 100000,
  28. }
  29. e.calculatePoW(0)
  30. if e.pow != 0.07692307692307693 {
  31. t.Fatalf("invalid PoW calculation. Expected 0.07692307692307693, got %v", e.pow)
  32. }
  33. }
  34. func TestPoWCalculationsWith8LeadingZeros(t *testing.T) {
  35. e := Envelope{
  36. TTL: 1,
  37. Data: []byte{0xde, 0xad, 0xbe, 0xef},
  38. Nonce: 276,
  39. }
  40. e.calculatePoW(0)
  41. if e.pow != 19.692307692307693 {
  42. t.Fatalf("invalid PoW calculation. Expected 19.692307692307693, got %v", e.pow)
  43. }
  44. }
  45. func TestEnvelopeOpenAcceptsOnlyOneKeyTypeInFilter(t *testing.T) {
  46. symKey := make([]byte, aesKeyLength)
  47. mrand.Read(symKey)
  48. asymKey, err := crypto.GenerateKey()
  49. if err != nil {
  50. t.Fatalf("failed GenerateKey with seed %d: %s.", seed, err)
  51. }
  52. params := MessageParams{
  53. PoW: 0.01,
  54. WorkTime: 1,
  55. TTL: uint32(mrand.Intn(1024)),
  56. Payload: make([]byte, 50),
  57. KeySym: symKey,
  58. Dst: nil,
  59. }
  60. mrand.Read(params.Payload)
  61. msg, err := NewSentMessage(&params)
  62. if err != nil {
  63. t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
  64. }
  65. e, err := msg.Wrap(&params)
  66. if err != nil {
  67. t.Fatalf("Failed to Wrap the message in an envelope with seed %d: %s", seed, err)
  68. }
  69. f := Filter{KeySym: symKey, KeyAsym: asymKey}
  70. decrypted := e.Open(&f)
  71. if decrypted != nil {
  72. t.Fatalf("Managed to decrypt a message with an invalid filter, seed %d", seed)
  73. }
  74. }