1289715Sglebius#include "config.h"
2289715Sglebius#include "unity.h"
3289715Sglebius#include "ntp_types.h"
4289715Sglebius
5289715Sglebius
6289715Sglebius//#include "log.h"
7289715Sglebius#include "log.c"
8289715Sglebius
9293894Sglebiusvoid setUp(void);
10289715Sglebiusvoid testChangePrognameInMysyslog(void);
11289715Sglebiusvoid testOpenLogfileTest(void);
12293894Sglebiusvoid testWriteInCustomLogfile(void);
13289715Sglebius
14289715Sglebius
15293894Sglebiusvoid
16293894SglebiussetUp(void) {
17293894Sglebius	init_lib();
18293894Sglebius}
19293894Sglebius
20293894Sglebius
21289715Sglebius//in var/log/syslog (may differ depending on your OS), logged name of the program will be "TEST_PROGNAME".
22289715Sglebius
23293894Sglebiusvoid
24293894SglebiustestChangePrognameInMysyslog(void)
25293894Sglebius{
26289715Sglebius	sntp_init_logging("TEST_PROGNAME");
27293894Sglebius	msyslog(LOG_ERR, "TESTING sntp_init_logging()");
28293894Sglebius
29293894Sglebius	return;
30289715Sglebius}
31289715Sglebius
32289715Sglebius//writes log files in your own file instead of syslog! (MAY BE USEFUL TO SUPPRESS ERROR MESSAGES!)
33289715Sglebius
34293894Sglebiusvoid
35293894SglebiustestOpenLogfileTest(void)
36293894Sglebius{
37289715Sglebius	sntp_init_logging("TEST_PROGNAME2"); //this name is consistent through the entire program unless changed
38293894Sglebius	open_logfile("testLogfile.log");
39289715Sglebius	//open_logfile("/var/log/syslog"); //this gives me "Permission Denied" when i do %m
40293894Sglebius
41289715Sglebius	msyslog(LOG_ERR, "Cannot open log file %s","abcXX");
42289715Sglebius	//cleanup_log(); //unnecessary  after log.c fix!
43293894Sglebius
44293894Sglebius	return;
45289715Sglebius}
46289715Sglebius
47289715Sglebius
48289715Sglebius//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.
49289715Sglebius
50293894Sglebiusvoid
51293894SglebiustestWriteInCustomLogfile(void)
52293894Sglebius{
53289715Sglebius	char testString[256] = "12345 ABC";
54289715Sglebius	char testName[256] = "TEST_PROGNAME3";
55289715Sglebius
56293894Sglebius	(void)remove("testLogfile2.log");
57289715Sglebius
58289715Sglebius	sntp_init_logging(testName);
59289715Sglebius	open_logfile("testLogfile2.log"); // ./ causing issues
60289715Sglebius	//sntp_init_logging(testName);
61289715Sglebius
62293894Sglebius
63293894Sglebius	msyslog(LOG_ERR, "%s", testString);
64289715Sglebius	FILE * f = fopen("testLogfile2.log","r");
65289715Sglebius	char line[256];
66289715Sglebius
67293894Sglebius	TEST_ASSERT_TRUE( f != NULL);
68293894Sglebius
69289715Sglebius	//should be only 1 line
70293894Sglebius	while (fgets(line, sizeof(line), f)) {
71293894Sglebius		printf("%s", line);
72293894Sglebius	}
73289715Sglebius
74293894Sglebius
75289715Sglebius	char* x = strstr(line,testName);
76293894Sglebius
77289715Sglebius	TEST_ASSERT_TRUE( x != NULL);
78289715Sglebius
79289715Sglebius	x = strstr(line,testString);
80289715Sglebius	TEST_ASSERT_TRUE( x != NULL);
81289715Sglebius	//cleanup_log();
82293894Sglebius	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);
83289715Sglebius	//After the 1st fclose, syslog_file = NULL, and is never reset -> hopefully fixed by editing log.c
84289715Sglebius	//TEST_ASSERT_EQUAL_STRING(testString,line); //doesn't work, line is dynamic because the process name is random.
85293894Sglebius
86293894Sglebius	return;
87289715Sglebius}
88