github.com/Ethersocial/go-esn@v0.3.7/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/ethersocial/go-esn/common" 29 "github.com/ethersocial/go-esn/p2p" 30 "github.com/ethersocial/go-esn/p2p/enode" 31 "github.com/ethersocial/go-esn/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 p := p2p.NewPeer(enode.ID{}, fmt.Sprintf("%x", common.FromHex(loaddrhex)), []p2p.Cap{}) 115 _, err = pssprotocols[lnodeinfo.ID].protocol.AddPeer(p, PingTopic, true, rpubkey) 116 if err != nil { 117 t.Fatal(err) 118 } 119 120 // sends ping asym, checks delivery 121 pssprotocols[lnodeinfo.ID].C <- false 122 select { 123 case <-lmsgC: 124 log.Debug("lnode ok") 125 case cerr := <-lctx.Done(): 126 t.Fatalf("test message timed out: %v", cerr) 127 } 128 select { 129 case <-rmsgC: 130 log.Debug("rnode ok") 131 case cerr := <-lctx.Done(): 132 t.Fatalf("test message timed out: %v", cerr) 133 } 134 135 // sends ping asym, checks delivery 136 pssprotocols[lnodeinfo.ID].C <- false 137 select { 138 case <-lmsgC: 139 log.Debug("lnode ok") 140 case cerr := <-lctx.Done(): 141 t.Fatalf("test message timed out: %v", cerr) 142 } 143 select { 144 case <-rmsgC: 145 log.Debug("rnode ok") 146 case cerr := <-lctx.Done(): 147 t.Fatalf("test message timed out: %v", cerr) 148 } 149 rw := pssprotocols[lnodeinfo.ID].protocol.pubKeyRWPool[rpubkey] 150 pssprotocols[lnodeinfo.ID].protocol.RemovePeer(true, rpubkey) 151 if err := rw.WriteMsg(p2p.Msg{ 152 Size: 3, 153 Payload: bytes.NewReader([]byte("foo")), 154 }); err == nil { 155 t.Fatalf("expected error on write") 156 } 157 }