Skip to content

Commit

Permalink
Tests: Convert more tests to Swift Testing
Browse files Browse the repository at this point in the history
Convert additional test from XCTest to Swift Testing to make use of
parallel execution and, in some cases, test parameterization.

Not all Test Suite in the respective folders have been converted as some
use helpers in swift-tools-core-support, which don't have a matching
swift testing helper.
  • Loading branch information
bkhouri committed Jan 15, 2025
1 parent 7c52d1c commit 5a67108
Show file tree
Hide file tree
Showing 7 changed files with 577 additions and 484 deletions.
248 changes: 157 additions & 91 deletions Tests/PackageSigningTests/FilePackageSigningEntityStorageTests.swift

Large diffs are not rendered by default.

85 changes: 35 additions & 50 deletions Tests/PackageSigningTests/SigningEntityTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import XCTest
import Foundation
import Testing

import Basics
@testable import PackageSigning
import _InternalTestSupport
import X509

final class SigningEntityTests: XCTestCase {
func testTwoADPSigningEntitiesAreEqualIfTeamIDEqual() {
struct SigningEntityTests {
@Test
func twoADPSigningEntitiesAreEqualIfTeamIDEqual() {
let adp1 = SigningEntity.recognized(
type: .adp,
name: "A. Appleseed",
Expand All @@ -37,74 +38,58 @@ final class SigningEntityTests: XCTestCase {
organizationalUnit: "SwiftPM Test Unit Y",
organization: "C"
)
XCTAssertEqual(adp1, adp2) // Only team ID (org unit) needs to match
XCTAssertNotEqual(adp1, adp3)
#expect(adp1 == adp2) // Only team ID (org unit) needs to match
#expect(adp1 != adp3)
}

func testFromECKeyCertificate() throws {
@Test(
"From certificate key",
arguments: [
(certificateFilename: "Test_ec.cer", id: "EC Key"),
(certificateFilename: "Test_rsa.cer", id: "RSA Key")
]
)
func fromCertificate(certificateFilename: String, id: String) throws {
try fixture(name: "Signing", createGitRepo: false) { fixturePath in
let certificateBytes = try readFileContents(
in: fixturePath,
pathComponents: "Certificates",
"Test_ec.cer"
certificateFilename
)
let certificate = try Certificate(certificateBytes)

let signingEntity = SigningEntity.from(certificate: certificate)
guard case .unrecognized(let name, let organizationalUnit, let organization) = signingEntity else {
return XCTFail("Expected SigningEntity.unrecognized but got \(signingEntity)")
Issue.record("Expected SigningEntity.unrecognized but got \(signingEntity)")
return
}
XCTAssertEqual(name, certificate.subject.commonName)
XCTAssertEqual(organizationalUnit, certificate.subject.organizationalUnitName)
XCTAssertEqual(organization, certificate.subject.organizationName)
#expect(name == certificate.subject.commonName)
#expect(organizationalUnit == certificate.subject.organizationalUnitName)
#expect(organization == certificate.subject.organizationName)
}
}

func testFromRSAKeyCertificate() throws {
try fixture(name: "Signing", createGitRepo: false) { fixturePath in
let certificateBytes = try readFileContents(
in: fixturePath,
pathComponents: "Certificates",
"Test_rsa.cer"
)
let certificate = try Certificate(certificateBytes)
@Test(
.enabled(if: isMacOS() && isRealSigningIdentityTestEnabled() && isEnvironmentVariableSet("REAL_SIGNING_IDENTITY_LABEL"))
)
func fromKeychainCertificate() async throws {
let label = try #require(Environment.current["REAL_SIGNING_IDENTITY_LABEL"])

let signingEntity = SigningEntity.from(certificate: certificate)
guard case .unrecognized(let name, let organizationalUnit, let organization) = signingEntity else {
return XCTFail("Expected SigningEntity.unrecognized but got \(signingEntity)")
}
XCTAssertEqual(name, certificate.subject.commonName)
XCTAssertEqual(organizationalUnit, certificate.subject.organizationalUnitName)
XCTAssertEqual(organization, certificate.subject.organizationName)
}
}

#if os(macOS)
func testFromKeychainCertificate() async throws {
#if ENABLE_REAL_SIGNING_IDENTITY_TEST
#else
try XCTSkipIf(true)
#endif

guard let label = Environment.current["REAL_SIGNING_IDENTITY_LABEL"] else {
throw XCTSkip("Skipping because 'REAL_SIGNING_IDENTITY_LABEL' env var is not set")
}
let identityStore = SigningIdentityStore(observabilityScope: ObservabilitySystem.NOOP)
let matches = identityStore.find(by: label)
XCTAssertTrue(!matches.isEmpty)
#expect(!matches.isEmpty)

let certificate = try Certificate(secIdentity: matches[0] as! SecIdentity)
let signingEntity = SigningEntity.from(certificate: certificate)
switch signingEntity {
case .recognized(_, let name, let organizationalUnit, let organization):
XCTAssertEqual(name, certificate.subject.commonName)
XCTAssertEqual(organizationalUnit, certificate.subject.organizationalUnitName)
XCTAssertEqual(organization, certificate.subject.organizationName)
case .unrecognized(let name, let organizationalUnit, let organization):
XCTAssertEqual(name, certificate.subject.commonName)
XCTAssertEqual(organizationalUnit, certificate.subject.organizationalUnitName)
XCTAssertEqual(organization, certificate.subject.organizationName)
case .recognized(_, let name, let organizationalUnit, let organization):
#expect(name == certificate.subject.commonName)
#expect(organizationalUnit == certificate.subject.organizationalUnitName)
#expect(organization == certificate.subject.organizationName)
case .unrecognized(let name, let organizationalUnit, let organization):
#expect(name == certificate.subject.commonName)
#expect(organizationalUnit == certificate.subject.organizationalUnitName)
#expect(organization == certificate.subject.organizationName)
}
}
#endif
}
50 changes: 24 additions & 26 deletions Tests/PackageSigningTests/SigningIdentityTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Foundation

import XCTest
import Testing

import _CryptoExtras // For RSA
import Basics
Expand All @@ -19,8 +20,9 @@ import Crypto
import _InternalTestSupport
import X509

final class SigningIdentityTests: XCTestCase {
func testSwiftSigningIdentityWithECKey() throws {
struct SigningIdentityTests {
@Test
func swiftSigningIdentityWithECKey() throws {
try fixture(name: "Signing", createGitRepo: false) { fixturePath in
let certificateBytes = try readFileContents(
in: fixturePath,
Expand All @@ -30,9 +32,9 @@ final class SigningIdentityTests: XCTestCase {
let certificate = try Certificate(certificateBytes)

let subject = certificate.subject
XCTAssertEqual("Test (EC) leaf", subject.commonName)
XCTAssertEqual("Test (EC) org unit", subject.organizationalUnitName)
XCTAssertEqual("Test (EC) org", subject.organizationName)
#expect("Test (EC) leaf" == subject.commonName)
#expect("Test (EC) org unit" == subject.organizationalUnitName)
#expect("Test (EC) org" == subject.organizationName)

let privateKeyBytes = try readFileContents(
in: fixturePath,
Expand All @@ -43,17 +45,19 @@ final class SigningIdentityTests: XCTestCase {
_ = SwiftSigningIdentity(certificate: certificate, privateKey: Certificate.PrivateKey(privateKey))

// Test public API
XCTAssertNoThrow(
#expect(throws: Never.self) {

try SwiftSigningIdentity(
derEncodedCertificate: certificateBytes,
derEncodedPrivateKey: privateKeyBytes,
privateKeyType: .p256
)
)
}
}
}

func testSwiftSigningIdentityWithRSAKey() throws {
@Test
func swiftSigningIdentityWithRSAKey() throws {
try fixture(name: "Signing", createGitRepo: false) { fixturePath in
let certificateBytes = try readFileContents(
in: fixturePath,
Expand All @@ -63,9 +67,9 @@ final class SigningIdentityTests: XCTestCase {
let certificate = try Certificate(certificateBytes)

let subject = certificate.subject
XCTAssertEqual("Test (RSA) leaf", subject.commonName)
XCTAssertEqual("Test (RSA) org unit", subject.organizationalUnitName)
XCTAssertEqual("Test (RSA) org", subject.organizationName)
#expect("Test (RSA) leaf" == subject.commonName)
#expect("Test (RSA) org unit" == subject.organizationalUnitName)
#expect("Test (RSA) org" == subject.organizationName)

let privateKeyBytes = try readFileContents(
in: fixturePath,
Expand All @@ -77,24 +81,18 @@ final class SigningIdentityTests: XCTestCase {
}
}

#if os(macOS)
@Test(
.enabled(if: isMacOS() && isRealSigningIdentityTestEnabled() && isEnvironmentVariableSet("REAL_SIGNING_IDENTITY_LABEL"))
)
func testSigningIdentityFromKeychain() async throws {
#if ENABLE_REAL_SIGNING_IDENTITY_TEST
#else
try XCTSkipIf(true)
#endif

guard let label = Environment.current["REAL_SIGNING_IDENTITY_LABEL"] else {
throw XCTSkip("Skipping because 'REAL_SIGNING_IDENTITY_LABEL' env var is not set")
}
let label = try #require(Environment.current["REAL_SIGNING_IDENTITY_LABEL"])
let identityStore = SigningIdentityStore(observabilityScope: ObservabilitySystem.NOOP)
let matches = identityStore.find(by: label)
XCTAssertTrue(!matches.isEmpty)
#expect(!matches.isEmpty)

let subject = try Certificate(secIdentity: matches[0] as! SecIdentity).subject
XCTAssertNotNil(subject.commonName)
XCTAssertNotNil(subject.organizationalUnitName)
XCTAssertNotNil(subject.organizationName)
#expect(subject.commonName != nil)
#expect(subject.organizationalUnitName != nil)
#expect(subject.organizationName != nil)
}
#endif
}
Loading

0 comments on commit 5a67108

Please sign in to comment.