|
1 | 1 | package peer |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "net" |
| 4 | + "fmt" |
| 5 | + "net/netip" |
| 6 | + "strconv" |
5 | 7 | "testing" |
6 | 8 |
|
7 | 9 | "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
8 | 11 | ) |
9 | 12 |
|
10 | | -func TestID(t *testing.T) { |
11 | | - addr, _ := net.ResolveTCPAddr("", "127.0.0.1:6868") |
12 | | - assert.Equal(t, "127.0.0.1-100500", peerImplID{addr: addr, nonce: 100500}.String()) |
| 13 | +type netAddr struct{ net, addr string } |
| 14 | + |
| 15 | +func (n netAddr) Network() string { return n.net } |
| 16 | + |
| 17 | +func (n netAddr) String() string { return n.addr } |
| 18 | + |
| 19 | +func TestPeerImplID(t *testing.T) { |
| 20 | + tests := []struct { |
| 21 | + net, addr string |
| 22 | + nonce uint64 |
| 23 | + errorStr string |
| 24 | + }{ |
| 25 | + {net: "tcp", addr: "127.0.0.1:100", nonce: 100501}, |
| 26 | + {net: "tcp4", addr: "127.0.0.1:100", nonce: 100502}, |
| 27 | + {net: "", addr: "127.0.0.1:100", nonce: 100504}, |
| 28 | + {net: "tcp", addr: "[2001:db8::1]:8080", nonce: 80}, |
| 29 | + {net: "tcp6", addr: "[2001:db8::1]:8080", nonce: 82}, |
| 30 | + { |
| 31 | + net: "tcp6", addr: "127.0.0.1:100", nonce: 100503, |
| 32 | + errorStr: "failed to resolve 'tcp6' addr from '127.0.0.1:100': address 127.0.0.1: no suitable address found", |
| 33 | + }, |
| 34 | + { |
| 35 | + net: "tcp4", addr: "[2001:db8::1]:8080", nonce: 81, |
| 36 | + errorStr: "failed to resolve 'tcp4' addr from '[2001:db8::1]:8080': address 2001:db8::1: no suitable address found", |
| 37 | + }, |
| 38 | + { |
| 39 | + net: "udp", addr: "[2001:db8::1]:8080", nonce: 80, |
| 40 | + errorStr: "failed to resolve 'udp' addr from '[2001:db8::1]:8080': unknown network udp", |
| 41 | + }, |
| 42 | + { |
| 43 | + net: "tcp", addr: "127.0.0.01", nonce: 90, |
| 44 | + errorStr: "failed to resolve 'tcp' addr from '127.0.0.01': address 127.0.0.01: missing port in address", |
| 45 | + }, |
| 46 | + } |
| 47 | + for i, test := range tests { |
| 48 | + t.Run(strconv.Itoa(i+1), func(t *testing.T) { |
| 49 | + id, err := newPeerImplID(netAddr{net: test.net, addr: test.addr}, test.nonce) |
| 50 | + if test.errorStr != "" { |
| 51 | + assert.EqualError(t, err, test.errorStr) |
| 52 | + } else { |
| 53 | + addrP, err := netip.ParseAddrPort(test.addr) |
| 54 | + require.NoError(t, err) |
| 55 | + expectedAddr := addrP.Addr() |
| 56 | + assert.Equal(t, expectedAddr.As16(), id.addr16) |
| 57 | + assert.Equal(t, test.nonce, id.nonce) |
| 58 | + expectedString := fmt.Sprintf("%s-%d", expectedAddr, test.nonce) |
| 59 | + assert.Equal(t, expectedString, id.String()) |
| 60 | + } |
| 61 | + }) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestPeerImplId_InMap(t *testing.T) { |
| 66 | + const ( |
| 67 | + net = "tcp" |
| 68 | + addr = "127.0.0.1:8080" |
| 69 | + ) |
| 70 | + type noncePair struct{ first, second uint64 } |
| 71 | + for i, np := range []noncePair{{100, 500}, {100, 100}} { |
| 72 | + t.Run(strconv.Itoa(i+1), func(t *testing.T) { |
| 73 | + first, err := newPeerImplID(netAddr{net: net, addr: addr}, np.first) |
| 74 | + require.NoError(t, err) |
| 75 | + second, err := newPeerImplID(netAddr{net: net, addr: addr}, np.second) |
| 76 | + require.NoError(t, err) |
| 77 | + |
| 78 | + m := map[ID]struct{}{first: {}} |
| 79 | + _, ok := m[second] |
| 80 | + if unique := np.first != np.second; unique { |
| 81 | + assert.False(t, ok) |
| 82 | + } else { |
| 83 | + assert.True(t, ok) |
| 84 | + } |
| 85 | + }) |
| 86 | + } |
13 | 87 | } |
0 commit comments