diff --git a/build.cake b/build.cake index 51c8746ea..dcb356bc0 100644 --- a/build.cake +++ b/build.cake @@ -67,7 +67,7 @@ Task("TestNetFramework") NuGetRestore( string.Format("./tests/{0}/{0}.csproj", frameworkTestProject), - new NuGetRestoreSettings { PackagesDirectory = string.Format("./tests/{0}/packages/", frameworkTestProject) }); + new NuGetRestoreSettings { PackagesDirectory = string.Format("./packages/", frameworkTestProject) }); MSBuild( string.Format("./tests/{0}/{0}.csproj", frameworkTestProject), diff --git a/tests/JWT.Tests.Common/JWT.Tests.Common.csproj b/tests/JWT.Tests.Common/JWT.Tests.Common.csproj index 15367752a..e98e648ce 100644 --- a/tests/JWT.Tests.Common/JWT.Tests.Common.csproj +++ b/tests/JWT.Tests.Common/JWT.Tests.Common.csproj @@ -4,4 +4,14 @@ netstandard1.0 + + + + + + + + + + \ No newline at end of file diff --git a/tests/JWT.Tests.Core/JwtDecoderTest.cs b/tests/JWT.Tests.Common/JwtDecoderTest.cs similarity index 97% rename from tests/JWT.Tests.Core/JwtDecoderTest.cs rename to tests/JWT.Tests.Common/JwtDecoderTest.cs index 521767dc4..5ca36046c 100644 --- a/tests/JWT.Tests.Core/JwtDecoderTest.cs +++ b/tests/JWT.Tests.Common/JwtDecoderTest.cs @@ -1,200 +1,200 @@ -using System; -using FluentAssertions; -using JWT.Algorithms; -using JWT.Serializers; -using Xunit; -using JWT.Tests.Common; - -namespace JWT.Tests -{ - public class JwtDecoderTest - { - [Fact] - public void Decode_Should_Decode_Token_To_Json_Encoded_String() - { - var serializer = new JsonNetSerializer(); - var urlEncoder = new JwtBase64UrlEncoder(); - var decoder = new JwtDecoder(serializer, null, urlEncoder); - - var expectedPayload = serializer.Serialize(TestData.Customer); - - var actualPayload = decoder.Decode(TestData.Token, "ABC", verify: false); - - actualPayload.Should().Be(expectedPayload); - } - - [Fact] - public void DecodeToObject_Should_Decode_Token_To_Dictionary() - { - var serializer = new JsonNetSerializer(); - var urlEncoder = new JwtBase64UrlEncoder(); - var decoder = new JwtDecoder(serializer, null, urlEncoder); - - var actualPayload = decoder.DecodeToObject(TestData.Token, "ABC", verify: false); - - actualPayload.ShouldBeEquivalentTo(TestData.DictionaryPayload, options => options.IncludingAllRuntimeProperties()); - } - - [Fact] - public void DecodeToObject_Should_Decode_Token_To_Generic_Type() - { - var serializer = new JsonNetSerializer(); - var urlEncoder = new JwtBase64UrlEncoder(); - var decoder = new JwtDecoder(serializer, null, urlEncoder); - - var actualPayload = decoder.DecodeToObject(TestData.Token, "ABC", verify: false); - - actualPayload.ShouldBeEquivalentTo(TestData.Customer); - } - - [Fact] - public void DecodeToObject_Should_Throw_Exception_On_Malformed_Token() - { - var serializer = new JsonNetSerializer(); - var urlEncoder = new JwtBase64UrlEncoder(); - var decoder = new JwtDecoder(serializer, null, urlEncoder); - - Action action = () => decoder.DecodeToObject(TestData.MalformedToken, "ABC", verify: false); - - action.ShouldThrow(); - } - - [Fact] - public void DecodeToObject_Should_Throw_Exception_On_Invalid_Key() - { - var serializer = new JsonNetSerializer(); - var validator = new JwtValidator(serializer, new UtcDateTimeProvider()); - var urlEncoder = new JwtBase64UrlEncoder(); - var decoder = new JwtDecoder(serializer, validator, urlEncoder); - - Action action = () => decoder.DecodeToObject(TestData.Token, "XYZ", verify: true); - - action.ShouldThrow(); - } - - [Fact] - public void DecodeToObject_Should_Throw_Exception_On_Invalid_Expiration_Claim() - { - var serializer = new JsonNetSerializer(); - var validator = new JwtValidator(serializer, new UtcDateTimeProvider()); - var urlEncoder = new JwtBase64UrlEncoder(); - var decoder = new JwtDecoder(serializer, validator, urlEncoder); - - var encoder = new JwtEncoder(new HMACSHA256Algorithm(), serializer, urlEncoder); - var invalidtoken = encoder.Encode(new { exp = "asdsad" }, "ABC"); - - Action action = () => decoder.DecodeToObject(invalidtoken, "ABC", verify: true); - - action.ShouldThrow(); - } - - [Fact] - public void DecodeToObject_Should_Throw_Exception_On_Null_Expiration_Claim() - { - var serializer = new JsonNetSerializer(); - var validator = new JwtValidator(serializer, new UtcDateTimeProvider()); - var urlEncoder = new JwtBase64UrlEncoder(); - var decoder = new JwtDecoder(serializer, validator, urlEncoder); - - var encoder = new JwtEncoder(new HMACSHA256Algorithm(), serializer, urlEncoder); - var invalidtoken = encoder.Encode(new { exp = (object)null }, "ABC"); - - Action action = () => decoder.DecodeToObject(invalidtoken, "ABC", verify: true); - - action.ShouldThrow().WithMessage("Claim 'exp' must be a number."); - } - - [Fact] - public void DecodeToObject_Should_Throw_Exception_On_Expired_Claim() - { - var serializer = new JsonNetSerializer(); - var dateTimeProvider = new UtcDateTimeProvider(); - var validator = new JwtValidator(serializer, dateTimeProvider); - var urlEncoder = new JwtBase64UrlEncoder(); - var decoder = new JwtDecoder(serializer, validator, urlEncoder); - - var now = dateTimeProvider.GetNow(); - var exp = (int)(now.AddHours(-1) - JwtValidator.UnixEpoch).TotalSeconds; - - var encoder = new JwtEncoder(new HMACSHA256Algorithm(), serializer, urlEncoder); - var expiredtoken = encoder.Encode(new { exp = exp }, "ABC"); - - Action action = () => decoder.DecodeToObject(expiredtoken, "ABC", verify: true); - - action.ShouldThrow(); - } - - [Fact] - public void DecodeToObject_Should_DecodeToken_On_Exp_Claim_After_Year2038() - { - var serializer = new JsonNetSerializer(); - var dateTimeProvider = new UtcDateTimeProvider(); - var validator = new JwtValidator(serializer, dateTimeProvider); - var urlEncoder = new JwtBase64UrlEncoder(); - var decoder = new JwtDecoder(serializer, validator, urlEncoder); - - //Why 2038? https://en.wikipedia.org/wiki/Year_2038_problem - var post2038 = new DateTime(2038, 1, 19, 3, 14, 8, DateTimeKind.Utc); - var exp = (post2038 - new DateTime(1970, 1, 1)).TotalSeconds; - var payload = new { exp = exp }; - var encoder = new JwtEncoder(new HMACSHA256Algorithm(), serializer, urlEncoder); - var validToken = encoder.Encode(payload, "ABC"); - - var expectedPayload = serializer.Serialize(payload); - var actualPayload = decoder.Decode(validToken, "ABC", true); - - actualPayload.Should().Be(expectedPayload); - } - - [Fact] - public void DecodeToObject_Should_Throw_Exception_Before_NotBefore_Becomes_Valid() - { - var serializer = new JsonNetSerializer(); - var dateTimeProvider = new UtcDateTimeProvider(); - var validator = new JwtValidator(serializer, dateTimeProvider); - var urlEncoder = new JwtBase64UrlEncoder(); - var decoder = new JwtDecoder(serializer, validator, urlEncoder); - - var encoder = new JwtEncoder(new HMACSHA256Algorithm(), serializer, urlEncoder); - var nbf = (int)(DateTime.UtcNow.AddHours(1) - JwtValidator.UnixEpoch).TotalSeconds; - var invalidnbftoken = encoder.Encode(new { nbf = nbf }, "ABC"); - - Action action = () => decoder.DecodeToObject(invalidnbftoken, "ABC", verify: true); - - action.ShouldThrow(); - } - - [Fact] - public void DecodeToObject_Should_Throw_Exception_On_Null_NotBefore_Claim() - { - var serializer = new JsonNetSerializer(); - var dateTimeProvider = new UtcDateTimeProvider(); - var validator = new JwtValidator(serializer, dateTimeProvider); - var urlEncoder = new JwtBase64UrlEncoder(); - var decoder = new JwtDecoder(serializer, validator, urlEncoder); - - var encoder = new JwtEncoder(new HMACSHA256Algorithm(), serializer, urlEncoder); - var invalidnbftoken = encoder.Encode(new { nbf = (object)null }, "ABC"); - - Action action = () => decoder.DecodeToObject(invalidnbftoken, "ABC", verify: true); - - action.ShouldThrow().WithMessage("Claim 'nbf' must be a number."); - } - - [Fact] - public void DecodeToObject_Should_Decode_Token_After_NotBefore_Becomes_Valid() - { - var serializer = new JsonNetSerializer(); - var dateTimeProvider = new UtcDateTimeProvider(); - var validator = new JwtValidator(serializer, dateTimeProvider); - var urlEncoder = new JwtBase64UrlEncoder(); - var decoder = new JwtDecoder(serializer, validator, urlEncoder); - - var encoder = new JwtEncoder(new HMACSHA256Algorithm(), serializer, urlEncoder); - var nbf = (int)(DateTime.UtcNow - JwtValidator.UnixEpoch).TotalSeconds; - var validnbftoken = encoder.Encode(new { nbf = nbf }, "ABC"); - - decoder.DecodeToObject(validnbftoken, "ABC", verify: true); - } - } +using System; +using FluentAssertions; +using JWT.Algorithms; +using JWT.Serializers; +using Xunit; +using JWT.Tests.Common; + +namespace JWT.Tests +{ + public class JwtDecoderTest + { + [Fact] + public void Decode_Should_Decode_Token_To_Json_Encoded_String() + { + var serializer = new JsonNetSerializer(); + var urlEncoder = new JwtBase64UrlEncoder(); + var decoder = new JwtDecoder(serializer, null, urlEncoder); + + var expectedPayload = serializer.Serialize(TestData.Customer); + + var actualPayload = decoder.Decode(TestData.Token, "ABC", verify: false); + + actualPayload.Should().Be(expectedPayload); + } + + [Fact] + public void DecodeToObject_Should_Decode_Token_To_Dictionary() + { + var serializer = new JsonNetSerializer(); + var urlEncoder = new JwtBase64UrlEncoder(); + var decoder = new JwtDecoder(serializer, null, urlEncoder); + + var actualPayload = decoder.DecodeToObject(TestData.Token, "ABC", verify: false); + + actualPayload.ShouldBeEquivalentTo(TestData.DictionaryPayload, options => options.IncludingAllRuntimeProperties()); + } + + [Fact] + public void DecodeToObject_Should_Decode_Token_To_Generic_Type() + { + var serializer = new JsonNetSerializer(); + var urlEncoder = new JwtBase64UrlEncoder(); + var decoder = new JwtDecoder(serializer, null, urlEncoder); + + var actualPayload = decoder.DecodeToObject(TestData.Token, "ABC", verify: false); + + actualPayload.ShouldBeEquivalentTo(TestData.Customer); + } + + [Fact] + public void DecodeToObject_Should_Throw_Exception_On_Malformed_Token() + { + var serializer = new JsonNetSerializer(); + var urlEncoder = new JwtBase64UrlEncoder(); + var decoder = new JwtDecoder(serializer, null, urlEncoder); + + Action action = () => decoder.DecodeToObject(TestData.MalformedToken, "ABC", verify: false); + + action.ShouldThrow(); + } + + [Fact] + public void DecodeToObject_Should_Throw_Exception_On_Invalid_Key() + { + var serializer = new JsonNetSerializer(); + var validator = new JwtValidator(serializer, new UtcDateTimeProvider()); + var urlEncoder = new JwtBase64UrlEncoder(); + var decoder = new JwtDecoder(serializer, validator, urlEncoder); + + Action action = () => decoder.DecodeToObject(TestData.Token, "XYZ", verify: true); + + action.ShouldThrow(); + } + + [Fact] + public void DecodeToObject_Should_Throw_Exception_On_Invalid_Expiration_Claim() + { + var serializer = new JsonNetSerializer(); + var validator = new JwtValidator(serializer, new UtcDateTimeProvider()); + var urlEncoder = new JwtBase64UrlEncoder(); + var decoder = new JwtDecoder(serializer, validator, urlEncoder); + + var encoder = new JwtEncoder(new HMACSHA256Algorithm(), serializer, urlEncoder); + var invalidtoken = encoder.Encode(new { exp = "asdsad" }, "ABC"); + + Action action = () => decoder.DecodeToObject(invalidtoken, "ABC", verify: true); + + action.ShouldThrow(); + } + + [Fact] + public void DecodeToObject_Should_Throw_Exception_On_Null_Expiration_Claim() + { + var serializer = new JsonNetSerializer(); + var validator = new JwtValidator(serializer, new UtcDateTimeProvider()); + var urlEncoder = new JwtBase64UrlEncoder(); + var decoder = new JwtDecoder(serializer, validator, urlEncoder); + + var encoder = new JwtEncoder(new HMACSHA256Algorithm(), serializer, urlEncoder); + var invalidtoken = encoder.Encode(new { exp = (object)null }, "ABC"); + + Action action = () => decoder.DecodeToObject(invalidtoken, "ABC", verify: true); + + action.ShouldThrow().WithMessage("Claim 'exp' must be a number."); + } + + [Fact] + public void DecodeToObject_Should_Throw_Exception_On_Expired_Claim() + { + var serializer = new JsonNetSerializer(); + var dateTimeProvider = new UtcDateTimeProvider(); + var validator = new JwtValidator(serializer, dateTimeProvider); + var urlEncoder = new JwtBase64UrlEncoder(); + var decoder = new JwtDecoder(serializer, validator, urlEncoder); + + var now = dateTimeProvider.GetNow(); + var exp = (int)(now.AddHours(-1) - JwtValidator.UnixEpoch).TotalSeconds; + + var encoder = new JwtEncoder(new HMACSHA256Algorithm(), serializer, urlEncoder); + var expiredtoken = encoder.Encode(new { exp = exp }, "ABC"); + + Action action = () => decoder.DecodeToObject(expiredtoken, "ABC", verify: true); + + action.ShouldThrow(); + } + + [Fact] + public void DecodeToObject_Should_DecodeToken_On_Exp_Claim_After_Year2038() + { + var serializer = new JsonNetSerializer(); + var dateTimeProvider = new UtcDateTimeProvider(); + var validator = new JwtValidator(serializer, dateTimeProvider); + var urlEncoder = new JwtBase64UrlEncoder(); + var decoder = new JwtDecoder(serializer, validator, urlEncoder); + + //Why 2038? https://en.wikipedia.org/wiki/Year_2038_problem + var post2038 = new DateTime(2038, 1, 19, 3, 14, 8, DateTimeKind.Utc); + var exp = (post2038 - new DateTime(1970, 1, 1)).TotalSeconds; + var payload = new { exp = exp }; + var encoder = new JwtEncoder(new HMACSHA256Algorithm(), serializer, urlEncoder); + var validToken = encoder.Encode(payload, "ABC"); + + var expectedPayload = serializer.Serialize(payload); + var actualPayload = decoder.Decode(validToken, "ABC", true); + + actualPayload.Should().Be(expectedPayload); + } + + [Fact] + public void DecodeToObject_Should_Throw_Exception_Before_NotBefore_Becomes_Valid() + { + var serializer = new JsonNetSerializer(); + var dateTimeProvider = new UtcDateTimeProvider(); + var validator = new JwtValidator(serializer, dateTimeProvider); + var urlEncoder = new JwtBase64UrlEncoder(); + var decoder = new JwtDecoder(serializer, validator, urlEncoder); + + var encoder = new JwtEncoder(new HMACSHA256Algorithm(), serializer, urlEncoder); + var nbf = (int)(DateTime.UtcNow.AddHours(1) - JwtValidator.UnixEpoch).TotalSeconds; + var invalidnbftoken = encoder.Encode(new { nbf = nbf }, "ABC"); + + Action action = () => decoder.DecodeToObject(invalidnbftoken, "ABC", verify: true); + + action.ShouldThrow(); + } + + [Fact] + public void DecodeToObject_Should_Throw_Exception_On_Null_NotBefore_Claim() + { + var serializer = new JsonNetSerializer(); + var dateTimeProvider = new UtcDateTimeProvider(); + var validator = new JwtValidator(serializer, dateTimeProvider); + var urlEncoder = new JwtBase64UrlEncoder(); + var decoder = new JwtDecoder(serializer, validator, urlEncoder); + + var encoder = new JwtEncoder(new HMACSHA256Algorithm(), serializer, urlEncoder); + var invalidnbftoken = encoder.Encode(new { nbf = (object)null }, "ABC"); + + Action action = () => decoder.DecodeToObject(invalidnbftoken, "ABC", verify: true); + + action.ShouldThrow().WithMessage("Claim 'nbf' must be a number."); + } + + [Fact] + public void DecodeToObject_Should_Decode_Token_After_NotBefore_Becomes_Valid() + { + var serializer = new JsonNetSerializer(); + var dateTimeProvider = new UtcDateTimeProvider(); + var validator = new JwtValidator(serializer, dateTimeProvider); + var urlEncoder = new JwtBase64UrlEncoder(); + var decoder = new JwtDecoder(serializer, validator, urlEncoder); + + var encoder = new JwtEncoder(new HMACSHA256Algorithm(), serializer, urlEncoder); + var nbf = (int)(DateTime.UtcNow - JwtValidator.UnixEpoch).TotalSeconds; + var validnbftoken = encoder.Encode(new { nbf = nbf }, "ABC"); + + decoder.DecodeToObject(validnbftoken, "ABC", verify: true); + } + } } \ No newline at end of file diff --git a/tests/JWT.Tests.Core/JwtEncoderTest.cs b/tests/JWT.Tests.Common/JwtEncoderTest.cs similarity index 96% rename from tests/JWT.Tests.Core/JwtEncoderTest.cs rename to tests/JWT.Tests.Common/JwtEncoderTest.cs index 2cf6f4f64..c2b88044a 100644 --- a/tests/JWT.Tests.Core/JwtEncoderTest.cs +++ b/tests/JWT.Tests.Common/JwtEncoderTest.cs @@ -1,37 +1,37 @@ -using System.Collections.Generic; -using FluentAssertions; -using JWT.Algorithms; -using JWT.Serializers; -using Xunit; -using JWT.Tests.Common; - -namespace JWT.Tests -{ - public class JwtEncoderTest - { - [Fact] - public void Encode_Should_Encode_To_Token() - { - var serializer = new JsonNetSerializer(); - var urlEncoder = new JwtBase64UrlEncoder(); - var encoder = new JwtEncoder(new HMACSHA256Algorithm(), serializer, urlEncoder); - - var actual = encoder.Encode(TestData.Customer, "ABC"); - - actual.Should().Be(TestData.Token); - } - - [Fact] - public void Encode_Should_Encode_To_Token_With_Extra_Headers() - { - var serializer = new JsonNetSerializer(); - var urlEncoder = new JwtBase64UrlEncoder(); - var encoder = new JwtEncoder(new HMACSHA256Algorithm(), serializer, urlEncoder); - - var extraheaders = new Dictionary { { "foo", "bar" } }; - var actual = encoder.Encode(extraheaders, TestData.Customer, "ABC"); - - actual.Should().Be(TestData.ExtraHeadersToken); - } - } +using System.Collections.Generic; +using FluentAssertions; +using JWT.Algorithms; +using JWT.Serializers; +using Xunit; +using JWT.Tests.Common; + +namespace JWT.Tests +{ + public class JwtEncoderTest + { + [Fact] + public void Encode_Should_Encode_To_Token() + { + var serializer = new JsonNetSerializer(); + var urlEncoder = new JwtBase64UrlEncoder(); + var encoder = new JwtEncoder(new HMACSHA256Algorithm(), serializer, urlEncoder); + + var actual = encoder.Encode(TestData.Customer, "ABC"); + + actual.Should().Be(TestData.Token); + } + + [Fact] + public void Encode_Should_Encode_To_Token_With_Extra_Headers() + { + var serializer = new JsonNetSerializer(); + var urlEncoder = new JwtBase64UrlEncoder(); + var encoder = new JwtEncoder(new HMACSHA256Algorithm(), serializer, urlEncoder); + + var extraheaders = new Dictionary { { "foo", "bar" } }; + var actual = encoder.Encode(extraheaders, TestData.Customer, "ABC"); + + actual.Should().Be(TestData.ExtraHeadersToken); + } + } } \ No newline at end of file diff --git a/tests/JWT.Tests.Core/JWT.Tests.Core.csproj b/tests/JWT.Tests.Core/JWT.Tests.Core.csproj index c21ebb526..f37a96ec0 100644 --- a/tests/JWT.Tests.Core/JWT.Tests.Core.csproj +++ b/tests/JWT.Tests.Core/JWT.Tests.Core.csproj @@ -4,6 +4,11 @@ netcoreapp1.1 + + + + + diff --git a/tests/JWT.Tests.NETFramework/JWT.Tests.NETFramework.csproj b/tests/JWT.Tests.NETFramework/JWT.Tests.NETFramework.csproj index f612a316b..1e01a6caf 100644 --- a/tests/JWT.Tests.NETFramework/JWT.Tests.NETFramework.csproj +++ b/tests/JWT.Tests.NETFramework/JWT.Tests.NETFramework.csproj @@ -39,36 +39,36 @@ - packages\FluentAssertions.4.19.2\lib\net45\FluentAssertions.dll + ..\..\packages\FluentAssertions.4.19.2\lib\net45\FluentAssertions.dll - .\packages\FluentAssertions.4.19.2\lib\net45\FluentAssertions.Core.dll - - - .\packages\ServiceStack.Text.4.5.6\lib\net45\ServiceStack.Text.dll + ..\..\packages\FluentAssertions.4.19.2\lib\net45\FluentAssertions.Core.dll - - .\packages\xunit.abstractions.2.0.1\lib\net35\xunit.abstractions.dll + ..\..\packages\xunit.abstractions.2.0.1\lib\net35\xunit.abstractions.dll - .\packages\xunit.assert.2.2.0\lib\netstandard1.1\xunit.assert.dll + ..\..\packages\xunit.assert.2.2.0\lib\netstandard1.1\xunit.assert.dll - .\packages\xunit.extensibility.core.2.2.0\lib\netstandard1.1\xunit.core.dll + ..\..\packages\xunit.extensibility.core.2.2.0\lib\netstandard1.1\xunit.core.dll - .\packages\xunit.extensibility.execution.2.2.0\lib\net452\xunit.execution.desktop.dll + ..\..\packages\xunit.extensibility.execution.2.2.0\lib\net452\xunit.execution.desktop.dll + + JwtDecoderTest.cs + + + JwtEncoderTest.cs + - - diff --git a/tests/JWT.Tests.NETFramework/Serializers/ServiceStackJsonSerializer.cs b/tests/JWT.Tests.NETFramework/Serializers/ServiceStackJsonSerializer.cs deleted file mode 100644 index 2e91208bf..000000000 --- a/tests/JWT.Tests.NETFramework/Serializers/ServiceStackJsonSerializer.cs +++ /dev/null @@ -1,17 +0,0 @@ -using ServiceStack.Text; - -namespace JWT.Tests.NETFramework.Serializers -{ - public class ServiceStackJsonSerializer : IJsonSerializer - { - public string Serialize(object obj) - { - return JsonSerializer.SerializeToString(obj); - } - - public T Deserialize(string json) - { - return JsonSerializer.DeserializeFromString(json); - } - } -} \ No newline at end of file diff --git a/tests/JWT.Tests.NETFramework/Serializers/WebScriptJsonSerializer.cs b/tests/JWT.Tests.NETFramework/Serializers/WebScriptJsonSerializer.cs deleted file mode 100644 index bdaf42c23..000000000 --- a/tests/JWT.Tests.NETFramework/Serializers/WebScriptJsonSerializer.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Web.Script.Serialization; - -namespace JWT.Tests.NETFramework.Serializers -{ - public class WebScriptJsonSerializer : IJsonSerializer - { - public string Serialize(object obj) - { - var serializer = new JavaScriptSerializer(); - return serializer.Serialize(obj); - } - - public T Deserialize(string json) - { - var serializer = new JavaScriptSerializer(); - return serializer.Deserialize(json); - } - } -}