diff --git a/src/Pandatech.Crypto.Tests/GZipTests.cs b/src/Pandatech.Crypto.Tests/GZipTests.cs index 9b22a17..8ccafdb 100644 --- a/src/Pandatech.Crypto.Tests/GZipTests.cs +++ b/src/Pandatech.Crypto.Tests/GZipTests.cs @@ -1,4 +1,5 @@ using System.Text; +using System.Text.Json.Serialization; namespace Pandatech.Crypto.Tests; @@ -6,8 +7,8 @@ public class GZipTests { private class TestClass { - public int Id { get; init; } - public string? Name { get; init; } + public int SomeLongId { get; init; } + public string? FullName { get; init; } } [Fact] @@ -15,8 +16,8 @@ public void CompressAndDecompress_ShouldReturnOriginalObject() { var originalObject = new TestClass { - Id = 1, - Name = "Test" + SomeLongId = 1, + FullName = "Test" }; // Act @@ -25,8 +26,28 @@ public void CompressAndDecompress_ShouldReturnOriginalObject() // Assert Assert.NotNull(decompressedObject); - Assert.Equal(originalObject.Id, decompressedObject.Id); - Assert.Equal(originalObject.Name, decompressedObject.Name); + Assert.Equal(originalObject.SomeLongId, decompressedObject.SomeLongId); + Assert.Equal(originalObject.FullName, decompressedObject.FullName); + } + + [Fact] + public void CompressAndDecompress_ShouldReturnOriginalObject2() + { + var originalObject = new TestClass + { + SomeLongId = 1, + FullName = "Test" + }; + + // Act + var compressedData = GZip.Compress(originalObject); + var stringData = Convert.ToBase64String(compressedData); + var decompressedObject = GZip.Decompress(stringData); + + // Assert + Assert.NotNull(decompressedObject); + Assert.Equal(originalObject.SomeLongId, decompressedObject.SomeLongId); + Assert.Equal(originalObject.FullName, decompressedObject.FullName); } [Fact] @@ -94,7 +115,7 @@ public void CompressAndDecompress_ByteArray_ReturnsOriginalData() { // Arrange var input = Encoding.UTF8.GetBytes("Sample text for compression"); - + // Act var compressed = GZip.Compress(input); var decompressed = GZip.Decompress(compressed); diff --git a/src/Pandatech.Crypto.Tests/HostBuilderTests.cs b/src/Pandatech.Crypto.Tests/HostBuilderTests.cs index 45cb08f..18395b8 100644 --- a/src/Pandatech.Crypto.Tests/HostBuilderTests.cs +++ b/src/Pandatech.Crypto.Tests/HostBuilderTests.cs @@ -90,5 +90,21 @@ public void AddPandatechCryptoArgon2Id_RegistersAsSingleton() Assert.Same(argon2IdInstance1, argon2IdInstance2); } + + + [Fact] + public void AddPandatechCryptoArgon2Id_RegistersServicesCorrectly2() + { + // Arrange + var services = new ServiceCollection(); + + // Act + services.AddPandatechCryptoArgon2Id(); + + // Assert + var serviceProvider = services.BuildServiceProvider(); + Assert.NotNull(serviceProvider.GetService()); + Assert.NotNull(serviceProvider.GetService()); + } } \ No newline at end of file diff --git a/src/Pandatech.Crypto/GZip.cs b/src/Pandatech.Crypto/GZip.cs index 4902aff..598fdbd 100644 --- a/src/Pandatech.Crypto/GZip.cs +++ b/src/Pandatech.Crypto/GZip.cs @@ -2,17 +2,19 @@ using System.Text; using System.Text.Json; + + namespace Pandatech.Crypto; public static class GZip { + private static readonly JsonSerializerOptions JsonSerializerOptions = new() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + }; public static byte[] Compress(T obj) { - var options = new JsonSerializerOptions - { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase - }; - var jsonString = JsonSerializer.Serialize(obj, options); + var jsonString = JsonSerializer.Serialize(obj, JsonSerializerOptions); return Compress(jsonString); } @@ -45,8 +47,16 @@ public static byte[] Compress(byte[] data) public static T? Decompress(byte[] compressedData) { - var jsonString = Decompress(compressedData); - return JsonSerializer.Deserialize(jsonString); + var decompressed = Decompress(compressedData); + var jsonString = Encoding.UTF8.GetString(decompressed); + return JsonSerializer.Deserialize(jsonString, JsonSerializerOptions); + } + + public static T? Decompress(string compressedData) + { + var decompressed = Decompress(compressedData); + var jsonString = Encoding.UTF8.GetString(decompressed); + return JsonSerializer.Deserialize(jsonString, JsonSerializerOptions); } public static byte[] Decompress(string compressedBase64) diff --git a/src/Pandatech.Crypto/Pandatech.Crypto.csproj b/src/Pandatech.Crypto/Pandatech.Crypto.csproj index bb006eb..f61e7ba 100644 --- a/src/Pandatech.Crypto/Pandatech.Crypto.csproj +++ b/src/Pandatech.Crypto/Pandatech.Crypto.csproj @@ -8,12 +8,12 @@ MIT pandatech.png Readme.md - 2.3.0 + 2.3.1 Pandatech.Crypto Pandatech, library, encryption, hash, algorythms, security PandaTech.Crypto is a .NET library simplifying common cryptograhic functions. https://github.com/PandaTechAM/be-lib-pandatech-crypto - Gzip underlying implementation change and Password symbols + Gzip serializer fix