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