github.com/alexanderbez/go-ethereum@v1.8.17-0.20181024144731-0a57b29f0c8e/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/ethereum/go-ethereum/common" 29 "github.com/ethereum/go-ethereum/p2p" 30 "github.com/ethereum/go-ethereum/p2p/discover" 31 "github.com/ethereum/go-ethereum/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) 96 defer lsub.Unsubscribe() 97 rmsgC := make(chan APIMsg) 98 rctx, cancel := context.WithTimeout(context.Background(), time.Second*10) 99 defer cancel() 100 rsub, err := clients[1].Subscribe(rctx, "pss", rmsgC, "receive", topic) 101 defer rsub.Unsubscribe() 102 103 // set reciprocal public keys 104 err = clients[0].Call(nil, "pss_setPeerPublicKey", rpubkey, topic, roaddrhex) 105 if err != nil { 106 t.Fatal(err) 107 } 108 err = clients[1].Call(nil, "pss_setPeerPublicKey", lpubkey, topic, loaddrhex) 109 if err != nil { 110 t.Fatal(err) 111 } 112 113 // add right peer's public key as protocol peer on left 114 nid, _ := discover.HexID("0x00") // this hack is needed to satisfy the p2p method 115 p := p2p.NewPeer(nid, fmt.Sprintf("%x", common.FromHex(loaddrhex)), []p2p.Cap{}) 116 _, err = pssprotocols[lnodeinfo.ID].protocol.AddPeer(p, PingTopic, true, rpubkey) 117 if err != nil { 118 t.Fatal(err) 119 } 120 121 // sends ping asym, checks delivery 122 pssprotocols[lnodeinfo.ID].C <- false 123 select { 124 case <-lmsgC: 125 log.Debug("lnode ok") 126 case cerr := <-lctx.Done(): 127 t.Fatalf("test message timed out: %v", cerr) 128 } 129 select { 130 case <-rmsgC: 131 log.Debug("rnode ok") 132 case cerr := <-lctx.Done(): 133 t.Fatalf("test message timed out: %v", cerr) 134 } 135 136 // sends ping asym, checks delivery 137 pssprotocols[lnodeinfo.ID].C <- false 138 select { 139 case <-lmsgC: 140 log.Debug("lnode ok") 141 case cerr := <-lctx.Done(): 142 t.Fatalf("test message timed out: %v", cerr) 143 } 144 select { 145 case <-rmsgC: 146 log.Debug("rnode ok") 147 case cerr := <-lctx.Done(): 148 t.Fatalf("test message timed out: %v", cerr) 149 } 150 rw := pssprotocols[lnodeinfo.ID].protocol.pubKeyRWPool[rpubkey] 151 pssprotocols[lnodeinfo.ID].protocol.RemovePeer(true, rpubkey) 152 if err := rw.WriteMsg(p2p.Msg{ 153 Size: 3, 154 Payload: bytes.NewReader([]byte("foo")), 155 }); err == nil { 156 t.Fatalf("expected error on write") 157 } 158 }