19034852cSGleb Smirnoff #include "config.h" 29034852cSGleb Smirnoff #include "unity.h" 39034852cSGleb Smirnoff #include "ntp_types.h" 49034852cSGleb Smirnoff 59034852cSGleb Smirnoff 69034852cSGleb Smirnoff //#include "log.h" 79034852cSGleb Smirnoff #include "log.c" 89034852cSGleb Smirnoff 9*3311ff84SXin LI void setUp(void); 109034852cSGleb Smirnoff void testChangePrognameInMysyslog(void); 119034852cSGleb Smirnoff void testOpenLogfileTest(void); 12*3311ff84SXin LI void testWriteInCustomLogfile(void); 13*3311ff84SXin LI 14*3311ff84SXin LI 15*3311ff84SXin LI void 16*3311ff84SXin LI setUp(void) { 17*3311ff84SXin LI init_lib(); 18*3311ff84SXin LI } 199034852cSGleb Smirnoff 209034852cSGleb Smirnoff 219034852cSGleb Smirnoff //in var/log/syslog (may differ depending on your OS), logged name of the program will be "TEST_PROGNAME". 229034852cSGleb Smirnoff 23*3311ff84SXin LI void 24*3311ff84SXin LI testChangePrognameInMysyslog(void) 25*3311ff84SXin LI { 269034852cSGleb Smirnoff sntp_init_logging("TEST_PROGNAME"); 27*3311ff84SXin LI msyslog(LOG_ERR, "TESTING sntp_init_logging()"); 28*3311ff84SXin LI 29*3311ff84SXin LI return; 309034852cSGleb Smirnoff } 319034852cSGleb Smirnoff 329034852cSGleb Smirnoff //writes log files in your own file instead of syslog! (MAY BE USEFUL TO SUPPRESS ERROR MESSAGES!) 339034852cSGleb Smirnoff 34*3311ff84SXin LI void 35*3311ff84SXin LI testOpenLogfileTest(void) 36*3311ff84SXin LI { 379034852cSGleb Smirnoff sntp_init_logging("TEST_PROGNAME2"); //this name is consistent through the entire program unless changed 389034852cSGleb Smirnoff open_logfile("testLogfile.log"); 399034852cSGleb Smirnoff //open_logfile("/var/log/syslog"); //this gives me "Permission Denied" when i do %m 409034852cSGleb Smirnoff 419034852cSGleb Smirnoff msyslog(LOG_ERR, "Cannot open log file %s","abcXX"); 429034852cSGleb Smirnoff //cleanup_log(); //unnecessary after log.c fix! 439034852cSGleb Smirnoff 44*3311ff84SXin LI return; 459034852cSGleb Smirnoff } 469034852cSGleb Smirnoff 479034852cSGleb Smirnoff 489034852cSGleb 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. 499034852cSGleb Smirnoff 50*3311ff84SXin LI void 51*3311ff84SXin LI testWriteInCustomLogfile(void) 52*3311ff84SXin LI { 539034852cSGleb Smirnoff char testString[256] = "12345 ABC"; 549034852cSGleb Smirnoff char testName[256] = "TEST_PROGNAME3"; 559034852cSGleb Smirnoff 56*3311ff84SXin LI (void)remove("testLogfile2.log"); 579034852cSGleb Smirnoff 589034852cSGleb Smirnoff sntp_init_logging(testName); 599034852cSGleb Smirnoff open_logfile("testLogfile2.log"); // ./ causing issues 609034852cSGleb Smirnoff //sntp_init_logging(testName); 619034852cSGleb Smirnoff 629034852cSGleb Smirnoff 63*3311ff84SXin LI msyslog(LOG_ERR, "%s", testString); 649034852cSGleb Smirnoff FILE * f = fopen("testLogfile2.log","r"); 659034852cSGleb Smirnoff char line[256]; 669034852cSGleb Smirnoff 67*3311ff84SXin LI TEST_ASSERT_TRUE( f != NULL); 68*3311ff84SXin LI 699034852cSGleb Smirnoff //should be only 1 line 709034852cSGleb Smirnoff while (fgets(line, sizeof(line), f)) { 719034852cSGleb Smirnoff printf("%s", line); 729034852cSGleb Smirnoff } 739034852cSGleb Smirnoff 749034852cSGleb Smirnoff 759034852cSGleb Smirnoff char* x = strstr(line,testName); 769034852cSGleb Smirnoff 779034852cSGleb Smirnoff TEST_ASSERT_TRUE( x != NULL); 789034852cSGleb Smirnoff 799034852cSGleb Smirnoff x = strstr(line,testString); 809034852cSGleb Smirnoff TEST_ASSERT_TRUE( x != NULL); 819034852cSGleb Smirnoff //cleanup_log(); 829034852cSGleb 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); 839034852cSGleb Smirnoff //After the 1st fclose, syslog_file = NULL, and is never reset -> hopefully fixed by editing log.c 849034852cSGleb Smirnoff //TEST_ASSERT_EQUAL_STRING(testString,line); //doesn't work, line is dynamic because the process name is random. 85*3311ff84SXin LI 86*3311ff84SXin LI return; 879034852cSGleb Smirnoff } 88