File: test_dot_notation.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 (217 lines) | stat: -rw-r--r-- 7,774 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
/*
 * Copyright (c) 2014 Balabit
 * Copyright (c) 2014 Balazs Scheidler <bazsi@balabit.hu>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; 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 "testutils.h"
#include "apphook.h"
#include "msg_parse_lib.h"
#include "dot-notation.h"

#include <json.h>

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

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


#define DOT_NOTATION_TESTCASE(x, ...) \
  do {                                                          \
      dot_notation_testcase_begin(#x, #__VA_ARGS__);  		\
      x(__VA_ARGS__);                                           \
      dot_notation_testcase_end();                              \
  } while(0)


static struct json_object *
compile_json(const gchar *json)
{
  struct json_tokener *tok;
  struct json_object *jso;

  tok = json_tokener_new();
  jso = json_tokener_parse_ex(tok, json, strlen(json));
  assert_true(tok->err == json_tokener_success, "expected to parse input json, but couldn't, json=%s", json);
  json_tokener_free(tok);
  return jso;
}

static void
assert_json_equals(struct json_object *a, struct json_object *b, const gchar *subscript)
{
  const gchar *a_str, *b_str;
  
  a_str = json_object_to_json_string(a);
  b_str = json_object_to_json_string(b);
  assert_string(a_str, b_str, "extraction didn't return the expected subscript of the object: %s", subscript);
}

static void
assert_dot_notation_eval_equals(const gchar *input_json, const gchar *subscript, const gchar *expected_json)
{
  struct json_object *input, *expected, *sub;

  input = compile_json(input_json);
  expected = compile_json(expected_json);
  
  sub = json_extract(input, subscript);
  assert_json_equals(sub, expected, subscript);
  json_object_put(expected);
  json_object_put(input);
}

static void
assert_dot_notation_eval_fails(const gchar *input_json, const gchar *subscript)
{
  struct json_object *input, *sub;

  input = compile_json(input_json);
  sub = json_extract(input, subscript);
  assert_null(sub, "extracted JSON is not NULL as expected, json=%s, subscript=%s", input_json, subscript);
  json_object_put(input);
}

static void
test_dot_notation_eval_empty_subscript_returns_the_object(void)
{
  assert_dot_notation_eval_equals("{'foo': 'bar'}", "", "{'foo': 'bar'}");
}

static void
test_dot_notation_eval_fails_with_an_identifier_that_doesnt_start_with_a_letter(void)
{
  assert_dot_notation_eval_fails("{}", "123");
}

static void
test_dot_notation_eval_fails_with_an_identifier_that_contains_a_non_alnum_character(void)
{
  assert_dot_notation_eval_fails("{}", "foo123_?");
}

static void
test_dot_notation_eval_fails_with_an_incorrect_array_reference(void)
{
  assert_dot_notation_eval_fails("[]", "foo[1]bar");
  assert_dot_notation_eval_fails("[]", "foo[zbc]");
  assert_dot_notation_eval_fails("{'foo': ['1', '2', '3']}", "foo.[0]");
}

static void
test_dot_notation_eval_suceeds_with_an_identifier_that_contains_all_possible_character_classes(void)
{
  assert_dot_notation_eval_equals("{'fOo123_': 'bar'}", "fOo123_", "'bar'");
}

static void
test_dot_notation_eval_object_subscript_extracts_the_child_object(void)
{
  assert_dot_notation_eval_equals("{'foo': 'bar'}", "foo", "'bar'");
  assert_dot_notation_eval_equals("{'foo': {'foo': 'bar'}}", "foo", "{'foo': 'bar'}");
  assert_dot_notation_eval_equals("{'foo': {'foo': 'bar'}}", "foo.foo", "'bar'");
  assert_dot_notation_eval_equals("{'foo': {'foo': {'foo': 'bar'}}}", "foo.foo.foo", "'bar'");
}

static void
test_dot_notation_eval_object_succeeds_with_odd_or_invalid_js_identifier(void)
{
  assert_dot_notation_eval_equals("{'@foo': 'bar'}", "@foo", "'bar'");
  assert_dot_notation_eval_equals("{'_foo': 'bar'}", "_foo", "'bar'");
  assert_dot_notation_eval_equals("{'foo+4': 'bar'}", "foo+4", "'bar'");
  assert_dot_notation_eval_equals("{'foo,bar': 'bar'}", "foo,bar", "'bar'");
  assert_dot_notation_eval_equals("{'foo bar': 'bar'}", "foo bar", "'bar'");
  assert_dot_notation_eval_equals("{'foo-bar': 'bar'}", "foo-bar", "'bar'");
  assert_dot_notation_eval_equals("{'1': 'bar'}", "1", "'bar'");
}

static void
test_dot_notation_eval_object_member_from_non_object_fails(void)
{
  assert_dot_notation_eval_fails("[1, 2, 3]", "foo");
}

static void
test_dot_notation_eval_array_item_from_non_array_fails(void)
{
  assert_dot_notation_eval_fails("{'foo': 'bar'}", "[0]");
}

static void
test_dot_notation_eval_array_item_by_index(void)
{
  assert_dot_notation_eval_equals("['foo', 'bar', 'baz']", "[0]", "'foo'");
  assert_dot_notation_eval_equals("['foo', 'bar', 'baz']", "[1]", "'bar'");
  assert_dot_notation_eval_equals("{'foo': 'bar', 'baz': ['1', '2', '3']}", "baz[2]", "'3'");
}


static void
test_dot_notation_eval_multi_array_item_by_multiple_indexes(void)
{
  assert_dot_notation_eval_equals("{'foo': 'bar', 'baz':"
"["
  "[ '1', '2', '3' ],"
  "[ '4', '5', '6' ],"
  "[ '7', '8', '9' ]"
"]}", "baz[0][2]", "'3'");
}

static void
test_dot_notation_eval_fails_with_out_of_bounds_index(void)
{
  assert_dot_notation_eval_fails("['foo', 'bar', 'baz']", "[3]");
  assert_dot_notation_eval_fails("['foo', 'bar', 'baz']", "[-1]");
}

static void
test_dot_notation_eval(void)
{
  DOT_NOTATION_TESTCASE(test_dot_notation_eval_empty_subscript_returns_the_object);
  DOT_NOTATION_TESTCASE(test_dot_notation_eval_object_subscript_extracts_the_child_object);
  DOT_NOTATION_TESTCASE(test_dot_notation_eval_object_succeeds_with_odd_or_invalid_js_identifier);
  DOT_NOTATION_TESTCASE(test_dot_notation_eval_fails_with_an_identifier_that_doesnt_start_with_a_letter);
  DOT_NOTATION_TESTCASE(test_dot_notation_eval_fails_with_an_identifier_that_contains_a_non_alnum_character);
  DOT_NOTATION_TESTCASE(test_dot_notation_eval_fails_with_an_incorrect_array_reference);
  DOT_NOTATION_TESTCASE(test_dot_notation_eval_suceeds_with_an_identifier_that_contains_all_possible_character_classes);
  DOT_NOTATION_TESTCASE(test_dot_notation_eval_object_member_from_non_object_fails);
  DOT_NOTATION_TESTCASE(test_dot_notation_eval_array_item_from_non_array_fails);
  DOT_NOTATION_TESTCASE(test_dot_notation_eval_array_item_by_index);
  DOT_NOTATION_TESTCASE(test_dot_notation_eval_multi_array_item_by_multiple_indexes);
  DOT_NOTATION_TESTCASE(test_dot_notation_eval_fails_with_out_of_bounds_index);
}

int
main(int argc G_GNUC_UNUSED, char *argv[] G_GNUC_UNUSED)
{
  app_startup();

  test_dot_notation_eval();
  app_shutdown();
  return 0;
}