]> git.saurik.com Git - apple/security.git/blob - OSX/utilities/test/SecTapToRadarTests.m
Security-59306.101.1.tar.gz
[apple/security.git] / OSX / utilities / test / SecTapToRadarTests.m
1 //
2 // SecTapToRadarTests.m
3 // TrustedPeersTests
4 //
5
6 #import <TargetConditionals.h>
7
8 #if !TARGET_OS_BRIDGE
9
10 #import <XCTest/XCTest.h>
11 #import <OCMock/OCMock.h>
12 #import "utilities/SecTapToRadar.h"
13
14 @interface SecTapToRadarTests : XCTestCase
15 @property BOOL isRateLimited;
16 @property BOOL userSay;
17
18 @property bool ttrDidAppear;
19 @property id mockTTR;
20
21 @end
22
23 @implementation SecTapToRadarTests
24
25 - (void)triggerTapToRadar:(SecTapToRadar *)ttrRequest
26 {
27 self.ttrDidAppear = true;
28 }
29
30 - (BOOL)isRateLimited:(SecTapToRadar *)ttrRequest
31 {
32 return self.isRateLimited;
33 }
34
35 - (BOOL)askUserIfTTR:(SecTapToRadar *)ttrRequest
36 {
37 return self.userSay;
38 }
39
40 - (void)setUp {
41
42 self.ttrDidAppear = NO;
43 self.isRateLimited = NO;
44 self.userSay = YES;
45
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:));
50 }
51
52 - (void)testSecTTRNormal {
53
54 SecTapToRadar *ttr = [[SecTapToRadar alloc] initTapToRadar:@"alert" description:@"test" radar:@"1"];
55
56 [ttr trigger];
57 XCTAssertTrue(self.ttrDidAppear, "should have appeared");
58 }
59
60 - (void)testSecTTRRateLimit {
61 SecTapToRadar *ttr = [[SecTapToRadar alloc] initTapToRadar:@"alert" description:@"test" radar:@"1"];
62
63 self.isRateLimited = YES;
64 [ttr trigger];
65 XCTAssertFalse(self.ttrDidAppear, "should not have appered");
66 }
67
68 - (void)testSecTTRUserSupress {
69
70 SecTapToRadar *ttr = [[SecTapToRadar alloc] initTapToRadar:@"alert" description:@"test" radar:@"1"];
71
72 self.userSay = false;
73 [ttr trigger];
74 XCTAssertFalse(self.ttrDidAppear, "should not have appered");
75
76 self.userSay = true;
77 [ttr trigger];
78 XCTAssertTrue(self.ttrDidAppear, "should have appeared");
79 }
80
81 - (void)testSecTTRRateLimiter {
82
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"];
86
87 [ttr clearRetryTimestamp];
88
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");
92
93 [ttr clearRetryTimestamp];
94 XCTAssertFalse([ttr isRateLimited], @"should not be ratelitmied after clear");
95
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");
101
102
103 }
104
105
106 @end
107
108 #endif /* TARGET_OS_BRIDGE */