Skip to content

Commit

Permalink
Bugfix/#110 windows ci issue fixing part 2 (#112)
Browse files Browse the repository at this point in the history
* #110 temp disabled

* #110 docker ignore

* #110 temp changes

* #110 test

* #110

* #110 clean up and imporve test

* #110 refactoring

* [Bug]: Windows ci pipeline breaks #110 revert ci and code coverage
  • Loading branch information
maythamfahmi authored Oct 19, 2024
1 parent c8518ab commit 19afd6a
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 179 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bin/
obj/
.git/
*.txt # If you want to exclude all .txt files
run_codecoverage.txt # Exclude this specific file
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ on:
- "main"
- "version3"
- "feature/*"
- "bugfix/*"
- "hotfix/*"
- "bugfix/*"
- "hotfix/*"
- "!feature/ci*"
paths-ignore:
- "**/README.md"
Expand Down
108 changes: 54 additions & 54 deletions .github/workflows/code-coverage.yml
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
# name: "Code Coverage"
name: "Code Coverage"

# on:
# push:
# branches:
# - "main"
# - "version3"
# - "feature/*"
# - "bugfix/*"
# - "hotfix/*"
on:
push:
branches:
- "main"
- "version3"
- "feature/*"
- "bugfix/*"
- "hotfix/*"

# jobs:
# build:
# runs-on: ubuntu-latest
jobs:
build:
runs-on: ubuntu-latest

# steps:
# - name: Checkout repository
# uses: actions/checkout@v4
steps:
- name: Checkout repository
uses: actions/checkout@v4

# - name: Setup .NET
# uses: actions/setup-dotnet@v4
# with:
# dotnet-version: 8.0.x
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x

# - name: Install ReportGenerator tool
# run: dotnet tool install --global dotnet-reportgenerator-globaltool
- name: Install ReportGenerator tool
run: dotnet tool install --global dotnet-reportgenerator-globaltool

# - name: Restore dependencies
# run: dotnet restore
- name: Restore dependencies
run: dotnet restore

# - name: Build the solution
# run: dotnet build --no-restore
- name: Build the solution
run: dotnet build --no-restore

# - name: Run tests and collect code coverage
# run: dotnet test --no-build --collect:"XPlat Code Coverage"
- name: Run tests and collect code coverage
run: dotnet test --no-build --collect:"XPlat Code Coverage"

# - name: Generate code coverage report
# run: reportgenerator -reports:"**/coverage.cobertura.xml" -targetdir:"coverage" -reporttypes:"HtmlInline_AzurePipelines;Badges"
- name: Generate code coverage report
run: reportgenerator -reports:"**/coverage.cobertura.xml" -targetdir:"coverage" -reporttypes:"HtmlInline_AzurePipelines;Badges"

# - name: Upload coverage report
# uses: actions/upload-artifact@v4
# with:
# name: code-coverage-report
# path: coverage
- name: Upload coverage report
uses: actions/upload-artifact@v4
with:
name: code-coverage-report
path: coverage

# - name: Upload coverage badge
# uses: actions/upload-artifact@v4
# with:
# name: code-coverage-badge
# path: coverage/badge_linecoverage.svg
- name: Upload coverage badge
uses: actions/upload-artifact@v4
with:
name: code-coverage-badge
path: coverage/badge_linecoverage.svg

# - name: Create coverage badge
# run: cp coverage/badge_linecoverage.svg ./coverage-badge.svg
- name: Create coverage badge
run: cp coverage/badge_linecoverage.svg ./coverage-badge.svg

# - name: Print Directory Structure
# uses: FiorelaCiroku/XDTesting-Print-Directory-Structure@v1.0.2
- name: Print Directory Structure
uses: FiorelaCiroku/XDTesting-Print-Directory-Structure@v1.0.2

# - name: Commit coverage badge
# run: |
# git config --global user.name 'github-actions[bot]'
# git config --global user.email 'github-actions[bot]@users.noreply.github.com'
# git add coverage-badge.svg
# if git diff-index --cached --name-only HEAD | grep -vsxF coverage-badge.svg; then
# git commit -m 'Update coverage badge'
# git push
# fi
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Commit coverage badge
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git add coverage-badge.svg
if git diff-index --cached --name-only HEAD | grep -vsxF coverage-badge.svg; then
git commit -m 'Update coverage badge'
git push
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
42 changes: 37 additions & 5 deletions CryptoNet.Share/Common.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using CryptoNet.Share.Extensions;

Expand All @@ -17,13 +18,9 @@ public static class Common

public static readonly string RsaStoredKeyPair = Path.Combine(RsaKeysPath, "RsaKeys");
public static readonly string EncryptedContentFile = Path.Combine(RsaKeysPath, "encrypted.txt");
public static readonly string PrivateKeyFile = Path.Combine(RsaKeysPath, "privateKey");
public static readonly string PublicKeyFile = Path.Combine(RsaKeysPath, "publicKey.pub");
public static readonly string[] DummyFiles =
[
EncryptedContentFile,
PublicKeyFile,
PrivateKeyFile
EncryptedContentFile
];

#region Private methods
Expand All @@ -46,5 +43,40 @@ public static string CalculateMd5(string content)
var hash = MD5.HashData(Encoding.UTF8.GetBytes(content));
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}

public static string UniqueKeyGenerator(string input)
{
byte[] inputBytes = Encoding.ASCII.GetBytes(input);
byte[] hashBytes = MD5.HashData(inputBytes);

var stringBuilder = new StringBuilder();
foreach (var byteValue in hashBytes)
{
stringBuilder.Append(byteValue.ToString("X2"));
}
return stringBuilder.ToString();
}

public static char[] ExportPemKey(X509Certificate2 cert, bool privateKey = true)
{
AsymmetricAlgorithm rsa = cert.GetRSAPrivateKey()!;

if (privateKey)
{
byte[] priKeyBytes = rsa.ExportPkcs8PrivateKey();
return PemEncoding.Write("PRIVATE KEY", priKeyBytes);
}

byte[] pubKeyBytes = rsa.ExportSubjectPublicKeyInfo();
return PemEncoding.Write("PUBLIC KEY", pubKeyBytes);
}

public static byte[] ExportPemKeyWithPassword(X509Certificate2 cert, string password)
{
AsymmetricAlgorithm rsa = cert.GetRSAPrivateKey()!;
byte[] pass = Encoding.UTF8.GetBytes(password);
return rsa.ExportEncryptedPkcs8PrivateKey(pass,
new PbeParameters(PbeEncryptionAlgorithm.Aes256Cbc, HashAlgorithmName.SHA256, iterationCount: 100_000));
}
#endregion
}
6 changes: 0 additions & 6 deletions CryptoNet.UnitTests/CryptoNet.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@
<None Update="Resources\RsaKeys\RsaKeys">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\RsaKeys\publicKey.pub">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\RsaKeys\privateKey">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\TestFiles\test.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
42 changes: 13 additions & 29 deletions CryptoNet.UnitTests/CryptoNetAesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ namespace CryptoNet.UnitTests;
[TestFixture]
public class CryptoNetAesTests
{
private const string ConfidentialData = @"Some Secret Data";
private static readonly string BaseFolder = AppDomain.CurrentDomain.BaseDirectory;
private static readonly string SymmetricKeyFile = Path.Combine(BaseFolder, $"{KeyType.SymmetricKey}.xml");
private static readonly byte[] symmetricKey = Encoding.UTF8.GetBytes("b14ca5898a4e4133bbce2ea2315a1916");
Expand All @@ -46,11 +45,11 @@ public void Encrypt_And_Decrypt_With_SymmetricKey_Test()
var cryptoNetAes = new CryptoNetAes(symmetricKey, iv);

// Act
var encryptedData = cryptoNetAes.EncryptFromString(ConfidentialData);
var encryptedData = cryptoNetAes.EncryptFromString(Common.ConfidentialDummyData);
var decryptedData = cryptoNetAes.DecryptToString(encryptedData);

// Assert
ConfidentialData.ShouldBe(decryptedData);
Common.ConfidentialDummyData.ShouldBe(decryptedData);
cryptoNetAes.Info.KeyType.ShouldBe(KeyType.SymmetricKey);
cryptoNetAes.Info.KeyType.ShouldNotBe(KeyType.PublicKey);
cryptoNetAes.Info.KeyType.ShouldNotBe(KeyType.PrivateKey);
Expand All @@ -66,7 +65,7 @@ public void Encrypt_And_Decrypt_With_Wrong_SymmetricKey_Should_Throw_Exception()
var wrongKey = Encoding.UTF8.GetBytes("b14ca5898a4e4133bbce2ea2315b1916");
var iv = new byte[16];
var cryptoNetAes = new CryptoNetAes(correctKey, iv);
var encryptedData = cryptoNetAes.EncryptFromString(ConfidentialData);
var encryptedData = cryptoNetAes.EncryptFromString(Common.ConfidentialDummyData);

// Act & Assert
Assert.Throws<CryptographicException>(() =>
Expand Down Expand Up @@ -123,11 +122,11 @@ public void Encrypt_And_Decrypt_Content_With_SelfGenerated_SymmetricKey_Test()
var cryptoNetAes = new CryptoNetAes(key);

// Act
var encryptedData = cryptoNetAes.EncryptFromString(ConfidentialData);
var encryptedData = cryptoNetAes.EncryptFromString(Common.ConfidentialDummyData);
var decryptedData = cryptoNetAes.DecryptToString(encryptedData);

// Assert
ConfidentialData.ShouldBe(decryptedData);
Common.ConfidentialDummyData.ShouldBe(decryptedData);
}

[Test]
Expand All @@ -139,12 +138,12 @@ public void SelfGenerated_And_Save_SymmetricKey_Test()

// Act
cryptoNet.SaveKey(file);
var encryptedData = cryptoNet.EncryptFromString(ConfidentialData);
var encryptedData = cryptoNet.EncryptFromString(Common.ConfidentialDummyData);
var decryptedData = new CryptoNetAes(file).DecryptToString(encryptedData);

// Assert
File.Exists(file.FullName).ShouldBeTrue();
ConfidentialData.ShouldBe(decryptedData);
Common.ConfidentialDummyData.ShouldBe(decryptedData);
}

[Test]
Expand All @@ -158,43 +157,29 @@ public void Encrypt_And_Decrypt_Content_With_Own_SymmetricKey_Test()
var cryptoNetAes = new CryptoNetAes(keyBytes, ivBytes);

// Act
var encryptedData = cryptoNetAes.EncryptFromString(ConfidentialData);
var encryptedData = cryptoNetAes.EncryptFromString(Common.ConfidentialDummyData);
var decryptedData = cryptoNetAes.DecryptToString(encryptedData);

// Assert
ConfidentialData.ShouldBe(decryptedData);
Common.ConfidentialDummyData.ShouldBe(decryptedData);
}

[Test]
public void Encrypt_And_Decrypt_With_Human_Readable_Key_And_Secret_SymmetricKey_Test()
{
// Arrange
var key = UniqueKeyGenerator("symmetricKey");
var iv = new string(UniqueKeyGenerator("password").Take(16).ToArray());
var key = Common.UniqueKeyGenerator("symmetricKey");
var iv = new string(Common.UniqueKeyGenerator("password").Take(16).ToArray());
var keyBytes = Encoding.UTF8.GetBytes(key);
var ivBytes = Encoding.UTF8.GetBytes(iv);
var cryptoNetAes = new CryptoNetAes(keyBytes, ivBytes);

// Act
var encryptedData = cryptoNetAes.EncryptFromString(ConfidentialData);
var encryptedData = cryptoNetAes.EncryptFromString(Common.ConfidentialDummyData);
var decryptedData = cryptoNetAes.DecryptToString(encryptedData);

// Assert
ConfidentialData.ShouldBe(decryptedData);
}

// Helper method
private static string UniqueKeyGenerator(string input)
{
byte[] inputBytes = Encoding.ASCII.GetBytes(input);
byte[] hashBytes = MD5.HashData(inputBytes);

var stringBuilder = new StringBuilder();
foreach (var byteValue in hashBytes)
{
stringBuilder.Append(byteValue.ToString("X2"));
}
return stringBuilder.ToString();
Common.ConfidentialDummyData.ShouldBe(decryptedData);
}

[Test]
Expand Down Expand Up @@ -231,6 +216,5 @@ public void DecryptContent_Throws_ArgumentNullException()
Assert.Throws<ArgumentNullException>(() => encoder.DecryptToBytes(null!));
Assert.Throws<ArgumentNullException>(() => encoder.DecryptToBytes(new byte[0]));
}

}

Loading

0 comments on commit 19afd6a

Please sign in to comment.