Skip to content

Commit 807ce10

Browse files
tianyeyouyouholiman
authored andcommitted
p2p/netutil: unittests for addrutil (ethereum#30439)
add unit tests for `p2p/addrutil` --------- Co-authored-by: Martin HS <[email protected]>
1 parent d25edae commit 807ce10

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

p2p/netutil/addrutil_test.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// Copyright 2024 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+
"net/netip"
22+
"path/filepath"
23+
"testing"
24+
)
25+
26+
// customNetAddr is a custom implementation of net.Addr for testing purposes.
27+
type customNetAddr struct{}
28+
29+
func (c *customNetAddr) Network() string { return "custom" }
30+
func (c *customNetAddr) String() string { return "custom" }
31+
32+
func TestAddrAddr(t *testing.T) {
33+
tempDir := t.TempDir()
34+
tests := []struct {
35+
name string
36+
addr net.Addr
37+
want netip.Addr
38+
}{
39+
{
40+
name: "IPAddr IPv4",
41+
addr: &net.IPAddr{IP: net.ParseIP("192.0.2.1")},
42+
want: netip.MustParseAddr("192.0.2.1"),
43+
},
44+
{
45+
name: "IPAddr IPv6",
46+
addr: &net.IPAddr{IP: net.ParseIP("2001:db8::1")},
47+
want: netip.MustParseAddr("2001:db8::1"),
48+
},
49+
{
50+
name: "TCPAddr IPv4",
51+
addr: &net.TCPAddr{IP: net.ParseIP("192.0.2.1"), Port: 8080},
52+
want: netip.MustParseAddr("192.0.2.1"),
53+
},
54+
{
55+
name: "TCPAddr IPv6",
56+
addr: &net.TCPAddr{IP: net.ParseIP("2001:db8::1"), Port: 8080},
57+
want: netip.MustParseAddr("2001:db8::1"),
58+
},
59+
{
60+
name: "UDPAddr IPv4",
61+
addr: &net.UDPAddr{IP: net.ParseIP("192.0.2.1"), Port: 8080},
62+
want: netip.MustParseAddr("192.0.2.1"),
63+
},
64+
{
65+
name: "UDPAddr IPv6",
66+
addr: &net.UDPAddr{IP: net.ParseIP("2001:db8::1"), Port: 8080},
67+
want: netip.MustParseAddr("2001:db8::1"),
68+
},
69+
{
70+
name: "Unsupported Addr type",
71+
addr: &net.UnixAddr{Name: filepath.Join(tempDir, "test.sock"), Net: "unix"},
72+
want: netip.Addr{},
73+
},
74+
{
75+
name: "Nil input",
76+
addr: nil,
77+
want: netip.Addr{},
78+
},
79+
{
80+
name: "Custom net.Addr implementation",
81+
addr: &customNetAddr{},
82+
want: netip.Addr{},
83+
},
84+
}
85+
86+
for _, tt := range tests {
87+
tt := tt
88+
t.Run(tt.name, func(t *testing.T) {
89+
if got := AddrAddr(tt.addr); got != tt.want {
90+
t.Errorf("AddrAddr() = %v, want %v", got, tt.want)
91+
}
92+
})
93+
}
94+
}
95+
96+
func TestIPToAddr(t *testing.T) {
97+
tests := []struct {
98+
name string
99+
ip net.IP
100+
want netip.Addr
101+
}{
102+
{
103+
name: "IPv4",
104+
ip: net.ParseIP("192.0.2.1"),
105+
want: netip.MustParseAddr("192.0.2.1"),
106+
},
107+
{
108+
name: "IPv6",
109+
ip: net.ParseIP("2001:db8::1"),
110+
want: netip.MustParseAddr("2001:db8::1"),
111+
},
112+
{
113+
name: "Invalid IP",
114+
ip: net.IP{1, 2, 3},
115+
want: netip.Addr{},
116+
},
117+
{
118+
name: "Invalid IP (5 octets)",
119+
ip: net.IP{192, 0, 2, 1, 1},
120+
want: netip.Addr{},
121+
},
122+
{
123+
name: "IPv4-mapped IPv6",
124+
ip: net.ParseIP("::ffff:192.0.2.1"),
125+
want: netip.MustParseAddr("192.0.2.1"),
126+
},
127+
{
128+
name: "Nil input",
129+
ip: nil,
130+
want: netip.Addr{},
131+
},
132+
}
133+
134+
for _, tt := range tests {
135+
tt := tt
136+
t.Run(tt.name, func(t *testing.T) {
137+
if got := IPToAddr(tt.ip); got != tt.want {
138+
t.Errorf("IPToAddr() = %v, want %v", got, tt.want)
139+
}
140+
})
141+
}
142+
}

0 commit comments

Comments
 (0)