/whisper/message_test.go

https://gitlab.com/akomba/ether-bot-wallet · Go · 158 lines · 123 code · 15 blank · 20 comment · 48 complexity · 1262548c718a8d9cfcca0703bd7e0923 MD5 · raw file

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