1 #include <test.h>
2 
3 #include <cf3.defs.h>
4 #include <cf3.extern.h>
5 
6 #include <syslog_client.h>
7 #include <string_lib.h>
8 
9 // This test uses syslog_client.c directly, without libpromises,
10 // this is necessary so we don't get "undefined symbol" errors:
11 char VFQNAME[CF_MAXVARSIZE];
12 
13 static struct sockaddr *got_address;
14 
15 #if SENDTO_RETURNS_SSIZE_T > 0
sendto(ARG_UNUSED int sockfd,ARG_UNUSED const void * buf,size_t len,ARG_UNUSED int flags,const struct sockaddr * dest_addr,ARG_UNUSED socklen_t addrlen)16 ssize_t sendto(ARG_UNUSED int sockfd, ARG_UNUSED const void *buf,
17                size_t len,
18                ARG_UNUSED int flags,
19                const struct sockaddr *dest_addr,
20                ARG_UNUSED socklen_t addrlen)
21 {
22     free(got_address); // RemoteSysLog can call this multiple times
23     got_address = xmemdup(dest_addr, sizeof(struct sockaddr_in));
24     return len;
25 }
26 #else
27 /*
28  * We might be naives by thinking that size_t, socklen_t and such are the same size as int.
29  * Given that we are not using them here, we can live with that assumption.
30  */
sendto(ARG_UNUSED int sockfd,ARG_UNUSED const void * buf,int len,ARG_UNUSED int flags,const void * dest_addr,ARG_UNUSED int addrlen)31 int sendto(ARG_UNUSED int sockfd, ARG_UNUSED const void *buf,
32                int len,
33                ARG_UNUSED int flags,
34                const void *dest_addr,
35                ARG_UNUSED int addrlen)
36 {
37     free(got_address); // RemoteSysLog can call this multiple times
38     got_address = xmemdup(dest_addr, sizeof(struct sockaddr_in));
39     return len;
40 }
41 #endif // SENDTO_RETURNS_SSIZE_T > 0
42 
test_set_port(void)43 static void test_set_port(void)
44 {
45     SetSyslogPort(5678);
46     RemoteSysLog(LOG_EMERG, "Test string");
47 
48     if (got_address->sa_family == AF_INET)
49     {
50         assert_int_equal(ntohs(((struct sockaddr_in *) got_address)->sin_port), 5678);
51     }
52     else if (got_address->sa_family == AF_INET6)
53     {
54         assert_int_equal(ntohs(((struct sockaddr_in6 *) got_address)->sin6_port), 5678);
55     }
56 
57     free(got_address);
58     got_address = NULL; // Safe to free(NULL) in another test
59 }
60 
test_set_host(void)61 static void test_set_host(void)
62 {
63     SetSyslogHost("127.0.0.55");
64     RemoteSysLog(LOG_EMERG, "Test string");
65 
66     assert_int_equal(got_address->sa_family, AF_INET);
67 
68     assert_int_equal(ntohl(((struct sockaddr_in *) got_address)->sin_addr.s_addr), 0x7f000037);
69     free(got_address);
70     got_address = NULL; // Safe to free(NULL) in another test
71 }
72 
73 #define check_level(str, lvl) \
74 {\
75     assert_int_equal(LogLevelFromString(str), lvl);\
76     assert_true(StringEqual_IgnoreCase(str, LogLevelToString(lvl)));\
77 }
78 
test_log_level(void)79 static void test_log_level(void)
80 {
81     check_level("CRITICAL", LOG_LEVEL_CRIT);
82     check_level("Error", LOG_LEVEL_ERR);
83     check_level("warning", LOG_LEVEL_WARNING);
84     check_level("notice", LOG_LEVEL_NOTICE);
85     check_level("info", LOG_LEVEL_INFO);
86     check_level("verbose", LOG_LEVEL_VERBOSE);
87     check_level("debug", LOG_LEVEL_DEBUG);
88 
89     // LogLevelFromString should accept half typed strings:
90     assert_int_equal(LogLevelFromString("CRIT"), LOG_LEVEL_CRIT);
91     assert_int_equal(LogLevelFromString("ERR"), LOG_LEVEL_ERR);
92     assert_int_equal(LogLevelFromString("warn"), LOG_LEVEL_WARNING);
93     assert_int_equal(LogLevelFromString("I"), LOG_LEVEL_INFO);
94     assert_int_equal(LogLevelFromString("i"), LOG_LEVEL_INFO);
95     assert_int_equal(LogLevelFromString("information"), LOG_LEVEL_INFO);
96     assert_int_equal(LogLevelFromString("v"), LOG_LEVEL_VERBOSE);
97 
98     //LogLevelFromString should return NOTHING in case of error:
99     assert_int_equal(LogLevelFromString(""), LOG_LEVEL_NOTHING);
100     assert_int_equal(LogLevelFromString("IX"), LOG_LEVEL_NOTHING);
101     assert_int_equal(LogLevelFromString("Infotmation"), LOG_LEVEL_NOTHING);
102 }
103 
main()104 int main()
105 {
106     PRINT_TEST_BANNER();
107     const UnitTest tests[] =
108     {
109         unit_test(test_set_port),
110         unit_test(test_set_host),
111         unit_test(test_log_level),
112     };
113 
114     return run_tests(tests);
115 }
116