gopkg.in/bitherhq/go-bither.v1@v1.7.1/p2p/netutil/net_test.go (about)

     1  // Copyright 2016 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 netutil
    18  
    19  import (
    20  	"net"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/davecgh/go-spew/spew"
    25  )
    26  
    27  func TestParseNetlist(t *testing.T) {
    28  	var tests = []struct {
    29  		input    string
    30  		wantErr  error
    31  		wantList *Netlist
    32  	}{
    33  		{
    34  			input:    "",
    35  			wantList: &Netlist{},
    36  		},
    37  		{
    38  			input:    "127.0.0.0/8",
    39  			wantErr:  nil,
    40  			wantList: &Netlist{{IP: net.IP{127, 0, 0, 0}, Mask: net.CIDRMask(8, 32)}},
    41  		},
    42  		{
    43  			input:   "127.0.0.0/44",
    44  			wantErr: &net.ParseError{Type: "CIDR address", Text: "127.0.0.0/44"},
    45  		},
    46  		{
    47  			input: "127.0.0.0/16, 23.23.23.23/24,",
    48  			wantList: &Netlist{
    49  				{IP: net.IP{127, 0, 0, 0}, Mask: net.CIDRMask(16, 32)},
    50  				{IP: net.IP{23, 23, 23, 0}, Mask: net.CIDRMask(24, 32)},
    51  			},
    52  		},
    53  	}
    54  
    55  	for _, test := range tests {
    56  		l, err := ParseNetlist(test.input)
    57  		if !reflect.DeepEqual(err, test.wantErr) {
    58  			t.Errorf("%q: got error %q, want %q", test.input, err, test.wantErr)
    59  			continue
    60  		}
    61  		if !reflect.DeepEqual(l, test.wantList) {
    62  			spew.Dump(l)
    63  			spew.Dump(test.wantList)
    64  			t.Errorf("%q: got %v, want %v", test.input, l, test.wantList)
    65  		}
    66  	}
    67  }
    68  
    69  func TestNilNetListContains(t *testing.T) {
    70  	var list *Netlist
    71  	checkContains(t, list.Contains, nil, []string{"1.2.3.4"})
    72  }
    73  
    74  func TestIsLAN(t *testing.T) {
    75  	checkContains(t, IsLAN,
    76  		[]string{ // included
    77  			"0.0.0.0",
    78  			"0.2.0.8",
    79  			"127.0.0.1",
    80  			"10.0.1.1",
    81  			"10.22.0.3",
    82  			"172.31.252.251",
    83  			"192.168.1.4",
    84  			"fe80::f4a1:8eff:fec5:9d9d",
    85  			"febf::ab32:2233",
    86  			"fc00::4",
    87  		},
    88  		[]string{ // excluded
    89  			"192.0.2.1",
    90  			"1.0.0.0",
    91  			"172.32.0.1",
    92  			"fec0::2233",
    93  		},
    94  	)
    95  }
    96  
    97  func TestIsSpecialNetwork(t *testing.T) {
    98  	checkContains(t, IsSpecialNetwork,
    99  		[]string{ // included
   100  			"192.0.2.1",
   101  			"192.0.2.44",
   102  			"2001:db8:85a3:8d3:1319:8a2e:370:7348",
   103  			"255.255.255.255",
   104  			"224.0.0.22", // IPv4 multicast
   105  			"ff05::1:3",  // IPv6 multicast
   106  		},
   107  		[]string{ // excluded
   108  			"192.0.3.1",
   109  			"1.0.0.0",
   110  			"172.32.0.1",
   111  			"fec0::2233",
   112  		},
   113  	)
   114  }
   115  
   116  func checkContains(t *testing.T, fn func(net.IP) bool, inc, exc []string) {
   117  	for _, s := range inc {
   118  		if !fn(parseIP(s)) {
   119  			t.Error("returned false for included address", s)
   120  		}
   121  	}
   122  	for _, s := range exc {
   123  		if fn(parseIP(s)) {
   124  			t.Error("returned true for excluded address", s)
   125  		}
   126  	}
   127  }
   128  
   129  func parseIP(s string) net.IP {
   130  	ip := net.ParseIP(s)
   131  	if ip == nil {
   132  		panic("invalid " + s)
   133  	}
   134  	return ip
   135  }
   136  
   137  func TestCheckRelayIP(t *testing.T) {
   138  	tests := []struct {
   139  		sender, addr string
   140  		want         error
   141  	}{
   142  		{"127.0.0.1", "0.0.0.0", errUnspecified},
   143  		{"192.168.0.1", "0.0.0.0", errUnspecified},
   144  		{"23.55.1.242", "0.0.0.0", errUnspecified},
   145  		{"127.0.0.1", "255.255.255.255", errSpecial},
   146  		{"192.168.0.1", "255.255.255.255", errSpecial},
   147  		{"23.55.1.242", "255.255.255.255", errSpecial},
   148  		{"192.168.0.1", "127.0.2.19", errLoopback},
   149  		{"23.55.1.242", "192.168.0.1", errLAN},
   150  
   151  		{"127.0.0.1", "127.0.2.19", nil},
   152  		{"127.0.0.1", "192.168.0.1", nil},
   153  		{"127.0.0.1", "23.55.1.242", nil},
   154  		{"192.168.0.1", "192.168.0.1", nil},
   155  		{"192.168.0.1", "23.55.1.242", nil},
   156  		{"23.55.1.242", "23.55.1.242", nil},
   157  	}
   158  
   159  	for _, test := range tests {
   160  		err := CheckRelayIP(parseIP(test.sender), parseIP(test.addr))
   161  		if err != test.want {
   162  			t.Errorf("%s from %s: got %q, want %q", test.addr, test.sender, err, test.want)
   163  		}
   164  	}
   165  }
   166  
   167  func BenchmarkCheckRelayIP(b *testing.B) {
   168  	sender := parseIP("23.55.1.242")
   169  	addr := parseIP("23.55.1.2")
   170  	for i := 0; i < b.N; i++ {
   171  		CheckRelayIP(sender, addr)
   172  	}
   173  }