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