server_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. package mailserver
  17. import (
  18. "crypto/ecdsa"
  19. "encoding/binary"
  20. "io/ioutil"
  21. "math/rand"
  22. "testing"
  23. "time"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/crypto"
  26. whisper "github.com/ethereum/go-ethereum/whisper/whisperv5"
  27. )
  28. const powRequirement = 0.00001
  29. var keyID string
  30. var shh *whisper.Whisper
  31. var seed = time.Now().Unix()
  32. type ServerTestParams struct {
  33. topic whisper.TopicType
  34. low uint32
  35. upp uint32
  36. key *ecdsa.PrivateKey
  37. }
  38. func assert(statement bool, text string, t *testing.T) {
  39. if !statement {
  40. t.Fatal(text)
  41. }
  42. }
  43. func TestDBKey(t *testing.T) {
  44. var h common.Hash
  45. i := uint32(time.Now().Unix())
  46. k := NewDbKey(i, h)
  47. assert(len(k.raw) == common.HashLength+4, "wrong DB key length", t)
  48. assert(byte(i%0x100) == k.raw[3], "raw representation should be big endian", t)
  49. assert(byte(i/0x1000000) == k.raw[0], "big endian expected", t)
  50. }
  51. func generateEnvelope(t *testing.T) *whisper.Envelope {
  52. params := &whisper.MessageParams{
  53. KeySym: []byte("test key"),
  54. Topic: whisper.TopicType{},
  55. Payload: []byte("test payload"),
  56. PoW: powRequirement,
  57. WorkTime: 2,
  58. }
  59. msg := whisper.NewSentMessage(params)
  60. env, err := msg.Wrap(params)
  61. if err != nil {
  62. t.Fatalf("failed to wrap with seed %d: %s.", seed, err)
  63. }
  64. return env
  65. }
  66. func TestMailServer(t *testing.T) {
  67. const password = "password_for_this_test"
  68. const dbPath = "whisper-server-test"
  69. dir, err := ioutil.TempDir("", dbPath)
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. var server WMailServer
  74. shh = whisper.New()
  75. shh.RegisterServer(&server)
  76. server.Init(shh, dir, password, powRequirement)
  77. defer server.Close()
  78. keyID, err = shh.AddSymKeyFromPassword(password)
  79. if err != nil {
  80. t.Fatalf("Failed to create symmetric key for mail request: %s", err)
  81. }
  82. rand.Seed(seed)
  83. env := generateEnvelope(t)
  84. server.Archive(env)
  85. deliverTest(t, &server, env)
  86. }
  87. func deliverTest(t *testing.T, server *WMailServer, env *whisper.Envelope) {
  88. id, err := shh.NewKeyPair()
  89. if err != nil {
  90. t.Fatalf("failed to generate new key pair with seed %d: %s.", seed, err)
  91. }
  92. testPeerID, err := shh.GetPrivateKey(id)
  93. if err != nil {
  94. t.Fatalf("failed to retrieve new key pair with seed %d: %s.", seed, err)
  95. }
  96. birth := env.Expiry - env.TTL
  97. p := &ServerTestParams{
  98. topic: env.Topic,
  99. low: birth - 1,
  100. upp: birth + 1,
  101. key: testPeerID,
  102. }
  103. singleRequest(t, server, env, p, true)
  104. p.low, p.upp = birth+1, 0xffffffff
  105. singleRequest(t, server, env, p, false)
  106. p.low, p.upp = 0, birth-1
  107. singleRequest(t, server, env, p, false)
  108. p.low = birth - 1
  109. p.upp = birth + 1
  110. p.topic[0]++
  111. singleRequest(t, server, env, p, false)
  112. }
  113. func singleRequest(t *testing.T, server *WMailServer, env *whisper.Envelope, p *ServerTestParams, expect bool) {
  114. request := createRequest(t, p)
  115. src := crypto.FromECDSAPub(&p.key.PublicKey)
  116. ok, lower, upper, topic := server.validateRequest(src, request)
  117. if !ok {
  118. t.Fatalf("request validation failed, seed: %d.", seed)
  119. }
  120. if lower != p.low {
  121. t.Fatalf("request validation failed (lower bound), seed: %d.", seed)
  122. }
  123. if upper != p.upp {
  124. t.Fatalf("request validation failed (upper bound), seed: %d.", seed)
  125. }
  126. if topic != p.topic {
  127. t.Fatalf("request validation failed (topic), seed: %d.", seed)
  128. }
  129. var exist bool
  130. mail := server.processRequest(nil, p.low, p.upp, p.topic)
  131. for _, msg := range mail {
  132. if msg.Hash() == env.Hash() {
  133. exist = true
  134. break
  135. }
  136. }
  137. if exist != expect {
  138. t.Fatalf("error: exist = %v, seed: %d.", exist, seed)
  139. }
  140. src[0]++
  141. ok, lower, upper, topic = server.validateRequest(src, request)
  142. if ok {
  143. t.Fatalf("request validation false positive, seed: %d.", seed)
  144. }
  145. }
  146. func createRequest(t *testing.T, p *ServerTestParams) *whisper.Envelope {
  147. data := make([]byte, 8+whisper.TopicLength)
  148. binary.BigEndian.PutUint32(data, p.low)
  149. binary.BigEndian.PutUint32(data[4:], p.upp)
  150. copy(data[8:], p.topic[:])
  151. key, err := shh.GetSymKey(keyID)
  152. if err != nil {
  153. t.Fatalf("failed to retrieve sym key with seed %d: %s.", seed, err)
  154. }
  155. params := &whisper.MessageParams{
  156. KeySym: key,
  157. Topic: p.topic,
  158. Payload: data,
  159. PoW: powRequirement * 2,
  160. WorkTime: 2,
  161. Src: p.key,
  162. }
  163. msg := whisper.NewSentMessage(params)
  164. env, err := msg.Wrap(params)
  165. if err != nil {
  166. t.Fatalf("failed to wrap with seed %d: %s.", seed, err)
  167. }
  168. return env
  169. }