2 * Copyright (c) 2016-2018 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
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
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)
38 * It is strongly recommended that all test dictionaries include the Anchors and VerifyDate keys.
39 * Addtional optional keys are defined below.
42 /* INSTRUCTIONS FOR DEBUGGING SUBTESTS:
43 * Add a debugging.plist to OSX/shared_regressions/si-20-sectrust-policies-data/ containing only those subtest dictionaries
47 #include <AssertMacros.h>
48 #import <XCTest/XCTest.h>
49 #import <Foundation/Foundation.h>
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>
58 #import "TrustEvaluationTestCase.h"
59 #include "../TestMacroConversions.h"
60 #include "../TrustEvaluationTestHelpers.h"
62 const NSString *kSecTrustTestPinningPolicyResources = @"si-20-sectrust-policies-data";
64 @interface PolicyTests : TrustEvaluationTestCase
67 @implementation PolicyTests
69 - (void)testPolicies {
70 NSURL *testPlist = nil;
71 NSArray *testsArray = nil;
73 testPlist = [[NSBundle bundleForClass:[self class]] URLForResource:@"debugging" withExtension:@"plist"
74 subdirectory:(NSString *)kSecTrustTestPinningPolicyResources];
76 testPlist = [[NSBundle bundleForClass:[self class]] URLForResource:nil withExtension:@"plist"
77 subdirectory:(NSString *)kSecTrustTestPinningPolicyResources ];
80 fail("Failed to get tests plist from %@", kSecTrustTestPinningPolicyResources);
84 testsArray = [NSArray arrayWithContentsOfURL: testPlist];
86 fail("Failed to create array from plist");
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);
94 NSError *testError = nil;
95 XCTAssert([testObj evaluateForExpectedResults:&testError], "Test %@ failed: %@", testObj.fullTestName, testError);