server_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU 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. // go-ethereum 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 General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. 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. const keyName = "6d604bac5401ce9a6b995f1b45a4ab"
  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. _, 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, dbPath, password, powRequirement)
  77. defer server.Close()
  78. err = shh.AddSymKey(keyName, []byte(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. testPeerID := shh.NewIdentity()
  89. birth := env.Expiry - env.TTL
  90. p := &ServerTestParams{
  91. topic: env.Topic,
  92. low: birth - 1,
  93. upp: birth + 1,
  94. key: testPeerID,
  95. }
  96. singleRequest(t, server, env, p, true)
  97. p.low, p.upp = birth+1, 0xffffffff
  98. singleRequest(t, server, env, p, false)
  99. p.low, p.upp = 0, birth-1
  100. singleRequest(t, server, env, p, false)
  101. p.low = birth - 1
  102. p.upp = birth + 1
  103. p.topic[0]++
  104. singleRequest(t, server, env, p, false)
  105. }
  106. func singleRequest(t *testing.T, server *WMailServer, env *whisper.Envelope, p *ServerTestParams, expect bool) {
  107. request := createRequest(t, p)
  108. src := crypto.FromECDSAPub(&p.key.PublicKey)
  109. ok, lower, upper, topic := server.validateRequest(src, request)
  110. if !ok {
  111. t.Fatalf("request validation failed, seed: %d.", seed)
  112. }
  113. if lower != p.low {
  114. t.Fatalf("request validation failed (lower bound), seed: %d.", seed)
  115. }
  116. if upper != p.upp {
  117. t.Fatalf("request validation failed (upper bound), seed: %d.", seed)
  118. }
  119. if topic != p.topic {
  120. t.Fatalf("request validation failed (topic), seed: %d.", seed)
  121. }
  122. var exist bool
  123. mail := server.processRequest(nil, p.low, p.upp, p.topic)
  124. for _, msg := range mail {
  125. if msg.Hash() == env.Hash() {
  126. exist = true
  127. break
  128. }
  129. }
  130. if exist != expect {
  131. t.Fatalf("error: exist = %v, seed: %d.", exist, seed)
  132. }
  133. src[0]++
  134. ok, lower, upper, topic = server.validateRequest(src, request)
  135. if ok {
  136. t.Fatalf("request validation false positive, seed: %d.", seed)
  137. }
  138. }
  139. func createRequest(t *testing.T, p *ServerTestParams) *whisper.Envelope {
  140. data := make([]byte, 8+whisper.TopicLength)
  141. binary.BigEndian.PutUint32(data, p.low)
  142. binary.BigEndian.PutUint32(data[4:], p.upp)
  143. copy(data[8:], p.topic[:])
  144. params := &whisper.MessageParams{
  145. KeySym: shh.GetSymKey(keyName),
  146. Topic: p.topic,
  147. Payload: data,
  148. PoW: powRequirement * 2,
  149. WorkTime: 2,
  150. Src: p.key,
  151. }
  152. msg := whisper.NewSentMessage(params)
  153. env, err := msg.Wrap(params)
  154. if err != nil {
  155. t.Fatalf("failed to wrap with seed %d: %s.", seed, err)
  156. }
  157. return env
  158. }