github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/swarm/network/simulation/connect_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 simulation
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/ethereum/go-ethereum/p2p/enode"
    23  )
    24  
    25  func TestConnectToPivotNode(t *testing.T) {
    26  	sim := New(noopServiceFuncMap)
    27  	defer sim.Close()
    28  
    29  	pid, err := sim.AddNode()
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  
    34  	sim.SetPivotNode(pid)
    35  
    36  	id, err := sim.AddNode()
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  
    41  	if len(sim.Net.Conns) > 0 {
    42  		t.Fatal("no connections should exist after just adding nodes")
    43  	}
    44  
    45  	err = sim.ConnectToPivotNode(id)
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	if sim.Net.GetConn(id, pid) == nil {
    51  		t.Error("node did not connect to pivot node")
    52  	}
    53  }
    54  
    55  func TestConnectToLastNode(t *testing.T) {
    56  	sim := New(noopServiceFuncMap)
    57  	defer sim.Close()
    58  
    59  	n := 10
    60  
    61  	ids, err := sim.AddNodes(n)
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  
    66  	id, err := sim.AddNode()
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  
    71  	if len(sim.Net.Conns) > 0 {
    72  		t.Fatal("no connections should exist after just adding nodes")
    73  	}
    74  
    75  	err = sim.ConnectToLastNode(id)
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  
    80  	for _, i := range ids[:n-2] {
    81  		if sim.Net.GetConn(id, i) != nil {
    82  			t.Error("node connected to the node that is not the last")
    83  		}
    84  	}
    85  
    86  	if sim.Net.GetConn(id, ids[n-1]) == nil {
    87  		t.Error("node did not connect to the last node")
    88  	}
    89  }
    90  
    91  func TestConnectToRandomNode(t *testing.T) {
    92  	sim := New(noopServiceFuncMap)
    93  	defer sim.Close()
    94  
    95  	n := 10
    96  
    97  	ids, err := sim.AddNodes(n)
    98  	if err != nil {
    99  		t.Fatal(err)
   100  	}
   101  
   102  	if len(sim.Net.Conns) > 0 {
   103  		t.Fatal("no connections should exist after just adding nodes")
   104  	}
   105  
   106  	err = sim.ConnectToRandomNode(ids[0])
   107  	if err != nil {
   108  		t.Fatal(err)
   109  	}
   110  
   111  	var cc int
   112  	for i := 0; i < n; i++ {
   113  		for j := i + 1; j < n; j++ {
   114  			if sim.Net.GetConn(ids[i], ids[j]) != nil {
   115  				cc++
   116  			}
   117  		}
   118  	}
   119  
   120  	if cc != 1 {
   121  		t.Errorf("expected one connection, got %v", cc)
   122  	}
   123  }
   124  
   125  func TestConnectNodesFull(t *testing.T) {
   126  	sim := New(noopServiceFuncMap)
   127  	defer sim.Close()
   128  
   129  	ids, err := sim.AddNodes(12)
   130  	if err != nil {
   131  		t.Fatal(err)
   132  	}
   133  
   134  	if len(sim.Net.Conns) > 0 {
   135  		t.Fatal("no connections should exist after just adding nodes")
   136  	}
   137  
   138  	err = sim.ConnectNodesFull(ids)
   139  	if err != nil {
   140  		t.Fatal(err)
   141  	}
   142  
   143  	testFull(t, sim, ids)
   144  }
   145  
   146  func testFull(t *testing.T, sim *Simulation, ids []enode.ID) {
   147  	n := len(ids)
   148  	var cc int
   149  	for i := 0; i < n; i++ {
   150  		for j := i + 1; j < n; j++ {
   151  			if sim.Net.GetConn(ids[i], ids[j]) != nil {
   152  				cc++
   153  			}
   154  		}
   155  	}
   156  
   157  	want := n * (n - 1) / 2
   158  
   159  	if cc != want {
   160  		t.Errorf("expected %v connection, got %v", want, cc)
   161  	}
   162  }
   163  
   164  func TestConnectNodesChain(t *testing.T) {
   165  	sim := New(noopServiceFuncMap)
   166  	defer sim.Close()
   167  
   168  	ids, err := sim.AddNodes(10)
   169  	if err != nil {
   170  		t.Fatal(err)
   171  	}
   172  
   173  	if len(sim.Net.Conns) > 0 {
   174  		t.Fatal("no connections should exist after just adding nodes")
   175  	}
   176  
   177  	err = sim.ConnectNodesChain(ids)
   178  	if err != nil {
   179  		t.Fatal(err)
   180  	}
   181  
   182  	testChain(t, sim, ids)
   183  }
   184  
   185  func testChain(t *testing.T, sim *Simulation, ids []enode.ID) {
   186  	n := len(ids)
   187  	for i := 0; i < n; i++ {
   188  		for j := i + 1; j < n; j++ {
   189  			c := sim.Net.GetConn(ids[i], ids[j])
   190  			if i == j-1 {
   191  				if c == nil {
   192  					t.Errorf("nodes %v and %v are not connected, but they should be", i, j)
   193  				}
   194  			} else {
   195  				if c != nil {
   196  					t.Errorf("nodes %v and %v are connected, but they should not be", i, j)
   197  				}
   198  			}
   199  		}
   200  	}
   201  }
   202  
   203  func TestConnectNodesRing(t *testing.T) {
   204  	sim := New(noopServiceFuncMap)
   205  	defer sim.Close()
   206  
   207  	ids, err := sim.AddNodes(10)
   208  	if err != nil {
   209  		t.Fatal(err)
   210  	}
   211  
   212  	if len(sim.Net.Conns) > 0 {
   213  		t.Fatal("no connections should exist after just adding nodes")
   214  	}
   215  
   216  	err = sim.ConnectNodesRing(ids)
   217  	if err != nil {
   218  		t.Fatal(err)
   219  	}
   220  
   221  	testRing(t, sim, ids)
   222  }
   223  
   224  func testRing(t *testing.T, sim *Simulation, ids []enode.ID) {
   225  	n := len(ids)
   226  	for i := 0; i < n; i++ {
   227  		for j := i + 1; j < n; j++ {
   228  			c := sim.Net.GetConn(ids[i], ids[j])
   229  			if i == j-1 || (i == 0 && j == n-1) {
   230  				if c == nil {
   231  					t.Errorf("nodes %v and %v are not connected, but they should be", i, j)
   232  				}
   233  			} else {
   234  				if c != nil {
   235  					t.Errorf("nodes %v and %v are connected, but they should not be", i, j)
   236  				}
   237  			}
   238  		}
   239  	}
   240  }
   241  
   242  func TestConnectToNodesStar(t *testing.T) {
   243  	sim := New(noopServiceFuncMap)
   244  	defer sim.Close()
   245  
   246  	ids, err := sim.AddNodes(10)
   247  	if err != nil {
   248  		t.Fatal(err)
   249  	}
   250  
   251  	if len(sim.Net.Conns) > 0 {
   252  		t.Fatal("no connections should exist after just adding nodes")
   253  	}
   254  
   255  	centerIndex := 2
   256  
   257  	err = sim.ConnectNodesStar(ids[centerIndex], ids)
   258  	if err != nil {
   259  		t.Fatal(err)
   260  	}
   261  
   262  	testStar(t, sim, ids, centerIndex)
   263  }
   264  
   265  func testStar(t *testing.T, sim *Simulation, ids []enode.ID, centerIndex int) {
   266  	n := len(ids)
   267  	for i := 0; i < n; i++ {
   268  		for j := i + 1; j < n; j++ {
   269  			c := sim.Net.GetConn(ids[i], ids[j])
   270  			if i == centerIndex || j == centerIndex {
   271  				if c == nil {
   272  					t.Errorf("nodes %v and %v are not connected, but they should be", i, j)
   273  				}
   274  			} else {
   275  				if c != nil {
   276  					t.Errorf("nodes %v and %v are connected, but they should not be", i, j)
   277  				}
   278  			}
   279  		}
   280  	}
   281  }
   282  
   283  func TestConnectToNodesStarPivot(t *testing.T) {
   284  	sim := New(noopServiceFuncMap)
   285  	defer sim.Close()
   286  
   287  	ids, err := sim.AddNodes(10)
   288  	if err != nil {
   289  		t.Fatal(err)
   290  	}
   291  
   292  	if len(sim.Net.Conns) > 0 {
   293  		t.Fatal("no connections should exist after just adding nodes")
   294  	}
   295  
   296  	pivotIndex := 4
   297  
   298  	sim.SetPivotNode(ids[pivotIndex])
   299  
   300  	err = sim.ConnectNodesStarPivot(ids)
   301  	if err != nil {
   302  		t.Fatal(err)
   303  	}
   304  
   305  	testStar(t, sim, ids, pivotIndex)
   306  }