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