| 1 | package dns |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | const normal string = ` |
| 11 | # Comment |
| 12 | domain somedomain.com |
| 13 | nameserver 10.28.10.2 |
| 14 | nameserver 11.28.10.1 |
| 15 | ` |
| 16 | |
| 17 | const missingNewline string = ` |
| 18 | domain somedomain.com |
| 19 | nameserver 10.28.10.2 |
| 20 | nameserver 11.28.10.1` // <- NOTE: NO newline. |
| 21 | |
| 22 | func testConfig(t *testing.T, data string) { |
| 23 | cc, err := ClientConfigFromReader(strings.NewReader(data)) |
| 24 | if err != nil { |
| 25 | t.Errorf("error parsing resolv.conf: %v", err) |
| 26 | } |
| 27 | if l := len(cc.Servers); l != 2 { |
| 28 | t.Errorf("incorrect number of nameservers detected: %d", l) |
| 29 | } |
| 30 | if l := len(cc.Search); l != 1 { |
| 31 | t.Errorf("domain directive not parsed correctly: %v", cc.Search) |
| 32 | } else { |
| 33 | if cc.Search[0] != "somedomain.com" { |
| 34 | t.Errorf("domain is unexpected: %v", cc.Search[0]) |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestNameserver(t *testing.T) { testConfig(t, normal) } |
| 40 | func TestMissingFinalNewLine(t *testing.T) { testConfig(t, missingNewline) } |
| 41 | |
| 42 | func TestNdots(t *testing.T) { |
| 43 | ndotsVariants := map[string]int{ |
| 44 | "options ndots:0": 0, |
| 45 | "options ndots:1": 1, |
| 46 | "options ndots:15": 15, |
| 47 | "options ndots:16": 15, |
| 48 | "options ndots:-1": 0, |
| 49 | "": 1, |
| 50 | } |
| 51 | |
| 52 | for data := range ndotsVariants { |
| 53 | cc, err := ClientConfigFromReader(strings.NewReader(data)) |
| 54 | if err != nil { |
| 55 | t.Errorf("error parsing resolv.conf: %v", err) |
| 56 | } |
| 57 | if cc.Ndots != ndotsVariants[data] { |
| 58 | t.Errorf("Ndots not properly parsed: (Expected: %d / Was: %d)", ndotsVariants[data], cc.Ndots) |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestClientConfigFromReaderAttempts(t *testing.T) { |
| 64 | testCases := []struct { |
| 65 | data string |
| 66 | expected int |
| 67 | }{ |
| 68 | {data: "options attempts:0", expected: 1}, |
| 69 | {data: "options attempts:1", expected: 1}, |
| 70 | {data: "options attempts:15", expected: 15}, |
| 71 | {data: "options attempts:16", expected: 16}, |
| 72 | {data: "options attempts:-1", expected: 1}, |
| 73 | {data: "options attempt:", expected: 2}, |
| 74 | } |
| 75 | |
| 76 | for _, test := range testCases { |
| 77 | test := test |
| 78 | t.Run(strings.Replace(test.data, ":", " ", -1), func(t *testing.T) { |
| 79 | t.Parallel() |
| 80 | |
| 81 | cc, err := ClientConfigFromReader(strings.NewReader(test.data)) |
| 82 | if err != nil { |
| 83 | t.Errorf("error parsing resolv.conf: %v", err) |
| 84 | } |
| 85 | if cc.Attempts != test.expected { |
| 86 | t.Errorf("A attempts not properly parsed: (Expected: %d / Was: %d)", test.expected, cc.Attempts) |
| 87 | } |
| 88 | }) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestReadFromFile(t *testing.T) { |
| 93 | tempDir := t.TempDir() |
| 94 | |
| 95 | path := filepath.Join(tempDir, "resolv.conf") |
| 96 | if err := os.WriteFile(path, []byte(normal), 0o644); err != nil { |
| 97 | t.Fatalf("writeFile: %v", err) |
| 98 | } |
| 99 | cc, err := ClientConfigFromFile(path) |
| 100 | if err != nil { |
| 101 | t.Errorf("error parsing resolv.conf: %v", err) |
| 102 | } |
| 103 | if l := len(cc.Servers); l != 2 { |
| 104 | t.Errorf("incorrect number of nameservers detected: %d", l) |
| 105 | } |
| 106 | if l := len(cc.Search); l != 1 { |
| 107 | t.Errorf("domain directive not parsed correctly: %v", cc.Search) |
| 108 | } else { |
| 109 | if cc.Search[0] != "somedomain.com" { |
| 110 | t.Errorf("domain is unexpected: %v", cc.Search[0]) |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func TestNameListNdots1(t *testing.T) { |
| 116 | cfg := ClientConfig{ |
| 117 | Ndots: 1, |
| 118 | } |
| 119 | // fqdn should be only result returned |
| 120 | names := cfg.NameList("miek.nl.") |
| 121 | if len(names) != 1 { |
| 122 | t.Errorf("NameList returned != 1 names: %v", names) |
| 123 | } else if names[0] != "miek.nl." { |
| 124 | t.Errorf("NameList didn't return sent fqdn domain: %v", names[0]) |
| 125 | } |
| 126 | |
| 127 | cfg.Search = []string{ |
| 128 | "test", |
| 129 | } |
| 130 | // Sent domain has NDots and search |
| 131 | names = cfg.NameList("miek.nl") |
| 132 | if len(names) != 2 { |
| 133 | t.Errorf("NameList returned != 2 names: %v", names) |
| 134 | } else if names[0] != "miek.nl." { |
| 135 | t.Errorf("NameList didn't return sent domain first: %v", names[0]) |
| 136 | } else if names[1] != "miek.nl.test." { |
| 137 | t.Errorf("NameList didn't return search last: %v", names[1]) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | func TestNameListNdots2(t *testing.T) { |
| 142 | cfg := ClientConfig{ |
| 143 | Ndots: 2, |
| 144 | } |
| 145 | |
| 146 | // Sent domain has less than NDots and search |
| 147 | cfg.Search = []string{ |
| 148 | "test", |
| 149 | } |
| 150 | names := cfg.NameList("miek.nl") |
| 151 | |
| 152 | if len(names) != 2 { |
| 153 | t.Errorf("NameList returned != 2 names: %v", names) |
| 154 | } else if names[0] != "miek.nl.test." { |
| 155 | t.Errorf("NameList didn't return search first: %v", names[0]) |
| 156 | } else if names[1] != "miek.nl." { |
| 157 | t.Errorf("NameList didn't return sent domain last: %v", names[1]) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | func TestNameListNdots0(t *testing.T) { |
| 162 | cfg := ClientConfig{ |
| 163 | Ndots: 0, |
| 164 | } |
| 165 | cfg.Search = []string{ |
| 166 | "test", |
| 167 | } |
| 168 | // Sent domain has less than NDots and search |
| 169 | names := cfg.NameList("miek") |
| 170 | if len(names) != 2 { |
| 171 | t.Errorf("NameList returned != 2 names: %v", names) |
| 172 | } else if names[0] != "miek." { |
| 173 | t.Errorf("NameList didn't return search first: %v", names[0]) |
| 174 | } else if names[1] != "miek.test." { |
| 175 | t.Errorf("NameList didn't return sent domain last: %v", names[1]) |
| 176 | } |
| 177 | } |