/* libsyslogp/src/tests/regr.c * * (c)2009, Laurence Withers, . * Released under the GNU GPLv3. See file COPYING or * http://www.gnu.org/copyleft/gpl.html for details. */ #include "syslogp.h" #include #include #include #include #define ISO_DAY_20081106 (733717) #define ISO_SEC_1700 (17 * 3600) const struct syslog_msg exp_simplecron = { .level = LOG_NOTICE, .facility = LOG_USER, .timestamp = { .day = ISO_DAY_20081106, .sec = ISO_SEC_1700, }, .hostname = "amethyst", .tag = "cron", .id = "31311", .msg = "(root) CMD (rm -f /var/spool/cron/lastrun/cron.hourly)", }; const struct syslog_msg exp_rfc5424cron = { .level = LOG_NOTICE, .facility = LOG_USER, .timestamp = { .day = ISO_DAY_20081106, .sec = ISO_SEC_1700, }, .hostname = "amethyst", .tag = "cron", .id = "31311", .msg = "- (root) CMD (rm -f /var/spool/cron/lastrun/cron.hourly)", }; struct test { const char* name; const char* orig; char* string; const struct syslog_msg* exp; }; struct test tests[] = { { .name = "bsd", .orig = "<13>Nov 6 17:00:00 amethyst cron[31311]: (root) CMD (rm -f /var/spool/cron/lastrun/cron.hourly)", .exp = &exp_simplecron, }, { .name = "space_bsd", .orig = "<13> Nov 6 17:00:00 amethyst cron[31311]: (root) CMD (rm -f /var/spool/cron/lastrun/cron.hourly)", .exp = &exp_simplecron, }, { .name = "bsd_newline", .orig = "<13>Nov 6 17:00:00 amethyst cron[31311]: (root) CMD (rm -f /var/spool/cron/lastrun/cron.hourly)\n", .exp = &exp_simplecron, }, { .name = "nopri_bsd_newline", .orig = "Nov 6 17:00:00 amethyst cron[31311]: (root) CMD (rm -f /var/spool/cron/lastrun/cron.hourly)\n", .exp = &exp_simplecron, }, { .name = "bsd_secfrac", .orig = "<13>Nov 6 17:00:00.000 amethyst cron[31311]: (root) CMD (rm -f /var/spool/cron/lastrun/cron.hourly)\n", .exp = &exp_simplecron, }, { .name = "linksys", .orig = "<13>Nov 6 17:00:00 2008 amethyst cron[31311]: (root) CMD (rm -f /var/spool/cron/lastrun/cron.hourly)\n", .exp = &exp_simplecron, }, { .name = "space_linksys", .orig = "<13> Nov 6 17:00:00 2008 amethyst cron[31311]: (root) CMD (rm -f /var/spool/cron/lastrun/cron.hourly)\n", .exp = &exp_simplecron, }, { .name = "pix", .orig = "<13>Nov 6 2008 17:00:00 amethyst cron[31311]: (root) CMD (rm -f /var/spool/cron/lastrun/cron.hourly)\n", .exp = &exp_simplecron, }, { .name = "space_pix", .orig = "<13> Nov 6 2008 17:00:00 amethyst cron[31311]: (root) CMD (rm -f /var/spool/cron/lastrun/cron.hourly)\n", .exp = &exp_simplecron, }, { .name = "iso", .orig = "<13>2008-11-06T17:00:00Z amethyst cron[31311]: (root) CMD (rm -f /var/spool/cron/lastrun/cron.hourly)\n", .exp = &exp_simplecron, }, { .name = "_iso", .orig = "<13> 2008-11-06T17:00:00Z amethyst cron[31311]: (root) CMD (rm -f /var/spool/cron/lastrun/cron.hourly)\n", .exp = &exp_simplecron, }, { .name = "rfc5424", .orig = "<13>1 2008-11-06T17:00:00Z amethyst cron 31311 - (root) CMD (rm -f /var/spool/cron/lastrun/cron.hourly)\n", .exp = &exp_rfc5424cron, }, { .name = 0, } }; int compare_msg(const struct syslog_msg* a, const struct syslog_msg* b) { if(a->level != b->level) return -1; if(a->facility != b->facility) return -1; if(iso8601_cmp(&a->timestamp, &b->timestamp)) return -1; #define CMP_STR(str) do { \ if((a->str && !b->str) || (b->str && !a->str)) return -1; \ if(a->str && strcmp(a->str, b->str)) return -1; \ }while(0) CMP_STR(hostname); CMP_STR(tag); CMP_STR(id); CMP_STR(msg); #undef CMP_STR return 0; } struct iso8601_details iso_details = { .date_prec = iso8601_prec_day, .time_prec = iso8601_prec_secfrac, .extended = 1, }; void dump_msg(const char* label, const struct syslog_msg* msg) { char isodate[40]; printf("======== %s ========\n" "Level : %d (%s)\n" "Facility : %d (%s)\n" "Timestamp: %s\n" "Hostname : '%s'\n" "Tag : '%s'\n" "ID : '%s'\n" "Message : ā€œ%sā€\n\n", label, msg->level, syslog_level_name(msg->level), msg->facility, syslog_facility_name(msg->facility), iso8601_print(isodate, sizeof(isodate), &msg->timestamp, &iso_details), msg->hostname, msg->tag, msg->id, msg->msg ); } int main(int argc, char* argv[]) { int ret = 0; struct test* test; struct syslog_msg msg_result; if(argc == 2 && !strcmp(argv[1], "--print-summary")) { fputs("Automated regression test.\n", stdout); return 0; } syslog_parse_set_implied_year(2008); for(test = tests; test->name; ++test) { /* operate on a copy of the string (constant is read only) */ test->string = strdup(test->orig); syslog_parse(&msg_result, test->string); /* compare the result, testing for and dumping errors */ ret = compare_msg(&msg_result, test->exp); printf("%s: %s\n", test->name, ret ? "FAILED" : "passed"); if(ret) { printf("*** Test failed while parsing:\nā€œ%sā€\n\n", test->orig); dump_msg("Expected result", test->exp); dump_msg("Actual result", &msg_result); break; } } return ret; } /* options for text editors kate: replace-trailing-space-save true; space-indent true; tab-width 4; vim: expandtab:ts=4:sw=4 */