Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: Convert more tests to Swift Testing #8100

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Sources/_InternalTestSupport/PlatformHelpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Basics

public func isMacOs() -> Bool {
#if os(macOS)
return true
#else
return false
#endif
}

public func isRealSigningIdentityTestEnabled() -> Bool {
#if ENABLE_REAL_SIGNING_IDENTITY_TEST
return true
#else
return false
#endif
}

public func isEnvironmentVariableSet(_ variableName: EnvironmentKey) -> Bool {
guard let value = Environment.current[variableName] else {return false}
return !value.isEmpty
}
87 changes: 37 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,60 @@ 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(
.disabled(if: !isMacOs(), "Test is only supported on MacOS"),
.disabled(if: !isRealSigningIdentityTestEnabled(), "Test only enabled during real signing indentity"),
.disabled(if: !isEnvironmentVariableSet("REAL_SIGNING_IDENTITY_LABEL"), "'REAL_SIGNING_IDENTITY_LABEL' env var is not set")
)
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
}
52 changes: 26 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just throw.


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,20 @@ final class SigningIdentityTests: XCTestCase {
}
}

#if os(macOS)
@Test(
.disabled(if: !isMacOs(), "Test is only supported on MacOS"),
.disabled(if: !isRealSigningIdentityTestEnabled(), "Test only enabled during real signing indentity"),
.disabled(if: !isEnvironmentVariableSet("REAL_SIGNING_IDENTITY_LABEL"), "'REAL_SIGNING_IDENTITY_LABEL' env var is not set")
)
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