xref: /xnu-11417.101.15/tests/vfs/statfs_ext.c (revision e3723e1f17661b24996789d8afc084c0c3303b26)
1 /*
2  * Copyright (c) 2024 Apple Computer, 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 /* compile: xcrun -sdk macosx.internal clang -ldarwintest -o statfs_ext statfs_ext.c -g -Weverything */
30 
31 #include <stdlib.h>
32 #include <fcntl.h>
33 #include <sys/mount.h>
34 #include <sys/stat.h>
35 
36 #include <darwintest.h>
37 #include <darwintest/utils.h>
38 
39 #define RUN_TEST     TARGET_OS_OSX
40 
41 #define FSTYPE_DEVFS "devfs"
42 static char template[MAXPATHLEN];
43 static char *testdir = NULL;
44 
45 #define TEST_MODE_STATFS  0
46 #define TEST_MODE_FSTATFS 1
47 
48 static const char *flag_name[] =
49 { "0", "STATFS_EXT_NOBLOCK" };
50 
51 static const char *mode_name[] =
52 { "TEST_MODE_STATFS", "TEST_MODE_FSTATFS" };
53 
54 T_GLOBAL_META(
55 	T_META_NAMESPACE("xnu.vfs"),
56 	T_META_RADAR_COMPONENT_NAME("xnu"),
57 	T_META_RADAR_COMPONENT_VERSION("vfs"),
58 	T_META_ASROOT(false),
59 	T_META_ENABLED(RUN_TEST),
60 	T_META_CHECK_LEAKS(false));
61 
62 static void
cleanup(void)63 cleanup(void)
64 {
65 	if (testdir) {
66 		unmount(testdir, MNT_FORCE);
67 		rmdir(testdir);
68 	}
69 }
70 
71 static void
statfs_compare(const char * path,struct statfs * sfs_ext,int mode,int flag,int expected_err)72 statfs_compare(const char *path, struct statfs *sfs_ext, int mode, int flag, int expected_err)
73 {
74 	int fd;
75 	struct statfs sfs;
76 
77 	T_LOG("Testing: path %s, sfs_ext %p, mode %s, flag 0x%x, expected_err %d", path, (void *) sfs_ext, mode_name[mode], flag, expected_err);
78 
79 	if (sfs_ext) {
80 		bzero(sfs_ext, sizeof(struct statfs));
81 	}
82 
83 	switch (mode) {
84 	case TEST_MODE_STATFS:
85 		if (expected_err) {
86 			T_ASSERT_POSIX_FAILURE(statfs_ext(path, sfs_ext, flag), expected_err, "Verifying that statfs_ext() fails with %d (%s)", expected_err, strerror(expected_err));
87 		} else {
88 			T_ASSERT_POSIX_SUCCESS(statfs(path, &sfs), "Calling stafs()");
89 			T_ASSERT_POSIX_SUCCESS(statfs_ext(path, sfs_ext, flag), "Calling statfs_ext() using the %s flag", flag_name[flag]);
90 		}
91 		break;
92 	case TEST_MODE_FSTATFS:
93 		T_ASSERT_POSIX_SUCCESS(fd = open(path, O_DIRECTORY | O_RDONLY), "Opening %s", path);
94 		if (expected_err) {
95 			T_ASSERT_POSIX_FAILURE(fstatfs_ext(fd, sfs_ext, flag), expected_err, "Verifying that fstatfs_ext() fails with %d (%s)", expected_err, strerror(expected_err));
96 		} else {
97 			T_ASSERT_POSIX_SUCCESS(fstatfs(fd, &sfs), "Calling fstafs()");
98 			T_ASSERT_POSIX_SUCCESS(fstatfs_ext(fd, sfs_ext, flag), "Calling fstatfs_ext() using the %s flag", flag_name[flag]);
99 		}
100 		T_ASSERT_POSIX_SUCCESS(close(fd), "Closing fd");
101 		break;
102 	default:
103 		T_FAIL("Unknown test mode");
104 	}
105 
106 	if (expected_err) {
107 		return;
108 	}
109 
110 	switch (flag) {
111 	case 0:
112 		T_ASSERT_EQ(memcmp(&sfs, sfs_ext, sizeof(struct statfs)), 0, "Validating statfs structure");
113 		break;
114 	case STATFS_EXT_NOBLOCK:
115 		T_ASSERT_EQ(sfs.f_fsid.val[0], sfs_ext->f_fsid.val[0], "Validating f_fsid.val[0]");
116 		T_ASSERT_EQ(sfs.f_fsid.val[1], sfs_ext->f_fsid.val[1], "Validating f_fsid.val[1]");
117 		T_ASSERT_EQ(sfs.f_owner, sfs_ext->f_owner, "Validating f_owner");
118 		T_ASSERT_EQ(sfs.f_type, sfs_ext->f_type, "Validating f_type");
119 		T_ASSERT_EQ(sfs.f_flags, sfs_ext->f_flags, "Validating f_flags");
120 		T_ASSERT_EQ(sfs.f_fssubtype, sfs_ext->f_fssubtype, "Validating f_fssubtype");
121 		T_ASSERT_EQ_STR(sfs.f_fstypename, sfs_ext->f_fstypename, "Validating f_fstypename");
122 		T_ASSERT_EQ_STR(sfs.f_mntonname, sfs_ext->f_mntonname, "Validating f_mntonname");
123 		T_ASSERT_EQ_STR(sfs.f_mntfromname, sfs_ext->f_mntfromname, "Validating f_mntfromname");
124 		T_ASSERT_EQ(sfs.f_flags_ext, sfs_ext->f_flags_ext, "Validating f_flags_ext");
125 		break;
126 	default:
127 		T_FAIL("Unknown flag");
128 	}
129 }
130 
131 T_DECL(statfs_ext,
132     "test statfs_ext and fstatfs_ext")
133 {
134 #if (!RUN_TEST)
135 	T_SKIP("Not macOS");
136 #endif
137 
138 	struct statfs sfs_ext;
139 
140 	T_ATEND(cleanup);
141 
142 	T_SETUPBEGIN;
143 
144 	snprintf(template, sizeof(template), "%s/statfs_ext-XXXXXX", dt_tmpdir());
145 	T_ASSERT_POSIX_NOTNULL((testdir = mkdtemp(template)), "Creating test root dir");
146 	T_ASSERT_POSIX_SUCCESS(mount(FSTYPE_DEVFS, testdir, MNT_RDONLY, NULL), "Mounting temporary %s mount using path %s", FSTYPE_DEVFS, testdir);
147 
148 	T_SETUPEND;
149 
150 	/* Test fstatfs_ext() with invalid flags */
151 	statfs_compare("/dev", &sfs_ext, TEST_MODE_STATFS, 0x10, EINVAL);
152 	statfs_compare(testdir, &sfs_ext, TEST_MODE_FSTATFS, STATFS_EXT_NOBLOCK | 0x8, EINVAL);
153 
154 	/* Test invalid inputs */
155 	statfs_compare(NULL, &sfs_ext, TEST_MODE_STATFS, STATFS_EXT_NOBLOCK, EFAULT);
156 	statfs_compare("/", NULL, TEST_MODE_STATFS, STATFS_EXT_NOBLOCK, EFAULT);
157 	statfs_compare("/dev", NULL, TEST_MODE_FSTATFS, STATFS_EXT_NOBLOCK, EFAULT);
158 
159 	/* Test fstatfs_ext() with zero flags */
160 	statfs_compare("/", &sfs_ext, TEST_MODE_STATFS, 0, 0);
161 	statfs_compare("/private/var/tmp", &sfs_ext, TEST_MODE_FSTATFS, 0, 0);
162 
163 	/* Test fstatfs_ext() with the STATFS_EXT_NOBLOCK flag */
164 	statfs_compare("/", &sfs_ext, TEST_MODE_STATFS, STATFS_EXT_NOBLOCK, 0);
165 	statfs_compare("/dev", &sfs_ext, TEST_MODE_STATFS, STATFS_EXT_NOBLOCK, 0);
166 	statfs_compare("/private/var/tmp", &sfs_ext, TEST_MODE_FSTATFS, STATFS_EXT_NOBLOCK, 0);
167 	statfs_compare(testdir, &sfs_ext, TEST_MODE_FSTATFS, STATFS_EXT_NOBLOCK, 0);
168 }
169