github.com/avence12/go-ethereum@v1.5.10-0.20170320123548-1dfd65f6d047/whisper/mailserver/server_test.go (about)

     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  	dir, err := ioutil.TempDir("", dbPath)
    82  	if err != nil {
    83  		t.Fatal(err)
    84  	}
    85  
    86  	var server WMailServer
    87  	shh = whisper.New()
    88  	shh.RegisterServer(&server)
    89  
    90  	server.Init(shh, dir, password, powRequirement)
    91  	defer server.Close()
    92  
    93  	err = shh.AddSymKey(keyName, []byte(password))
    94  	if err != nil {
    95  		t.Fatalf("Failed to create symmetric key for mail request: %s", err)
    96  	}
    97  
    98  	rand.Seed(seed)
    99  	env := generateEnvelope(t)
   100  	server.Archive(env)
   101  	deliverTest(t, &server, env)
   102  }
   103  
   104  func deliverTest(t *testing.T, server *WMailServer, env *whisper.Envelope) {
   105  	testPeerID := shh.NewIdentity()
   106  	birth := env.Expiry - env.TTL
   107  	p := &ServerTestParams{
   108  		topic: env.Topic,
   109  		low:   birth - 1,
   110  		upp:   birth + 1,
   111  		key:   testPeerID,
   112  	}
   113  	singleRequest(t, server, env, p, true)
   114  
   115  	p.low, p.upp = birth+1, 0xffffffff
   116  	singleRequest(t, server, env, p, false)
   117  
   118  	p.low, p.upp = 0, birth-1
   119  	singleRequest(t, server, env, p, false)
   120  
   121  	p.low = birth - 1
   122  	p.upp = birth + 1
   123  	p.topic[0]++
   124  	singleRequest(t, server, env, p, false)
   125  }
   126  
   127  func singleRequest(t *testing.T, server *WMailServer, env *whisper.Envelope, p *ServerTestParams, expect bool) {
   128  	request := createRequest(t, p)
   129  	src := crypto.FromECDSAPub(&p.key.PublicKey)
   130  	ok, lower, upper, topic := server.validateRequest(src, request)
   131  	if !ok {
   132  		t.Fatalf("request validation failed, seed: %d.", seed)
   133  	}
   134  	if lower != p.low {
   135  		t.Fatalf("request validation failed (lower bound), seed: %d.", seed)
   136  	}
   137  	if upper != p.upp {
   138  		t.Fatalf("request validation failed (upper bound), seed: %d.", seed)
   139  	}
   140  	if topic != p.topic {
   141  		t.Fatalf("request validation failed (topic), seed: %d.", seed)
   142  	}
   143  
   144  	var exist bool
   145  	mail := server.processRequest(nil, p.low, p.upp, p.topic)
   146  	for _, msg := range mail {
   147  		if msg.Hash() == env.Hash() {
   148  			exist = true
   149  			break
   150  		}
   151  	}
   152  
   153  	if exist != expect {
   154  		t.Fatalf("error: exist = %v, seed: %d.", exist, seed)
   155  	}
   156  
   157  	src[0]++
   158  	ok, lower, upper, topic = server.validateRequest(src, request)
   159  	if ok {
   160  		t.Fatalf("request validation false positive, seed: %d.", seed)
   161  	}
   162  }
   163  
   164  func createRequest(t *testing.T, p *ServerTestParams) *whisper.Envelope {
   165  	data := make([]byte, 8+whisper.TopicLength)
   166  	binary.BigEndian.PutUint32(data, p.low)
   167  	binary.BigEndian.PutUint32(data[4:], p.upp)
   168  	copy(data[8:], p.topic[:])
   169  
   170  	params := &whisper.MessageParams{
   171  		KeySym:   shh.GetSymKey(keyName),
   172  		Topic:    p.topic,
   173  		Payload:  data,
   174  		PoW:      powRequirement * 2,
   175  		WorkTime: 2,
   176  		Src:      p.key,
   177  	}
   178  
   179  	msg := whisper.NewSentMessage(params)
   180  	env, err := msg.Wrap(params)
   181  	if err != nil {
   182  		t.Fatalf("failed to wrap with seed %d: %s.", seed, err)
   183  	}
   184  	return env
   185  }