File: test_lexer.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (540 lines) | stat: -rw-r--r-- 17,385 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
 * Copyright (c) 2013 Balabit
 * Copyright (c) 2013 Gergely Nagy <algernon@balabit.hu>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */

#include <criterion/criterion.h>
#include "libtest/mock-cfg-parser.h"
#include "libtest/grab-logging.h"

#include "cfg-lexer.h"
#include "cfg-grammar.h"
#include "apphook.h"

#define TESTDATA_DIR TOP_SRCDIR "/lib/tests/testdata-lexer"


CfgParserMock *parser = NULL;


static void
_input(const gchar *input)
{
  cfg_parser_mock_input(parser, input);
}

static void
_next_token(void)
{
  cfg_parser_mock_next_token(parser);
}

static CFG_STYPE *
_current_token(void)
{
  return parser->yylval;
}

static CFG_LTYPE *
_current_lloc(void)
{
  return parser->yylloc;
}

#define assert_token_type(expected)                                     \
  cr_assert_eq(_current_token()->type, expected, "Unexpected token type %d != %d", _current_token()->type, expected);

#define assert_parser_string(expected)                          \
  _next_token();                                                        \
  assert_token_type(LL_STRING);                                        \
  cr_assert_str_eq(_current_token()->cptr, expected, "Unexpected string value parsed >>>%s<<< != >>>%s<<<", _current_token()->cptr, expected);

#define assert_parser_token(expected)                          \
  _next_token();                                                        \
  assert_token_type(expected);

#define assert_parser_block(expected) \
  _next_token();                                                        \
  assert_token_type(LL_BLOCK);                                         \
  cr_assert_str_eq(_current_token()->cptr, expected, "Unexpected block value parsed >>>%s<<< != >>>%s<<<", _current_token()->cptr, expected);

#define assert_parser_error() \
  _next_token();                                                        \
  assert_token_type(LL_ERROR);

#define assert_parser_pragma() \
  _next_token();                                                        \
  assert_token_type(LL_PRAGMA);

#define assert_parser_number(expected) \
  _next_token();                                                        \
  assert_token_type(LL_NUMBER);                                        \
  cr_assert_eq(_current_token()->num, expected, "Unexpected number parsed %" G_GINT64_FORMAT " != %" G_GINT64_FORMAT, (gint64) _current_token()->num, (gint64) expected);

#define assert_parser_float(expected)                           \
  _next_token();                                                        \
  assert_token_type(LL_FLOAT);                                         \
  cr_assert_float_eq(_current_token()->fnum, expected, 1e-7, "Unexpected float parsed %lf != %lf", _current_token()->fnum, expected);

#define assert_parser_identifier(expected) \
  _next_token();                                                        \
  assert_token_type(LL_IDENTIFIER);                                         \
  cr_assert_str_eq(_current_token()->cptr, expected, "Unexpected identifier parsed >>>%s<<< != >>>%s<<<", _current_token()->cptr, expected);

#define assert_parser_char(expected) \
  _next_token();                                                        \
  cr_assert_eq(_current_token()->type, expected, "Unexpected character parsed %c != %c", _current_token()->type, expected);

#define assert_location(line, column) \
  cr_assert_eq(_current_lloc()->first_line, line,          \
              "The line number in the location information "        \
              "does not match the expected value, %d != %d", _current_lloc()->first_line, line);  \
  cr_assert_eq(_current_lloc()->first_column, column,          \
              "The column number in the location information "        \
              "does not match the expected value, %d != %d", _current_lloc()->first_column, column);

#define assert_location_range(_first_line, _first_column, _last_line, _last_column) \
  assert_location(_first_line, _first_column); \
  cr_assert_eq(_current_lloc()->last_line, _last_line,          \
              "The last_line number in the location information "        \
              "does not match the expected value, %d != %d", _current_lloc()->last_line, _last_line);  \
  cr_assert_eq(_current_lloc()->last_column, _last_column,          \
              "The last_column number in the location information "        \
              "does not match the expected value, %d != %d", _current_lloc()->last_column, _last_column);


static gchar *
_format_location_tag_message(void)
{
  EVTCONTEXT *ctx;
  EVTREC *rec;
  char *msg;

  ctx = evt_ctx_init("test", EVT_FAC_USER);
  rec = evt_rec_init(ctx, EVT_PRI_WARNING, "test");
  evt_rec_add_tag(rec, cfg_lexer_format_location_tag(parser->lexer, _current_lloc()));
  msg = evt_format(rec);
  evt_rec_free(rec);
  evt_ctx_free(ctx);

  return msg;
}

#define assert_location_tag(expected) \
  ({                      \
    char *msg = _format_location_tag_message(); \
    const gchar *tag_repr;        \
    tag_repr = strstr(msg, "; ");                 \
    tag_repr = tag_repr ? tag_repr + 2 : NULL;    \
    cr_assert_str_eq(tag_repr, expected, "Formatted location tag does not match %s <> %s", tag_repr, expected);  \
    free(msg);                    \
                                                                                        \
  })

Test(lexer, test_string)
{
  _input("\"test\"");
  assert_parser_string("test");
  _input("\"test\\x0a\"");
  assert_parser_string("test\n");
  _input("\"test\\o011\"");
  assert_parser_string("test\t");
  _input("\"test\\n\\r\\a\\t\\v\\c\"");
  assert_parser_string("test\n\r\a\t\vc");
}

Test(lexer, test_unquoted_string)
{
  _input("test");
  assert_parser_identifier("test");

  _input("..test");
  assert_parser_token(LL_DOTDOT);

  _input(".test");
  assert_parser_token('.');
  assert_parser_identifier("test");
}

Test(lexer, test_qstring)
{
  _input("'test'");
  assert_parser_string("test");
  _input("'\"test\\n\\r\"'");
  assert_parser_string("\"test\\n\\r\"");
}

Test(lexer, block_token_is_taken_literally_between_a_pair_of_enclosing_characters)
{
  _input(" { hello }");
  cfg_lexer_start_block_state(parser->lexer, "{}");
  assert_parser_block(" hello ");
  assert_location_range(1, 1, 1, 11);

  _input(" { hello\nworld }");
  cfg_lexer_start_block_state(parser->lexer, "{}");
  assert_parser_block(" hello\nworld ");
  assert_location_range(1, 1, 2, 8);

  _input(" { hello\\\nworld }");
  cfg_lexer_start_block_state(parser->lexer, "{}");
  assert_parser_block(" hello\\\nworld ");
  assert_location_range(1, 1, 2, 8);

  _input(" { 'hello' }");
  cfg_lexer_start_block_state(parser->lexer, "{}");
  assert_parser_block(" 'hello' ");
  assert_location_range(1, 1, 1, 13);

  _input(" { 'hello\nworld' }");
  cfg_lexer_start_block_state(parser->lexer, "{}");
  assert_parser_block(" 'hello\nworld' ");
  assert_location_range(1, 1, 2, 9);

  _input(" { 'hello\\\nworld' }");
  cfg_lexer_start_block_state(parser->lexer, "{}");
  assert_parser_block(" 'hello\\\nworld' ");
  assert_location_range(1, 1, 2, 9);

  _input(" { \"hello\" }");
  cfg_lexer_start_block_state(parser->lexer, "{}");
  assert_parser_block(" \"hello\" ");
  assert_location_range(1, 1, 1, 13);

  _input(" { \"hello\nworld\" }");
  cfg_lexer_start_block_state(parser->lexer, "{}");
  assert_parser_block(" \"hello\nworld\" ");
  assert_location_range(1, 1, 2, 9);

  _input(" { \"hello\\\nworld\" }");
  cfg_lexer_start_block_state(parser->lexer, "{}");
  assert_parser_block(" \"hello\\\nworld\" ");
  assert_location_range(1, 1, 2, 9);

  _input(" { \"hello \\a\\n\\r\\t\\v\\x40\\o100 world\" }");
  cfg_lexer_start_block_state(parser->lexer, "{}");
  assert_parser_block(" \"hello \\a\\n\\r\\t\\v\\x40\\o100 world\" ");
  assert_location_range(1, 1, 1, 39);

  _input(" { \"hello \\\" world\" }");
  cfg_lexer_start_block_state(parser->lexer, "{}");
  assert_parser_block(" \"hello \\\" world\" ");
  assert_location_range(1, 1, 1, 22);
}

Test(lexer, block_new_lines_in_text_leading_to_the_opening_bracket_are_tracked_properly)
{
  _input("\n\n\n{ \"hello\\\nworld\" }");
  cfg_lexer_start_block_state(parser->lexer, "{}");
  assert_parser_block(" \"hello\\\nworld\" ");
  assert_location_range(4, 1, 5, 9);

}

Test(lexer, block_closing_brackets_in_embedded_strings_or_comments_dont_close_the_block)
{
  _input(" { 'hello}' }");
  cfg_lexer_start_block_state(parser->lexer, "{}");
  assert_parser_block(" 'hello}' ");

  _input(" { \"hello}\" }");
  cfg_lexer_start_block_state(parser->lexer, "{}");
  assert_parser_block(" \"hello}\" ");

  _input(" {\nfoo# hello}\nbar\n}");
  cfg_lexer_start_block_state(parser->lexer, "{}");
  assert_parser_block("\nfoo# hello}\nbar\n");
}

Test(lexer, block_imbalanced_closing_dont_close_the_block)
{
  _input(" {foo {hello} bar}");
  cfg_lexer_start_block_state(parser->lexer, "{}");
  assert_parser_block("foo {hello} bar");
}

Test(lexer, block_complex_input)
{
  _input("\t \n{'hello world' \"test value\" {other_block} other\text}");
  cfg_lexer_start_block_state(parser->lexer, "{}");
  assert_parser_block("'hello world' \"test value\" {other_block} other\text");
}

Test(lexer, block_input_without_opening_character_is_reported_as_an_error)
{
  start_grabbing_messages();
  _input("this is a block that does not start with an opening brace");
  cfg_lexer_start_block_state(parser->lexer, "{}");
  assert_parser_error();
  assert_grabbed_log_contains("Expected opening bracket as block boundary");
  stop_grabbing_messages();
}

Test(lexer, block_empty_input_in_parens_is_processed_as_a_NULL_pointer)
{
  _input("()");
  cfg_lexer_start_block_state(parser->lexer, "()");
  _next_token();
  assert_token_type(LL_BLOCK);
  cr_assert(_current_token()->cptr == NULL, "%p", _current_token()->cptr);
}

Test(lexer, block_empty_string_in_parens_input_is_processed_as_an_empty_string)
{
  _input("('')");
  cfg_lexer_start_block_state(parser->lexer, "()");
  assert_parser_block("");

  _input("(\"\")");
  cfg_lexer_start_block_state(parser->lexer, "()");
  assert_parser_block("");
}

Test(lexer, at_version_stores_config_version_in_parsed_version_in_hex_form)
{
  parser->lexer->ignore_pragma = FALSE;

  start_grabbing_messages();
  cfg_set_version_without_validation(configuration, 0);
  _input("@version: 3.1\n\
bar\n");
  assert_parser_identifier("bar");
  cr_assert_eq(configuration->user_version, 0x0301,
               "@version parsing mismatch, value %04x expected %04x", configuration->user_version, 0x0301);

  assert_grabbed_log_contains("Configuration file format is too old");

  reset_grabbed_messages();
  cfg_set_version_without_validation(configuration, 0);
  _input("@version: 3.5\n\
baz\n");
  assert_parser_identifier("baz");
  cr_assert_eq(configuration->user_version, 0x0305,
               "@version parsing mismatch, value %04x expected %04x", configuration->user_version, 0x0305);

  assert_grabbed_log_contains("Configuration file format is too old");
}

Test(lexer, current_version)
{
  parser->lexer->ignore_pragma = FALSE;

  cfg_set_version_without_validation(configuration, 0);
  _input("@version: current\n\
foo\n");
  assert_parser_identifier("foo");
  cr_assert_eq(configuration->user_version, VERSION_VALUE_CURRENT,
               "@version parsing mismatch, value %04x expected %04x", configuration->user_version, VERSION_VALUE_CURRENT);
}

Test(lexer, test_lexer_others)
{
  _input("#This is a full line comment\nfoobar");
  assert_parser_identifier("foobar");
  _input("():;{}|");
  assert_parser_char('(');
  assert_parser_char(')');
  assert_parser_char(':');
  assert_parser_char(';');
  assert_parser_char('{');
  assert_parser_char('}');
  assert_parser_char('|');
  _input("4.2 12 0x50 011 +12 -12 -4.2 +4.2");
  assert_parser_float(4.2);
  assert_parser_number(12);
  assert_parser_number(80 /*0x50*/);
  assert_parser_number(9 /*011 */);
  assert_parser_number(12);
  assert_parser_number(-12);
  assert_parser_float(-4.2);
  assert_parser_float(4.2);
  _input("test_value");
  assert_parser_identifier("test_value");
  _input("..");
  assert_parser_token(LL_DOTDOT);
}

Test(lexer, test_location_tracking)
{

  _input("test another\nfoo\nbar\n");
  _next_token();
  assert_location(1, 1);
  _next_token();
  assert_location(1, 6);
  _next_token();
  assert_location(2, 1);
  _next_token();
  assert_location(3, 1);

  assert_location_tag("location='#test-buffer:3:1'");
}

Test(lexer, test_multiline_string_literals)
{
  _input("\"test another\\\nfoo\"\nbar");
  assert_parser_string("test anotherfoo");
  assert_location(1, 1);
  assert_parser_identifier("bar");
  assert_location(3, 1);

  _input("\"test another\nfoo\"\nbar");
  assert_parser_string("test another\nfoo");
  assert_location(1, 1);
  assert_parser_identifier("bar");
  assert_location(3, 1);
}

Test(lexer, test_multiline_qstring_literals)
{
  _input("'test another\nfoo'\nbar");
  assert_parser_string("test another\nfoo");
  assert_location(1, 1);
  assert_parser_identifier("bar");
  assert_location(3, 1);

  _input("'test another\\\nfoo'\nbar");
  assert_parser_string("test another\\\nfoo");
  assert_location(1, 1);
  assert_parser_identifier("bar");
  assert_location(3, 1);
}

Test(lexer, defined_variables_are_substituted_when_enclosed_in_backticks)
{
  parser->lexer->ignore_pragma = FALSE;
  _input("@define var1 value1\n\
@define var2 value2\n\
value0");

  assert_parser_identifier("value0");

  /* we need to supply the variables to be substituted in a separate
   * _input() call as their resolution happens when 1) read from a file, 2)
   * included */

  _input("`var1`\n\
`var2`\n");
  assert_parser_identifier("value1");
  assert_parser_identifier("value2");
}

Test(lexer, include_file_expands_the_content_of_that_file_in_the_token_stream)
{
  parser->lexer->ignore_pragma = FALSE;
  _input("@include \"" TESTDATA_DIR "/include-test/foo.conf\"\n");
  assert_parser_identifier("foo");
}


Test(lexer, include_wildcard_files_expands_the_content_of_all_files_in_the_token_stream_in_alphabetical_order)
{
  parser->lexer->ignore_pragma = FALSE;
  _input("@include \"" TESTDATA_DIR "/include-test/*.conf\"\n");
  assert_parser_identifier("bar");
  assert_parser_identifier("baz");
  assert_parser_identifier("foo");
}

Test(lexer, include_directory_expands_the_content_of_all_files_in_that_directory_in_alphabetical_ordre)
{
  parser->lexer->ignore_pragma = FALSE;
  _input("@include \"" TESTDATA_DIR "/include-test\"\n");
  assert_parser_identifier("bar");
  assert_parser_identifier("baz");
  assert_parser_identifier("foo");
}

Test(lexer, include_finds_files_in_include_path)
{
  parser->lexer->ignore_pragma = FALSE;
  _input("@define include-path \"" TESTDATA_DIR "/include-test\"\n\
@include foo.conf\n");
  assert_parser_identifier("foo");
}

Test(lexer, include_finds_wildcards_files_in_include_path)
{
  parser->lexer->ignore_pragma = FALSE;
  _input("@define include-path \"" TESTDATA_DIR "/include-test\"\n\
@include \"*.conf\"\n");
  assert_parser_identifier("bar");
  assert_parser_identifier("baz");
  assert_parser_identifier("foo");
}

static gboolean
_fake_generator_generate(CfgBlockGenerator *self, GlobalConfig *cfg, gpointer args, GString *result,
                         const gchar *reference)
{
  g_string_append(result, "fake_generator_content");
  return TRUE;
}

CfgBlockGenerator *
fake_generator_new(void)
{
  CfgBlockGenerator *self = g_new0(CfgBlockGenerator, 1);
  cfg_block_generator_init_instance(self, LL_CONTEXT_ROOT, "fake-generator");
  self->generate = _fake_generator_generate;
  return self;
}

Test(lexer, generator_plugins_are_expanded)
{
  CfgLexer *lexer = parser->lexer;

  CfgBlockGenerator *gen = fake_generator_new();
  cfg_lexer_register_generator_plugin(&configuration->plugin_context, gen);
  parser->lexer->ignore_pragma = FALSE;
  cfg_lexer_push_context(parser->lexer, main_parser.context, main_parser.keywords, main_parser.name);
  _input("fake-generator();\n");
  assert_parser_identifier("fake_generator_content");
  cfg_lexer_pop_context(lexer);
}

Test(lexer, context_name_lookup)
{
  for (int i=LL_CONTEXT_MAX-1; i >= 1; --i)
    {
      cr_assert_eq(i, cfg_lexer_lookup_context_type_by_name(cfg_lexer_lookup_context_name_by_type(i)));
    }
}

static void
setup(void)
{
  app_startup();
  configuration = cfg_new_snippet();
  parser = cfg_parser_mock_new();
}

static void
teardown(void)
{
  cfg_parser_mock_free(parser);
  cfg_free(configuration);
  configuration = NULL;
  app_shutdown();
}

TestSuite(lexer, .init = setup, .fini = teardown);