summaryrefslogtreecommitdiff
path: root/server/tests/simple_unittest.c
blob: 6544d828973add15b6be2a0cca043d07ac2527b6 (plain)
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
116
117
118
119
120
121
122
123
124
125
/*
 * Copyright (C) 2012-2016 Internet Systems Consortium, Inc. ("ISC")
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

#include <config.h>
#include <dhcpd.h>
#include <atf-c.h>

/* That is an example ATF test case, tailored to ISC DHCP sources.
   For detailed description with examples, see man 3 atf-c-api. */

/* this macro defines a name of a test case. Typical test case constists
   of an initial test declaration (ATF_TC()) followed by 3 phases:

   - Initialization: ATF_TC_HEAD()
   - Main body: ATF_TC_BODY()
   - Cleanup: ATF_TC_CLEANUP()

 In many cases initialization or cleanup are not needed. Use
 ATF_TC_WITHOUT_HEAD() or ATF_TC_WITH_CLEANUP() as needed. */
ATF_TC(simple_test_case);


ATF_TC_HEAD(simple_test_case, tc)
{
    atf_tc_set_md_var(tc, "descr", "This test case is a simple DHCP test.");
}
ATF_TC_BODY(simple_test_case, tc)
{
    int condition = 1;
    int this_is_linux = 1;
    /* Failing condition will fail the test, but the code
       itself will continue */
    ATF_CHECK( 2 > 1 );

    /* assert style check. Test will abort if the condition is not met. */
    ATF_REQUIRE( 5 > 4 );

    ATF_CHECK_EQ(4, 2 + 2); /* Non-fatal test. */
    ATF_REQUIRE_EQ(4, 2 + 2); /* Fatal test. */

    /* tests can also explicitly report test result */
    if (!condition) {
        atf_tc_fail("Condition not met!"); /* Explicit failure. */
    }

    if (!this_is_linux) {
        atf_tc_skip("Skipping test. This Linux-only test.");
    }

    if (condition && this_is_linux) {
        /* no extra comments for pass needed. It just passed. */
        atf_tc_pass();
    }

}

#ifdef DHCPv6
ATF_TC(parse_byte_order);

ATF_TC_HEAD(parse_byte_order, tc)
{
    atf_tc_set_md_var(tc, "descr", "Tests byte-order conversion.");
}

ATF_TC_BODY(parse_byte_order, tc)
{
    uint32_t ret_value = 0;
    uint32_t source_value = 0xaabbccdd;

    /* With order set to 0, function should default to no conversion */
    authoring_byte_order = 0;
    ret_value = parse_byte_order_uint32(&source_value);
    if (ret_value != source_value) {
        atf_tc_fail("default/non-conversion failed!");
    }

    /* With matching byte order, function should not do the conversion */
    authoring_byte_order = DHCP_BYTE_ORDER;
    ret_value = parse_byte_order_uint32(&source_value);
    if (ret_value != source_value) {
        atf_tc_fail("matching/non-conversion failed!");
    }

    /* With opposite byte order, function should do the conversion */
    authoring_byte_order = (DHCP_BYTE_ORDER == LITTLE_ENDIAN ?
                            BIG_ENDIAN : LITTLE_ENDIAN);
    ret_value = parse_byte_order_uint32(&source_value);
    if (ret_value != 0xddccbbaa) {
        atf_tc_fail("conversion failed!");
    }

    /* Converting the converted value should give us the original value */
    ret_value = parse_byte_order_uint32(&ret_value);
    if (ret_value != source_value) {
        atf_tc_fail("round trip conversion failed!");
    }

    atf_tc_pass();
}
#endif /*  DHCPv6 */

/* This macro defines main() method that will call specified
   test cases. tp and simple_test_case names can be whatever you want
   as long as it is a valid variable identifier. */
ATF_TP_ADD_TCS(tp)
{
    ATF_TP_ADD_TC(tp, simple_test_case);
#ifdef DHCPv6
    ATF_TP_ADD_TC(tp, parse_byte_order);
#endif
    return (atf_no_error());
}