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
|
/*
* Copyright (c) 2015 Balabit
* Copyright (c) 2015 Vincent Bernat <Vincent.Bernat@exoscale.ch>
* Copyright (c) 2015 Balázs Scheidler <balazs.scheidler@balabit.com>
*
* 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 <criterion/criterion.h>
#include <criterion/parameterized.h>
#include "libtest/fake-time.h"
#include "date-parser.h"
#include "apphook.h"
#include "timeutils/cache.h"
#include "timeutils/format.h"
#include <locale.h>
#include <stdlib.h>
struct date_params
{
gchar *msg;
gchar *timezone_;
gchar *format;
gint time_stamp;
gchar *expected;
};
static LogParser *
_construct_parser(gchar *timezone_, gchar *format, gint time_stamp)
{
LogParser *parser;
parser = date_parser_new (configuration);
if (format != NULL)
date_parser_set_formats(parser, g_list_append(NULL, g_strdup(format)));
if (timezone_ != NULL)
date_parser_set_timezone(parser, timezone_);
date_parser_set_time_stamp(parser, time_stamp);
log_pipe_init(&parser->super);
return parser;
}
static LogMessage *
_construct_logmsg(const gchar *msg)
{
LogMessage *logmsg;
logmsg = log_msg_new_empty();
log_msg_set_value(logmsg, LM_V_MESSAGE, msg, -1);
return logmsg;
}
void
setup(void)
{
setlocale (LC_ALL, "C");
setenv("TZ", "CET-1", TRUE);
app_startup();
configuration = cfg_new_snippet();
/* year heuristics depends on the current time */
/* Dec 30 2015 */
fake_time(1451473200);
}
void
teardown(void)
{
app_shutdown();
}
TestSuite(date, .init = setup, .fini = teardown);
ParameterizedTestParameters(date, test_date_parser)
{
static struct date_params params[] =
{
{ "2015-01-26T16:14:49+03:00", NULL, NULL, LM_TS_RECVD, "2015-01-26T16:14:49+03:00" },
/* Various ISO8601 formats */
{ "2015-01-26T16:14:49+0300", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49+03:00" },
{ "2015-01-26T16:14:49+0330", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49+03:30" },
{ "2015-01-26T16:14:49+0200", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49+02:00" },
{ "2015-01-26T16:14:49+03:00", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49+03:00" },
{ "2015-01-26T16:14:49+03:30", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49+03:30" },
{ "2015-01-26T16:14:49+02:00", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49+02:00" },
{ "2015-01-26T16:14:49Z", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49+00:00" },
{ "2015-01-26T16:14:49A", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49-01:00" },
{ "2015-01-26T16:14:49B", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49-02:00" },
{ "2015-01-26T16:14:49N", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49+01:00" },
{ "2015-01-26T16:14:49O", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49+02:00" },
{ "2015-01-26T16:14:49GMT", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49+00:00" },
{ "2015-01-26T16:14:49PDT", NULL, NULL, LM_TS_STAMP, "2015-01-26T16:14:49-07:00" },
/* RFC 2822 */
{ "Tue, 27 Jan 2015 11:48:46 +0200", NULL, "%a, %d %b %Y %T %z", LM_TS_STAMP, "2015-01-27T11:48:46+02:00" },
/* Apache-like */
{ "21/Jan/2015:14:40:07 +0500", NULL, "%d/%b/%Y:%T %z", LM_TS_STAMP, "2015-01-21T14:40:07+05:00" },
/* Dates without timezones. America/Phoenix has no DST */
{ "Tue, 27 Jan 2015 11:48:46", NULL, "%a, %d %b %Y %T", LM_TS_STAMP, "2015-01-27T11:48:46+01:00" },
{ "Tue, 27 Jan 2015 11:48:46", "America/Phoenix", "%a, %d %b %Y %T", LM_TS_STAMP, "2015-01-27T11:48:46-07:00" },
{ "Tue, 27 Jan 2015 11:48:46", "+05:00", "%a, %d %b %Y %T", LM_TS_STAMP, "2015-01-27T11:48:46+05:00" },
/* Try without the year. */
{ "01/Jan:00:40:07 +0500", NULL, "%d/%b:%T %z", LM_TS_STAMP, "2016-01-01T00:40:07+05:00" },
{ "01/Aug:00:40:07 +0500", NULL, "%d/%b:%T %z", LM_TS_STAMP, "2015-08-01T00:40:07+05:00" },
{ "01/Sep:00:40:07 +0500", NULL, "%d/%b:%T %z", LM_TS_STAMP, "2015-09-01T00:40:07+05:00" },
{ "01/Oct:00:40:07 +0500", NULL, "%d/%b:%T %z", LM_TS_STAMP, "2015-10-01T00:40:07+05:00" },
{ "01/Nov:00:40:07 +0500", NULL, "%d/%b:%T %z", LM_TS_STAMP, "2015-11-01T00:40:07+05:00" },
{ "1446128356 +01:00", NULL, "%s %z", LM_TS_STAMP, "2015-10-29T15:19:16+01:00" },
{ "1446128356", "Europe/Budapest", "%s", LM_TS_STAMP, "2015-10-29T15:19:16+01:00" },
/* %Y-%m-%d %H:%M:%S %z */
{ "2015-01-26 00:40:07 PDT", NULL, "%Y-%m-%d %H:%M:%S %z", LM_TS_STAMP, "2015-01-26T00:40:07-07:00" },
{ "2015-01-26 00:40:07 EDT", NULL, "%Y-%m-%d %H:%M:%S %z", LM_TS_STAMP, "2015-01-26T00:40:07-04:00" },
{ "2015-01-26 00:40:07 CET", NULL, "%Y-%m-%d %H:%M:%S %z", LM_TS_STAMP, "2015-01-26T00:40:07+01:00" },
//The same test as above but using %Z instead of %z
/* RFC 2822 */
{ "Tue, 27 Jan 2015 11:48:46 +0200", NULL, "%a, %d %b %Y %T %Z", LM_TS_STAMP, "2015-01-27T11:48:46+02:00" },
/* Apache-like */
{ "21/Jan/2015:14:40:07 +0500", NULL, "%d/%b/%Y:%T %Z", LM_TS_STAMP, "2015-01-21T14:40:07+05:00" },
/* Try without the year. */
{ "01/Jan:00:40:07 +0500", NULL, "%d/%b:%T %Z", LM_TS_STAMP, "2016-01-01T00:40:07+05:00" },
{ "01/Aug:00:40:07 +0500", NULL, "%d/%b:%T %Z", LM_TS_STAMP, "2015-08-01T00:40:07+05:00" },
{ "01/Sep:00:40:07 +0500", NULL, "%d/%b:%T %Z", LM_TS_STAMP, "2015-09-01T00:40:07+05:00" },
{ "01/Oct:00:40:07 +0500", NULL, "%d/%b:%T %Z", LM_TS_STAMP, "2015-10-01T00:40:07+05:00" },
{ "01/Nov:00:40:07 +0500", NULL, "%d/%b:%T %Z", LM_TS_STAMP, "2015-11-01T00:40:07+05:00" },
{ "1446128356 +01:00", NULL, "%s %z", LM_TS_STAMP, "2015-10-29T15:19:16+01:00" },
{ "1446128356", "Europe/Budapest", "%s", LM_TS_STAMP, "2015-10-29T15:19:16+01:00" },
/* %Y-%m-%d %H:%M:%S %Z */
{ "2015-01-26 00:40:07 PDT", NULL, "%Y-%m-%d %H:%M:%S %Z", LM_TS_STAMP, "2015-01-26T00:40:07-07:00" },
{ "2015-01-26 00:40:07 EDT", NULL, "%Y-%m-%d %H:%M:%S %Z", LM_TS_STAMP, "2015-01-26T00:40:07-04:00" },
{ "2015-01-26 00:40:07 CET", NULL, "%Y-%m-%d %H:%M:%S %Z", LM_TS_STAMP, "2015-01-26T00:40:07+01:00" },
/* Try with different missing fields*/
{ "10:30:00 PDT", NULL, "%H:%M:%S %Z", LM_TS_STAMP, "2015-12-30T10:30:00-07:00" },
{ "03-17 10:30:00 PDT", NULL, "%m-%d %H:%M:%S %Z", LM_TS_STAMP, "2015-03-17T10:30:00-07:00" },
{ "03 10:30:00 PDT", NULL, "%m %H:%M:%S %Z", LM_TS_STAMP, "2015-03-01T10:30:00-07:00" },
{ "2015-03 10:30:00 EDT", NULL, "%Y-%m %H:%M:%S %Z", LM_TS_STAMP, "2015-03-01T10:30:00-04:00" },
{ "2015-03-01 EDT", NULL, "%Y-%m-%d %Z", LM_TS_STAMP, "2015-03-01T00:00:00-04:00" },
{ "2015-03 EDT", NULL, "%Y-%m %Z", LM_TS_STAMP, "2015-03-01T00:00:00-04:00" },
{ "2015-03-01 10:30 EDT", NULL, "%Y-%m-%d %H:%M %Z", LM_TS_STAMP, "2015-03-01T10:30:00-04:00" },
};
return cr_make_param_array(struct date_params, params, sizeof(params) / sizeof(struct date_params));
}
ParameterizedTest(struct date_params *params, date, test_date_parser)
{
LogMessage *logmsg;
LogParser *parser = _construct_parser(params->timezone_, params->format, params->time_stamp);
gboolean success;
GString *res = g_string_sized_new(128);
logmsg = _construct_logmsg(params->msg);
success = log_parser_process(parser, &logmsg, NULL, log_msg_get_value(logmsg, LM_V_MESSAGE, NULL), -1);
cr_assert(success, "unable to parse format=%s msg=%s", params->format, params->msg);
append_format_unix_time(&logmsg->timestamps[params->time_stamp], res, TS_FMT_ISO, -1, 0);
cr_assert_str_eq(res->str, params->expected,
"incorrect date parsed msg=%s format=%s, result=%s, expected=%s",
params->msg, params->format, res->str, params->expected);
g_string_free(res, TRUE);
log_pipe_unref(&parser->super);
log_msg_unref(logmsg);
}
Test(date, test_date_with_additional_text_at_the_end)
{
const gchar *msg = "2015-01-26T16:14:49+0300 Disappointing log file";
LogParser *parser = _construct_parser(NULL, NULL, LM_TS_STAMP);
LogMessage *logmsg = _construct_logmsg(msg);
gboolean success = log_parser_process(parser, &logmsg, NULL, log_msg_get_value(logmsg, LM_V_MESSAGE, NULL), -1);
cr_assert_not(success, "successfully parsed but expected failure, msg=%s", msg);
log_pipe_unref(&parser->super);
log_msg_unref(logmsg);
}
struct date_with_multiple_formats_params
{
const gchar *msg;
int expected_usec;
};
ParameterizedTestParameters(date, test_date_with_multiple_formats)
{
static struct date_with_multiple_formats_params params[] =
{
{ "2017-02-02 00:29:16", 0 },
{ "2017-02-02 00:29:16,706", 706000 },
{ "2019-05-04T21:55:46.989+02:00", 989000 },
};
return cr_make_param_array(struct date_with_multiple_formats_params, params,
sizeof (params) / sizeof(struct date_with_multiple_formats_params));
}
ParameterizedTest(struct date_with_multiple_formats_params *params, date, test_date_with_multiple_formats)
{
LogParser *parser;
GList *formats;
formats = g_list_prepend(NULL, g_strdup("%FT%T.%f%z"));
formats = g_list_prepend(formats, g_strdup("%F %T,%f"));
formats = g_list_prepend(formats, g_strdup("%F %T"));
parser = date_parser_new (configuration);
date_parser_set_formats(parser, formats);
date_parser_set_time_stamp(parser, LM_TS_STAMP);
LogMessage *logmsg = _construct_logmsg(params->msg);
gboolean success = log_parser_process(parser, &logmsg, NULL, log_msg_get_value(logmsg, LM_V_MESSAGE, NULL), -1);
cr_assert(success, "unable to parse msg=%s with a list of formats", params->msg);
cr_assert(logmsg->timestamps[LM_TS_STAMP].ut_usec == params->expected_usec, "expected %d us, got %d",
params->expected_usec,
logmsg->timestamps[LM_TS_STAMP].ut_usec);
log_msg_unref(logmsg);
log_pipe_unref(&parser->super);
}
Test(date, test_date_with_guess_timezone)
{
const gchar *msg = "2015-12-30T12:00:00+05:00";
GString *res = g_string_sized_new(128);
LogParser *parser = _construct_parser(NULL, NULL, LM_TS_STAMP);
date_parser_process_flag(parser, "guess-timezone");
LogMessage *logmsg = _construct_logmsg(msg);
gboolean success = log_parser_process(parser, &logmsg, NULL, log_msg_get_value(logmsg, LM_V_MESSAGE, NULL), -1);
cr_assert(success, "failed to parse timestamp, msg=%s", msg);
append_format_unix_time(&logmsg->timestamps[LM_TS_STAMP], res, TS_FMT_ISO, -1, 0);
/* this should fix up the timezone */
cr_assert_str_eq(res->str, "2015-12-30T12:00:00+01:00",
"incorrect date parsed msg=%s result=%s",
msg, res->str);
log_pipe_unref(&parser->super);
log_msg_unref(logmsg);
g_string_free(res, TRUE);
}
|