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