2 // SecExperimentTests.m
6 #import <XCTest/XCTest.h>
7 #import "SecExperimentInternal.h"
8 #import "SecExperimentPriv.h"
10 @interface SecExperimentTests : XCTestCase
13 @implementation SecExperimentTests
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");
20 xpc_object_t tlsconfig = nil;
21 tlsconfig = sec_experiment_copy_configuration(experiment);
22 XCTAssertNotNil(tlsconfig);
26 - (void)testCStyleGetTlsConfig_SkipSampling {
27 sec_experiment_t experiment = sec_experiment_create(kSecExperimentTLSMobileAssetConfig);
28 XCTAssert(experiment, @"sec_experiment_create");
30 sec_experiment_set_sampling_disabled(experiment, true);
31 xpc_object_t tlsconfig = sec_experiment_copy_configuration(experiment);
32 XCTAssertNotNil(tlsconfig);
35 - (void)testExperimentRun_SkipSamping {
36 dispatch_queue_t test_queue = dispatch_queue_create("test_queue", NULL);
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;
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);
51 - (void)testDefaultsConfigCopy {
52 const char *test_key = "test_defaults_experiment_key";
53 const char *test_value = "test_value";
55 sec_experiment_t experiment = sec_experiment_create(test_key);
56 XCTAssert(experiment, @"sec_experiment_create");
58 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
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]];
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);
73 // Clear the persistent domain
74 [defaults removePersistentDomainForName:[NSString stringWithUTF8String:kSecExperimentDefaultsDomain]];