message_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2014 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 whisper
  17. import (
  18. "bytes"
  19. "crypto/elliptic"
  20. "testing"
  21. "time"
  22. "github.com/ethereum/go-ethereum/crypto"
  23. "github.com/ethereum/go-ethereum/crypto/secp256k1"
  24. )
  25. // Tests whether a message can be wrapped without any identity or encryption.
  26. func TestMessageSimpleWrap(t *testing.T) {
  27. payload := []byte("hello world")
  28. msg := NewMessage(payload)
  29. if _, err := msg.Wrap(DefaultPoW, Options{}); err != nil {
  30. t.Fatalf("failed to wrap message: %v", err)
  31. }
  32. if msg.Flags&signatureFlag != 0 {
  33. t.Fatalf("signature flag mismatch: have %d, want %d", msg.Flags&signatureFlag, 0)
  34. }
  35. if len(msg.Signature) != 0 {
  36. t.Fatalf("signature found for simple wrapping: 0x%x", msg.Signature)
  37. }
  38. if bytes.Compare(msg.Payload, payload) != 0 {
  39. t.Fatalf("payload mismatch after wrapping: have 0x%x, want 0x%x", msg.Payload, payload)
  40. }
  41. if msg.TTL/time.Second != DefaultTTL/time.Second {
  42. t.Fatalf("message TTL mismatch: have %v, want %v", msg.TTL, DefaultTTL)
  43. }
  44. }
  45. // Tests whether a message can be signed, and wrapped in plain-text.
  46. func TestMessageCleartextSignRecover(t *testing.T) {
  47. key, err := crypto.GenerateKey()
  48. if err != nil {
  49. t.Fatalf("failed to create crypto key: %v", err)
  50. }
  51. payload := []byte("hello world")
  52. msg := NewMessage(payload)
  53. if _, err := msg.Wrap(DefaultPoW, Options{
  54. From: key,
  55. }); err != nil {
  56. t.Fatalf("failed to sign message: %v", err)
  57. }
  58. if msg.Flags&signatureFlag != signatureFlag {
  59. t.Fatalf("signature flag mismatch: have %d, want %d", msg.Flags&signatureFlag, signatureFlag)
  60. }
  61. if bytes.Compare(msg.Payload, payload) != 0 {
  62. t.Fatalf("payload mismatch after signing: have 0x%x, want 0x%x", msg.Payload, payload)
  63. }
  64. pubKey := msg.Recover()
  65. if pubKey == nil {
  66. t.Fatalf("failed to recover public key")
  67. }
  68. p1 := elliptic.Marshal(secp256k1.S256(), key.PublicKey.X, key.PublicKey.Y)
  69. p2 := elliptic.Marshal(secp256k1.S256(), pubKey.X, pubKey.Y)
  70. if !bytes.Equal(p1, p2) {
  71. t.Fatalf("public key mismatch: have 0x%x, want 0x%x", p2, p1)
  72. }
  73. }
  74. // Tests whether a message can be encrypted and decrypted using an anonymous
  75. // sender (i.e. no signature).
  76. func TestMessageAnonymousEncryptDecrypt(t *testing.T) {
  77. key, err := crypto.GenerateKey()
  78. if err != nil {
  79. t.Fatalf("failed to create recipient crypto key: %v", err)
  80. }
  81. payload := []byte("hello world")
  82. msg := NewMessage(payload)
  83. envelope, err := msg.Wrap(DefaultPoW, Options{
  84. To: &key.PublicKey,
  85. })
  86. if err != nil {
  87. t.Fatalf("failed to encrypt message: %v", err)
  88. }
  89. if msg.Flags&signatureFlag != 0 {
  90. t.Fatalf("signature flag mismatch: have %d, want %d", msg.Flags&signatureFlag, 0)
  91. }
  92. if len(msg.Signature) != 0 {
  93. t.Fatalf("signature found for anonymous message: 0x%x", msg.Signature)
  94. }
  95. out, err := envelope.Open(key)
  96. if err != nil {
  97. t.Fatalf("failed to open encrypted message: %v", err)
  98. }
  99. if !bytes.Equal(out.Payload, payload) {
  100. t.Errorf("payload mismatch: have 0x%x, want 0x%x", out.Payload, payload)
  101. }
  102. }
  103. // Tests whether a message can be properly signed and encrypted.
  104. func TestMessageFullCrypto(t *testing.T) {
  105. fromKey, err := crypto.GenerateKey()
  106. if err != nil {
  107. t.Fatalf("failed to create sender crypto key: %v", err)
  108. }
  109. toKey, err := crypto.GenerateKey()
  110. if err != nil {
  111. t.Fatalf("failed to create recipient crypto key: %v", err)
  112. }
  113. payload := []byte("hello world")
  114. msg := NewMessage(payload)
  115. envelope, err := msg.Wrap(DefaultPoW, Options{
  116. From: fromKey,
  117. To: &toKey.PublicKey,
  118. })
  119. if err != nil {
  120. t.Fatalf("failed to encrypt message: %v", err)
  121. }
  122. if msg.Flags&signatureFlag != signatureFlag {
  123. t.Fatalf("signature flag mismatch: have %d, want %d", msg.Flags&signatureFlag, signatureFlag)
  124. }
  125. if len(msg.Signature) == 0 {
  126. t.Fatalf("no signature found for signed message")
  127. }
  128. out, err := envelope.Open(toKey)
  129. if err != nil {
  130. t.Fatalf("failed to open encrypted message: %v", err)
  131. }
  132. if !bytes.Equal(out.Payload, payload) {
  133. t.Errorf("payload mismatch: have 0x%x, want 0x%x", out.Payload, payload)
  134. }
  135. pubKey := out.Recover()
  136. if pubKey == nil {
  137. t.Fatalf("failed to recover public key")
  138. }
  139. p1 := elliptic.Marshal(secp256k1.S256(), fromKey.PublicKey.X, fromKey.PublicKey.Y)
  140. p2 := elliptic.Marshal(secp256k1.S256(), pubKey.X, pubKey.Y)
  141. if !bytes.Equal(p1, p2) {
  142. t.Fatalf("public key mismatch: have 0x%x, want 0x%x", p2, p1)
  143. }
  144. }