message_test.go 4.1 KB

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