diff options
author | gluk256 <gluk256@users.noreply.github.com> | 2017-02-13 20:15:20 +0800 |
---|---|---|
committer | Felix Lange <fjl@users.noreply.github.com> | 2017-02-13 20:15:20 +0800 |
commit | 65ed6a9def4d23bbe6109ae4b841a56510d0c476 (patch) | |
tree | 4012f9fe986db7deb3d9c8dbf5b0ba5652f6e32c /whisper | |
parent | 564b60520c68a1f06171abd705c01946b932492f (diff) | |
download | dexon-65ed6a9def4d23bbe6109ae4b841a56510d0c476.tar.gz dexon-65ed6a9def4d23bbe6109ae4b841a56510d0c476.tar.zst dexon-65ed6a9def4d23bbe6109ae4b841a56510d0c476.zip |
whisper: add tests for mailserver (#3631)
Diffstat (limited to 'whisper')
-rw-r--r-- | whisper/mailserver/mailserver.go | 36 | ||||
-rw-r--r-- | whisper/mailserver/server_test.go | 183 |
2 files changed, 211 insertions, 8 deletions
diff --git a/whisper/mailserver/mailserver.go b/whisper/mailserver/mailserver.go index f7d6c3e5c..3e08a3b7e 100644 --- a/whisper/mailserver/mailserver.go +++ b/whisper/mailserver/mailserver.go @@ -22,6 +22,7 @@ import ( "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/rlp" @@ -101,11 +102,19 @@ func (s *WMailServer) Archive(env *whisper.Envelope) { } func (s *WMailServer) DeliverMail(peer *whisper.Peer, request *whisper.Envelope) { - ok, lower, upper, topic := s.validate(peer, request) - if !ok { + if peer == nil { + glog.V(logger.Error).Info("Whisper peer is nil") return } + ok, lower, upper, topic := s.validateRequest(peer.ID(), request) + if ok { + s.processRequest(peer, lower, upper, topic) + } +} + +func (s *WMailServer) processRequest(peer *whisper.Peer, lower, upper uint32, topic whisper.TopicType) []*whisper.Envelope { + ret := make([]*whisper.Envelope, 0) var err error var zero common.Hash var empty whisper.TopicType @@ -122,10 +131,15 @@ func (s *WMailServer) DeliverMail(peer *whisper.Peer, request *whisper.Envelope) } if topic == empty || envelope.Topic == topic { - err = s.w.SendP2PDirect(peer, &envelope) - if err != nil { - glog.V(logger.Error).Infof("Failed to send direct message to peer: %s", err) - return + if peer == nil { + // used for test purposes + ret = append(ret, &envelope) + } else { + err = s.w.SendP2PDirect(peer, &envelope) + if err != nil { + glog.V(logger.Error).Infof("Failed to send direct message to peer: %s", err) + return nil + } } } } @@ -134,9 +148,11 @@ func (s *WMailServer) DeliverMail(peer *whisper.Peer, request *whisper.Envelope) if err != nil { glog.V(logger.Error).Infof("Level DB iterator error: %s", err) } + + return ret } -func (s *WMailServer) validate(peer *whisper.Peer, request *whisper.Envelope) (bool, uint32, uint32, whisper.TopicType) { +func (s *WMailServer) validateRequest(peerID []byte, request *whisper.Envelope) (bool, uint32, uint32, whisper.TopicType) { var topic whisper.TopicType if s.pow > 0.0 && request.PoW() < s.pow { return false, 0, 0, topic @@ -154,7 +170,11 @@ func (s *WMailServer) validate(peer *whisper.Peer, request *whisper.Envelope) (b return false, 0, 0, topic } - if bytes.Equal(peer.ID(), decrypted.Signature) { + src := crypto.FromECDSAPub(decrypted.Src) + if len(src)-len(peerID) == 1 { + src = src[1:] + } + if !bytes.Equal(peerID, src) { glog.V(logger.Warn).Infof("Wrong signature of p2p request") return false, 0, 0, topic } diff --git a/whisper/mailserver/server_test.go b/whisper/mailserver/server_test.go new file mode 100644 index 000000000..24abf6c1a --- /dev/null +++ b/whisper/mailserver/server_test.go @@ -0,0 +1,183 @@ +// Copyright 2016 The go-ethereum Authors +// This file is part of go-ethereum. +// +// go-ethereum is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// go-ethereum is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. + +package mailserver + +import ( + "crypto/ecdsa" + "encoding/binary" + "io/ioutil" + "math/rand" + "testing" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" + whisper "github.com/ethereum/go-ethereum/whisper/whisperv5" +) + +const powRequirement = 0.00001 +const keyName = "6d604bac5401ce9a6b995f1b45a4ab" + +var shh *whisper.Whisper +var seed = time.Now().Unix() + +type ServerTestParams struct { + topic whisper.TopicType + low uint32 + upp uint32 + key *ecdsa.PrivateKey +} + +func assert(statement bool, text string, t *testing.T) { + if !statement { + t.Fatal(text) + } +} + +func TestDBKey(t *testing.T) { + var h common.Hash + i := uint32(time.Now().Unix()) + k := NewDbKey(i, h) + assert(len(k.raw) == common.HashLength+4, "wrong DB key length", t) + assert(byte(i%0x100) == k.raw[3], "raw representation should be big endian", t) + assert(byte(i/0x1000000) == k.raw[0], "big endian expected", t) +} + +func generateEnvelope(t *testing.T) *whisper.Envelope { + params := &whisper.MessageParams{ + KeySym: []byte("test key"), + Topic: whisper.TopicType{}, + Payload: []byte("test payload"), + PoW: powRequirement, + WorkTime: 2, + } + + msg := whisper.NewSentMessage(params) + env, err := msg.Wrap(params) + if err != nil { + t.Fatalf("failed to wrap with seed %d: %s.", seed, err) + } + return env +} + +func TestMailServer(t *testing.T) { + const password = "password_for_this_test" + const dbPath = "whisper-server-test" + + _, err := ioutil.TempDir("", dbPath) + if err != nil { + t.Fatal(err) + } + + var server WMailServer + shh = whisper.NewWhisper(&server) + server.Init(shh, dbPath, password, powRequirement) + defer server.Close() + + err = shh.AddSymKey(keyName, []byte(password)) + if err != nil { + t.Fatalf("Failed to create symmetric key for mail request: %s", err) + } + + rand.Seed(seed) + env := generateEnvelope(t) + server.Archive(env) + deliverTest(t, &server, env) +} + +func deliverTest(t *testing.T, server *WMailServer, env *whisper.Envelope) { + testPeerID := shh.NewIdentity() + birth := env.Expiry - env.TTL + p := &ServerTestParams{ + topic: env.Topic, + low: birth - 1, + upp: birth + 1, + key: testPeerID, + } + singleRequest(t, server, env, p, true) + + p.low, p.upp = birth+1, 0xffffffff + singleRequest(t, server, env, p, false) + + p.low, p.upp = 0, birth-1 + singleRequest(t, server, env, p, false) + + p.low = birth - 1 + p.upp = birth + 1 + p.topic[0]++ + singleRequest(t, server, env, p, false) +} + +func singleRequest(t *testing.T, server *WMailServer, env *whisper.Envelope, p *ServerTestParams, expect bool) { + request := createRequest(t, p) + src := crypto.FromECDSAPub(&p.key.PublicKey) + ok, lower, upper, topic := server.validateRequest(src, request) + if !ok { + t.Fatalf("request validation failed, seed: %d.", seed) + } + if lower != p.low { + t.Fatalf("request validation failed (lower bound), seed: %d.", seed) + } + if upper != p.upp { + t.Fatalf("request validation failed (upper bound), seed: %d.", seed) + } + if topic != p.topic { + t.Fatalf("request validation failed (topic), seed: %d.", seed) + } + + var exist bool + mail := server.processRequest(nil, p.low, p.upp, p.topic) + for _, msg := range mail { + if msg.Hash() == env.Hash() { + exist = true + break + } + } + + if exist != expect { + t.Fatalf("error: exist = %v, seed: %d.", exist, seed) + } + + src[0]++ + ok, lower, upper, topic = server.validateRequest(src, request) + if ok { + t.Fatalf("request validation false positive, seed: %d.", seed) + } +} + +func createRequest(t *testing.T, p *ServerTestParams) *whisper.Envelope { + data := make([]byte, 8+whisper.TopicLength) + binary.BigEndian.PutUint32(data, p.low) + binary.BigEndian.PutUint32(data[4:], p.upp) + copy(data[8:], p.topic[:]) + + params := &whisper.MessageParams{ + KeySym: shh.GetSymKey(keyName), + Topic: p.topic, + Payload: data, + PoW: powRequirement * 2, + WorkTime: 2, + Src: p.key, + } + + msg := whisper.NewSentMessage(params) + env, err := msg.Wrap(params) + if err != nil { + t.Fatalf("failed to wrap with seed %d: %s.", seed, err) + } + return env +} |