2016-10-29 05:11:37 -07:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library 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 Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
package whisperv5
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
2017-03-22 11:48:51 -07:00
|
|
|
mrand "math/rand"
|
2016-10-29 05:11:37 -07:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
)
|
|
|
|
|
|
|
|
var seed int64
|
|
|
|
|
|
|
|
// InitSingleTest should be called in the beginning of every
|
|
|
|
// test, which uses RNG, in order to make the tests
|
|
|
|
// reproduciblity independent of their sequence.
|
|
|
|
func InitSingleTest() {
|
|
|
|
seed = time.Now().Unix()
|
2017-03-22 11:48:51 -07:00
|
|
|
mrand.Seed(seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func InitDebugTest(i int64) {
|
|
|
|
seed = i
|
2017-03-22 11:48:51 -07:00
|
|
|
mrand.Seed(seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type FilterTestCase struct {
|
|
|
|
f *Filter
|
2017-02-23 00:41:47 -08:00
|
|
|
id string
|
2016-10-29 05:11:37 -07:00
|
|
|
alive bool
|
|
|
|
msgCnt int
|
|
|
|
}
|
|
|
|
|
2016-12-01 11:09:22 -08:00
|
|
|
func generateFilter(t *testing.T, symmetric bool) (*Filter, error) {
|
2016-10-29 05:11:37 -07:00
|
|
|
var f Filter
|
|
|
|
f.Messages = make(map[common.Hash]*ReceivedMessage)
|
|
|
|
|
|
|
|
const topicNum = 8
|
|
|
|
f.Topics = make([]TopicType, topicNum)
|
|
|
|
for i := 0; i < topicNum; i++ {
|
2017-03-22 11:48:51 -07:00
|
|
|
mrand.Read(f.Topics[i][:])
|
2016-10-29 05:11:37 -07:00
|
|
|
f.Topics[i][0] = 0x01
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("generateFilter 1 failed with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f.Src = &key.PublicKey
|
|
|
|
|
|
|
|
if symmetric {
|
|
|
|
f.KeySym = make([]byte, 12)
|
2017-03-22 11:48:51 -07:00
|
|
|
mrand.Read(f.KeySym)
|
2016-10-29 05:11:37 -07:00
|
|
|
f.SymKeyHash = crypto.Keccak256Hash(f.KeySym)
|
|
|
|
} else {
|
|
|
|
f.KeyAsym, err = crypto.GenerateKey()
|
|
|
|
if err != nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("generateFilter 2 failed with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AcceptP2P & PoW are not set
|
|
|
|
return &f, nil
|
|
|
|
}
|
|
|
|
|
2016-12-01 11:09:22 -08:00
|
|
|
func generateTestCases(t *testing.T, SizeTestFilters int) []FilterTestCase {
|
2016-10-29 05:11:37 -07:00
|
|
|
cases := make([]FilterTestCase, SizeTestFilters)
|
|
|
|
for i := 0; i < SizeTestFilters; i++ {
|
2016-12-01 11:09:22 -08:00
|
|
|
f, _ := generateFilter(t, true)
|
2016-10-29 05:11:37 -07:00
|
|
|
cases[i].f = f
|
2017-03-22 11:48:51 -07:00
|
|
|
cases[i].alive = (mrand.Int()&int(1) == 0)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
return cases
|
|
|
|
}
|
|
|
|
|
2016-12-01 11:09:22 -08:00
|
|
|
func TestInstallFilters(t *testing.T) {
|
2016-10-29 05:11:37 -07:00
|
|
|
InitSingleTest()
|
|
|
|
|
|
|
|
const SizeTestFilters = 256
|
2017-02-14 06:44:47 -08:00
|
|
|
w := New()
|
2016-10-29 05:11:37 -07:00
|
|
|
filters := NewFilters(w)
|
2016-12-01 11:09:22 -08:00
|
|
|
tst := generateTestCases(t, SizeTestFilters)
|
2016-10-29 05:11:37 -07:00
|
|
|
|
2017-02-23 00:41:47 -08:00
|
|
|
var err error
|
|
|
|
var j string
|
2016-10-29 05:11:37 -07:00
|
|
|
for i := 0; i < SizeTestFilters; i++ {
|
2017-02-23 00:41:47 -08:00
|
|
|
j, err = filters.Install(tst[i].f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("seed %d: failed to install filter: %s", seed, err)
|
|
|
|
}
|
2016-10-29 05:11:37 -07:00
|
|
|
tst[i].id = j
|
2017-02-23 00:41:47 -08:00
|
|
|
if len(j) != 40 {
|
|
|
|
t.Fatalf("seed %d: wrong filter id size [%d]", seed, len(j))
|
|
|
|
}
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
2016-12-01 11:09:22 -08:00
|
|
|
for _, testCase := range tst {
|
|
|
|
if !testCase.alive {
|
|
|
|
filters.Uninstall(testCase.id)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 11:09:22 -08:00
|
|
|
for i, testCase := range tst {
|
|
|
|
fil := filters.Get(testCase.id)
|
2016-10-29 05:11:37 -07:00
|
|
|
exist := (fil != nil)
|
2016-12-01 11:09:22 -08:00
|
|
|
if exist != testCase.alive {
|
|
|
|
t.Fatalf("seed %d: failed alive: %d, %v, %v", seed, i, exist, testCase.alive)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
2016-12-01 11:09:22 -08:00
|
|
|
if exist && fil.PoW != testCase.f.PoW {
|
|
|
|
t.Fatalf("seed %d: failed Get: %d, %v, %v", seed, i, exist, testCase.alive)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 11:09:22 -08:00
|
|
|
func TestComparePubKey(t *testing.T) {
|
2016-10-29 05:11:37 -07:00
|
|
|
InitSingleTest()
|
|
|
|
|
|
|
|
key1, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed to generate first key with seed %d: %s.", seed, err)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
key2, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed to generate second key with seed %d: %s.", seed, err)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
2016-12-19 15:58:01 -08:00
|
|
|
if IsPubKeyEqual(&key1.PublicKey, &key2.PublicKey) {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("public keys are equal, seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// generate key3 == key1
|
2017-03-22 11:48:51 -07:00
|
|
|
mrand.Seed(seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
key3, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed to generate third key with seed %d: %s.", seed, err)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
2016-12-19 15:58:01 -08:00
|
|
|
if IsPubKeyEqual(&key1.PublicKey, &key3.PublicKey) {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("key1 == key3, seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 11:09:22 -08:00
|
|
|
func TestMatchEnvelope(t *testing.T) {
|
2016-10-29 05:11:37 -07:00
|
|
|
InitSingleTest()
|
|
|
|
|
2016-12-01 11:09:22 -08:00
|
|
|
fsym, err := generateFilter(t, true)
|
2016-10-29 05:11:37 -07:00
|
|
|
if err != nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed generateFilter with seed %d: %s.", seed, err)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
2016-12-01 11:09:22 -08:00
|
|
|
fasym, err := generateFilter(t, false)
|
2016-10-29 05:11:37 -07:00
|
|
|
if err != nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed generateFilter() with seed %d: %s.", seed, err)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
params, err := generateMessageParams()
|
|
|
|
if err != nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
params.Topic[0] = 0xFF // ensure mismatch
|
|
|
|
|
|
|
|
// mismatch with pseudo-random data
|
|
|
|
msg := NewSentMessage(params)
|
|
|
|
env, err := msg.Wrap(params)
|
|
|
|
if err != nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
match := fsym.MatchEnvelope(env)
|
|
|
|
if match {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope symmetric with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
match = fasym.MatchEnvelope(env)
|
|
|
|
if match {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope asymmetric with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// encrypt symmetrically
|
2017-03-22 11:48:51 -07:00
|
|
|
i := mrand.Int() % 4
|
2016-10-29 05:11:37 -07:00
|
|
|
fsym.Topics[i] = params.Topic
|
|
|
|
fasym.Topics[i] = params.Topic
|
|
|
|
msg = NewSentMessage(params)
|
|
|
|
env, err = msg.Wrap(params)
|
|
|
|
if err != nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed Wrap() with seed %d: %s.", seed, err)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// symmetric + matching topic: match
|
|
|
|
match = fsym.MatchEnvelope(env)
|
|
|
|
if !match {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope() symmetric with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// asymmetric + matching topic: mismatch
|
|
|
|
match = fasym.MatchEnvelope(env)
|
|
|
|
if match {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope() asymmetric with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// symmetric + matching topic + insufficient PoW: mismatch
|
|
|
|
fsym.PoW = env.PoW() + 1.0
|
|
|
|
match = fsym.MatchEnvelope(env)
|
|
|
|
if match {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope(symmetric + matching topic + insufficient PoW) asymmetric with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// symmetric + matching topic + sufficient PoW: match
|
|
|
|
fsym.PoW = env.PoW() / 2
|
|
|
|
match = fsym.MatchEnvelope(env)
|
|
|
|
if !match {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope(symmetric + matching topic + sufficient PoW) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
2016-12-01 11:09:22 -08:00
|
|
|
// symmetric + topics are nil (wildcard): match
|
2016-10-29 05:11:37 -07:00
|
|
|
prevTopics := fsym.Topics
|
|
|
|
fsym.Topics = nil
|
2016-12-01 11:09:22 -08:00
|
|
|
match = fsym.MatchEnvelope(env)
|
|
|
|
if !match {
|
|
|
|
t.Fatalf("failed MatchEnvelope(symmetric + topics are nil) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
fsym.Topics = prevTopics
|
|
|
|
|
|
|
|
// encrypt asymmetrically
|
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed GenerateKey with seed %d: %s.", seed, err)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
params.KeySym = nil
|
|
|
|
params.Dst = &key.PublicKey
|
|
|
|
msg = NewSentMessage(params)
|
|
|
|
env, err = msg.Wrap(params)
|
|
|
|
if err != nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed Wrap() with seed %d: %s.", seed, err)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// encryption method mismatch
|
|
|
|
match = fsym.MatchEnvelope(env)
|
|
|
|
if match {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope(encryption method mismatch) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// asymmetric + mismatching topic: mismatch
|
|
|
|
match = fasym.MatchEnvelope(env)
|
|
|
|
if !match {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope(asymmetric + mismatching topic) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// asymmetric + matching topic: match
|
|
|
|
fasym.Topics[i] = fasym.Topics[i+1]
|
|
|
|
match = fasym.MatchEnvelope(env)
|
|
|
|
if match {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope(asymmetric + matching topic) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
2016-12-01 11:09:22 -08:00
|
|
|
// asymmetric + filter without topic (wildcard): match
|
2016-10-29 05:11:37 -07:00
|
|
|
fasym.Topics = nil
|
|
|
|
match = fasym.MatchEnvelope(env)
|
|
|
|
if !match {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope(asymmetric + filter without topic) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// asymmetric + insufficient PoW: mismatch
|
|
|
|
fasym.PoW = env.PoW() + 1.0
|
|
|
|
match = fasym.MatchEnvelope(env)
|
|
|
|
if match {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope(asymmetric + insufficient PoW) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// asymmetric + sufficient PoW: match
|
|
|
|
fasym.PoW = env.PoW() / 2
|
|
|
|
match = fasym.MatchEnvelope(env)
|
|
|
|
if !match {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope(asymmetric + sufficient PoW) with seed %d.", seed)
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter without topic + envelope without topic: match
|
|
|
|
env.Topic = TopicType{}
|
|
|
|
match = fasym.MatchEnvelope(env)
|
|
|
|
if !match {
|
|
|
|
t.Fatalf("failed MatchEnvelope(filter without topic + envelope without topic) with seed %d.", seed)
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter with topic + envelope without topic: mismatch
|
|
|
|
fasym.Topics = fsym.Topics
|
|
|
|
match = fasym.MatchEnvelope(env)
|
|
|
|
if match {
|
|
|
|
t.Fatalf("failed MatchEnvelope(filter without topic + envelope without topic) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 11:09:22 -08:00
|
|
|
func TestMatchMessageSym(t *testing.T) {
|
2016-10-29 05:11:37 -07:00
|
|
|
InitSingleTest()
|
|
|
|
|
|
|
|
params, err := generateMessageParams()
|
|
|
|
if err != nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
2016-12-01 11:09:22 -08:00
|
|
|
f, err := generateFilter(t, true)
|
2016-10-29 05:11:37 -07:00
|
|
|
if err != nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed generateFilter with seed %d: %s.", seed, err)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const index = 1
|
|
|
|
params.KeySym = f.KeySym
|
|
|
|
params.Topic = f.Topics[index]
|
|
|
|
|
|
|
|
sentMessage := NewSentMessage(params)
|
|
|
|
env, err := sentMessage.Wrap(params)
|
|
|
|
if err != nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := env.Open(f)
|
|
|
|
if msg == nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed Open with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Src mismatch
|
|
|
|
if f.MatchMessage(msg) {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchMessage(src mismatch) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Src: match
|
|
|
|
*f.Src.X = *params.Src.PublicKey.X
|
|
|
|
*f.Src.Y = *params.Src.PublicKey.Y
|
|
|
|
if !f.MatchMessage(msg) {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope(src match) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// insufficient PoW: mismatch
|
|
|
|
f.PoW = msg.PoW + 1.0
|
|
|
|
if f.MatchMessage(msg) {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope(insufficient PoW) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// sufficient PoW: match
|
|
|
|
f.PoW = msg.PoW / 2
|
|
|
|
if !f.MatchMessage(msg) {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope(sufficient PoW) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// topic mismatch
|
|
|
|
f.Topics[index][0]++
|
|
|
|
if f.MatchMessage(msg) {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope(topic mismatch) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
f.Topics[index][0]--
|
|
|
|
|
|
|
|
// key mismatch
|
|
|
|
f.SymKeyHash[0]++
|
|
|
|
if f.MatchMessage(msg) {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope(key mismatch) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
f.SymKeyHash[0]--
|
|
|
|
|
|
|
|
// Src absent: match
|
|
|
|
f.Src = nil
|
|
|
|
if !f.MatchMessage(msg) {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope(src absent) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
2016-12-01 11:09:22 -08:00
|
|
|
// key hash mismatch
|
2016-10-29 05:11:37 -07:00
|
|
|
h := f.SymKeyHash
|
|
|
|
f.SymKeyHash = common.Hash{}
|
|
|
|
if f.MatchMessage(msg) {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope(key hash mismatch) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
f.SymKeyHash = h
|
|
|
|
if !f.MatchMessage(msg) {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope(key hash match) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// encryption method mismatch
|
|
|
|
f.KeySym = nil
|
|
|
|
f.KeyAsym, err = crypto.GenerateKey()
|
|
|
|
if err != nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed GenerateKey with seed %d: %s.", seed, err)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
if f.MatchMessage(msg) {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope(encryption method mismatch) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 11:09:22 -08:00
|
|
|
func TestMatchMessageAsym(t *testing.T) {
|
2016-10-29 05:11:37 -07:00
|
|
|
InitSingleTest()
|
|
|
|
|
2016-12-01 11:09:22 -08:00
|
|
|
f, err := generateFilter(t, false)
|
2016-10-29 05:11:37 -07:00
|
|
|
if err != nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed generateFilter with seed %d: %s.", seed, err)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
params, err := generateMessageParams()
|
|
|
|
if err != nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const index = 1
|
|
|
|
params.Topic = f.Topics[index]
|
|
|
|
params.Dst = &f.KeyAsym.PublicKey
|
|
|
|
keySymOrig := params.KeySym
|
|
|
|
params.KeySym = nil
|
|
|
|
|
|
|
|
sentMessage := NewSentMessage(params)
|
|
|
|
env, err := sentMessage.Wrap(params)
|
|
|
|
if err != nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := env.Open(f)
|
|
|
|
if msg == nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed to open with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Src mismatch
|
|
|
|
if f.MatchMessage(msg) {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchMessage(src mismatch) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Src: match
|
|
|
|
*f.Src.X = *params.Src.PublicKey.X
|
|
|
|
*f.Src.Y = *params.Src.PublicKey.Y
|
|
|
|
if !f.MatchMessage(msg) {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchMessage(src match) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// insufficient PoW: mismatch
|
|
|
|
f.PoW = msg.PoW + 1.0
|
|
|
|
if f.MatchMessage(msg) {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope(insufficient PoW) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// sufficient PoW: match
|
|
|
|
f.PoW = msg.PoW / 2
|
|
|
|
if !f.MatchMessage(msg) {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope(sufficient PoW) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
2016-12-01 11:09:22 -08:00
|
|
|
// topic mismatch
|
2016-10-29 05:11:37 -07:00
|
|
|
f.Topics[index][0]++
|
2016-12-01 11:09:22 -08:00
|
|
|
if f.MatchMessage(msg) {
|
|
|
|
t.Fatalf("failed MatchEnvelope(topic mismatch) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
f.Topics[index][0]--
|
|
|
|
|
|
|
|
// key mismatch
|
|
|
|
prev := *f.KeyAsym.PublicKey.X
|
|
|
|
zero := *big.NewInt(0)
|
|
|
|
*f.KeyAsym.PublicKey.X = zero
|
|
|
|
if f.MatchMessage(msg) {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope(key mismatch) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
*f.KeyAsym.PublicKey.X = prev
|
|
|
|
|
|
|
|
// Src absent: match
|
|
|
|
f.Src = nil
|
|
|
|
if !f.MatchMessage(msg) {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope(src absent) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// encryption method mismatch
|
|
|
|
f.KeySym = keySymOrig
|
|
|
|
f.KeyAsym = nil
|
|
|
|
if f.MatchMessage(msg) {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed MatchEnvelope(encryption method mismatch) with seed %d.", seed)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cloneFilter(orig *Filter) *Filter {
|
|
|
|
var clone Filter
|
|
|
|
clone.Messages = make(map[common.Hash]*ReceivedMessage)
|
|
|
|
clone.Src = orig.Src
|
|
|
|
clone.KeyAsym = orig.KeyAsym
|
|
|
|
clone.KeySym = orig.KeySym
|
|
|
|
clone.Topics = orig.Topics
|
|
|
|
clone.PoW = orig.PoW
|
|
|
|
clone.AcceptP2P = orig.AcceptP2P
|
|
|
|
clone.SymKeyHash = orig.SymKeyHash
|
|
|
|
return &clone
|
|
|
|
}
|
|
|
|
|
2016-12-01 11:09:22 -08:00
|
|
|
func generateCompatibeEnvelope(t *testing.T, f *Filter) *Envelope {
|
2016-10-29 05:11:37 -07:00
|
|
|
params, err := generateMessageParams()
|
|
|
|
if err != nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
|
2016-10-29 05:11:37 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
params.KeySym = f.KeySym
|
|
|
|
params.Topic = f.Topics[2]
|
|
|
|
sentMessage := NewSentMessage(params)
|
|
|
|
env, err := sentMessage.Wrap(params)
|
|
|
|
if err != nil {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
|
2016-10-29 05:11:37 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
2016-12-01 11:09:22 -08:00
|
|
|
func TestWatchers(t *testing.T) {
|
2016-10-29 05:11:37 -07:00
|
|
|
InitSingleTest()
|
|
|
|
|
|
|
|
const NumFilters = 16
|
|
|
|
const NumMessages = 256
|
2016-12-01 11:09:22 -08:00
|
|
|
var i int
|
|
|
|
var j uint32
|
2016-10-29 05:11:37 -07:00
|
|
|
var e *Envelope
|
2017-02-23 00:41:47 -08:00
|
|
|
var x, firstID string
|
|
|
|
var err error
|
2016-10-29 05:11:37 -07:00
|
|
|
|
2017-02-14 06:44:47 -08:00
|
|
|
w := New()
|
2016-10-29 05:11:37 -07:00
|
|
|
filters := NewFilters(w)
|
2016-12-01 11:09:22 -08:00
|
|
|
tst := generateTestCases(t, NumFilters)
|
2016-10-29 05:11:37 -07:00
|
|
|
for i = 0; i < NumFilters; i++ {
|
|
|
|
tst[i].f.Src = nil
|
2017-02-23 00:41:47 -08:00
|
|
|
x, err = filters.Install(tst[i].f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to install filter with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
tst[i].id = x
|
|
|
|
if len(firstID) == 0 {
|
|
|
|
firstID = x
|
|
|
|
}
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
2017-02-23 00:41:47 -08:00
|
|
|
lastID := x
|
2016-10-29 05:11:37 -07:00
|
|
|
|
|
|
|
var envelopes [NumMessages]*Envelope
|
|
|
|
for i = 0; i < NumMessages; i++ {
|
2017-03-22 11:48:51 -07:00
|
|
|
j = mrand.Uint32() % NumFilters
|
2016-12-01 11:09:22 -08:00
|
|
|
e = generateCompatibeEnvelope(t, tst[j].f)
|
2016-10-29 05:11:37 -07:00
|
|
|
envelopes[i] = e
|
|
|
|
tst[j].msgCnt++
|
|
|
|
}
|
|
|
|
|
|
|
|
for i = 0; i < NumMessages; i++ {
|
2016-12-19 15:58:01 -08:00
|
|
|
filters.NotifyWatchers(envelopes[i], false)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var total int
|
|
|
|
var mail []*ReceivedMessage
|
|
|
|
var count [NumFilters]int
|
|
|
|
|
|
|
|
for i = 0; i < NumFilters; i++ {
|
|
|
|
mail = tst[i].f.Retrieve()
|
|
|
|
count[i] = len(mail)
|
|
|
|
total += len(mail)
|
|
|
|
}
|
|
|
|
|
|
|
|
if total != NumMessages {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed with seed %d: total = %d, want: %d.", seed, total, NumMessages)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for i = 0; i < NumFilters; i++ {
|
|
|
|
mail = tst[i].f.Retrieve()
|
|
|
|
if len(mail) != 0 {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed with seed %d: i = %d.", seed, i)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if tst[i].msgCnt != count[i] {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed with seed %d: count[%d]: get %d, want %d.", seed, i, tst[i].msgCnt, count[i])
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// another round with a cloned filter
|
|
|
|
|
|
|
|
clone := cloneFilter(tst[0].f)
|
2017-02-23 00:41:47 -08:00
|
|
|
filters.Uninstall(lastID)
|
2016-10-29 05:11:37 -07:00
|
|
|
total = 0
|
2017-02-23 00:41:47 -08:00
|
|
|
last := NumFilters - 1
|
2016-10-29 05:11:37 -07:00
|
|
|
tst[last].f = clone
|
|
|
|
filters.Install(clone)
|
|
|
|
for i = 0; i < NumFilters; i++ {
|
|
|
|
tst[i].msgCnt = 0
|
|
|
|
count[i] = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that the first watcher receives at least one message
|
2016-12-01 11:09:22 -08:00
|
|
|
e = generateCompatibeEnvelope(t, tst[0].f)
|
2016-10-29 05:11:37 -07:00
|
|
|
envelopes[0] = e
|
|
|
|
tst[0].msgCnt++
|
|
|
|
for i = 1; i < NumMessages; i++ {
|
2017-03-22 11:48:51 -07:00
|
|
|
j = mrand.Uint32() % NumFilters
|
2016-12-01 11:09:22 -08:00
|
|
|
e = generateCompatibeEnvelope(t, tst[j].f)
|
2016-10-29 05:11:37 -07:00
|
|
|
envelopes[i] = e
|
|
|
|
tst[j].msgCnt++
|
|
|
|
}
|
|
|
|
|
|
|
|
for i = 0; i < NumMessages; i++ {
|
2016-12-19 15:58:01 -08:00
|
|
|
filters.NotifyWatchers(envelopes[i], false)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for i = 0; i < NumFilters; i++ {
|
|
|
|
mail = tst[i].f.Retrieve()
|
|
|
|
count[i] = len(mail)
|
|
|
|
total += len(mail)
|
|
|
|
}
|
|
|
|
|
|
|
|
combined := tst[0].msgCnt + tst[last].msgCnt
|
|
|
|
if total != NumMessages+count[0] {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed with seed %d: total = %d, count[0] = %d.", seed, total, count[0])
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if combined != count[0] {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed with seed %d: combined = %d, count[0] = %d.", seed, combined, count[0])
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if combined != count[last] {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed with seed %d: combined = %d, count[last] = %d.", seed, combined, count[last])
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for i = 1; i < NumFilters-1; i++ {
|
|
|
|
mail = tst[i].f.Retrieve()
|
|
|
|
if len(mail) != 0 {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed with seed %d: i = %d.", seed, i)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if tst[i].msgCnt != count[i] {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed with seed %d: i = %d, get %d, want %d.", seed, i, tst[i].msgCnt, count[i])
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test AcceptP2P
|
|
|
|
|
|
|
|
total = 0
|
2016-12-19 15:58:01 -08:00
|
|
|
filters.NotifyWatchers(envelopes[0], true)
|
2016-10-29 05:11:37 -07:00
|
|
|
|
|
|
|
for i = 0; i < NumFilters; i++ {
|
|
|
|
mail = tst[i].f.Retrieve()
|
|
|
|
total += len(mail)
|
|
|
|
}
|
|
|
|
|
|
|
|
if total != 0 {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed with seed %d: total: got %d, want 0.", seed, total)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
|
2017-02-23 00:41:47 -08:00
|
|
|
f := filters.Get(firstID)
|
2016-12-01 11:09:22 -08:00
|
|
|
if f == nil {
|
|
|
|
t.Fatalf("failed to get the filter with seed %d.", seed)
|
|
|
|
}
|
2016-10-29 05:11:37 -07:00
|
|
|
f.AcceptP2P = true
|
|
|
|
total = 0
|
2016-12-19 15:58:01 -08:00
|
|
|
filters.NotifyWatchers(envelopes[0], true)
|
2016-10-29 05:11:37 -07:00
|
|
|
|
|
|
|
for i = 0; i < NumFilters; i++ {
|
|
|
|
mail = tst[i].f.Retrieve()
|
|
|
|
total += len(mail)
|
|
|
|
}
|
|
|
|
|
|
|
|
if total != 1 {
|
2016-12-01 11:09:22 -08:00
|
|
|
t.Fatalf("failed with seed %d: total: got %d, want 1.", seed, total)
|
2016-10-29 05:11:37 -07:00
|
|
|
}
|
|
|
|
}
|