From aadef00d5740f82b316bd44eaa97061d0d3fb1c8 Mon Sep 17 00:00:00 2001 From: patricioxavier8 <116120453+patricioxavier8@users.noreply.github.com> Date: Tue, 6 Oct 2026 21:57:30 -0500 Subject: [PATCH] fix: report download hash mismatch when ignore flag is false - Fix inverted boolean check in NetworkFacade.decryptFile that prevented hash mismatches from ever being reported - Update testShouldFailIfHashIsNotMatching to assert EnrichedError with HashMissmatch cause - Add testShouldNotFailIfHashIsNotMatchingAndIgnoreFlagIsTrue to verify ignoreHashMissmatchCheck flag --- .../Services/Network/NetworkFacade.swift | 2 +- .../Services/Network/NetworkFacadeTests.swift | 21 +++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Sources/InternxtSwiftCore/Services/Network/NetworkFacade.swift b/Sources/InternxtSwiftCore/Services/Network/NetworkFacade.swift index 337b8c8..d5d6ffe 100644 --- a/Sources/InternxtSwiftCore/Services/Network/NetworkFacade.swift +++ b/Sources/InternxtSwiftCore/Services/Network/NetworkFacade.swift @@ -429,7 +429,7 @@ public struct NetworkFacade { let hashMatch = encryptedContentHash.toHexString() == encryptedFileDownloadResult.expectedContentHash - if hashMatch == false && ignoreHashMissmatchCheck != false { + if hashMatch == false && ignoreHashMissmatchCheck == false { throw EnrichedError( code: .downloadHashMismatch, step: .downloadDecrypt, diff --git a/Tests/InternxtSwiftCoreTests/Services/Network/NetworkFacadeTests.swift b/Tests/InternxtSwiftCoreTests/Services/Network/NetworkFacadeTests.swift index 6ce0252..91d8dd9 100644 --- a/Tests/InternxtSwiftCoreTests/Services/Network/NetworkFacadeTests.swift +++ b/Tests/InternxtSwiftCoreTests/Services/Network/NetworkFacadeTests.swift @@ -232,9 +232,26 @@ final class NetworkFacadeTests: XCTestCase { do { _ = try await sut.decryptFile(bucketId: "93535c0bfff5de6d59c8eec72b46b605", destinationURL: destination, progressHandler: {_ in }, encryptedFileDownloadResult: downloadResult) + XCTFail("Expected an error to be thrown") + } catch let enrichedError as EnrichedError { + XCTAssertEqual(enrichedError.cause as? NetworkFacadeError, NetworkFacadeError.HashMissmatch) } catch { - - XCTAssertEqual(error as? NetworkFacadeError, NetworkFacadeError.HashMissmatch) + XCTFail("Expected EnrichedError but got \(type(of: error))") + } + } + + func testShouldNotFailIfHashIsNotMatchingAndIgnoreFlagIsTrue() async throws { + let destination = getTemporaryDestination() + let contentURL = getTemporaryDestination() + // Write into the file + try "filedata".data(using: .utf8)?.write(to: contentURL) + + let downloadResult = DownloadResult(url: contentURL, expectedContentHash: "4ae6fcc4dd6ebcdb9076f2396d64da48", index: "2ec6d83f8987fe2bd04d0260208521d49d4c79187d71989a16ca79d41b90b8f1") + + do { + _ = try await sut.decryptFile(bucketId: "93535c0bfff5de6d59c8eec72b46b605", destinationURL: destination, progressHandler: {_ in }, encryptedFileDownloadResult: downloadResult, ignoreHashMissmatchCheck: true) + } catch { + XCTFail("Expected no error when ignoreHashMissmatchCheck is true, but got \(error)") } }