Path: blob/main/tests/sys/netinet/libalias/1_instance.c
39488 views
/*1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright 2021 Lutz Donnerhacke4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above12* copyright notice, this list of conditions and the following13* disclaimer in the documentation and/or other materials provided14* with the distribution.15* 3. Neither the name of the copyright holder nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND20* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,21* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF22* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE23* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS24* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,25* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED26* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON28* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR29* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF30* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/33#include <atf-c.h>34#include <alias.h>35#include <stdio.h>36#include <stdlib.h>3738#include "util.h"3940ATF_TC(2_destroynull);41ATF_TC_HEAD(2_destroynull, env)42{43atf_tc_set_md_var(env, "descr", "Destroy the NULL instance");44}45ATF_TC_BODY(2_destroynull, dummy)46{47atf_tc_expect_death("Code expects valid pointer.");48LibAliasUninit(NULL);49}5051ATF_TC(1_singleinit);52ATF_TC_HEAD(1_singleinit, env)53{54atf_tc_set_md_var(env, "descr", "Create an instance");55}56ATF_TC_BODY(1_singleinit, dummy)57{58struct libalias *la;5960la = LibAliasInit(NULL);61ATF_CHECK_MSG(la != NULL, "Creating an instance failed.");62LibAliasUninit(la);63}6465ATF_TC(3_multiinit);66ATF_TC_HEAD(3_multiinit, env)67{68atf_tc_set_md_var(env, "descr", "Recreate an instance multiple times");69}70ATF_TC_BODY(3_multiinit, dummy)71{72struct libalias *la;73int i;7475la = LibAliasInit(NULL);76for(i = 1; i < 30; i++) {77struct libalias *lo = la;7879la = LibAliasInit(la);80ATF_CHECK_MSG(la == lo, "Recreating moved the instance around: %d", i);81}82LibAliasUninit(la);83}8485ATF_TC(4_multiinstance);86ATF_TC_HEAD(4_multiinstance, env)87{88atf_tc_set_md_var(env, "descr", "Create and destoy multiple instances.");89}90ATF_TC_BODY(4_multiinstance, dummy)91{92struct libalias *la[300];93int const num_instances = sizeof(la) / sizeof(*la);94int i;9596for (i = 0; i < num_instances; i++) {97la[i] = LibAliasInit(NULL);98ATF_CHECK_MSG(la[i] != NULL, "Creating instance %d failed.", i);99}100101qsort(la, num_instances, sizeof(*la), randcmp);102103for (i = 0; i < num_instances; i++)104LibAliasUninit(la[i]);105}106107ATF_TP_ADD_TCS(instance)108{109/* Use "dd if=/dev/random bs=2 count=1 | od -x" to reproduce */110srand(0x5ac4);111112ATF_TP_ADD_TC(instance, 2_destroynull);113ATF_TP_ADD_TC(instance, 1_singleinit);114ATF_TP_ADD_TC(instance, 3_multiinit);115ATF_TP_ADD_TC(instance, 4_multiinstance);116117return atf_no_error();118}119120121