mirror of
https://github.com/apple/swift-crypto
synced 2026-11-29 05:14:21 +00:00
Use `FoundationEssentials` if available. <!-- Thanks for contributing to Swift Crypto! Before you submit your issue, please make sure you followed our checklist and check the appropriate boxes by putting an x in the [ ]: [x] --> ### Checklist - [x] I've run tests to see all new and existing tests pass (macOS and linux) - [x] I've followed the code style of the rest of the project - [x] I've read the [Contribution Guidelines](CONTRIBUTING.md) - [x] I've updated the documentation if necessary #### If you've made changes to `gyb` files - [ ] I've run `.script/generate_boilerplate_files_with_gyb` and included updated generated files in a commit of this pull request ``` bash .script/generate_boilerplate_files_with_gyb bash: .script/generate_boilerplate_files_with_gyb: No such file or directory ``` ### Motivation: Would be great that packages that depend on `swift-crypto` can be built without linking full `Foundation`. ### Modifications: This patch simply replaces `import Foundation` with a conditional `import FoundationEssentials`. In some files this also required to explicitly import the C standard lib. Maybe we could follow the same approach as in https://github.com/swiftlang/swift-tools-support-core/blob/main/Sources/TSCLibc/libc.swift and add an internal module that abstracts this import dance in a single place. ### Result: After this change the package should only depend on `FoundationEssentials`. --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Cory Benfield <lukasa@apple.com>
111 lines
4.2 KiB
Swift
111 lines
4.2 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftCrypto open source project
|
|
//
|
|
// Copyright (c) 2025 Apple Inc. and the SwiftCrypto project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
import Crypto
|
|
import XCTest
|
|
|
|
#if canImport(FoundationEssentials)
|
|
import FoundationEssentials
|
|
#else
|
|
import Foundation
|
|
#endif
|
|
|
|
#if !canImport(Darwin) || canImport(CryptoKit, _version: 241.100.42)
|
|
// Corresponds to the CryptoKit in XCode 16.3, which has Sendable annotations
|
|
import Crypto
|
|
@testable import CryptoExtras
|
|
#else
|
|
@preconcurrency import Crypto
|
|
@testable import CryptoExtras
|
|
#endif
|
|
|
|
final class ECToolboxBoringSSLTests: XCTestCase {
|
|
func testThreadLocalFFAC() async {
|
|
await testThreadLocalFFAC(P256.self)
|
|
await testThreadLocalFFAC(P384.self)
|
|
await testThreadLocalFFAC(P521.self)
|
|
}
|
|
|
|
func testThreadLocalFFAC(_ Curve: (some OpenSSLSupportedNISTCurve & Sendable).Type) async {
|
|
let numThreads = 3
|
|
let numReadsPerThread = 2
|
|
|
|
var threads:
|
|
[(
|
|
thread: Thread,
|
|
thisThreadDidReads: XCTestExpectation,
|
|
allThreadsDidReads: XCTestExpectation,
|
|
thisThreadFinished: XCTestExpectation
|
|
)] = []
|
|
|
|
let objectIdentifiers: LockedBox<[(threadID: Int, ffacID: ObjectIdentifier)]> = .init(initialValue: [])
|
|
|
|
for i in 1...numThreads {
|
|
let thisThreadDidReads = expectation(description: "this thread did its reads")
|
|
let allThreadsDidReads = expectation(description: "all threads did their reads")
|
|
let thisThreadFinished = expectation(description: "this thread is finished")
|
|
let thread = Thread {
|
|
for _ in 1...numReadsPerThread {
|
|
objectIdentifiers.withLockedValue {
|
|
$0.append((i, ObjectIdentifier(Curve.__ffac)))
|
|
}
|
|
}
|
|
thisThreadDidReads.fulfill()
|
|
XCTWaiter().wait(for: [allThreadsDidReads], timeout: .greatestFiniteMagnitude)
|
|
thisThreadFinished.fulfill()
|
|
}
|
|
thread.name = "thread-\(i)"
|
|
threads.append((thread, thisThreadDidReads, allThreadsDidReads, thisThreadFinished))
|
|
thread.start()
|
|
}
|
|
await fulfillment(of: threads.map(\.thisThreadDidReads), timeout: 0.5)
|
|
for thread in threads { thread.allThreadsDidReads.fulfill() }
|
|
await fulfillment(of: threads.map(\.thisThreadFinished), timeout: 0.5)
|
|
|
|
objectIdentifiers.withLockedValue { objectIdentifiers in
|
|
XCTAssertEqual(objectIdentifiers.count, numThreads * numReadsPerThread)
|
|
for threadID in 1...numThreads {
|
|
let partitionBoundary = objectIdentifiers.partition(by: { $0.threadID == threadID })
|
|
let otherThreadsObjIDs = objectIdentifiers[..<partitionBoundary].map(\.ffacID)
|
|
let thisThreadObjIDs = objectIdentifiers[partitionBoundary...].map(\.ffacID)
|
|
let intersection = Set(thisThreadObjIDs).intersection(Set(otherThreadsObjIDs))
|
|
XCTAssertEqual(
|
|
thisThreadObjIDs.count,
|
|
numReadsPerThread,
|
|
"Thread should read \(numReadsPerThread) times."
|
|
)
|
|
XCTAssertEqual(Set(thisThreadObjIDs).count, 1, "Thread should see same object on every read.")
|
|
XCTAssert(intersection.isEmpty, "Thread should see different objects from other threads.")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
final class LockedBox<Value: Sendable>: @unchecked Sendable {
|
|
private let lock: NSLock
|
|
private var value: Value
|
|
|
|
init(initialValue: Value) {
|
|
self.value = initialValue
|
|
self.lock = NSLock()
|
|
}
|
|
|
|
func withLockedValue<ReturnType>(_ body: (inout Value) throws -> ReturnType) rethrows -> ReturnType {
|
|
self.lock.lock()
|
|
defer {
|
|
self.lock.unlock()
|
|
}
|
|
return try body(&self.value)
|
|
}
|
|
}
|