github.com/jincm/wesharechain@v0.0.0-20210122032815-1537409ce26a/chain/swarm/network/stream/lightnode_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 package stream 17 18 import ( 19 "testing" 20 21 p2ptest "github.com/ethereum/go-ethereum/p2p/testing" 22 ) 23 24 // This test checks the default behavior of the server, that is 25 // when it is serving Retrieve requests. 26 func TestLigthnodeRetrieveRequestWithRetrieve(t *testing.T) { 27 registryOptions := &RegistryOptions{ 28 Retrieval: RetrievalClientOnly, 29 Syncing: SyncingDisabled, 30 } 31 tester, _, _, teardown, err := newStreamerTester(registryOptions) 32 if err != nil { 33 t.Fatal(err) 34 } 35 defer teardown() 36 37 node := tester.Nodes[0] 38 39 stream := NewStream(swarmChunkServerStreamName, "", false) 40 41 err = tester.TestExchanges(p2ptest.Exchange{ 42 Label: "SubscribeMsg", 43 Triggers: []p2ptest.Trigger{ 44 { 45 Code: 4, 46 Msg: &SubscribeMsg{ 47 Stream: stream, 48 }, 49 Peer: node.ID(), 50 }, 51 }, 52 }) 53 if err != nil { 54 t.Fatalf("Got %v", err) 55 } 56 57 err = tester.TestDisconnected(&p2ptest.Disconnect{Peer: node.ID()}) 58 if err == nil || err.Error() != "timed out waiting for peers to disconnect" { 59 t.Fatalf("Expected no disconnect, got %v", err) 60 } 61 } 62 63 // This test checks the Lightnode behavior of server, when serving Retrieve 64 // requests are disabled 65 func TestLigthnodeRetrieveRequestWithoutRetrieve(t *testing.T) { 66 registryOptions := &RegistryOptions{ 67 Retrieval: RetrievalDisabled, 68 Syncing: SyncingDisabled, 69 } 70 tester, _, _, teardown, err := newStreamerTester(registryOptions) 71 if err != nil { 72 t.Fatal(err) 73 } 74 defer teardown() 75 76 node := tester.Nodes[0] 77 78 stream := NewStream(swarmChunkServerStreamName, "", false) 79 80 err = tester.TestExchanges( 81 p2ptest.Exchange{ 82 Label: "SubscribeMsg", 83 Triggers: []p2ptest.Trigger{ 84 { 85 Code: 4, 86 Msg: &SubscribeMsg{ 87 Stream: stream, 88 }, 89 Peer: node.ID(), 90 }, 91 }, 92 Expects: []p2ptest.Expect{ 93 { 94 Code: 7, 95 Msg: &SubscribeErrorMsg{ 96 Error: "stream RETRIEVE_REQUEST not registered", 97 }, 98 Peer: node.ID(), 99 }, 100 }, 101 }) 102 if err != nil { 103 t.Fatalf("Got %v", err) 104 } 105 } 106 107 // This test checks the default behavior of the server, that is 108 // when syncing is enabled. 109 func TestLigthnodeRequestSubscriptionWithSync(t *testing.T) { 110 registryOptions := &RegistryOptions{ 111 Retrieval: RetrievalDisabled, 112 Syncing: SyncingRegisterOnly, 113 } 114 tester, _, _, teardown, err := newStreamerTester(registryOptions) 115 if err != nil { 116 t.Fatal(err) 117 } 118 defer teardown() 119 120 node := tester.Nodes[0] 121 122 syncStream := NewStream("SYNC", FormatSyncBinKey(1), false) 123 124 err = tester.TestExchanges( 125 p2ptest.Exchange{ 126 Label: "RequestSubscription", 127 Triggers: []p2ptest.Trigger{ 128 { 129 Code: 8, 130 Msg: &RequestSubscriptionMsg{ 131 Stream: syncStream, 132 }, 133 Peer: node.ID(), 134 }, 135 }, 136 Expects: []p2ptest.Expect{ 137 { 138 Code: 4, 139 Msg: &SubscribeMsg{ 140 Stream: syncStream, 141 }, 142 Peer: node.ID(), 143 }, 144 }, 145 }) 146 147 if err != nil { 148 t.Fatalf("Got %v", err) 149 } 150 } 151 152 // This test checks the Lightnode behavior of the server, that is 153 // when syncing is disabled. 154 func TestLigthnodeRequestSubscriptionWithoutSync(t *testing.T) { 155 registryOptions := &RegistryOptions{ 156 Retrieval: RetrievalDisabled, 157 Syncing: SyncingDisabled, 158 } 159 tester, _, _, teardown, err := newStreamerTester(registryOptions) 160 if err != nil { 161 t.Fatal(err) 162 } 163 defer teardown() 164 165 node := tester.Nodes[0] 166 167 syncStream := NewStream("SYNC", FormatSyncBinKey(1), false) 168 169 err = tester.TestExchanges(p2ptest.Exchange{ 170 Label: "RequestSubscription", 171 Triggers: []p2ptest.Trigger{ 172 { 173 Code: 8, 174 Msg: &RequestSubscriptionMsg{ 175 Stream: syncStream, 176 }, 177 Peer: node.ID(), 178 }, 179 }, 180 Expects: []p2ptest.Expect{ 181 { 182 Code: 7, 183 Msg: &SubscribeErrorMsg{ 184 Error: "stream SYNC not registered", 185 }, 186 Peer: node.ID(), 187 }, 188 }, 189 }, p2ptest.Exchange{ 190 Label: "RequestSubscription", 191 Triggers: []p2ptest.Trigger{ 192 { 193 Code: 4, 194 Msg: &SubscribeMsg{ 195 Stream: syncStream, 196 }, 197 Peer: node.ID(), 198 }, 199 }, 200 Expects: []p2ptest.Expect{ 201 { 202 Code: 7, 203 Msg: &SubscribeErrorMsg{ 204 Error: "stream SYNC not registered", 205 }, 206 Peer: node.ID(), 207 }, 208 }, 209 }) 210 211 if err != nil { 212 t.Fatalf("Got %v", err) 213 } 214 }