File: logging_test.c

package info (click to toggle)
cfengine3 3.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,552 kB
  • sloc: ansic: 163,161; sh: 10,296; python: 2,950; makefile: 1,744; lex: 784; yacc: 633; perl: 211; pascal: 157; xml: 21; sed: 13
file content (115 lines) | stat: -rw-r--r-- 3,656 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <test.h>

#include <cf3.defs.h>
#include <cf3.extern.h>

#include <syslog_client.h>
#include <string_lib.h>

// This test uses syslog_client.c directly, without libpromises,
// this is necessary so we don't get "undefined symbol" errors:
char VFQNAME[CF_MAXVARSIZE];

static struct sockaddr *got_address;

#if SENDTO_RETURNS_SSIZE_T > 0
ssize_t 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)
{
    free(got_address); // RemoteSysLog can call this multiple times
    got_address = xmemdup(dest_addr, sizeof(struct sockaddr_in));
    return len;
}
#else
/*
 * We might be naives by thinking that size_t, socklen_t and such are the same size as int.
 * Given that we are not using them here, we can live with that assumption.
 */
int sendto(ARG_UNUSED int sockfd, ARG_UNUSED const void *buf,
               int len,
               ARG_UNUSED int flags,
               const void *dest_addr,
               ARG_UNUSED int addrlen)
{
    free(got_address); // RemoteSysLog can call this multiple times
    got_address = xmemdup(dest_addr, sizeof(struct sockaddr_in));
    return len;
}
#endif // SENDTO_RETURNS_SSIZE_T > 0

static void test_set_port(void)
{
    SetSyslogPort(5678);
    RemoteSysLog(LOG_EMERG, "Test string");

    if (got_address->sa_family == AF_INET)
    {
        assert_int_equal(ntohs(((struct sockaddr_in *) got_address)->sin_port), 5678);
    }
    else if (got_address->sa_family == AF_INET6)
    {
        assert_int_equal(ntohs(((struct sockaddr_in6 *) got_address)->sin6_port), 5678);
    }

    free(got_address);
    got_address = NULL; // Safe to free(NULL) in another test
}

static void test_set_host(void)
{
    SetSyslogHost("127.0.0.55");
    RemoteSysLog(LOG_EMERG, "Test string");

    assert_int_equal(got_address->sa_family, AF_INET);

    assert_int_equal(ntohl(((struct sockaddr_in *) got_address)->sin_addr.s_addr), 0x7f000037);
    free(got_address);
    got_address = NULL; // Safe to free(NULL) in another test
}

#define check_level(str, lvl) \
{\
    assert_int_equal(LogLevelFromString(str), lvl);\
    assert_true(StringEqual_IgnoreCase(str, LogLevelToString(lvl)));\
}

static void test_log_level(void)
{
    check_level("CRITICAL", LOG_LEVEL_CRIT);
    check_level("Error", LOG_LEVEL_ERR);
    check_level("warning", LOG_LEVEL_WARNING);
    check_level("notice", LOG_LEVEL_NOTICE);
    check_level("info", LOG_LEVEL_INFO);
    check_level("verbose", LOG_LEVEL_VERBOSE);
    check_level("debug", LOG_LEVEL_DEBUG);

    // LogLevelFromString should accept half typed strings:
    assert_int_equal(LogLevelFromString("CRIT"), LOG_LEVEL_CRIT);
    assert_int_equal(LogLevelFromString("ERR"), LOG_LEVEL_ERR);
    assert_int_equal(LogLevelFromString("warn"), LOG_LEVEL_WARNING);
    assert_int_equal(LogLevelFromString("I"), LOG_LEVEL_INFO);
    assert_int_equal(LogLevelFromString("i"), LOG_LEVEL_INFO);
    assert_int_equal(LogLevelFromString("information"), LOG_LEVEL_INFO);
    assert_int_equal(LogLevelFromString("v"), LOG_LEVEL_VERBOSE);

    //LogLevelFromString should return NOTHING in case of error:
    assert_int_equal(LogLevelFromString(""), LOG_LEVEL_NOTHING);
    assert_int_equal(LogLevelFromString("IX"), LOG_LEVEL_NOTHING);
    assert_int_equal(LogLevelFromString("Infotmation"), LOG_LEVEL_NOTHING);
}

int main()
{
    PRINT_TEST_BANNER();
    const UnitTest tests[] =
    {
        unit_test(test_set_port),
        unit_test(test_set_host),
        unit_test(test_log_level),
    };

    return run_tests(tests);
}