]> git.saurik.com Git - apple/xnu.git/blame - tests/xnu_quick_test_entitled.c
xnu-4903.241.1.tar.gz
[apple/xnu.git] / tests / xnu_quick_test_entitled.c
CommitLineData
d9a64523
A
1#include <darwintest.h>
2
3#include <fcntl.h>
4#include <stdlib.h>
5#include <unistd.h>
6#include <sys/types.h>
7#include <sys/sysctl.h>
8#include <sys/disk.h>
9#include <sys/ioctl.h>
10#include <sys/mount.h>
11
12#if !TARGET_OS_EMBEDDED
13#include <sys/csr.h>
14#endif
15
16T_GLOBAL_META (T_META_NAMESPACE("xnu.quicktest"), T_META_CHECK_LEAKS(false));
17
18
19/* **************************************************************************************************************
20 * Test ioctl system calls.
21 * **************************************************************************************************************
22 */
23T_DECL(ioctl, "Sanity check of ioctl by exercising DKIOCGETBLOCKCOUNT and DKIOCGETBLOCKSIZE",
24 T_META_ASROOT(true))
25{
26 int my_err;
27 int my_fd = -1;
28 struct statfs * my_infop;
29 char * my_ptr;
30 int my_blksize;
31 long long my_block_count;
32 char my_name[ MAXPATHLEN ];
33
34#if !TARGET_OS_EMBEDDED
35 /*
36 * this test won't be able to open the root disk device unless CSR is
37 * disabled or in AppleInternal mode
38 */
39 if (csr_check( CSR_ALLOW_UNRESTRICTED_FS ) &&
40 csr_check( CSR_ALLOW_APPLE_INTERNAL ) ) {
41 T_SKIP("System Integrity Protection is enabled");
42 }
43#endif
44
45 T_SETUPBEGIN;
46
47 T_WITH_ERRNO;
48 T_ASSERT_GT(getmntinfo( &my_infop, MNT_NOWAIT ), 0, "getmntinfo");
49
50 /* make this a raw device */
51 strlcpy( &my_name[0], &my_infop->f_mntfromname[0], sizeof(my_name) );
52 if ( (my_ptr = strrchr( &my_name[0], '/' )) != 0 ) {
53 if ( my_ptr[1] != 'r' ) {
54 my_ptr[ strlen( my_ptr ) ] = 0x00;
55 memmove( &my_ptr[2], &my_ptr[1], (strlen( &my_ptr[1] ) + 1) );
56 my_ptr[1] = 'r';
57 }
58 }
59
60 T_ASSERT_POSIX_SUCCESS(my_fd = open( &my_name[0], O_RDONLY ), "open");
61
62 T_SETUPEND;
63
64 /* obtain the size of the media (in blocks) */
65 T_EXPECT_POSIX_SUCCESS(my_err = ioctl( my_fd, DKIOCGETBLOCKCOUNT, &my_block_count ),
66 "ioctl DKIOCGETBLOCKCOUNT");
67
68 /* obtain the block size of the media */
69 T_EXPECT_POSIX_SUCCESS(my_err = ioctl( my_fd, DKIOCGETBLOCKSIZE, &my_blksize ),
70 "ioctl DKIOCGETBLOCKSIZE");
71
72 T_LOG( "my_block_count %qd my_blksize %d \n", my_block_count, my_blksize );
73
74 if (my_err != -1) {
75 /* make sure the returned data looks somewhat valid */
76 T_EXPECT_GE(my_blksize, 0, NULL);
77 T_EXPECT_LE(my_blksize, 1024 * 1000, NULL);
78 }
79
80 close( my_fd );
81}