File: test_cfg_lexer_subst.c

package info (click to toggle)
syslog-ng 3.8.1-10
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 47,320 kB
  • ctags: 43,937
  • sloc: ansic: 159,432; yacc: 25,059; sh: 13,574; makefile: 4,669; python: 3,468; java: 3,218; xml: 2,309; perl: 318; lex: 316; awk: 184
file content (318 lines) | stat: -rw-r--r-- 11,542 bytes parent folder | download
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
/*
 * Copyright (c) 2002-2013 Balabit
 *
 * 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 "cfg-lexer-subst.h"
#include "testutils.h"
#include "apphook.h"

#include <string.h>

#define SUBST_TESTCASE(x, ...) do { subst_testcase_begin(#x, #__VA_ARGS__); x(__VA_ARGS__); subst_testcase_end(); } while(0)

#define subst_testcase_begin(func, args)                    \
  do                                                            \
    {                                                           \
      testcase_begin("%s(%s)", func, args);                     \
    }                                                           \
  while (0)

#define subst_testcase_end()                                \
  do                                                            \
    {                                                           \
      testcase_end();                                           \
    }                                                           \
  while (0)

static CfgArgs *
construct_cfg_args_for_args(const gchar *additional_values[])
{
  CfgArgs *args = cfg_args_new();
  gint i;

  cfg_args_set(args, "arg", "arg_value");
  cfg_args_set(args, "simple_string", "\"simple_string_value\"");
  cfg_args_set(args, "simple_qstring", "'simple_qstring_value'");
  cfg_args_set(args, "escaped_string", "\"escaped_string\\\"\\r\\n\"");

  for (i = 0; additional_values && additional_values[i] && additional_values[i + 1]; i += 2)
    {
      cfg_args_set(args, additional_values[i], additional_values[i + 1]);
    }
  return args;
}

static CfgArgs *
construct_cfg_args_for_defaults(void)
{
  CfgArgs *args = cfg_args_new();
  cfg_args_set(args, "arg", "default_for_arg");
  cfg_args_set(args, "def", "default_for_def");
  return args;
}

static CfgArgs *
construct_cfg_args_for_globals(void)
{
  CfgArgs *args = cfg_args_new();
  cfg_args_set(args, "arg", "global_for_arg");
  cfg_args_set(args, "def", "global_for_def");
  cfg_args_set(args, "globl", "global_for_globl");
  return args;
}

static CfgLexerSubst *
construct_object_with_values(const gchar *additional_values[])
{
  return cfg_lexer_subst_new(construct_cfg_args_for_globals(),
                             construct_cfg_args_for_defaults(),
                             construct_cfg_args_for_args(additional_values));
}

static CfgLexerSubst *
construct_object(void)
{
  return construct_object_with_values(NULL);
}

static gchar *
duplicate_string_without_with_trailing_nonzero_bytes(const gchar *input)
{
  gsize input_len = strlen(input);
  const gint canary_size = 10;
  gchar *input_dup = malloc(input_len + canary_size);

  memcpy(input_dup, input, input_len);
  memset(input_dup + input_len, 'A', canary_size - 1);
  input_dup[input_len + canary_size - 1] = 0;
  return input_dup;
}

static void
_assert_invoke_result(CfgLexerSubst *subst, gchar *input_dup, gssize input_len, const gchar *expected_output)
{
  gchar *result;
  gsize result_len;
  GError *error = NULL;

  result = cfg_lexer_subst_invoke(subst, input_dup, input_len, &result_len, &error);
  assert_true(error == NULL, "Error value is non-null while no error is expected");
  assert_true(result != NULL, "value substitution returned an unexpected failure");
  assert_string(result, expected_output, "value substitution is broken");
  assert_guint64(result_len, strlen(expected_output), "length returned by invoke_result is invalid");
  g_free(result);
}

static void
assert_invoke_result_with_input_length_explicit(CfgLexerSubst *subst, const gchar *input, const gchar *expected_output)
{
  gchar *input_dup;

  input_dup = duplicate_string_without_with_trailing_nonzero_bytes(input);
  _assert_invoke_result(subst, input_dup, strlen(input), expected_output);
  g_free(input_dup);
}

static void
assert_invoke_result_with_zero_terminated_input(CfgLexerSubst *subst, const gchar *input, const gchar *expected_output)
{
  gchar *input_dup;

  input_dup = strdup(input);
  _assert_invoke_result(subst, input_dup, -1, expected_output);
  g_free(input_dup);
}

static void
assert_invoke_result(CfgLexerSubst *subst, const gchar *input, const gchar *expected_output)
{
  assert_invoke_result_with_input_length_explicit(subst, input, expected_output);
  assert_invoke_result_with_zero_terminated_input(subst, input, expected_output);
}

static void
assert_invoke_failure(CfgLexerSubst *subst, const gchar *input, const gchar *expected_error)
{
  gchar *result;
  gsize result_len;
  gchar *input_dup = g_strdup(input);
  GError *error = NULL;

  result = cfg_lexer_subst_invoke(subst, input_dup, strlen(input), &result_len, &error);
  assert_true(result == NULL, "expected failure for value substitution, but success was returned");
  assert_true(error != NULL, "expected a non-NULL error object for failure");
  assert_string(error->message, expected_error, "error message mismatch");
  g_free(input_dup);
}

static void
test_double_backtick_replaced_with_a_single_one(void)
{
  CfgLexerSubst *subst = construct_object();
  assert_invoke_result(subst, "``", "`");
  cfg_lexer_subst_free(subst);
}

static void
test_single_backtick_causes_an_error(void)
{
  CfgLexerSubst *subst = construct_object();
  assert_invoke_failure(subst, "foo ` bar", "missing closing backtick (`) character");
  cfg_lexer_subst_free(subst);
}

static void
test_backtick_after_quoted_character_succeeds(void)
{
  CfgLexerSubst *subst = construct_object();
  assert_invoke_result(subst, "foo \"string \\n`arg`\" bar", "foo \"string \\narg_value\" bar");
  cfg_lexer_subst_free(subst);
}

static void
test_backtick_as_a_quoted_character_in_a_string_results_in_failure(void)
{
  CfgLexerSubst *subst = construct_object();

  assert_invoke_failure(subst, "foo \"string \\`arg`\" bar", "cannot subsitute backticked values right after a string quote character");
  cfg_lexer_subst_free(subst);
}

static void
test_value_in_normal_text_replaced_with_its_literal_value(void)
{
  CfgLexerSubst *subst = construct_object();
  assert_invoke_result(subst, "foo `arg` bar", "foo arg_value bar");
  assert_invoke_result(subst, "foo `simple_string` bar", "foo \"simple_string_value\" bar");
  assert_invoke_result(subst, "foo `simple_qstring` bar", "foo 'simple_qstring_value' bar");
  assert_invoke_result(subst, "foo `escaped_string` bar", "foo \"escaped_string\\\"\\r\\n\" bar");
  cfg_lexer_subst_free(subst);
}

static void
test_values_are_resolution_order_args_defaults_globals_env(void)
{
  CfgLexerSubst *subst = construct_object();

  putenv("env=env_for_env");
  assert_invoke_result(subst, "foo `arg` bar", "foo arg_value bar");
  assert_invoke_result(subst, "foo `def` bar", "foo default_for_def bar");
  assert_invoke_result(subst, "foo `globl` bar", "foo global_for_globl bar");
  assert_invoke_result(subst, "foo `env` bar", "foo env_for_env bar");
  cfg_lexer_subst_free(subst);
}

static void
test_values_are_inserted_within_strings(void)
{
  CfgLexerSubst *subst = construct_object();

  assert_invoke_result(subst, "foo \"`arg`\" bar", "foo \"arg_value\" bar");
  assert_invoke_result(subst, "foo '`arg`' bar", "foo 'arg_value' bar");
  cfg_lexer_subst_free(subst);
}

static void
test_string_literals_are_inserted_into_strings_without_quotes(void)
{
  const gchar *additional_values[] = {
    "simple_string_with_whitespace", "  \"string_with_whitespace\"   ",
    NULL, NULL
  };
  CfgLexerSubst *subst = construct_object_with_values(additional_values);

  /* double quotes */
  assert_invoke_result(subst, "foo \"x `simple_string` y\" bar", "foo \"x simple_string_value y\" bar");
  /* apostrophes */
  assert_invoke_result(subst, "foo 'x `simple_string` y' bar", "foo 'x simple_string_value y' bar");
  assert_invoke_result(subst, "foo \"x `simple_string_with_whitespace` y\" bar", "foo \"x string_with_whitespace y\" bar");
  cfg_lexer_subst_free(subst);
}

static void
test_incorrect_strings_and_multiple_tokens_are_inserted_verbatim(void)
{
  const gchar *additional_values[] = {
    "half_string", "\"halfstring",
    "tokens_that_start_with_string", "\"str\", token",
    "tokens_enclosed_in_strings", "\"str1\", token, \"str2\"",
    NULL, NULL
  };
  CfgLexerSubst *subst = construct_object_with_values(additional_values);

  assert_invoke_result(subst, "foo \"x `simple_string` y\" bar", "foo \"x simple_string_value y\" bar");
  assert_invoke_result(subst, "foo \"x `half_string` y\" bar", "foo \"x \"halfstring y\" bar");
  assert_invoke_result(subst, "foo \"x `tokens_that_start_with_string` y\" bar", "foo \"x \"str\", token y\" bar");
  assert_invoke_result(subst, "foo \"x `tokens_enclosed_in_strings` y\" bar", "foo \"x \"str1\", token, \"str2\" y\" bar");
  cfg_lexer_subst_free(subst);
}

static void
test_strings_with_special_chars_are_properly_encoded_in_strings(void)
{
  const gchar *additional_values[] = {
    "string_with_characters_that_need_quoting", "\"quote: \\\", newline: \\r\\n, backslash: \\\\\"",
    NULL, NULL
  };
  CfgLexerSubst *subst = construct_object_with_values(additional_values);

  assert_invoke_result(subst, "foo \"x `string_with_characters_that_need_quoting` y\" bar", "foo \"x quote: \\\", newline: \\r\\n, backslash: \\\\ y\" bar");
  cfg_lexer_subst_free(subst);
}

static void
test_strings_with_embedded_apostrophe_cause_an_error_when_encoding_in_qstring(void)
{
  const gchar *additional_values[] = {
    "string_with_apostrophe", "\"'foo'\"",
    NULL, NULL
  };
  CfgLexerSubst *subst = construct_object_with_values(additional_values);

  assert_invoke_result(subst, "foo \"x `string_with_apostrophe` y\" bar", "foo \"x 'foo' y\" bar");
  assert_invoke_failure(subst, "foo 'x `string_with_apostrophe` y' bar", "cannot represent apostrophes within apostroph-enclosed string");
  cfg_lexer_subst_free(subst);
}

static void
test_cfg_lexer_subst(void)
{
  SUBST_TESTCASE(test_double_backtick_replaced_with_a_single_one);
  SUBST_TESTCASE(test_single_backtick_causes_an_error);
  SUBST_TESTCASE(test_backtick_after_quoted_character_succeeds);
  SUBST_TESTCASE(test_backtick_as_a_quoted_character_in_a_string_results_in_failure);
  SUBST_TESTCASE(test_value_in_normal_text_replaced_with_its_literal_value);
  SUBST_TESTCASE(test_values_are_resolution_order_args_defaults_globals_env);
  SUBST_TESTCASE(test_values_are_inserted_within_strings);
  SUBST_TESTCASE(test_string_literals_are_inserted_into_strings_without_quotes);
  SUBST_TESTCASE(test_incorrect_strings_and_multiple_tokens_are_inserted_verbatim);
  SUBST_TESTCASE(test_strings_with_special_chars_are_properly_encoded_in_strings);
  SUBST_TESTCASE(test_strings_with_embedded_apostrophe_cause_an_error_when_encoding_in_qstring);
}

int main()
{
  app_startup();

  test_cfg_lexer_subst();
  app_shutdown();
  return 0;
}