]> git.saurik.com Git - apple/security.git/blob - keychain/trust/TrustedPeersTests/TPHashTests.m
Security-58286.1.32.tar.gz
[apple/security.git] / keychain / trust / TrustedPeersTests / TPHashTests.m
1 /*
2 * Copyright (c) 2017 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 #import <XCTest/XCTest.h>
25 #import <TrustedPeers/TrustedPeers.h>
26
27 @interface TPHashTests : XCTestCase
28
29 @property (nonatomic, strong) NSData *hello;
30
31 @end
32
33 @implementation TPHashTests
34
35 - (void)setUp
36 {
37 self.hello = [@"hello" dataUsingEncoding:NSUTF8StringEncoding];
38 }
39
40 - (void)testSHA224
41 {
42 NSString *hash = [TPHashBuilder hashWithAlgo:kTPHashAlgoSHA224 ofData:self.hello];
43 XCTAssertEqualObjects(hash, @"SHA224:6gmunMZ2jFD87pA+0FRVblv8g0eQfxJZiqJBkw==");
44 TPHashAlgo algo = [TPHashBuilder algoOfHash:hash];
45 XCTAssertEqual(kTPHashAlgoSHA224, algo);
46 }
47
48 - (void)testSHA256
49 {
50 NSString *hash = [TPHashBuilder hashWithAlgo:kTPHashAlgoSHA256 ofData:self.hello];
51 XCTAssertEqualObjects(hash, @"SHA256:LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=");
52 TPHashAlgo algo = [TPHashBuilder algoOfHash:hash];
53 XCTAssertEqual(kTPHashAlgoSHA256, algo);
54 }
55
56 - (void)testSHA384
57 {
58 NSString *hash = [TPHashBuilder hashWithAlgo:kTPHashAlgoSHA384 ofData:self.hello];
59 XCTAssertEqualObjects(hash, @"SHA384:WeF0h3dEjGnea4ANejO7+5/xtGPkQ1TDVTvNucZm+pASWjx5+QOXvfX2oT3oKGhP");
60 TPHashAlgo algo = [TPHashBuilder algoOfHash:hash];
61 XCTAssertEqual(kTPHashAlgoSHA384, algo);
62 }
63
64 - (void)testSHA512
65 {
66 NSString *hash = [TPHashBuilder hashWithAlgo:kTPHashAlgoSHA512 ofData:self.hello];
67 XCTAssertEqualObjects(hash, @"SHA512:m3HSJL1i83hdltRq0+o9czGb+8KJDKra4t/3JRlnPKcjI8PZm6XBHXx6zG4UuMXaDEZjR1wuXDre9G9zvN7AQw==");
68 TPHashAlgo algo = [TPHashBuilder algoOfHash:hash];
69 XCTAssertEqual(kTPHashAlgoSHA512, algo);
70 }
71
72 - (void)testBadAlgo
73 {
74 XCTAssertEqual(kTPHashAlgoUnknown, [TPHashBuilder algoOfHash:@""]);
75 XCTAssertEqual(kTPHashAlgoUnknown, [TPHashBuilder algoOfHash:@"foo"]);
76 XCTAssertEqual(kTPHashAlgoUnknown, [TPHashBuilder algoOfHash:@"foo:"]);
77 XCTAssertEqual(kTPHashAlgoUnknown, [TPHashBuilder algoOfHash:@":"]);
78 XCTAssertEqual(kTPHashAlgoUnknown, [TPHashBuilder algoOfHash:@"foo:bar"]);
79 XCTAssertEqual(kTPHashAlgoUnknown, [TPHashBuilder algoOfHash:@"SHA256"]);
80
81 XCTAssertThrows([TPHashBuilder hashWithAlgo:kTPHashAlgoUnknown ofData:self.hello]);
82 }
83
84 - (void)testBadReuse
85 {
86 TPHashBuilder *builder = [[TPHashBuilder alloc] initWithAlgo:kTPHashAlgoSHA256];
87 [builder finalHash];
88 XCTAssertThrows([builder updateWithData:self.hello]);
89 XCTAssertThrows([builder finalHash]);
90 }
91
92 @end