Skip to content

Commit

Permalink
Feature/#86 unit testing share project (phase 1) (#115)
Browse files Browse the repository at this point in the history
* #86 remove unused code

* #86 adding unittest for DirectoryExensionTests

* #86 imporve check content

* #86 rename test class to share project and test check content

* #86 adding coverlet msbuild for code coverage test

* #86  gitingore encrypt file from test

* #86 test UniqueKeyGenerator

* #86 add exception to UniqueKeyGenerator

* #86 fix test

* #86 tmp allow test to check if it works

* #86 update editor config with 2 space for csproj

* #86 space intent

* #86 remove unnec tags from AES example csproj

* #86 temp ignore example test to check all tests for other os

* #86 remove old and dead cli from docker test

* #86 remove not wanted test

* #86 update text messge for ignore,
  • Loading branch information
maythamfahmi authored Oct 20, 2024
1 parent 19afd6a commit d66ed05
Show file tree
Hide file tree
Showing 15 changed files with 374 additions and 216 deletions.
14 changes: 12 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[*.cs]
[ "*.cs" ]

# CA1507: Use nameof to express symbol names
dotnet_diagnostic.CA1507.severity = none
Expand All @@ -19,6 +19,9 @@ csharp_space_around_binary_operators = before_and_after

# CS8618: Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
dotnet_diagnostic.CS8618.severity = none
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_top_level_statements = true:silent
csharp_style_prefer_primary_constructors = true:suggestion

[*.{cs,vb}]
dotnet_style_operator_placement_when_wrapping = beginning_of_line
Expand Down Expand Up @@ -85,4 +88,11 @@ dotnet_diagnostic.IDE0017.severity = none
dotnet_diagnostic.IDE0063.severity = none

# IDE0090: Use 'new(...)'
dotnet_diagnostic.IDE0090.severity = none
dotnet_diagnostic.IDE0090.severity = none

# All files
[*]
indent_style = space

[*.csproj]
indent_size = 2
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,4 @@ CoverageReport
*.vpw
*.vpwhist
*.vtg
/Resources/RsaKeys/encrypted.txt
108 changes: 54 additions & 54 deletions CryptoNet.Extensions/CryptoNet.Extensions.csproj
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>10.0</LangVersion>
<PackageId>CryptoNet.Extensions</PackageId>
<RepositoryType>git</RepositoryType>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/maythamfahmi/CryptoNet</RepositoryUrl>
<PackageProjectUrl>https://github.com/maythamfahmi/CryptoNet</PackageProjectUrl>
<ApplicationIcon>CryptoNetLogo-icon.ico</ApplicationIcon>
<PackageIcon>CryptoNetLogo-icon.png</PackageIcon>
<PackageIconUrl>https://raw.githubusercontent.com/maythamfahmi/CryptoNet/main/img/CryptoNetLogo-icon.png</PackageIconUrl>
<Company>NextBix</Company>
<Authors>Maytham Fahmi</Authors>
<MinClientVersion>1.0.0</MinClientVersion>
<Version>3.0.0</Version>
<Title>CryptoNet.Extensions</Title>
<Product>CryptoNet.Extensions</Product>
<RootNamespace>CryptoNet.Extensions</RootNamespace>
<AssemblyName>CryptoNet.Extensions</AssemblyName>
<PackageDescription>Simple and lightweight content encryption and decryption package. CryptoNet extensions methods.</PackageDescription>
<PackageTags>Encryption; Decryption; Security; Cryptography; Asymmetric; X509; RSA; AES</PackageTags>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Copyright>Copyright © 2024</Copyright>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
<_SkipUpgradeNetAnalyzersNuGetWarning>true</_SkipUpgradeNetAnalyzersNuGetWarning>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<Target Name="PrepareReleaseNotes" BeforeTargets="GenerateNuspec">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>10.0</LangVersion>
<PackageId>CryptoNet.Extensions</PackageId>
<RepositoryType>git</RepositoryType>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/maythamfahmi/CryptoNet</RepositoryUrl>
<PackageProjectUrl>https://github.com/maythamfahmi/CryptoNet</PackageProjectUrl>
<ApplicationIcon>CryptoNetLogo-icon.ico</ApplicationIcon>
<PackageIcon>CryptoNetLogo-icon.png</PackageIcon>
<PackageIconUrl>https://raw.githubusercontent.com/maythamfahmi/CryptoNet/main/img/CryptoNetLogo-icon.png</PackageIconUrl>
<Company>NextBix</Company>
<Authors>Maytham Fahmi</Authors>
<MinClientVersion>1.0.0</MinClientVersion>
<Version>3.0.0</Version>
<Title>CryptoNet.Extensions</Title>
<Product>CryptoNet.Extensions</Product>
<RootNamespace>CryptoNet.Extensions</RootNamespace>
<AssemblyName>CryptoNet.Extensions</AssemblyName>
<PackageDescription>Simple and lightweight content encryption and decryption package. CryptoNet extensions methods.</PackageDescription>
<PackageTags>Encryption; Decryption; Security; Cryptography; Asymmetric; X509; RSA; AES</PackageTags>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Copyright>Copyright © 2024</Copyright>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
<_SkipUpgradeNetAnalyzersNuGetWarning>true</_SkipUpgradeNetAnalyzersNuGetWarning>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Description>$([System.IO.File]::ReadAllText("$(MSBuildProjectDirectory)/../RELEASE-NOTES"))</Description>
</PropertyGroup>
</Target>

<Target Name="PrepareReleaseNotes" BeforeTargets="GenerateNuspec">
<PropertyGroup>
<Description>$([System.IO.File]::ReadAllText("$(MSBuildProjectDirectory)/../RELEASE-NOTES"))</Description>
</PropertyGroup>
</Target>

<ItemGroup>
<Content Include="CryptoNetLogo-icon.ico">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<Content Include="CryptoNetLogo-icon.ico">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<None Include="..\LICENSE" Pack="true" PackagePath="\" />
<None Include="..\README.md" Pack="true" PackagePath="\" />
<None Include="..\img\CryptoNetLogo-icon.png" Pack="true" PackagePath="\">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Include="..\LICENSE" Pack="true" PackagePath="\" />
<None Include="..\README.md" Pack="true" PackagePath="\" />
<None Include="..\img\CryptoNetLogo-icon.png" Pack="true" PackagePath="\">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CryptoNet.Models\CryptoNet.Models.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CryptoNet.Models\CryptoNet.Models.csproj" />
</ItemGroup>

</Project>
14 changes: 9 additions & 5 deletions CryptoNet.Models/CryptoNet.Models.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>

</Project>
17 changes: 8 additions & 9 deletions CryptoNet.Share/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,13 @@ public static class Common
EncryptedContentFile
];

#region Private methods
public static void DeleteTestFiles(string[] files)
public static bool CheckContent(string originalContent, string decryptedContent)
{
Thread.Sleep(500);
foreach (string file in files)
if (originalContent == null || decryptedContent == null)
{
File.Delete(file);
return originalContent == decryptedContent;
}
}

public static bool CheckContent(string originalContent, string decryptedContent)
{
return CalculateMd5(originalContent).Equals(CalculateMd5(decryptedContent));
}

Expand All @@ -46,6 +41,11 @@ public static string CalculateMd5(string content)

public static string UniqueKeyGenerator(string input)
{
if (string.IsNullOrEmpty(input))
{
throw new ArgumentNullException(nameof(input), "Input cannot be null or empty");
}

byte[] inputBytes = Encoding.ASCII.GetBytes(input);
byte[] hashBytes = MD5.HashData(inputBytes);

Expand Down Expand Up @@ -78,5 +78,4 @@ public static byte[] ExportPemKeyWithPassword(X509Certificate2 cert, string pass
return rsa.ExportEncryptedPkcs8PrivateKey(pass,
new PbeParameters(PbeEncryptionAlgorithm.Aes256Cbc, HashAlgorithmName.SHA256, iterationCount: 100_000));
}
#endregion
}
32 changes: 0 additions & 32 deletions CryptoNet.Share/Extensions/DebugExtension.cs

This file was deleted.

86 changes: 43 additions & 43 deletions CryptoNet.UnitTests/CryptoNet.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="ReportGenerator" Version="5.3.11" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="SharperHacks.CoreLibs.IO" Version="5.0.1" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="ReportGenerator" Version="5.3.11" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="SharperHacks.CoreLibs.IO" Version="5.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CryptoNet.Share\CryptoNet.Share.csproj" />
<ProjectReference Include="..\CryptoNet\CryptoNet.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CryptoNet.Share\CryptoNet.Share.csproj" />
<ProjectReference Include="..\CryptoNet\CryptoNet.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="Resources\RsaKeys\RsaKeys">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\TestFiles\test.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\TestFiles\test.pdf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\TestFiles\test.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\TestFiles\test.xlsx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Update="Resources\RsaKeys\RsaKeys">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\TestFiles\test.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\TestFiles\test.pdf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\TestFiles\test.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\TestFiles\test.xlsx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
7 changes: 3 additions & 4 deletions CryptoNet.UnitTests/CryptoNetRsaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ public void Encrypt_With_PublicKey_Decrypt_With_PrivateKey_Using_SelfGenerated_A

// Assert
Common.CheckContent(Common.ConfidentialDummyData, decryptWithPrivateKey).ShouldBeTrue();
Common.DeleteTestFiles(Common.DummyFiles);
}

[TestCase("test.docx")]
Expand Down Expand Up @@ -202,7 +201,7 @@ public void Encrypt_With_PublicKey_Decrypt_With_PrivateKey_Of_Content_Test()
Common.ConfidentialDummyData.ShouldBe(decryptedData);
}

[Ignore("Private")]
[Ignore("Private: This test works only on local Windows enviroment for debugging and testing. You can also put your own certifciate when debugging and testing")]
public void Encrypt_Decrypt_Using_X509_Certificate_Test()
{
// Arrange
Expand All @@ -219,7 +218,7 @@ public void Encrypt_Decrypt_Using_X509_Certificate_Test()

}

[Ignore("Private")]
[Ignore("Private: This test works only on local Windows enviroment for debugging and testing. You can also put your own certifciate when debugging and testing")]
public void Export_Public_Key_For_X509_Certificate_Test()
{
// Arrange
Expand All @@ -234,7 +233,7 @@ public void Export_Public_Key_For_X509_Certificate_Test()
publicKey.ShouldNotBeEmpty();
}

[Ignore("Private")]
[Ignore("Private: This test works only on local Windows enviroment for debugging and testing. You can also put your own certifciate when debugging and testing")]
public void Customize_PEM_Key_Encryption_Decryption_Test()
{
// Arrange
Expand Down
Loading

0 comments on commit d66ed05

Please sign in to comment.