Skip to content

Commit 64c9352

Browse files
committed
CustomPrivateKeyTests
1 parent ee0f956 commit 64c9352

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftCertificates open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the SwiftCertificates project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftCertificates project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import CryptoKit
16+
import SwiftASN1
17+
import X509
18+
import XCTest
19+
20+
final class CustomPrivateKeyTests: XCTestCase {
21+
22+
func testCustomPrivateKeyBackingProperties() {
23+
let keyBacking = TestAsyncKey()
24+
let privateKey = Certificate.PrivateKey(keyBacking)
25+
XCTAssertEqual(privateKey.publicKey, keyBacking.publicKey)
26+
XCTAssertEqual(privateKey.description, "CustomPrivateKey")
27+
XCTAssertEqual(keyBacking.hashValue, privateKey.hashValue)
28+
}
29+
30+
func testCustomPrivateKeySigning() async throws {
31+
let privateKey = Certificate.PrivateKey(TestAsyncKey())
32+
33+
_ = try await privateKey.signAsynchronously(
34+
bytes: Data(),
35+
signatureAlgorithm: .ecdsaWithSHA256
36+
)
37+
XCTAssertThrowsError(
38+
try privateKey.sign(
39+
bytes: Data(),
40+
signatureAlgorithm: .ecdsaWithSHA256
41+
)
42+
)
43+
}
44+
45+
func testCustomPrivateKeyBackingEquality() {
46+
let keyBacking = TestAsyncKey()
47+
let leftKey = Certificate.PrivateKey(keyBacking)
48+
let rightKey = Certificate.PrivateKey(keyBacking)
49+
XCTAssertEqual(leftKey, rightKey)
50+
}
51+
52+
func testCustomPrivateKeySerialization() {
53+
let privateKey = Certificate.PrivateKey(TestAsyncKey())
54+
XCTAssertThrowsError(try privateKey.serializeAsPEM())
55+
}
56+
57+
}
58+
59+
/// A theoretical private key which only supports asynchronous signing.
60+
private struct TestAsyncKey: CustomPrivateKey {
61+
62+
var publicKey: Certificate.PublicKey { privateKey.publicKey }
63+
64+
// Not required for CustomPrivateKey protocol.
65+
private let privateKey = Certificate.PrivateKey(P256.Signing.PrivateKey())
66+
67+
let defaultSignatureAlgorithm: Certificate.SignatureAlgorithm = .sha256WithRSAEncryption
68+
69+
func signSynchronously(
70+
bytes: some DataProtocol,
71+
signatureAlgorithm: Certificate.SignatureAlgorithm
72+
) throws -> Certificate.Signature {
73+
throw MyError()
74+
}
75+
76+
func signAsynchronously(
77+
bytes: some DataProtocol & Sendable,
78+
signatureAlgorithm: Certificate.SignatureAlgorithm
79+
) async throws -> Certificate.Signature {
80+
try await Task {
81+
try privateKey.sign(bytes: bytes, signatureAlgorithm: signatureAlgorithm)
82+
}
83+
.value
84+
}
85+
86+
static let defaultPEMDiscriminator: String = "TestKey"
87+
88+
func serializeAsPEM(discriminator: String) throws -> PEMDocument {
89+
throw MyError()
90+
}
91+
92+
func serialize(into coder: inout DER.Serializer) throws {
93+
throw MyError()
94+
}
95+
96+
struct MyError: Error {}
97+
98+
}

0 commit comments

Comments
 (0)