| 1 |
package dnsutil |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/netip" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
func TestAddrReverse(t *testing.T) { |
| 9 |
testcases := []struct { |
| 10 |
reverse string |
| 11 |
addr netip.Addr |
| 12 |
}{ |
| 13 |
{"54.119.58.176.in-addr.arpa.", netip.MustParseAddr("176.58.119.54")}, |
| 14 |
{".58.176.in-addr.arpa.", netip.Addr{}}, |
| 15 |
{"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.in-addr.arpa.", netip.Addr{}}, |
| 16 |
{"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.", netip.MustParseAddr("2001:db8::567:89ab")}, |
| 17 |
{"d.0.1.0.0.2.ip6.arpa.", netip.Addr{}}, |
| 18 |
{"54.119.58.176.ip6.arpa.", netip.Addr{}}, |
| 19 |
{"NONAME", netip.Addr{}}, |
| 20 |
{"", netip.Addr{}}, |
| 21 |
} |
| 22 |
for i, tc := range testcases { |
| 23 |
got := AddrReverse(tc.reverse) |
| 24 |
if got != tc.addr { |
| 25 |
t.Errorf("Test %d, expected '%s', got '%s'", i, tc.addr, got) |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func BenchmarkAddrReverse(b *testing.B) { |
| 31 |
b.Run("IPv4", func(b *testing.B) { |
| 32 |
for i := 0; i < b.N; i++ { |
| 33 |
AddrReverse("54.119.58.176.in-addr.arpa.") |
| 34 |
} |
| 35 |
}) |
| 36 |
b.Run("IPv6", func(b *testing.B) { |
| 37 |
for i := 0; i < b.N; i++ { |
| 38 |
AddrReverse("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.") |
| 39 |
} |
| 40 |
}) |
| 41 |
} |