2 * Copyright (c) 2017 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@
24 #import <XCTest/XCTest.h>
25 #import <TrustedPeers/TrustedPeers.h>
27 @interface TPHashTests : XCTestCase
29 @property (nonatomic, strong) NSData *hello;
33 @implementation TPHashTests
37 self.hello = [@"hello" dataUsingEncoding:NSUTF8StringEncoding];
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);
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);
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);
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);
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"]);
81 XCTAssertThrows([TPHashBuilder hashWithAlgo:kTPHashAlgoUnknown ofData:self.hello]);
86 TPHashBuilder *builder = [[TPHashBuilder alloc] initWithAlgo:kTPHashAlgoSHA256];
88 XCTAssertThrows([builder updateWithData:self.hello]);
89 XCTAssertThrows([builder finalHash]);