github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/dnsproxy/lookup_test.go (about) 1 package dnsproxy 2 3 import ( 4 "context" 5 "net" 6 "reflect" 7 "runtime" 8 "testing" 9 "time" 10 11 "github.com/miekg/dns" 12 "github.com/stretchr/testify/require" 13 14 "github.com/datawire/dlib/dlog" 15 "github.com/telepresenceio/telepresence/v2/pkg/iputil" 16 ) 17 18 func TestLookup(t *testing.T) { 19 type tType struct { 20 qType uint16 21 qName string 22 } 23 tests := []tType{ 24 { 25 dns.TypeA, 26 "google.com.", 27 }, 28 { 29 dns.TypeCNAME, 30 "_smpp_._tcp.golang.org.", 31 }, 32 { 33 dns.TypePTR, 34 "78.217.250.142.in-addr.arpa.", 35 }, 36 { 37 dns.TypeMX, 38 "gmail.com.", 39 }, 40 { 41 dns.TypeTXT, 42 "dns.google.", 43 }, 44 { 45 dns.TypeSRV, 46 "_myservice._tcp.tada.se.", 47 }, 48 } 49 // AAAA returns an error on Windows: 50 // "getaddrinfow: The requested name is valid, but no data of the requested type was found" 51 if runtime.GOOS != "windows" { 52 tests = append(tests, tType{ 53 dns.TypeAAAA, 54 "google.com.", 55 }) 56 } 57 for _, tt := range tests { 58 t.Run(dns.TypeToString[tt.qType], func(t *testing.T) { 59 if tt.qType == dns.TypeSRV && runtime.GOOS == "darwin" { 60 t.Skip("SRV sporadically fails to parse reply on darwin") 61 } 62 ctx := dlog.NewTestContext(t, false) 63 got, _, err := Lookup(ctx, tt.qType, tt.qName) 64 require.NoError(t, err) 65 require.Greater(t, len(got), 0) 66 }) 67 } 68 } 69 70 func TestPtrAddress_v4(t *testing.T) { 71 ip, err := PtrAddress("32.127.168.192.in-addr.arpa.") 72 require.NoError(t, err) 73 require.Equal(t, net.IP{192, 168, 127, 32}, ip) 74 } 75 76 func TestPtrAddress_v6(t *testing.T) { 77 ip, err := PtrAddress("b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.") 78 require.NoError(t, err) 79 require.Equal(t, iputil.Parse("2001:db8::567:89ab"), ip) 80 } 81 82 func TestTimedExternalLookup(t *testing.T) { 83 t.Skip("needs some more love, the diff between lookups is not predicable") 84 names := []string{ 85 "google.com", 86 } 87 for _, name := range names { 88 t.Run(name, func(t *testing.T) { 89 wips, err := net.LookupIP(name) 90 require.NoError(t, err) 91 want := iputil.UniqueSorted(wips) 92 got := TimedExternalLookup(context.Background(), name, 2*time.Second).UniqueSorted() 93 if !reflect.DeepEqual(got, want) { 94 t.Errorf("TimedExternalLookup() = %v, want %v", got, want) 95 } 96 }) 97 } 98 }