2015-07-07 03:54:22 +03:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 19:48:40 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 03:54:22 +03:00
|
|
|
//
|
2015-07-23 19:35:11 +03:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 03:54:22 +03:00
|
|
|
// 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.
|
|
|
|
//
|
2015-07-22 19:48:40 +03:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 03:54:22 +03:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 19:48:40 +03:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 03:54:22 +03:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 19:48:40 +03:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 03:54:22 +03:00
|
|
|
|
2015-04-14 18:21:56 +03:00
|
|
|
package whisper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2015-04-15 12:50:10 +03:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
2015-04-14 18:21:56 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type testPeer struct {
|
|
|
|
client *Whisper
|
|
|
|
stream *p2p.MsgPipeRW
|
|
|
|
termed chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startTestPeer() *testPeer {
|
|
|
|
// Create a simulated P2P remote peer and data streams to it
|
2015-04-15 12:50:10 +03:00
|
|
|
remote := p2p.NewPeer(discover.NodeID{}, "", nil)
|
2015-04-14 18:21:56 +03:00
|
|
|
tester, tested := p2p.MsgPipe()
|
|
|
|
|
|
|
|
// Create a whisper client and connect with it to the tester peer
|
|
|
|
client := New()
|
2015-11-17 18:33:25 +02:00
|
|
|
client.Start(nil)
|
2015-04-14 18:21:56 +03:00
|
|
|
|
|
|
|
termed := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer client.Stop()
|
|
|
|
defer close(termed)
|
|
|
|
defer tested.Close()
|
|
|
|
|
|
|
|
client.handlePeer(remote, tested)
|
|
|
|
}()
|
2015-04-15 12:50:10 +03:00
|
|
|
|
2015-04-14 18:21:56 +03:00
|
|
|
return &testPeer{
|
|
|
|
client: client,
|
|
|
|
stream: tester,
|
|
|
|
termed: termed,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 10:50:31 +03:00
|
|
|
func startTestPeerInited() (*testPeer, error) {
|
|
|
|
peer := startTestPeer()
|
|
|
|
|
|
|
|
if err := p2p.ExpectMsg(peer.stream, statusCode, []uint64{protocolVersion}); err != nil {
|
|
|
|
peer.stream.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := p2p.SendItems(peer.stream, statusCode, protocolVersion); err != nil {
|
|
|
|
peer.stream.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return peer, nil
|
|
|
|
}
|
|
|
|
|
2015-04-14 18:21:56 +03:00
|
|
|
func TestPeerStatusMessage(t *testing.T) {
|
|
|
|
tester := startTestPeer()
|
|
|
|
|
|
|
|
// Wait for the handshake status message and check it
|
|
|
|
if err := p2p.ExpectMsg(tester.stream, statusCode, []uint64{protocolVersion}); err != nil {
|
|
|
|
t.Fatalf("status message mismatch: %v", err)
|
|
|
|
}
|
|
|
|
// Terminate the node
|
|
|
|
tester.stream.Close()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-tester.termed:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("local close timed out")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerHandshakeFail(t *testing.T) {
|
|
|
|
tester := startTestPeer()
|
|
|
|
|
|
|
|
// Wait for and check the handshake
|
|
|
|
if err := p2p.ExpectMsg(tester.stream, statusCode, []uint64{protocolVersion}); err != nil {
|
|
|
|
t.Fatalf("status message mismatch: %v", err)
|
|
|
|
}
|
|
|
|
// Send an invalid handshake status and verify disconnect
|
|
|
|
if err := p2p.SendItems(tester.stream, messagesCode); err != nil {
|
|
|
|
t.Fatalf("failed to send malformed status: %v", err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-tester.termed:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("remote close timed out")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerHandshakeSuccess(t *testing.T) {
|
|
|
|
tester := startTestPeer()
|
|
|
|
|
|
|
|
// Wait for and check the handshake
|
|
|
|
if err := p2p.ExpectMsg(tester.stream, statusCode, []uint64{protocolVersion}); err != nil {
|
|
|
|
t.Fatalf("status message mismatch: %v", err)
|
|
|
|
}
|
|
|
|
// Send a valid handshake status and make sure connection stays live
|
|
|
|
if err := p2p.SendItems(tester.stream, statusCode, protocolVersion); err != nil {
|
|
|
|
t.Fatalf("failed to send status: %v", err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-tester.termed:
|
|
|
|
t.Fatalf("valid handshake disconnected")
|
|
|
|
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
}
|
|
|
|
// Clean up the test
|
|
|
|
tester.stream.Close()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-tester.termed:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("local close timed out")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerSend(t *testing.T) {
|
|
|
|
// Start a tester and execute the handshake
|
2015-04-15 10:50:31 +03:00
|
|
|
tester, err := startTestPeerInited()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to start initialized peer: %v", err)
|
|
|
|
}
|
2015-04-14 18:21:56 +03:00
|
|
|
defer tester.stream.Close()
|
|
|
|
|
|
|
|
// Construct a message and inject into the tester
|
|
|
|
message := NewMessage([]byte("peer broadcast test message"))
|
|
|
|
envelope, err := message.Wrap(DefaultPoW, Options{
|
|
|
|
TTL: DefaultTTL,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to wrap message: %v", err)
|
|
|
|
}
|
|
|
|
if err := tester.client.Send(envelope); err != nil {
|
|
|
|
t.Fatalf("failed to send message: %v", err)
|
|
|
|
}
|
|
|
|
// Check that the message is eventually forwarded
|
|
|
|
payload := []interface{}{envelope}
|
|
|
|
if err := p2p.ExpectMsg(tester.stream, messagesCode, payload); err != nil {
|
|
|
|
t.Fatalf("message mismatch: %v", err)
|
|
|
|
}
|
|
|
|
// Make sure that even with a re-insert, an empty batch is received
|
|
|
|
if err := tester.client.Send(envelope); err != nil {
|
|
|
|
t.Fatalf("failed to send message: %v", err)
|
|
|
|
}
|
|
|
|
if err := p2p.ExpectMsg(tester.stream, messagesCode, []interface{}{}); err != nil {
|
|
|
|
t.Fatalf("message mismatch: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerDeliver(t *testing.T) {
|
|
|
|
// Start a tester and execute the handshake
|
2015-04-15 10:50:31 +03:00
|
|
|
tester, err := startTestPeerInited()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to start initialized peer: %v", err)
|
|
|
|
}
|
2015-04-14 18:21:56 +03:00
|
|
|
defer tester.stream.Close()
|
|
|
|
|
|
|
|
// Watch for all inbound messages
|
|
|
|
arrived := make(chan struct{}, 1)
|
|
|
|
tester.client.Watch(Filter{
|
|
|
|
Fn: func(message *Message) {
|
|
|
|
arrived <- struct{}{}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
// Construct a message and deliver it to the tester peer
|
|
|
|
message := NewMessage([]byte("peer broadcast test message"))
|
|
|
|
envelope, err := message.Wrap(DefaultPoW, Options{
|
|
|
|
TTL: DefaultTTL,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to wrap message: %v", err)
|
|
|
|
}
|
|
|
|
if err := p2p.Send(tester.stream, messagesCode, []*Envelope{envelope}); err != nil {
|
|
|
|
t.Fatalf("failed to transfer message: %v", err)
|
|
|
|
}
|
|
|
|
// Check that the message is delivered upstream
|
|
|
|
select {
|
|
|
|
case <-arrived:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("message delivery timeout")
|
|
|
|
}
|
|
|
|
// Check that a resend is not delivered
|
|
|
|
if err := p2p.Send(tester.stream, messagesCode, []*Envelope{envelope}); err != nil {
|
|
|
|
t.Fatalf("failed to transfer message: %v", err)
|
|
|
|
}
|
|
|
|
select {
|
2015-04-16 11:20:01 +03:00
|
|
|
case <-time.After(2 * transmissionCycle):
|
2015-04-14 18:21:56 +03:00
|
|
|
case <-arrived:
|
|
|
|
t.Fatalf("repeating message arrived")
|
|
|
|
}
|
|
|
|
}
|
2015-04-16 13:05:35 +03:00
|
|
|
|
|
|
|
func TestPeerMessageExpiration(t *testing.T) {
|
|
|
|
// Start a tester and execute the handshake
|
|
|
|
tester, err := startTestPeerInited()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to start initialized peer: %v", err)
|
|
|
|
}
|
|
|
|
defer tester.stream.Close()
|
|
|
|
|
|
|
|
// Fetch the peer instance for later inspection
|
|
|
|
tester.client.peerMu.RLock()
|
|
|
|
if peers := len(tester.client.peers); peers != 1 {
|
|
|
|
t.Fatalf("peer pool size mismatch: have %v, want %v", peers, 1)
|
|
|
|
}
|
|
|
|
var peer *peer
|
|
|
|
for peer, _ = range tester.client.peers {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
tester.client.peerMu.RUnlock()
|
|
|
|
|
|
|
|
// Construct a message and pass it through the tester
|
|
|
|
message := NewMessage([]byte("peer test message"))
|
|
|
|
envelope, err := message.Wrap(DefaultPoW, Options{
|
|
|
|
TTL: time.Second,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to wrap message: %v", err)
|
|
|
|
}
|
|
|
|
if err := tester.client.Send(envelope); err != nil {
|
|
|
|
t.Fatalf("failed to send message: %v", err)
|
|
|
|
}
|
|
|
|
payload := []interface{}{envelope}
|
|
|
|
if err := p2p.ExpectMsg(tester.stream, messagesCode, payload); err != nil {
|
2015-11-20 20:23:55 +02:00
|
|
|
// A premature empty message may have been broadcast, check the next too
|
|
|
|
if err := p2p.ExpectMsg(tester.stream, messagesCode, payload); err != nil {
|
|
|
|
t.Fatalf("message mismatch: %v", err)
|
|
|
|
}
|
2015-04-16 13:05:35 +03:00
|
|
|
}
|
|
|
|
// Check that the message is inside the cache
|
|
|
|
if !peer.known.Has(envelope.Hash()) {
|
|
|
|
t.Fatalf("message not found in cache")
|
|
|
|
}
|
|
|
|
// Discard messages until expiration and check cache again
|
2015-11-20 20:23:55 +02:00
|
|
|
exp := time.Now().Add(time.Second + 2*expirationCycle + 100*time.Millisecond)
|
2015-04-16 13:05:35 +03:00
|
|
|
for time.Now().Before(exp) {
|
|
|
|
if err := p2p.ExpectMsg(tester.stream, messagesCode, []interface{}{}); err != nil {
|
|
|
|
t.Fatalf("message mismatch: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if peer.known.Has(envelope.Hash()) {
|
|
|
|
t.Fatalf("message not expired from cache")
|
|
|
|
}
|
|
|
|
}
|