Skip to content

Commit

Permalink
Finish Testing migration
Browse files Browse the repository at this point in the history
  • Loading branch information
maxgoedjen committed Jan 5, 2025
1 parent e332b7c commit 8e70754
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 298 deletions.
78 changes: 42 additions & 36 deletions Sources/Packages/Tests/BriefTests/ReleaseParsingTests.swift
Original file line number Diff line number Diff line change
@@ -1,54 +1,66 @@
import XCTest
import Testing
import Foundation
@testable import Brief

class ReleaseParsingTests: XCTestCase {

func testNonCritical() {
@Suite struct ReleaseParsingTests {

@Test
func nonCritical() {
let release = Release(name: "1.0.0", prerelease: false, html_url: URL(string: "https://example.com")!, body: "Initial release")
XCTAssert(release.critical == false)
#expect(release.critical == false)
}

func testCritical() {
@Test
func critical() {
let release = Release(name: "1.0.0", prerelease: false, html_url: URL(string: "https://example.com")!, body: "Critical Security Update")
XCTAssert(release.critical == true)
#expect(release.critical == true)
}

func testOSMissing() {
@Test
func osMissing() {
let release = Release(name: "1.0.0", prerelease: false, html_url: URL(string: "https://example.com")!, body: "Critical Security Update")
XCTAssert(release.minimumOSVersion == SemVer("11.0.0"))
#expect(release.minimumOSVersion == SemVer("11.0.0"))
}

func testOSPresentWithContentBelow() {
@Test
func osPresentWithContentBelow() {
let release = Release(name: "1.0.0", prerelease: false, html_url: URL(string: "https://example.com")!, body: "Critical Security Update ##Minimum macOS Version\n1.2.3\nBuild info")
XCTAssert(release.minimumOSVersion == SemVer("1.2.3"))
#expect(release.minimumOSVersion == SemVer("1.2.3"))
}

func testOSPresentAtEnd() {
@Test
func osPresentAtEnd() {
let release = Release(name: "1.0.0", prerelease: false, html_url: URL(string: "https://example.com")!, body: "Critical Security Update Minimum macOS Version: 1.2.3")
XCTAssert(release.minimumOSVersion == SemVer("1.2.3"))
#expect(release.minimumOSVersion == SemVer("1.2.3"))
}

func testOSWithMacOSPrefix() {
@Test
func osWithMacOSPrefix() {
let release = Release(name: "1.0.0", prerelease: false, html_url: URL(string: "https://example.com")!, body: "Critical Security Update Minimum macOS Version: macOS 1.2.3")
XCTAssert(release.minimumOSVersion == SemVer("1.2.3"))
#expect(release.minimumOSVersion == SemVer("1.2.3"))
}

func testOSGreaterThanMinimum() {
@Test
func osGreaterThanMinimum() {
let release = Release(name: "1.0.0", prerelease: false, html_url: URL(string: "https://example.com")!, body: "Critical Security Update Minimum macOS Version: 1.2.3")
XCTAssert(release.minimumOSVersion < SemVer("11.0.0"))
#expect(release.minimumOSVersion < SemVer("11.0.0"))
}

func testOSEqualToMinimum() {
@Test
func osEqualToMinimum() {
let release = Release(name: "1.0.0", prerelease: false, html_url: URL(string: "https://example.com")!, body: "Critical Security Update Minimum macOS Version: 11.2.3")
XCTAssert(release.minimumOSVersion <= SemVer("11.2.3"))
#expect(release.minimumOSVersion <= SemVer("11.2.3"))
}

func testOSLessThanMinimum() {
@Test
func osLessThanMinimum() {
let release = Release(name: "1.0.0", prerelease: false, html_url: URL(string: "https://example.com")!, body: "Critical Security Update Minimum macOS Version: 1.2.3")
XCTAssert(release.minimumOSVersion > SemVer("1.0.0"))
#expect(release.minimumOSVersion > SemVer("1.0.0"))
}

func testGreatestSelectedIfOldPatchIsPublishedLater() {
@Test
func greatestSelectedIfOldPatchIsPublishedLater() async throws {
// If 2.x.x series has been published, and a patch for 1.x.x is issued
// 2.x.x should still be selected if user can run it.
let updater = Updater(checkOnLaunch: false, osVersion: SemVer("2.2.3"), currentVersion: SemVer("1.0.0"))
Expand All @@ -60,16 +72,13 @@ class ReleaseParsingTests: XCTestCase {
Release(name: "1.0.2", prerelease: false, html_url: URL(string: "https://example.com")!, body: "Emergency patch! Minimum macOS Version: 1.2.3"),
]

let expectation = XCTestExpectation()
updater.evaluate(releases: releases)
DispatchQueue.main.async {
XCTAssert(updater.update == two)
expectation.fulfill()
}
wait(for: [expectation], timeout: 1)
try await Task.sleep(nanoseconds: 500)
#expect(updater.update == two)
}

func testLatestVersionIsRunnable() {
@Test
func latestVersionIsRunnable() async throws {
// If the 2.x.x series has been published but the user can't run it
// the last version the user can run should be selected.
let updater = Updater(checkOnLaunch: false, osVersion: SemVer("1.2.3"), currentVersion: SemVer("1.0.0"))
Expand All @@ -80,16 +89,13 @@ class ReleaseParsingTests: XCTestCase {
Release(name: "2.0.0", prerelease: false, html_url: URL(string: "https://example.com")!, body: "2.0 available! Minimum macOS Version: 2.2.3"),
Release(name: "1.0.2", prerelease: false, html_url: URL(string: "https://example.com")!, body: "Emergency patch! Minimum macOS Version: 1.2.3"),
]
let expectation = XCTestExpectation()
updater.evaluate(releases: releases)
DispatchQueue.main.async {
XCTAssert(updater.update == oneOhTwo)
expectation.fulfill()
}
wait(for: [expectation], timeout: 1)
try await Task.sleep(nanoseconds: 500)
#expect(updater.update == oneOhTwo)
}

func testSorting() {
@Test
func sorting() {
let two = Release(name: "2.0.0", prerelease: false, html_url: URL(string: "https://example.com")!, body: "2.0 available!")
let releases = [
Release(name: "1.0.0", prerelease: false, html_url: URL(string: "https://example.com")!, body: "Initial release"),
Expand All @@ -98,7 +104,7 @@ class ReleaseParsingTests: XCTestCase {
Release(name: "1.0.2", prerelease: false, html_url: URL(string: "https://example.com")!, body: "Emergency patch!"),
]
let sorted = releases.sorted().reversed().first
XCTAssert(sorted == two)
#expect(sorted == two)
}

}
37 changes: 19 additions & 18 deletions Sources/Packages/Tests/BriefTests/SemVerTests.swift
Original file line number Diff line number Diff line change
@@ -1,51 +1,52 @@
import XCTest
import Testing
import Foundation
@testable import Brief

class SemVerTests: XCTestCase {
@Suite struct SemVerTests {

func testEqual() {
@Test func equal() {
let current = SemVer("1.0.2")
let old = SemVer("1.0.2")
XCTAssert(!(current > old))
#expect(!(current > old))
}

func testPatchGreaterButMinorLess() {
@Test func patchGreaterButMinorLess() {
let current = SemVer("1.1.0")
let old = SemVer("1.0.2")
XCTAssert(current > old)
#expect(current > old)
}

func testMajorSameMinorGreater() {
@Test func majorSameMinorGreater() {
let current = SemVer("1.0.2")
let new = SemVer("1.0.3")
XCTAssert(current < new)
#expect(current < new)
}

func testMajorGreaterMinorLesser() {
@Test func majorGreaterMinorLesser() {
let current = SemVer("1.0.2")
let new = SemVer("2.0.0")
XCTAssert(current < new)
#expect(current < new)
}

func testRegularParsing() {
@Test func regularParsing() {
let current = SemVer("1.0.2")
XCTAssert(current.versionNumbers == [1, 0, 2])
#expect(current.versionNumbers == [1, 0, 2])
}

func testNoPatch() {
@Test func noPatch() {
let current = SemVer("1.1")
XCTAssert(current.versionNumbers == [1, 1, 0])
#expect(current.versionNumbers == [1, 1, 0])
}

func testGarbage() {
@Test func garbage() {
let current = SemVer("Test")
XCTAssert(current.versionNumbers == [0, 0, 0])
#expect(current.versionNumbers == [0, 0, 0])
}

func testBeta() {
@Test func beta() {
let current = SemVer("1.0.2")
let new = SemVer("1.1.0_beta1")
XCTAssert(current < new)
#expect(current < new)
}

}
54 changes: 27 additions & 27 deletions Sources/Packages/Tests/SecretAgentKitTests/AgentTests.swift
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
import Foundation
import XCTest
import Testing
import CryptoKit
@testable import SecretKit
@testable import SecretAgentKit

class AgentTests: XCTestCase {
@Suite struct AgentTests {

let stubWriter = StubFileHandleWriter()

// MARK: Identity Listing

func testEmptyStores() async {
@Test func emptyStores() async {
let stubReader = StubFileHandleReader(availableData: Constants.Requests.requestIdentities)
let agent = Agent(storeList: SecretStoreList())
await agent.handle(reader: stubReader, writer: stubWriter)
XCTAssertEqual(stubWriter.data, Constants.Responses.requestIdentitiesEmpty)
#expect(stubWriter.data == Constants.Responses.requestIdentitiesEmpty)
}

func testIdentitiesList() async {
@Test func identitiesList() async {
let stubReader = StubFileHandleReader(availableData: Constants.Requests.requestIdentities)
let list = storeList(with: [Constants.Secrets.ecdsa256Secret, Constants.Secrets.ecdsa384Secret])
let agent = Agent(storeList: list)
await agent.handle(reader: stubReader, writer: stubWriter)
XCTAssertEqual(stubWriter.data, Constants.Responses.requestIdentitiesMultiple)
#expect(stubWriter.data == Constants.Responses.requestIdentitiesMultiple)
}

// MARK: Signatures

func testNoMatchingIdentities() async {
@Test func noMatchingIdentities() async {
let stubReader = StubFileHandleReader(availableData: Constants.Requests.requestSignatureWithNoneMatching)
let list = storeList(with: [Constants.Secrets.ecdsa256Secret, Constants.Secrets.ecdsa384Secret])
let agent = Agent(storeList: list)
await agent.handle(reader: stubReader, writer: stubWriter)
// XCTAssertEqual(stubWriter.data, Constants.Responses.requestFailure)
#expect(stubWriter.data == Constants.Responses.requestFailure)
}

func testSignature() async throws {
@Test func signature() async throws {
let stubReader = StubFileHandleReader(availableData: Constants.Requests.requestSignature)
let requestReader = OpenSSHReader(data: Constants.Requests.requestSignature[5...])
_ = requestReader.readNextChunk()
Expand Down Expand Up @@ -67,27 +67,27 @@ class AgentTests: XCTestCase {
let invalidRandomSignature = try await store.verify(signature: "invalid".data(using: .utf8)!, for: dataToSign, with: AnySecret(Constants.Secrets.ecdsa256Secret))
let invalidRandomData = try await store.verify(signature: signature.derRepresentation, for: "invalid".data(using: .utf8)!, with: AnySecret(Constants.Secrets.ecdsa256Secret))
let invalidWrongKey = try await store.verify(signature: signature.derRepresentation, for: dataToSign, with: AnySecret(Constants.Secrets.ecdsa384Secret))
XCTAssertTrue(referenceValid)
XCTAssertTrue(derVerifies)
XCTAssert(invalidRandomSignature == false)
XCTAssert(invalidRandomData == false)
XCTAssert(invalidWrongKey == false)
#expect(referenceValid)
#expect(derVerifies)
#expect(invalidRandomSignature == false)
#expect(invalidRandomData == false)
#expect(invalidWrongKey == false)
}

// MARK: Witness protocol

func testWitnessObjectionStopsRequest() async {
@Test func witnessObjectionStopsRequest() async {
let stubReader = StubFileHandleReader(availableData: Constants.Requests.requestSignature)
let list = storeList(with: [Constants.Secrets.ecdsa256Secret])
let witness = StubWitness(speakNow: { _,_ in
return true
}, witness: { _, _ in })
let agent = Agent(storeList: list, witness: witness)
await agent.handle(reader: stubReader, writer: stubWriter)
XCTAssertEqual(stubWriter.data, Constants.Responses.requestFailure)
#expect(stubWriter.data == Constants.Responses.requestFailure)
}

func testWitnessSignature() async {
@Test func witnessSignature() async {
let stubReader = StubFileHandleReader(availableData: Constants.Requests.requestSignature)
let list = storeList(with: [Constants.Secrets.ecdsa256Secret])
var witnessed = false
Expand All @@ -98,10 +98,10 @@ class AgentTests: XCTestCase {
})
let agent = Agent(storeList: list, witness: witness)
await agent.handle(reader: stubReader, writer: stubWriter)
XCTAssertTrue(witnessed)
#expect(witnessed)
}

func testRequestTracing() async {
@Test func requestTracing() async {
let stubReader = StubFileHandleReader(availableData: Constants.Requests.requestSignature)
let list = storeList(with: [Constants.Secrets.ecdsa256Secret])
var speakNowTrace: SigningRequestProvenance! = nil
Expand All @@ -114,31 +114,31 @@ class AgentTests: XCTestCase {
})
let agent = Agent(storeList: list, witness: witness)
await agent.handle(reader: stubReader, writer: stubWriter)
XCTAssertEqual(witnessTrace, speakNowTrace)
XCTAssertEqual(witnessTrace.origin.displayName, "Finder")
XCTAssertEqual(witnessTrace.origin.validSignature, true)
XCTAssertEqual(witnessTrace.origin.parentPID, 1)
#expect(witnessTrace == speakNowTrace)
#expect(witnessTrace.origin.displayName == "Finder")
#expect(witnessTrace.origin.validSignature == true)
#expect(witnessTrace.origin.parentPID == 1)
}

// MARK: Exception Handling

func testSignatureException() async {
@Test func signatureException() async {
let stubReader = StubFileHandleReader(availableData: Constants.Requests.requestSignature)
let list = storeList(with: [Constants.Secrets.ecdsa256Secret, Constants.Secrets.ecdsa384Secret])
let store = list.stores.first?.base as! Stub.Store
store.shouldThrow = true
let agent = Agent(storeList: list)
await agent.handle(reader: stubReader, writer: stubWriter)
XCTAssertEqual(stubWriter.data, Constants.Responses.requestFailure)
#expect(stubWriter.data == Constants.Responses.requestFailure)
}

// MARK: Unsupported

func testUnhandledAdd() async {
@Test func unhandledAdd() async {
let stubReader = StubFileHandleReader(availableData: Constants.Requests.addIdentity)
let agent = Agent(storeList: SecretStoreList())
await agent.handle(reader: stubReader, writer: stubWriter)
XCTAssertEqual(stubWriter.data, Constants.Responses.requestFailure)
#expect(stubWriter.data == Constants.Responses.requestFailure)
}

}
Expand Down
16 changes: 8 additions & 8 deletions Sources/Packages/Tests/SecretKitTests/AnySecretTests.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import Foundation
import XCTest
import Testing
@testable import SecretKit
@testable import SecureEnclaveSecretKit
@testable import SmartCardSecretKit

class AnySecretTests: XCTestCase {
@Suite struct AnySecretTests {

func testEraser() {
@Test func eraser() {
let secret = SmartCard.Secret(id: UUID().uuidString.data(using: .utf8)!, name: "Name", algorithm: .ellipticCurve, keySize: 256, publicKey: UUID().uuidString.data(using: .utf8)!)
let erased = AnySecret(secret)
XCTAssert(erased.id == secret.id as AnyHashable)
XCTAssert(erased.name == secret.name)
XCTAssert(erased.algorithm == secret.algorithm)
XCTAssert(erased.keySize == secret.keySize)
XCTAssert(erased.publicKey == secret.publicKey)
#expect(erased.id == secret.id as AnyHashable)
#expect(erased.name == secret.name)
#expect(erased.algorithm == secret.algorithm)
#expect(erased.keySize == secret.keySize)
#expect(erased.publicKey == secret.publicKey)
}

}
Loading

0 comments on commit 8e70754

Please sign in to comment.