SPB Git

Repository Public

NextFilesTests.swift 30.6 KB 578 lines swift

1// SP Notes Core2// File: Tests/SPNotesAPITests/NextFilesTests.swift3// Description: Ranged file access: the real answers of the scratch backend (group `files`: 200 / 206 / 416, If-Range,4//              ETag = sha256) and the resumable download against a local stub that follows the same rules (206 append,5//              200 restart, 416, changed ETag, cancellation and connection loss mid-way, then resume).6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import CryptoKit10import Foundation11import XCTest12@testable import SPNotesAPI1314// MARK: - A file server stub that honours Range / If-Range like the backend1516/// What the stub serves and how it misbehaves on demand.17final class RangeFileServer: @unchecked Sendable {18    static let shared = RangeFileServer()19    private let lock = NSLock()20    private var _content = Data()21    private var _etag: String?22    private var _requests: [RecordedRequest] = []23    /// Bytes of body after which the answer stalls for ever (until the client cancels).24    private var _stallAfter: Int?25    /// Bytes of body after which the connection is lost.26    private var _dropAfter: Int?27    private var _status: Int?28    private var _ignoresRange = false2930    static func sha256(_ data: Data) -> String { SHA256.hash(data: data).map { String(format: "%02x", $0) }.joined() }3132    func serve(_ content: Data, etag: String? = nil) {33        lock.withLock {34            _content = content35            _etag = etag ?? "\"\(RangeFileServer.sha256(content))\""36            _requests = []37            _stallAfter = nil38            _dropAfter = nil39            _status = nil40            _ignoresRange = false41        }42    }4344    var requests: [RecordedRequest] { lock.withLock { _requests } }45    var etag: String { lock.withLock { _etag ?? "" } }46    func stall(after bytes: Int?) { lock.withLock { _stallAfter = bytes } }47    func drop(after bytes: Int?) { lock.withLock { _dropAfter = bytes } }48    func fail(status: Int?) { lock.withLock { _status = status } }4950    struct Plan {51        var status: Int52        var headers: [String: String]53        var body: Data54        var stallAfter: Int?55        var dropAfter: Int?56    }5758    func plan(for request: RecordedRequest) -> Plan {59        lock.withLock {60            _requests.append(request)61            let etag = _etag ?? ""62            let size = _content.count63            var headers = ["Accept-Ranges": "bytes", "ETag": etag, "Content-Type": "application/octet-stream",64                           "Content-Disposition": "attachment; filename*=UTF-8''stub.bin"]65            if let status = _status {66                return Plan(status: status, headers: ["Content-Type": "application/json"], body: Data(#"{"error":"Not found","code":"not_found"}"#.utf8))67            }68            func whole() -> Plan {69                headers["Content-Length"] = String(size)70                return Plan(status: 200, headers: headers, body: _content, stallAfter: _stallAfter, dropAfter: _dropAfter)71            }72            guard let range = request.header("Range"), range.hasPrefix("bytes="), !range.contains(",") else { return whole() }73            if let condition = request.header("If-Range"), condition != etag { return whole() }74            let spec = range.dropFirst("bytes=".count).split(separator: "-", maxSplits: 1, omittingEmptySubsequences: false)75            guard spec.count == 2 else { return whole() }76            var lower = 0, upper = size - 177            if spec[0].isEmpty {78                guard let n = Int(spec[1]), n > 0 else { return whole() }79                lower = max(0, size - n)80            } else {81                guard let a = Int(spec[0]) else { return whole() }82                lower = a83                if !spec[1].isEmpty { guard let b = Int(spec[1]) else { return whole() }; upper = min(b, size - 1) }84            }85            guard lower < size, lower <= upper else {86                headers["Content-Range"] = "bytes */\(size)"87                return Plan(status: 416, headers: headers, body: Data())88            }89            headers["Content-Range"] = "bytes \(lower)-\(upper)/\(size)"90            headers["Content-Length"] = String(upper - lower + 1)91            return Plan(status: 206, headers: headers, body: _content.subdata(in: lower..<(upper + 1)), stallAfter: _stallAfter, dropAfter: _dropAfter)92        }93    }94}9596/// Serves `RangeFileServer.shared` in chunks, so that a transfer can be interrupted half-way.97final class RangeFileProtocol: URLProtocol, @unchecked Sendable {98    private let stopped = NSLock()99    private var isStopped = false100101    override class func canInit(with request: URLRequest) -> Bool { true }102    override class func canonicalRequest(for request: URLRequest) -> URLRequest { request }103104    override func startLoading() {105        let recorded = RecordedRequest(method: request.httpMethod ?? "GET", url: request.url!, headers: request.allHTTPHeaderFields ?? [:], body: Data())106        let plan = RangeFileServer.shared.plan(for: recorded)107        let response = HTTPURLResponse(url: request.url!, statusCode: plan.status, httpVersion: "HTTP/1.1", headerFields: plan.headers)!108        client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)109        DispatchQueue.global().async { [self] in110            let chunk = 16 * 1024111            var sent = 0112            while sent < plan.body.count {113                if stopped.withLock({ isStopped }) { return }114                var end = min(sent + chunk, plan.body.count)115                if let limit = plan.dropAfter ?? plan.stallAfter { end = min(end, limit) }116                if end > sent {117                    client?.urlProtocol(self, didLoad: plan.body.subdata(in: sent..<end))118                    sent = end119                }120                if let limit = plan.dropAfter, sent >= limit {121                    // A real connection dies some time after its last bytes (URLSession drops what the reader has not122                    // consumed yet when the error arrives).123                    Thread.sleep(forTimeInterval: 0.3)124                    client?.urlProtocol(self, didFailWithError: URLError(.networkConnectionLost))125                    return126                }127                if let limit = plan.stallAfter, sent >= limit { return }   // never finishes: the client has to cancel128            }129            client?.urlProtocolDidFinishLoading(self)130        }131    }132133    override func stopLoading() { stopped.withLock { isStopped = true } }134}135136// MARK: - Tests137138final class NextFilesTests: XCTestCase {139    private var directory: URL!140141    override func setUpWithError() throws {142        directory = FileManager.default.temporaryDirectory.appendingPathComponent("spn-files-\(UUID().uuidString)")143        try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)144    }145146    override func tearDownWithError() throws {147        try? FileManager.default.removeItem(at: directory)148    }149150    private func api() -> NotesAPI {151        let session = URLSessionConfiguration.ephemeral152        session.protocolClasses = [RangeFileProtocol.self]153        return NotesAPI(client: HTTPClient(configuration: TestEnv.configuration(), tokenStore: InMemoryTokenStore(token: TestEnv.token), sessionConfiguration: session))154    }155156    /// Deterministic bytes (the same generator as Scripts/recorders/files.py).157    private func bytes(_ count: Int, seed: String = "spn-fixture") -> Data {158        var data = Data()159        var i = 0160        while data.count < count {161            data.append(contentsOf: SHA256.hash(data: Data("\(seed)-\(i)".utf8)))162            i += 1163        }164        return data.prefix(count)165    }166167    private var destination: URL { directory.appendingPathComponent("file.bin") }168    private var partial: URL { directory.appendingPathComponent("file.bin.part") }169    private var sidecar: URL { NotesAPI.etagSidecarURL(forPartial: partial) }170    private func exists(_ url: URL) -> Bool { FileManager.default.fileExists(atPath: url.path) }171172    // MARK: recorded answers173174    func testRecordedRanges() throws {175        let content = bytes(3000)176        let whole = try NextFixture.load("files", "file-whole")177        XCTAssertEqual(whole.entry.status, 200)178        XCTAssertEqual(whole.data, content, "the recorder uploaded these bytes")179        XCTAssertEqual(whole.entry.headers["accept-ranges"], "bytes")180        let etag = try XCTUnwrap(whole.entry.headers["etag"])181        XCTAssertEqual(etag, "\"\(RangeFileServer.sha256(content))\"", "ETag = quoted sha256 of the content")182        XCTAssertEqual(NotesAPI.sha256Hex(fromETag: etag), RangeFileServer.sha256(content))183        XCTAssertEqual(whole.entry.headers["content-length"], "3000")184        XCTAssertEqual(whole.entry.headers["content-disposition"].flatMap(NotesAPI.filename(fromContentDisposition:)), "fixture-range.bin")185186        let expected: [(String, ClosedRange<Int64>)] = [("file-range-first", 0...9), ("file-range-open", 1000...2999), ("file-range-suffix", 2984...2999),187                                                         ("file-range-clamped", 2990...2999), ("file-if-range-match", 1000...2999)]188        for (name, range) in expected {189            let fixture = try NextFixture.load("files", name)190            XCTAssertEqual(fixture.entry.status, 206, name)191            let parsed = try XCTUnwrap(fixture.entry.headers["content-range"].flatMap(ContentRange.init(header:)), name)192            XCTAssertEqual(parsed, ContentRange(range: range, total: 3000), name)193            XCTAssertEqual(fixture.data, content.subdata(in: Int(range.lowerBound)..<Int(range.upperBound + 1)), name)194            XCTAssertEqual(fixture.entry.headers["content-length"], String(range.count), name)195            XCTAssertEqual(fixture.entry.headers["etag"], etag, "\(name): a range carries the ETag of the whole content")196        }197198        let unsatisfiable = try NextFixture.load("files", "file-range-416")199        XCTAssertEqual(unsatisfiable.entry.status, 416)200        XCTAssertTrue(unsatisfiable.data.isEmpty, "no JSON body")201        XCTAssertEqual(unsatisfiable.entry.headers["content-range"].flatMap(ContentRange.init(header:)), ContentRange(range: nil, total: 3000))202        XCTAssertEqual(unsatisfiable.entry.headers["etag"], etag)203        let error = APIError.fromResponse(status: 416, body: unsatisfiable.data)204        XCTAssertNil(error.code)205        XCTAssertFalse(error.isRetryable)206207        // The whole file again: another ETag in If-Range, several ranges, a malformed header.208        for name in ["file-if-range-mismatch", "file-range-multiple", "file-range-malformed"] {209            let fixture = try NextFixture.load("files", name)210            XCTAssertEqual(fixture.entry.status, 200, name)211            XCTAssertNil(fixture.entry.headers["content-range"], name)212            XCTAssertEqual(fixture.data, content, name)213        }214    }215216    func testRecordedFileErrorsAndUpload() throws {217        let uploaded = try NextFixture.decode(UploadedAttachment.self, "files", "upload")218        XCTAssertEqual(uploaded.size, 3000)219        XCTAssertEqual(uploaded.url, NotesAPI.filePath(id: uploaded.id))220        let again = try NextFixture.load("files", "upload-existing")221        XCTAssertEqual(again.entry.status, 200)222        XCTAssertEqual(again.entry.headers["x-existing"], "1")223        XCTAssertEqual(try NextFixture.decode([AttachmentInfo].self, "files", "attachments-list").map(\.id), [uploaded.id])224225        let missing = try NextFixture.error("files", "file-not-found")226        XCTAssertTrue(missing.isNotFound)227        XCTAssertEqual(missing.code, APIError.NextCode.notFound)228        XCTAssertEqual(try NextFixture.error("files", "file-bad-id").code, APIError.NextCode.badRequest)229        // 3.0: a soft-deleted attachment is no longer served, with or without Range.230        for name in ["file-deleted", "file-deleted-range"] {231            let deleted = try NextFixture.error("files", name)232            XCTAssertEqual(deleted.status, 404, name)233            XCTAssertEqual(deleted.code, APIError.NextCode.notFound, name)234        }235    }236237    // MARK: header parsing238239    func testByteRangeAndContentRange() {240        XCTAssertEqual(ByteRange.bounded(0...9).headerValue, "bytes=0-9")241        XCTAssertEqual(ByteRange.from(1000).headerValue, "bytes=1000-")242        XCTAssertEqual(ByteRange.suffix(16).headerValue, "bytes=-16")243        XCTAssertEqual(ContentRange(header: "bytes 0-9/200"), ContentRange(range: 0...9, total: 200))244        XCTAssertEqual(ContentRange(header: " Bytes 5-5/6 "), ContentRange(range: 5...5, total: 6))245        XCTAssertEqual(ContentRange(header: "bytes */200"), ContentRange(range: nil, total: 200))246        XCTAssertEqual(ContentRange(header: "bytes 10-19/*"), ContentRange(range: 10...19, total: nil))247        for bad in ["", "items 0-9/200", "bytes 9-0/200", "bytes a-b/200", "bytes 0-9", "bytes 0-9/-1", "bytes -5/10"] {248            XCTAssertNil(ContentRange(header: bad), bad)249        }250        XCTAssertTrue(NotesAPI.sameETag("W/\"abc\"", "\"abc\""))251        XCTAssertFalse(NotesAPI.sameETag("\"abc\"", "\"abd\""))252        XCTAssertFalse(NotesAPI.sameETag(nil, "\"abc\""))253        XCTAssertNil(NotesAPI.sha256Hex(fromETag: "\"nu1s2pkvxv92g\""), "not every ETag is a checksum")254        XCTAssertNil(NotesAPI.sha256Hex(fromETag: nil))255    }256257    // MARK: fileRange258259    func testFileRange() async throws {260        let content = bytes(5000)261        RangeFileServer.shared.serve(content)262        let api = api()263264        let slice = try await api.fileRange(id: "att-1", range: .bounded(10...19))265        XCTAssertEqual(slice.status, 206)266        XCTAssertTrue(slice.isPartial)267        XCTAssertEqual(slice.data, content.subdata(in: 10..<20))268        XCTAssertEqual(slice.contentRange, ContentRange(range: 10...19, total: 5000))269        XCTAssertEqual(slice.totalSize, 5000)270        XCTAssertEqual(slice.etag, RangeFileServer.shared.etag)271        XCTAssertEqual(slice.contentType, "application/octet-stream")272        let request = try XCTUnwrap(RangeFileServer.shared.requests.last)273        XCTAssertEqual(request.method, "GET")274        XCTAssertEqual(request.path, "/api/files/att-1")275        XCTAssertEqual(request.header("Range"), "bytes=10-19")276        XCTAssertNil(request.header("If-Range"))277        XCTAssertEqual(request.header("Accept-Encoding"), "identity", "offsets are those of the stored bytes")278        XCTAssertEqual(request.header("Accept"), "*/*")279        XCTAssertEqual(request.header("Cookie"), "__Host-note_session=\(TestEnv.token)")280        XCTAssertNil(request.header("Origin"), "a GET carries no Origin")281282        let tail = try await api.fileRange(id: "att-1", range: .suffix(4))283        XCTAssertEqual(tail.data, content.suffix(4))284        XCTAssertEqual(tail.contentRange?.range, 4996...4999)285286        // 416 is an answer, not an error.287        let beyond = try await api.fileRange(id: "att-1", range: .from(5000))288        XCTAssertEqual(beyond.status, 416)289        XCTAssertTrue(beyond.isUnsatisfiable)290        XCTAssertTrue(beyond.data.isEmpty)291        XCTAssertEqual(beyond.totalSize, 5000)292293        // If-Range with another ETag: the whole file.294        let changed = try await api.fileRange(id: "att-1", range: .from(100), ifRange: "\"old\"")295        XCTAssertEqual(RangeFileServer.shared.requests.last?.header("If-Range"), "\"old\"")296        XCTAssertEqual(changed.status, 200)297        XCTAssertFalse(changed.isPartial)298        XCTAssertEqual(changed.data, content)299        XCTAssertEqual(changed.totalSize, 5000)300        let same = try await api.fileRange(id: "att-1", range: .from(100), ifRange: RangeFileServer.shared.etag)301        XCTAssertEqual(same.status, 206)302        XCTAssertEqual(same.data.count, 4900)303304        RangeFileServer.shared.fail(status: 404)305        let error = await captureAPIError { try await api.fileRange(id: "att-1", range: .from(0)) }306        XCTAssertEqual(error?.status, 404)307        XCTAssertEqual(error?.code, APIError.NextCode.notFound)308    }309310    // MARK: resumable download311312    func testFreshDownload() async throws {313        let content = bytes(200_000)314        RangeFileServer.shared.serve(content)315        let progress = RangeProgressLog()316        let result = try await api().downloadFile(id: "att-1", to: destination, resumingFrom: partial) { progress.add($0) }317        XCTAssertEqual(try Data(contentsOf: destination), content)318        XCTAssertEqual(result.fileURL, destination)319        XCTAssertEqual(result.size, 200_000)320        XCTAssertEqual(result.resumedFrom, 0)321        XCTAssertFalse(result.restarted)322        XCTAssertTrue(result.verified)323        XCTAssertEqual(result.etag, RangeFileServer.shared.etag)324        XCTAssertEqual(result.contentType, "application/octet-stream")325        XCTAssertEqual(result.suggestedFilename, "stub.bin")326        XCTAssertFalse(exists(partial), "the partial file became the destination")327        XCTAssertFalse(exists(sidecar))328        let requests = RangeFileServer.shared.requests329        XCTAssertEqual(requests.count, 1)330        XCTAssertNil(requests[0].header("Range"), "nothing on disk: no Range")331        XCTAssertNil(requests[0].header("If-Range"))332        let values = progress.values333        XCTAssertEqual(values.last, TransferProgress(completedBytes: 200_000, totalBytes: 200_000))334        XCTAssertEqual(values.map(\.completedBytes), values.map(\.completedBytes).sorted())335    }336337    func testResumeAppendsOn206() async throws {338        let content = bytes(100_000)339        RangeFileServer.shared.serve(content)340        try content.prefix(30_000).write(to: partial)341        try Data(RangeFileServer.shared.etag.utf8).write(to: sidecar)342        let progress = RangeProgressLog()343        let result = try await api().downloadFile(id: "att-1", to: destination, resumingFrom: partial) { progress.add($0) }344        XCTAssertEqual(try Data(contentsOf: destination), content)345        XCTAssertEqual(result.resumedFrom, 30_000)346        XCTAssertFalse(result.restarted)347        XCTAssertTrue(result.verified)348        let requests = RangeFileServer.shared.requests349        XCTAssertEqual(requests.count, 1)350        XCTAssertEqual(requests[0].header("Range"), "bytes=30000-")351        XCTAssertEqual(requests[0].header("If-Range"), RangeFileServer.shared.etag, "the ETag of the sidecar")352        XCTAssertEqual(progress.values.first?.completedBytes ?? 0 > 30_000, true, "progress counts the resumed bytes")353        XCTAssertEqual(progress.values.last, TransferProgress(completedBytes: 100_000, totalBytes: 100_000))354        XCTAssertFalse(exists(sidecar))355    }356357    func testResumeRestartsOn200WhenTheContentChanged() async throws {358        let old = bytes(50_000, seed: "old")359        let new = bytes(80_000, seed: "new")360        RangeFileServer.shared.serve(new)361        try old.prefix(20_000).write(to: partial)362        let oldETag = "\"\(RangeFileServer.sha256(old))\""363        // The caller kept the ETag itself (no sidecar).364        let result = try await api().downloadFile(id: "att-1", to: destination, resumingFrom: partial, etag: oldETag)365        XCTAssertEqual(try Data(contentsOf: destination), new, "no byte of the old content survives")366        XCTAssertTrue(result.restarted)367        XCTAssertEqual(result.resumedFrom, 0)368        XCTAssertTrue(result.verified)369        XCTAssertEqual(result.etag, RangeFileServer.shared.etag)370        let requests = RangeFileServer.shared.requests371        XCTAssertEqual(requests.count, 1, "If-Range lets the server answer the whole file at once")372        XCTAssertEqual(requests[0].header("Range"), "bytes=20000-")373        XCTAssertEqual(requests[0].header("If-Range"), oldETag)374    }375376    func test416MeansAlreadyComplete() async throws {377        let content = bytes(40_000)378        RangeFileServer.shared.serve(content)379        try content.write(to: partial)380        try Data(RangeFileServer.shared.etag.utf8).write(to: sidecar)381        let progress = RangeProgressLog()382        let result = try await api().downloadFile(id: "att-1", to: destination, resumingFrom: partial) { progress.add($0) }383        XCTAssertEqual(try Data(contentsOf: destination), content)384        XCTAssertEqual(result.resumedFrom, 40_000)385        XCTAssertFalse(result.restarted)386        XCTAssertTrue(result.verified, "the bytes on disk are checked against the ETag of the 416")387        XCTAssertEqual(RangeFileServer.shared.requests.map { $0.header("Range") }, ["bytes=40000-"], "one request, no byte transferred")388        XCTAssertEqual(progress.values, [TransferProgress(completedBytes: 40_000, totalBytes: 40_000)])389        XCTAssertFalse(exists(partial))390        XCTAssertFalse(exists(sidecar))391    }392393    func test416WithAnotherContentRestarts() async throws {394        // On disk: more bytes than the file now has on the server (it was replaced by a shorter one).395        let old = bytes(60_000, seed: "old")396        let new = bytes(25_000, seed: "new")397        RangeFileServer.shared.serve(new)398        try old.write(to: partial)399        let result = try await api().downloadFile(id: "att-1", to: destination, resumingFrom: partial)400        XCTAssertEqual(try Data(contentsOf: destination), new)401        XCTAssertTrue(result.restarted)402        XCTAssertEqual(result.resumedFrom, 0)403        XCTAssertTrue(result.verified)404        XCTAssertEqual(RangeFileServer.shared.requests.map { $0.header("Range") }, ["bytes=60000-", nil], "416, then a clean request without Range")405406        // Same size but another ETag: not "already complete".407        let twin = bytes(25_000, seed: "twin")408        try twin.write(to: partial)409        try Data("\"\(RangeFileServer.sha256(twin))\"".utf8).write(to: sidecar)410        RangeFileServer.shared.serve(new)411        let second = try await api().downloadFile(id: "att-1", to: destination, resumingFrom: partial)412        XCTAssertEqual(try Data(contentsOf: destination), new)413        XCTAssertTrue(second.restarted)414        // If-Range with the old ETag makes the server send the whole file directly.415        XCTAssertEqual(RangeFileServer.shared.requests.count, 1)416    }417418    func testResumedBytesOfAnotherContentAreCaughtByTheChecksum() async throws {419        // No ETag is known for the partial bytes, so the server appends (206) — the sha256 does not match.420        let old = bytes(90_000, seed: "old")421        let new = bytes(90_000, seed: "new")422        RangeFileServer.shared.serve(new)423        try old.prefix(10_000).write(to: partial)424        let result = try await api().downloadFile(id: "att-1", to: destination, resumingFrom: partial)425        XCTAssertEqual(try Data(contentsOf: destination), new)426        XCTAssertTrue(result.restarted)427        XCTAssertTrue(result.verified)428        XCTAssertEqual(RangeFileServer.shared.requests.map { $0.header("Range") }, ["bytes=10000-", nil], "one automatic clean restart")429430        // Without verification the mismatch would go unnoticed: the option exists for callers that check themselves.431        RangeFileServer.shared.serve(new)432        try old.prefix(10_000).write(to: partial)433        let unchecked = try await api().downloadFile(id: "att-1", to: destination, resumingFrom: partial, verifyChecksum: false)434        XCTAssertFalse(unchecked.verified)435        XCTAssertEqual(unchecked.resumedFrom, 10_000)436        XCTAssertNotEqual(try Data(contentsOf: destination), new)437    }438439    func testChecksumMismatchOnAFreshDownloadIsAnError() async throws {440        let content = bytes(20_000)441        RangeFileServer.shared.serve(content, etag: "\"\(String(repeating: "0", count: 64))\"")442        let error = await captureAPIError { try await self.api().downloadFile(id: "att-1", to: self.destination, resumingFrom: self.partial) }443        guard case .file(let message)? = error else { return XCTFail("expected a file error, got \(String(describing: error))") }444        XCTAssertTrue(message.contains("checksum"))445        XCTAssertFalse(exists(destination), "a file that fails its checksum never reaches the destination")446        XCTAssertFalse(exists(partial))447        XCTAssertEqual(RangeFileServer.shared.requests.count, 1, "no endless retry")448449        // An ETag that is not a sha256 cannot be verified: the download succeeds, unverified.450        RangeFileServer.shared.serve(content, etag: "\"v42\"")451        let result = try await api().downloadFile(id: "att-1", to: destination, resumingFrom: partial)452        XCTAssertFalse(result.verified)453        XCTAssertEqual(try Data(contentsOf: destination), content)454    }455456    func testCancellationKeepsThePartialFileThenResumes() async throws {457        let content = bytes(400_000)458        RangeFileServer.shared.serve(content)459        RangeFileServer.shared.stall(after: 200_000)   // the answer hangs after 200 kB460        let api = api()461        let destination = destination, partial = partial462        let download = Task { try await api.downloadFile(id: "att-1", to: destination, resumingFrom: partial) }463        // Wait until bytes have reached the disk, then cancel the calling task.464        let deadline = Date().addingTimeInterval(10)465        while NotesAPI.fileSize(partial) < 64 * 1024, Date() < deadline { try await Task.sleep(nanoseconds: 10_000_000) }466        XCTAssertGreaterThanOrEqual(NotesAPI.fileSize(partial), 64 * 1024, "the transfer writes as it receives")467        download.cancel()468        do {469            _ = try await download.value470            XCTFail("a cancelled download must throw")471        } catch let error as APIError {472            XCTAssertEqual(error, .cancelled)473        }474        let kept = NotesAPI.fileSize(partial)475        XCTAssertGreaterThanOrEqual(kept, 64 * 1024)476        XCTAssertLessThanOrEqual(kept, 200_000)477        XCTAssertEqual(try Data(contentsOf: partial), content.prefix(Int(kept)), "what is on disk is a clean prefix of the content")478        XCTAssertEqual(try String(contentsOf: sidecar, encoding: .utf8), RangeFileServer.shared.etag, "the ETag is remembered for the next attempt")479        XCTAssertFalse(exists(destination))480481        // Second attempt: continues where the first one stopped.482        RangeFileServer.shared.stall(after: nil)483        let result = try await api.downloadFile(id: "att-1", to: destination, resumingFrom: partial)484        XCTAssertEqual(try Data(contentsOf: destination), content)485        XCTAssertEqual(result.resumedFrom, kept)486        XCTAssertFalse(result.restarted)487        XCTAssertTrue(result.verified)488        let last = try XCTUnwrap(RangeFileServer.shared.requests.last)489        XCTAssertEqual(last.header("Range"), "bytes=\(kept)-")490        XCTAssertEqual(last.header("If-Range"), RangeFileServer.shared.etag)491        XCTAssertEqual(RangeFileServer.shared.requests.count, 2)492    }493494    func testConnectionLossKeepsThePartialFileThenResumes() async throws {495        let content = bytes(300_000)496        RangeFileServer.shared.serve(content)497        RangeFileServer.shared.drop(after: 150_000)498        let api = api()499        let error = await captureAPIError { try await api.downloadFile(id: "att-1", to: self.destination, resumingFrom: self.partial) }500        XCTAssertEqual(error, .transport(.networkConnectionLost))501        XCTAssertEqual(error?.isRetryable, true)502        let kept = NotesAPI.fileSize(partial)503        XCTAssertGreaterThan(kept, 0)504        XCTAssertLessThanOrEqual(kept, 150_000)505        XCTAssertEqual(try Data(contentsOf: partial), content.prefix(Int(kept)))506507        RangeFileServer.shared.drop(after: nil)508        let result = try await api.downloadFile(id: "att-1", to: destination, resumingFrom: partial)509        XCTAssertEqual(try Data(contentsOf: destination), content)510        XCTAssertEqual(result.resumedFrom, kept)511        XCTAssertTrue(result.verified)512        XCTAssertEqual(RangeFileServer.shared.requests.last?.header("Range"), "bytes=\(kept)-")513    }514515    func testHTTPErrorLeavesThePartialFileAlone() async throws {516        let content = bytes(10_000)517        RangeFileServer.shared.serve(content)518        try content.prefix(4_000).write(to: partial)519        RangeFileServer.shared.fail(status: 404)520        let error = await captureAPIError { try await self.api().downloadFile(id: "att-1", to: self.destination, resumingFrom: self.partial) }521        XCTAssertEqual(error?.status, 404)522        XCTAssertEqual(error?.code, APIError.NextCode.notFound, "the JSON error of the route is decoded")523        XCTAssertEqual(NotesAPI.fileSize(partial), 4_000, "the caller decides what to do with it")524        XCTAssertFalse(exists(destination))525    }526527    func testEmptyFileAndReplacedDestination() async throws {528        RangeFileServer.shared.serve(Data())529        try Data("previous".utf8).write(to: destination)530        let result = try await api().downloadFile(id: "att-1", to: destination, resumingFrom: partial)531        XCTAssertEqual(result.size, 0)532        XCTAssertTrue(result.verified, "sha256 of nothing")533        XCTAssertEqual(try Data(contentsOf: destination), Data(), "the destination is replaced")534    }535536    // MARK: transport additions537538    func testExtraHeadersNeverOverrideTheTransport() async throws {539        let (client, _) = TestEnv.client()540        var request = HTTPRequest(method: .post, path: "/api/query", body: Data("{}".utf8), contentType: "application/json")541        request.extraHeaders = ["Range": "bytes=0-1", "Cookie": "evil=1", "Origin": "https://evil.test", "User-Agent": "x", "X-Request-Id": "forged-request-id", "Accept": "text/html"]542        let built = try await client.urlRequest(for: request)543        XCTAssertEqual(built.value(forHTTPHeaderField: "Range"), "bytes=0-1")544        XCTAssertEqual(built.value(forHTTPHeaderField: "Cookie"), "__Host-note_session=\(TestEnv.token)")545        XCTAssertEqual(built.value(forHTTPHeaderField: "Origin"), "https://notes.test")546        XCTAssertEqual(built.value(forHTTPHeaderField: "Accept"), "application/json")547        XCTAssertNotEqual(built.value(forHTTPHeaderField: "x-request-id"), "forged-request-id")548        XCTAssertTrue(built.value(forHTTPHeaderField: "User-Agent")?.hasPrefix("SPNotes/") ?? false)549        XCTAssertEqual(HTTPRequest.get("/x").extraHeaders, [:], "nothing is added by default")550    }551552    func testOpenBytesDoesNotJudgeTheStatus() async throws {553        let (client, _) = TestEnv.client()554        StubServer.shared.reset { _ in StubReply(status: 416, headers: ["Content-Range": "bytes */7"], body: Data()) }555        let (response, bytes) = try await client.openBytes(.get("/api/files/x"))556        XCTAssertEqual(response.status, 416)557        XCTAssertEqual(response.header("content-range"), "bytes */7")558        var count = 0559        for try await _ in bytes { count += 1 }560        XCTAssertEqual(count, 0)561562        // A 401 on the current token still ends the session.563        StubServer.shared.reset { _ in .json(#"{"error":"Sign-in required","code":"unauthenticated"}"#, status: 401) }564        let (unauthorized, _) = try await client.openBytes(.get("/api/files/x"))565        XCTAssertEqual(unauthorized.status, 401)566        let has = await client.hasSession567        XCTAssertFalse(has)568    }569}570571/// Collects progress callbacks (they arrive on a background task).572final class RangeProgressLog: @unchecked Sendable {573    private let lock = NSLock()574    private var items: [TransferProgress] = []575    func add(_ progress: TransferProgress) { lock.withLock { items.append(progress) } }576    var values: [TransferProgress] { lock.withLock { items } }577}578
Files main
Menu

Theme