2 // SecTapToRadarTests.m
6 #import <TargetConditionals.h>
10 #import <XCTest/XCTest.h>
11 #import <OCMock/OCMock.h>
12 #import "utilities/SecTapToRadar.h"
14 @interface SecTapToRadarTests : XCTestCase
15 @property BOOL isRateLimited;
16 @property BOOL userSay;
18 @property bool ttrDidAppear;
23 @implementation SecTapToRadarTests
25 - (void)triggerTapToRadar:(SecTapToRadar *)ttrRequest
27 self.ttrDidAppear = true;
30 - (BOOL)isRateLimited:(SecTapToRadar *)ttrRequest
32 return self.isRateLimited;
35 - (BOOL)askUserIfTTR:(SecTapToRadar *)ttrRequest
42 self.ttrDidAppear = NO;
43 self.isRateLimited = NO;
46 self.mockTTR = OCMClassMock([SecTapToRadar class]);
47 OCMStub([self.mockTTR triggerTapToRadar:[OCMArg any]]).andCall(self, @selector(triggerTapToRadar:));
48 OCMStub([self.mockTTR isRateLimited:[OCMArg any]]).andCall(self, @selector(isRateLimited:));
49 OCMStub([self.mockTTR askUserIfTTR:[OCMArg any]]).andCall(self, @selector(askUserIfTTR:));
52 - (void)testSecTTRNormal {
54 SecTapToRadar *ttr = [[SecTapToRadar alloc] initTapToRadar:@"alert" description:@"test" radar:@"1"];
57 XCTAssertTrue(self.ttrDidAppear, "should have appeared");
60 - (void)testSecTTRRateLimit {
61 SecTapToRadar *ttr = [[SecTapToRadar alloc] initTapToRadar:@"alert" description:@"test" radar:@"1"];
63 self.isRateLimited = YES;
65 XCTAssertFalse(self.ttrDidAppear, "should not have appered");
68 - (void)testSecTTRUserSupress {
70 SecTapToRadar *ttr = [[SecTapToRadar alloc] initTapToRadar:@"alert" description:@"test" radar:@"1"];
74 XCTAssertFalse(self.ttrDidAppear, "should not have appered");
78 XCTAssertTrue(self.ttrDidAppear, "should have appeared");
81 - (void)testSecTTRRateLimiter {
83 SecTapToRadar *ttr = [[SecTapToRadar alloc] initTapToRadar:@"alert" description:@"test" radar:@"1"];
84 NSString *key = [SecTapToRadar keyname:ttr];
85 NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"com.apple.security"];
87 [ttr clearRetryTimestamp];
89 XCTAssertFalse([ttr isRateLimited], @"should not be rate-limited for first request");
90 [ttr updateRetryTimestamp];
91 XCTAssertTrue([ttr isRateLimited], @"should be rate-limited after first request");
93 [ttr clearRetryTimestamp];
94 XCTAssertFalse([ttr isRateLimited], @"should not be ratelitmied after clear");
96 // check invalid settings
97 [defaults setObject:@"invalid" forKey:key];
98 XCTAssertNotNil([defaults objectForKey:key], "should have cleared setting");
99 XCTAssertFalse([ttr isRateLimited], @"should not be rate-limited if invalid type");
100 XCTAssertNil([defaults objectForKey:key], "should have cleared setting");
108 #endif /* TARGET_OS_BRIDGE */