SPB Git

Repository Public

TestSupport.swift 3.3 KB 79 lines swift

1import Foundation2import XCTest3@testable import SharedVaultKit45enum Paths {6    /// apple/SharedVaultKit/Tests/SharedVaultKitTests7    static let testsDir = URL(fileURLWithPath: #filePath).deletingLastPathComponent()8    static let packageDir = testsDir.deletingLastPathComponent().deletingLastPathComponent()9    static let repoRoot = packageDir.deletingLastPathComponent().deletingLastPathComponent()10    static let vectorsDir = repoRoot.appendingPathComponent("packages/crypto-test-vectors")11    static let fixtures = testsDir.appendingPathComponent("Fixtures")12}1314func loadJSON(_ url: URL) throws -> JSONValue { try JSONValue.parse(try Data(contentsOf: url)) }1516/// Deterministic byte stream shared with Tests/Tools/generate-parity.ts (xorshift32).17final class XorShift {18    private var s: UInt3219    private(set) var calls = 020    init(seed: UInt32) { s = seed }21    func bytes(_ n: Int) -> Data {22        calls += 123        var out = Data(capacity: n)24        for _ in 0..<n { s ^= s << 13; s ^= s >> 17; s ^= s << 5; out.append(UInt8(s & 0xff)) }25        return out26    }27}2829/// Replays a fixed list of 4-byte values, then fails.30final class ScriptedRandom {31    private var queue: [UInt32]32    private(set) var consumed = 033    init(_ values: [UInt32]) { queue = values }34    func bytes(_ n: Int) -> Data {35        XCTAssertEqual(n, 4)36        guard consumed < queue.count else { XCTFail("random source exhausted"); return Data(count: n) }37        let v = queue[consumed]38        consumed += 139        return Data([UInt8(v >> 24), UInt8((v >> 16) & 255), UInt8((v >> 8) & 255), UInt8(v & 255)])40    }41}4243func assertThrows<T>(_ expected: VaultError, _ expression: @autoclosure () throws -> T, _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) {44    XCTAssertThrowsError(try expression(), message(), file: file, line: line) { error in45        XCTAssertEqual(error as? VaultError, expected, message(), file: file, line: line)46    }47}4849/// b(n, seed) from packages/crypto-test-vectors/generate.ts50func pattern(_ n: Int, _ seed: Int) -> Data { Data((0..<n).map { UInt8(($0 * 7 + seed) & 0xff) }) }5152extension JSONValue {53    func step(_ p: String) -> JSONValue? { if arrayValue != nil, let i = Int(p) { return self[i] }; return self[p] }54    /// Non-throwing accessor for assertion messages.55    func text(_ path: String...) -> String {56        var v: JSONValue? = self57        for p in path { v = v?.step(p) }58        return v?.stringValue ?? v?.serializedString() ?? ""59    }60    func str(_ path: String...) throws -> String {61        var v: JSONValue? = self62        for p in path { v = v?.step(p) }63        guard let s = v?.stringValue else { throw VaultError.invalidJSON("missing string at \(path.joined(separator: "."))") }64        return s65    }66    func int(_ path: String...) throws -> Int {67        var v: JSONValue? = self68        for p in path { v = v?.step(p) }69        guard let n = v?.intValue else { throw VaultError.invalidJSON("missing integer at \(path.joined(separator: "."))") }70        return n71    }72    func value(_ path: String...) throws -> JSONValue {73        var v: JSONValue? = self74        for p in path { v = v?.step(p) }75        guard let r = v else { throw VaultError.invalidJSON("missing value at \(path.joined(separator: "."))") }76        return r77    }78}79
Files main
Menu

Theme