From 731d1b6a8b0b4206bd968afd7db0f6b4fa6fc7f7 Mon Sep 17 00:00:00 2001 From: Alberto Spelta Date: Tue, 17 Sep 2024 13:31:51 +0000 Subject: [PATCH] Fix to load unobfuscated values during dictionary deserialization (#63) --- .../ObfuscationDictionary.cs | 12 ++---------- src/Dax.Vpax.Obfuscator/DaxModelObfuscator.cs | 5 ++--- src/Directory.Build.props | 2 +- .../ObfuscationDictionaryTests.cs | 9 +++++---- .../DaxModelObfuscatorTests.cs | 2 +- 5 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/Dax.Vpax.Obfuscator.Common/ObfuscationDictionary.cs b/src/Dax.Vpax.Obfuscator.Common/ObfuscationDictionary.cs index 4a7f94f..655d49a 100644 --- a/src/Dax.Vpax.Obfuscator.Common/ObfuscationDictionary.cs +++ b/src/Dax.Vpax.Obfuscator.Common/ObfuscationDictionary.cs @@ -9,16 +9,8 @@ public sealed class ObfuscationDictionary private readonly Dictionary _values; private readonly Dictionary _obfuscated; - public ObfuscationDictionary(string id, string version, IEnumerable texts, IEnumerable unobfuscatedValues) - : this(id, version, texts) - { - if (unobfuscatedValues == null) throw new ArgumentNullException(nameof(unobfuscatedValues)); - - UnobfuscatedValues = unobfuscatedValues.Distinct(StringComparer.OrdinalIgnoreCase).ToList(); - } - [JsonConstructor] - public ObfuscationDictionary(string id, string version, IEnumerable texts) + public ObfuscationDictionary(string id, string version, IEnumerable texts, IEnumerable unobfuscatedValues) { if (id == null || !Guid.TryParseExact(id, "D", out var guid) || guid == Guid.Empty) throw new ArgumentException("The dictionary identifier is not valid.", nameof(id)); if (version == null) throw new ArgumentNullException(nameof(version)); @@ -27,7 +19,7 @@ public ObfuscationDictionary(string id, string version, IEnumerable t.Value).ToArray(); - UnobfuscatedValues = Array.Empty(); + UnobfuscatedValues = unobfuscatedValues?.ToArray() ?? []; // Create dictionaries to enable fast lookups and ensure key uniqueness. An error will be thrown if duplicate keys are detected. _values = Texts.ToDictionary((text) => text.Value, StringComparer.OrdinalIgnoreCase); diff --git a/src/Dax.Vpax.Obfuscator/DaxModelObfuscator.cs b/src/Dax.Vpax.Obfuscator/DaxModelObfuscator.cs index b7ddff0..7876e6c 100644 --- a/src/Dax.Vpax.Obfuscator/DaxModelObfuscator.cs +++ b/src/Dax.Vpax.Obfuscator/DaxModelObfuscator.cs @@ -48,10 +48,9 @@ public ObfuscationDictionary Obfuscate() var id = Model.ObfuscatorDictionaryId; var version = VpaxObfuscator.Version; var texts = Texts.Select((t) => t.ToObfuscationText()); + var unobfuscatedValues = Options.TrackUnobfuscated ? UnobfuscatedValues.Distinct(StringComparer.OrdinalIgnoreCase) : []; - return Options.TrackUnobfuscated - ? new ObfuscationDictionary(id, version, texts, UnobfuscatedValues) - : new ObfuscationDictionary(id, version, texts); + return new ObfuscationDictionary(id, version, texts, unobfuscatedValues); } private void ObfuscateIdentifiers(Table table) diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 02b7b84..71f539a 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ enable - latest + 12.0 enable $(MSBuildThisFileDirectory)artifacts true diff --git a/tests/Dax.Vpax.Obfuscator.Common.Tests/ObfuscationDictionaryTests.cs b/tests/Dax.Vpax.Obfuscator.Common.Tests/ObfuscationDictionaryTests.cs index ffaa91a..0193dbe 100644 --- a/tests/Dax.Vpax.Obfuscator.Common.Tests/ObfuscationDictionaryTests.cs +++ b/tests/Dax.Vpax.Obfuscator.Common.Tests/ObfuscationDictionaryTests.cs @@ -7,14 +7,15 @@ public class ObfuscationDictionaryTests [Fact] public void ctor_EmptyTexts_ReturnsEmptyDictionary() { - var dictionary = new ObfuscationDictionary(id: Guid.NewGuid().ToString("D"), "0.0.0-test", texts: []); + var dictionary = new ObfuscationDictionary(id: Guid.NewGuid().ToString("D"), "0.0.0-test", texts: [], unobfuscatedValues: []); Assert.Empty(dictionary.Texts); + Assert.Empty(dictionary.UnobfuscatedValues); } [Fact] public void ctor_EmptyId_Throws() { - var exception = Assert.Throws(() => new ObfuscationDictionary(id: Guid.Empty.ToString("D"), "0.0.0-test", texts: [])); + var exception = Assert.Throws(() => new ObfuscationDictionary(id: Guid.Empty.ToString("D"), "0.0.0-test", texts: [], unobfuscatedValues: [])); Assert.StartsWith("The dictionary identifier is not valid.", exception.Message); } @@ -27,7 +28,7 @@ public void ctor_DuplicateTexts_Throws() new ObfuscationText("VALUE", "XXXXXX"), }; - var exception = Assert.Throws(() => new ObfuscationDictionary(id: Guid.NewGuid().ToString("D"), "0.0.0-test", texts)); + var exception = Assert.Throws(() => new ObfuscationDictionary(id: Guid.NewGuid().ToString("D"), "0.0.0-test", texts, unobfuscatedValues: [])); Assert.StartsWith("An item with the same key has already been added.", exception.Message); } @@ -128,6 +129,6 @@ private static ObfuscationDictionary CreateTestDictionary() private static ObfuscationDictionary CreateDictionary(ObfuscationText[] texts) { - return new ObfuscationDictionary(id: Guid.NewGuid().ToString("D"), "0.0.0-test", texts); + return new ObfuscationDictionary(id: Guid.NewGuid().ToString("D"), "0.0.0-test", texts, unobfuscatedValues: []); } } diff --git a/tests/Dax.Vpax.Obfuscator.Tests/DaxModelObfuscatorTests.cs b/tests/Dax.Vpax.Obfuscator.Tests/DaxModelObfuscatorTests.cs index c3eef8a..9a5667d 100644 --- a/tests/Dax.Vpax.Obfuscator.Tests/DaxModelObfuscatorTests.cs +++ b/tests/Dax.Vpax.Obfuscator.Tests/DaxModelObfuscatorTests.cs @@ -521,7 +521,7 @@ private static DaxModelObfuscator CreateObfuscator(DaxText[] texts, Model? model private static DaxModelDeobfuscator CreateDeobfuscator(IEnumerable texts) { var obfuscationTexts = texts.Select((t) => t.ToObfuscationText()).ToArray(); - var dictionary = new ObfuscationDictionary(id: Guid.NewGuid().ToString("D"), "0.0.0-test", obfuscationTexts); + var dictionary = new ObfuscationDictionary(id: Guid.NewGuid().ToString("D"), "0.0.0-test", obfuscationTexts, unobfuscatedValues: []); return CreateDeobfuscator(dictionary); }