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