]> git.saurik.com Git - apple/security.git/blob - OSX/utilities/test/SecXPCHelperTests.m
Security-59306.11.20.tar.gz
[apple/security.git] / OSX / utilities / test / SecXPCHelperTests.m
1 //
2 // SecXPCHelperTests.m
3 // SecurityUtilityTests
4 //
5
6 #import <XCTest/XCTest.h>
7 #import "utilities/SecXPCHelper.h"
8
9 @interface UnsafeType : NSObject
10 @property(class, readonly) BOOL supportsSecureCoding;
11 - (id)initWithCoder:(NSCoder *)decoder;
12 @end
13
14 @implementation UnsafeType
15
16 - (id)init {
17 return [super init];
18 }
19
20 + (BOOL)supportsSecureCoding {
21 return NO;
22 }
23
24 - (id)initWithCoder:(NSCoder *)decoder {
25 return [[UnsafeType alloc] init];
26 }
27
28 @end
29
30 @interface SafeType : NSObject
31 @property(class, readonly) BOOL supportsSecureCoding;
32 - (id)initWithCoder:(NSCoder *)decoder;
33 @end
34
35 @implementation SafeType
36
37 - (id)init {
38 return [super init];
39 }
40
41 + (BOOL)supportsSecureCoding {
42 return YES;
43 }
44
45 - (id)initWithCoder:(NSCoder *)decoder {
46 return [[SafeType alloc] init];
47 }
48
49 @end
50
51 @interface SecXPCHelperTests : XCTestCase
52 @end
53
54 @implementation SecXPCHelperTests
55
56 - (void)testSanitizerWithNilError {
57 XCTAssertNil([SecXPCHelper cleanseErrorForXPC:nil]);
58 }
59
60 - (void)testSanitizerWithCleanError {
61 NSDictionary *cleanInfo = @{@"Key" : @"Safe String"};
62 NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:-1 userInfo:cleanInfo];
63 NSError *cleansed = [SecXPCHelper cleanseErrorForXPC:error];
64 XCTAssertNotNil(cleansed);
65 XCTAssertTrue(cleansed.code == error.code);
66 XCTAssertTrue(cleansed.domain == error.domain);
67 XCTAssertTrue([cleansed.userInfo isEqualToDictionary:cleanInfo]);
68 }
69
70 - (void)testSanitizerWithSafeCodingError {
71 SafeType *safe = [[SafeType alloc] init];
72 NSDictionary *cleanInfo = @{@"Key" : safe};
73 NSDictionary *sanitizedInfo = @{@"Key" : [[safe class] description]};
74 NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:-1 userInfo:cleanInfo];
75 NSError *cleansed = [SecXPCHelper cleanseErrorForXPC:error];
76 XCTAssertNotNil(cleansed);
77 XCTAssertTrue(cleansed.code == error.code);
78 XCTAssertTrue(cleansed.domain == error.domain);
79 XCTAssertTrue([cleansed.userInfo isEqualToDictionary:sanitizedInfo]);
80 }
81
82 - (void)testSanitizerWithDirtyUnsafeError {
83 UnsafeType *unsafe = [[UnsafeType alloc] init];
84 NSDictionary *cleanInfo = @{@"Key" : unsafe};
85 NSDictionary *sanitizedInfo = @{@"Key" : [[unsafe class] description]};
86 NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:-1 userInfo:cleanInfo];
87 NSError *cleansed = [SecXPCHelper cleanseErrorForXPC:error];
88 XCTAssertNotNil(cleansed);
89 XCTAssertTrue(cleansed.code == error.code);
90 XCTAssertTrue(cleansed.domain == error.domain);
91 XCTAssertTrue([cleansed.userInfo isEqualToDictionary:sanitizedInfo]);
92 }
93
94 - (void)testErrorEncodingUnsafe {
95 NSDictionary *unsafeInfo = @{ @"info" : [[NSObject alloc] init] };
96 NSError *unsafeError = [NSError errorWithDomain:@"domain" code:23 userInfo:unsafeInfo];
97 NSData *unsafeEncodedData = nil;
98 bool exceptionCaught = false;
99 @try {
100 unsafeEncodedData = [SecXPCHelper encodedDataFromError:unsafeError];
101 } @catch (NSException *e) {
102 XCTAssertNotNil(e);
103 XCTAssertEqualObjects(e.name, NSInvalidUnarchiveOperationException);
104 exceptionCaught = true;
105 }
106 XCTAssertTrue(exceptionCaught);
107 XCTAssertNil(unsafeEncodedData);
108 }
109
110 - (void)testErrorEncodingSafe {
111 NSDictionary *safeInfo = @{ @"info" : @(57) };
112 NSError *safeError = [NSError errorWithDomain:@"domain" code:19 userInfo:safeInfo];
113 NSData *safeEncodedData = [SecXPCHelper encodedDataFromError:safeError];
114 XCTAssertNotNil(safeEncodedData);
115 NSError *decodedSafeError = [SecXPCHelper errorFromEncodedData:safeEncodedData];
116 XCTAssertNotNil(decodedSafeError);
117 XCTAssertEqualObjects(decodedSafeError.domain, safeError.domain);
118 XCTAssertEqual(decodedSafeError.code, safeError.code);
119 XCTAssertEqualObjects(decodedSafeError.userInfo, safeError.userInfo);
120
121 /* Double-check the archive key */
122 NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:safeEncodedData error:NULL];
123 XCTAssertNotNil(unarchiver);
124 XCTAssertTrue([unarchiver containsValueForKey:@"error"], "missing expected key (this is a wire format!)");
125 }
126
127 @end