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