]> git.saurik.com Git - apple/libc.git/blob - tests/os_variant.c
3717f6d7d14b0a9af515d4167bcb231a875f77a1
[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
24 #define VARIANT_SKIP_EXPORTED
25
26 #include "../libdarwin/variant.c"
27
28 T_DECL(os_variant_detailed, "Looks at individual checks")
29 {
30 T_MAYFAIL;
31 T_EXPECT_FALSE(_check_disabled(VP_ALL), NULL);
32
33 T_MAYFAIL;
34 T_EXPECT_FALSE(_check_disabled(VP_CONTENT), NULL);
35
36 T_MAYFAIL;
37 T_EXPECT_FALSE(_check_disabled(VP_DIAGNOSTICS), NULL);
38
39 T_MAYFAIL;
40 T_EXPECT_FALSE(_check_disabled(VP_UI), NULL);
41
42 T_MAYFAIL;
43 T_EXPECT_FALSE(_check_disabled(VP_SECURITY), NULL);
44
45 #if !TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
46 T_MAYFAIL;
47 T_EXPECT_TRUE(_check_internal_content(), NULL);
48 #endif
49
50 #if TARGET_OS_IPHONE
51 T_MAYFAIL;
52 T_EXPECT_TRUE(_check_internal_release_type(), NULL);
53 #else
54 T_MAYFAIL;
55 T_EXPECT_FALSE(_check_internal_diags_profile(), NULL);
56 #endif
57
58 T_MAYFAIL;
59 T_EXPECT_TRUE(_check_can_has_debugger(), NULL);
60 }
61
62 T_DECL(os_variant_override_parse, "Checks the parsing of the override file")
63 {
64 // Warm up the dispatch_once
65 _check_disabled(VP_ALL);
66
67 T_LOG("Override: NULL"); // Live system
68 _parse_disabled_status(NULL);
69 T_MAYFAIL; T_EXPECT_FALSE(_check_disabled(VP_CONTENT), NULL);
70 T_MAYFAIL; T_EXPECT_FALSE(_check_disabled(VP_DIAGNOSTICS), NULL);
71 T_MAYFAIL; T_EXPECT_FALSE(_check_disabled(VP_UI), NULL);
72 T_MAYFAIL; T_EXPECT_FALSE(_check_disabled(VP_SECURITY), NULL);
73
74 T_LOG("Override: \"content\"");
75 _parse_disabled_status("content");
76 T_EXPECT_TRUE(_check_disabled(VP_CONTENT), NULL);
77 T_EXPECT_FALSE(_check_disabled(VP_DIAGNOSTICS), NULL);
78 T_EXPECT_FALSE(_check_disabled(VP_UI), NULL);
79 T_EXPECT_FALSE(_check_disabled(VP_SECURITY), NULL);
80
81 T_LOG("Override: \"ui\"");
82 _parse_disabled_status("ui");
83 T_EXPECT_FALSE(_check_disabled(VP_CONTENT), NULL);
84 T_EXPECT_FALSE(_check_disabled(VP_DIAGNOSTICS), NULL);
85 T_EXPECT_TRUE(_check_disabled(VP_UI), NULL);
86 T_EXPECT_FALSE(_check_disabled(VP_SECURITY), NULL);
87
88 T_LOG("Override: \"security,diagnostics\"");
89 _parse_disabled_status("security,diagnostics");
90 T_EXPECT_FALSE(_check_disabled(VP_CONTENT), NULL);
91 T_EXPECT_TRUE(_check_disabled(VP_DIAGNOSTICS), NULL);
92 T_EXPECT_FALSE(_check_disabled(VP_UI), NULL);
93 T_EXPECT_TRUE(_check_disabled(VP_SECURITY), NULL);
94
95 T_LOG("Override: \"content,diagnostics,ui,security\"");
96 _parse_disabled_status("content,diagnostics,ui,security");
97 T_EXPECT_TRUE(_check_disabled(VP_CONTENT), NULL);
98 T_EXPECT_TRUE(_check_disabled(VP_DIAGNOSTICS), NULL);
99 T_EXPECT_TRUE(_check_disabled(VP_UI), NULL);
100 T_EXPECT_TRUE(_check_disabled(VP_SECURITY), NULL);
101
102 T_LOG("Override: \"diagnostics\\n"); // Now check newline-handling.
103 _parse_disabled_status("diagnostics\n");
104 T_EXPECT_FALSE(_check_disabled(VP_CONTENT), NULL);
105 T_EXPECT_TRUE(_check_disabled(VP_DIAGNOSTICS), NULL);
106 T_EXPECT_FALSE(_check_disabled(VP_UI), NULL);
107 T_EXPECT_FALSE(_check_disabled(VP_SECURITY), NULL);
108
109 T_LOG("Override: \"content,diagnostics\\nui,security\\n\"");
110 _parse_disabled_status("content,diagnostics\nui,security\n");
111 T_EXPECT_TRUE(_check_disabled(VP_CONTENT), NULL);
112 T_EXPECT_TRUE(_check_disabled(VP_DIAGNOSTICS), NULL);
113 T_EXPECT_TRUE(_check_disabled(VP_UI), NULL);
114 T_EXPECT_TRUE(_check_disabled(VP_SECURITY), NULL);
115 }
116
117 T_DECL(os_status_cache, "Checks saving and restoring of state")
118 {
119 uint64_t status = 0;
120 size_t status_size = sizeof(status);
121 int ret = sysctlbyname(CACHE_SYSCTL_NAME, &status, &status_size, NULL, 0);
122 T_EXPECT_POSIX_ZERO(ret, "sysctlbyname(kern.osvariant_status)");
123 T_EXPECT_GT(status, 0ULL, "Kernel's status has bits set");
124 T_EXPECT_EQ(status & STATUS_INITIAL_BITS, STATUS_INITIAL_BITS, "Kernel's status has initial bits set");
125
126 T_MAYFAIL;
127 T_EXPECT_TRUE(_check_can_has_debugger(), NULL);
128
129 status = _get_cached_check_status();
130 T_LOG("Cached status: %llx", status);
131
132 T_EXPECT_EQ(status & STATUS_INITIAL_BITS, STATUS_INITIAL_BITS, "Our status has initial bits set");
133
134 _restore_cached_check_status(status);
135
136 T_MAYFAIL;
137 T_EXPECT_TRUE(os_variant_allows_internal_security_policies(NULL), NULL);
138
139 status = STATUS_INITIAL_BITS |
140 (S_NO << (SFP_CAN_HAS_DEBUGGER * STATUS_BIT_WIDTH));
141 T_LOG("Restoring status without can_has_debugger: %llx", status);
142 _restore_cached_check_status(status);
143
144 T_EXPECT_FALSE(_check_can_has_debugger(), NULL);
145
146 // Trigger dispatch_once internally with known state
147 _check_disabled(VP_SECURITY);
148
149 status = STATUS_INITIAL_BITS |
150 (0x1ULL << (VP_SECURITY + 32));
151 T_LOG("Restoring status with override: %llx", status);
152 _restore_cached_check_status(status);
153
154 T_EXPECT_TRUE(_check_disabled(VP_SECURITY), NULL);
155 }