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
|
From: Balazs Scheidler <bazsi77@gmail.com>
Date: Sat, 20 Aug 2022 12:43:42 +0200
Subject: timeutils: add tests for non-zero terminated inputs
Signed-off-by: Balazs Scheidler <bazsi77@gmail.com>
Origin: https://github.com/syslog-ng/syslog-ng/commit/45f051239312e43bd4f92b9339fe67c6798a0321
---
lib/timeutils/tests/test_scan-timestamp.c | 126 +++++++++++++++++++++++++++---
1 file changed, 113 insertions(+), 13 deletions(-)
diff --git a/lib/timeutils/tests/test_scan-timestamp.c b/lib/timeutils/tests/test_scan-timestamp.c
index 2756bf3..da375c8 100644
--- a/lib/timeutils/tests/test_scan-timestamp.c
+++ b/lib/timeutils/tests/test_scan-timestamp.c
@@ -49,17 +49,21 @@ fake_time_add(time_t diff)
}
static gboolean
-_parse_rfc3164(const gchar *ts, gchar isotimestamp[32])
+_parse_rfc3164(const gchar *ts, gint len, gchar isotimestamp[32])
{
UnixTime stamp;
- const guchar *data = (const guchar *) ts;
- gint length = strlen(ts);
+ const guchar *tsu = (const guchar *) ts;
+ gint tsu_len = len < 0 ? strlen(ts) : len;
GString *result = g_string_new("");
WallClockTime wct = WALL_CLOCK_TIME_INIT;
-
+ const guchar *data = tsu;
+ gint length = tsu_len;
gboolean success = scan_rfc3164_timestamp(&data, &length, &wct);
+ cr_assert(length >= 0);
+ cr_assert(data == &tsu[tsu_len - length]);
+
unix_time_unset(&stamp);
convert_wall_clock_time_to_unix_time(&wct, &stamp);
@@ -70,16 +74,21 @@ _parse_rfc3164(const gchar *ts, gchar isotimestamp[32])
}
static gboolean
-_parse_rfc5424(const gchar *ts, gchar isotimestamp[32])
+_parse_rfc5424(const gchar *ts, gint len, gchar isotimestamp[32])
{
UnixTime stamp;
- const guchar *data = (const guchar *) ts;
- gint length = strlen(ts);
+ const guchar *tsu = (const guchar *) ts;
+ gint tsu_len = len < 0 ? strlen(ts) : len;
GString *result = g_string_new("");
WallClockTime wct = WALL_CLOCK_TIME_INIT;
+ const guchar *data = tsu;
+ gint length = tsu_len;
gboolean success = scan_rfc5424_timestamp(&data, &length, &wct);
+ cr_assert(length >= 0);
+ cr_assert(data == &tsu[tsu_len - length]);
+
unix_time_unset(&stamp);
convert_wall_clock_time_to_unix_time(&wct, &stamp);
@@ -90,31 +99,60 @@ _parse_rfc5424(const gchar *ts, gchar isotimestamp[32])
}
static gboolean
-_rfc3164_timestamp_eq(const gchar *ts, const gchar *expected, gchar converted[32])
+_rfc3164_timestamp_eq(const gchar *ts, gint len, const gchar *expected, gchar converted[32])
{
- cr_assert(_parse_rfc3164(ts, converted));
+ cr_assert(_parse_rfc3164(ts, len, converted));
return strcmp(converted, expected) == 0;
}
static gboolean
-_rfc5424_timestamp_eq(const gchar *ts, const gchar *expected, gchar converted[32])
+_rfc5424_timestamp_eq(const gchar *ts, gint len, const gchar *expected, gchar converted[32])
{
- cr_assert(_parse_rfc5424(ts, converted));
+ cr_assert(_parse_rfc5424(ts, len, converted));
return strcmp(converted, expected) == 0;
}
#define _expect_rfc3164_timestamp_eq(ts, expected) \
({ \
gchar converted[32]; \
- cr_expect(_rfc3164_timestamp_eq(ts, expected, converted), "Parsed RFC3164 timestamp does not equal expected, ts=%s, converted=%s, expected=%s", ts, converted, expected); \
+ cr_expect(_rfc3164_timestamp_eq(ts, -1, expected, converted), "Parsed RFC3164 timestamp does not equal expected, ts=%s, converted=%s, expected=%s", ts, converted, expected); \
+ })
+
+#define _expect_rfc3164_timestamp_len_eq(ts, len, expected) \
+ ({ \
+ gchar converted[32]; \
+ cr_expect(_rfc3164_timestamp_eq(ts, len, expected, converted), "Parsed RFC3164 timestamp does not equal expected, ts=%s, converted=%s, expected=%s", ts, converted, expected); \
+ })
+
+#define _expect_rfc3164_fails(ts, len) \
+ ({ \
+ WallClockTime wct = WALL_CLOCK_TIME_INIT; \
+ const guchar *data = (guchar *) ts; \
+ gint length = len < 0 ? strlen(ts) : len; \
+ cr_assert_not(scan_rfc3164_timestamp(&data, &length, &wct)); \
})
#define _expect_rfc5424_timestamp_eq(ts, expected) \
({ \
gchar converted[32]; \
- cr_expect(_rfc5424_timestamp_eq(ts, expected, converted), "Parsed RFC5424 timestamp does not equal expected, ts=%s, converted=%s, expected=%s", ts, converted, expected); \
+ cr_expect(_rfc5424_timestamp_eq(ts, -1, expected, converted), "Parsed RFC5424 timestamp does not equal expected, ts=%s, converted=%s, expected=%s", ts, converted, expected); \
+ })
+
+#define _expect_rfc5424_timestamp_len_eq(ts, len, expected) \
+ ({ \
+ gchar converted[32]; \
+ cr_expect(_rfc5424_timestamp_eq(ts, len, expected, converted), "Parsed RFC5424 timestamp does not equal expected, ts=%s, converted=%s, expected=%s", ts, converted, expected); \
+ })
+
+#define _expect_rfc5424_fails(ts, len) \
+ ({ \
+ WallClockTime wct = WALL_CLOCK_TIME_INIT; \
+ const guchar *data = (guchar *) ts; \
+ gint length = len < 0 ? strlen(ts) : len; \
+ cr_assert_not(scan_rfc5424_timestamp(&data, &length, &wct)); \
})
+
Test(parse_timestamp, standard_bsd_format)
{
_expect_rfc3164_timestamp_eq("Oct 1 17:46:12", "2017-10-01T17:46:12.000+02:00");
@@ -148,6 +186,68 @@ Test(parse_timestamp, standard_bsd_format_year_in_the_past)
_expect_rfc3164_timestamp_eq("Dec 31 17:46:12", "2017-12-31T17:46:12.000+01:00");
}
+Test(parse_timestamp, non_zero_terminated_rfc3164_iso_input_is_handled_properly)
+{
+ gchar *ts = "2022-08-17T05:02:28.417Z whatever";
+ gint ts_len = 24;
+
+ _expect_rfc3164_timestamp_len_eq(ts, strlen(ts), "2022-08-17T05:02:28.417+00:00");
+ _expect_rfc3164_timestamp_len_eq(ts, ts_len + 5, "2022-08-17T05:02:28.417+00:00");
+ _expect_rfc3164_timestamp_len_eq(ts, ts_len, "2022-08-17T05:02:28.417+00:00");
+
+ /* no "Z" parsed, timezone defaults to local, forced CET */
+ _expect_rfc3164_timestamp_len_eq(ts, ts_len - 1, "2022-08-17T05:02:28.417+02:00");
+
+ /* msec is partially parsed as we trim the string from the right */
+ _expect_rfc3164_timestamp_len_eq(ts, ts_len - 2, "2022-08-17T05:02:28.410+02:00");
+ _expect_rfc3164_timestamp_len_eq(ts, ts_len - 3, "2022-08-17T05:02:28.400+02:00");
+ _expect_rfc3164_timestamp_len_eq(ts, ts_len - 4, "2022-08-17T05:02:28.000+02:00");
+ _expect_rfc3164_timestamp_len_eq(ts, ts_len - 5, "2022-08-17T05:02:28.000+02:00");
+
+ for (gint i = 6; i < ts_len; i++)
+ _expect_rfc3164_fails(ts, ts_len - i);
+
+}
+
+Test(parse_timestamp, non_zero_terminated_rfc3164_bsd_pix_or_asa_input_is_handled_properly)
+{
+ gchar *ts = "Aug 17 2022 05:02:28: whatever";
+ gint ts_len = 21;
+
+ _expect_rfc3164_timestamp_len_eq(ts, strlen(ts), "2022-08-17T05:02:28.000+02:00");
+ _expect_rfc3164_timestamp_len_eq(ts, ts_len + 5, "2022-08-17T05:02:28.000+02:00");
+ _expect_rfc3164_timestamp_len_eq(ts, ts_len, "2022-08-17T05:02:28.000+02:00");
+
+ /* no ":" at the end, that's a problem, unrecognized */
+ _expect_rfc3164_fails(ts, ts_len - 1);
+
+ for (gint i = 1; i < ts_len; i++)
+ _expect_rfc3164_fails(ts, ts_len - i);
+}
+
+Test(parse_timestamp, non_zero_terminated_rfc5424_input_is_handled_properly)
+{
+ gchar *ts = "2022-08-17T05:02:28.417Z whatever";
+ gint ts_len = 24;
+
+ _expect_rfc5424_timestamp_len_eq(ts, strlen(ts), "2022-08-17T05:02:28.417+00:00");
+ _expect_rfc5424_timestamp_len_eq(ts, ts_len + 5, "2022-08-17T05:02:28.417+00:00");
+ _expect_rfc5424_timestamp_len_eq(ts, ts_len, "2022-08-17T05:02:28.417+00:00");
+
+ /* no "Z" parsed, timezone defaults to local, forced CET */
+ _expect_rfc5424_timestamp_len_eq(ts, ts_len - 1, "2022-08-17T05:02:28.417+02:00");
+
+ /* msec is partially parsed as we trim the string from the right */
+ _expect_rfc5424_timestamp_len_eq(ts, ts_len - 2, "2022-08-17T05:02:28.410+02:00");
+ _expect_rfc5424_timestamp_len_eq(ts, ts_len - 3, "2022-08-17T05:02:28.400+02:00");
+ _expect_rfc5424_timestamp_len_eq(ts, ts_len - 4, "2022-08-17T05:02:28.000+02:00");
+ _expect_rfc5424_timestamp_len_eq(ts, ts_len - 5, "2022-08-17T05:02:28.000+02:00");
+
+ for (gint i = 6; i < ts_len; i++)
+ _expect_rfc5424_fails(ts, ts_len - i);
+
+}
+
Test(parse_timestamp, daylight_saving_behavior_at_spring_with_explicit_timezones)
{
|