xref: /freebsd/contrib/ntp/sntp/tests/t-log.c (revision 9034852c84a13f0e3b5527e1c886ca94b2863b2b)
1*9034852cSGleb Smirnoff #include "config.h"
2*9034852cSGleb Smirnoff #include "unity.h"
3*9034852cSGleb Smirnoff #include "ntp_types.h"
4*9034852cSGleb Smirnoff 
5*9034852cSGleb Smirnoff 
6*9034852cSGleb Smirnoff //#include "log.h"
7*9034852cSGleb Smirnoff #include "log.c"
8*9034852cSGleb Smirnoff 
9*9034852cSGleb Smirnoff void testChangePrognameInMysyslog(void);
10*9034852cSGleb Smirnoff void testOpenLogfileTest(void);
11*9034852cSGleb Smirnoff 
12*9034852cSGleb Smirnoff 
13*9034852cSGleb Smirnoff //in var/log/syslog (may differ depending on your OS), logged name of the program will be "TEST_PROGNAME".
14*9034852cSGleb Smirnoff 
15*9034852cSGleb Smirnoff void testChangePrognameInMysyslog(void){
16*9034852cSGleb Smirnoff 	sntp_init_logging("TEST_PROGNAME");
17*9034852cSGleb Smirnoff 	msyslog(LOG_ERR, "TESTING sntp_init_logging()"); //%m will print the last errno?
18*9034852cSGleb Smirnoff }
19*9034852cSGleb Smirnoff 
20*9034852cSGleb Smirnoff //writes log files in your own file instead of syslog! (MAY BE USEFUL TO SUPPRESS ERROR MESSAGES!)
21*9034852cSGleb Smirnoff 
22*9034852cSGleb Smirnoff void testOpenLogfileTest(void){
23*9034852cSGleb Smirnoff 	sntp_init_logging("TEST_PROGNAME2"); //this name is consistent through the entire program unless changed
24*9034852cSGleb Smirnoff 	open_logfile("testLogfile.log");
25*9034852cSGleb Smirnoff 	//open_logfile("/var/log/syslog"); //this gives me "Permission Denied" when i do %m
26*9034852cSGleb Smirnoff 
27*9034852cSGleb Smirnoff 	msyslog(LOG_ERR, "Cannot open log file %s","abcXX");
28*9034852cSGleb Smirnoff 	//cleanup_log(); //unnecessary  after log.c fix!
29*9034852cSGleb Smirnoff 
30*9034852cSGleb Smirnoff }
31*9034852cSGleb Smirnoff 
32*9034852cSGleb Smirnoff 
33*9034852cSGleb 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.
34*9034852cSGleb Smirnoff 
35*9034852cSGleb Smirnoff void testWriteInCustomLogfile(void){
36*9034852cSGleb Smirnoff 	char testString[256] = "12345 ABC";
37*9034852cSGleb Smirnoff 	char testName[256] = "TEST_PROGNAME3";
38*9034852cSGleb Smirnoff 
39*9034852cSGleb Smirnoff 	remove("testLogfile2.log");
40*9034852cSGleb Smirnoff 
41*9034852cSGleb Smirnoff 	sntp_init_logging(testName);
42*9034852cSGleb Smirnoff 	open_logfile("testLogfile2.log"); // ./ causing issues
43*9034852cSGleb Smirnoff 	//sntp_init_logging(testName);
44*9034852cSGleb Smirnoff 
45*9034852cSGleb Smirnoff 
46*9034852cSGleb Smirnoff 	msyslog(LOG_ERR, testString);
47*9034852cSGleb Smirnoff 	FILE * f = fopen("testLogfile2.log","r");
48*9034852cSGleb Smirnoff 	char line[256];
49*9034852cSGleb Smirnoff 
50*9034852cSGleb Smirnoff 	//should be only 1 line
51*9034852cSGleb Smirnoff  	while (fgets(line, sizeof(line), f)) {
52*9034852cSGleb Smirnoff         	printf("%s", line);
53*9034852cSGleb Smirnoff     	}
54*9034852cSGleb Smirnoff 
55*9034852cSGleb Smirnoff 
56*9034852cSGleb Smirnoff 	char* x = strstr(line,testName);
57*9034852cSGleb Smirnoff 
58*9034852cSGleb Smirnoff 	TEST_ASSERT_TRUE( x != NULL);
59*9034852cSGleb Smirnoff 
60*9034852cSGleb Smirnoff 	x = strstr(line,testString);
61*9034852cSGleb Smirnoff 	TEST_ASSERT_TRUE( x != NULL);
62*9034852cSGleb Smirnoff 	//cleanup_log();
63*9034852cSGleb 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);
64*9034852cSGleb Smirnoff 	//After the 1st fclose, syslog_file = NULL, and is never reset -> hopefully fixed by editing log.c
65*9034852cSGleb Smirnoff 	//TEST_ASSERT_EQUAL_STRING(testString,line); //doesn't work, line is dynamic because the process name is random.
66*9034852cSGleb Smirnoff }
67*9034852cSGleb Smirnoff 
68*9034852cSGleb Smirnoff 
69