whisper: add tests for mailserver (#3631)

This commit is contained in:
gluk256 2017-02-13 13:15:20 +01:00 committed by Felix Lange
parent 564b60520c
commit 65ed6a9def
3 changed files with 217 additions and 9 deletions

View File

@ -209,10 +209,15 @@ func initialize() {
nodeid = shh.NewIdentity()
}
maxPeers := 80
if *bootstrapMode {
maxPeers = 800
}
server = &p2p.Server{
Config: p2p.Config{
PrivateKey: nodeid,
MaxPeers: 128,
MaxPeers: maxPeers,
Name: common.MakeName("whisper-go", "5.0"),
Protocols: shh.Protocols(),
ListenAddr: *argIP,

View File

@ -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
}

View File

@ -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
}