Skip to content

Commit 8f76297

Browse files
committed
refactor some classes and files and add a test for catch block to test sonarcloud coverage
1 parent cbfe6d5 commit 8f76297

6 files changed

Lines changed: 20 additions & 8 deletions

File tree

csharp_cryptoexamples/src/ExampleExampleStringEncryptionPasswordBased.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static String DemonstrateStringEncryptionPasswordBased(String plainText,
2323
{
2424
//----------------------------Encrypt----------------------------
2525
//If no password is provided througth the user, use the generated key from the AesManaged class.
26-
if (password != null && password.Length != 0)
26+
if (!string.IsNullOrEmpty(password))
2727
{
2828
//Generating random salt
2929
byte[] salt = new byte[32];
File renamed without changes.

csharp_cryptoexamples/src/ExampleSignature.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace com.cryptoexamples.csharp
77
{
8-
public static class ExampleSigning
8+
public static class ExampleSignature
99
{
1010
public static void Main()
1111
{

csharp_cryptoexamples/src/ExampleStringEncryptionKeyBased.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace com.cryptoexamples.csharp
77
{
8-
public static class ExampleStringEncryptionKeyBasedInOneMethod
8+
public static class ExampleStringEncryptionKeyBased
99
{
1010
public static void Main()
1111
{

csharp_cryptoexamples/src/ExmapleKeyStorageProvider.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using System;
22
using System.Security.Cryptography;
3+
using Serilog;
34

45
namespace csharp_cryptoexamples.src
56
{
67
public static class ExmapleKeyStorageProvider
78
{
89
public static void Main()
910
{
11+
Log.Logger = new LoggerConfiguration().WriteTo.Console().CreateLogger();
1012
DemonstrateKeyStorage("MyKeyContainerName");
1113
}
1214

@@ -29,10 +31,18 @@ public static RSACryptoServiceProvider DemonstrateKeyStorage(String ContainerNam
2931
KeyContainerName = ContainerName
3032
};
3133

32-
// Create a new instance of RSACryptoServiceProvider that accesses
33-
// the key container MyKeyContainerName.
34-
RSACryptoServiceProvider rsa2 = new RSACryptoServiceProvider(getParametersFromKSP);
34+
RSACryptoServiceProvider rsa2 = null;
3535

36+
try
37+
{
38+
// Create a new instance of RSACryptoServiceProvider that accesses
39+
// the key container MyKeyContainerName.
40+
rsa2 = new RSACryptoServiceProvider(getParametersFromKSP);
41+
}
42+
catch (Exception e)
43+
{
44+
Log.Error(e.Message);
45+
}
3646
return rsa2;
3747
}
3848
}

csharp_cryptoexamplesTest/csharp_cryptoexamplesTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public void StringEncryptionPasswordBasedTest()
3434
// Basic test if encryption and decryption is working.
3535
ExampleStringEncryptionPasswordBased.Main();
3636
Assert.Contains("They are the same: True", outMessage.ToString());
37+
ExampleStringEncryptionPasswordBased.DemonstrateStringEncryptionPasswordBased(null, null);
38+
Assert.Contains("Error", outMessage.ToString());
3739
}
3840

3941
[Fact]
@@ -54,7 +56,7 @@ public void HashingTest()
5456
public void SigningTest()
5557
{
5658
Refresh();
57-
ExampleSigning.Main();
59+
ExampleSignature.Main();
5860
Assert.Contains("The data was verified.", outMessage.ToString());
5961
}
6062

@@ -63,7 +65,7 @@ public void StringEncryptionKeyBasedTest()
6365
{
6466
Refresh();
6567
// Basic test if encryption and decryption is working.
66-
ExampleStringEncryptionKeyBasedInOneMethod.Main();
68+
ExampleStringEncryptionKeyBased.Main();
6769
Assert.Contains("They are the same: True", outMessage.ToString());
6870
}
6971

0 commit comments

Comments
 (0)