Skip to content

Commit

Permalink
Merge pull request #17 from PandaTechAM/development
Browse files Browse the repository at this point in the history
gzip serializer fix
  • Loading branch information
HaikAsatryan authored Mar 6, 2024
2 parents 9765b56 + 99bda80 commit d471a4b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 16 deletions.
35 changes: 28 additions & 7 deletions src/Pandatech.Crypto.Tests/GZipTests.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
using System.Text;
using System.Text.Json.Serialization;

namespace Pandatech.Crypto.Tests;

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]
public void CompressAndDecompress_ShouldReturnOriginalObject()
{
var originalObject = new TestClass
{
Id = 1,
Name = "Test"
SomeLongId = 1,
FullName = "Test"
};

// Act
Expand All @@ -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<TestClass>(stringData);

// Assert
Assert.NotNull(decompressedObject);
Assert.Equal(originalObject.SomeLongId, decompressedObject.SomeLongId);
Assert.Equal(originalObject.FullName, decompressedObject.FullName);
}

[Fact]
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions src/Pandatech.Crypto.Tests/HostBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Argon2IdOptions>());
Assert.NotNull(serviceProvider.GetService<Argon2Id>());
}

}
24 changes: 17 additions & 7 deletions src/Pandatech.Crypto/GZip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>(T obj)
{
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
var jsonString = JsonSerializer.Serialize(obj, options);
var jsonString = JsonSerializer.Serialize(obj, JsonSerializerOptions);
return Compress(jsonString);
}

Expand Down Expand Up @@ -45,8 +47,16 @@ public static byte[] Compress(byte[] data)

public static T? Decompress<T>(byte[] compressedData)
{
var jsonString = Decompress(compressedData);
return JsonSerializer.Deserialize<T>(jsonString);
var decompressed = Decompress(compressedData);
var jsonString = Encoding.UTF8.GetString(decompressed);
return JsonSerializer.Deserialize<T>(jsonString, JsonSerializerOptions);
}

public static T? Decompress<T>(string compressedData)
{
var decompressed = Decompress(compressedData);
var jsonString = Encoding.UTF8.GetString(decompressed);
return JsonSerializer.Deserialize<T>(jsonString, JsonSerializerOptions);
}

public static byte[] Decompress(string compressedBase64)
Expand Down
4 changes: 2 additions & 2 deletions src/Pandatech.Crypto/Pandatech.Crypto.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
<Copyright>MIT</Copyright>
<PackageIcon>pandatech.png</PackageIcon>
<PackageReadmeFile>Readme.md</PackageReadmeFile>
<Version>2.3.0</Version>
<Version>2.3.1</Version>
<Title>Pandatech.Crypto</Title>
<PackageTags>Pandatech, library, encryption, hash, algorythms, security</PackageTags>
<Description>PandaTech.Crypto is a .NET library simplifying common cryptograhic functions.</Description>
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-pandatech-crypto</RepositoryUrl>
<PackageReleaseNotes>Gzip underlying implementation change and Password symbols</PackageReleaseNotes>
<PackageReleaseNotes>Gzip serializer fix</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit d471a4b

Please sign in to comment.