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