server_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. h := crypto.Keccak256Hash([]byte("test sample data"))
  53. params := &whisper.MessageParams{
  54. KeySym: h[:],
  55. Topic: whisper.TopicType{},
  56. Payload: []byte("test payload"),
  57. PoW: powRequirement,
  58. WorkTime: 2,
  59. }
  60. msg, err := whisper.NewSentMessage(params)
  61. if err != nil {
  62. t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
  63. }
  64. env, err := msg.Wrap(params)
  65. if err != nil {
  66. t.Fatalf("failed to wrap with seed %d: %s.", seed, err)
  67. }
  68. return env
  69. }
  70. func TestMailServer(t *testing.T) {
  71. const password = "password_for_this_test"
  72. const dbPath = "whisper-server-test"
  73. dir, err := ioutil.TempDir("", dbPath)
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. var server WMailServer
  78. shh = whisper.New(&whisper.DefaultConfig)
  79. shh.RegisterServer(&server)
  80. server.Init(shh, dir, password, powRequirement)
  81. defer server.Close()
  82. keyID, err = shh.AddSymKeyFromPassword(password)
  83. if err != nil {
  84. t.Fatalf("Failed to create symmetric key for mail request: %s", err)
  85. }
  86. rand.Seed(seed)
  87. env := generateEnvelope(t)
  88. server.Archive(env)
  89. deliverTest(t, &server, env)
  90. }
  91. func deliverTest(t *testing.T, server *WMailServer, env *whisper.Envelope) {
  92. id, err := shh.NewKeyPair()
  93. if err != nil {
  94. t.Fatalf("failed to generate new key pair with seed %d: %s.", seed, err)
  95. }
  96. testPeerID, err := shh.GetPrivateKey(id)
  97. if err != nil {
  98. t.Fatalf("failed to retrieve new key pair with seed %d: %s.", seed, err)
  99. }
  100. birth := env.Expiry - env.TTL
  101. p := &ServerTestParams{
  102. topic: env.Topic,
  103. low: birth - 1,
  104. upp: birth + 1,
  105. key: testPeerID,
  106. }
  107. singleRequest(t, server, env, p, true)
  108. p.low, p.upp = birth+1, 0xffffffff
  109. singleRequest(t, server, env, p, false)
  110. p.low, p.upp = 0, birth-1
  111. singleRequest(t, server, env, p, false)
  112. p.low = birth - 1
  113. p.upp = birth + 1
  114. p.topic[0]++
  115. singleRequest(t, server, env, p, false)
  116. }
  117. func singleRequest(t *testing.T, server *WMailServer, env *whisper.Envelope, p *ServerTestParams, expect bool) {
  118. request := createRequest(t, p)
  119. src := crypto.FromECDSAPub(&p.key.PublicKey)
  120. ok, lower, upper, topic := server.validateRequest(src, request)
  121. if !ok {
  122. t.Fatalf("request validation failed, seed: %d.", seed)
  123. }
  124. if lower != p.low {
  125. t.Fatalf("request validation failed (lower bound), seed: %d.", seed)
  126. }
  127. if upper != p.upp {
  128. t.Fatalf("request validation failed (upper bound), seed: %d.", seed)
  129. }
  130. if topic != p.topic {
  131. t.Fatalf("request validation failed (topic), seed: %d.", seed)
  132. }
  133. var exist bool
  134. mail := server.processRequest(nil, p.low, p.upp, p.topic)
  135. for _, msg := range mail {
  136. if msg.Hash() == env.Hash() {
  137. exist = true
  138. break
  139. }
  140. }
  141. if exist != expect {
  142. t.Fatalf("error: exist = %v, seed: %d.", exist, seed)
  143. }
  144. src[0]++
  145. ok, lower, upper, topic = server.validateRequest(src, request)
  146. if ok {
  147. t.Fatalf("request validation false positive, seed: %d.", seed)
  148. }
  149. }
  150. func createRequest(t *testing.T, p *ServerTestParams) *whisper.Envelope {
  151. data := make([]byte, 8+whisper.TopicLength)
  152. binary.BigEndian.PutUint32(data, p.low)
  153. binary.BigEndian.PutUint32(data[4:], p.upp)
  154. copy(data[8:], p.topic[:])
  155. key, err := shh.GetSymKey(keyID)
  156. if err != nil {
  157. t.Fatalf("failed to retrieve sym key with seed %d: %s.", seed, err)
  158. }
  159. params := &whisper.MessageParams{
  160. KeySym: key,
  161. Topic: p.topic,
  162. Payload: data,
  163. PoW: powRequirement * 2,
  164. WorkTime: 2,
  165. Src: p.key,
  166. }
  167. msg, err := whisper.NewSentMessage(params)
  168. if err != nil {
  169. t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
  170. }
  171. env, err := msg.Wrap(params)
  172. if err != nil {
  173. t.Fatalf("failed to wrap with seed %d: %s.", seed, err)
  174. }
  175. return env
  176. }