]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/tests/CKKSDispatchTests.m
Security-59306.80.4.tar.gz
[apple/security.git] / keychain / ckks / tests / CKKSDispatchTests.m
1 /*
2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <dispatch/dispatch.h>
25 #import <XCTest/XCTest.h>
26 #import "keychain/ckks/CKKSNearFutureScheduler.h"
27
28 @interface CKKSNearFutureSchedulerTests : XCTestCase
29
30 @end
31
32 @implementation CKKSNearFutureSchedulerTests
33
34 - (void)setUp {
35 [super setUp];
36 }
37
38 - (void)tearDown {
39 [super tearDown];
40 }
41
42 - (void)testOneShot {
43 XCTestExpectation *expectation = [self expectationWithDescription:@"FutureScheduler fired"];
44
45 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay:50*NSEC_PER_MSEC keepProcessAlive:true block:^{
46 [expectation fulfill];
47 }];
48
49 [scheduler trigger];
50
51 [self waitForExpectationsWithTimeout:1 handler:nil];
52 }
53
54 - (void)testOneShotDelay {
55 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
56 toofastexpectation.inverted = YES;
57
58 XCTestExpectation *expectation = [self expectationWithDescription:@"FutureScheduler fired"];
59
60 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 200*NSEC_PER_MSEC keepProcessAlive:false block:^{
61 [toofastexpectation fulfill];
62 [expectation fulfill];
63 }];
64
65 [scheduler trigger];
66
67 // Make sure it waits at least 0.1 seconds
68 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
69
70 // But finishes within 1.1s (total)
71 [self waitForExpectations: @[expectation] timeout:1];
72 }
73
74 - (void)testOneShotManyTrigger {
75 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
76 toofastexpectation.inverted = YES;
77
78 XCTestExpectation *expectation = [self expectationWithDescription:@"FutureScheduler fired"];
79 expectation.assertForOverFulfill = YES;
80
81 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 200*NSEC_PER_MSEC keepProcessAlive:true block:^{
82 [toofastexpectation fulfill];
83 [expectation fulfill];
84 }];
85
86 [scheduler trigger];
87 [scheduler trigger];
88 [scheduler trigger];
89 [scheduler trigger];
90 [scheduler trigger];
91 [scheduler trigger];
92 [scheduler trigger];
93 [scheduler trigger];
94
95 // Make sure it waits at least 0.1 seconds
96 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
97
98 // But finishes within .6s (total)
99 [self waitForExpectations: @[expectation] timeout:0.5];
100
101 // Ensure we don't get called again in the next 0.3 s
102 XCTestExpectation* waitmore = [self expectationWithDescription:@"waiting"];
103 waitmore.inverted = YES;
104 [self waitForExpectations: @[waitmore] timeout: 0.3];
105 }
106
107
108 - (void)testMultiShot {
109 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
110 first.assertForOverFulfill = NO;
111
112 XCTestExpectation *second = [self expectationWithDescription:@"FutureScheduler fired (two)"];
113 second.expectedFulfillmentCount = 2;
114 second.assertForOverFulfill = YES;
115
116 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 100*NSEC_PER_MSEC keepProcessAlive:false block:^{
117 [first fulfill];
118 [second fulfill];
119 }];
120
121 [scheduler trigger];
122
123 [self waitForExpectations: @[first] timeout:0.2];
124
125 [scheduler trigger];
126 [scheduler trigger];
127 [scheduler trigger];
128
129 [self waitForExpectations: @[second] timeout:0.2];
130
131 XCTestExpectation* waitmore = [self expectationWithDescription:@"waiting"];
132 waitmore.inverted = YES;
133 [self waitForExpectations: @[waitmore] timeout: 0.2];
134 }
135
136 - (void)testMultiShotDelays {
137 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
138 first.assertForOverFulfill = NO;
139
140 XCTestExpectation *longdelay = [self expectationWithDescription:@"FutureScheduler fired (long delay expectation)"];
141 longdelay.inverted = YES;
142 longdelay.expectedFulfillmentCount = 2;
143
144 XCTestExpectation *second = [self expectationWithDescription:@"FutureScheduler fired (two)"];
145 second.expectedFulfillmentCount = 2;
146 second.assertForOverFulfill = YES;
147
148 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" initialDelay: 50*NSEC_PER_MSEC continuingDelay:300*NSEC_PER_MSEC keepProcessAlive:false block:^{
149 [first fulfill];
150 [longdelay fulfill];
151 [second fulfill];
152 }];
153
154 [scheduler trigger];
155
156 [self waitForExpectations: @[first] timeout:0.2];
157
158 [scheduler trigger];
159 [scheduler trigger];
160 [scheduler trigger];
161
162 // longdelay should NOT be fulfilled twice in the first 0.3 seconds
163 [self waitForExpectations: @[longdelay] timeout:0.2];
164
165 // But second should be fulfilled in the first 0.8 seconds
166 [self waitForExpectations: @[second] timeout:0.5];
167
168 XCTestExpectation* waitmore = [self expectationWithDescription:@"waiting"];
169 waitmore.inverted = YES;
170 [self waitForExpectations: @[waitmore] timeout: 0.2];
171 }
172
173 - (void)testCancel {
174 XCTestExpectation *cancelexpectation = [self expectationWithDescription:@"FutureScheduler fired (after cancel)"];
175 cancelexpectation.inverted = YES;
176
177 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 100*NSEC_PER_MSEC keepProcessAlive:true block:^{
178 [cancelexpectation fulfill];
179 }];
180
181 [scheduler trigger];
182 [scheduler cancel];
183
184 // Make sure it does not fire in 0.5 s
185 [self waitForExpectations: @[cancelexpectation] timeout:0.2];
186 }
187
188 - (void)testDelayedNoShot {
189 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
190 toofastexpectation.inverted = YES;
191
192 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 10*NSEC_PER_MSEC keepProcessAlive:false block:^{
193 [toofastexpectation fulfill];
194 }];
195
196 // Tell the scheduler to wait, but don't trigger it. It shouldn't fire.
197 [scheduler waitUntil: 50*NSEC_PER_MSEC];
198
199 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
200 }
201
202 - (void)testDelayedOneShot {
203 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
204 first.assertForOverFulfill = NO;
205
206 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
207 toofastexpectation.inverted = YES;
208
209 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 10*NSEC_PER_MSEC keepProcessAlive:false block:^{
210 [first fulfill];
211 [toofastexpectation fulfill];
212 }];
213
214 [scheduler waitUntil: 150*NSEC_PER_MSEC];
215 [scheduler trigger];
216
217 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
218 [self waitForExpectations: @[first] timeout:0.2];
219 }
220
221 - (void)testDelayedMultiShot {
222 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
223 first.assertForOverFulfill = NO;
224
225 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
226 toofastexpectation.expectedFulfillmentCount = 2;
227 toofastexpectation.inverted = YES;
228
229 XCTestExpectation *second = [self expectationWithDescription:@"FutureScheduler fired (two)"];
230 second.expectedFulfillmentCount = 2;
231 second.assertForOverFulfill = YES;
232
233 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 10*NSEC_PER_MSEC keepProcessAlive:false block:^{
234 [first fulfill];
235 [second fulfill];
236 [toofastexpectation fulfill];
237 }];
238
239 [scheduler trigger];
240 [self waitForExpectations: @[first] timeout:0.2];
241
242 [scheduler waitUntil: 150*NSEC_PER_MSEC];
243 [scheduler trigger];
244
245 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
246 [self waitForExpectations: @[second] timeout:0.3];
247 }
248
249 @end