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
|
/*
* Copyright (c) 2016 Balabit
*
* 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 "geoip-parser.h"
#include "apphook.h"
#include "msg_parse_lib.h"
#define geoip_parser_testcase_begin(func, args) \
do \
{ \
testcase_begin("%s(%s)", func, args); \
geoip_parser = geoip_parser_new(configuration); \
} \
while (0)
#define geoip_parser_testcase_end() \
do \
{ \
log_pipe_deinit(&geoip_parser->super); \
log_pipe_unref(&geoip_parser->super); \
testcase_end(); \
} \
while (0)
#define KV_PARSER_TESTCASE(x, ...) \
do { \
geoip_parser_testcase_begin(#x, #__VA_ARGS__); \
x(__VA_ARGS__); \
geoip_parser_testcase_end(); \
} while(0)
LogParser *geoip_parser;
static LogMessage *
parse_geoip_into_log_message_no_check(const gchar *input)
{
LogMessage *msg;
LogPathOptions path_options = LOG_PATH_OPTIONS_INIT;
LogParser *cloned_parser;
gboolean success;
cloned_parser = (LogParser *) log_pipe_clone(&geoip_parser->super);
log_pipe_init(&cloned_parser->super);
msg = log_msg_new_empty();
log_msg_set_value(msg, LM_V_MESSAGE, input, -1);
success = log_parser_process_message(cloned_parser, &msg, &path_options);
if (!success)
{
log_msg_unref(msg);
msg = NULL;
}
log_pipe_deinit(&cloned_parser->super);
log_pipe_unref(&cloned_parser->super);
return msg;
}
static LogMessage *
parse_geoip_into_log_message(const gchar *input)
{
LogMessage *msg;
msg = parse_geoip_into_log_message_no_check(input);
assert_not_null(msg, "expected geoip-parser success and it returned failure, input=%s", input);
return msg;
}
static void
test_geoip_parser_basics(void)
{
LogMessage *msg;
msg = parse_geoip_into_log_message("217.20.130.99");
assert_log_message_value(msg, log_msg_get_value_handle(".geoip.country_code"), "HU");
log_msg_unref(msg);
geoip_parser_set_prefix(geoip_parser, ".prefix.");
msg = parse_geoip_into_log_message("217.20.130.99");
assert_log_message_value(msg, log_msg_get_value_handle(".prefix.country_code"), "HU");
log_msg_unref(msg);
}
static void
test_geoip_parser_uses_template_to_parse_input(void)
{
LogMessage *msg;
LogTemplate *template;
template = log_template_new(NULL, NULL);
log_template_compile(template, "217.20.130.99", NULL);
log_parser_set_template(geoip_parser, template);
msg = parse_geoip_into_log_message("8.8.8.8");
assert_log_message_value(msg, log_msg_get_value_handle(".geoip.country_code"), "HU");
log_msg_unref(msg);
}
static void
test_geoip_parser(void)
{
KV_PARSER_TESTCASE(test_geoip_parser_basics);
KV_PARSER_TESTCASE(test_geoip_parser_uses_template_to_parse_input);
}
int
main(int argc G_GNUC_UNUSED, char *argv[] G_GNUC_UNUSED)
{
app_startup();
test_geoip_parser();
app_shutdown();
return 0;
}
|