Browse Source

Add tests on ttail_logline2date() function

Yann Weber 7 years ago
parent
commit
55b331a15e
3 changed files with 78 additions and 4 deletions
  1. 1
    1
      src/include/ttail_search.h
  2. 7
    1
      src/ttail_search.c
  3. 70
    2
      tests/ttail_search_check.c

+ 1
- 1
src/include/ttail_search.h View File

@@ -74,7 +74,7 @@ ttail_cmp_res ttail_cmp_logline(ttail_t*, const char*, struct tm*);
74 74
  *@param ttail_t* ttail
75 75
  *@param const char* logline
76 76
  *@param struct tm* tm will be set to extracted date if not NULL
77
- *@return 0 if ok, -1 if error 1 if no date found
77
+ *@return 0 if ok, -1 if error 1 if no prefix found 2 if no date found
78 78
  */
79 79
 int ttail_logline2date(ttail_t*, const char*, struct tm*);
80 80
 

+ 7
- 1
src/ttail_search.c View File

@@ -35,10 +35,16 @@ int ttail_logline2date(ttail_t* ttail, const char* logline, struct tm* tm)
35 35
 	subst = ttail_logline_subst(ttail, logline);
36 36
 	if(!subst)
37 37
 	{
38
+		memset(tm, 0,sizeof(struct tm));
38 39
 		return 1;
39 40
 	}
40 41
 	ret = strptime(subst, ttail->fmt, tm);
41
-	return ret?0:1;
42
+	if(!ret)
43
+	{
44
+		memset(tm, 0,sizeof(struct tm));
45
+		return 2;
46
+	}
47
+	return 0;
42 48
 }
43 49
 
44 50
 const char* ttail_logline_subst(ttail_t* t, const char* logline)

+ 70
- 2
tests/ttail_search_check.c View File

@@ -238,12 +238,72 @@ START_TEST (test_search_subst_re_nomatch)
238 238
 }
239 239
 END_TEST
240 240
 
241
+/*
242
+ * ttail_logline2date() checks
243
+ */
244
+START_TEST (test_search_log2date1)
245
+{
246
+	char re[] = "^[0-9]+ ";
247
+	char fmt[] = "%Y-%m-%d:%H:%M";
248
+	struct tm tm;
249
+	int ret;
250
+	ttail_set_flag_re_ex(ttail);
251
+	ttail_set_prefix(ttail, re);
252
+	ttail_set_fmt(ttail, fmt);
253
+	ret = ttail_logline2date(ttail, "1337 1988-10-22:22:10 foobar", &tm);
254
+	ck_assert_int_eq(ret, 0);
255
+	ck_assert_int_eq(tm.tm_year, 88);
256
+	ck_assert_int_eq(tm.tm_mon, 9);
257
+	ck_assert_int_eq(tm.tm_mday, 22);
258
+	ck_assert_int_eq(tm.tm_hour, 22);
259
+	ck_assert_int_eq(tm.tm_min, 10);
260
+}
261
+END_TEST
262
+
263
+START_TEST (test_search_log2date_failpref)
264
+{
265
+	char re[] = "^[0-9]+aa ";
266
+	char fmt[] = "%Y-%m-%d:%H:%M";
267
+	struct tm tm;
268
+	int ret;
269
+	ttail_set_flag_re_ex(ttail);
270
+	ttail_set_prefix(ttail, re);
271
+	ttail_set_fmt(ttail, fmt);
272
+	ret = ttail_logline2date(ttail, "1337 1988-10-22:22:10 foobar", &tm);
273
+	ck_assert_int_eq(ret, 1);
274
+	ck_assert_int_eq(tm.tm_year, 0);
275
+	ck_assert_int_eq(tm.tm_mon, 0);
276
+	ck_assert_int_eq(tm.tm_mday, 0);
277
+	ck_assert_int_eq(tm.tm_hour, 0);
278
+	ck_assert_int_eq(tm.tm_min, 0);
279
+}
280
+END_TEST
281
+
282
+START_TEST (test_search_log2date_faildate)
283
+{
284
+	char re[] = "^[0-9]+ ";
285
+	char fmt[] = "%y-%m-%d:%H:%M";
286
+	struct tm tm;
287
+	int ret;
288
+	ttail_set_flag_re_ex(ttail);
289
+	ttail_set_prefix(ttail, re);
290
+	ttail_set_fmt(ttail, fmt);
291
+	ret = ttail_logline2date(ttail, "1337 1988-10-22:22:10 foobar", &tm);
292
+	ck_assert_int_eq(ret, 2);
293
+	ck_assert_int_eq(tm.tm_year, 0);
294
+	ck_assert_int_eq(tm.tm_mon, 0);
295
+	ck_assert_int_eq(tm.tm_mday, 0);
296
+	ck_assert_int_eq(tm.tm_hour, 0);
297
+	ck_assert_int_eq(tm.tm_min, 0);
298
+}
299
+END_TEST
300
+
241 301
 Suite * ttail_search_files_suite(void)
242 302
 {
243 303
 	Suite *s;
244
-	TCase *tc_search_subst;
245 304
 	TCase *tc_search_closest_fileinit, *tc_file_line,
246 305
 		*tc_file_minmax;
306
+	TCase *tc_search_subst, *tc_search_logline2date;
247 307
 
248 308
 	s = suite_create("ttail search_files checks");
249 309
 	tc_search_closest_fileinit = tcase_create("\
@@ -276,10 +336,18 @@ ttail_logline_closest_files_init() checks");
276 336
 	tcase_add_test(tc_search_subst, test_search_subst_re2);
277 337
 	tcase_add_test(tc_search_subst, test_search_subst_re_nomatch);
278 338
 
279
-	suite_add_tcase(s, tc_search_subst);
339
+	tc_search_logline2date = tcase_create("ttail_logline2date() checks");
340
+	tcase_add_checked_fixture(tc_search_logline2date,
341
+		setup_closest_fileinit, teardown_closest_fileinit);
342
+	tcase_add_test(tc_search_subst, test_search_log2date1);
343
+	tcase_add_test(tc_search_subst, test_search_log2date_failpref);
344
+	tcase_add_test(tc_search_subst, test_search_log2date_faildate);
345
+
280 346
 	suite_add_tcase(s, tc_search_closest_fileinit);
281 347
 	suite_add_tcase(s, tc_file_line);
282 348
 	suite_add_tcase(s, tc_file_minmax);
349
+	suite_add_tcase(s, tc_search_subst);
350
+	suite_add_tcase(s, tc_search_logline2date);
283 351
 	return s;
284 352
 }
285 353
 

Loading…
Cancel
Save