]> git.saurik.com Git - apple/security.git/blob - tests/TrustTests/EvaluationTests/PolicyTests.m
Security-59306.11.20.tar.gz
[apple/security.git] / tests / TrustTests / EvaluationTests / PolicyTests.m
1 /*
2 * Copyright (c) 2016-2018 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
25 /* INSTRUCTIONS FOR ADDING NEW SUBTESTS:
26 * 1. Add the certificates, as DER-encoded files with the 'cer' extension, to OSX/shared_regressions/si-20-sectrust-policies-data/
27 * NOTE: If your cert needs to be named with "(i[Pp]hone|i[Pp]ad|i[Pp]od)", you need to make two copies -- one named properly
28 * and another named such that it doesn't match that regex. Use the regex trick below for TARGET_OS_TV to make sure your test
29 * works.
30 * 2. Add a new dictionary to the test plist (OSX/shared_regressions/si-20-sectrust-policies-data/PinningPolicyTrustTest.plist).
31 * This dictionary must include: (see constants below)
32 * MajorTestName
33 * MinorTestName
34 * Policies
35 * Leaf
36 * Intermediates
37 * ExpectedResult
38 * It is strongly recommended that all test dictionaries include the Anchors and VerifyDate keys.
39 * Addtional optional keys are defined below.
40 */
41
42 /* INSTRUCTIONS FOR DEBUGGING SUBTESTS:
43 * Add a debugging.plist to OSX/shared_regressions/si-20-sectrust-policies-data/ containing only those subtest dictionaries
44 * you want to debug.
45 */
46
47 #include <AssertMacros.h>
48 #import <XCTest/XCTest.h>
49 #import <Foundation/Foundation.h>
50
51 #include <utilities/SecInternalReleasePriv.h>
52 #include <utilities/SecCFRelease.h>
53 #include <Security/SecCertificate.h>
54 #include <Security/SecCertificatePriv.h>
55 #include <Security/SecPolicyPriv.h>
56 #include <Security/SecTrust.h>
57
58 #import "TrustEvaluationTestCase.h"
59 #include "../TestMacroConversions.h"
60 #include "../TrustEvaluationTestHelpers.h"
61
62 const NSString *kSecTrustTestPinningPolicyResources = @"si-20-sectrust-policies-data";
63
64 @interface PolicyTests : TrustEvaluationTestCase
65 @end
66
67 @implementation PolicyTests
68
69 - (void)testPolicies {
70 NSURL *testPlist = nil;
71 NSArray *testsArray = nil;
72
73 testPlist = [[NSBundle bundleForClass:[self class]] URLForResource:@"debugging" withExtension:@"plist"
74 subdirectory:(NSString *)kSecTrustTestPinningPolicyResources];
75 if (!testPlist) {
76 testPlist = [[NSBundle bundleForClass:[self class]] URLForResource:nil withExtension:@"plist"
77 subdirectory:(NSString *)kSecTrustTestPinningPolicyResources ];
78 }
79 if (!testPlist) {
80 fail("Failed to get tests plist from %@", kSecTrustTestPinningPolicyResources);
81 return;
82 }
83
84 testsArray = [NSArray arrayWithContentsOfURL: testPlist];
85 if (!testsArray) {
86 fail("Failed to create array from plist");
87 return;
88 }
89
90 [testsArray enumerateObjectsUsingBlock:^(NSDictionary *testDict, NSUInteger idx, BOOL * _Nonnull stop) {
91 TestTrustEvaluation *testObj = [[TestTrustEvaluation alloc] initWithTrustDictionary:testDict];
92 XCTAssertNotNil(testObj, "failed to create test object for %lu", (unsigned long)idx);
93
94 NSError *testError = nil;
95 XCTAssert([testObj evaluateForExpectedResults:&testError], "Test %@ failed: %@", testObj.fullTestName, testError);
96 }];
97 }
98
99 @end