xref: /freebsd/tests/sys/netinet/libalias/1_instance.c (revision 7b8696bf128754712a24ba98ce2d88eed2ee68dc)
1*7b8696bfSLutz Donnerhacke /*
2*7b8696bfSLutz Donnerhacke  * SPDX-License-Identifier: BSD-3-Clause
3*7b8696bfSLutz Donnerhacke  *
4*7b8696bfSLutz Donnerhacke  * Copyright 2021 Lutz Donnerhacke
5*7b8696bfSLutz Donnerhacke  *
6*7b8696bfSLutz Donnerhacke  * Redistribution and use in source and binary forms, with or without
7*7b8696bfSLutz Donnerhacke  * modification, are permitted provided that the following conditions
8*7b8696bfSLutz Donnerhacke  * are met:
9*7b8696bfSLutz Donnerhacke  *
10*7b8696bfSLutz Donnerhacke  * 1. Redistributions of source code must retain the above copyright
11*7b8696bfSLutz Donnerhacke  *    notice, this list of conditions and the following disclaimer.
12*7b8696bfSLutz Donnerhacke  * 2. Redistributions in binary form must reproduce the above
13*7b8696bfSLutz Donnerhacke  *    copyright notice, this list of conditions and the following
14*7b8696bfSLutz Donnerhacke  *    disclaimer in the documentation and/or other materials provided
15*7b8696bfSLutz Donnerhacke  *    with the distribution.
16*7b8696bfSLutz Donnerhacke  * 3. Neither the name of the copyright holder nor the names of its
17*7b8696bfSLutz Donnerhacke  *    contributors may be used to endorse or promote products derived
18*7b8696bfSLutz Donnerhacke  *    from this software without specific prior written permission.
19*7b8696bfSLutz Donnerhacke  *
20*7b8696bfSLutz Donnerhacke  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
21*7b8696bfSLutz Donnerhacke  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
22*7b8696bfSLutz Donnerhacke  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23*7b8696bfSLutz Donnerhacke  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24*7b8696bfSLutz Donnerhacke  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25*7b8696bfSLutz Donnerhacke  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26*7b8696bfSLutz Donnerhacke  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27*7b8696bfSLutz Donnerhacke  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28*7b8696bfSLutz Donnerhacke  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29*7b8696bfSLutz Donnerhacke  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*7b8696bfSLutz Donnerhacke  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
31*7b8696bfSLutz Donnerhacke  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*7b8696bfSLutz Donnerhacke  * SUCH DAMAGE.
33*7b8696bfSLutz Donnerhacke  */
347fd8baeeSLutz Donnerhacke #include <atf-c.h>
357fd8baeeSLutz Donnerhacke #include <alias.h>
367fd8baeeSLutz Donnerhacke #include <stdio.h>
377fd8baeeSLutz Donnerhacke #include <stdlib.h>
387fd8baeeSLutz Donnerhacke 
39c1fbb54fSLutz Donnerhacke #include "util.h"
407fd8baeeSLutz Donnerhacke 
417fd8baeeSLutz Donnerhacke ATF_TC(2_destroynull);
427fd8baeeSLutz Donnerhacke ATF_TC_HEAD(2_destroynull, env)
437fd8baeeSLutz Donnerhacke {
447fd8baeeSLutz Donnerhacke 	atf_tc_set_md_var(env, "descr", "Destroy the NULL instance");
457fd8baeeSLutz Donnerhacke }
467fd8baeeSLutz Donnerhacke ATF_TC_BODY(2_destroynull, dummy)
477fd8baeeSLutz Donnerhacke {
487fd8baeeSLutz Donnerhacke 	atf_tc_expect_death("Code expects valid pointer.");
497fd8baeeSLutz Donnerhacke 	LibAliasUninit(NULL);
507fd8baeeSLutz Donnerhacke }
517fd8baeeSLutz Donnerhacke 
527fd8baeeSLutz Donnerhacke ATF_TC(1_singleinit);
537fd8baeeSLutz Donnerhacke ATF_TC_HEAD(1_singleinit, env)
547fd8baeeSLutz Donnerhacke {
557fd8baeeSLutz Donnerhacke 	atf_tc_set_md_var(env, "descr", "Create an instance");
567fd8baeeSLutz Donnerhacke }
577fd8baeeSLutz Donnerhacke ATF_TC_BODY(1_singleinit, dummy)
587fd8baeeSLutz Donnerhacke {
597fd8baeeSLutz Donnerhacke 	struct libalias *la;
607fd8baeeSLutz Donnerhacke 
617fd8baeeSLutz Donnerhacke 	la = LibAliasInit(NULL);
627fd8baeeSLutz Donnerhacke 	ATF_CHECK_MSG(la != NULL, "Creating an instance failed.");
637fd8baeeSLutz Donnerhacke 	LibAliasUninit(la);
647fd8baeeSLutz Donnerhacke }
657fd8baeeSLutz Donnerhacke 
667fd8baeeSLutz Donnerhacke ATF_TC(3_multiinit);
677fd8baeeSLutz Donnerhacke ATF_TC_HEAD(3_multiinit, env)
687fd8baeeSLutz Donnerhacke {
697fd8baeeSLutz Donnerhacke 	atf_tc_set_md_var(env, "descr", "Recreate an instance multiple times");
707fd8baeeSLutz Donnerhacke }
717fd8baeeSLutz Donnerhacke ATF_TC_BODY(3_multiinit, dummy)
727fd8baeeSLutz Donnerhacke {
737fd8baeeSLutz Donnerhacke 	struct libalias *la;
747fd8baeeSLutz Donnerhacke 	int i;
757fd8baeeSLutz Donnerhacke 
767fd8baeeSLutz Donnerhacke 	la = LibAliasInit(NULL);
777fd8baeeSLutz Donnerhacke 	for(i = 1; i < 30; i++) {
787fd8baeeSLutz Donnerhacke 		struct libalias *lo = la;
797fd8baeeSLutz Donnerhacke 
807fd8baeeSLutz Donnerhacke 		la = LibAliasInit(la);
817fd8baeeSLutz Donnerhacke 		ATF_CHECK_MSG(la == lo, "Recreating moved the instance around: %d", i);
827fd8baeeSLutz Donnerhacke 	}
837fd8baeeSLutz Donnerhacke 	LibAliasUninit(la);
847fd8baeeSLutz Donnerhacke }
857fd8baeeSLutz Donnerhacke 
867fd8baeeSLutz Donnerhacke ATF_TC(4_multiinstance);
877fd8baeeSLutz Donnerhacke ATF_TC_HEAD(4_multiinstance, env)
887fd8baeeSLutz Donnerhacke {
897fd8baeeSLutz Donnerhacke 	atf_tc_set_md_var(env, "descr", "Create and destoy multiple instances.");
907fd8baeeSLutz Donnerhacke }
917fd8baeeSLutz Donnerhacke ATF_TC_BODY(4_multiinstance, dummy)
927fd8baeeSLutz Donnerhacke {
937fd8baeeSLutz Donnerhacke 	struct libalias *la[300];
947fd8baeeSLutz Donnerhacke 	int const num_instances = sizeof(la) / sizeof(*la);
957fd8baeeSLutz Donnerhacke 	int i;
967fd8baeeSLutz Donnerhacke 
977fd8baeeSLutz Donnerhacke 	for (i = 0; i < num_instances; i++) {
987fd8baeeSLutz Donnerhacke 		la[i] = LibAliasInit(NULL);
997fd8baeeSLutz Donnerhacke 		ATF_CHECK_MSG(la[i] != NULL, "Creating instance %d failed.", i);
1007fd8baeeSLutz Donnerhacke 	}
1017fd8baeeSLutz Donnerhacke 
1027fd8baeeSLutz Donnerhacke 	qsort(la, num_instances, sizeof(*la), randcmp);
1037fd8baeeSLutz Donnerhacke 
1047fd8baeeSLutz Donnerhacke 	for (i = 0; i < num_instances; i++)
1057fd8baeeSLutz Donnerhacke 		LibAliasUninit(la[i]);
1067fd8baeeSLutz Donnerhacke }
1077fd8baeeSLutz Donnerhacke 
ATF_TP_ADD_TCS(instance)1087fd8baeeSLutz Donnerhacke ATF_TP_ADD_TCS(instance)
1097fd8baeeSLutz Donnerhacke {
1107fd8baeeSLutz Donnerhacke 	/* Use "dd if=/dev/random bs=2 count=1 | od -x" to reproduce */
1117fd8baeeSLutz Donnerhacke 	srand(0x5ac4);
1127fd8baeeSLutz Donnerhacke 
1137fd8baeeSLutz Donnerhacke 	ATF_TP_ADD_TC(instance, 2_destroynull);
1147fd8baeeSLutz Donnerhacke 	ATF_TP_ADD_TC(instance, 1_singleinit);
1157fd8baeeSLutz Donnerhacke 	ATF_TP_ADD_TC(instance, 3_multiinit);
1167fd8baeeSLutz Donnerhacke 	ATF_TP_ADD_TC(instance, 4_multiinstance);
1177fd8baeeSLutz Donnerhacke 
1187fd8baeeSLutz Donnerhacke 	return atf_no_error();
1197fd8baeeSLutz Donnerhacke }
120