1*eabc0478Schristos /* $NetBSD: t-log.c,v 1.3 2024/08/18 20:47:26 christos Exp $ */ 2067f5680Schristos 3a6f3f22fSchristos #include "config.h" 4a6f3f22fSchristos #include "unity.h" 5a6f3f22fSchristos #include "ntp_types.h" 6a6f3f22fSchristos 7a6f3f22fSchristos 8a6f3f22fSchristos #include "log.c" 9a6f3f22fSchristos 104c290c01Schristos void setUp(void); 11a6f3f22fSchristos void testChangePrognameInMysyslog(void); 12a6f3f22fSchristos void testOpenLogfileTest(void); 134c290c01Schristos void testWriteInCustomLogfile(void); 144c290c01Schristos 154c290c01Schristos 164c290c01Schristos void 174c290c01Schristos setUp(void) { 184c290c01Schristos init_lib(); 194c290c01Schristos } 20a6f3f22fSchristos 21a6f3f22fSchristos 22a6f3f22fSchristos //in var/log/syslog (may differ depending on your OS), logged name of the program will be "TEST_PROGNAME". 23a6f3f22fSchristos 244c290c01Schristos void 254c290c01Schristos testChangePrognameInMysyslog(void) 264c290c01Schristos { 27a6f3f22fSchristos sntp_init_logging("TEST_PROGNAME"); 284c290c01Schristos msyslog(LOG_ERR, "TESTING sntp_init_logging()"); 294c290c01Schristos 304c290c01Schristos return; 31a6f3f22fSchristos } 32a6f3f22fSchristos 33a6f3f22fSchristos //writes log files in your own file instead of syslog! (MAY BE USEFUL TO SUPPRESS ERROR MESSAGES!) 34a6f3f22fSchristos 354c290c01Schristos void 364c290c01Schristos testOpenLogfileTest(void) 374c290c01Schristos { 38a6f3f22fSchristos sntp_init_logging("TEST_PROGNAME2"); //this name is consistent through the entire program unless changed 39a6f3f22fSchristos open_logfile("testLogfile.log"); 40a6f3f22fSchristos //open_logfile("/var/log/syslog"); //this gives me "Permission Denied" when i do %m 41a6f3f22fSchristos 42a6f3f22fSchristos msyslog(LOG_ERR, "Cannot open log file %s","abcXX"); 43a6f3f22fSchristos //cleanup_log(); //unnecessary after log.c fix! 44a6f3f22fSchristos 454c290c01Schristos return; 46a6f3f22fSchristos } 47a6f3f22fSchristos 48a6f3f22fSchristos 49a6f3f22fSchristos //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. 50a6f3f22fSchristos 514c290c01Schristos void 524c290c01Schristos testWriteInCustomLogfile(void) 534c290c01Schristos { 54a6f3f22fSchristos char testString[256] = "12345 ABC"; 55a6f3f22fSchristos char testName[256] = "TEST_PROGNAME3"; 56a6f3f22fSchristos 574c290c01Schristos (void)remove("testLogfile2.log"); 58a6f3f22fSchristos 59a6f3f22fSchristos sntp_init_logging(testName); 60a6f3f22fSchristos open_logfile("testLogfile2.log"); // ./ causing issues 61a6f3f22fSchristos //sntp_init_logging(testName); 62a6f3f22fSchristos 63a6f3f22fSchristos 644c290c01Schristos msyslog(LOG_ERR, "%s", testString); 65a6f3f22fSchristos FILE * f = fopen("testLogfile2.log","r"); 66a6f3f22fSchristos char line[256]; 67a6f3f22fSchristos 684c290c01Schristos TEST_ASSERT_TRUE( f != NULL); 694c290c01Schristos 70a6f3f22fSchristos //should be only 1 line 71a6f3f22fSchristos while (fgets(line, sizeof(line), f)) { 72a6f3f22fSchristos printf("%s", line); 73a6f3f22fSchristos } 74a6f3f22fSchristos 75a6f3f22fSchristos 76a6f3f22fSchristos char* x = strstr(line,testName); 77a6f3f22fSchristos 78a6f3f22fSchristos TEST_ASSERT_TRUE( x != NULL); 79a6f3f22fSchristos 80a6f3f22fSchristos x = strstr(line,testString); 81a6f3f22fSchristos TEST_ASSERT_TRUE( x != NULL); 82a6f3f22fSchristos //cleanup_log(); 83a6f3f22fSchristos 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); 84a6f3f22fSchristos //After the 1st fclose, syslog_file = NULL, and is never reset -> hopefully fixed by editing log.c 85a6f3f22fSchristos //TEST_ASSERT_EQUAL_STRING(testString,line); //doesn't work, line is dynamic because the process name is random. 864c290c01Schristos 874c290c01Schristos return; 88a6f3f22fSchristos } 89