-
Notifications
You must be signed in to change notification settings - Fork 5
/
SerializedKeyValidatorTests.swift
56 lines (47 loc) · 2.29 KB
/
SerializedKeyValidatorTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import Foundation
import XCTest
import BIP32
final class SerializedKeyValidatorTests: XCTestCase {
private let serializedKeyCoder = SerializedKeyCoder()
private let jsonDecoder = JSONDecoder()
private var invalidTestVectors: [SerializedKeyValidatorTestVector]!
private var validPrivateMasterKeyTestVectors: [MasterKeyTestVector]!
private var validPublicMasterKeyTestVectors: [MasterKeyTestVector]!
override func setUpWithError() throws {
invalidTestVectors = try jsonDecoder.decode([SerializedKeyValidatorTestVector].self, from: serializedKeyValidatorTestData)
validPrivateMasterKeyTestVectors = try jsonDecoder.decode([MasterKeyTestVector].self, from: privateMasterKeyTestData)
validPublicMasterKeyTestVectors = try jsonDecoder.decode([MasterKeyTestVector].self, from: publicMasterKeyTestData)
}
private func sut() -> SerializedKeyValidator {
.init()
}
func testGivenVector_WithInvalidEncodedKey_WhenValidate_ThenThrowError() {
let sut = self.sut()
for testVector in invalidTestVectors {
let keyAccessControl: KeyAccessControl = testVector.isPrivate ? .`private` : .`public`
if let serializedKey = try? serializedKeyCoder.decode(string: testVector.base58CheckEncodedKey) {
XCTAssertThrowsError(
try sut.validate(serializedKey: serializedKey, keyAccessControl: keyAccessControl)
)
}
}
}
func testGivenValidEncodedPrivateKey_WhenValidate_ThenNoErrorThrown() throws {
let sut = self.sut()
for testVector in validPrivateMasterKeyTestVectors {
let serializedKey = try serializedKeyCoder.decode(string: testVector.base58CheckEncodedKey)
XCTAssertNoThrow(
try sut.validate(serializedKey: serializedKey, keyAccessControl: .`private`)
)
}
}
func testGivenValidEncodedPublicKey_WhenValidate_ThenNoErrorThrown() throws {
let sut = self.sut()
for testVector in validPublicMasterKeyTestVectors {
let serializedKey = try serializedKeyCoder.decode(string: testVector.base58CheckEncodedKey)
XCTAssertNoThrow(
try sut.validate(serializedKey: serializedKey, keyAccessControl: .`public`)
)
}
}
}