]> git.saurik.com Git - apple/libc.git/blob - tests/os_variant.c
Libc-1439.100.3.tar.gz
[apple/libc.git] / tests / os_variant.c
1 #include <os/variant_private.h>
2
3 #include <darwintest.h>
4
5 /*
6 * Most of these are MAYFAIL because the test might sometimes run on non-internal devices.
7 */
8
9 T_DECL(os_variant_basic, "Just calls all the APIs")
10 {
11 T_MAYFAIL;
12 T_EXPECT_TRUE(os_variant_has_internal_content("com.apple.Libc.tests"), NULL);
13
14 T_MAYFAIL;
15 T_EXPECT_TRUE(os_variant_has_internal_diagnostics("com.apple.Libc.tests"), NULL);
16
17 T_MAYFAIL;
18 T_EXPECT_TRUE(os_variant_has_internal_ui("com.apple.Libc.tests"), NULL);
19
20 T_MAYFAIL;
21 T_EXPECT_TRUE(os_variant_allows_internal_security_policies("com.apple.Libc.tests"), NULL);
22
23 T_MAYFAIL;
24 T_EXPECT_FALSE(os_variant_has_factory_content("com.apple.Libc.tests"), NULL);
25
26 T_MAYFAIL;
27 T_EXPECT_FALSE(os_variant_is_darwinos("com.apple.Libc.tests"), NULL);
28
29 T_MAYFAIL;
30 T_EXPECT_FALSE(os_variant_uses_ephemeral_storage("com.apple.Libc.tests"), NULL);
31
32 T_MAYFAIL;
33 T_EXPECT_TRUE(os_variant_check("com.apple.Libc.tests", "HasFullLogging"), NULL);
34 }
35
36 #define VARIANT_SKIP_EXPORTED
37
38 #pragma clang diagnostic push
39 #pragma clang diagnostic ignored "-Wunused-function"
40 #include "../libdarwin/variant.c"
41 #pragma clang diagnostic pop
42
43 T_DECL(os_variant_detailed, "Looks at individual checks")
44 {
45 T_MAYFAIL;
46 T_EXPECT_FALSE(_check_disabled(VP_CONTENT), NULL);
47
48 T_MAYFAIL;
49 T_EXPECT_FALSE(_check_disabled(VP_DIAGNOSTICS), NULL);
50
51 T_MAYFAIL;
52 T_EXPECT_FALSE(_check_disabled(VP_UI), NULL);
53
54 T_MAYFAIL;
55 T_EXPECT_FALSE(_check_disabled(VP_SECURITY), NULL);
56
57 #if !TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
58 T_MAYFAIL;
59 T_EXPECT_TRUE(_check_internal_content(), NULL);
60 #endif
61
62 #if TARGET_OS_IPHONE
63 T_MAYFAIL;
64 T_EXPECT_TRUE(_check_internal_release_type(), NULL);
65
66 T_MAYFAIL;
67 T_EXPECT_TRUE(_check_factory_release_type(), NULL);
68 #else
69 T_MAYFAIL;
70 T_EXPECT_FALSE(_check_internal_diags_profile(), NULL);
71
72 T_MAYFAIL;
73 T_EXPECT_FALSE(_check_factory_content(), NULL);
74 #endif
75
76 T_MAYFAIL;
77 T_EXPECT_TRUE(_check_can_has_debugger(), NULL);
78 }
79
80 T_DECL(os_variant_override_parse, "Checks the parsing of the override file")
81 {
82 // Warm up the dispatch_once
83 _check_disabled(VP_CONTENT);
84
85 T_LOG("Override: NULL"); // Live system
86 _parse_disabled_status(NULL);
87 T_MAYFAIL; T_EXPECT_FALSE(_check_disabled(VP_CONTENT), NULL);
88 T_MAYFAIL; T_EXPECT_FALSE(_check_disabled(VP_DIAGNOSTICS), NULL);
89 T_MAYFAIL; T_EXPECT_FALSE(_check_disabled(VP_UI), NULL);
90 T_MAYFAIL; T_EXPECT_FALSE(_check_disabled(VP_SECURITY), NULL);
91
92 T_LOG("Override: \"content\"");
93 _parse_disabled_status("content");
94 T_EXPECT_TRUE(_check_disabled(VP_CONTENT), NULL);
95 T_EXPECT_FALSE(_check_disabled(VP_DIAGNOSTICS), NULL);
96 T_EXPECT_FALSE(_check_disabled(VP_UI), NULL);
97 T_EXPECT_FALSE(_check_disabled(VP_SECURITY), NULL);
98
99 T_LOG("Override: \"ui\"");
100 _parse_disabled_status("ui");
101 T_EXPECT_FALSE(_check_disabled(VP_CONTENT), NULL);
102 T_EXPECT_FALSE(_check_disabled(VP_DIAGNOSTICS), NULL);
103 T_EXPECT_TRUE(_check_disabled(VP_UI), NULL);
104 T_EXPECT_FALSE(_check_disabled(VP_SECURITY), NULL);
105
106 T_LOG("Override: \"security,diagnostics\"");
107 _parse_disabled_status("security,diagnostics");
108 T_EXPECT_FALSE(_check_disabled(VP_CONTENT), NULL);
109 T_EXPECT_TRUE(_check_disabled(VP_DIAGNOSTICS), NULL);
110 T_EXPECT_FALSE(_check_disabled(VP_UI), NULL);
111 T_EXPECT_TRUE(_check_disabled(VP_SECURITY), NULL);
112
113 T_LOG("Override: \"content,diagnostics,ui,security\"");
114 _parse_disabled_status("content,diagnostics,ui,security");
115 T_EXPECT_TRUE(_check_disabled(VP_CONTENT), NULL);
116 T_EXPECT_TRUE(_check_disabled(VP_DIAGNOSTICS), NULL);
117 T_EXPECT_TRUE(_check_disabled(VP_UI), NULL);
118 T_EXPECT_TRUE(_check_disabled(VP_SECURITY), NULL);
119
120 T_LOG("Override: \"diagnostics\\n"); // Now check newline-handling.
121 _parse_disabled_status("diagnostics\n");
122 T_EXPECT_FALSE(_check_disabled(VP_CONTENT), NULL);
123 T_EXPECT_TRUE(_check_disabled(VP_DIAGNOSTICS), NULL);
124 T_EXPECT_FALSE(_check_disabled(VP_UI), NULL);
125 T_EXPECT_FALSE(_check_disabled(VP_SECURITY), NULL);
126
127 T_LOG("Override: \"content,diagnostics\\nui,security\\n\"");
128 _parse_disabled_status("content,diagnostics\nui,security\n");
129 T_EXPECT_TRUE(_check_disabled(VP_CONTENT), NULL);
130 T_EXPECT_TRUE(_check_disabled(VP_DIAGNOSTICS), NULL);
131 T_EXPECT_TRUE(_check_disabled(VP_UI), NULL);
132 T_EXPECT_TRUE(_check_disabled(VP_SECURITY), NULL);
133 }
134
135 T_DECL(os_status_cache, "Checks saving and restoring of state")
136 {
137 uint64_t status = 0;
138 size_t status_size = sizeof(status);
139 int ret = sysctlbyname(CACHE_SYSCTL_NAME, &status, &status_size, NULL, 0);
140 T_EXPECT_POSIX_ZERO(ret, "sysctlbyname(kern.osvariant_status)");
141 T_EXPECT_GT(status, 0ULL, "Kernel's status has bits set");
142 T_EXPECT_EQ(status & STATUS_INITIAL_BITS, STATUS_INITIAL_BITS, "Kernel's status has initial bits set");
143
144 T_MAYFAIL;
145 T_EXPECT_TRUE(_check_can_has_debugger(), NULL);
146
147 status = _get_cached_check_status();
148 T_LOG("Cached status: %llx", status);
149
150 T_EXPECT_EQ(status & STATUS_INITIAL_BITS, STATUS_INITIAL_BITS, "Our status has initial bits set");
151
152 _restore_cached_check_status(status);
153
154 T_MAYFAIL;
155 T_EXPECT_TRUE(os_variant_allows_internal_security_policies(NULL), NULL);
156
157 status = STATUS_INITIAL_BITS |
158 (S_NO << (SFP_CAN_HAS_DEBUGGER * STATUS_BIT_WIDTH)) |
159 (S_NO << (SFP_DEVELOPMENT_KERNEL * STATUS_BIT_WIDTH));
160 T_LOG("Restoring status without can_has_debugger and development_kernel: %llx", status);
161 _restore_cached_check_status(status);
162
163 T_EXPECT_FALSE(_check_can_has_debugger(), NULL);
164
165 // Trigger dispatch_once internally with known state
166 _check_disabled(VP_SECURITY);
167
168 status = STATUS_INITIAL_BITS |
169 (0x1ULL << (VP_SECURITY + 32));
170 T_LOG("Restoring status with override: %llx", status);
171 _restore_cached_check_status(status);
172
173 T_EXPECT_TRUE(_check_disabled(VP_SECURITY), NULL);
174 }
175
176 // Ignore the last NULL entry in _variant_map.
177 #define VARIANTS_LEN (sizeof(_variant_map) / sizeof(_variant_map[0])) - 1
178
179 T_DECL(os_variant_asciibetical,
180 "Check that the variant map is in asciibetical order")
181 {
182 const char *prev_variant = _variant_map[0].variant;
183 for (size_t i = 1; i < VARIANTS_LEN; i++) {
184 const char *variant = _variant_map[i].variant;
185 T_EXPECT_GT(strcmp(variant, prev_variant), 0,
186 "Variant %s should be asciibetically after variant %s",
187 variant, prev_variant);
188 prev_variant = variant;
189 }
190 }
191
192 T_DECL(os_variant_copy_description,
193 "Check that the description matches what os_variant_check returns")
194 {
195 T_SETUPBEGIN;
196 struct {
197 const char *variant;
198 bool seen;
199 } seen_variants[VARIANTS_LEN] = {};
200 for (size_t i = 0; i < VARIANTS_LEN; i++) {
201 seen_variants[i].variant = _variant_map[i].variant;
202 T_QUIET; T_ASSERT_NOTNULL(seen_variants[i].variant,
203 "copied variant %zu to test for", i);
204 }
205 T_SETUPEND;
206
207 char *variant_desc = os_variant_copy_description("com.apple.Libc.tests");
208 T_WITH_ERRNO; T_ASSERT_NOTNULL(variant_desc,
209 "copied os_variant description");
210 T_LOG("Got os_variant description: %s", variant_desc);
211
212 char *next_variant = variant_desc;
213 while (next_variant && next_variant[0] != '\0') {
214 char *variant_end = strchr(next_variant, ' ');
215 if (variant_end) {
216 variant_end[0] = '\0';
217 variant_end += 1;
218 }
219 for (size_t i = 0; i < VARIANTS_LEN; i++) {
220 if (strcmp(next_variant, seen_variants[i].variant) == 0) {
221 seen_variants[i].seen = true;
222 }
223 }
224 T_EXPECT_TRUE(os_variant_check("com.apple.Libc.tests", next_variant),
225 "Y: %s: os_variant_check should agree with description",
226 next_variant);
227 next_variant = variant_end;
228 }
229
230 for (size_t i = 0; i < VARIANTS_LEN; i++) {
231 if (!seen_variants[i].seen) {
232 T_EXPECT_FALSE(os_variant_check("com.apple.Libc.tests",
233 seen_variants[i].variant),
234 "N: %s: os_variant_check should return false for variant "
235 "missing from description", seen_variants[i].variant);
236 }
237 }
238
239 free(variant_desc);
240 }