@@ -320,3 +320,96 @@ func TestFilterResolvDns(t *testing.T) {
320320 }
321321 }
322322}
323+
324+ func TestFilterResolvDnsWithLocalhostOption (t * testing.T ) {
325+ // Test 1: allowLocalhostDNS=false should strip localhost (original behavior)
326+ ns1 := "nameserver 127.0.0.1\n nameserver 10.16.60.14\n "
327+ expected := "nameserver 10.16.60.14\n "
328+ if result , _ := FilterResolvDNSWithLocalhostOption ([]byte (ns1 ), true , false ); result != nil {
329+ if expected != string (result .Content ) {
330+ t .Fatalf ("Failed allowLocalhostDNS=false: expected \n <%s> got \n <%s>" , expected , string (result .Content ))
331+ }
332+ }
333+
334+ // Test 2: allowLocalhostDNS=true should preserve localhost DNS
335+ ns1 = "nameserver 127.0.0.1\n nameserver 10.16.60.14\n "
336+ expected = "nameserver 127.0.0.1\n nameserver 10.16.60.14\n "
337+ if result , _ := FilterResolvDNSWithLocalhostOption ([]byte (ns1 ), true , true ); result != nil {
338+ if expected != string (result .Content ) {
339+ t .Fatalf ("Failed allowLocalhostDNS=true with mixed servers: expected \n <%s> got \n <%s>" , expected , string (result .Content ))
340+ }
341+ }
342+
343+ // Test 3: allowLocalhostDNS=true with only localhost DNS should be preserved
344+ ns1 = "nameserver 127.0.0.1\n "
345+ expected = "nameserver 127.0.0.1\n "
346+ if result , _ := FilterResolvDNSWithLocalhostOption ([]byte (ns1 ), true , true ); result != nil {
347+ if expected != string (result .Content ) {
348+ t .Fatalf ("Failed allowLocalhostDNS=true with only localhost: expected \n <%s> got \n <%s>" , expected , string (result .Content ))
349+ }
350+ }
351+
352+ // Test 4: allowLocalhostDNS=true with IPv6 localhost (::1) should preserve it
353+ ns1 = "nameserver ::1\n nameserver 10.16.60.14\n "
354+ expected = "nameserver ::1\n nameserver 10.16.60.14\n "
355+ if result , _ := FilterResolvDNSWithLocalhostOption ([]byte (ns1 ), true , true ); result != nil {
356+ if expected != string (result .Content ) {
357+ t .Fatalf ("Failed allowLocalhostDNS=true with IPv6 localhost: expected \n <%s> got \n <%s>" , expected , string (result .Content ))
358+ }
359+ }
360+
361+ // Test 5: allowLocalhostDNS=true with 127.0.0.53 (systemd-resolved) should preserve it
362+ ns1 = "nameserver 127.0.0.53\n nameserver 10.16.60.14\n "
363+ expected = "nameserver 127.0.0.53\n nameserver 10.16.60.14\n "
364+ if result , _ := FilterResolvDNSWithLocalhostOption ([]byte (ns1 ), true , true ); result != nil {
365+ if expected != string (result .Content ) {
366+ t .Fatalf ("Failed allowLocalhostDNS=true with 127.0.0.53: expected \n <%s> got \n <%s>" , expected , string (result .Content ))
367+ }
368+ }
369+
370+ // Test 6: allowLocalhostDNS=false should filter localhost even with IPv6 enabled
371+ ns1 = "nameserver 127.0.0.1\n nameserver ::1\n nameserver 10.16.60.14\n nameserver 2002:dead:beef::1\n "
372+ expected = "nameserver 10.16.60.14\n nameserver 2002:dead:beef::1\n "
373+ if result , _ := FilterResolvDNSWithLocalhostOption ([]byte (ns1 ), true , false ); result != nil {
374+ if expected != string (result .Content ) {
375+ t .Fatalf ("Failed allowLocalhostDNS=false with mixed IPv4/IPv6: expected \n <%s> got \n <%s>" , expected , string (result .Content ))
376+ }
377+ }
378+
379+ // Test 7: allowLocalhostDNS=true with IPv6 disabled should strip IPv6 but keep localhost
380+ ns1 = "nameserver 127.0.0.1\n nameserver ::1\n nameserver 10.16.60.14\n nameserver 2002:dead:beef::1\n "
381+ expected = "nameserver 127.0.0.1\n nameserver 10.16.60.14\n "
382+ if result , _ := FilterResolvDNSWithLocalhostOption ([]byte (ns1 ), false , true ); result != nil {
383+ if expected != string (result .Content ) {
384+ t .Fatalf ("Failed allowLocalhostDNS=true with IPv6 disabled: expected \n <%s> got \n <%s>" , expected , string (result .Content ))
385+ }
386+ }
387+
388+ // Test 8: allowLocalhostDNS=true with only localhost and no external DNS, IPv6 enabled
389+ // should preserve localhost even though we would normally add defaults
390+ ns1 = "nameserver 127.0.0.1\n nameserver ::1\n "
391+ expected = "nameserver 127.0.0.1\n nameserver ::1\n "
392+ if result , _ := FilterResolvDNSWithLocalhostOption ([]byte (ns1 ), true , true ); result != nil {
393+ if expected != string (result .Content ) {
394+ t .Fatalf ("Failed allowLocalhostDNS=true with only localhost IPs: expected \n <%s> got \n <%s>" , expected , string (result .Content ))
395+ }
396+ }
397+
398+ // Test 9: allowLocalhostDNS=false with only localhost should fall back to Google DNS (IPv4+IPv6)
399+ ns0 := "\n nameserver 8.8.8.8\n nameserver 8.8.4.4\n nameserver 2001:4860:4860::8888\n nameserver 2001:4860:4860::8844"
400+ ns1 = "nameserver 127.0.0.1\n nameserver ::1\n "
401+ if result , _ := FilterResolvDNSWithLocalhostOption ([]byte (ns1 ), true , false ); result != nil {
402+ if ns0 != string (result .Content ) {
403+ t .Fatalf ("Failed allowLocalhostDNS=false fallback: expected \n <%s> got \n <%s>" , ns0 , string (result .Content ))
404+ }
405+ }
406+
407+ // Test 10: Verify backward compatibility - FilterResolvDNS still filters localhost
408+ ns1 = "nameserver 127.0.0.1\n nameserver 10.16.60.14\n "
409+ expected = "nameserver 10.16.60.14\n "
410+ if result , _ := FilterResolvDNS ([]byte (ns1 ), true ); result != nil {
411+ if expected != string (result .Content ) {
412+ t .Fatalf ("Failed backward compatibility: expected \n <%s> got \n <%s>" , expected , string (result .Content ))
413+ }
414+ }
415+ }
0 commit comments