From 85c6967345a05f02a89e0ab124e02fba4d4d7e97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Gutowski?= Date: Mon, 29 Jan 2024 21:38:27 +0100 Subject: [PATCH] Prevent configuration decoding errors from being swallowed --- .../Models/Configuration/Configuration.swift | 6 +-- .../Configuration/ConfigurationTests.swift | 43 +++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/Sources/ConfidentialCore/Models/Configuration/Configuration.swift b/Sources/ConfidentialCore/Models/Configuration/Configuration.swift index cdbe835..5509da7 100644 --- a/Sources/ConfidentialCore/Models/Configuration/Configuration.swift +++ b/Sources/ConfidentialCore/Models/Configuration/Configuration.swift @@ -26,9 +26,9 @@ public struct Configuration: Equatable, Decodable { let container = try decoder.container(keyedBy: CodingKeys.self) self = .init( algorithm: try container.decode([String].self, forKey: .algorithm)[...], - defaultAccessModifier: try? container.decodeIfPresent(String.self, forKey: .defaultAccessModifier), - defaultNamespace: try? container.decodeIfPresent(String.self, forKey: .defaultNamespace), - implementationOnlyImport: try? container.decodeIfPresent(Bool.self, forKey: .implementationOnlyImport), + defaultAccessModifier: try container.decodeIfPresent(String.self, forKey: .defaultAccessModifier), + defaultNamespace: try container.decodeIfPresent(String.self, forKey: .defaultNamespace), + implementationOnlyImport: try container.decodeIfPresent(Bool.self, forKey: .implementationOnlyImport), secrets: try container.decode([Secret].self, forKey: .secrets)[...] ) } diff --git a/Tests/ConfidentialCoreTests/Models/Configuration/ConfigurationTests.swift b/Tests/ConfidentialCoreTests/Models/Configuration/ConfigurationTests.swift index 4234bd0..8964806 100644 --- a/Tests/ConfidentialCoreTests/Models/Configuration/ConfigurationTests.swift +++ b/Tests/ConfidentialCoreTests/Models/Configuration/ConfigurationTests.swift @@ -56,6 +56,49 @@ final class ConfigurationTests: XCTestCase { ) } + func test_givenInvalidJSONEncodedConfiguration_whenDecodeWithJSONDecoder_thenThrowsError() { + // given + let invalidConfigurations = [ + """ + { + "algorithm": ["compress using lz4"] + } + """, + """ + { + "secrets": [{ "name": "secret", "value": "value" }] + } + """, + """ + { + "algorithm": ["compress using lz4"], + "defaultAccessModifier": [], + "secrets": [{ "name": "secret", "value": "value" }] + } + """, + """ + { + "algorithm": ["compress using lz4"], + "defaultNamespace": [], + "secrets": [{ "name": "secret", "value": "value" }] + } + """, + """ + { + "algorithm": ["compress using lz4"], + "implementationOnlyImport": 1, + "secrets": [{ "name": "secret", "value": "value" }] + } + """ + ].map { ($0, $0.data(using: .utf8)!) } + let decoder = JSONDecoder() + + // when & then + for (json, jsonData) in invalidConfigurations { + XCTAssertThrowsError(try decoder.decode(Configuration.self, from: jsonData), json) + } + } + func test_givenSecretSingleValue_whenJSONEncodeUnderlyingValue_thenResultEqualsJSONEncodedAssociatedValue() throws { // given let associatedValue = "test"