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