xref: /xnu-10002.81.5/tests/xnu_quick_test.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions #include <darwintest.h>
2*5e3eaea3SApple OSS Distributions #include "xnu_quick_test_helpers.h"
3*5e3eaea3SApple OSS Distributions 
4*5e3eaea3SApple OSS Distributions #include <fcntl.h>
5*5e3eaea3SApple OSS Distributions #include <stdlib.h>
6*5e3eaea3SApple OSS Distributions #include <unistd.h>
7*5e3eaea3SApple OSS Distributions #include <mach/mach.h>
8*5e3eaea3SApple OSS Distributions #include <sys/stat.h>
9*5e3eaea3SApple OSS Distributions #include <sys/syscall.h>
10*5e3eaea3SApple OSS Distributions #include <sys/sysctl.h>
11*5e3eaea3SApple OSS Distributions #include <sys/wait.h>
12*5e3eaea3SApple OSS Distributions 
13*5e3eaea3SApple OSS Distributions T_GLOBAL_META(
14*5e3eaea3SApple OSS Distributions 	T_META_NAMESPACE("xnu.quicktest"),
15*5e3eaea3SApple OSS Distributions 	T_META_CHECK_LEAKS(false),
16*5e3eaea3SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true)
17*5e3eaea3SApple OSS Distributions 	);
18*5e3eaea3SApple OSS Distributions 
19*5e3eaea3SApple OSS Distributions char g_target_path[PATH_MAX];
20*5e3eaea3SApple OSS Distributions 
21*5e3eaea3SApple OSS Distributions T_DECL(syscall, "xnu_quick_test for syscall")
22*5e3eaea3SApple OSS Distributions {
23*5e3eaea3SApple OSS Distributions 	int                             my_fd = -1;
24*5e3eaea3SApple OSS Distributions 	char *                  my_pathp;
25*5e3eaea3SApple OSS Distributions 	kern_return_t   my_kr;
26*5e3eaea3SApple OSS Distributions 
27*5e3eaea3SApple OSS Distributions 	T_SETUPBEGIN;
28*5e3eaea3SApple OSS Distributions 
29*5e3eaea3SApple OSS Distributions 	create_target_directory(TEST_DIRECTORY);
30*5e3eaea3SApple OSS Distributions 
31*5e3eaea3SApple OSS Distributions 	T_SETUPEND;
32*5e3eaea3SApple OSS Distributions 
33*5e3eaea3SApple OSS Distributions 	my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp,
34*5e3eaea3SApple OSS Distributions 	    PATH_MAX, VM_FLAGS_ANYWHERE);
35*5e3eaea3SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(my_kr, "Allocating vm to path %s", my_pathp);
36*5e3eaea3SApple OSS Distributions 
37*5e3eaea3SApple OSS Distributions 	*my_pathp = 0x00;
38*5e3eaea3SApple OSS Distributions 	strcpy( my_pathp, &g_target_path[0] );
39*5e3eaea3SApple OSS Distributions 	strcat( my_pathp, "/" );
40*5e3eaea3SApple OSS Distributions 
41*5e3eaea3SApple OSS Distributions 	/* create a test file */
42*5e3eaea3SApple OSS Distributions 
43*5e3eaea3SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS( create_random_name( my_pathp, 1), "Create random test file" );
44*5e3eaea3SApple OSS Distributions 	/* use an indirect system call to open our test file.
45*5e3eaea3SApple OSS Distributions 	 * I picked open since it uses a path pointer which grows to 64 bits in an LP64 environment.
46*5e3eaea3SApple OSS Distributions 	 */
47*5e3eaea3SApple OSS Distributions 	T_EXPECT_NE(my_fd = syscall( SYS_open, my_pathp, (O_RDWR | O_EXCL), 0 ),
48*5e3eaea3SApple OSS Distributions 	    -1, "Attempt to open file using indirect syscall %s", my_pathp);
49*5e3eaea3SApple OSS Distributions 
50*5e3eaea3SApple OSS Distributions 	if (my_fd != -1) {
51*5e3eaea3SApple OSS Distributions 		close(my_fd);
52*5e3eaea3SApple OSS Distributions 	}
53*5e3eaea3SApple OSS Distributions 
54*5e3eaea3SApple OSS Distributions 	if (my_pathp != NULL) {
55*5e3eaea3SApple OSS Distributions 		remove(my_pathp);
56*5e3eaea3SApple OSS Distributions 		vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
57*5e3eaea3SApple OSS Distributions 	}
58*5e3eaea3SApple OSS Distributions 
59*5e3eaea3SApple OSS Distributions 	T_ATEND(remove_target_directory);
60*5e3eaea3SApple OSS Distributions }
61*5e3eaea3SApple OSS Distributions 
62*5e3eaea3SApple OSS Distributions T_DECL(fork_wait4_exit,
63*5e3eaea3SApple OSS Distributions     "Tests forking off a process and waiting for the child to exit")
64*5e3eaea3SApple OSS Distributions {
65*5e3eaea3SApple OSS Distributions 	int                             my_err, my_status;
66*5e3eaea3SApple OSS Distributions 	pid_t                       my_pid, my_wait_pid;
67*5e3eaea3SApple OSS Distributions 	struct rusage   my_usage;
68*5e3eaea3SApple OSS Distributions 
69*5e3eaea3SApple OSS Distributions 	strncpy(g_target_path, "/", 2);
70*5e3eaea3SApple OSS Distributions 
71*5e3eaea3SApple OSS Distributions 	/* spin off another process */
72*5e3eaea3SApple OSS Distributions 	T_ASSERT_NE(my_pid = fork(), -1, "Fork off a process");
73*5e3eaea3SApple OSS Distributions 
74*5e3eaea3SApple OSS Distributions 	if (my_pid == 0) {
75*5e3eaea3SApple OSS Distributions 		struct stat             my_sb;
76*5e3eaea3SApple OSS Distributions 
77*5e3eaea3SApple OSS Distributions 		/* child process does very little then exits */
78*5e3eaea3SApple OSS Distributions 		my_err = stat( &g_target_path[0], &my_sb );
79*5e3eaea3SApple OSS Distributions 		T_WITH_ERRNO;
80*5e3eaea3SApple OSS Distributions 		T_ASSERT_TRUE(my_err == 0, "stat call with path: \"%s\" returned \"%d\"", &g_target_path[0], errno);
81*5e3eaea3SApple OSS Distributions 		exit( 44 );
82*5e3eaea3SApple OSS Distributions 	}
83*5e3eaea3SApple OSS Distributions 
84*5e3eaea3SApple OSS Distributions 	/* parent process waits for child to exit */
85*5e3eaea3SApple OSS Distributions 	T_ASSERT_NE(my_wait_pid = wait4( my_pid, &my_status, 0, &my_usage ), -1,
86*5e3eaea3SApple OSS Distributions 	    "Wait for child to exit\n");
87*5e3eaea3SApple OSS Distributions 
88*5e3eaea3SApple OSS Distributions 	/* wait4 should return our child's pid when it exits */
89*5e3eaea3SApple OSS Distributions 	T_ASSERT_EQ(my_wait_pid, my_pid,
90*5e3eaea3SApple OSS Distributions 	    "wait4 should return our child's pid when it exits");
91*5e3eaea3SApple OSS Distributions 
92*5e3eaea3SApple OSS Distributions 	/* kind of just guessing on these values so if this fails we should take a closer
93*5e3eaea3SApple OSS Distributions 	 * look at the returned rusage structure.
94*5e3eaea3SApple OSS Distributions 	 */
95*5e3eaea3SApple OSS Distributions 	T_ASSERT_FALSE((my_usage.ru_utime.tv_sec > 1 ||
96*5e3eaea3SApple OSS Distributions 	    my_usage.ru_stime.tv_sec > 1 || my_usage.ru_majflt > 1000 ||
97*5e3eaea3SApple OSS Distributions 	    my_usage.ru_msgsnd > 100), "wait4 returned rusage structure");
98*5e3eaea3SApple OSS Distributions 
99*5e3eaea3SApple OSS Distributions 	T_ASSERT_TRUE((WIFEXITED( my_status ) && WEXITSTATUS( my_status ) == 44),
100*5e3eaea3SApple OSS Distributions 	    "check if wait4 returns right exit status");
101*5e3eaea3SApple OSS Distributions }
102*5e3eaea3SApple OSS Distributions 
103*5e3eaea3SApple OSS Distributions T_DECL(getrusage, "check getrusage works")
104*5e3eaea3SApple OSS Distributions {
105*5e3eaea3SApple OSS Distributions 	struct rusage rubuf;
106*5e3eaea3SApple OSS Distributions 
107*5e3eaea3SApple OSS Distributions 	int ret = getrusage(RUSAGE_SELF, &rubuf);
108*5e3eaea3SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "getrusage for self");
109*5e3eaea3SApple OSS Distributions 
110*5e3eaea3SApple OSS Distributions 	T_EXPECT_LT(rubuf.ru_msgrcv, 1000, "upper bound on messages received");
111*5e3eaea3SApple OSS Distributions 	T_EXPECT_GE(rubuf.ru_msgrcv, 0, "lower bound on messages reseived");
112*5e3eaea3SApple OSS Distributions 	T_EXPECT_LT(rubuf.ru_nsignals, 1000, "upper bound on signals");
113*5e3eaea3SApple OSS Distributions 	T_EXPECT_GE(rubuf.ru_nsignals, 0, "lower bound on signals");
114*5e3eaea3SApple OSS Distributions }
115