1/*	$NetBSD: simple_unittest.c,v 1.2 2018/04/07 22:37:30 christos Exp $	*/
2
3/*
4 * Copyright (C) 2012-2017 by Internet Systems Consortium, Inc. ("ISC")
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <config.h>
20#include <dhcpd.h>
21#include <atf-c.h>
22
23/* That is an example ATF test case, tailored to ISC DHCP sources.
24   For detailed description with examples, see man 3 atf-c-api. */
25
26/* this macro defines a name of a test case. Typical test case constists
27   of an initial test declaration (ATF_TC()) followed by 3 phases:
28
29   - Initialization: ATF_TC_HEAD()
30   - Main body: ATF_TC_BODY()
31   - Cleanup: ATF_TC_CLEANUP()
32
33 In many cases initialization or cleanup are not needed. Use
34 ATF_TC_WITHOUT_HEAD() or ATF_TC_WITH_CLEANUP() as needed. */
35ATF_TC(simple_test_case);
36
37
38ATF_TC_HEAD(simple_test_case, tc)
39{
40    atf_tc_set_md_var(tc, "descr", "This test case is a simple DHCP test.");
41}
42ATF_TC_BODY(simple_test_case, tc)
43{
44    int condition = 1;
45    int this_is_linux = 1;
46    /* Failing condition will fail the test, but the code
47       itself will continue */
48    ATF_CHECK( 2 > 1 );
49
50    /* assert style check. Test will abort if the condition is not met. */
51    ATF_REQUIRE( 5 > 4 );
52
53    ATF_CHECK_EQ(4, 2 + 2); /* Non-fatal test. */
54    ATF_REQUIRE_EQ(4, 2 + 2); /* Fatal test. */
55
56    /* tests can also explicitly report test result */
57    if (!condition) {
58        atf_tc_fail("Condition not met!"); /* Explicit failure. */
59    }
60
61    if (!this_is_linux) {
62        atf_tc_skip("Skipping test. This Linux-only test.");
63    }
64
65    if (condition && this_is_linux) {
66        /* no extra comments for pass needed. It just passed. */
67        atf_tc_pass();
68    }
69
70}
71
72#ifdef DHCPv6
73ATF_TC(parse_byte_order);
74
75ATF_TC_HEAD(parse_byte_order, tc)
76{
77    atf_tc_set_md_var(tc, "descr", "Tests byte-order conversion.");
78}
79
80ATF_TC_BODY(parse_byte_order, tc)
81{
82    uint32_t ret_value = 0;
83    uint32_t source_value = 0xaabbccdd;
84
85    /* With order set to 0, function should default to no conversion */
86    authoring_byte_order = 0;
87    ret_value = parse_byte_order_uint32(&source_value);
88    if (ret_value != source_value) {
89        atf_tc_fail("default/non-conversion failed!");
90    }
91
92    /* With matching byte order, function should not do the conversion */
93    authoring_byte_order = DHCP_BYTE_ORDER;
94    ret_value = parse_byte_order_uint32(&source_value);
95    if (ret_value != source_value) {
96        atf_tc_fail("matching/non-conversion failed!");
97    }
98
99    /* With opposite byte order, function should do the conversion */
100    authoring_byte_order = (DHCP_BYTE_ORDER == LITTLE_ENDIAN ?
101                            BIG_ENDIAN : LITTLE_ENDIAN);
102    ret_value = parse_byte_order_uint32(&source_value);
103    if (ret_value != 0xddccbbaa) {
104        atf_tc_fail("conversion failed!");
105    }
106
107    /* Converting the converted value should give us the original value */
108    ret_value = parse_byte_order_uint32(&ret_value);
109    if (ret_value != source_value) {
110        atf_tc_fail("round trip conversion failed!");
111    }
112
113    atf_tc_pass();
114}
115#endif /*  DHCPv6 */
116
117/* This macro defines main() method that will call specified
118   test cases. tp and simple_test_case names can be whatever you want
119   as long as it is a valid variable identifier. */
120ATF_TP_ADD_TCS(tp)
121{
122    ATF_TP_ADD_TC(tp, simple_test_case);
123#ifdef DHCPv6
124    ATF_TP_ADD_TC(tp, parse_byte_order);
125#endif
126    return (atf_no_error());
127}
128