Skip to content

Commit

Permalink
Update RSA params tests
Browse files Browse the repository at this point in the history
  • Loading branch information
poulad committed Sep 5, 2018
1 parent 0cb7780 commit 5e621a6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# http://EditorConfig.org
# https://www.jetbrains.com/help/rider/EditorConfig_Properties.html

# This file is the top-most EditorConfig file
root = true
Expand All @@ -18,6 +19,8 @@ csharp_align_multiline_parameter = true
csharp_align_multiline_extends_list = true
csharp_align_linq_query = true
csharp_place_attribute_on_same_line = false
csharp_empty_block_style = together_same_line
csharp_space_within_single_line_array_initializer_braces = true

# Solution Files
[*.sln]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public async Task Should_Generate_Auth_Link()
new PassportScopeElementOneOfSeveral(new[]
{
new PassportScopeElementOne(PassportEnums.Scope.IdentityCard),
new PassportScopeElementOne(PassportEnums.Scope.Passport),
new PassportScopeElementOne(PassportEnums.Scope.InternalPassport),
})
{
Selfie = true, // selfie can only be requested for documents used as proof of identity
Expand Down
63 changes: 41 additions & 22 deletions test/UnitTests/Decryption/Private Key Import Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Reflection;
using System.Security.Cryptography;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
Expand Down Expand Up @@ -110,7 +111,7 @@ public void Should_Read_RSA_Parameters_From_Pem()
RSAParameters parameters;
{
PemReader pemReader = new PemReader(new System.IO.StringReader(PrivateKeyPem));
AsymmetricCipherKeyPair keyPair = (AsymmetricCipherKeyPair)pemReader.ReadObject();
AsymmetricCipherKeyPair keyPair = (AsymmetricCipherKeyPair) pemReader.ReadObject();
parameters = DotNetUtilities.ToRSAParameters(keyPair.Private as RsaPrivateCrtKeyParameters);
}

Expand All @@ -124,34 +125,41 @@ public void Should_Read_RSA_Parameters_From_Pem()
Assert.Equal(inverseQ, parameters.InverseQ);
}

[Fact(DisplayName = "Should JSON-serialize RSAParameters and check whether fields are serializable on this platform")]
[Fact(DisplayName =
"Should JSON-serialize RSAParameters and check whether fields are serializable on this platform")]
public void Should_JSON_Serialize_RSAParameters_If_Supported()
{
bool areAllFieldsSerilizable = typeof(RSAParameters)
.GetField(nameof(RSAParameters.P))
.GetCustomAttribute<NonSerializedAttribute>() == null;
bool areAllFieldsSerializable = typeof(RSAParameters)
.GetField(nameof(RSAParameters.P))
.GetCustomAttribute<NonSerializedAttribute>() == null;

if (!areAllFieldsSerilizable)
if (!areAllFieldsSerializable)
{
_output.WriteLine("Some fields of RSAParameters struct are NOT serializable on this platform.");
}
string expectedJson = areAllFieldsSerilizable

string expectedJson = areAllFieldsSerializable
? @"{""D"":""nrXEeOl2Ky3iHu0679sqJRg0ZZnwM32Tp6IFxNGJEX6Liysm+Gl3GP4cwbi8tCo1DaPc7HNY5Ol9AkyB1H+tDZyTyr+N1fSR1B6aJwHz22a6XUcavct1WTqOicg4p2Yr9dx//R+bkp1NRKhnoHi+14BG1imaLB6/JQ8muwUZY8Ug/KMMTiVKKZZBUhK0P0/81Ij8Bb5uO65Av0XJzz55Yde0944OZKNZLPM6qPed0XPpQFdA6jjUZzn6bjNJrEnaB6UaNXay0cFCHYfqJD+bkyOMR3VjKY8ldgEt2iw5HYeeadSJlzvW4CNQ/xuvCfdw/T6+2aPN3+RnXl4CbuQJIQ=="",""DP"":""KZYZWbsy/boJSS3Z8N5tkeA19cq54KrSjZOJEnJJ4tyOUcr/3AXzixOANqbvK3jIg/qaTPHNOCdVVe8rWEkWPy8m2DXewst0EP5yJQ4KY++fRhhr9wVPIWBiGZng9HXu1bzCC7k9CuC7ccnhKN0jYRow3l/BmmZ93j1aFr/lk60="",""DQ"":""Y25KgzPjR0Ej902TINX5EjuoVGr0wdpnYC2w7AeeGwTqvA/CoXhb9++99DlifCZGfmz2Yr7daG/wEzSMgK/fSEUnTkSsVmdonpQ656hQM0cB6E37lmrRfRzphzp6U3/6ZwWYrHgXA5/HwX3FGi6hPeaPoB9bOGWHFbQIULAdBd0="",""Exponent"":""AQAB"",""InverseQ"":""0153qXwUkMN3FQRlWI8GwcUMaC/RgVEzIY0WkH6lAVhiVTEmsYm/unQG2jCAFcNAyDChvLU2DRIxKchrh70obeL2UYGqwjag5NdxCmPNC5Mjr9h/ydNa7YGmZOvh+tlucxpyG7c6HQdp+yWrCuetSHP0HKxLYXguEJq/kV3tN6Y="",""Modulus"":""0VElWoQA2SK1csG2/sY/wlssO1bjXRx+t+JlIgS6jLPCefyCAcZBv7ElcSPJQIPEXNwN2XdnTc2wEIjZ8bTgBlBqXppj471bJeX8Mi2uAxAqOUDuvGuqth+mq7DMqol3MNH5P9FO6li7nZxI1FX39u2r/4H4PXRiWx13gsVQRL6Clq2jcXFHc9CvNaCQEJX95jgQFAybal216EwlnnVVgiT/TNsfFjW41XJZsHUny9k+dAfyPzqAk54cgrvjgAHJayDWjapq90Fm/+e/DVQ6BHGkV0POQMkkBrvvhAIQu222j+03frm9b2yZrhX/qS01lyjW4VaQytGV0wlewV6BFw=="",""P"":""56MdiwsGari+fhab1/laCiJfml0HQPvDFl5mCvOxQzJ1t1u49j+cHIZyUVrbG6GSW7X8mJEKLnx8ZOEWqNkyLc71f7uSWytF2gW5qyY84JUQ4LS4UXz6nnjJbnwS776f9GqFQQon3MqSAT9KsDMXlymZk9Wz5TJaWn6i7FSwDaM="",""Q"":""51UN2sd3x/w/qArIRFoPsBGLgrl140reesnkFEDDKV5pvR3lkKmq/lWSWAU5E9iVA2cu+SZhSjKWpzTvWljmCW/zhqgiMuigOK42XQkvykO3ZhWliqjMu+OYwvS3vbcBFy75HvhC6+U9QXA7sIg0rcwVw00Rw8oob0P4J44NTf0=""}"
: @"{""Exponent"":""AQAB"",""Modulus"":""0VElWoQA2SK1csG2/sY/wlssO1bjXRx+t+JlIgS6jLPCefyCAcZBv7ElcSPJQIPEXNwN2XdnTc2wEIjZ8bTgBlBqXppj471bJeX8Mi2uAxAqOUDuvGuqth+mq7DMqol3MNH5P9FO6li7nZxI1FX39u2r/4H4PXRiWx13gsVQRL6Clq2jcXFHc9CvNaCQEJX95jgQFAybal216EwlnnVVgiT/TNsfFjW41XJZsHUny9k+dAfyPzqAk54cgrvjgAHJayDWjapq90Fm/+e/DVQ6BHGkV0POQMkkBrvvhAIQu222j+03frm9b2yZrhX/qS01lyjW4VaQytGV0wlewV6BFw==""}"
;
;

RSAParameters parameters;
{
PemReader pemReader = new PemReader(new System.IO.StringReader(PrivateKeyPem));
AsymmetricCipherKeyPair keyPair = (AsymmetricCipherKeyPair)pemReader.ReadObject();
AsymmetricCipherKeyPair keyPair = (AsymmetricCipherKeyPair) pemReader.ReadObject();
parameters = DotNetUtilities.ToRSAParameters(keyPair.Private as RsaPrivateCrtKeyParameters);
}
string json = JsonConvert.SerializeObject(parameters);
_output.WriteLine("JSON value is:\n" + json);

Assert.Equal(expectedJson, json);
Assert.True(JToken.DeepEquals(
JToken.Parse(expectedJson),
JToken.Parse(json)
));
}

[Fact(DisplayName = "Should deserialize RSAParameters and check whether fields are serializable on this platform")]
[Fact(DisplayName =
"Should deserialize RSAParameters and check whether fields are serializable on this platform")]
public void Should_Deserialize_RSAParameters_From_JSON()
{
byte[] d =
Expand Down Expand Up @@ -231,16 +239,16 @@ public void Should_Deserialize_RSAParameters_From_JSON()
195, 77, 17, 195, 202, 40, 111, 67, 248, 39, 142, 13, 77, 253
};

bool areAllFieldsSerilizable = typeof(RSAParameters)
.GetField(nameof(RSAParameters.P))
.GetCustomAttribute<NonSerializedAttribute>() == null;
bool areAllFieldsSerializable = typeof(RSAParameters)
.GetField(nameof(RSAParameters.P))
.GetCustomAttribute<NonSerializedAttribute>() == null;

if (!areAllFieldsSerilizable)
if (!areAllFieldsSerializable)
{
_output.WriteLine("Some fields of RSAParameters struct are NOT serializable on this platform.");
}

if (areAllFieldsSerilizable)
if (areAllFieldsSerializable)
{
RSAParameters rsaParameters = JsonConvert.DeserializeObject<RSAParameters>(@"{
""D"":""nrXEeOl2Ky3iHu0679sqJRg0ZZnwM32Tp6IFxNGJEX6Liysm+Gl3GP4cwbi8tCo1DaPc7HNY5Ol9AkyB1H+tDZyTyr+N1fSR1B6aJwHz22a6XUcavct1WTqOicg4p2Yr9dx//R+bkp1NRKhnoHi+14BG1imaLB6/JQ8muwUZY8Ug/KMMTiVKKZZBUhK0P0/81Ij8Bb5uO65Av0XJzz55Yde0944OZKNZLPM6qPed0XPpQFdA6jjUZzn6bjNJrEnaB6UaNXay0cFCHYfqJD+bkyOMR3VjKY8ldgEt2iw5HYeeadSJlzvW4CNQ/xuvCfdw/T6+2aPN3+RnXl4CbuQJIQ=="",
Expand Down Expand Up @@ -365,10 +373,21 @@ public void Should_JSON_Serialize_EncryptionKeyParameters()
};

string json = JsonConvert.SerializeObject(keyParameters);
Assert.Equal(
@"{""E"":""AQAB"",""M"":""0VElWoQA2SK1csG2/sY/wlssO1bjXRx+t+JlIgS6jLPCefyCAcZBv7ElcSPJQIPEXNwN2XdnTc2wEIjZ8bTgBlBqXppj471bJeX8Mi2uAxAqOUDuvGuqth+mq7DMqol3MNH5P9FO6li7nZxI1FX39u2r/4H4PXRiWx13gsVQRL6Clq2jcXFHc9CvNaCQEJX95jgQFAybal216EwlnnVVgiT/TNsfFjW41XJZsHUny9k+dAfyPzqAk54cgrvjgAHJayDWjapq90Fm/+e/DVQ6BHGkV0POQMkkBrvvhAIQu222j+03frm9b2yZrhX/qS01lyjW4VaQytGV0wlewV6BFw=="",""P"":""56MdiwsGari+fhab1/laCiJfml0HQPvDFl5mCvOxQzJ1t1u49j+cHIZyUVrbG6GSW7X8mJEKLnx8ZOEWqNkyLc71f7uSWytF2gW5qyY84JUQ4LS4UXz6nnjJbnwS776f9GqFQQon3MqSAT9KsDMXlymZk9Wz5TJaWn6i7FSwDaM="",""Q"":""51UN2sd3x/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ON value is:\n" + json);

Assert.True(JToken.DeepEquals(
JToken.Parse(@"{
""E"":""AQAB"",
""M"":""0VElWoQA2SK1csG2/sY/wlssO1bjXRx+t+JlIgS6jLPCefyCAcZBv7ElcSPJQIPEXNwN2XdnTc2wEIjZ8bTgBlBqXppj471bJeX8Mi2uAxAqOUDuvGuqth+mq7DMqol3MNH5P9FO6li7nZxI1FX39u2r/4H4PXRiWx13gsVQRL6Clq2jcXFHc9CvNaCQEJX95jgQFAybal216EwlnnVVgiT/TNsfFjW41XJZsHUny9k+dAfyPzqAk54cgrvjgAHJayDWjapq90Fm/+e/DVQ6BHGkV0POQMkkBrvvhAIQu222j+03frm9b2yZrhX/qS01lyjW4VaQytGV0wlewV6BFw=="",
""P"":""56MdiwsGari+fhab1/laCiJfml0HQPvDFl5mCvOxQzJ1t1u49j+cHIZyUVrbG6GSW7X8mJEKLnx8ZOEWqNkyLc71f7uSWytF2gW5qyY84JUQ4LS4UXz6nnjJbnwS776f9GqFQQon3MqSAT9KsDMXlymZk9Wz5TJaWn6i7FSwDaM="",
""Q"":""51UN2sd3x/w/qArIRFoPsBGLgrl140reesnkFEDDKV5pvR3lkKmq/lWSWAU5E9iVA2cu+SZhSjKWpzTvWljmCW/zhqgiMuigOK42XQkvykO3ZhWliqjMu+OYwvS3vbcBFy75HvhC6+U9QXA7sIg0rcwVw00Rw8oob0P4J44NTf0="",
""D"":""nrXEeOl2Ky3iHu0679sqJRg0ZZnwM32Tp6IFxNGJEX6Liysm+Gl3GP4cwbi8tCo1DaPc7HNY5Ol9AkyB1H+tDZyTyr+N1fSR1B6aJwHz22a6XUcavct1WTqOicg4p2Yr9dx//R+bkp1NRKhnoHi+14BG1imaLB6/JQ8muwUZY8Ug/KMMTiVKKZZBUhK0P0/81Ij8Bb5uO65Av0XJzz55Yde0944OZKNZLPM6qPed0XPpQFdA6jjUZzn6bjNJrEnaB6UaNXay0cFCHYfqJD+bkyOMR3VjKY8ldgEt2iw5HYeeadSJlzvW4CNQ/xuvCfdw/T6+2aPN3+RnXl4CbuQJIQ=="",
""DP"":""KZYZWbsy/boJSS3Z8N5tkeA19cq54KrSjZOJEnJJ4tyOUcr/3AXzixOANqbvK3jIg/qaTPHNOCdVVe8rWEkWPy8m2DXewst0EP5yJQ4KY++fRhhr9wVPIWBiGZng9HXu1bzCC7k9CuC7ccnhKN0jYRow3l/BmmZ93j1aFr/lk60="",
""DQ"":""Y25KgzPjR0Ej902TINX5EjuoVGr0wdpnYC2w7AeeGwTqvA/CoXhb9++99DlifCZGfmz2Yr7daG/wEzSMgK/fSEUnTkSsVmdonpQ656hQM0cB6E37lmrRfRzphzp6U3/6ZwWYrHgXA5/HwX3FGi6hPeaPoB9bOGWHFbQIULAdBd0="",
""IQ"":""0153qXwUkMN3FQRlWI8GwcUMaC/RgVEzIY0WkH6lAVhiVTEmsYm/unQG2jCAFcNAyDChvLU2DRIxKchrh70obeL2UYGqwjag5NdxCmPNC5Mjr9h/ydNa7YGmZOvh+tlucxpyG7c6HQdp+yWrCuetSHP0HKxLYXguEJq/kV3tN6Y=""
}"),
JToken.Parse(json)
));
}

[Fact(DisplayName = "Should deserialize an EncryptionKeyParameters instance from JSON")]
Expand Down Expand Up @@ -564,7 +583,7 @@ public void Should_Cast_RSAParameters_To_EncryptionKeyParameters()
InverseQ = inverseQ,
};

EncryptionKeyParameters keyParameters = (EncryptionKeyParameters)rsaParameters;
EncryptionKeyParameters keyParameters = (EncryptionKeyParameters) rsaParameters;

Assert.Equal(exponent, keyParameters.E);
Assert.Equal(mod, keyParameters.M);
Expand Down Expand Up @@ -668,7 +687,7 @@ public void Should_Cast_EncryptionKeyParameters_To_RSAParameters()
IQ = inverseQ,
};

RSAParameters rsaParameters = (RSAParameters)keyParameters;
RSAParameters rsaParameters = (RSAParameters) keyParameters;

Assert.Equal(exponent, rsaParameters.Exponent);
Assert.Equal(mod, rsaParameters.Modulus);
Expand Down

0 comments on commit 5e621a6

Please sign in to comment.