Skip to content

Commit 9064e13

Browse files
committed
Decryption unit tests for new SDK
1 parent fc24419 commit 9064e13

1 file changed

Lines changed: 232 additions & 1 deletion

File tree

UnitTests/TestSuite.cs

Lines changed: 232 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,22 @@ public void DecodeBytesHS512()
132132
Assert.Equal(BinaryPayload, payload);
133133
}
134134

135+
[Fact]
136+
public void DecodeBytesHS512_StringSDK()
137+
{
138+
// This test decodes a payload consisting of arbitrary binary data. Only a single signature algorithm is tested
139+
// in the binary data scenario, as the internal flow is the same as for the non-Bytes methods and the
140+
// other tests also cover the primary JOSE functionality, with only the binary-payload-specific part tested here.
141+
142+
string token = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0-P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn-AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq-wsbKztLW2t7i5uru8vb6_wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t_g4eLj5OXm5-jp6uvs7e7v8PHy8_T19vf4-fr7_P3-_w.3_-H4HJiNi8--Ss-VAMM1Dg0JtTGEXNvMo1LAHEnQ7bZpQiblqAu5tt-G9p8KFnSlSYOG6l64pIqmqu5p5RvuQ";
143+
144+
var payload = Jose.JWT.DecodeBytes(token, Encoding.UTF8.GetBytes(key), "HS512");
145+
146+
Console.Out.WriteLine("DecodeBytesHS512: " + BitConverter.ToString(payload));
147+
148+
Assert.Equal(BinaryPayload, payload);
149+
}
150+
135151
[Fact]
136152
public void EncryptAndDecryptBytes_RSA1_5_A256GCM()
137153
{
@@ -148,6 +164,22 @@ public void EncryptAndDecryptBytes_RSA1_5_A256GCM()
148164
Assert.Equal(BinaryPayload, payload);
149165
}
150166

167+
[Fact]
168+
public void EncryptAndDecryptBytes_RSA1_5_A256GCM_StringSDK()
169+
{
170+
// This test encodes a payload consisting of arbitrary binary data. Only a single encryption algorithm is tested
171+
// in the binary data scenario, as the internal flow is the same as for the non-Bytes methods and the
172+
// other tests also cover the primary JOSE functionality, with only the binary-payload-specific part tested here.
173+
174+
string token = Jose.JWT.EncodeBytes(BinaryPayload, testSuiteUtils.PubKey(), JweAlgorithm.RSA1_5, JweEncryption.A256GCM);
175+
176+
Console.Out.WriteLine("EncryptAndDecryptBytes_RSA1_5_A256GCM: " + token);
177+
178+
var payload = Jose.JWT.DecodeBytes(token, testSuiteUtils.PrivKey(), "RSA1_5", "A256GCM");
179+
180+
Assert.Equal(BinaryPayload, payload);
181+
}
182+
151183
[Fact]
152184
public void DecodeHS256()
153185
{
@@ -4667,6 +4699,19 @@ public void DecodeSignedTokenValidationSuccess()
46674699
Assert.Equal(@"{""hello"": ""world""}", json);
46684700
}
46694701

4702+
[Fact]
4703+
public void DecodeSignedTokenValidationSuccess_StringSDK()
4704+
{
4705+
// given
4706+
string token = "eyJhbGciOiJSUzI1NiIsImN0eSI6InRleHRcL3BsYWluIn0.eyJoZWxsbyI6ICJ3b3JsZCJ9.NL_dfVpZkhNn4bZpCyMq5TmnXbT4yiyecuB6Kax_lV8Yq2dG8wLfea-T4UKnrjLOwxlbwLwuKzffWcnWv3LVAWfeBxhGTa0c4_0TX_wzLnsgLuU6s9M2GBkAIuSMHY6UTFumJlEeRBeiqZNrlqvmAzQ9ppJHfWWkW4stcgLCLMAZbTqvRSppC1SMxnvPXnZSWn_Fk_q3oGKWw6Nf0-j-aOhK0S0Lcr0PV69ZE4xBYM9PUS1MpMe2zF5J3Tqlc1VBcJ94fjDj1F7y8twmMT3H1PI9RozO-21R0SiXZ_a93fxhE_l_dj5drgOek7jUN9uBDjkXUwJPAyp9YPehrjyLdw";
4707+
4708+
// when
4709+
string json = Jose.JWT.Decode(token, testSuiteUtils.PubKey(), "RS256");
4710+
4711+
// then
4712+
Assert.Equal(@"{""hello"": ""world""}", json);
4713+
}
4714+
46704715
[Fact]
46714716
public void DecodeSignedTokenValidationFailure()
46724717
{
@@ -4675,7 +4720,18 @@ public void DecodeSignedTokenValidationFailure()
46754720

46764721
// then
46774722
Assert.Throws<InvalidAlgorithmException>(() => Jose.JWT.Decode(token, testSuiteUtils.PubKey(), JwsAlgorithm.RS512));
4678-
Assert.Throws<InvalidAlgorithmException>(() => Jose.JWT.Decode(token, testSuiteUtils.PubKey(), JweAlgorithm.RSA_OAEP_256, JweEncryption.A192GCM));
4723+
Assert.Throws<InvalidAlgorithmException>(() => Jose.JWT.Decode(token, testSuiteUtils.PubKey(), JweAlgorithm.RSA_OAEP_256, JweEncryption.A192GCM));
4724+
}
4725+
4726+
[Fact]
4727+
public void DecodeSignedTokenValidationFailure_StringSDK()
4728+
{
4729+
// given
4730+
string token = "eyJhbGciOiJSUzI1NiIsImN0eSI6InRleHRcL3BsYWluIn0.eyJoZWxsbyI6ICJ3b3JsZCJ9.NL_dfVpZkhNn4bZpCyMq5TmnXbT4yiyecuB6Kax_lV8Yq2dG8wLfea-T4UKnrjLOwxlbwLwuKzffWcnWv3LVAWfeBxhGTa0c4_0TX_wzLnsgLuU6s9M2GBkAIuSMHY6UTFumJlEeRBeiqZNrlqvmAzQ9ppJHfWWkW4stcgLCLMAZbTqvRSppC1SMxnvPXnZSWn_Fk_q3oGKWw6Nf0-j-aOhK0S0Lcr0PV69ZE4xBYM9PUS1MpMe2zF5J3Tqlc1VBcJ94fjDj1F7y8twmMT3H1PI9RozO-21R0SiXZ_a93fxhE_l_dj5drgOek7jUN9uBDjkXUwJPAyp9YPehrjyLdw";
4731+
4732+
// then
4733+
Assert.Throws<InvalidAlgorithmException>(() => Jose.JWT.Decode(token, testSuiteUtils.PubKey(), "RS512"));
4734+
Assert.Throws<InvalidAlgorithmException>(() => Jose.JWT.Decode(token, testSuiteUtils.PubKey(), "RSA-OAEP-256", "A192GCM"));
46794735
}
46804736

46814737
[Fact]
@@ -4692,6 +4748,20 @@ public void DecodeEncryptedTokenValidationSuccess()
46924748
Assert.Equal(json, decodedToken);
46934749
}
46944750

4751+
[Fact]
4752+
public void DecodeEncryptedTokenValidationSuccess_StringSDK()
4753+
{
4754+
// given
4755+
string json = @"{""hello"": ""world""}";
4756+
string token = Jose.JWT.Encode(json, testSuiteUtils.PubKey(), "RSA-OAEP-256", "A192GCM");
4757+
4758+
// when
4759+
string decodedToken = Jose.JWT.Decode(token, testSuiteUtils.PrivKey(), "RSA-OAEP-256", "A192GCM");
4760+
4761+
// then
4762+
Assert.Equal(json, decodedToken);
4763+
}
4764+
46954765
[Fact]
46964766
public void DecodeEncryptedTokenValidationFailure()
46974767
{
@@ -4705,6 +4775,19 @@ public void DecodeEncryptedTokenValidationFailure()
47054775
Assert.Throws<InvalidAlgorithmException>(() => Jose.JWT.Decode(token, testSuiteUtils.PrivKey(), JwsAlgorithm.RS256));
47064776
}
47074777

4778+
[Fact]
4779+
public void DecodeEncryptedTokenValidationFailure_StringSDK()
4780+
{
4781+
// given
4782+
string json = @"{""hello"": ""world""}";
4783+
string token = Jose.JWT.Encode(json, testSuiteUtils.PubKey(), "RSA-OAEP-256", "A192GCM");
4784+
4785+
// then
4786+
Assert.Throws<InvalidAlgorithmException>(() => Jose.JWT.Decode(token, testSuiteUtils.PrivKey(), "RSA-OAEP", "A192GCM"));
4787+
Assert.Throws<InvalidAlgorithmException>(() => Jose.JWT.Decode(token, testSuiteUtils.PrivKey(), "RSA-OAEP-256", "A128CBC-HS256"));
4788+
Assert.Throws<InvalidAlgorithmException>(() => Jose.JWT.Decode(token, testSuiteUtils.PrivKey(), "RS256"));
4789+
}
4790+
47084791
[Fact]
47094792
public void DecodeAndMapSignedTokenValidationSuccess()
47104793
{
@@ -4718,6 +4801,19 @@ public void DecodeAndMapSignedTokenValidationSuccess()
47184801
Assert.Equal(new Dictionary<string, object> { { "hello", "world" } }, test);
47194802
}
47204803

4804+
[Fact]
4805+
public void DecodeAndMapSignedTokenValidationSuccess_StringSDK()
4806+
{
4807+
// given
4808+
string token = "eyJhbGciOiJSUzI1NiIsImN0eSI6InRleHRcL3BsYWluIn0.eyJoZWxsbyI6ICJ3b3JsZCJ9.NL_dfVpZkhNn4bZpCyMq5TmnXbT4yiyecuB6Kax_lV8Yq2dG8wLfea-T4UKnrjLOwxlbwLwuKzffWcnWv3LVAWfeBxhGTa0c4_0TX_wzLnsgLuU6s9M2GBkAIuSMHY6UTFumJlEeRBeiqZNrlqvmAzQ9ppJHfWWkW4stcgLCLMAZbTqvRSppC1SMxnvPXnZSWn_Fk_q3oGKWw6Nf0-j-aOhK0S0Lcr0PV69ZE4xBYM9PUS1MpMe2zF5J3Tqlc1VBcJ94fjDj1F7y8twmMT3H1PI9RozO-21R0SiXZ_a93fxhE_l_dj5drgOek7jUN9uBDjkXUwJPAyp9YPehrjyLdw";
4809+
4810+
// when
4811+
var test = Jose.JWT.Decode<IDictionary<string, object>>(token, testSuiteUtils.PubKey(), "RS256");
4812+
4813+
// then
4814+
Assert.Equal(new Dictionary<string, object> { { "hello", "world" } }, test);
4815+
}
4816+
47214817
[Fact]
47224818
public void DecodeAndMapEncryptedTokenValidationSuccess()
47234819
{
@@ -4732,6 +4828,20 @@ public void DecodeAndMapEncryptedTokenValidationSuccess()
47324828
Assert.Equal(new Dictionary<string, object> { { "hello", "world" } }, test);
47334829
}
47344830

4831+
[Fact]
4832+
public void DecodeAndMapEncryptedTokenValidationSuccess_StringSDK()
4833+
{
4834+
// given
4835+
string json = @"{""hello"": ""world""}";
4836+
string token = Jose.JWT.Encode(json, testSuiteUtils.PrivKey(), "RSA-OAEP-256", "A192GCM");
4837+
4838+
// when
4839+
var test = Jose.JWT.Decode<IDictionary<string, object>>(token, testSuiteUtils.PrivKey(), "RSA-OAEP-256", "A192GCM");
4840+
4841+
// then
4842+
Assert.Equal(new Dictionary<string, object> { { "hello", "world" } }, test);
4843+
}
4844+
47354845
[Fact]
47364846
public void UnknownJwsAlg()
47374847
{
@@ -4784,6 +4894,30 @@ public void Verify()
47844894
Assert.Equal(@"{""hello"": ""world""}", json);
47854895
}
47864896

4897+
[Fact]
4898+
public void VerifyExplicitAlg()
4899+
{
4900+
//given
4901+
const string token = "eyJhbGciOiJSUzI1NiIsImN0eSI6InRleHRcL3BsYWluIn0.eyJoZWxsbyI6ICJ3b3JsZCJ9.NL_dfVpZkhNn4bZpCyMq5TmnXbT4yiyecuB6Kax_lV8Yq2dG8wLfea-T4UKnrjLOwxlbwLwuKzffWcnWv3LVAWfeBxhGTa0c4_0TX_wzLnsgLuU6s9M2GBkAIuSMHY6UTFumJlEeRBeiqZNrlqvmAzQ9ppJHfWWkW4stcgLCLMAZbTqvRSppC1SMxnvPXnZSWn_Fk_q3oGKWw6Nf0-j-aOhK0S0Lcr0PV69ZE4xBYM9PUS1MpMe2zF5J3Tqlc1VBcJ94fjDj1F7y8twmMT3H1PI9RozO-21R0SiXZ_a93fxhE_l_dj5drgOek7jUN9uBDjkXUwJPAyp9YPehrjyLdw";
4902+
4903+
//then
4904+
string json = Jose.JWT.Verify(token, testSuiteUtils.PubKey(), JwsAlgorithm.RS256);
4905+
4906+
Assert.Equal(@"{""hello"": ""world""}", json);
4907+
}
4908+
4909+
[Fact]
4910+
public void VerifyExplicitAlg_StringSDK()
4911+
{
4912+
//given
4913+
const string token = "eyJhbGciOiJSUzI1NiIsImN0eSI6InRleHRcL3BsYWluIn0.eyJoZWxsbyI6ICJ3b3JsZCJ9.NL_dfVpZkhNn4bZpCyMq5TmnXbT4yiyecuB6Kax_lV8Yq2dG8wLfea-T4UKnrjLOwxlbwLwuKzffWcnWv3LVAWfeBxhGTa0c4_0TX_wzLnsgLuU6s9M2GBkAIuSMHY6UTFumJlEeRBeiqZNrlqvmAzQ9ppJHfWWkW4stcgLCLMAZbTqvRSppC1SMxnvPXnZSWn_Fk_q3oGKWw6Nf0-j-aOhK0S0Lcr0PV69ZE4xBYM9PUS1MpMe2zF5J3Tqlc1VBcJ94fjDj1F7y8twmMT3H1PI9RozO-21R0SiXZ_a93fxhE_l_dj5drgOek7jUN9uBDjkXUwJPAyp9YPehrjyLdw";
4914+
4915+
//then
4916+
string json = Jose.JWT.Verify(token, testSuiteUtils.PubKey(), "RS256");
4917+
4918+
Assert.Equal(@"{""hello"": ""world""}", json);
4919+
}
4920+
47874921
[Fact]
47884922
public void VerifyBytes()
47894923
{
@@ -4810,6 +4944,19 @@ public void VerifyBytesExplicitAlg()
48104944
Assert.Equal(BinaryPayload, payload);
48114945
}
48124946

4947+
[Fact]
4948+
public void VerifyBytesExplicitAlg_StringSDK()
4949+
{
4950+
//given
4951+
const string token = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0-P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn-AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq-wsbKztLW2t7i5uru8vb6_wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t_g4eLj5OXm5-jp6uvs7e7v8PHy8_T19vf4-fr7_P3-_w.3_-H4HJiNi8--Ss-VAMM1Dg0JtTGEXNvMo1LAHEnQ7bZpQiblqAu5tt-G9p8KFnSlSYOG6l64pIqmqu5p5RvuQ";
4952+
4953+
// when
4954+
var payload = Jose.JWT.VerifyBytes(token, Encoding.UTF8.GetBytes(key), "HS512");
4955+
4956+
// then
4957+
Assert.Equal(BinaryPayload, payload);
4958+
}
4959+
48134960
[Fact]
48144961
public void VerifyUnknownAlg()
48154962
{
@@ -4878,6 +5025,58 @@ public void DecryptBytes()
48785025
Assert.Equal(BinaryPayload, payload);
48795026
}
48805027

5028+
[Fact]
5029+
public void DecryptBytesExplicitAlg()
5030+
{
5031+
//given
5032+
string token = Jose.JWT.EncodeBytes(BinaryPayload, testSuiteUtils.PubKey(), JweAlgorithm.RSA1_5, JweEncryption.A256GCM);
5033+
5034+
//when
5035+
var payload = Jose.JWT.DecryptBytes(token, testSuiteUtils.PrivKey(), JweAlgorithm.RSA1_5);
5036+
5037+
//then
5038+
Assert.Equal(BinaryPayload, payload);
5039+
}
5040+
5041+
[Fact]
5042+
public void DecryptBytesExplicitAlg_StringSDk()
5043+
{
5044+
//given
5045+
string token = Jose.JWT.EncodeBytes(BinaryPayload, testSuiteUtils.PubKey(), "RSA1_5", "A256GCM");
5046+
5047+
//when
5048+
var payload = Jose.JWT.DecryptBytes(token, testSuiteUtils.PrivKey(), "RSA1_5");
5049+
5050+
//then
5051+
Assert.Equal(BinaryPayload, payload);
5052+
}
5053+
5054+
[Fact]
5055+
public void DecryptBytesExplicitEnc()
5056+
{
5057+
//given
5058+
string token = Jose.JWT.EncodeBytes(BinaryPayload, testSuiteUtils.PubKey(), JweAlgorithm.RSA1_5, JweEncryption.A256GCM);
5059+
5060+
//when
5061+
var payload = Jose.JWT.DecryptBytes(token, testSuiteUtils.PrivKey(), JweAlgorithm.RSA1_5, JweEncryption.A256GCM);
5062+
5063+
//then
5064+
Assert.Equal(BinaryPayload, payload);
5065+
}
5066+
5067+
[Fact]
5068+
public void DecryptBytesExplicitEnc_StringSDK()
5069+
{
5070+
//given
5071+
string token = Jose.JWT.EncodeBytes(BinaryPayload, testSuiteUtils.PubKey(), "RSA1_5", "A256GCM");
5072+
5073+
//when
5074+
var payload = Jose.JWT.DecryptBytes(token, testSuiteUtils.PrivKey(), "RSA1_5", "A256GCM");
5075+
5076+
//then
5077+
Assert.Equal(BinaryPayload, payload);
5078+
}
5079+
48815080
[Fact]
48825081
public void DecryptCorrectAlgEnc()
48835082
{
@@ -4891,6 +5090,19 @@ public void DecryptCorrectAlgEnc()
48915090
Assert.Equal(@"{""exp"":1392548520,""sub"":""alice"",""nbf"":1392547920,""aud"":[""https:\/\/app-one.com"",""https:\/\/app-two.com""],""iss"":""https:\/\/openid.net"",""jti"":""0e659a67-1cd3-438b-8888-217e72951ec9"",""iat"":1392547920}", json);
48925091
}
48935092

5093+
[Fact]
5094+
public void DecryptCorrectAlgEnc_StringSDK()
5095+
{
5096+
//given
5097+
const string token = "eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4R0NNIn0..yVi-LdQQngN0C5WS.1McwSmhZzAtmmLp9y-OdnJwaJFo1nj_4ashmzl2LhubGf0Jl1OTEVJzsHZb7bkup7cGTkuxh6Vfv10ljHsjWf_URXoxP3stQqQeViVcuPV0y2Q_WHYzTNGZpmHGe-hM6gjDhyZyvu3yeXGFSvfPQmp9pWVOgDjI4RC0MQ83rzzn-rRdnZkznWjbmOPxwPrR72Qng0BISsEwbkPn4oO8-vlHkVmPpuDTaYzCT2ZR5K9JnIU8d8QdxEAGb7-s8GEJ1yqtd_w._umbK59DAKA3O89h15VoKQ";
5098+
5099+
//when
5100+
string json = Jose.JWT.Decode(token, aes128Key, "dir", "A128GCM");
5101+
5102+
//then
5103+
Assert.Equal(@"{""exp"":1392548520,""sub"":""alice"",""nbf"":1392547920,""aud"":[""https:\/\/app-one.com"",""https:\/\/app-two.com""],""iss"":""https:\/\/openid.net"",""jti"":""0e659a67-1cd3-438b-8888-217e72951ec9"",""iat"":1392547920}", json);
5104+
}
5105+
48945106
[Fact]
48955107
public void DecryptUnknownAlg()
48965108
{
@@ -4921,6 +5133,16 @@ public void DecryptWrongAlg()
49215133
Assert.Throws<InvalidAlgorithmException>(() => Jose.JWT.Decrypt(token, aes192Key, JweAlgorithm.RSA_OAEP));
49225134
}
49235135

5136+
[Fact]
5137+
public void DecryptWrongAlg_StringSDK()
5138+
{
5139+
//given
5140+
const string token = "eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4R0NNIn0..yVi-LdQQngN0C5WS.1McwSmhZzAtmmLp9y-OdnJwaJFo1nj_4ashmzl2LhubGf0Jl1OTEVJzsHZb7bkup7cGTkuxh6Vfv10ljHsjWf_URXoxP3stQqQeViVcuPV0y2Q_WHYzTNGZpmHGe-hM6gjDhyZyvu3yeXGFSvfPQmp9pWVOgDjI4RC0MQ83rzzn-rRdnZkznWjbmOPxwPrR72Qng0BISsEwbkPn4oO8-vlHkVmPpuDTaYzCT2ZR5K9JnIU8d8QdxEAGb7-s8GEJ1yqtd_w._umbK59DAKA3O89h15VoKQ";
5141+
5142+
//then
5143+
Assert.Throws<InvalidAlgorithmException>(() => Jose.JWT.Decrypt(token, aes192Key, "RSA-OAEP"));
5144+
}
5145+
49245146
[Fact]
49255147
public void DecryptWrongEnc()
49265148
{
@@ -4930,6 +5152,15 @@ public void DecryptWrongEnc()
49305152
//then
49315153
Assert.Throws<InvalidAlgorithmException>(() => Jose.JWT.Decrypt(token, aes128Key, JweAlgorithm.DIR, JweEncryption.A192GCM));
49325154
}
5155+
[Fact]
5156+
public void DecryptWrongEnc_StringSDK()
5157+
{
5158+
//given
5159+
const string token = "eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4R0NNIn0..yVi-LdQQngN0C5WS.1McwSmhZzAtmmLp9y-OdnJwaJFo1nj_4ashmzl2LhubGf0Jl1OTEVJzsHZb7bkup7cGTkuxh6Vfv10ljHsjWf_URXoxP3stQqQeViVcuPV0y2Q_WHYzTNGZpmHGe-hM6gjDhyZyvu3yeXGFSvfPQmp9pWVOgDjI4RC0MQ83rzzn-rRdnZkznWjbmOPxwPrR72Qng0BISsEwbkPn4oO8-vlHkVmPpuDTaYzCT2ZR5K9JnIU8d8QdxEAGb7-s8GEJ1yqtd_w._umbK59DAKA3O89h15VoKQ";
5160+
5161+
//then
5162+
Assert.Throws<InvalidAlgorithmException>(() => Jose.JWT.Decrypt(token, aes128Key, "dir", "A192GCM"));
5163+
}
49335164

49345165
[Fact]
49355166
public void DecryptSignedToken()

0 commit comments

Comments
 (0)