]> git.saurik.com Git - apple/security.git/blob - SecExperiment/test/SecExperimentTests.m
Security-59306.41.2.tar.gz
[apple/security.git] / SecExperiment / test / SecExperimentTests.m
1 //
2 // SecExperimentTests.m
3 //
4 //
5
6 #import <XCTest/XCTest.h>
7 #import "SecExperimentInternal.h"
8 #import "SecExperimentPriv.h"
9
10 @interface SecExperimentTests : XCTestCase
11 @end
12
13 @implementation SecExperimentTests
14
15 - (void)testCStyleGetTlsConfig {
16 sec_experiment_t experiment = sec_experiment_create(kSecExperimentTLSMobileAssetConfig);
17 sec_experiment_set_sampling_disabled(experiment, true);
18 XCTAssert(experiment, @"sec_experiment_create");
19
20 xpc_object_t tlsconfig = nil;
21 tlsconfig = sec_experiment_copy_configuration(experiment);
22 XCTAssertNotNil(tlsconfig);
23
24 }
25
26 - (void)testCStyleGetTlsConfig_SkipSampling {
27 sec_experiment_t experiment = sec_experiment_create(kSecExperimentTLSMobileAssetConfig);
28 XCTAssert(experiment, @"sec_experiment_create");
29
30 sec_experiment_set_sampling_disabled(experiment, true);
31 xpc_object_t tlsconfig = sec_experiment_copy_configuration(experiment);
32 XCTAssertNotNil(tlsconfig);
33 }
34
35 - (void)testExperimentRun_SkipSamping {
36 dispatch_queue_t test_queue = dispatch_queue_create("test_queue", NULL);
37
38 __block dispatch_semaphore_t blocker = dispatch_semaphore_create(0);
39 __block bool experiment_invoked = false;
40 sec_experiment_run_block_t run_block = ^bool(const char *identifier, xpc_object_t config) {
41 experiment_invoked = identifier != NULL && config != NULL;
42 dispatch_semaphore_signal(blocker);
43 return experiment_invoked;
44 };
45
46 sec_experiment_run_internal(kSecExperimentTLSMobileAssetConfig, true, test_queue, run_block);
47 XCTAssertTrue(dispatch_semaphore_wait(blocker, dispatch_time(DISPATCH_TIME_NOW, (uint64_t)(5 * NSEC_PER_SEC))) == 0L);
48 XCTAssertTrue(experiment_invoked);
49 }
50
51 - (void)testDefaultsConfigCopy {
52 const char *test_key = "test_defaults_experiment_key";
53 const char *test_value = "test_value";
54
55 sec_experiment_t experiment = sec_experiment_create(test_key);
56 XCTAssert(experiment, @"sec_experiment_create");
57
58 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
59
60 NSString *test_key_string = [NSString stringWithUTF8String:test_key];
61 NSString *test_value_string = [NSString stringWithUTF8String:test_value];
62 NSDictionary *testConfig = @{test_key_string: test_value_string};
63 [defaults setPersistentDomain:testConfig forName:[NSString stringWithUTF8String:kSecExperimentDefaultsDomain]];
64
65 sec_experiment_set_sampling_disabled(experiment, true);
66 xpc_object_t tlsconfig = sec_experiment_copy_configuration(experiment);
67 XCTAssertNotNil(tlsconfig);
68 XCTAssertTrue(xpc_get_type(tlsconfig) == XPC_TYPE_STRING);
69 if (xpc_get_type(tlsconfig) == XPC_TYPE_STRING) {
70 XCTAssertTrue(strcmp(xpc_string_get_string_ptr(tlsconfig), test_value) == 0);
71 }
72
73 // Clear the persistent domain
74 [defaults removePersistentDomainForName:[NSString stringWithUTF8String:kSecExperimentDefaultsDomain]];
75 }
76
77 @end