Skip to content

Commit 65ed6a9

Browse files
gluk256fjl
authored andcommitted
whisper: add tests for mailserver (ethereum#3631)
1 parent 564b605 commit 65ed6a9

File tree

3 files changed

+217
-9
lines changed

3 files changed

+217
-9
lines changed

cmd/wnode/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,15 @@ func initialize() {
209209
nodeid = shh.NewIdentity()
210210
}
211211

212+
maxPeers := 80
213+
if *bootstrapMode {
214+
maxPeers = 800
215+
}
216+
212217
server = &p2p.Server{
213218
Config: p2p.Config{
214219
PrivateKey: nodeid,
215-
MaxPeers: 128,
220+
MaxPeers: maxPeers,
216221
Name: common.MakeName("whisper-go", "5.0"),
217222
Protocols: shh.Protocols(),
218223
ListenAddr: *argIP,

whisper/mailserver/mailserver.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/ethereum/go-ethereum/cmd/utils"
2424
"github.com/ethereum/go-ethereum/common"
25+
"github.com/ethereum/go-ethereum/crypto"
2526
"github.com/ethereum/go-ethereum/logger"
2627
"github.com/ethereum/go-ethereum/logger/glog"
2728
"github.com/ethereum/go-ethereum/rlp"
@@ -101,11 +102,19 @@ func (s *WMailServer) Archive(env *whisper.Envelope) {
101102
}
102103

103104
func (s *WMailServer) DeliverMail(peer *whisper.Peer, request *whisper.Envelope) {
104-
ok, lower, upper, topic := s.validate(peer, request)
105-
if !ok {
105+
if peer == nil {
106+
glog.V(logger.Error).Info("Whisper peer is nil")
106107
return
107108
}
108109

110+
ok, lower, upper, topic := s.validateRequest(peer.ID(), request)
111+
if ok {
112+
s.processRequest(peer, lower, upper, topic)
113+
}
114+
}
115+
116+
func (s *WMailServer) processRequest(peer *whisper.Peer, lower, upper uint32, topic whisper.TopicType) []*whisper.Envelope {
117+
ret := make([]*whisper.Envelope, 0)
109118
var err error
110119
var zero common.Hash
111120
var empty whisper.TopicType
@@ -122,10 +131,15 @@ func (s *WMailServer) DeliverMail(peer *whisper.Peer, request *whisper.Envelope)
122131
}
123132

124133
if topic == empty || envelope.Topic == topic {
125-
err = s.w.SendP2PDirect(peer, &envelope)
126-
if err != nil {
127-
glog.V(logger.Error).Infof("Failed to send direct message to peer: %s", err)
128-
return
134+
if peer == nil {
135+
// used for test purposes
136+
ret = append(ret, &envelope)
137+
} else {
138+
err = s.w.SendP2PDirect(peer, &envelope)
139+
if err != nil {
140+
glog.V(logger.Error).Infof("Failed to send direct message to peer: %s", err)
141+
return nil
142+
}
129143
}
130144
}
131145
}
@@ -134,9 +148,11 @@ func (s *WMailServer) DeliverMail(peer *whisper.Peer, request *whisper.Envelope)
134148
if err != nil {
135149
glog.V(logger.Error).Infof("Level DB iterator error: %s", err)
136150
}
151+
152+
return ret
137153
}
138154

139-
func (s *WMailServer) validate(peer *whisper.Peer, request *whisper.Envelope) (bool, uint32, uint32, whisper.TopicType) {
155+
func (s *WMailServer) validateRequest(peerID []byte, request *whisper.Envelope) (bool, uint32, uint32, whisper.TopicType) {
140156
var topic whisper.TopicType
141157
if s.pow > 0.0 && request.PoW() < s.pow {
142158
return false, 0, 0, topic
@@ -154,7 +170,11 @@ func (s *WMailServer) validate(peer *whisper.Peer, request *whisper.Envelope) (b
154170
return false, 0, 0, topic
155171
}
156172

157-
if bytes.Equal(peer.ID(), decrypted.Signature) {
173+
src := crypto.FromECDSAPub(decrypted.Src)
174+
if len(src)-len(peerID) == 1 {
175+
src = src[1:]
176+
}
177+
if !bytes.Equal(peerID, src) {
158178
glog.V(logger.Warn).Infof("Wrong signature of p2p request")
159179
return false, 0, 0, topic
160180
}

whisper/mailserver/server_test.go

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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+
17+
package mailserver
18+
19+
import (
20+
"crypto/ecdsa"
21+
"encoding/binary"
22+
"io/ioutil"
23+
"math/rand"
24+
"testing"
25+
"time"
26+
27+
"github.com/ethereum/go-ethereum/common"
28+
"github.com/ethereum/go-ethereum/crypto"
29+
whisper "github.com/ethereum/go-ethereum/whisper/whisperv5"
30+
)
31+
32+
const powRequirement = 0.00001
33+
const keyName = "6d604bac5401ce9a6b995f1b45a4ab"
34+
35+
var shh *whisper.Whisper
36+
var seed = time.Now().Unix()
37+
38+
type ServerTestParams struct {
39+
topic whisper.TopicType
40+
low uint32
41+
upp uint32
42+
key *ecdsa.PrivateKey
43+
}
44+
45+
func assert(statement bool, text string, t *testing.T) {
46+
if !statement {
47+
t.Fatal(text)
48+
}
49+
}
50+
51+
func TestDBKey(t *testing.T) {
52+
var h common.Hash
53+
i := uint32(time.Now().Unix())
54+
k := NewDbKey(i, h)
55+
assert(len(k.raw) == common.HashLength+4, "wrong DB key length", t)
56+
assert(byte(i%0x100) == k.raw[3], "raw representation should be big endian", t)
57+
assert(byte(i/0x1000000) == k.raw[0], "big endian expected", t)
58+
}
59+
60+
func generateEnvelope(t *testing.T) *whisper.Envelope {
61+
params := &whisper.MessageParams{
62+
KeySym: []byte("test key"),
63+
Topic: whisper.TopicType{},
64+
Payload: []byte("test payload"),
65+
PoW: powRequirement,
66+
WorkTime: 2,
67+
}
68+
69+
msg := whisper.NewSentMessage(params)
70+
env, err := msg.Wrap(params)
71+
if err != nil {
72+
t.Fatalf("failed to wrap with seed %d: %s.", seed, err)
73+
}
74+
return env
75+
}
76+
77+
func TestMailServer(t *testing.T) {
78+
const password = "password_for_this_test"
79+
const dbPath = "whisper-server-test"
80+
81+
_, err := ioutil.TempDir("", dbPath)
82+
if err != nil {
83+
t.Fatal(err)
84+
}
85+
86+
var server WMailServer
87+
shh = whisper.NewWhisper(&server)
88+
server.Init(shh, dbPath, password, powRequirement)
89+
defer server.Close()
90+
91+
err = shh.AddSymKey(keyName, []byte(password))
92+
if err != nil {
93+
t.Fatalf("Failed to create symmetric key for mail request: %s", err)
94+
}
95+
96+
rand.Seed(seed)
97+
env := generateEnvelope(t)
98+
server.Archive(env)
99+
deliverTest(t, &server, env)
100+
}
101+
102+
func deliverTest(t *testing.T, server *WMailServer, env *whisper.Envelope) {
103+
testPeerID := shh.NewIdentity()
104+
birth := env.Expiry - env.TTL
105+
p := &ServerTestParams{
106+
topic: env.Topic,
107+
low: birth - 1,
108+
upp: birth + 1,
109+
key: testPeerID,
110+
}
111+
singleRequest(t, server, env, p, true)
112+
113+
p.low, p.upp = birth+1, 0xffffffff
114+
singleRequest(t, server, env, p, false)
115+
116+
p.low, p.upp = 0, birth-1
117+
singleRequest(t, server, env, p, false)
118+
119+
p.low = birth - 1
120+
p.upp = birth + 1
121+
p.topic[0]++
122+
singleRequest(t, server, env, p, false)
123+
}
124+
125+
func singleRequest(t *testing.T, server *WMailServer, env *whisper.Envelope, p *ServerTestParams, expect bool) {
126+
request := createRequest(t, p)
127+
src := crypto.FromECDSAPub(&p.key.PublicKey)
128+
ok, lower, upper, topic := server.validateRequest(src, request)
129+
if !ok {
130+
t.Fatalf("request validation failed, seed: %d.", seed)
131+
}
132+
if lower != p.low {
133+
t.Fatalf("request validation failed (lower bound), seed: %d.", seed)
134+
}
135+
if upper != p.upp {
136+
t.Fatalf("request validation failed (upper bound), seed: %d.", seed)
137+
}
138+
if topic != p.topic {
139+
t.Fatalf("request validation failed (topic), seed: %d.", seed)
140+
}
141+
142+
var exist bool
143+
mail := server.processRequest(nil, p.low, p.upp, p.topic)
144+
for _, msg := range mail {
145+
if msg.Hash() == env.Hash() {
146+
exist = true
147+
break
148+
}
149+
}
150+
151+
if exist != expect {
152+
t.Fatalf("error: exist = %v, seed: %d.", exist, seed)
153+
}
154+
155+
src[0]++
156+
ok, lower, upper, topic = server.validateRequest(src, request)
157+
if ok {
158+
t.Fatalf("request validation false positive, seed: %d.", seed)
159+
}
160+
}
161+
162+
func createRequest(t *testing.T, p *ServerTestParams) *whisper.Envelope {
163+
data := make([]byte, 8+whisper.TopicLength)
164+
binary.BigEndian.PutUint32(data, p.low)
165+
binary.BigEndian.PutUint32(data[4:], p.upp)
166+
copy(data[8:], p.topic[:])
167+
168+
params := &whisper.MessageParams{
169+
KeySym: shh.GetSymKey(keyName),
170+
Topic: p.topic,
171+
Payload: data,
172+
PoW: powRequirement * 2,
173+
WorkTime: 2,
174+
Src: p.key,
175+
}
176+
177+
msg := whisper.NewSentMessage(params)
178+
env, err := msg.Wrap(params)
179+
if err != nil {
180+
t.Fatalf("failed to wrap with seed %d: %s.", seed, err)
181+
}
182+
return env
183+
}

0 commit comments

Comments
 (0)