Debian Codegraph @

caddyconfig/caddyfile/lexer_test.go

raw symbols SCIP
1 // Copyright 2015 Matthew Holt and The Caddy Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package caddyfile
16
17 import (
18 "testing"
19 )
20
21 func TestLexer(t *testing.T) {
22 testCases := []struct {
23 input []byte
24 expected []Token
25 expectErr bool
26 errorMessage string
27 }{
28 {
29 input: []byte(`host:123`),
30 expected: []Token{
31 {Line: 1, Text: "host:123"},
32 },
33 },
34 {
35 input: []byte(`host:123
36
37 directive`),
38 expected: []Token{
39 {Line: 1, Text: "host:123"},
40 {Line: 3, Text: "directive"},
41 },
42 },
43 {
44 input: []byte(`host:123 {
45 directive
46 }`),
47 expected: []Token{
48 {Line: 1, Text: "host:123"},
49 {Line: 1, Text: "{"},
50 {Line: 2, Text: "directive"},
51 {Line: 3, Text: "}"},
52 },
53 },
54 {
55 input: []byte(`host:123 { directive }`),
56 expected: []Token{
57 {Line: 1, Text: "host:123"},
58 {Line: 1, Text: "{"},
59 {Line: 1, Text: "directive"},
60 {Line: 1, Text: "}"},
61 },
62 },
63 {
64 input: []byte(`host:123 {
65 #comment
66 directive
67 # comment
68 foobar # another comment
69 }`),
70 expected: []Token{
71 {Line: 1, Text: "host:123"},
72 {Line: 1, Text: "{"},
73 {Line: 3, Text: "directive"},
74 {Line: 5, Text: "foobar"},
75 {Line: 6, Text: "}"},
76 },
77 },
78 {
79 input: []byte(`host:123 {
80 # hash inside string is not a comment
81 redir / /some/#/path
82 }`),
83 expected: []Token{
84 {Line: 1, Text: "host:123"},
85 {Line: 1, Text: "{"},
86 {Line: 3, Text: "redir"},
87 {Line: 3, Text: "/"},
88 {Line: 3, Text: "/some/#/path"},
89 {Line: 4, Text: "}"},
90 },
91 },
92 {
93 input: []byte("# comment at beginning of file\n# comment at beginning of line\nhost:123"),
94 expected: []Token{
95 {Line: 3, Text: "host:123"},
96 },
97 },
98 {
99 input: []byte(`a "quoted value" b
100 foobar`),
101 expected: []Token{
102 {Line: 1, Text: "a"},
103 {Line: 1, Text: "quoted value"},
104 {Line: 1, Text: "b"},
105 {Line: 2, Text: "foobar"},
106 },
107 },
108 {
109 input: []byte(`A "quoted \"value\" inside" B`),
110 expected: []Token{
111 {Line: 1, Text: "A"},
112 {Line: 1, Text: `quoted "value" inside`},
113 {Line: 1, Text: "B"},
114 },
115 },
116 {
117 input: []byte("An escaped \"newline\\\ninside\" quotes"),
118 expected: []Token{
119 {Line: 1, Text: "An"},
120 {Line: 1, Text: "escaped"},
121 {Line: 1, Text: "newline\\\ninside"},
122 {Line: 2, Text: "quotes"},
123 },
124 },
125 {
126 input: []byte("An escaped newline\\\noutside quotes"),
127 expected: []Token{
128 {Line: 1, Text: "An"},
129 {Line: 1, Text: "escaped"},
130 {Line: 1, Text: "newline"},
131 {Line: 1, Text: "outside"},
132 {Line: 1, Text: "quotes"},
133 },
134 },
135 {
136 input: []byte("line1\\\nescaped\nline2\nline3"),
137 expected: []Token{
138 {Line: 1, Text: "line1"},
139 {Line: 1, Text: "escaped"},
140 {Line: 3, Text: "line2"},
141 {Line: 4, Text: "line3"},
142 },
143 },
144 {
145 input: []byte("line1\\\nescaped1\\\nescaped2\nline4\nline5"),
146 expected: []Token{
147 {Line: 1, Text: "line1"},
148 {Line: 1, Text: "escaped1"},
149 {Line: 1, Text: "escaped2"},
150 {Line: 4, Text: "line4"},
151 {Line: 5, Text: "line5"},
152 },
153 },
154 {
155 input: []byte(`"unescapable\ in quotes"`),
156 expected: []Token{
157 {Line: 1, Text: `unescapable\ in quotes`},
158 },
159 },
160 {
161 input: []byte(`"don't\escape"`),
162 expected: []Token{
163 {Line: 1, Text: `don't\escape`},
164 },
165 },
166 {
167 input: []byte(`"don't\\escape"`),
168 expected: []Token{
169 {Line: 1, Text: `don't\\escape`},
170 },
171 },
172 {
173 input: []byte(`un\escapable`),
174 expected: []Token{
175 {Line: 1, Text: `un\escapable`},
176 },
177 },
178 {
179 input: []byte(`A "quoted value with line
180 break inside" {
181 foobar
182 }`),
183 expected: []Token{
184 {Line: 1, Text: "A"},
185 {Line: 1, Text: "quoted value with line\n\t\t\t\t\tbreak inside"},
186 {Line: 2, Text: "{"},
187 {Line: 3, Text: "foobar"},
188 {Line: 4, Text: "}"},
189 },
190 },
191 {
192 input: []byte(`"C:\php\php-cgi.exe"`),
193 expected: []Token{
194 {Line: 1, Text: `C:\php\php-cgi.exe`},
195 },
196 },
197 {
198 input: []byte(`empty "" string`),
199 expected: []Token{
200 {Line: 1, Text: `empty`},
201 {Line: 1, Text: ``},
202 {Line: 1, Text: `string`},
203 },
204 },
205 {
206 input: []byte("skip those\r\nCR characters"),
207 expected: []Token{
208 {Line: 1, Text: "skip"},
209 {Line: 1, Text: "those"},
210 {Line: 2, Text: "CR"},
211 {Line: 2, Text: "characters"},
212 },
213 },
214 {
215 input: []byte("\xEF\xBB\xBF:8080"), // test with leading byte order mark
216 expected: []Token{
217 {Line: 1, Text: ":8080"},
218 },
219 },
220 {
221 input: []byte("simple `backtick quoted` string"),
222 expected: []Token{
223 {Line: 1, Text: `simple`},
224 {Line: 1, Text: `backtick quoted`},
225 {Line: 1, Text: `string`},
226 },
227 },
228 {
229 input: []byte("multiline `backtick\nquoted\n` string"),
230 expected: []Token{
231 {Line: 1, Text: `multiline`},
232 {Line: 1, Text: "backtick\nquoted\n"},
233 {Line: 3, Text: `string`},
234 },
235 },
236 {
237 input: []byte("nested `\"quotes inside\" backticks` string"),
238 expected: []Token{
239 {Line: 1, Text: `nested`},
240 {Line: 1, Text: `"quotes inside" backticks`},
241 {Line: 1, Text: `string`},
242 },
243 },
244 {
245 input: []byte("reverse-nested \"`backticks` inside\" quotes"),
246 expected: []Token{
247 {Line: 1, Text: `reverse-nested`},
248 {Line: 1, Text: "`backticks` inside"},
249 {Line: 1, Text: `quotes`},
250 },
251 },
252 {
253 input: []byte(`heredoc <<EOF
254 content
255 EOF same-line-arg
256 `),
257 expected: []Token{
258 {Line: 1, Text: `heredoc`},
259 {Line: 1, Text: "content"},
260 {Line: 3, Text: `same-line-arg`},
261 },
262 },
263 {
264 input: []byte(`heredoc <<VERY-LONG-MARKER
265 content
266 VERY-LONG-MARKER same-line-arg
267 `),
268 expected: []Token{
269 {Line: 1, Text: `heredoc`},
270 {Line: 1, Text: "content"},
271 {Line: 3, Text: `same-line-arg`},
272 },
273 },
274 {
275 input: []byte(`heredoc <<EOF
276 extra-newline
277
278 EOF same-line-arg
279 `),
280 expected: []Token{
281 {Line: 1, Text: `heredoc`},
282 {Line: 1, Text: "extra-newline\n"},
283 {Line: 4, Text: `same-line-arg`},
284 },
285 },
286 {
287 input: []byte(`heredoc <<EOF
288 EOF
289 HERE same-line-arg
290 `),
291 expected: []Token{
292 {Line: 1, Text: `heredoc`},
293 {Line: 1, Text: ``},
294 {Line: 3, Text: `HERE`},
295 {Line: 3, Text: `same-line-arg`},
296 },
297 },
298 {
299 input: []byte(`heredoc <<EOF
300 EOF same-line-arg
301 `),
302 expected: []Token{
303 {Line: 1, Text: `heredoc`},
304 {Line: 1, Text: ""},
305 {Line: 2, Text: `same-line-arg`},
306 },
307 },
308 {
309 input: []byte(`heredoc <<EOF
310 content
311 EOF same-line-arg
312 `),
313 expected: []Token{
314 {Line: 1, Text: `heredoc`},
315 {Line: 1, Text: "content"},
316 {Line: 3, Text: `same-line-arg`},
317 },
318 },
319 {
320 input: []byte(`prev-line
321 heredoc <<EOF
322 multi
323 line
324 content
325 EOF same-line-arg
326 next-line
327 `),
328 expected: []Token{
329 {Line: 1, Text: `prev-line`},
330 {Line: 2, Text: `heredoc`},
331 {Line: 2, Text: "\tmulti\n\tline\n\tcontent"},
332 {Line: 6, Text: `same-line-arg`},
333 {Line: 7, Text: `next-line`},
334 },
335 },
336 {
337 input: []byte(`escaped-heredoc \<< >>`),
338 expected: []Token{
339 {Line: 1, Text: `escaped-heredoc`},
340 {Line: 1, Text: `<<`},
341 {Line: 1, Text: `>>`},
342 },
343 },
344 {
345 input: []byte(`not-a-heredoc <EOF
346 content
347 `),
348 expected: []Token{
349 {Line: 1, Text: `not-a-heredoc`},
350 {Line: 1, Text: `<EOF`},
351 {Line: 2, Text: `content`},
352 },
353 },
354 {
355 input: []byte(`not-a-heredoc <<<EOF content`),
356 expected: []Token{
357 {Line: 1, Text: `not-a-heredoc`},
358 {Line: 1, Text: `<<<EOF`},
359 {Line: 1, Text: `content`},
360 },
361 },
362 {
363 input: []byte(`not-a-heredoc "<<" ">>"`),
364 expected: []Token{
365 {Line: 1, Text: `not-a-heredoc`},
366 {Line: 1, Text: `<<`},
367 {Line: 1, Text: `>>`},
368 },
369 },
370 {
371 input: []byte(`not-a-heredoc << >>`),
372 expected: []Token{
373 {Line: 1, Text: `not-a-heredoc`},
374 {Line: 1, Text: `<<`},
375 {Line: 1, Text: `>>`},
376 },
377 },
378 {
379 input: []byte(`not-a-heredoc <<HERE SAME LINE
380 content
381 HERE same-line-arg
382 `),
383 expected: []Token{
384 {Line: 1, Text: `not-a-heredoc`},
385 {Line: 1, Text: `<<HERE`},
386 {Line: 1, Text: `SAME`},
387 {Line: 1, Text: `LINE`},
388 {Line: 2, Text: `content`},
389 {Line: 3, Text: `HERE`},
390 {Line: 3, Text: `same-line-arg`},
391 },
392 },
393 {
394 input: []byte(`heredoc <<s
395 �
396 s
397 `),
398 expected: []Token{
399 {Line: 1, Text: `heredoc`},
400 {Line: 1, Text: "�"},
401 },
402 },
403 {
404 input: []byte("\u000Aheredoc \u003C\u003C\u0073\u0073\u000A\u00BF\u0057\u0001\u0000\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u003D\u001F\u000A\u0073\u0073\u000A\u00BF\u0057\u0001\u0000\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u003D\u001F\u000A\u00BF\u00BF\u0057\u0001\u0000\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u003D\u001F"),
405 expected: []Token{
406 {
407 Line: 2,
408 Text: "heredoc",
409 },
410 {
411 Line: 2,
412 Text: "\u00BF\u0057\u0001\u0000\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u003D\u001F",
413 },
414 {
415 Line: 5,
416 Text: "\u00BF\u0057\u0001\u0000\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u003D\u001F",
417 },
418 {
419 Line: 6,
420 Text: "\u00BF\u00BF\u0057\u0001\u0000\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u003D\u001F",
421 },
422 },
423 },
424 {
425 input: []byte("not-a-heredoc <<\n"),
426 expectErr: true,
427 errorMessage: "missing opening heredoc marker on line #1; must contain only alphanumeric characters, dashes and underscores; got empty string",
428 },
429 {
430 input: []byte(`heredoc <<<EOF
431 content
432 EOF same-line-arg
433 `),
434 expectErr: true,
435 errorMessage: "too many '<' for heredoc on line #1; only use two, for example <<END",
436 },
437 {
438 input: []byte(`heredoc <<EOF
439 content
440 `),
441 expectErr: true,
442 errorMessage: "incomplete heredoc <<EOF on line #3, expected ending marker EOF",
443 },
444 {
445 input: []byte(`heredoc <<EOF
446 content
447 EOF
448 `),
449 expectErr: true,
450 errorMessage: "mismatched leading whitespace in heredoc <<EOF on line #2 [\tcontent], expected whitespace [\t\t] to match the closing marker",
451 },
452 {
453 input: []byte(`heredoc <<EOF
454 content
455 EOF
456 `),
457 expectErr: true,
458 errorMessage: "mismatched leading whitespace in heredoc <<EOF on line #2 [ content], expected whitespace [\t\t] to match the closing marker",
459 },
460 {
461 input: []byte(`heredoc <<EOF
462 The next line is a blank line
463
464 The previous line is a blank line
465 EOF`),
466 expected: []Token{
467 {Line: 1, Text: "heredoc"},
468 {Line: 1, Text: "The next line is a blank line\n\nThe previous line is a blank line"},
469 },
470 },
471 {
472 input: []byte(`heredoc <<EOF
473 One tab indented heredoc with blank next line
474
475 One tab indented heredoc with blank previous line
476 EOF`),
477 expected: []Token{
478 {Line: 1, Text: "heredoc"},
479 {Line: 1, Text: "One tab indented heredoc with blank next line\n\nOne tab indented heredoc with blank previous line"},
480 },
481 },
482 {
483 input: []byte(`heredoc <<EOF
484 The next line is a blank line with one tab
485
486 The previous line is a blank line with one tab
487 EOF`),
488 expected: []Token{
489 {Line: 1, Text: "heredoc"},
490 {Line: 1, Text: "The next line is a blank line with one tab\n\t\nThe previous line is a blank line with one tab"},
491 },
492 },
493 {
494 input: []byte(`heredoc <<EOF
495 The next line is a blank line with one tab less than the correct indentation
496
497 The previous line is a blank line with one tab less than the correct indentation
498 EOF`),
499 expectErr: true,
500 errorMessage: "mismatched leading whitespace in heredoc <<EOF on line #3 [\t], expected whitespace [\t\t] to match the closing marker",
501 },
502 }
503
504 for i, testCase := range testCases {
505 actual, err := Tokenize(testCase.input, "")
506 if testCase.expectErr {
507 if err == nil {
508 t.Fatalf("expected error, got actual: %v", actual)
509 continue
510 }
511 if err.Error() != testCase.errorMessage {
512 t.Fatalf("expected error '%v', got: %v", testCase.errorMessage, err)
513 }
514 continue
515 }
516
517 if err != nil {
518 t.Fatalf("%v", err)
519 }
520 lexerCompare(t, i, testCase.expected, actual)
521 }
522 }
523
524 func lexerCompare(t *testing.T, n int, expected, actual []Token) {
525 if len(expected) != len(actual) {
526 t.Fatalf("Test case %d: expected %d token(s) but got %d", n, len(expected), len(actual))
527 }
528
529 for i := 0; i < len(actual) && i < len(expected); i++ {
530 if actual[i].Line != expected[i].Line {
531 t.Fatalf("Test case %d token %d ('%s'): expected line %d but was line %d",
532 n, i, expected[i].Text, expected[i].Line, actual[i].Line)
533 break
534 }
535 if actual[i].Text != expected[i].Text {
536 t.Fatalf("Test case %d token %d: expected text '%s' but was '%s'",
537 n, i, expected[i].Text, actual[i].Text)
538 break
539 }
540 }
541 }