File: test-mongodb-config.c

package info (click to toggle)
syslog-ng 3.19.1-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,176 kB
  • sloc: ansic: 114,472; makefile: 4,697; sh: 4,391; python: 4,282; java: 4,047; xml: 2,435; yacc: 1,108; lex: 426; perl: 193; awk: 184
file content (345 lines) | stat: -rw-r--r-- 10,820 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
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
/*
 * Copyright (c) 2016 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 "syslog-ng.h"
#include "testutils.h"
#include "mainloop.h"
#include "modules/afmongodb/afmongodb-parser.h"
#include "logthrdestdrv.h"

static int _tests_failed = 0;
static GlobalConfig *test_cfg;
static LogDriver *mongodb;

#define SAFEOPTS "?wtimeoutMS=60000&socketTimeoutMS=60000&connectTimeoutMS=60000"

static void
_before_test(void)
{
  start_grabbing_messages();
  mongodb = afmongodb_dd_new(test_cfg);
}

static gboolean
_after_test(void)
{
  gboolean uri_init_ok = afmongodb_dd_private_uri_init(mongodb);
  stop_grabbing_messages();
  return uri_init_ok;
}

static void
_free_test(void)
{
  reset_grabbed_messages();
  if (mongodb)
    {
      log_pipe_unref(&mongodb->super);
      mongodb = NULL;
    }
}

typedef gboolean (*Checks)(const gchar *user_data);

static gboolean
_execute(const gchar *testcase, Checks checks, const gchar *user_data)
{
  gboolean uri_init_ok = _after_test();

  testcase_begin("%s(%s, %s)", __FUNCTION__, testcase, user_data);
  if (!checks(user_data))
    _tests_failed = 1;
  reset_grabbed_messages();
  testcase_end();

  _free_test();
  _before_test();
  return uri_init_ok;
}

static gboolean
_execute_find_text_in_log(const gchar *pattern)
{
  return assert_grabbed_messages_contain_non_fatal(pattern, "mismatch");
}

static void
_log_error(const gchar *message)
{
  fprintf(stderr, "error: %s\n", message);
}

static void
_execute_correct(const gchar *testcase, Checks checks, const gchar *user_data)
{
  if (!_execute(testcase, checks, user_data))
    {
      _log_error("expected the subject to succeed, but it failed");
      _tests_failed = 1;
    }
}

static void
_execute_failing(const gchar *testcase, Checks checks, const gchar *user_data)
{
  if (_execute(testcase, checks, user_data))
    {
      _log_error("expected the subject to fail, but it succeeded");
      _tests_failed = 1;
    }
}

static void
_expect_error_in_log(const gchar *testcase, const gchar *pattern)
{
  _execute_failing(testcase, _execute_find_text_in_log, pattern);
}

static void
_expect_uri_in_log(const gchar *testcase, const gchar *uri, const gchar *db, const gchar *coll)
{
  GString *pattern = g_string_sized_new(0);
  g_string_append_printf(pattern,
                         "Initializing MongoDB destination;"
                         " uri='mongodb://%s', db='%s', collection='%s'",
                         uri, db, coll);
  _execute_correct(testcase, _execute_find_text_in_log, pattern->str);
  g_string_free(pattern, TRUE);
}

static gboolean
_execute_compare_persist_name(const gchar *expected_name)
{
  LogThreadedDestDriver *self = (LogThreadedDestDriver *)mongodb;
  const gchar *name = log_pipe_get_persist_name((const LogPipe *)self);
  return assert_nstring_non_fatal(name, -1, expected_name, -1, "mismatch");
}

static void
_test_persist_name(void)
{
  afmongodb_dd_set_uri(mongodb, "mongodb://127.0.0.2:27018,localhost:1234/syslog"
                       SAFEOPTS "&replicaSet=x");
  _execute_correct("persist", _execute_compare_persist_name,
                   "afmongodb(127.0.0.2:27018,syslog,x,messages)");
}

static gboolean
_execute_compare_stats_name(const gchar *expected_name)
{
  LogThreadedDestDriver *self = (LogThreadedDestDriver *)mongodb;
  const gchar *name = self->format_stats_instance(self);
  return assert_nstring_non_fatal(name, -1, expected_name, -1, "mismatch");
}

static void
_test_stats_name(void)
{
  afmongodb_dd_set_uri(mongodb, "mongodb://127.0.0.2:27018,localhost:1234/syslog"
                       SAFEOPTS "&replicaSet=x");
  _execute_correct("stats", _execute_compare_stats_name,
                   "mongodb,127.0.0.2:27018,syslog,x,messages");
}

static void
_test_uri_correct(void)
{
  _expect_uri_in_log("default_uri", "127.0.0.1:27017/syslog" SAFEOPTS, "syslog", "messages");

  afmongodb_dd_set_uri(mongodb, "mongodb://%2Ftmp%2Fmongo.sock/syslog");
  _expect_uri_in_log("socket", "%2Ftmp%2Fmongo.sock/syslog", "syslog", "messages");

  afmongodb_dd_set_uri(mongodb, "mongodb://localhost:1234/syslog-ng");
  _expect_uri_in_log("uri", "localhost:1234/syslog-ng", "syslog-ng", "messages");

  afmongodb_dd_set_collection(mongodb, "messages2");
  _expect_uri_in_log("collection", "127.0.0.1:27017/syslog" SAFEOPTS, "syslog", "messages2");
}

static void
_test_uri_error(void)
{
  afmongodb_dd_set_uri(mongodb, "INVALID-URI");
  _expect_error_in_log("invalid_uri", "Error parsing MongoDB URI; uri='INVALID-URI'");

  afmongodb_dd_set_uri(mongodb, "mongodb://127.0.0.1:27017/");
  _expect_error_in_log("missing_db", "Missing DB name from MongoDB URI;"
                       " uri='mongodb://127.0.0.1:27017/'");
}

#if SYSLOG_NG_ENABLE_LEGACY_MONGODB_OPTIONS
#define UNSAFEOPTS "?w=0&safe=false&socketTimeoutMS=60000&connectTimeoutMS=60000"

static void
_test_legacy_correct(void)
{
  GList *servers = g_list_append(NULL, g_strdup("127.0.0.2:27018"));
  servers = g_list_append(servers, g_strdup("localhost:1234"));
  afmongodb_dd_set_servers(mongodb, servers);
  _expect_uri_in_log("servers_multi", "127.0.0.2:27018,localhost:1234/syslog" SAFEOPTS,
                     "syslog", "messages");

  servers = g_list_append(NULL, g_strdup("127.0.0.2"));
  afmongodb_dd_set_servers(mongodb, servers);
  _expect_uri_in_log("servers_single", "127.0.0.2:27017/syslog" SAFEOPTS, "syslog", "messages");

  afmongodb_dd_set_host(mongodb, "localhost");
  _expect_uri_in_log("host", "localhost:27017/syslog" SAFEOPTS, "syslog", "messages");

  afmongodb_dd_set_host(mongodb, "localhost");
  afmongodb_dd_set_port(mongodb, 1234);
  _expect_uri_in_log("host_port", "localhost:1234/syslog" SAFEOPTS, "syslog", "messages");

  afmongodb_dd_set_port(mongodb, 27017);
  _expect_uri_in_log("port_default", "127.0.0.1:27017/syslog" SAFEOPTS, "syslog", "messages");

  afmongodb_dd_set_port(mongodb, 1234);
  _expect_uri_in_log("port", "127.0.0.1:1234/syslog" SAFEOPTS, "syslog", "messages");

  afmongodb_dd_set_database(mongodb, "syslog-ng");
  _expect_uri_in_log("database", "127.0.0.1:27017/syslog-ng" SAFEOPTS, "syslog-ng", "messages");

  afmongodb_dd_set_safe_mode(mongodb, TRUE);
  _expect_uri_in_log("safe_mode_true", "127.0.0.1:27017/syslog" SAFEOPTS, "syslog", "messages");

  afmongodb_dd_set_safe_mode(mongodb, FALSE);
  _expect_uri_in_log("safe_mode_false", "127.0.0.1:27017/syslog" UNSAFEOPTS, "syslog", "messages");

  afmongodb_dd_set_user(mongodb, "user");
  afmongodb_dd_set_password(mongodb, "password");
  _expect_uri_in_log("user_password", "user:password@127.0.0.1:27017/syslog" SAFEOPTS,
                     "syslog", "messages");

  afmongodb_dd_set_collection(mongodb, "messages2");
  afmongodb_dd_set_safe_mode(mongodb, FALSE);
  _expect_uri_in_log("collection_safe_mode", "127.0.0.1:27017/syslog" UNSAFEOPTS,
                     "syslog", "messages2");

  afmongodb_dd_set_user(mongodb, "");
  afmongodb_dd_set_password(mongodb, "password");
  _expect_uri_in_log("empty_user", ":password@127.0.0.1:27017/syslog" SAFEOPTS,
                     "syslog", "messages");

  afmongodb_dd_set_user(mongodb, "user");
  afmongodb_dd_set_password(mongodb, "");
  _expect_uri_in_log("empty_password", "user:@127.0.0.1:27017/syslog" SAFEOPTS,
                     "syslog", "messages");

  afmongodb_dd_set_user(mongodb, "");
  afmongodb_dd_set_password(mongodb, "");
  _expect_uri_in_log("empty_user_password", ":@127.0.0.1:27017/syslog" SAFEOPTS,
                     "syslog", "messages");

  afmongodb_dd_set_user(mongodb, "127.0.0.1:27017/syslog?dont-care=");
  afmongodb_dd_set_password(mongodb, "");
  _expect_uri_in_log("hijacked_user", "127.0.0.1:27017/syslog?dont-care=:@127.0.0.1:27017/syslog"
                     SAFEOPTS, "syslog", "messages");
}

static void
_test_legacy_error(void)
{
  afmongodb_dd_set_safe_mode(mongodb, FALSE);
  afmongodb_dd_set_uri(mongodb, "mongodb://127.0.0.1:27017/syslog");
  _expect_error_in_log("uri_safe_mode", "Error: either specify a MongoDB URI "
                       "(and optional collection) or only legacy options;");

  afmongodb_dd_set_host(mongodb, "?");
  _expect_error_in_log("host_invalid", "Cannot parse MongoDB URI; uri=");

  afmongodb_dd_set_host(mongodb, "");
  _expect_error_in_log("host_none", "Cannot parse the primary host; primary=\'\'");

  afmongodb_dd_set_host(mongodb, "localhost,127.0.0.1");
  _expect_error_in_log("host_multi", "Multiple hosts found in MongoDB URI; uri=");

  GList *servers = g_list_append(NULL, g_strdup("localhost,127.0.0.1"));
  afmongodb_dd_set_servers(mongodb, servers);
  _expect_error_in_log("servers_multi", "Multiple hosts found in MongoDB URI; uri=");

  servers = g_list_append(NULL, g_strdup(""));
  afmongodb_dd_set_servers(mongodb, servers);
  _expect_error_in_log("servers_none", "Cannot parse MongoDB URI; uri=");

  afmongodb_dd_set_password(mongodb, "password");
  _expect_error_in_log("missing_user", "Neither the username, nor the password can be empty;");

  afmongodb_dd_set_user(mongodb, "user");
  _expect_error_in_log("missing_password", "Neither the username, nor the password can be empty;");
}
#endif

static void
_setup(void)
{
  msg_init(FALSE);
  g_thread_init(NULL);

  debug_flag = TRUE;
  verbose_flag = TRUE;
  trace_flag = TRUE;

  log_msg_registry_init();

  test_cfg = cfg_new_snippet();
  g_assert(test_cfg);

  const gchar *persist_filename = "";
  test_cfg->state = persist_state_new(persist_filename);

  _before_test();
}

static void
_teardown(void)
{
  _free_test();
  if (test_cfg->persist)
    {
      persist_config_free(test_cfg->persist);
      test_cfg->persist = NULL;
    }
  cfg_free(test_cfg);

  log_msg_registry_deinit();
  msg_deinit();
}

int
main(int argc, char **argv)
{
  _setup();

  _test_persist_name();
  _test_stats_name();
  _test_uri_correct();
  _test_uri_error();

#if SYSLOG_NG_ENABLE_LEGACY_MONGODB_OPTIONS
  _test_legacy_correct();
  _test_legacy_error();
#endif

  _teardown();
  return _tests_failed;
}