19034852cSGleb Smirnoff #include "config.h"
29034852cSGleb Smirnoff #include "unity.h"
39034852cSGleb Smirnoff #include "ntp_types.h"
49034852cSGleb Smirnoff
59034852cSGleb Smirnoff
69034852cSGleb Smirnoff #include "log.c"
79034852cSGleb Smirnoff
8*3311ff84SXin LI void setUp(void);
99034852cSGleb Smirnoff void testChangePrognameInMysyslog(void);
109034852cSGleb Smirnoff void testOpenLogfileTest(void);
11*3311ff84SXin LI void testWriteInCustomLogfile(void);
12*3311ff84SXin LI
13*3311ff84SXin LI
14*3311ff84SXin LI void
setUp(void)15*3311ff84SXin LI setUp(void) {
16*3311ff84SXin LI init_lib();
17*3311ff84SXin LI }
189034852cSGleb Smirnoff
199034852cSGleb Smirnoff
209034852cSGleb Smirnoff //in var/log/syslog (may differ depending on your OS), logged name of the program will be "TEST_PROGNAME".
219034852cSGleb Smirnoff
22*3311ff84SXin LI void
testChangePrognameInMysyslog(void)23*3311ff84SXin LI testChangePrognameInMysyslog(void)
24*3311ff84SXin LI {
259034852cSGleb Smirnoff sntp_init_logging("TEST_PROGNAME");
26*3311ff84SXin LI msyslog(LOG_ERR, "TESTING sntp_init_logging()");
27*3311ff84SXin LI
28*3311ff84SXin LI return;
299034852cSGleb Smirnoff }
309034852cSGleb Smirnoff
319034852cSGleb Smirnoff //writes log files in your own file instead of syslog! (MAY BE USEFUL TO SUPPRESS ERROR MESSAGES!)
329034852cSGleb Smirnoff
33*3311ff84SXin LI void
testOpenLogfileTest(void)34*3311ff84SXin LI testOpenLogfileTest(void)
35*3311ff84SXin LI {
369034852cSGleb Smirnoff sntp_init_logging("TEST_PROGNAME2"); //this name is consistent through the entire program unless changed
379034852cSGleb Smirnoff open_logfile("testLogfile.log");
389034852cSGleb Smirnoff //open_logfile("/var/log/syslog"); //this gives me "Permission Denied" when i do %m
399034852cSGleb Smirnoff
409034852cSGleb Smirnoff msyslog(LOG_ERR, "Cannot open log file %s","abcXX");
419034852cSGleb Smirnoff //cleanup_log(); //unnecessary after log.c fix!
429034852cSGleb Smirnoff
43*3311ff84SXin LI return;
449034852cSGleb Smirnoff }
459034852cSGleb Smirnoff
469034852cSGleb Smirnoff
479034852cSGleb Smirnoff //multiple cleanup_log() causes segfault. Probably the reason it's static. Opening multiple open_logfile(name) will cause segfault x.x I'm guessing it's not intended to be changed. Cleanup after unity test doesn't fix it, looks like. Calling in tearDown() also causes issues.
489034852cSGleb Smirnoff
49*3311ff84SXin LI void
testWriteInCustomLogfile(void)50*3311ff84SXin LI testWriteInCustomLogfile(void)
51*3311ff84SXin LI {
529034852cSGleb Smirnoff char testString[256] = "12345 ABC";
539034852cSGleb Smirnoff char testName[256] = "TEST_PROGNAME3";
549034852cSGleb Smirnoff
55*3311ff84SXin LI (void)remove("testLogfile2.log");
569034852cSGleb Smirnoff
579034852cSGleb Smirnoff sntp_init_logging(testName);
589034852cSGleb Smirnoff open_logfile("testLogfile2.log"); // ./ causing issues
599034852cSGleb Smirnoff //sntp_init_logging(testName);
609034852cSGleb Smirnoff
619034852cSGleb Smirnoff
62*3311ff84SXin LI msyslog(LOG_ERR, "%s", testString);
639034852cSGleb Smirnoff FILE * f = fopen("testLogfile2.log","r");
649034852cSGleb Smirnoff char line[256];
659034852cSGleb Smirnoff
66*3311ff84SXin LI TEST_ASSERT_TRUE( f != NULL);
67*3311ff84SXin LI
689034852cSGleb Smirnoff //should be only 1 line
699034852cSGleb Smirnoff while (fgets(line, sizeof(line), f)) {
709034852cSGleb Smirnoff printf("%s", line);
719034852cSGleb Smirnoff }
729034852cSGleb Smirnoff
739034852cSGleb Smirnoff
749034852cSGleb Smirnoff char* x = strstr(line,testName);
759034852cSGleb Smirnoff
769034852cSGleb Smirnoff TEST_ASSERT_TRUE( x != NULL);
779034852cSGleb Smirnoff
789034852cSGleb Smirnoff x = strstr(line,testString);
799034852cSGleb Smirnoff TEST_ASSERT_TRUE( x != NULL);
809034852cSGleb Smirnoff //cleanup_log();
819034852cSGleb Smirnoff fclose(f); //using this will also cause segfault, because at the end, log.c will call (using atexit(func) function) cleanup_log(void)-> fclose(syslog_file);
829034852cSGleb Smirnoff //After the 1st fclose, syslog_file = NULL, and is never reset -> hopefully fixed by editing log.c
839034852cSGleb Smirnoff //TEST_ASSERT_EQUAL_STRING(testString,line); //doesn't work, line is dynamic because the process name is random.
84*3311ff84SXin LI
85*3311ff84SXin LI return;
869034852cSGleb Smirnoff }
87