@@ -14,248 +14,168 @@ func TestFilterPrivateAddrs(t *testing.T) {
1414 expected []string
1515 }{
1616 {
17- name : "filter_out_private_ipv4 " ,
17+ name : "mixed_private_and_public_ipv4 " ,
1818 input : []string {
19- "/ip4/10.0.0.1/tcp/4001" ,
20- "/ip4/8.8.8.8/tcp/4001" ,
21- "/ip4/172.16.0.1/tcp/4001" ,
22- "/ip4/1.1.1.1/tcp/4001" ,
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
2326 },
2427 expected : []string {
2528 "/ip4/8.8.8.8/tcp/4001" ,
2629 "/ip4/1.1.1.1/tcp/4001" ,
2730 },
2831 },
2932 {
30- name : "filter_out_localhost " ,
33+ name : "mixed_private_and_public_ipv6 " ,
3134 input : []string {
32- "/ip4/127.0.0.1/tcp/4001" ,
33- "/ip4/8.8.8.8/tcp/4001" ,
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
3438 },
3539 expected : []string {
36- "/ip4/8.8.8.8 /tcp/4001" ,
40+ "/ip6/2001:4860:4860::8888 /tcp/4001" ,
3741 },
3842 },
3943 {
40- name : "filter_out_link_local " ,
44+ name : "rfc1918_172_range_boundaries " ,
4145 input : []string {
42- "/ip4/169.254.1.1/tcp/4001" ,
43- "/ip4/8.8.8.8/tcp/4001" ,
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)
4450 },
4551 expected : []string {
46- "/ip4/8.8.8.8/tcp/4001" ,
52+ "/ip4/172.15.0.1/tcp/4001" ,
53+ "/ip4/172.32.0.1/tcp/4001" ,
4754 },
4855 },
4956 {
50- name : "filter_out_192_168 " ,
57+ name : "edge_cases " ,
5158 input : []string {
52- "/ip4/192.168.1.1/tcp/4001" ,
53- "/ip4/8.8.8.8/tcp/4001" ,
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
5462 },
5563 expected : []string {
5664 "/ip4/8.8.8.8/tcp/4001" ,
5765 },
5866 },
5967 {
60- name : "filter_out_ipv6_private" ,
61- input : []string {
62- "/ip6/fc00::1/tcp/4001" ,
63- "/ip6/2001:4860:4860::8888/tcp/4001" ,
64- },
65- expected : []string {
66- "/ip6/2001:4860:4860::8888/tcp/4001" ,
67- },
68- },
69- {
70- name : "filter_out_ipv6_link_local" ,
71- input : []string {
72- "/ip6/fe80::1/tcp/4001" ,
73- "/ip6/2001:4860:4860::8888/tcp/4001" ,
74- },
75- expected : []string {
76- "/ip6/2001:4860:4860::8888/tcp/4001" ,
77- },
68+ name : "empty_input" ,
69+ input : []string {},
70+ expected : []string {},
7871 },
7972 {
80- name : "all_private_returns_empty " ,
73+ name : "all_private " ,
8174 input : []string {
8275 "/ip4/10.0.0.1/tcp/4001" ,
8376 "/ip4/192.168.1.1/tcp/4001" ,
84- "/ip4/127.0.0.1/tcp/4001" ,
8577 },
8678 expected : []string {},
8779 },
8880 {
89- name : "all_public_returns_all " ,
81+ name : "all_public " ,
9082 input : []string {
9183 "/ip4/8.8.8.8/tcp/4001" ,
92- "/ip4/1.1.1.1/tcp/4001" ,
9384 "/ip6/2001:4860:4860::8888/tcp/4001" ,
9485 },
9586 expected : []string {
9687 "/ip4/8.8.8.8/tcp/4001" ,
97- "/ip4/1.1.1.1/tcp/4001" ,
9888 "/ip6/2001:4860:4860::8888/tcp/4001" ,
9989 },
10090 },
101- {
102- name : "empty_input_returns_empty" ,
103- input : []string {},
104- expected : []string {},
105- },
106- {
107- name : "mixed_valid_and_invalid" ,
108- input : []string {
109- "/ip4/8.8.8.8/tcp/4001" ,
110- "invalid-addr" ,
111- "/ip4/10.0.0.1/tcp/4001" ,
112- },
113- expected : []string {
114- "/ip4/8.8.8.8/tcp/4001" ,
115- },
116- },
117- {
118- name : "edge_case_172_15_not_filtered" ,
119- input : []string {
120- "/ip4/172.15.0.1/tcp/4001" , // Not in 172.16-31 range
121- "/ip4/8.8.8.8/tcp/4001" ,
122- },
123- expected : []string {
124- "/ip4/172.15.0.1/tcp/4001" ,
125- "/ip4/8.8.8.8/tcp/4001" ,
126- },
127- },
128- {
129- name : "edge_case_172_32_not_filtered" ,
130- input : []string {
131- "/ip4/172.32.0.1/tcp/4001" , // Not in 172.16-31 range
132- "/ip4/8.8.8.8/tcp/4001" ,
133- },
134- expected : []string {
135- "/ip4/172.32.0.1/tcp/4001" ,
136- "/ip4/8.8.8.8/tcp/4001" ,
137- },
138- },
139- {
140- name : "edge_case_172_16_filtered" ,
141- input : []string {
142- "/ip4/172.16.0.1/tcp/4001" ,
143- "/ip4/8.8.8.8/tcp/4001" ,
144- },
145- expected : []string {
146- "/ip4/8.8.8.8/tcp/4001" ,
147- },
148- },
149- {
150- name : "edge_case_172_31_filtered" ,
151- input : []string {
152- "/ip4/172.31.255.255/tcp/4001" ,
153- "/ip4/8.8.8.8/tcp/4001" ,
154- },
155- expected : []string {
156- "/ip4/8.8.8.8/tcp/4001" ,
157- },
158- },
15991 }
16092
16193 for _ , tt := range tests {
16294 t .Run (tt .name , func (t * testing.T ) {
163- // Convert string multiaddrs to multiaddr.Multiaddr
164- inputAddrs := make ([]multiaddr.Multiaddr , 0 , len (tt .input ))
165- for _ , addrStr := range tt .input {
166- addr , err := multiaddr .NewMultiaddr (addrStr )
167- if err != nil {
168- // Skip invalid addresses in input
169- continue
170- }
171- inputAddrs = append (inputAddrs , addr )
172- }
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+ }
173112
174- // Call filterPrivateAddrs
175- result := filterPrivateAddrs (inputAddrs )
113+ // assertAddrsEqual compares expected string addresses with actual multiaddrs
114+ func assertAddrsEqual (t * testing.T , expected []string , actual []multiaddr.Multiaddr ) {
115+ t .Helper ()
176116
177- // Convert result back to strings for comparison
178- resultStrs := make ([]string , 0 , len (result ))
179- for _ , addr := range result {
180- resultStrs = append (resultStrs , addr .String ())
181- }
117+ actualStrs := make ([]string , len (actual ))
118+ for i , addr := range actual {
119+ actualStrs [i ] = addr .String ()
120+ }
182121
183- // Check length
184- if len (resultStrs ) != len (tt .expected ) {
185- t .Errorf ("Expected %d addresses, got %d.\n Expected: %v\n Got: %v" ,
186- len (tt .expected ), len (resultStrs ), tt .expected , resultStrs )
187- return
188- }
122+ if len (actualStrs ) != len (expected ) {
123+ t .Errorf ("Expected %d addresses, got %d.\n Expected: %v\n Got: %v" ,
124+ len (expected ), len (actualStrs ), expected , actualStrs )
125+ return
126+ }
189127
190- // Check each address
191- for i , expectedAddr := range tt .expected {
192- if resultStrs [i ] != expectedAddr {
193- t .Errorf ("Address %d: expected %s, got %s" , i , expectedAddr , resultStrs [i ])
194- }
195- }
196- })
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+ }
197132 }
198133}
199134
200135func TestIsPrivateIP (t * testing.T ) {
201- tests := []struct {
202- name string
203- ip string
204- expected bool
205- }{
206- // RFC1918 private networks
207- {"10.0.0.1" , "10.0.0.1" , true },
208- {"10.255.255.255" , "10.255.255.255" , true },
209- {"172.16.0.1" , "172.16.0.1" , true },
210- {"172.31.255.255" , "172.31.255.255" , true },
211- {"192.168.1.1" , "192.168.1.1" , true },
212- {"192.168.255.255" , "192.168.255.255" , true },
213-
214- // Edge cases for 172.x range
215- {"172.15.0.1_not_private" , "172.15.0.1" , false },
216- {"172.32.0.1_not_private" , "172.32.0.1" , false },
217-
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" ,
218141 // Link-local
219- {"169.254.1.1" , "169.254.1.1" , true },
220-
142+ "169.254.1.1" ,
221143 // Loopback
222- {"127.0.0.1" , "127.0.0.1" , true },
223- {"127.255.255.255" , "127.255.255.255" , true },
224-
225- // Public IPs
226- {"8.8.8.8" , "8.8.8.8" , false },
227- {"1.1.1.1" , "1.1.1.1" , false },
228- {"208.67.222.222" , "208.67.222.222" , false },
229-
230- // IPv6 unique local (fc00::/7)
231- {"fc00::1" , "fc00::1" , true },
232- {"fd00::1" , "fd00::1" , true },
233-
234- // IPv6 link-local (fe80::/10)
235- {"fe80::1" , "fe80::1" , true },
236-
237- // IPv6 loopback
238- {"::1" , "::1" , true },
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+ }
239148
240- // IPv6 public
241- {"2001:4860:4860::8888" , "2001:4860:4860::8888" , false },
242- {"2606:4700:4700::1111" , "2606:4700:4700::1111" , false },
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" ,
243156 }
244157
245- for _ , tt := range tests {
246- t .Run (tt . name , func (t * testing.T ) {
247- ip := parseIP ( tt . ip )
158+ for _ , ipStr := range privateIPs {
159+ t .Run (ipStr , func (t * testing.T ) {
160+ ip := net . ParseIP ( ipStr )
248161 if ip == nil {
249- t .Fatalf ("Failed to parse IP: %s" , tt . ip )
162+ t .Fatalf ("Failed to parse IP: %s" , ipStr )
250163 }
251- result := isPrivateIP (ip )
252- if result != tt .expected {
253- t .Errorf ("isPrivateIP(%s) = %v, want %v" , tt .ip , result , tt .expected )
164+ if ! isPrivateIP (ip ) {
165+ t .Errorf ("isPrivateIP(%s) = false, want true" , ipStr )
254166 }
255167 })
256168 }
257- }
258169
259- func parseIP (s string ) net.IP {
260- return net .ParseIP (s )
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+ }
261181}
0 commit comments