1289999Sglebius#include "config.h"
2289999Sglebius#include "unity.h"
3289999Sglebius#include "ntp_types.h"
4289999Sglebius
5289999Sglebius
6289999Sglebius//#include "log.h"
7289999Sglebius#include "log.c"
8289999Sglebius
9293893Sglebiusvoid setUp(void);
10289999Sglebiusvoid testChangePrognameInMysyslog(void);
11289999Sglebiusvoid testOpenLogfileTest(void);
12293893Sglebiusvoid testWriteInCustomLogfile(void);
13289999Sglebius
14289999Sglebius
15293893Sglebiusvoid
16293893SglebiussetUp(void) {
17293893Sglebius	init_lib();
18293893Sglebius}
19293893Sglebius
20293893Sglebius
21289999Sglebius//in var/log/syslog (may differ depending on your OS), logged name of the program will be "TEST_PROGNAME".
22289999Sglebius
23293893Sglebiusvoid
24293893SglebiustestChangePrognameInMysyslog(void)
25293893Sglebius{
26289999Sglebius	sntp_init_logging("TEST_PROGNAME");
27293893Sglebius	msyslog(LOG_ERR, "TESTING sntp_init_logging()");
28293893Sglebius
29293893Sglebius	return;
30289999Sglebius}
31289999Sglebius
32289999Sglebius//writes log files in your own file instead of syslog! (MAY BE USEFUL TO SUPPRESS ERROR MESSAGES!)
33289999Sglebius
34293893Sglebiusvoid
35293893SglebiustestOpenLogfileTest(void)
36293893Sglebius{
37289999Sglebius	sntp_init_logging("TEST_PROGNAME2"); //this name is consistent through the entire program unless changed
38293893Sglebius	open_logfile("testLogfile.log");
39289999Sglebius	//open_logfile("/var/log/syslog"); //this gives me "Permission Denied" when i do %m
40293893Sglebius
41289999Sglebius	msyslog(LOG_ERR, "Cannot open log file %s","abcXX");
42289999Sglebius	//cleanup_log(); //unnecessary  after log.c fix!
43293893Sglebius
44293893Sglebius	return;
45289999Sglebius}
46289999Sglebius
47289999Sglebius
48289999Sglebius//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.
49289999Sglebius
50293893Sglebiusvoid
51293893SglebiustestWriteInCustomLogfile(void)
52293893Sglebius{
53289999Sglebius	char testString[256] = "12345 ABC";
54289999Sglebius	char testName[256] = "TEST_PROGNAME3";
55289999Sglebius
56293893Sglebius	(void)remove("testLogfile2.log");
57289999Sglebius
58289999Sglebius	sntp_init_logging(testName);
59289999Sglebius	open_logfile("testLogfile2.log"); // ./ causing issues
60289999Sglebius	//sntp_init_logging(testName);
61289999Sglebius
62293893Sglebius
63293893Sglebius	msyslog(LOG_ERR, "%s", testString);
64289999Sglebius	FILE * f = fopen("testLogfile2.log","r");
65289999Sglebius	char line[256];
66289999Sglebius
67293893Sglebius	TEST_ASSERT_TRUE( f != NULL);
68293893Sglebius
69289999Sglebius	//should be only 1 line
70293893Sglebius	while (fgets(line, sizeof(line), f)) {
71293893Sglebius		printf("%s", line);
72293893Sglebius	}
73289999Sglebius
74293893Sglebius
75289999Sglebius	char* x = strstr(line,testName);
76293893Sglebius
77289999Sglebius	TEST_ASSERT_TRUE( x != NULL);
78289999Sglebius
79289999Sglebius	x = strstr(line,testString);
80289999Sglebius	TEST_ASSERT_TRUE( x != NULL);
81289999Sglebius	//cleanup_log();
82293893Sglebius	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);
83289999Sglebius	//After the 1st fclose, syslog_file = NULL, and is never reset -> hopefully fixed by editing log.c
84289999Sglebius	//TEST_ASSERT_EQUAL_STRING(testString,line); //doesn't work, line is dynamic because the process name is random.
85293893Sglebius
86293893Sglebius	return;
87289999Sglebius}
88