|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftCrypto open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the SwiftCrypto 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 SwiftCrypto project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | +import Crypto |
| 15 | +import Foundation |
| 16 | +import XCTest |
| 17 | + |
| 18 | +@testable import _CryptoExtras |
| 19 | + |
| 20 | +final class ECToolboxBoringSSLTests: XCTestCase { |
| 21 | + func testThreadLocalFFAC() async { |
| 22 | + await testThreadLocalFFAC(P256.self) |
| 23 | + await testThreadLocalFFAC(P384.self) |
| 24 | + await testThreadLocalFFAC(P521.self) |
| 25 | + } |
| 26 | + |
| 27 | + func testThreadLocalFFAC(_ Curve: (some OpenSSLSupportedNISTCurve & Sendable).Type) async { |
| 28 | + let numThreads = 3 |
| 29 | + let numReadsPerThread = 2 |
| 30 | + |
| 31 | + var threads: |
| 32 | + [( |
| 33 | + thread: Thread, |
| 34 | + thisThreadDidReads: XCTestExpectation, |
| 35 | + allThreadsDidReads: XCTestExpectation, |
| 36 | + thisThreadFinished: XCTestExpectation |
| 37 | + )] = [] |
| 38 | + |
| 39 | + var objectIdentifiers: [(threadID: Int, ffacID: ObjectIdentifier)] = [] |
| 40 | + let lock = NSLock() |
| 41 | + |
| 42 | + for i in 1...numThreads { |
| 43 | + let thisThreadDidReads = expectation(description: "this thread did its reads") |
| 44 | + let allThreadsDidReads = expectation(description: "all threads did their reads") |
| 45 | + let thisThreadFinished = expectation(description: "this thread is finished") |
| 46 | + let thread = Thread { |
| 47 | + for _ in 1...numReadsPerThread { |
| 48 | + lock.lock() |
| 49 | + objectIdentifiers.append((i, ObjectIdentifier(Curve.__ffac))) |
| 50 | + lock.unlock() |
| 51 | + } |
| 52 | + thisThreadDidReads.fulfill() |
| 53 | + XCTWaiter().wait(for: [allThreadsDidReads], timeout: .greatestFiniteMagnitude) |
| 54 | + thisThreadFinished.fulfill() |
| 55 | + } |
| 56 | + thread.name = "thread-\(i)" |
| 57 | + threads.append((thread, thisThreadDidReads, allThreadsDidReads, thisThreadFinished)) |
| 58 | + thread.start() |
| 59 | + } |
| 60 | + await fulfillment(of: threads.map(\.thisThreadDidReads), timeout: 0.5) |
| 61 | + for thread in threads { thread.allThreadsDidReads.fulfill() } |
| 62 | + await fulfillment(of: threads.map(\.thisThreadFinished), timeout: 0.5) |
| 63 | + |
| 64 | + XCTAssertEqual(objectIdentifiers.count, numThreads * numReadsPerThread) |
| 65 | + for threadID in 1...numThreads { |
| 66 | + let partitionBoundary = objectIdentifiers.partition(by: { $0.threadID == threadID }) |
| 67 | + let otherThreadsObjIDs = objectIdentifiers[..<partitionBoundary].map(\.ffacID) |
| 68 | + let thisThreadObjIDs = objectIdentifiers[partitionBoundary...].map(\.ffacID) |
| 69 | + let intersection = Set(thisThreadObjIDs).intersection(Set(otherThreadsObjIDs)) |
| 70 | + XCTAssertEqual(thisThreadObjIDs.count, numReadsPerThread, "Thread should read \(numReadsPerThread) times.") |
| 71 | + XCTAssertEqual(Set(thisThreadObjIDs).count, 1, "Thread should see same object on every read.") |
| 72 | + XCTAssert(intersection.isEmpty, "Thread should see different objects from other threads.") |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments