]>
git.saurik.com Git - apple/xnu.git/blob - tests/xnu_quick_test.c
1 #include <darwintest.h>
2 #include "xnu_quick_test_helpers.h"
9 #include <sys/syscall.h>
10 #include <sys/sysctl.h>
13 T_GLOBAL_META (T_META_NAMESPACE("xnu.quicktest"), T_META_CHECK_LEAKS(false));
14 char g_target_path
[ PATH_MAX
];
16 /* **************************************************************************************************************
17 * Test the syscall system call.
18 * **************************************************************************************************************
21 "xnu_quick_test for syscall", T_META_CHECK_LEAKS(NO
))
29 create_target_directory(TEST_DIRECTORY
);
33 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
,
34 PATH_MAX
, VM_FLAGS_ANYWHERE
);
35 T_ASSERT_MACH_SUCCESS(my_kr
, "Allocating vm to path %s", my_pathp
);
38 strcpy( my_pathp
, &g_target_path
[0] );
39 strcat( my_pathp
, "/" );
41 /* create a test file */
43 T_ASSERT_MACH_SUCCESS( create_random_name( my_pathp
, 1), "Create random test file" );
44 /* use an indirect system call to open our test file.
45 * I picked open since it uses a path pointer which grows to 64 bits in an LP64 environment.
47 T_EXPECT_NE(my_fd
= syscall( SYS_open
, my_pathp
, (O_RDWR
| O_EXCL
), 0 ),
48 -1, "Attempt to open file using indirect syscall %s", my_pathp
);
53 if (my_pathp
!= NULL
) {
55 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
58 T_ATEND(remove_target_directory
);
61 /* **************************************************************************************************************
62 * Test fork wait4, and exit system calls.
63 * **************************************************************************************************************
65 T_DECL(fork_wait4_exit
,
66 "Tests forking off a process and waiting for the child to exit", T_META_CHECK_LEAKS(false))
68 int my_err
, my_status
;
69 pid_t my_pid
, my_wait_pid
;
70 struct rusage my_usage
;
72 strncpy(g_target_path
, "/", 2);
74 /* spin off another process */
75 T_ASSERT_NE(my_pid
= fork(), -1, "Fork off a process");
80 /* child process does very little then exits */
81 my_err
= stat( &g_target_path
[0], &my_sb
);
83 T_ASSERT_TRUE(my_err
== 0, "stat call with path: \"%s\" returned \"%d\"", &g_target_path
[0], errno
);
87 /* parent process waits for child to exit */
88 T_ASSERT_NE(my_wait_pid
= wait4( my_pid
, &my_status
, 0, &my_usage
), -1,
89 "Wait for child to exit\n");
91 /* wait4 should return our child's pid when it exits */
92 T_ASSERT_EQ(my_wait_pid
, my_pid
,
93 "wait4 should return our child's pid when it exits");
95 /* kind of just guessing on these values so if this fails we should take a closer
96 * look at the returned rusage structure.
98 T_ASSERT_FALSE(( my_usage
.ru_utime
.tv_sec
> 1 ||
99 my_usage
.ru_stime
.tv_sec
> 1 || my_usage
.ru_majflt
> 1000 ||
100 my_usage
.ru_msgsnd
> 100 ), "wait4 returned rusage structure");
102 T_ASSERT_TRUE(( WIFEXITED( my_status
) && WEXITSTATUS( my_status
) == 44 ),
103 "check if wait4 returns right exit status");
106 T_DECL (getrusage
, "Sanity check of getrusage")
108 struct rusage my_rusage
;
111 T_ASSERT_EQ(getrusage( RUSAGE_SELF
, &my_rusage
), 0, NULL
);
112 T_LOG("Checking that getrusage returned sane values");
113 T_EXPECT_LT(my_rusage
.ru_msgrcv
, 1000, NULL
);
114 T_EXPECT_GE(my_rusage
.ru_msgrcv
, 0, NULL
);
115 T_EXPECT_LT(my_rusage
.ru_nsignals
, 1000, NULL
);
116 T_EXPECT_GE(my_rusage
.ru_nsignals
, 0, NULL
);