|
| 1 | +package dns |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestAddressScope(t *testing.T) { |
| 6 | + for _, tc := range []struct{ addr, want string }{ |
| 7 | + {"127.0.0.1", ScopeLoopback}, |
| 8 | + {"127.10.0.5", ScopeLoopback}, |
| 9 | + {"::1", ScopeLoopback}, |
| 10 | + {"0.0.0.0", ScopeUnspecified}, |
| 11 | + {"::", ScopeUnspecified}, |
| 12 | + {"169.254.169.254", ScopeLinkLocal}, |
| 13 | + {"fe80::1", ScopeLinkLocal}, |
| 14 | + {"10.0.0.1", ScopePrivate}, |
| 15 | + {"172.16.0.1", ScopePrivate}, |
| 16 | + {"172.31.255.255", ScopePrivate}, |
| 17 | + {"192.168.1.1", ScopePrivate}, |
| 18 | + {"fd00::1", ScopePrivate}, |
| 19 | + {"::ffff:192.168.1.1", ScopePrivate}, |
| 20 | + {"100.64.0.1", ScopeCGNAT}, |
| 21 | + {"100.127.255.255", ScopeCGNAT}, |
| 22 | + {"100.128.0.1", ""}, |
| 23 | + {"172.32.0.1", ""}, |
| 24 | + {"8.8.8.8", ""}, |
| 25 | + {"192.0.2.1", ""}, |
| 26 | + {"2001:db8::1", ""}, |
| 27 | + {"target.example.com", ""}, |
| 28 | + {"", ""}, |
| 29 | + } { |
| 30 | + if got := AddressScope(tc.addr); got != tc.want { |
| 31 | + t.Errorf("AddressScope(%q) = %q, want %q", tc.addr, got, tc.want) |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestInternalScope(t *testing.T) { |
| 37 | + for _, tc := range []struct { |
| 38 | + name string |
| 39 | + records []Record |
| 40 | + want string |
| 41 | + }{ |
| 42 | + {"none", nil, ""}, |
| 43 | + {"public", []Record{{Type: "A", Value: "192.0.2.1"}, {Type: "AAAA", Value: "2001:db8::1"}}, ""}, |
| 44 | + {"private v4", []Record{{Type: "A", Value: "192.0.2.1"}, {Type: "A", Value: "10.1.2.3"}}, ScopePrivate}, |
| 45 | + {"private v6", []Record{{Type: "AAAA", Value: "fd12::1"}}, ScopePrivate}, |
| 46 | + {"loopback wins", []Record{{Type: "A", Value: "10.1.2.3"}, {Type: "A", Value: "127.0.0.1"}}, ScopeLoopback}, |
| 47 | + {"link-local over private", []Record{{Type: "A", Value: "10.1.2.3"}, {Type: "AAAA", Value: "fe80::1"}}, ScopeLinkLocal}, |
| 48 | + {"cname value ignored", []Record{{Type: "CNAME", Value: "10.0.0.1"}}, ""}, |
| 49 | + } { |
| 50 | + if got := InternalScope(tc.records); got != tc.want { |
| 51 | + t.Errorf("%s: InternalScope = %q, want %q", tc.name, got, tc.want) |
| 52 | + } |
| 53 | + } |
| 54 | +} |
0 commit comments