|
| 1 | +package p2p |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/multiformats/go-multiaddr" |
| 8 | +) |
| 9 | + |
| 10 | +func TestFilterPrivateAddrs(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + input []string |
| 14 | + expected []string |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "mixed_private_and_public_ipv4", |
| 18 | + input: []string{ |
| 19 | + "/ip4/10.0.0.1/tcp/4001", // RFC1918 |
| 20 | + "/ip4/8.8.8.8/tcp/4001", // public |
| 21 | + "/ip4/172.16.0.1/tcp/4001", // RFC1918 |
| 22 | + "/ip4/1.1.1.1/tcp/4001", // public |
| 23 | + "/ip4/192.168.1.1/tcp/4001", // RFC1918 |
| 24 | + "/ip4/127.0.0.1/tcp/4001", // loopback |
| 25 | + "/ip4/169.254.1.1/tcp/4001", // link-local |
| 26 | + }, |
| 27 | + expected: []string{ |
| 28 | + "/ip4/8.8.8.8/tcp/4001", |
| 29 | + "/ip4/1.1.1.1/tcp/4001", |
| 30 | + }, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "mixed_private_and_public_ipv6", |
| 34 | + input: []string{ |
| 35 | + "/ip6/fc00::1/tcp/4001", // unique local |
| 36 | + "/ip6/2001:4860:4860::8888/tcp/4001", // public |
| 37 | + "/ip6/fe80::1/tcp/4001", // link-local |
| 38 | + }, |
| 39 | + expected: []string{ |
| 40 | + "/ip6/2001:4860:4860::8888/tcp/4001", |
| 41 | + }, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "rfc1918_172_range_boundaries", |
| 45 | + input: []string{ |
| 46 | + "/ip4/172.15.0.1/tcp/4001", // not private (below range) |
| 47 | + "/ip4/172.16.0.1/tcp/4001", // private (start of range) |
| 48 | + "/ip4/172.31.255.255/tcp/4001", // private (end of range) |
| 49 | + "/ip4/172.32.0.1/tcp/4001", // not private (above range) |
| 50 | + }, |
| 51 | + expected: []string{ |
| 52 | + "/ip4/172.15.0.1/tcp/4001", |
| 53 | + "/ip4/172.32.0.1/tcp/4001", |
| 54 | + }, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "edge_cases", |
| 58 | + input: []string{ |
| 59 | + "invalid-addr", // invalid address |
| 60 | + "/ip4/8.8.8.8/tcp/4001", // valid public |
| 61 | + "/ip4/10.0.0.1/tcp/4001", // valid private |
| 62 | + }, |
| 63 | + expected: []string{ |
| 64 | + "/ip4/8.8.8.8/tcp/4001", |
| 65 | + }, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "empty_input", |
| 69 | + input: []string{}, |
| 70 | + expected: []string{}, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "all_private", |
| 74 | + input: []string{ |
| 75 | + "/ip4/10.0.0.1/tcp/4001", |
| 76 | + "/ip4/192.168.1.1/tcp/4001", |
| 77 | + }, |
| 78 | + expected: []string{}, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "all_public", |
| 82 | + input: []string{ |
| 83 | + "/ip4/8.8.8.8/tcp/4001", |
| 84 | + "/ip6/2001:4860:4860::8888/tcp/4001", |
| 85 | + }, |
| 86 | + expected: []string{ |
| 87 | + "/ip4/8.8.8.8/tcp/4001", |
| 88 | + "/ip6/2001:4860:4860::8888/tcp/4001", |
| 89 | + }, |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + for _, tt := range tests { |
| 94 | + t.Run(tt.name, func(t *testing.T) { |
| 95 | + result := filterPrivateAddrs(testParseMultiaddrs(tt.input)) |
| 96 | + assertAddrsEqual(t, tt.expected, result) |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// testParseMultiaddrs converts string addresses to multiaddr.Multiaddr, skipping invalid ones |
| 102 | +func testParseMultiaddrs(addrs []string) []multiaddr.Multiaddr { |
| 103 | + result := make([]multiaddr.Multiaddr, 0, len(addrs)) |
| 104 | + for _, addrStr := range addrs { |
| 105 | + addr, err := multiaddr.NewMultiaddr(addrStr) |
| 106 | + if err == nil { |
| 107 | + result = append(result, addr) |
| 108 | + } |
| 109 | + } |
| 110 | + return result |
| 111 | +} |
| 112 | + |
| 113 | +// assertAddrsEqual compares expected string addresses with actual multiaddrs |
| 114 | +func assertAddrsEqual(t *testing.T, expected []string, actual []multiaddr.Multiaddr) { |
| 115 | + t.Helper() |
| 116 | + |
| 117 | + actualStrs := make([]string, len(actual)) |
| 118 | + for i, addr := range actual { |
| 119 | + actualStrs[i] = addr.String() |
| 120 | + } |
| 121 | + |
| 122 | + if len(actualStrs) != len(expected) { |
| 123 | + t.Errorf("Expected %d addresses, got %d.\nExpected: %v\nGot: %v", |
| 124 | + len(expected), len(actualStrs), expected, actualStrs) |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + for i, expectedAddr := range expected { |
| 129 | + if actualStrs[i] != expectedAddr { |
| 130 | + t.Errorf("Address %d: expected %s, got %s", i, expectedAddr, actualStrs[i]) |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func TestIsPrivateIP(t *testing.T) { |
| 136 | + privateIPs := []string{ |
| 137 | + // RFC1918 |
| 138 | + "10.0.0.1", "10.255.255.255", |
| 139 | + "172.16.0.1", "172.31.255.255", |
| 140 | + "192.168.1.1", "192.168.255.255", |
| 141 | + // Link-local |
| 142 | + "169.254.1.1", |
| 143 | + // Loopback |
| 144 | + "127.0.0.1", "127.255.255.255", |
| 145 | + // IPv6 unique local and link-local |
| 146 | + "fc00::1", "fd00::1", "fe80::1", "::1", |
| 147 | + } |
| 148 | + |
| 149 | + publicIPs := []string{ |
| 150 | + // Public IPv4 |
| 151 | + "8.8.8.8", "1.1.1.1", "208.67.222.222", |
| 152 | + // RFC1918 172.x boundaries (not in 16-31 range) |
| 153 | + "172.15.0.1", "172.32.0.1", |
| 154 | + // Public IPv6 |
| 155 | + "2001:4860:4860::8888", "2606:4700:4700::1111", |
| 156 | + } |
| 157 | + |
| 158 | + for _, ipStr := range privateIPs { |
| 159 | + t.Run(ipStr, func(t *testing.T) { |
| 160 | + ip := net.ParseIP(ipStr) |
| 161 | + if ip == nil { |
| 162 | + t.Fatalf("Failed to parse IP: %s", ipStr) |
| 163 | + } |
| 164 | + if !isPrivateIP(ip) { |
| 165 | + t.Errorf("isPrivateIP(%s) = false, want true", ipStr) |
| 166 | + } |
| 167 | + }) |
| 168 | + } |
| 169 | + |
| 170 | + for _, ipStr := range publicIPs { |
| 171 | + t.Run(ipStr, func(t *testing.T) { |
| 172 | + ip := net.ParseIP(ipStr) |
| 173 | + if ip == nil { |
| 174 | + t.Fatalf("Failed to parse IP: %s", ipStr) |
| 175 | + } |
| 176 | + if isPrivateIP(ip) { |
| 177 | + t.Errorf("isPrivateIP(%s) = true, want false", ipStr) |
| 178 | + } |
| 179 | + }) |
| 180 | + } |
| 181 | +} |
0 commit comments