1289715Sglebius#include "config.h"
2289715Sglebius#include "unity.h"
3289715Sglebius#include "ntp_types.h"
4289715Sglebius
5289715Sglebius
6289715Sglebius//#include "log.h"
7289715Sglebius#include "log.c"
8289715Sglebius
9293650Sglebiusvoid setUp(void);
10289715Sglebiusvoid testChangePrognameInMysyslog(void);
11289715Sglebiusvoid testOpenLogfileTest(void);
12293650Sglebiusvoid testWriteInCustomLogfile(void);
13289715Sglebius
14289715Sglebius
15293650Sglebiusvoid
16293650SglebiussetUp(void) {
17293650Sglebius	init_lib();
18293650Sglebius}
19293650Sglebius
20293650Sglebius
21289715Sglebius//in var/log/syslog (may differ depending on your OS), logged name of the program will be "TEST_PROGNAME".
22289715Sglebius
23293650Sglebiusvoid
24293650SglebiustestChangePrognameInMysyslog(void)
25293650Sglebius{
26289715Sglebius	sntp_init_logging("TEST_PROGNAME");
27293650Sglebius	msyslog(LOG_ERR, "TESTING sntp_init_logging()");
28293650Sglebius
29293650Sglebius	return;
30289715Sglebius}
31289715Sglebius
32289715Sglebius//writes log files in your own file instead of syslog! (MAY BE USEFUL TO SUPPRESS ERROR MESSAGES!)
33289715Sglebius
34293650Sglebiusvoid
35293650SglebiustestOpenLogfileTest(void)
36293650Sglebius{
37289715Sglebius	sntp_init_logging("TEST_PROGNAME2"); //this name is consistent through the entire program unless changed
38293650Sglebius	open_logfile("testLogfile.log");
39289715Sglebius	//open_logfile("/var/log/syslog"); //this gives me "Permission Denied" when i do %m
40293650Sglebius
41289715Sglebius	msyslog(LOG_ERR, "Cannot open log file %s","abcXX");
42289715Sglebius	//cleanup_log(); //unnecessary  after log.c fix!
43293650Sglebius
44293650Sglebius	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
50293650Sglebiusvoid
51293650SglebiustestWriteInCustomLogfile(void)
52293650Sglebius{
53289715Sglebius	char testString[256] = "12345 ABC";
54289715Sglebius	char testName[256] = "TEST_PROGNAME3";
55289715Sglebius
56293650Sglebius	(void)remove("testLogfile2.log");
57289715Sglebius
58289715Sglebius	sntp_init_logging(testName);
59289715Sglebius	open_logfile("testLogfile2.log"); // ./ causing issues
60289715Sglebius	//sntp_init_logging(testName);
61289715Sglebius
62293650Sglebius
63293650Sglebius	msyslog(LOG_ERR, "%s", testString);
64289715Sglebius	FILE * f = fopen("testLogfile2.log","r");
65289715Sglebius	char line[256];
66289715Sglebius
67293650Sglebius	TEST_ASSERT_TRUE( f != NULL);
68293650Sglebius
69289715Sglebius	//should be only 1 line
70293650Sglebius	while (fgets(line, sizeof(line), f)) {
71293650Sglebius		printf("%s", line);
72293650Sglebius	}
73289715Sglebius
74293650Sglebius
75289715Sglebius	char* x = strstr(line,testName);
76293650Sglebius
77289715Sglebius	TEST_ASSERT_TRUE( x != NULL);
78289715Sglebius
79289715Sglebius	x = strstr(line,testString);
80289715Sglebius	TEST_ASSERT_TRUE( x != NULL);
81289715Sglebius	//cleanup_log();
82293650Sglebius	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.
85293650Sglebius
86293650Sglebius	return;
87289715Sglebius}
88