github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/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/atheioschain/go-atheios/common"
    28  	"github.com/atheioschain/go-atheios/crypto"
    29  	whisper "github.com/atheioschain/go-atheios/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  }