github.com/shyftnetwork/go-empyrean@v1.8.3-0.20191127201940-fbfca9338f04/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/ShyftNetwork/go-empyrean/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(t, registryOptions)
    32  	defer teardown()
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    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(t, registryOptions)
    71  	defer teardown()
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    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(t, registryOptions)
   115  	defer teardown()
   116  	if err != nil {
   117  		t.Fatal(err)
   118  	}
   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(t, registryOptions)
   160  	defer teardown()
   161  	if err != nil {
   162  		t.Fatal(err)
   163  	}
   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  }