| 1 | package dnsutil |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestAddOrigin(t *testing.T) { |
| 6 | var tests = []struct{ e1, e2, expected string }{ |
| 7 | {"@", "example.com", "example.com"}, |
| 8 | {"foo", "example.com", "foo.example.com"}, |
| 9 | {"foo.", "example.com", "foo."}, |
| 10 | {"@", "example.com.", "example.com."}, |
| 11 | {"foo", "example.com.", "foo.example.com."}, |
| 12 | {"foo.", "example.com.", "foo."}, |
| 13 | {"example.com", ".", "example.com."}, |
| 14 | {"example.com.", ".", "example.com."}, |
| 15 | // Oddball tests: |
| 16 | // In general origin should not be "" or "." but at least |
| 17 | // these tests verify we don't crash and will keep results |
| 18 | // from changing unexpectedly. |
| 19 | {"*.", "", "*."}, |
| 20 | {"@", "", "@"}, |
| 21 | {"foobar", "", "foobar"}, |
| 22 | {"foobar.", "", "foobar."}, |
| 23 | {"*.", ".", "*."}, |
| 24 | {"@", ".", "."}, |
| 25 | {"foobar", ".", "foobar."}, |
| 26 | {"foobar.", ".", "foobar."}, |
| 27 | } |
| 28 | for _, test := range tests { |
| 29 | actual := AddOrigin(test.e1, test.e2) |
| 30 | if test.expected != actual { |
| 31 | t.Errorf("AddOrigin(%#v, %#v) expected %#v, got %#v\n", test.e1, test.e2, test.expected, actual) |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func TestTrimDomainName(t *testing.T) { |
| 37 | // Basic tests. |
| 38 | // Try trimming "example.com" and "example.com." from typical use cases. |
| 39 | testsEx := []struct{ experiment, expected string }{ |
| 40 | {"foo.example.com", "foo"}, |
| 41 | {"foo.example.com.", "foo"}, |
| 42 | {".foo.example.com", ".foo"}, |
| 43 | {".foo.example.com.", ".foo"}, |
| 44 | {"*.example.com", "*"}, |
| 45 | {"example.com", "@"}, |
| 46 | {"example.com.", "@"}, |
| 47 | {"com.", "com."}, |
| 48 | {"foo.", "foo."}, |
| 49 | {"serverfault.com.", "serverfault.com."}, |
| 50 | {"serverfault.com", "serverfault.com"}, |
| 51 | {".foo.ronco.com", ".foo.ronco.com"}, |
| 52 | {".foo.ronco.com.", ".foo.ronco.com."}, |
| 53 | } |
| 54 | for _, dom := range []string{"example.com", "example.com."} { |
| 55 | for i, test := range testsEx { |
| 56 | actual := TrimDomainName(test.experiment, dom) |
| 57 | if test.expected != actual { |
| 58 | t.Errorf("%d TrimDomainName(%#v, %#v): expected %v, got %v\n", i, test.experiment, dom, test.expected, actual) |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // Paranoid tests. |
| 64 | // These test shouldn't be needed but I was weary of off-by-one errors. |
| 65 | // In theory, these can't happen because there are no single-letter TLDs, |
| 66 | // but it is good to exercise the code this way. |
| 67 | tests := []struct{ experiment, expected string }{ |
| 68 | {"", "@"}, |
| 69 | {".", "."}, |
| 70 | {"a.b.c.d.e.f.", "a.b.c.d.e"}, |
| 71 | {"b.c.d.e.f.", "b.c.d.e"}, |
| 72 | {"c.d.e.f.", "c.d.e"}, |
| 73 | {"d.e.f.", "d.e"}, |
| 74 | {"e.f.", "e"}, |
| 75 | {"f.", "@"}, |
| 76 | {".a.b.c.d.e.f.", ".a.b.c.d.e"}, |
| 77 | {".b.c.d.e.f.", ".b.c.d.e"}, |
| 78 | {".c.d.e.f.", ".c.d.e"}, |
| 79 | {".d.e.f.", ".d.e"}, |
| 80 | {".e.f.", ".e"}, |
| 81 | {".f.", "@"}, |
| 82 | {"a.b.c.d.e.f", "a.b.c.d.e"}, |
| 83 | {"a.b.c.d.e.", "a.b.c.d.e."}, |
| 84 | {"a.b.c.d.e", "a.b.c.d.e"}, |
| 85 | {"a.b.c.d.", "a.b.c.d."}, |
| 86 | {"a.b.c.d", "a.b.c.d"}, |
| 87 | {"a.b.c.", "a.b.c."}, |
| 88 | {"a.b.c", "a.b.c"}, |
| 89 | {"a.b.", "a.b."}, |
| 90 | {"a.b", "a.b"}, |
| 91 | {"a.", "a."}, |
| 92 | {"a", "a"}, |
| 93 | {".a.b.c.d.e.f", ".a.b.c.d.e"}, |
| 94 | {".a.b.c.d.e.", ".a.b.c.d.e."}, |
| 95 | {".a.b.c.d.e", ".a.b.c.d.e"}, |
| 96 | {".a.b.c.d.", ".a.b.c.d."}, |
| 97 | {".a.b.c.d", ".a.b.c.d"}, |
| 98 | {".a.b.c.", ".a.b.c."}, |
| 99 | {".a.b.c", ".a.b.c"}, |
| 100 | {".a.b.", ".a.b."}, |
| 101 | {".a.b", ".a.b"}, |
| 102 | {".a.", ".a."}, |
| 103 | {".a", ".a"}, |
| 104 | } |
| 105 | for _, dom := range []string{"f", "f."} { |
| 106 | for i, test := range tests { |
| 107 | actual := TrimDomainName(test.experiment, dom) |
| 108 | if test.expected != actual { |
| 109 | t.Errorf("%d TrimDomainName(%#v, %#v): expected %v, got %v\n", i, test.experiment, dom, test.expected, actual) |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Test cases for bugs found in the wild. |
| 115 | // These test cases provide both origin, s, and the expected result. |
| 116 | // If you find a bug in the while, this is probably the easiest place |
| 117 | // to add it as a test case. |
| 118 | var testsWild = []struct{ e1, e2, expected string }{ |
| 119 | {"mathoverflow.net.", ".", "mathoverflow.net"}, |
| 120 | {"mathoverflow.net", ".", "mathoverflow.net"}, |
| 121 | {"", ".", "@"}, |
| 122 | {"@", ".", "@"}, |
| 123 | } |
| 124 | for i, test := range testsWild { |
| 125 | actual := TrimDomainName(test.e1, test.e2) |
| 126 | if test.expected != actual { |
| 127 | t.Errorf("%d TrimDomainName(%#v, %#v): expected %v, got %v\n", i, test.e1, test.e2, test.expected, actual) |
| 128 | } |
| 129 | } |
| 130 | } |