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