1 #include <os/variant_private.h>
3 #include <darwintest.h>
6 * Most of these are MAYFAIL because the test might sometimes run on non-internal devices.
9 T_DECL(os_variant_basic
, "Just calls all the APIs")
12 T_EXPECT_TRUE(os_variant_has_internal_content("com.apple.Libc.tests"), NULL
);
15 T_EXPECT_TRUE(os_variant_has_internal_diagnostics("com.apple.Libc.tests"), NULL
);
18 T_EXPECT_TRUE(os_variant_has_internal_ui("com.apple.Libc.tests"), NULL
);
21 T_EXPECT_TRUE(os_variant_allows_internal_security_policies("com.apple.Libc.tests"), NULL
);
24 T_EXPECT_FALSE(os_variant_has_factory_content("com.apple.Libc.tests"), NULL
);
27 T_EXPECT_FALSE(os_variant_is_darwinos("com.apple.Libc.tests"), NULL
);
30 T_EXPECT_FALSE(os_variant_uses_ephemeral_storage("com.apple.Libc.tests"), NULL
);
33 T_EXPECT_TRUE(os_variant_check("com.apple.Libc.tests", "HasFullLogging"), NULL
);
36 #define VARIANT_SKIP_EXPORTED
38 #pragma clang diagnostic push
39 #pragma clang diagnostic ignored "-Wunused-function"
40 #include "../libdarwin/variant.c"
41 #pragma clang diagnostic pop
43 T_DECL(os_variant_detailed
, "Looks at individual checks")
46 T_EXPECT_FALSE(_check_disabled(VP_CONTENT
), NULL
);
49 T_EXPECT_FALSE(_check_disabled(VP_DIAGNOSTICS
), NULL
);
52 T_EXPECT_FALSE(_check_disabled(VP_UI
), NULL
);
55 T_EXPECT_FALSE(_check_disabled(VP_SECURITY
), NULL
);
57 #if !TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
59 T_EXPECT_TRUE(_check_internal_content(), NULL
);
64 T_EXPECT_TRUE(_check_internal_release_type(), NULL
);
67 T_EXPECT_TRUE(_check_factory_release_type(), NULL
);
70 T_EXPECT_FALSE(_check_internal_diags_profile(), NULL
);
73 T_EXPECT_FALSE(_check_factory_content(), NULL
);
77 T_EXPECT_TRUE(_check_can_has_debugger(), NULL
);
80 T_DECL(os_variant_override_parse
, "Checks the parsing of the override file")
82 // Warm up the dispatch_once
83 _check_disabled(VP_CONTENT
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
135 T_DECL(os_status_cache
, "Checks saving and restoring of state")
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");
145 T_EXPECT_TRUE(_check_can_has_debugger(), NULL
);
147 status
= _get_cached_check_status();
148 T_LOG("Cached status: %llx", status
);
150 T_EXPECT_EQ(status
& STATUS_INITIAL_BITS
, STATUS_INITIAL_BITS
, "Our status has initial bits set");
152 _restore_cached_check_status(status
);
155 T_EXPECT_TRUE(os_variant_allows_internal_security_policies(NULL
), NULL
);
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
);
163 T_EXPECT_FALSE(_check_can_has_debugger(), NULL
);
165 // Trigger dispatch_once internally with known state
166 _check_disabled(VP_SECURITY
);
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
);
173 T_EXPECT_TRUE(_check_disabled(VP_SECURITY
), NULL
);
176 // Ignore the last NULL entry in _variant_map.
177 #define VARIANTS_LEN (sizeof(_variant_map) / sizeof(_variant_map[0])) - 1
179 T_DECL(os_variant_asciibetical
,
180 "Check that the variant map is in asciibetical order")
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
;
192 T_DECL(os_variant_copy_description
,
193 "Check that the description matches what os_variant_check returns")
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
);
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
);
212 char *next_variant
= variant_desc
;
213 while (next_variant
&& next_variant
[0] != '\0') {
214 char *variant_end
= strchr(next_variant
, ' ');
216 variant_end
[0] = '\0';
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;
224 T_EXPECT_TRUE(os_variant_check("com.apple.Libc.tests", next_variant
),
225 "Y: %s: os_variant_check should agree with description",
227 next_variant
= variant_end
;
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
);