2017-04-14 08:29:00 +00:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
2017-02-13 12:15:20 +00:00
|
|
|
//
|
2017-04-14 08:29:00 +00:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
2017-02-13 12:15:20 +00:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2017-04-14 08:29:00 +00:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2017-02-13 12:15:20 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-04-14 08:29:00 +00:00
|
|
|
// GNU Lesser General Public License for more details.
|
2017-02-13 12:15:20 +00:00
|
|
|
//
|
2017-04-14 08:29:00 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2017-02-13 12:15:20 +00:00
|
|
|
|
|
|
|
package mailserver
|
|
|
|
|
|
|
|
import (
|
2018-03-01 15:04:09 +00:00
|
|
|
"bytes"
|
2017-02-13 12:15:20 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"encoding/binary"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2020-08-03 17:40:46 +00:00
|
|
|
"github.com/ethereum/go-ethereum/node"
|
2018-02-10 13:35:32 +00:00
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
|
2017-02-13 12:15:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const powRequirement = 0.00001
|
|
|
|
|
2017-04-09 21:49:22 +00:00
|
|
|
var keyID string
|
2017-02-13 12:15:20 +00:00
|
|
|
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 {
|
2017-04-26 19:05:48 +00:00
|
|
|
h := crypto.Keccak256Hash([]byte("test sample data"))
|
2017-02-13 12:15:20 +00:00
|
|
|
params := &whisper.MessageParams{
|
2017-04-26 19:05:48 +00:00
|
|
|
KeySym: h[:],
|
2018-03-01 15:04:09 +00:00
|
|
|
Topic: whisper.TopicType{0x1F, 0x7E, 0xA1, 0x7F},
|
2017-02-13 12:15:20 +00:00
|
|
|
Payload: []byte("test payload"),
|
|
|
|
PoW: powRequirement,
|
|
|
|
WorkTime: 2,
|
|
|
|
}
|
|
|
|
|
2017-04-26 19:05:48 +00:00
|
|
|
msg, err := whisper.NewSentMessage(params)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
|
|
|
|
}
|
2017-02-13 12:15:20 +00:00
|
|
|
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"
|
|
|
|
|
2017-02-24 08:21:01 +00:00
|
|
|
dir, err := ioutil.TempDir("", dbPath)
|
2017-02-13 12:15:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var server WMailServer
|
2020-08-03 17:40:46 +00:00
|
|
|
|
|
|
|
stack, w := newNode(t)
|
|
|
|
defer stack.Close()
|
|
|
|
shh = w
|
|
|
|
|
2017-02-14 14:44:47 +00:00
|
|
|
shh.RegisterServer(&server)
|
|
|
|
|
2018-05-04 09:10:18 +00:00
|
|
|
err = server.Init(shh, dir, password, powRequirement)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-02-13 12:15:20 +00:00
|
|
|
defer server.Close()
|
|
|
|
|
2017-04-09 21:49:22 +00:00
|
|
|
keyID, err = shh.AddSymKeyFromPassword(password)
|
2017-02-13 12:15:20 +00:00
|
|
|
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) {
|
2017-04-09 21:49:22 +00:00
|
|
|
id, err := shh.NewKeyPair()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to generate new key pair with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
testPeerID, err := shh.GetPrivateKey(id)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve new key pair with seed %d: %s.", seed, err)
|
|
|
|
}
|
2017-02-13 12:15:20 +00:00
|
|
|
birth := env.Expiry - env.TTL
|
|
|
|
p := &ServerTestParams{
|
|
|
|
topic: env.Topic,
|
|
|
|
low: birth - 1,
|
|
|
|
upp: birth + 1,
|
|
|
|
key: testPeerID,
|
|
|
|
}
|
2018-03-01 15:04:09 +00:00
|
|
|
|
2017-02-13 12:15:20 +00:00
|
|
|
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
|
2018-03-01 15:04:09 +00:00
|
|
|
p.topic[0] = 0xFF
|
2017-02-13 12:15:20 +00:00
|
|
|
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)
|
2018-03-01 15:04:09 +00:00
|
|
|
ok, lower, upper, bloom := server.validateRequest(src, request)
|
2017-02-13 12:15:20 +00:00
|
|
|
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)
|
|
|
|
}
|
2018-03-01 15:04:09 +00:00
|
|
|
expectedBloom := whisper.TopicToBloom(p.topic)
|
|
|
|
if !bytes.Equal(bloom, expectedBloom) {
|
2017-02-13 12:15:20 +00:00
|
|
|
t.Fatalf("request validation failed (topic), seed: %d.", seed)
|
|
|
|
}
|
|
|
|
|
|
|
|
var exist bool
|
2018-03-01 15:04:09 +00:00
|
|
|
mail := server.processRequest(nil, p.low, p.upp, bloom)
|
2017-02-13 12:15:20 +00:00
|
|
|
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]++
|
2019-07-22 10:34:41 +00:00
|
|
|
ok, lower, upper, _ = server.validateRequest(src, request)
|
2018-02-27 14:52:10 +00:00
|
|
|
if !ok {
|
|
|
|
// request should be valid regardless of signature
|
2018-02-27 23:39:38 +00:00
|
|
|
t.Fatalf("request validation false negative, seed: %d (lower: %d, upper: %d).", seed, lower, upper)
|
2017-02-13 12:15:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createRequest(t *testing.T, p *ServerTestParams) *whisper.Envelope {
|
2018-03-01 15:04:09 +00:00
|
|
|
bloom := whisper.TopicToBloom(p.topic)
|
|
|
|
data := make([]byte, 8)
|
2017-02-13 12:15:20 +00:00
|
|
|
binary.BigEndian.PutUint32(data, p.low)
|
|
|
|
binary.BigEndian.PutUint32(data[4:], p.upp)
|
2018-03-01 15:04:09 +00:00
|
|
|
data = append(data, bloom...)
|
2017-02-13 12:15:20 +00:00
|
|
|
|
2017-04-09 21:49:22 +00:00
|
|
|
key, err := shh.GetSymKey(keyID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve sym key with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
|
2017-02-13 12:15:20 +00:00
|
|
|
params := &whisper.MessageParams{
|
2017-04-09 21:49:22 +00:00
|
|
|
KeySym: key,
|
2017-02-13 12:15:20 +00:00
|
|
|
Topic: p.topic,
|
|
|
|
Payload: data,
|
|
|
|
PoW: powRequirement * 2,
|
|
|
|
WorkTime: 2,
|
|
|
|
Src: p.key,
|
|
|
|
}
|
|
|
|
|
2017-04-26 19:05:48 +00:00
|
|
|
msg, err := whisper.NewSentMessage(params)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
|
|
|
|
}
|
2017-02-13 12:15:20 +00:00
|
|
|
env, err := msg.Wrap(params)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to wrap with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
return env
|
|
|
|
}
|
2020-08-03 17:40:46 +00:00
|
|
|
|
|
|
|
// newNode creates a new node using a default config and
|
|
|
|
// creates and registers a new Whisper service on it.
|
|
|
|
func newNode(t *testing.T) (*node.Node, *whisper.Whisper) {
|
|
|
|
stack, err := node.New(&node.DefaultConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create new node: %v", err)
|
|
|
|
}
|
|
|
|
w, err := whisper.New(stack, &whisper.DefaultConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create new whisper service: %v", err)
|
|
|
|
}
|
|
|
|
err = stack.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not start node: %v", err)
|
|
|
|
}
|
|
|
|
return stack, w
|
|
|
|
}
|