2016-10-29 12:11:37 +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 whisperv5
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
2016-12-19 23:58:01 +00:00
|
|
|
"time"
|
2016-10-29 12:11:37 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
)
|
|
|
|
|
2016-12-01 19:09:22 +00:00
|
|
|
func TestWhisperBasic(t *testing.T) {
|
2016-10-29 12:11:37 +00:00
|
|
|
w := NewWhisper(nil)
|
|
|
|
p := w.Protocols()
|
|
|
|
shh := p[0]
|
|
|
|
if shh.Name != ProtocolName {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed Protocol Name: %v.", shh.Name)
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if uint64(shh.Version) != ProtocolVersion {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed Protocol Version: %v.", shh.Version)
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if shh.Length != NumberOfMessageCodes {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed Protocol Length: %v.", shh.Length)
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if shh.Run == nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed shh.Run.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if uint64(w.Version()) != ProtocolVersion {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed whisper Version: %v.", shh.Version)
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if w.GetFilter(0) != nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed GetFilter.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
peerID := make([]byte, 64)
|
|
|
|
randomize(peerID)
|
2017-01-09 10:16:06 +00:00
|
|
|
peer, _ := w.getPeer(peerID)
|
2016-10-29 12:11:37 +00:00
|
|
|
if peer != nil {
|
2017-01-09 10:16:06 +00:00
|
|
|
t.Fatal("found peer for random key.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
2017-01-09 10:16:06 +00:00
|
|
|
if err := w.MarkPeerTrusted(peerID); err == nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed MarkPeerTrusted.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
2017-01-09 10:16:06 +00:00
|
|
|
if err := w.RequestHistoricMessages(peerID, peerID); err == nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed RequestHistoricMessages.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
2017-01-09 10:16:06 +00:00
|
|
|
if err := w.SendP2PMessage(peerID, nil); err == nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed SendP2PMessage.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
exist := w.HasSymKey("non-existing")
|
|
|
|
if exist {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed HasSymKey.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
key := w.GetSymKey("non-existing")
|
|
|
|
if key != nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed GetSymKey.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
mail := w.Envelopes()
|
|
|
|
if len(mail) != 0 {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed w.Envelopes().")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
m := w.Messages(0)
|
|
|
|
if len(m) != 0 {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed w.Messages.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var derived []byte
|
|
|
|
ver := uint64(0xDEADBEEF)
|
2017-01-09 10:16:06 +00:00
|
|
|
if _, err := deriveKeyMaterial(peerID, ver); err != unknownVersionError(ver) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed deriveKeyMaterial with param = %v: %s.", peerID, err)
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
2017-01-09 10:16:06 +00:00
|
|
|
derived, err := deriveKeyMaterial(peerID, 0)
|
2016-10-29 12:11:37 +00:00
|
|
|
if err != nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed second deriveKeyMaterial with param = %v: %s.", peerID, err)
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if !validateSymmetricKey(derived) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed validateSymmetricKey with param = %v.", derived)
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if containsOnlyZeros(derived) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed containsOnlyZeros with param = %v.", derived)
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf := []byte{0xFF, 0xE5, 0x80, 0x2, 0}
|
|
|
|
le := bytesToIntLittleEndian(buf)
|
|
|
|
be := BytesToIntBigEndian(buf)
|
|
|
|
if le != uint64(0x280e5ff) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed bytesToIntLittleEndian: %d.", le)
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if be != uint64(0xffe5800200) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed BytesToIntBigEndian: %d.", be)
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pk := w.NewIdentity()
|
|
|
|
if !validatePrivateKey(pk) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed validatePrivateKey: %v.", pk)
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if !ValidatePublicKey(&pk.PublicKey) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed ValidatePublicKey: %v.", pk)
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 19:09:22 +00:00
|
|
|
func TestWhisperIdentityManagement(t *testing.T) {
|
2016-10-29 12:11:37 +00:00
|
|
|
w := NewWhisper(nil)
|
|
|
|
id1 := w.NewIdentity()
|
|
|
|
id2 := w.NewIdentity()
|
|
|
|
pub1 := common.ToHex(crypto.FromECDSAPub(&id1.PublicKey))
|
|
|
|
pub2 := common.ToHex(crypto.FromECDSAPub(&id2.PublicKey))
|
|
|
|
pk1 := w.GetIdentity(pub1)
|
|
|
|
pk2 := w.GetIdentity(pub2)
|
|
|
|
if !w.HasIdentity(pub1) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed HasIdentity(pub1).")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if !w.HasIdentity(pub2) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed HasIdentity(pub2).")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if pk1 != id1 {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed GetIdentity(pub1).")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if pk2 != id2 {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed GetIdentity(pub2).")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete one identity
|
|
|
|
w.DeleteIdentity(pub1)
|
|
|
|
pk1 = w.GetIdentity(pub1)
|
|
|
|
pk2 = w.GetIdentity(pub2)
|
|
|
|
if w.HasIdentity(pub1) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed DeleteIdentity(pub1): still exist.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if !w.HasIdentity(pub2) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed DeleteIdentity(pub1): pub2 does not exist.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if pk1 != nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed DeleteIdentity(pub1): first key still exist.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if pk2 != id2 {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed DeleteIdentity(pub1): second key does not exist.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete again non-existing identity
|
|
|
|
w.DeleteIdentity(pub1)
|
|
|
|
pk1 = w.GetIdentity(pub1)
|
|
|
|
pk2 = w.GetIdentity(pub2)
|
|
|
|
if w.HasIdentity(pub1) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed delete non-existing identity: exist.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if !w.HasIdentity(pub2) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed delete non-existing identity: pub2 does not exist.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if pk1 != nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed delete non-existing identity: first key exist.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if pk2 != id2 {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed delete non-existing identity: second key does not exist.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete second identity
|
|
|
|
w.DeleteIdentity(pub2)
|
|
|
|
pk1 = w.GetIdentity(pub1)
|
|
|
|
pk2 = w.GetIdentity(pub2)
|
|
|
|
if w.HasIdentity(pub1) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed delete second identity: first identity exist.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if w.HasIdentity(pub2) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed delete second identity: still exist.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if pk1 != nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed delete second identity: first key exist.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if pk2 != nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed delete second identity: second key exist.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 19:09:22 +00:00
|
|
|
func TestWhisperSymKeyManagement(t *testing.T) {
|
2016-10-29 12:11:37 +00:00
|
|
|
InitSingleTest()
|
|
|
|
|
|
|
|
var k1, k2 []byte
|
|
|
|
w := NewWhisper(nil)
|
|
|
|
id1 := string("arbitrary-string-1")
|
|
|
|
id2 := string("arbitrary-string-2")
|
|
|
|
|
|
|
|
err := w.GenerateSymKey(id1)
|
|
|
|
if err != nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed GenerateSymKey with seed %d: %s.", seed, err)
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
k1 = w.GetSymKey(id1)
|
|
|
|
k2 = w.GetSymKey(id2)
|
|
|
|
if !w.HasSymKey(id1) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed HasSymKey(id1).")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if w.HasSymKey(id2) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed HasSymKey(id2).")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if k1 == nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("first key does not exist.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if k2 != nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("second key still exist.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add existing id, nothing should change
|
|
|
|
randomKey := make([]byte, 16)
|
|
|
|
randomize(randomKey)
|
|
|
|
err = w.AddSymKey(id1, randomKey)
|
|
|
|
if err == nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed AddSymKey with seed %d.", seed)
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
k1 = w.GetSymKey(id1)
|
|
|
|
k2 = w.GetSymKey(id2)
|
|
|
|
if !w.HasSymKey(id1) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed w.HasSymKey(id1).")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if w.HasSymKey(id2) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed w.HasSymKey(id2).")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if k1 == nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("first key does not exist.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
2017-01-06 15:44:20 +00:00
|
|
|
if bytes.Equal(k1, randomKey) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("k1 == randomKey.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if k2 != nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("second key already exist.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = w.AddSymKey(id2, randomKey) // add non-existing (yet)
|
|
|
|
if err != nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed AddSymKey(id2) with seed %d: %s.", seed, err)
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
k1 = w.GetSymKey(id1)
|
|
|
|
k2 = w.GetSymKey(id2)
|
|
|
|
if !w.HasSymKey(id1) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("HasSymKey(id1) failed.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if !w.HasSymKey(id2) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("HasSymKey(id2) failed.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if k1 == nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("k1 does not exist.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if k2 == nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("k2 does not exist.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
2017-01-06 15:44:20 +00:00
|
|
|
if bytes.Equal(k1, k2) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("k1 == k2.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
2017-01-06 15:44:20 +00:00
|
|
|
if bytes.Equal(k1, randomKey) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("k1 == randomKey.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if len(k1) != aesKeyLength {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("wrong length of k1.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if len(k2) != aesKeyLength {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("wrong length of k2.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.DeleteSymKey(id1)
|
|
|
|
k1 = w.GetSymKey(id1)
|
|
|
|
k2 = w.GetSymKey(id2)
|
|
|
|
if w.HasSymKey(id1) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed to delete first key: still exist.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if !w.HasSymKey(id2) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed to delete first key: second key does not exist.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if k1 != nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed to delete first key.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if k2 == nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed to delete first key: second key is nil.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.DeleteSymKey(id1)
|
|
|
|
w.DeleteSymKey(id2)
|
|
|
|
k1 = w.GetSymKey(id1)
|
|
|
|
k2 = w.GetSymKey(id2)
|
|
|
|
if w.HasSymKey(id1) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed to delete second key: first key exist.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if w.HasSymKey(id2) {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed to delete second key: still exist.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if k1 != nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed to delete second key: first key is not nil.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
if k2 != nil {
|
2016-12-01 19:09:22 +00:00
|
|
|
t.Fatalf("failed to delete second key: second key is not nil.")
|
2016-10-29 12:11:37 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-19 23:58:01 +00:00
|
|
|
|
|
|
|
func TestExpiry(t *testing.T) {
|
|
|
|
InitSingleTest()
|
|
|
|
|
|
|
|
w := NewWhisper(nil)
|
|
|
|
w.test = true
|
|
|
|
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
|
|
|
|
msg := NewSentMessage(params)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait till received or timeout
|
|
|
|
var received, expired bool
|
|
|
|
for j := 0; j < 20; j++ {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
if len(w.Envelopes()) > 0 {
|
|
|
|
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++ {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
if len(w.Envelopes()) == 0 {
|
|
|
|
expired = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !expired {
|
|
|
|
t.Fatalf("expire failed, seed: %d.", seed)
|
|
|
|
}
|
|
|
|
}
|