1 /* 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright 2021 Lutz Donnerhacke 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above 13 * copyright notice, this list of conditions and the following 14 * disclaimer in the documentation and/or other materials provided 15 * with the distribution. 16 * 3. Neither the name of the copyright holder nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 21 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 22 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 27 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 29 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 31 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 #include <atf-c.h> 35 #include <alias.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 39 #include "util.h" 40 41 ATF_TC(2_destroynull); 42 ATF_TC_HEAD(2_destroynull, env) 43 { 44 atf_tc_set_md_var(env, "descr", "Destroy the NULL instance"); 45 } 46 ATF_TC_BODY(2_destroynull, dummy) 47 { 48 atf_tc_expect_death("Code expects valid pointer."); 49 LibAliasUninit(NULL); 50 } 51 52 ATF_TC(1_singleinit); 53 ATF_TC_HEAD(1_singleinit, env) 54 { 55 atf_tc_set_md_var(env, "descr", "Create an instance"); 56 } 57 ATF_TC_BODY(1_singleinit, dummy) 58 { 59 struct libalias *la; 60 61 la = LibAliasInit(NULL); 62 ATF_CHECK_MSG(la != NULL, "Creating an instance failed."); 63 LibAliasUninit(la); 64 } 65 66 ATF_TC(3_multiinit); 67 ATF_TC_HEAD(3_multiinit, env) 68 { 69 atf_tc_set_md_var(env, "descr", "Recreate an instance multiple times"); 70 } 71 ATF_TC_BODY(3_multiinit, dummy) 72 { 73 struct libalias *la; 74 int i; 75 76 la = LibAliasInit(NULL); 77 for(i = 1; i < 30; i++) { 78 struct libalias *lo = la; 79 80 la = LibAliasInit(la); 81 ATF_CHECK_MSG(la == lo, "Recreating moved the instance around: %d", i); 82 } 83 LibAliasUninit(la); 84 } 85 86 ATF_TC(4_multiinstance); 87 ATF_TC_HEAD(4_multiinstance, env) 88 { 89 atf_tc_set_md_var(env, "descr", "Create and destoy multiple instances."); 90 } 91 ATF_TC_BODY(4_multiinstance, dummy) 92 { 93 struct libalias *la[300]; 94 int const num_instances = sizeof(la) / sizeof(*la); 95 int i; 96 97 for (i = 0; i < num_instances; i++) { 98 la[i] = LibAliasInit(NULL); 99 ATF_CHECK_MSG(la[i] != NULL, "Creating instance %d failed.", i); 100 } 101 102 qsort(la, num_instances, sizeof(*la), randcmp); 103 104 for (i = 0; i < num_instances; i++) 105 LibAliasUninit(la[i]); 106 } 107 108 ATF_TP_ADD_TCS(instance) 109 { 110 /* Use "dd if=/dev/random bs=2 count=1 | od -x" to reproduce */ 111 srand(0x5ac4); 112 113 ATF_TP_ADD_TC(instance, 2_destroynull); 114 ATF_TP_ADD_TC(instance, 1_singleinit); 115 ATF_TP_ADD_TC(instance, 3_multiinit); 116 ATF_TP_ADD_TC(instance, 4_multiinstance); 117 118 return atf_no_error(); 119 } 120