SPB Git

Repository Public License

HashTests.swift 7.8 KB 164 lines swift

1// ORIX Secure2// File: Packages/ORIXFileInspection/Tests/ORIXFileInspectionTests/HashTests.swift3// Description: Streamed SHA-256 against known vectors and an independent implementation, across chunk sizes, with4//              progress, cancellation and bounded memory on a 64 MB file.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import CryptoKit9import Foundation10import Testing11@testable import ORIXFileInspection1213@Suite("SHA-256 streaming")14struct HashTests {15    final class Counter: @unchecked Sendable {16        private let lock = NSLock()17        private var values: [UInt64] = []18        func add(_ value: UInt64) { lock.lock(); values.append(value); lock.unlock() }19        var all: [UInt64] { lock.lock(); defer { lock.unlock() }; return values }20    }2122    static func residentPeakBytes() -> Int {23        var usage = rusage()24        getrusage(RUSAGE_SELF, &usage)25        return Int(usage.ru_maxrss)26    }2728    @Test("Known vectors: empty input, abc, one million a")29    func knownVectors() async throws {30        let dir = try TempDir()31        let empty = try dir.write("empty", [])32        let emptyDigest = try await FileHasher.sha256(fileAt: empty)33        #expect(emptyDigest == "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")34        let abc = try dir.write("abc", bytes("abc"))35        let abcDigest = try await FileHasher.sha256(fileAt: abc)36        #expect(abcDigest == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")37        let million = try dir.write("million", [UInt8](repeating: 0x61, count: 1_000_000))38        let millionDigest = try await FileHasher.sha256(fileAt: million, chunkSize: 4096)39        #expect(millionDigest == "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0")40        #expect(FileHasher.sha256(of: Data("abc".utf8)) == abcDigest)41    }4243    @Test("A 1 MiB pattern matches the system shasum tool")44    func patternAgainstShasum() async throws {45        let dir = try TempDir()46        let pattern = (0..<(1 << 20)).map { UInt8(truncatingIfNeeded: ($0 &* 31) ^ ($0 >> 8)) }47        let url = try dir.write("pattern.bin", pattern)48        let digest = try await FileHasher.sha256(fileAt: url)49        #expect(digest == FileHasher.sha256(of: Data(pattern)))50        #if os(macOS)51        let process = Process()52        process.executableURL = URL(fileURLWithPath: "/usr/bin/shasum")53        process.arguments = ["-a", "256", url.path]54        let pipe = Pipe()55        process.standardOutput = pipe56        try process.run()57        let output = pipe.fileHandleForReading.readDataToEndOfFile()58        process.waitUntilExit()59        let expected = String(decoding: output, as: UTF8.self).split(separator: " ").first.map(String.init)60        #expect(process.terminationStatus == 0)61        #expect(digest == expected)62        #endif63    }6465    @Test("Chunk size does not change the result; progress is monotonic and complete")66    func multiChunk() async throws {67        let dir = try TempDir()68        var generator = SplitMix64(seed: 42)69        let content = (0..<((3 << 20) + 12_345)).map { _ in UInt8.random(in: 0...255, using: &generator) }70        let url = try dir.write("random.bin", content)71        let reference = SHA256.hash(data: Data(content)).map { String(format: "%02x", $0) }.joined()72        for chunk in [1, 4096, 65_536, 1 << 20, 100 << 20] {73            let counter = Counter()74            let digest = try await FileHasher.sha256(fileAt: url, chunkSize: chunk) { counter.add($0.bytesProcessed) }75            #expect(digest == reference, "chunk \(chunk)")76            let steps = counter.all77            #expect(steps.last == UInt64(content.count))78            #expect(zip(steps, steps.dropFirst()).allSatisfy { $0 < $1 })79            if chunk == 65_536 { #expect(steps.count == 49) }80        }81    }8283    @Test("A byte limit hashes only the prefix")84    func prefixLimit() async throws {85        let dir = try TempDir()86        let content = (0..<200_000).map { UInt8(truncatingIfNeeded: $0) }87        let url = try dir.write("data.bin", content)88        let prefix = try await FileHasher.sha256(fileAt: url, maxBytes: 65_536)89        #expect(prefix == FileHasher.sha256(of: Data(content.prefix(65_536))))90        let beyond = try await FileHasher.sha256(fileAt: url, maxBytes: 10_000_000)91        #expect(beyond == FileHasher.sha256(of: Data(content)))92        let zero = try await FileHasher.sha256(fileAt: url, maxBytes: 0)93        #expect(zero == "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")94    }9596    @Test("A missing file throws instead of returning a digest")97    func missingFile() async throws {98        let dir = try TempDir()99        await #expect(throws: (any Error).self) {100            _ = try await FileHasher.sha256(fileAt: dir.url.appendingPathComponent("absent.bin"))101        }102    }103104    @Test("Cancellation in the middle of a file stops the hash")105    func cancellation() async throws {106        let dir = try TempDir()107        let url = dir.url.appendingPathComponent("large.bin")108        FileManager.default.createFile(atPath: url.path, contents: nil)109        let handle = try FileHandle(forWritingTo: url)110        try handle.truncate(atOffset: 48 << 20)111        try handle.close()112        let counter = Counter()113        let task = Task {114            try await FileHasher.sha256(fileAt: url, chunkSize: 1 << 20) { progress in115                counter.add(progress.bytesProcessed)116                if progress.bytesProcessed >= 5 << 20 { withUnsafeCurrentTask { $0?.cancel() } }117            }118        }119        let result = await task.result120        #expect(throws: CancellationError.self) { try result.get() }121        let last = try #require(counter.all.last)122        #expect(last == 5 << 20)123        // The inspector propagates cancellation as well.124        let inspection = Task {125            try await FileInspector().inspect(fileAt: url) { progress in126                if progress.bytesProcessed >= 2 << 20 { withUnsafeCurrentTask { $0?.cancel() } }127            }128        }129        let inspected = await inspection.result130        #expect(throws: CancellationError.self) { try inspected.get() }131    }132133    @Test("A 64 MB file is hashed in bounded memory")134    func largeFileThroughput() async throws {135        let dir = try TempDir()136        let url = dir.url.appendingPathComponent("zeros.bin")137        FileManager.default.createFile(atPath: url.path, contents: nil)138        let handle = try FileHandle(forWritingTo: url)139        try handle.truncate(atOffset: 64 << 20)140        try handle.close()141        _ = try await FileHasher.sha256(fileAt: url, maxBytes: 1 << 20)142        let before = Self.residentPeakBytes()143        let counter = Counter()144        let start = Date()145        let digest = try await FileHasher.sha256(fileAt: url) { counter.add($0.bytesProcessed) }146        let elapsed = Date().timeIntervalSince(start)147        let growth = Self.residentPeakBytes() - before148        print("SHA-256 throughput: 64 MB in \(String(format: "%.3f", elapsed)) s = \(String(format: "%.0f", 64 / elapsed)) MB/s; peak resident memory growth \(growth / 1024) KiB")149        // Bounded memory, deterministically: the file reaches the hasher in pieces of at most one chunk (1 MiB).150        let steps = counter.all151        let largestStep = zip([0] + steps, steps).map { $1 - $0 }.max() ?? 0152        #expect(steps.count == 64)153        #expect(largestStep == 1 << 20)154        // SHA-256 of 64 MiB of zero bytes, computed independently in pieces with CryptoKit.155        var reference = SHA256()156        let block = Data(count: 1 << 16)157        for _ in 0..<1024 { reference.update(data: block) }158        #expect(digest == reference.finalize().map { String(format: "%02x", $0) }.joined())159        // The resident peak is process-wide and shared with tests running in parallel: it only has to stay under the file size.160        #expect(growth < 64 << 20)161        #expect(elapsed < 20)162    }163}164
Files main
Menu

Theme