xref: /xnu-11215/tests/icmp_basic.c (revision 8d741a5d)
1 /*
2  * Copyright (c) 2024 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 #include <sys/socket.h>
30 #include <sys/un.h>
31 #include <sys/errno.h>
32 
33 #include <unistd.h>
34 
35 #include <darwintest.h>
36 #include <darwintest_utils.h>
37 
38 T_GLOBAL_META(
39 	T_META_NAMESPACE("xnu.net"),
40 	T_META_RADAR_COMPONENT_NAME("xnu"),
41 	T_META_RADAR_COMPONENT_VERSION("networking"));
42 
43 // NOTE: 17.253.144.10 is an anycast address run by AppleCDN.
44 // At the time of this test, this address is apple.com
45 static char *test_ip = "17.253.144.10";
46 
47 static int
run_ping(char * dest,char * payload_size)48 run_ping(char *dest, char *payload_size)
49 {
50 	int ping_ret, pid;
51 	// eg ping -t 3 -c 2 -s 120 17.253.144.10
52 	char *ping_args[]  = {"/sbin/ping", "-t", "3", "-c", "2", "-s", payload_size, dest, NULL};
53 	ping_ret = posix_spawn(&pid, ping_args[0], NULL, NULL, ping_args, NULL);
54 	if (ping_ret < 0) {
55 		return ping_ret;
56 	}
57 	waitpid(pid, &ping_ret, 0);
58 	return ping_ret;
59 }
60 
61 static void
require_internet(void)62 require_internet(void)
63 {
64 	int ret;
65 
66 	ret = run_ping(test_ip, "128");
67 	if (ret == 0 || (WIFEXITED(ret) && !WEXITSTATUS(ret))) {
68 		T_PASS("Initial ping to %s passed, continuing test", test_ip);
69 	} else {
70 		T_SKIP("Initial ping to %s failed, skipping.", test_ip);
71 	}
72 }
73 
74 T_DECL(icmp_internet_root, "test small Internet pings as root",
75     T_META_ASROOT(true),
76     T_META_REQUIRES_NETWORK(true))
77 {
78 	int ret;
79 
80 	require_internet();
81 
82 	ret = run_ping(test_ip, "10");
83 	if (ret == 0 || (WIFEXITED(ret) && !WEXITSTATUS(ret))) {
84 		T_PASS("ping completed");
85 	} else {
86 		T_FAIL("ping %s failed", test_ip);
87 	}
88 }
89 
90 T_DECL(icmp_internet_non_root, "test small Internet pings as non-root",
91     T_META_ASROOT(false),
92     T_META_REQUIRES_NETWORK(true))
93 {
94 	int ret;
95 
96 	require_internet();
97 
98 	ret = run_ping(test_ip, "10");
99 	if (ret == 0 || (WIFEXITED(ret) && !WEXITSTATUS(ret))) {
100 		// And we did not crash
101 		T_PASS("ping completed");
102 	} else {
103 		T_FAIL("ping %s failed", test_ip);
104 	}
105 }
106 
107 T_DECL(icmp_localhost_non_root, "test small localhost pings as non-root",
108     T_META_ASROOT(false))
109 {
110 	int ret;
111 	ret = run_ping("127.0.0.1", "10");
112 	if (ret == 0 || (WIFEXITED(ret) && !WEXITSTATUS(ret))) {
113 		// And we did not crash
114 		T_PASS("ping completed");
115 	} else {
116 		T_FAIL("ping failed");
117 	}
118 }
119 
120 T_DECL(icmp_localhost_root, "test small localhost pings as root",
121     T_META_ASROOT(true))
122 {
123 	int ret;
124 	ret = run_ping("127.0.0.1", "10");
125 	if (ret == 0 || (WIFEXITED(ret) && !WEXITSTATUS(ret))) {
126 		T_PASS("ping completed");
127 	} else {
128 		T_FAIL("ping failed");
129 	}
130 }
131