github.com/shyftnetwork/go-empyrean@v1.8.3-0.20191127201940-fbfca9338f04/swarm/pss/protocol_test.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser 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  // The go-ethereum library 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 Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package pss
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"fmt"
    23  	"strconv"
    24  	"strings"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/ShyftNetwork/go-empyrean/common"
    29  	"github.com/ShyftNetwork/go-empyrean/p2p"
    30  	"github.com/ShyftNetwork/go-empyrean/p2p/enode"
    31  	"github.com/ShyftNetwork/go-empyrean/swarm/log"
    32  )
    33  
    34  type protoCtrl struct {
    35  	C        chan bool
    36  	protocol *Protocol
    37  	run      func(*p2p.Peer, p2p.MsgReadWriter) error
    38  }
    39  
    40  // simple ping pong protocol test for the pss devp2p emulation
    41  func TestProtocol(t *testing.T) {
    42  	t.Run("32", testProtocol)
    43  	t.Run("8", testProtocol)
    44  	t.Run("0", testProtocol)
    45  }
    46  
    47  func testProtocol(t *testing.T) {
    48  
    49  	// address hint size
    50  	var addrsize int64
    51  	paramstring := strings.Split(t.Name(), "/")
    52  	addrsize, _ = strconv.ParseInt(paramstring[1], 10, 0)
    53  	log.Info("protocol test", "addrsize", addrsize)
    54  
    55  	topic := PingTopic.String()
    56  
    57  	clients, err := setupNetwork(2, false)
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  	var loaddrhex string
    62  	err = clients[0].Call(&loaddrhex, "pss_baseAddr")
    63  	if err != nil {
    64  		t.Fatalf("rpc get node 1 baseaddr fail: %v", err)
    65  	}
    66  	loaddrhex = loaddrhex[:2+(addrsize*2)]
    67  	var roaddrhex string
    68  	err = clients[1].Call(&roaddrhex, "pss_baseAddr")
    69  	if err != nil {
    70  		t.Fatalf("rpc get node 2 baseaddr fail: %v", err)
    71  	}
    72  	roaddrhex = roaddrhex[:2+(addrsize*2)]
    73  	lnodeinfo := &p2p.NodeInfo{}
    74  	err = clients[0].Call(&lnodeinfo, "admin_nodeInfo")
    75  	if err != nil {
    76  		t.Fatalf("rpc nodeinfo node 11 fail: %v", err)
    77  	}
    78  
    79  	var lpubkey string
    80  	err = clients[0].Call(&lpubkey, "pss_getPublicKey")
    81  	if err != nil {
    82  		t.Fatalf("rpc get node 1 pubkey fail: %v", err)
    83  	}
    84  	var rpubkey string
    85  	err = clients[1].Call(&rpubkey, "pss_getPublicKey")
    86  	if err != nil {
    87  		t.Fatalf("rpc get node 2 pubkey fail: %v", err)
    88  	}
    89  
    90  	time.Sleep(time.Millisecond * 1000) // replace with hive healthy code
    91  
    92  	lmsgC := make(chan APIMsg)
    93  	lctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
    94  	defer cancel()
    95  	lsub, err := clients[0].Subscribe(lctx, "pss", lmsgC, "receive", topic, false, false)
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  	defer lsub.Unsubscribe()
   100  	rmsgC := make(chan APIMsg)
   101  	rctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
   102  	defer cancel()
   103  	rsub, err := clients[1].Subscribe(rctx, "pss", rmsgC, "receive", topic, false, false)
   104  	if err != nil {
   105  		t.Fatal(err)
   106  	}
   107  	defer rsub.Unsubscribe()
   108  
   109  	// set reciprocal public keys
   110  	err = clients[0].Call(nil, "pss_setPeerPublicKey", rpubkey, topic, roaddrhex)
   111  	if err != nil {
   112  		t.Fatal(err)
   113  	}
   114  	err = clients[1].Call(nil, "pss_setPeerPublicKey", lpubkey, topic, loaddrhex)
   115  	if err != nil {
   116  		t.Fatal(err)
   117  	}
   118  
   119  	// add right peer's public key as protocol peer on left
   120  	p := p2p.NewPeer(enode.ID{}, fmt.Sprintf("%x", common.FromHex(loaddrhex)), []p2p.Cap{})
   121  	_, err = pssprotocols[lnodeinfo.ID].protocol.AddPeer(p, PingTopic, true, rpubkey)
   122  	if err != nil {
   123  		t.Fatal(err)
   124  	}
   125  
   126  	// sends ping asym, checks delivery
   127  	pssprotocols[lnodeinfo.ID].C <- false
   128  	select {
   129  	case <-lmsgC:
   130  		log.Debug("lnode ok")
   131  	case cerr := <-lctx.Done():
   132  		t.Fatalf("test message timed out: %v", cerr)
   133  		return
   134  	}
   135  	select {
   136  	case <-rmsgC:
   137  		log.Debug("rnode ok")
   138  	case cerr := <-lctx.Done():
   139  		t.Fatalf("test message timed out: %v", cerr)
   140  	}
   141  
   142  	// sends ping asym, checks delivery
   143  	pssprotocols[lnodeinfo.ID].C <- false
   144  	select {
   145  	case <-lmsgC:
   146  		log.Debug("lnode ok")
   147  	case cerr := <-lctx.Done():
   148  		t.Fatalf("test message timed out: %v", cerr)
   149  	}
   150  	select {
   151  	case <-rmsgC:
   152  		log.Debug("rnode ok")
   153  	case cerr := <-lctx.Done():
   154  		t.Fatalf("test message timed out: %v", cerr)
   155  	}
   156  	rw := pssprotocols[lnodeinfo.ID].protocol.pubKeyRWPool[rpubkey]
   157  	pssprotocols[lnodeinfo.ID].protocol.RemovePeer(true, rpubkey)
   158  	if err := rw.WriteMsg(p2p.Msg{
   159  		Size:    3,
   160  		Payload: bytes.NewReader([]byte("foo")),
   161  	}); err == nil {
   162  		t.Fatalf("expected error on write")
   163  	}
   164  }