|
| 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 | + t.Run(tt.name, func(t *testing.T) { |
| 88 | + if got := AddrAddr(tt.addr); got != tt.want { |
| 89 | + t.Errorf("AddrAddr() = %v, want %v", got, tt.want) |
| 90 | + } |
| 91 | + }) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestIPToAddr(t *testing.T) { |
| 96 | + tests := []struct { |
| 97 | + name string |
| 98 | + ip net.IP |
| 99 | + want netip.Addr |
| 100 | + }{ |
| 101 | + { |
| 102 | + name: "IPv4", |
| 103 | + ip: net.ParseIP("192.0.2.1"), |
| 104 | + want: netip.MustParseAddr("192.0.2.1"), |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "IPv6", |
| 108 | + ip: net.ParseIP("2001:db8::1"), |
| 109 | + want: netip.MustParseAddr("2001:db8::1"), |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "Invalid IP", |
| 113 | + ip: net.IP{1, 2, 3}, |
| 114 | + want: netip.Addr{}, |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "Invalid IP (5 octets)", |
| 118 | + ip: net.IP{192, 0, 2, 1, 1}, |
| 119 | + want: netip.Addr{}, |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "IPv4-mapped IPv6", |
| 123 | + ip: net.ParseIP("::ffff:192.0.2.1"), |
| 124 | + want: netip.MustParseAddr("192.0.2.1"), |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "Nil input", |
| 128 | + ip: nil, |
| 129 | + want: netip.Addr{}, |
| 130 | + }, |
| 131 | + } |
| 132 | + |
| 133 | + for _, tt := range tests { |
| 134 | + t.Run(tt.name, func(t *testing.T) { |
| 135 | + if got := IPToAddr(tt.ip); got != tt.want { |
| 136 | + t.Errorf("IPToAddr() = %v, want %v", got, tt.want) |
| 137 | + } |
| 138 | + }) |
| 139 | + } |
| 140 | +} |
0 commit comments