envelope_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2015 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. package whisperv2
  17. import (
  18. "bytes"
  19. "testing"
  20. "time"
  21. "github.com/ethereum/go-ethereum/crypto"
  22. "github.com/ethereum/go-ethereum/crypto/ecies"
  23. )
  24. func TestEnvelopeOpen(t *testing.T) {
  25. payload := []byte("hello world")
  26. message := NewMessage(payload)
  27. envelope, err := message.Wrap(DefaultPoW, Options{})
  28. if err != nil {
  29. t.Fatalf("failed to wrap message: %v", err)
  30. }
  31. opened, err := envelope.Open(nil)
  32. if err != nil {
  33. t.Fatalf("failed to open envelope: %v", err)
  34. }
  35. if opened.Flags != message.Flags {
  36. t.Fatalf("flags mismatch: have %d, want %d", opened.Flags, message.Flags)
  37. }
  38. if !bytes.Equal(opened.Signature, message.Signature) {
  39. t.Fatalf("signature mismatch: have 0x%x, want 0x%x", opened.Signature, message.Signature)
  40. }
  41. if !bytes.Equal(opened.Payload, message.Payload) {
  42. t.Fatalf("payload mismatch: have 0x%x, want 0x%x", opened.Payload, message.Payload)
  43. }
  44. if opened.Sent.Unix() != message.Sent.Unix() {
  45. t.Fatalf("send time mismatch: have %v, want %v", opened.Sent, message.Sent)
  46. }
  47. if opened.TTL/time.Second != DefaultTTL/time.Second {
  48. t.Fatalf("message TTL mismatch: have %v, want %v", opened.TTL, DefaultTTL)
  49. }
  50. if opened.Hash != envelope.Hash() {
  51. t.Fatalf("message hash mismatch: have 0x%x, want 0x%x", opened.Hash, envelope.Hash())
  52. }
  53. }
  54. func TestEnvelopeAnonymousOpenUntargeted(t *testing.T) {
  55. payload := []byte("hello envelope")
  56. envelope, err := NewMessage(payload).Wrap(DefaultPoW, Options{})
  57. if err != nil {
  58. t.Fatalf("failed to wrap message: %v", err)
  59. }
  60. opened, err := envelope.Open(nil)
  61. if err != nil {
  62. t.Fatalf("failed to open envelope: %v", err)
  63. }
  64. if opened.To != nil {
  65. t.Fatalf("recipient mismatch: have 0x%x, want nil", opened.To)
  66. }
  67. if !bytes.Equal(opened.Payload, payload) {
  68. t.Fatalf("payload mismatch: have 0x%x, want 0x%x", opened.Payload, payload)
  69. }
  70. }
  71. func TestEnvelopeAnonymousOpenTargeted(t *testing.T) {
  72. key, err := crypto.GenerateKey()
  73. if err != nil {
  74. t.Fatalf("failed to generate test identity: %v", err)
  75. }
  76. payload := []byte("hello envelope")
  77. envelope, err := NewMessage(payload).Wrap(DefaultPoW, Options{
  78. To: &key.PublicKey,
  79. })
  80. if err != nil {
  81. t.Fatalf("failed to wrap message: %v", err)
  82. }
  83. opened, err := envelope.Open(nil)
  84. if err != nil {
  85. t.Fatalf("failed to open envelope: %v", err)
  86. }
  87. if opened.To != nil {
  88. t.Fatalf("recipient mismatch: have 0x%x, want nil", opened.To)
  89. }
  90. if bytes.Equal(opened.Payload, payload) {
  91. t.Fatalf("payload match, should have been encrypted: 0x%x", opened.Payload)
  92. }
  93. }
  94. func TestEnvelopeIdentifiedOpenUntargeted(t *testing.T) {
  95. key, err := crypto.GenerateKey()
  96. if err != nil {
  97. t.Fatalf("failed to generate test identity: %v", err)
  98. }
  99. payload := []byte("hello envelope")
  100. envelope, err := NewMessage(payload).Wrap(DefaultPoW, Options{})
  101. if err != nil {
  102. t.Fatalf("failed to wrap message: %v", err)
  103. }
  104. opened, err := envelope.Open(key)
  105. switch err {
  106. case nil:
  107. t.Fatalf("envelope opened with bad key: %v", opened)
  108. case ecies.ErrInvalidPublicKey:
  109. // Ok, key mismatch but opened
  110. default:
  111. t.Fatalf("failed to open envelope: %v", err)
  112. }
  113. if opened.To != nil {
  114. t.Fatalf("recipient mismatch: have 0x%x, want nil", opened.To)
  115. }
  116. if !bytes.Equal(opened.Payload, payload) {
  117. t.Fatalf("payload mismatch: have 0x%x, want 0x%x", opened.Payload, payload)
  118. }
  119. }
  120. func TestEnvelopeIdentifiedOpenTargeted(t *testing.T) {
  121. key, err := crypto.GenerateKey()
  122. if err != nil {
  123. t.Fatalf("failed to generate test identity: %v", err)
  124. }
  125. payload := []byte("hello envelope")
  126. envelope, err := NewMessage(payload).Wrap(DefaultPoW, Options{
  127. To: &key.PublicKey,
  128. })
  129. if err != nil {
  130. t.Fatalf("failed to wrap message: %v", err)
  131. }
  132. opened, err := envelope.Open(key)
  133. if err != nil {
  134. t.Fatalf("failed to open envelope: %v", err)
  135. }
  136. if opened.To != nil {
  137. t.Fatalf("recipient mismatch: have 0x%x, want nil", opened.To)
  138. }
  139. if !bytes.Equal(opened.Payload, payload) {
  140. t.Fatalf("payload mismatch: have 0x%x, want 0x%x", opened.Payload, payload)
  141. }
  142. }