SPB Git

Repository Public

AuditRegressionTests.swift 5.1 KB 96 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : Tests/QyvoraRecoveryTests/AuditRegressionTests.swift8// Module           : Tests9// Repository       : https://git.spboucher.ai/qyvora10// Project Website  : Not specified11// Created          : 2026-12-0412// Last Modified    : 2026-12-0413//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation42import QyvoraMath43import QyvoraStorage44import QyvoraVault45import Testing4647@testable import QyvoraRecovery4849/// Regression tests of the security audit (finding QYV-SVC-005): a backup whose records carry timestamps close to50/// 2^64 seconds used to trap the process during restoration (`UInt64(Double)` on a value that rounds up to 2^64),51/// after the new vault had already been created.52@Suite struct RecoveryAuditRegressionTests {53    @Test func vaultTimeNeverTraps() {54        #expect(VaultTime.seconds(Date(timeIntervalSince1970: 0)) == 0)55        #expect(VaultTime.seconds(Date(timeIntervalSince1970: -1e300)) == 0)56        #expect(VaultTime.seconds(Date(timeIntervalSince1970: 1_800_000_000.9)) == 1_800_000_000)57        #expect(VaultTime.seconds(Date(timeIntervalSince1970: Double(UInt64.max))) == UInt64.max)58        #expect(VaultTime.seconds(Date(timeIntervalSince1970: 1e300)) == UInt64.max)59        #expect(VaultTime.seconds(Date(timeIntervalSince1970: .infinity)) == UInt64.max)60        #expect(VaultTime.seconds(Date(timeIntervalSince1970: -.infinity)) == 0)61        #expect(VaultTime.seconds(Date(timeIntervalSince1970: .nan)) == 0)62        #expect(VaultTime.seconds(.distantFuture) > 60_000_000_000)63        #expect(VaultTime.seconds(.distantPast) == 0)64    }6566    @Test func backupWithExtremeTimestampsIsWrittenReadAndRestoredWithoutTrap() async throws {67        // what a parser produces for a stored value of 0xFFFF…FF: a date whose interval is 2^64 as a Double68        let extreme = Date(timeIntervalSince1970: TimeInterval(UInt64.max))69        let records = [70            VaultRecord(id: UUID(), type: .recoveryPhrase, createdAt: extreme, updatedAt: extreme, payload: Bytes("abandon about".utf8)),71            VaultRecord(id: UUID(), type: .setting, createdAt: Date(timeIntervalSince1970: -5), updatedAt: .distantFuture, payload: [1, 2, 3]),72        ]73        let backup = try BackupFile.create(74            records: records, vaultID: Bytes(repeating: 7, count: 16), password: "backup password", kdf: .insecureForTesting, createdAt: extreme)75        let contents = try BackupFile.open(backup, password: "backup password")76        #expect(contents.records.count == 2)77        #expect(contents.records.map(\.payload) == records.map(\.payload))78        #expect(VaultTime.seconds(contents.records[0].createdAt) == UInt64.max)79        #expect(VaultTime.seconds(contents.header.createdAt) == UInt64.max)8081        let root = FileManager.default.temporaryDirectory.appendingPathComponent("qyvora-recovery-audit-\(UUID().uuidString)", isDirectory: true)82        defer { try? FileManager.default.removeItem(at: root) }83        let vault = Vault(fileStore: try FileStore(root: root), keychain: InMemoryKeychainStore(), configuration: .init(kdf: .insecureForTesting))84        try await vault.create(password: "new device password")85        let restored = try await BackupFile.restore(backup, password: "backup password", into: vault)86        #expect(restored.records.count == 2)87        let stored = try await vault.records()88        #expect(Set(stored.map(\.id)) == Set(records.map(\.id)))89        #expect(stored.first { $0.type == .recoveryPhrase }?.payload == Bytes("abandon about".utf8))90        // the restored vault is intact and can be backed up again91        #expect(try await vault.verifyIntegrity().recordsVerified == 2)92        let again = try await BackupFile.export(from: vault, password: "p", kdf: .insecureForTesting)93        #expect(try BackupFile.open(again, password: "p").records.count == 2)94    }95}96
Files main
Menu

Theme