2017-11-03 20:29:49 +00: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 whisperv6
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/ecdsa"
|
2017-12-08 15:08:56 +00:00
|
|
|
"crypto/sha256"
|
2017-11-03 20:29:49 +00:00
|
|
|
mrand "math/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-12-08 15:08:56 +00:00
|
|
|
"golang.org/x/crypto/pbkdf2"
|
2017-11-03 20:29:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWhisperBasic(t *testing.T) {
|
|
|
|
w := New(&DefaultConfig)
|
|
|
|
p := w.Protocols()
|
|
|
|
shh := p[0]
|
|
|
|
if shh.Name != ProtocolName {
|
|
|
|
t.Fatalf("failed Protocol Name: %v.", shh.Name)
|
|
|
|
}
|
|
|
|
if uint64(shh.Version) != ProtocolVersion {
|
|
|
|
t.Fatalf("failed Protocol Version: %v.", shh.Version)
|
|
|
|
}
|
|
|
|
if shh.Length != NumberOfMessageCodes {
|
|
|
|
t.Fatalf("failed Protocol Length: %v.", shh.Length)
|
|
|
|
}
|
|
|
|
if shh.Run == nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed shh.Run.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if uint64(w.Version()) != ProtocolVersion {
|
|
|
|
t.Fatalf("failed whisper Version: %v.", shh.Version)
|
|
|
|
}
|
|
|
|
if w.GetFilter("non-existent") != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed GetFilter.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
peerID := make([]byte, 64)
|
|
|
|
mrand.Read(peerID)
|
|
|
|
peer, _ := w.getPeer(peerID)
|
|
|
|
if peer != nil {
|
|
|
|
t.Fatal("found peer for random key.")
|
|
|
|
}
|
|
|
|
if err := w.AllowP2PMessagesFromPeer(peerID); err == nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed MarkPeerTrusted.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
exist := w.HasSymKey("non-existing")
|
|
|
|
if exist {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed HasSymKey.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
key, err := w.GetSymKey("non-existing")
|
|
|
|
if err == nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("failed GetSymKey(non-existing): false positive. key=%v", key)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if key != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("failed GetSymKey: false positive. key=%v", key)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
mail := w.Envelopes()
|
|
|
|
if len(mail) != 0 {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("failed w.Envelopes(). length=%d", len(mail))
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 07:30:09 +00:00
|
|
|
derived := pbkdf2.Key(peerID, nil, 65356, aesKeyLength, sha256.New)
|
2018-01-30 08:55:08 +00:00
|
|
|
if !validateDataIntegrity(derived, aesKeyLength) {
|
2017-11-03 20:29:49 +00:00
|
|
|
t.Fatalf("failed validateSymmetricKey with param = %v.", derived)
|
|
|
|
}
|
|
|
|
if containsOnlyZeros(derived) {
|
|
|
|
t.Fatalf("failed containsOnlyZeros with param = %v.", derived)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := []byte{0xFF, 0xE5, 0x80, 0x2, 0}
|
|
|
|
le := bytesToUintLittleEndian(buf)
|
|
|
|
be := BytesToUintBigEndian(buf)
|
|
|
|
if le != uint64(0x280e5ff) {
|
|
|
|
t.Fatalf("failed bytesToIntLittleEndian: %d.", le)
|
|
|
|
}
|
|
|
|
if be != uint64(0xffe5800200) {
|
|
|
|
t.Fatalf("failed BytesToIntBigEndian: %d.", be)
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := w.NewKeyPair()
|
|
|
|
if err != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("failed to generate new key pair: %v.", err)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
pk, err := w.GetPrivateKey(id)
|
|
|
|
if err != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("failed to retrieve new key pair: %v.", err)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if !validatePrivateKey(pk) {
|
|
|
|
t.Fatalf("failed validatePrivateKey: %v.", pk)
|
|
|
|
}
|
|
|
|
if !ValidatePublicKey(&pk.PublicKey) {
|
|
|
|
t.Fatalf("failed ValidatePublicKey: %v.", pk)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWhisperAsymmetricKeyImport(t *testing.T) {
|
|
|
|
var (
|
|
|
|
w = New(&DefaultConfig)
|
|
|
|
privateKeys []*ecdsa.PrivateKey
|
|
|
|
)
|
|
|
|
|
|
|
|
for i := 0; i < 50; i++ {
|
|
|
|
id, err := w.NewKeyPair()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not generate key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pk, err := w.GetPrivateKey(id)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not export private key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
privateKeys = append(privateKeys, pk)
|
|
|
|
|
|
|
|
if !w.DeleteKeyPair(id) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("could not delete private key")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pk := range privateKeys {
|
|
|
|
if _, err := w.AddKeyPair(pk); err != nil {
|
|
|
|
t.Fatalf("could not import private key: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWhisperIdentityManagement(t *testing.T) {
|
|
|
|
w := New(&DefaultConfig)
|
|
|
|
id1, err := w.NewKeyPair()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to generate new key pair: %s.", err)
|
|
|
|
}
|
|
|
|
id2, err := w.NewKeyPair()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to generate new key pair: %s.", err)
|
|
|
|
}
|
|
|
|
pk1, err := w.GetPrivateKey(id1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve the key pair: %s.", err)
|
|
|
|
}
|
|
|
|
pk2, err := w.GetPrivateKey(id2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve the key pair: %s.", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !w.HasKeyPair(id1) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed HasIdentity(pk1).")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if !w.HasKeyPair(id2) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed HasIdentity(pk2).")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if pk1 == nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed GetIdentity(pk1).")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if pk2 == nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed GetIdentity(pk2).")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !validatePrivateKey(pk1) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("pk1 is invalid.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if !validatePrivateKey(pk2) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("pk2 is invalid.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete one identity
|
|
|
|
done := w.DeleteKeyPair(id1)
|
|
|
|
if !done {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed to delete id1.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
pk1, err = w.GetPrivateKey(id1)
|
|
|
|
if err == nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("retrieve the key pair: false positive. key=%v", pk1)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
pk2, err = w.GetPrivateKey(id2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve the key pair: %s.", err)
|
|
|
|
}
|
|
|
|
if w.HasKeyPair(id1) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed DeleteIdentity(pub1): still exist.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if !w.HasKeyPair(id2) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed DeleteIdentity(pub1): pub2 does not exist.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if pk1 != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed DeleteIdentity(pub1): first key still exist.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if pk2 == nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed DeleteIdentity(pub1): second key does not exist.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete again non-existing identity
|
|
|
|
done = w.DeleteKeyPair(id1)
|
|
|
|
if done {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("delete id1: false positive.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
pk1, err = w.GetPrivateKey(id1)
|
|
|
|
if err == nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("retrieve the key pair: false positive. key=%v", pk1)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
pk2, err = w.GetPrivateKey(id2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve the key pair: %s.", err)
|
|
|
|
}
|
|
|
|
if w.HasKeyPair(id1) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed delete non-existing identity: exist.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if !w.HasKeyPair(id2) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed delete non-existing identity: pub2 does not exist.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if pk1 != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("failed delete non-existing identity: first key exist. key=%v", pk1)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if pk2 == nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed delete non-existing identity: second key does not exist.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete second identity
|
|
|
|
done = w.DeleteKeyPair(id2)
|
|
|
|
if !done {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed to delete id2.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
pk1, err = w.GetPrivateKey(id1)
|
|
|
|
if err == nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("retrieve the key pair: false positive. key=%v", pk1)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
pk2, err = w.GetPrivateKey(id2)
|
|
|
|
if err == nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("retrieve the key pair: false positive. key=%v", pk2)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if w.HasKeyPair(id1) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed delete second identity: first identity exist.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if w.HasKeyPair(id2) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed delete second identity: still exist.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if pk1 != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("failed delete second identity: first key exist. key=%v", pk1)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if pk2 != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("failed delete second identity: second key exist. key=%v", pk2)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWhisperSymKeyManagement(t *testing.T) {
|
|
|
|
InitSingleTest()
|
|
|
|
|
2019-11-18 08:49:18 +00:00
|
|
|
var (
|
|
|
|
k1, k2 []byte
|
|
|
|
w = New(&DefaultConfig)
|
|
|
|
id2 = string("arbitrary-string-2")
|
|
|
|
)
|
|
|
|
id1, err := w.GenerateSymKey()
|
2017-11-03 20:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed GenerateSymKey with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
k1, err = w.GetSymKey(id1)
|
|
|
|
if err != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("failed GetSymKey(id1). err=%v", err)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
k2, err = w.GetSymKey(id2)
|
|
|
|
if err == nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("failed GetSymKey(id2): false positive. key=%v", k2)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if !w.HasSymKey(id1) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed HasSymKey(id1).")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if w.HasSymKey(id2) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed HasSymKey(id2): false positive.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if k1 == nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("first key does not exist.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if k2 != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("second key still exist. key=%v", k2)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add existing id, nothing should change
|
|
|
|
randomKey := make([]byte, aesKeyLength)
|
|
|
|
mrand.Read(randomKey)
|
|
|
|
id1, err = w.AddSymKeyDirect(randomKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed AddSymKey with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
k1, err = w.GetSymKey(id1)
|
|
|
|
if err != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("failed w.GetSymKey(id1). err=%v", err)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
k2, err = w.GetSymKey(id2)
|
|
|
|
if err == nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("failed w.GetSymKey(id2): false positive. key=%v", k2)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if !w.HasSymKey(id1) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed w.HasSymKey(id1).")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if w.HasSymKey(id2) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed w.HasSymKey(id2): false positive.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if k1 == nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("first key does not exist.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if !bytes.Equal(k1, randomKey) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("k1 != randomKey.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if k2 != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("second key already exist. key=%v", k2)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
id2, err = w.AddSymKeyDirect(randomKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed AddSymKey(id2) with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
k1, err = w.GetSymKey(id1)
|
|
|
|
if err != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("failed w.GetSymKey(id1). err=%v", err)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
k2, err = w.GetSymKey(id2)
|
|
|
|
if err != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("failed w.GetSymKey(id2). err=%v", err)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if !w.HasSymKey(id1) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("HasSymKey(id1) failed.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if !w.HasSymKey(id2) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("HasSymKey(id2) failed.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if k1 == nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("k1 does not exist.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if k2 == nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("k2 does not exist.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if !bytes.Equal(k1, k2) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("k1 != k2.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if !bytes.Equal(k1, randomKey) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("k1 != randomKey.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if len(k1) != aesKeyLength {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("wrong length of k1. length=%d", len(k1))
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if len(k2) != aesKeyLength {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("wrong length of k2. length=%d", len(k2))
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.DeleteSymKey(id1)
|
|
|
|
k1, err = w.GetSymKey(id1)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("failed w.GetSymKey(id1): false positive.")
|
|
|
|
}
|
|
|
|
if k1 != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("failed GetSymKey(id1): false positive. key=%v", k1)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
k2, err = w.GetSymKey(id2)
|
|
|
|
if err != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("failed w.GetSymKey(id2). err=%v", err)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if w.HasSymKey(id1) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed to delete first key: still exist.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if !w.HasSymKey(id2) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed to delete first key: second key does not exist.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if k2 == nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed to delete first key: second key is nil.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.DeleteSymKey(id1)
|
|
|
|
w.DeleteSymKey(id2)
|
|
|
|
k1, err = w.GetSymKey(id1)
|
|
|
|
if err == nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("failed w.GetSymKey(id1): false positive. key=%v", k1)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
k2, err = w.GetSymKey(id2)
|
|
|
|
if err == nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("failed w.GetSymKey(id2): false positive. key=%v", k2)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if k1 != nil || k2 != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("k1 or k2 is not nil")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if w.HasSymKey(id1) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed to delete second key: first key exist.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if w.HasSymKey(id2) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed to delete second key: still exist.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if k1 != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed to delete second key: first key is not nil.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if k2 != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("failed to delete second key: second key is not nil.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
randomKey = make([]byte, aesKeyLength+1)
|
|
|
|
mrand.Read(randomKey)
|
|
|
|
_, err = w.AddSymKeyDirect(randomKey)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("added the key with wrong size, seed %d.", seed)
|
|
|
|
}
|
|
|
|
|
|
|
|
const password = "arbitrary data here"
|
|
|
|
id1, err = w.AddSymKeyFromPassword(password)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed AddSymKeyFromPassword(id1) with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
id2, err = w.AddSymKeyFromPassword(password)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed AddSymKeyFromPassword(id2) with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
k1, err = w.GetSymKey(id1)
|
|
|
|
if err != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("failed w.GetSymKey(id1). err=%v", err)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
k2, err = w.GetSymKey(id2)
|
|
|
|
if err != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("failed w.GetSymKey(id2). err=%v", err)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if !w.HasSymKey(id1) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("HasSymKey(id1) failed.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if !w.HasSymKey(id2) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("HasSymKey(id2) failed.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
2018-01-30 08:55:08 +00:00
|
|
|
if !validateDataIntegrity(k2, aesKeyLength) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("key validation failed.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if !bytes.Equal(k1, k2) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("k1 != k2.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExpiry(t *testing.T) {
|
|
|
|
InitSingleTest()
|
|
|
|
|
|
|
|
w := New(&DefaultConfig)
|
2017-12-21 14:17:27 +00:00
|
|
|
w.SetMinimumPowTest(0.0000001)
|
|
|
|
defer w.SetMinimumPowTest(DefaultMinimumPoW)
|
2017-11-03 20:29:49 +00:00
|
|
|
w.Start(nil)
|
|
|
|
defer w.Stop()
|
|
|
|
|
|
|
|
params, err := generateMessageParams()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
params.TTL = 1
|
|
|
|
|
2018-08-29 10:56:13 +00:00
|
|
|
messagesCount := 5
|
|
|
|
|
|
|
|
// Send a few messages one after another. Due to low PoW and expiration buckets
|
|
|
|
// with one second resolution, it covers a case when there are multiple items
|
|
|
|
// in a single expiration bucket.
|
|
|
|
for i := 0; i < messagesCount; i++ {
|
|
|
|
msg, err := NewSentMessage(params)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
env, err := msg.Wrap(params)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = w.Send(env)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to send envelope with seed %d: %s.", seed, err)
|
|
|
|
}
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// wait till received or timeout
|
|
|
|
var received, expired bool
|
2020-06-23 08:46:59 +00:00
|
|
|
ticker := time.NewTicker(100 * time.Millisecond)
|
|
|
|
defer ticker.Stop()
|
2017-11-03 20:29:49 +00:00
|
|
|
for j := 0; j < 20; j++ {
|
2020-06-23 08:46:59 +00:00
|
|
|
<-ticker.C
|
2018-08-29 10:56:13 +00:00
|
|
|
if len(w.Envelopes()) == messagesCount {
|
2017-11-03 20:29:49 +00:00
|
|
|
received = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !received {
|
|
|
|
t.Fatalf("did not receive the sent envelope, seed: %d.", seed)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait till expired or timeout
|
|
|
|
for j := 0; j < 20; j++ {
|
2020-06-23 08:46:59 +00:00
|
|
|
<-ticker.C
|
2017-11-03 20:29:49 +00:00
|
|
|
if len(w.Envelopes()) == 0 {
|
|
|
|
expired = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !expired {
|
|
|
|
t.Fatalf("expire failed, seed: %d.", seed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCustomization(t *testing.T) {
|
|
|
|
InitSingleTest()
|
|
|
|
|
|
|
|
w := New(&DefaultConfig)
|
2017-12-21 14:17:27 +00:00
|
|
|
defer w.SetMinimumPowTest(DefaultMinimumPoW)
|
2017-11-03 20:29:49 +00:00
|
|
|
defer w.SetMaxMessageSize(DefaultMaxMessageSize)
|
|
|
|
w.Start(nil)
|
|
|
|
defer w.Stop()
|
|
|
|
|
|
|
|
const smallPoW = 0.00001
|
|
|
|
|
|
|
|
f, err := generateFilter(t, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
params, err := generateMessageParams()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
params.KeySym = f.KeySym
|
|
|
|
params.Topic = BytesToTopic(f.Topics[2])
|
|
|
|
params.PoW = smallPoW
|
|
|
|
params.TTL = 3600 * 24 // one day
|
2018-02-10 13:35:32 +00:00
|
|
|
msg, err := NewSentMessage(params)
|
2017-11-03 20:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
env, err := msg.Wrap(params)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = w.Send(env)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("successfully sent envelope with PoW %.06f, false positive (seed %d).", env.PoW(), seed)
|
|
|
|
}
|
|
|
|
|
2017-12-21 14:17:27 +00:00
|
|
|
w.SetMinimumPowTest(smallPoW / 2)
|
2017-11-03 20:29:49 +00:00
|
|
|
err = w.Send(env)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to send envelope with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
params.TTL++
|
2018-02-10 13:35:32 +00:00
|
|
|
msg, err = NewSentMessage(params)
|
2017-11-03 20:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
env, err = msg.Wrap(params)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
w.SetMaxMessageSize(uint32(env.size() - 1))
|
|
|
|
err = w.Send(env)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("successfully sent oversized envelope (seed %d): false positive.", seed)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.SetMaxMessageSize(DefaultMaxMessageSize)
|
|
|
|
err = w.Send(env)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to send second envelope with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait till received or timeout
|
|
|
|
var received bool
|
2020-06-23 08:46:59 +00:00
|
|
|
ticker := time.NewTicker(100 * time.Millisecond)
|
|
|
|
defer ticker.Stop()
|
2017-11-03 20:29:49 +00:00
|
|
|
for j := 0; j < 20; j++ {
|
2020-06-23 08:46:59 +00:00
|
|
|
<-ticker.C
|
2017-11-03 20:29:49 +00:00
|
|
|
if len(w.Envelopes()) > 1 {
|
|
|
|
received = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !received {
|
|
|
|
t.Fatalf("did not receive the sent envelope, seed: %d.", seed)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check w.messages()
|
2018-02-27 22:38:20 +00:00
|
|
|
_, err = w.Subscribe(f)
|
2017-11-03 20:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed subscribe with seed %d: %s.", seed, err)
|
|
|
|
}
|
2020-06-23 08:46:59 +00:00
|
|
|
<-ticker.C
|
2017-11-03 20:29:49 +00:00
|
|
|
mail := f.Retrieve()
|
|
|
|
if len(mail) > 0 {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("received premature mail. mail=%v", mail)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSymmetricSendCycle(t *testing.T) {
|
|
|
|
InitSingleTest()
|
|
|
|
|
|
|
|
w := New(&DefaultConfig)
|
2017-12-21 14:17:27 +00:00
|
|
|
defer w.SetMinimumPowTest(DefaultMinimumPoW)
|
2017-11-03 20:29:49 +00:00
|
|
|
defer w.SetMaxMessageSize(DefaultMaxMessageSize)
|
|
|
|
w.Start(nil)
|
|
|
|
defer w.Stop()
|
|
|
|
|
|
|
|
filter1, err := generateFilter(t, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
filter1.PoW = DefaultMinimumPoW
|
|
|
|
|
|
|
|
// Copy the first filter since some of its fields
|
|
|
|
// are randomly gnerated.
|
|
|
|
filter2 := &Filter{
|
|
|
|
KeySym: filter1.KeySym,
|
|
|
|
Topics: filter1.Topics,
|
|
|
|
PoW: filter1.PoW,
|
|
|
|
AllowP2P: filter1.AllowP2P,
|
|
|
|
Messages: make(map[common.Hash]*ReceivedMessage),
|
|
|
|
}
|
|
|
|
|
|
|
|
params, err := generateMessageParams()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
filter1.Src = ¶ms.Src.PublicKey
|
|
|
|
filter2.Src = ¶ms.Src.PublicKey
|
|
|
|
|
|
|
|
params.KeySym = filter1.KeySym
|
|
|
|
params.Topic = BytesToTopic(filter1.Topics[2])
|
|
|
|
params.PoW = filter1.PoW
|
|
|
|
params.WorkTime = 10
|
|
|
|
params.TTL = 50
|
2018-02-10 13:35:32 +00:00
|
|
|
msg, err := NewSentMessage(params)
|
2017-11-03 20:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
env, err := msg.Wrap(params)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Subscribe(filter1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed subscribe 1 with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Subscribe(filter2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed subscribe 2 with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = w.Send(env)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed sending envelope with PoW %.06f (seed %d): %s", env.PoW(), seed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait till received or timeout
|
|
|
|
var received bool
|
2020-06-23 08:46:59 +00:00
|
|
|
ticker := time.NewTicker(10 * time.Millisecond)
|
|
|
|
defer ticker.Stop()
|
2017-11-03 20:29:49 +00:00
|
|
|
for j := 0; j < 200; j++ {
|
2020-06-23 08:46:59 +00:00
|
|
|
<-ticker.C
|
2017-11-03 20:29:49 +00:00
|
|
|
if len(w.Envelopes()) > 0 {
|
|
|
|
received = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !received {
|
|
|
|
t.Fatalf("did not receive the sent envelope, seed: %d.", seed)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check w.messages()
|
2020-06-23 08:46:59 +00:00
|
|
|
<-ticker.C
|
2017-11-03 20:29:49 +00:00
|
|
|
mail1 := filter1.Retrieve()
|
|
|
|
mail2 := filter2.Retrieve()
|
|
|
|
if len(mail2) == 0 {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("did not receive any email for filter 2.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
if len(mail1) == 0 {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("did not receive any email for filter 1.")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSymmetricSendWithoutAKey(t *testing.T) {
|
|
|
|
InitSingleTest()
|
|
|
|
|
|
|
|
w := New(&DefaultConfig)
|
2017-12-21 14:17:27 +00:00
|
|
|
defer w.SetMinimumPowTest(DefaultMinimumPoW)
|
2017-11-03 20:29:49 +00:00
|
|
|
defer w.SetMaxMessageSize(DefaultMaxMessageSize)
|
|
|
|
w.Start(nil)
|
|
|
|
defer w.Stop()
|
|
|
|
|
|
|
|
filter, err := generateFilter(t, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
filter.PoW = DefaultMinimumPoW
|
|
|
|
|
|
|
|
params, err := generateMessageParams()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
filter.Src = nil
|
|
|
|
|
|
|
|
params.KeySym = filter.KeySym
|
|
|
|
params.Topic = BytesToTopic(filter.Topics[2])
|
|
|
|
params.PoW = filter.PoW
|
|
|
|
params.WorkTime = 10
|
|
|
|
params.TTL = 50
|
2018-02-10 13:35:32 +00:00
|
|
|
msg, err := NewSentMessage(params)
|
2017-11-03 20:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
env, err := msg.Wrap(params)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Subscribe(filter)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed subscribe 1 with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = w.Send(env)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed sending envelope with PoW %.06f (seed %d): %s", env.PoW(), seed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait till received or timeout
|
|
|
|
var received bool
|
2020-06-23 08:46:59 +00:00
|
|
|
ticker := time.NewTicker(10 * time.Millisecond)
|
|
|
|
defer ticker.Stop()
|
2017-11-03 20:29:49 +00:00
|
|
|
for j := 0; j < 200; j++ {
|
2020-06-23 08:46:59 +00:00
|
|
|
<-ticker.C
|
2017-11-03 20:29:49 +00:00
|
|
|
if len(w.Envelopes()) > 0 {
|
|
|
|
received = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !received {
|
|
|
|
t.Fatalf("did not receive the sent envelope, seed: %d.", seed)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check w.messages()
|
2020-06-23 08:46:59 +00:00
|
|
|
<-ticker.C
|
2017-11-03 20:29:49 +00:00
|
|
|
mail := filter.Retrieve()
|
|
|
|
if len(mail) == 0 {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("did not receive message in spite of not setting a public key")
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSymmetricSendKeyMismatch(t *testing.T) {
|
|
|
|
InitSingleTest()
|
|
|
|
|
|
|
|
w := New(&DefaultConfig)
|
2017-12-21 14:17:27 +00:00
|
|
|
defer w.SetMinimumPowTest(DefaultMinimumPoW)
|
2017-11-03 20:29:49 +00:00
|
|
|
defer w.SetMaxMessageSize(DefaultMaxMessageSize)
|
|
|
|
w.Start(nil)
|
|
|
|
defer w.Stop()
|
|
|
|
|
|
|
|
filter, err := generateFilter(t, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
filter.PoW = DefaultMinimumPoW
|
|
|
|
|
|
|
|
params, err := generateMessageParams()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
params.KeySym = filter.KeySym
|
|
|
|
params.Topic = BytesToTopic(filter.Topics[2])
|
|
|
|
params.PoW = filter.PoW
|
|
|
|
params.WorkTime = 10
|
|
|
|
params.TTL = 50
|
2018-02-10 13:35:32 +00:00
|
|
|
msg, err := NewSentMessage(params)
|
2017-11-03 20:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
env, err := msg.Wrap(params)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Subscribe(filter)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed subscribe 1 with seed %d: %s.", seed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = w.Send(env)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed sending envelope with PoW %.06f (seed %d): %s", env.PoW(), seed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait till received or timeout
|
|
|
|
var received bool
|
2020-06-23 08:46:59 +00:00
|
|
|
ticker := time.NewTicker(10 * time.Millisecond)
|
|
|
|
defer ticker.Stop()
|
2017-11-03 20:29:49 +00:00
|
|
|
for j := 0; j < 200; j++ {
|
2020-06-23 08:46:59 +00:00
|
|
|
<-ticker.C
|
2017-11-03 20:29:49 +00:00
|
|
|
if len(w.Envelopes()) > 0 {
|
|
|
|
received = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !received {
|
|
|
|
t.Fatalf("did not receive the sent envelope, seed: %d.", seed)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check w.messages()
|
2020-06-23 08:46:59 +00:00
|
|
|
<-ticker.C
|
2017-11-03 20:29:49 +00:00
|
|
|
mail := filter.Retrieve()
|
|
|
|
if len(mail) > 0 {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("received a message when keys weren't matching. message=%v", mail)
|
2017-11-03 20:29:49 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-12 11:11:22 +00:00
|
|
|
|
|
|
|
func TestBloom(t *testing.T) {
|
|
|
|
topic := TopicType{0, 0, 255, 6}
|
|
|
|
b := TopicToBloom(topic)
|
2018-03-01 15:04:09 +00:00
|
|
|
x := make([]byte, BloomFilterSize)
|
2018-01-12 11:11:22 +00:00
|
|
|
x[0] = byte(1)
|
|
|
|
x[32] = byte(1)
|
2018-03-01 15:04:09 +00:00
|
|
|
x[BloomFilterSize-1] = byte(128)
|
|
|
|
if !BloomFilterMatch(x, b) || !BloomFilterMatch(b, x) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("bloom filter does not match the mask")
|
2018-01-12 11:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := mrand.Read(b)
|
|
|
|
if err != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("math rand error. err=%v", err)
|
2018-01-12 11:11:22 +00:00
|
|
|
}
|
|
|
|
_, err = mrand.Read(x)
|
|
|
|
if err != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatalf("math rand error. err=%v", err)
|
2018-01-12 11:11:22 +00:00
|
|
|
}
|
2018-03-01 15:04:09 +00:00
|
|
|
if !BloomFilterMatch(b, b) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("bloom filter does not match self")
|
2018-01-12 11:11:22 +00:00
|
|
|
}
|
|
|
|
x = addBloom(x, b)
|
2018-03-01 15:04:09 +00:00
|
|
|
if !BloomFilterMatch(x, b) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("bloom filter does not match combined bloom")
|
2018-01-12 11:11:22 +00:00
|
|
|
}
|
|
|
|
if !isFullNode(nil) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("isFullNode did not recognize nil as full node")
|
2018-01-12 11:11:22 +00:00
|
|
|
}
|
|
|
|
x[17] = 254
|
|
|
|
if isFullNode(x) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("isFullNode false positive")
|
2018-01-12 11:11:22 +00:00
|
|
|
}
|
2018-03-01 15:04:09 +00:00
|
|
|
for i := 0; i < BloomFilterSize; i++ {
|
2018-01-12 11:11:22 +00:00
|
|
|
b[i] = byte(255)
|
|
|
|
}
|
|
|
|
if !isFullNode(b) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("isFullNode false negative")
|
2018-01-12 11:11:22 +00:00
|
|
|
}
|
2018-03-01 15:04:09 +00:00
|
|
|
if BloomFilterMatch(x, b) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("bloomFilterMatch false positive")
|
2018-01-12 11:11:22 +00:00
|
|
|
}
|
2018-03-01 15:04:09 +00:00
|
|
|
if !BloomFilterMatch(b, x) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("bloomFilterMatch false negative")
|
2018-01-12 11:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w := New(&DefaultConfig)
|
|
|
|
f := w.BloomFilter()
|
|
|
|
if f != nil {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("wrong bloom on creation")
|
2018-01-12 11:11:22 +00:00
|
|
|
}
|
|
|
|
err = w.SetBloomFilter(x)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to set bloom filter: %s", err)
|
|
|
|
}
|
|
|
|
f = w.BloomFilter()
|
2018-03-01 15:04:09 +00:00
|
|
|
if !BloomFilterMatch(f, x) || !BloomFilterMatch(x, f) {
|
2020-07-21 08:53:06 +00:00
|
|
|
t.Fatal("retireved wrong bloom filter")
|
2018-01-12 11:11:22 +00:00
|
|
|
}
|
|
|
|
}
|