xref: /xnu-12377.81.4/tests/net_siocdifaddr.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions /*
2*043036a2SApple OSS Distributions  * Copyright (c) 2025 Apple Inc. All rights reserved.
3*043036a2SApple OSS Distributions  *
4*043036a2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*043036a2SApple OSS Distributions  *
6*043036a2SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*043036a2SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*043036a2SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*043036a2SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*043036a2SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*043036a2SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*043036a2SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*043036a2SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*043036a2SApple OSS Distributions  *
15*043036a2SApple OSS Distributions  * Please obtain a copy of the License at
16*043036a2SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*043036a2SApple OSS Distributions  *
18*043036a2SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*043036a2SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*043036a2SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*043036a2SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*043036a2SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*043036a2SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*043036a2SApple OSS Distributions  * limitations under the License.
25*043036a2SApple OSS Distributions  *
26*043036a2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*043036a2SApple OSS Distributions  */
28*043036a2SApple OSS Distributions 
29*043036a2SApple OSS Distributions /*
30*043036a2SApple OSS Distributions  * net_siocdifaddr.c
31*043036a2SApple OSS Distributions  * - verify that SIOCDIFADDR succeeds
32*043036a2SApple OSS Distributions  */
33*043036a2SApple OSS Distributions 
34*043036a2SApple OSS Distributions #include <stdio.h>
35*043036a2SApple OSS Distributions #include <unistd.h>
36*043036a2SApple OSS Distributions #include <stddef.h>
37*043036a2SApple OSS Distributions #include <stdlib.h>
38*043036a2SApple OSS Distributions #include <string.h>
39*043036a2SApple OSS Distributions #include <TargetConditionals.h>
40*043036a2SApple OSS Distributions #include <darwintest.h>
41*043036a2SApple OSS Distributions #include <darwintest_utils.h>
42*043036a2SApple OSS Distributions 
43*043036a2SApple OSS Distributions #include "net_test_lib.h"
44*043036a2SApple OSS Distributions 
45*043036a2SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.net"),
46*043036a2SApple OSS Distributions     T_META_RADAR_COMPONENT_NAME("xnu"),
47*043036a2SApple OSS Distributions     T_META_RADAR_COMPONENT_VERSION("networking"),
48*043036a2SApple OSS Distributions     T_META_ASROOT(true));
49*043036a2SApple OSS Distributions 
50*043036a2SApple OSS Distributions static char ifname[IF_NAMESIZE];
51*043036a2SApple OSS Distributions 
52*043036a2SApple OSS Distributions static void
fake_set_fail_ioctl(bool fail)53*043036a2SApple OSS Distributions fake_set_fail_ioctl(bool fail)
54*043036a2SApple OSS Distributions {
55*043036a2SApple OSS Distributions 	int     error;
56*043036a2SApple OSS Distributions 	int     val;
57*043036a2SApple OSS Distributions 
58*043036a2SApple OSS Distributions 	val = fail ? 1 : 0;
59*043036a2SApple OSS Distributions #define FAKE_FAIL_IOCTL         "net.link.fake.fail_ioctl"
60*043036a2SApple OSS Distributions 	error = sysctlbyname(FAKE_FAIL_IOCTL, NULL, 0,
61*043036a2SApple OSS Distributions 	    &val, sizeof(val));
62*043036a2SApple OSS Distributions 	T_ASSERT_EQ(error, 0, FAKE_FAIL_IOCTL " %d", val);
63*043036a2SApple OSS Distributions }
64*043036a2SApple OSS Distributions 
65*043036a2SApple OSS Distributions static void
test_cleanup(void)66*043036a2SApple OSS Distributions test_cleanup(void)
67*043036a2SApple OSS Distributions {
68*043036a2SApple OSS Distributions 	if (ifname[0] != '\0') {
69*043036a2SApple OSS Distributions 		(void)ifnet_destroy(ifname, false);
70*043036a2SApple OSS Distributions 		T_LOG("ifnet_destroy %s", ifname);
71*043036a2SApple OSS Distributions 	}
72*043036a2SApple OSS Distributions 	fake_set_fail_ioctl(false);
73*043036a2SApple OSS Distributions }
74*043036a2SApple OSS Distributions 
75*043036a2SApple OSS Distributions static void
sigint_cleanup(__unused int sig)76*043036a2SApple OSS Distributions sigint_cleanup(__unused int sig)
77*043036a2SApple OSS Distributions {
78*043036a2SApple OSS Distributions 	signal(SIGINT, SIG_DFL);
79*043036a2SApple OSS Distributions 	test_cleanup();
80*043036a2SApple OSS Distributions }
81*043036a2SApple OSS Distributions 
82*043036a2SApple OSS Distributions static void
test_siocdifaddr(void)83*043036a2SApple OSS Distributions test_siocdifaddr(void)
84*043036a2SApple OSS Distributions {
85*043036a2SApple OSS Distributions 	struct in_addr  addr;
86*043036a2SApple OSS Distributions 	int             error;
87*043036a2SApple OSS Distributions 	struct in_addr  mask;
88*043036a2SApple OSS Distributions 
89*043036a2SApple OSS Distributions 	addr.s_addr = htonl(IN_LINKLOCALNETNUM + 1);
90*043036a2SApple OSS Distributions 	mask.s_addr = htonl(IN_CLASSB_NET);
91*043036a2SApple OSS Distributions 
92*043036a2SApple OSS Distributions 	signal(SIGINT, sigint_cleanup);
93*043036a2SApple OSS Distributions 	T_ATEND(test_cleanup);
94*043036a2SApple OSS Distributions 
95*043036a2SApple OSS Distributions 	strlcpy(ifname, FETH_NAME, sizeof(ifname));
96*043036a2SApple OSS Distributions 	error = ifnet_create_2(ifname, sizeof(ifname));
97*043036a2SApple OSS Distributions 	if (error != 0) {
98*043036a2SApple OSS Distributions 		ifname[0] = '\0';
99*043036a2SApple OSS Distributions 		T_FAIL("ifnet_create_2 %s", FETH_NAME);
100*043036a2SApple OSS Distributions 	}
101*043036a2SApple OSS Distributions 	fake_set_fail_ioctl(true);
102*043036a2SApple OSS Distributions 	ifnet_add_ip_address(ifname, addr, mask);
103*043036a2SApple OSS Distributions 	ifnet_remove_ip_address(ifname, addr, mask);
104*043036a2SApple OSS Distributions }
105*043036a2SApple OSS Distributions 
106*043036a2SApple OSS Distributions T_DECL(siocdifaddr,
107*043036a2SApple OSS Distributions     "Verify SIOCDIFADDR succeeds when interface returns failure",
108*043036a2SApple OSS Distributions     T_META_ASROOT(true), T_META_TAG_VM_PREFERRED)
109*043036a2SApple OSS Distributions {
110*043036a2SApple OSS Distributions 	test_siocdifaddr();
111*043036a2SApple OSS Distributions }
112