summaryrefslogtreecommitdiffstats
path: root/contrib/ntp/sntp/tests/t-log.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/ntp/sntp/tests/t-log.c')
-rw-r--r--contrib/ntp/sntp/tests/t-log.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/contrib/ntp/sntp/tests/t-log.c b/contrib/ntp/sntp/tests/t-log.c
new file mode 100644
index 0000000..1546584
--- /dev/null
+++ b/contrib/ntp/sntp/tests/t-log.c
@@ -0,0 +1,68 @@
+#include "config.h"
+#include "unity.h"
+#include "ntp_types.h"
+
+
+//#include "log.h"
+#include "log.c"
+
+void testChangePrognameInMysyslog(void);
+void testOpenLogfileTest(void);
+
+
+//in var/log/syslog (may differ depending on your OS), logged name of the program will be "TEST_PROGNAME".
+
+void testChangePrognameInMysyslog(void){
+ sntp_init_logging("TEST_PROGNAME");
+ msyslog(LOG_ERR, "TESTING sntp_init_logging()"); //%m will print the last errno?
+}
+
+//writes log files in your own file instead of syslog! (MAY BE USEFUL TO SUPPRESS ERROR MESSAGES!)
+
+void testOpenLogfileTest(void){
+ sntp_init_logging("TEST_PROGNAME2"); //this name is consistent through the entire program unless changed
+ open_logfile("testLogfile.log");
+ //open_logfile("/var/log/syslog"); //this gives me "Permission Denied" when i do %m
+
+ msyslog(LOG_ERR, "Cannot open log file %s","abcXX");
+ //cleanup_log(); //unnecessary after log.c fix!
+
+}
+
+
+//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.
+
+void testWriteInCustomLogfile(void){
+ char testString[256] = "12345 ABC";
+ char testName[256] = "TEST_PROGNAME3";
+
+ remove("testLogfile2.log");
+
+ sntp_init_logging(testName);
+ open_logfile("testLogfile2.log"); // ./ causing issues
+ //sntp_init_logging(testName);
+
+
+ msyslog(LOG_ERR, testString);
+ FILE * f = fopen("testLogfile2.log","r");
+ char line[256];
+
+ //should be only 1 line
+ while (fgets(line, sizeof(line), f)) {
+ printf("%s", line);
+ }
+
+
+ char* x = strstr(line,testName);
+
+ TEST_ASSERT_TRUE( x != NULL);
+
+ x = strstr(line,testString);
+ TEST_ASSERT_TRUE( x != NULL);
+ //cleanup_log();
+ 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);
+ //After the 1st fclose, syslog_file = NULL, and is never reset -> hopefully fixed by editing log.c
+ //TEST_ASSERT_EQUAL_STRING(testString,line); //doesn't work, line is dynamic because the process name is random.
+}
+
+
OpenPOWER on IntegriCloud