github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/p2p/distip/net_test.go (about)

     1  package distip
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"testing"
     7  )
     8  
     9  func parseIP(s string) net.IP {
    10  	ip := net.ParseIP(s)
    11  	if ip == nil {
    12  		panic("invalid " + s)
    13  	}
    14  	return ip
    15  }
    16  
    17  func checkContains(t *testing.T, fn func(net.IP) bool, inc, exc []string) {
    18  	for _, s := range inc {
    19  		if !fn(parseIP(s)) {
    20  			t.Error("returned false for included address", s)
    21  		}
    22  	}
    23  	for _, s := range exc {
    24  		if fn(parseIP(s)) {
    25  			t.Error("returned true for excluded address", s)
    26  		}
    27  	}
    28  }
    29  
    30  func TestDistinctNetSet(t *testing.T) {
    31  	ops := []struct {
    32  		add, remove string
    33  		fails       bool
    34  	}{
    35  		{add: "127.0.0.1"},
    36  		{add: "127.0.0.2"},
    37  		{add: "127.0.0.3", fails: true},
    38  		{add: "127.32.0.1"},
    39  		{add: "127.32.0.2"},
    40  		{add: "127.32.0.3", fails: true},
    41  		{add: "127.33.0.1", fails: true},
    42  		{add: "127.34.0.1"},
    43  		{add: "127.34.0.2"},
    44  		{add: "127.34.0.3", fails: true},
    45  		// Make room for an address, then add again.
    46  		{remove: "127.0.0.1"},
    47  		{add: "127.0.0.3"},
    48  		{add: "127.0.0.3", fails: true},
    49  	}
    50  
    51  	set := DistinctNetSet{Subnet: 15, Limit: 2}
    52  	for _, op := range ops {
    53  		var desc string
    54  		if op.add != "" {
    55  			desc = fmt.Sprintf("Add(%s)", op.add)
    56  			if ok := set.Add(parseIP(op.add)); ok != !op.fails {
    57  				t.Errorf("%s == %t, want %t", desc, ok, !op.fails)
    58  			}
    59  		} else {
    60  			desc = fmt.Sprintf("Remove(%s)", op.remove)
    61  			set.Remove(parseIP(op.remove))
    62  		}
    63  		t.Logf("%s: %v", desc, set)
    64  	}
    65  }
    66  
    67  func TestIsLAN(t *testing.T) {
    68  	checkContains(t, IsLAN,
    69  		[]string{ // included
    70  			"0.0.0.0",
    71  			"0.2.0.8",
    72  			"127.0.0.1",
    73  			"10.0.1.1",
    74  			"10.22.0.3",
    75  			"172.31.252.251",
    76  			"192.168.1.4",
    77  			"fe80::f4a1:8eff:fec5:9d9d",
    78  			"febf::ab32:2233",
    79  			"fc00::4",
    80  		},
    81  		[]string{ // excluded
    82  			"192.0.2.1",
    83  			"1.0.0.0",
    84  			"172.32.0.1",
    85  			"fec0::2233",
    86  		},
    87  	)
    88  }
    89  
    90  func TestCheckRelayIP(t *testing.T) {
    91  	tests := []struct {
    92  		sender, addr string
    93  		want         error
    94  	}{
    95  		{"127.0.0.1", "0.0.0.0", errUnspecified},
    96  		{"192.168.0.1", "0.0.0.0", errUnspecified},
    97  		{"23.55.1.242", "0.0.0.0", errUnspecified},
    98  		{"127.0.0.1", "255.255.255.255", errSpecial},
    99  		{"192.168.0.1", "255.255.255.255", errSpecial},
   100  		{"23.55.1.242", "255.255.255.255", errSpecial},
   101  		{"192.168.0.1", "127.0.2.19", errLoopback},
   102  		{"23.55.1.242", "192.168.0.1", errLAN},
   103  
   104  		{"127.0.0.1", "127.0.2.19", nil},
   105  		{"127.0.0.1", "192.168.0.1", nil},
   106  		{"127.0.0.1", "23.55.1.242", nil},
   107  		{"192.168.0.1", "192.168.0.1", nil},
   108  		{"192.168.0.1", "23.55.1.242", nil},
   109  		{"23.55.1.242", "23.55.1.242", nil},
   110  	}
   111  
   112  	for _, test := range tests {
   113  		err := CheckRelayIP(parseIP(test.sender), parseIP(test.addr))
   114  		if err != test.want {
   115  			t.Errorf("%s from %s: got %q, want %q", test.addr, test.sender, err, test.want)
   116  		}
   117  	}
   118  }