]> git.saurik.com Git - apple/xnu.git/blob - tests/trial_experiments.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / tests / trial_experiments.c
1 #include <errno.h>
2 #include <sys/types.h>
3 #include <sys/sysctl.h>
4
5 #include <darwintest.h>
6
7 #include "drop_priv.h"
8 #include "test_utils.h"
9
10 #if ENTITLED
11 #define SET_TREATMENT_ID set_treatment_id_entitled
12 #define SET_TREATMENT_ID_DESCR "Can set treatment id with entitlement"
13 #else /* ENTITLED */
14 #define SET_TREATMENT_ID set_treatment_id_unentitled
15 #define SET_TREATMENT_ID_DESCR "Can't set treatment id without entitlement"
16 #endif /* ENTITLED */
17
18 T_DECL(SET_TREATMENT_ID, "Verifies that EXPERIMENT sysctls can only be set with the entitlement", T_META_ASROOT(false))
19 {
20 #define TEST_STR "testing"
21 #define IDENTIFIER_LENGTH 36
22
23 int ret;
24 errno_t err;
25 char val[IDENTIFIER_LENGTH + 1] = {0};
26 size_t len = sizeof(val);
27 char new_val[IDENTIFIER_LENGTH + 1] = {0};
28
29 if (!is_development_kernel()) {
30 T_SKIP("skipping test on release kernel");
31 }
32
33 strlcpy(new_val, TEST_STR, sizeof(new_val));
34 drop_priv();
35
36 ret = sysctlbyname("kern.trial_treatment_id", val, &len, new_val, strlen(new_val));
37 err = errno;
38 #if ENTITLED
39 len = sizeof(val);
40 memset(new_val, 0, sizeof(new_val));
41 T_ASSERT_POSIX_SUCCESS(ret, "set kern.trial_treatment_id");
42 /* Cleanup. Set it back to the empty string. */
43 ret = sysctlbyname("kern.trial_treatment_id", val, &len, new_val, 1);
44 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "reset kern.trial_treatment_id");
45 #else
46 T_ASSERT_POSIX_FAILURE(ret, EPERM, "set kern.trial_treatment_id");
47 #endif /* ENTITLED */
48 }
49
50 #if ENTITLED
51 /* Check min and max value limits on numeric factors */
52 T_DECL(experiment_factor_numeric_limits,
53 "Can only set factors within the legal range.",
54 T_META_ASROOT(false))
55 {
56 #define kMinVal 5 /* The min value allowed for the testing factor. */
57 #define kMaxVal 10 /* The max value allowed for the testing factor. */
58 errno_t err;
59 int ret;
60 unsigned int current_val;
61 size_t len = sizeof(current_val);
62 unsigned int new_val;
63
64 drop_priv();
65 new_val = kMinVal - 1;
66 ret = sysctlbyname("kern.testing_experiment_factor", &current_val, &len, &new_val, sizeof(new_val));
67 err = errno;
68 T_ASSERT_POSIX_FAILURE(ret, EINVAL, "set kern.testing_experiment_factor below range.");
69
70 new_val = kMaxVal + 1;
71 ret = sysctlbyname("kern.testing_experiment_factor", &current_val, &len, &new_val, sizeof(new_val));
72 err = errno;
73 T_ASSERT_POSIX_FAILURE(ret, EINVAL, "set kern.testing_experiment_factor above range.");
74
75 new_val = kMaxVal;
76 ret = sysctlbyname("kern.testing_experiment_factor", &current_val, &len, &new_val, sizeof(new_val));
77 T_ASSERT_POSIX_SUCCESS(ret, "set kern.testing_experiment_factor at top of range.");
78
79 new_val = kMinVal;
80 ret = sysctlbyname("kern.testing_experiment_factor", &current_val, &len, &new_val, sizeof(new_val));
81 T_ASSERT_POSIX_SUCCESS(ret, "set kern.testing_experiment_factor at bottom of range.");
82 }
83 #endif /* ENTITLED */