From 580a9d93922c672667a39daa4b6f22f4b6a1a554 Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Wed, 10 Dec 2008 07:19:05 -0800 Subject: [PATCH] Added several tests and fixed a bug. --- src/DotNetOpenAuth.Test/DotNetOpenAuth.Test.csproj | 4 + .../Messages/CheckAuthenticationRequestTests.cs | 21 +++++ .../Messages/CheckAuthenticationResponseTests.cs | 21 +++++ .../OpenId/Messages/IndirectErrorResponseTests.cs | 10 +- .../Messages/NegativeAssertionResponseTests.cs | 21 +++++ .../Messages/PositiveAssertionResponseTests.cs | 101 +++++++++++++++++++++ src/DotNetOpenAuth.Test/OpenId/OpenIdTestBase.cs | 4 + .../OpenId/Messages/PositiveAssertionResponse.cs | 3 +- 8 files changed, 178 insertions(+), 7 deletions(-) create mode 100644 src/DotNetOpenAuth.Test/OpenId/Messages/CheckAuthenticationRequestTests.cs create mode 100644 src/DotNetOpenAuth.Test/OpenId/Messages/CheckAuthenticationResponseTests.cs create mode 100644 src/DotNetOpenAuth.Test/OpenId/Messages/NegativeAssertionResponseTests.cs create mode 100644 src/DotNetOpenAuth.Test/OpenId/Messages/PositiveAssertionResponseTests.cs diff --git a/src/DotNetOpenAuth.Test/DotNetOpenAuth.Test.csproj b/src/DotNetOpenAuth.Test/DotNetOpenAuth.Test.csproj index 9fa78c9..188c9be 100644 --- a/src/DotNetOpenAuth.Test/DotNetOpenAuth.Test.csproj +++ b/src/DotNetOpenAuth.Test/DotNetOpenAuth.Test.csproj @@ -111,8 +111,12 @@ + + + + diff --git a/src/DotNetOpenAuth.Test/OpenId/Messages/CheckAuthenticationRequestTests.cs b/src/DotNetOpenAuth.Test/OpenId/Messages/CheckAuthenticationRequestTests.cs new file mode 100644 index 0000000..96a2e23 --- /dev/null +++ b/src/DotNetOpenAuth.Test/OpenId/Messages/CheckAuthenticationRequestTests.cs @@ -0,0 +1,21 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) Andrew Arnott. All rights reserved. +// +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test.OpenId.Messages { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class CheckAuthenticationRequestTests : OpenIdTestBase { + [TestInitialize] + public override void SetUp() { + base.SetUp(); + } + } +} diff --git a/src/DotNetOpenAuth.Test/OpenId/Messages/CheckAuthenticationResponseTests.cs b/src/DotNetOpenAuth.Test/OpenId/Messages/CheckAuthenticationResponseTests.cs new file mode 100644 index 0000000..cf97aa3 --- /dev/null +++ b/src/DotNetOpenAuth.Test/OpenId/Messages/CheckAuthenticationResponseTests.cs @@ -0,0 +1,21 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) Andrew Arnott. All rights reserved. +// +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test.OpenId.Messages { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class CheckAuthenticationResponseTests : OpenIdTestBase { + [TestInitialize] + public override void SetUp() { + base.SetUp(); + } + } +} diff --git a/src/DotNetOpenAuth.Test/OpenId/Messages/IndirectErrorResponseTests.cs b/src/DotNetOpenAuth.Test/OpenId/Messages/IndirectErrorResponseTests.cs index eb67294..b0e9130 100644 --- a/src/DotNetOpenAuth.Test/OpenId/Messages/IndirectErrorResponseTests.cs +++ b/src/DotNetOpenAuth.Test/OpenId/Messages/IndirectErrorResponseTests.cs @@ -12,21 +12,19 @@ namespace DotNetOpenAuth.Test.OpenId.Messages { using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] - public class IndirectErrorResponseTests { - private static readonly Uri provider = new Uri("http://provider"); - private static readonly Uri rp = new Uri("http://rp"); + public class IndirectErrorResponseTests : OpenIdTestBase { private IndirectErrorResponse response; [TestInitialize] public void Setup() { - CheckIdRequest request = new CheckIdRequest(Protocol.V20.Version, provider, true); - request.ReturnTo = rp; + CheckIdRequest request = new CheckIdRequest(Protocol.V20.Version, ProviderUri, true); + request.ReturnTo = RPUri; this.response = new IndirectErrorResponse(request); } [TestMethod] public void Ctor() { - Assert.AreEqual(rp, this.response.Recipient); + Assert.AreEqual(RPUri, this.response.Recipient); } [TestMethod] diff --git a/src/DotNetOpenAuth.Test/OpenId/Messages/NegativeAssertionResponseTests.cs b/src/DotNetOpenAuth.Test/OpenId/Messages/NegativeAssertionResponseTests.cs new file mode 100644 index 0000000..908257d --- /dev/null +++ b/src/DotNetOpenAuth.Test/OpenId/Messages/NegativeAssertionResponseTests.cs @@ -0,0 +1,21 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) Andrew Arnott. All rights reserved. +// +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test.OpenId.Messages { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class NegativeAssertionResponseTests : OpenIdTestBase { + [TestInitialize] + public override void SetUp() { + base.SetUp(); + } + } +} diff --git a/src/DotNetOpenAuth.Test/OpenId/Messages/PositiveAssertionResponseTests.cs b/src/DotNetOpenAuth.Test/OpenId/Messages/PositiveAssertionResponseTests.cs new file mode 100644 index 0000000..9b6a455 --- /dev/null +++ b/src/DotNetOpenAuth.Test/OpenId/Messages/PositiveAssertionResponseTests.cs @@ -0,0 +1,101 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) Andrew Arnott. All rights reserved. +// +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test.OpenId.Messages { + using System; + using System.Collections.Generic; + using System.Globalization; + using System.Linq; + using System.Text; + using DotNetOpenAuth.Messaging.Bindings; + using DotNetOpenAuth.OpenId; + using DotNetOpenAuth.OpenId.Messages; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class PositiveAssertionResponseTests : OpenIdTestBase { + private const string CreationDateString = "2005-05-15T17:11:51Z"; + private readonly DateTime creationDate = DateTime.Parse(CreationDateString, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal); + private CheckIdRequest request; + private PositiveAssertionResponse response; + private PositiveAssertionResponse unsolicited; + + [TestInitialize] + public override void SetUp() { + base.SetUp(); + + this.request = new CheckIdRequest(Protocol.V20.Version, ProviderUri, false); + this.request.ReturnTo = RPUri; + this.response = new PositiveAssertionResponse(this.request); + + this.unsolicited = new PositiveAssertionResponse(Protocol.V20.Version, RPUri); + } + + [TestMethod] + public void CtorFromRequest() { + Assert.AreEqual(this.request.ProtocolVersion, this.response.ProtocolVersion); + Assert.AreEqual(this.request.ReturnTo, this.response.Recipient); + Assert.AreEqual(ProviderUri, this.response.ProviderEndpoint); + } + + [TestMethod] + public void CtorUnsolicited() { + Assert.AreEqual(Protocol.V20.Version, this.unsolicited.ProtocolVersion); + Assert.AreEqual(RPUri, this.unsolicited.Recipient); + + Assert.IsNull(this.unsolicited.ProviderEndpoint); + this.unsolicited.ProviderEndpoint = ProviderUri; + Assert.AreEqual(ProviderUri, this.unsolicited.ProviderEndpoint); + } + + [TestMethod] + public void ResponseNonceSetter() { + const string HybridValue = CreationDateString + "UNIQUE"; + var responseAccessor = PositiveAssertionResponse_Accessor.AttachShadow(this.response); + IReplayProtectedProtocolMessage responseReplay = this.response; + responseAccessor.ResponseNonce = HybridValue; + Assert.AreEqual(HybridValue, responseAccessor.ResponseNonce); + Assert.AreEqual(this.creationDate, responseReplay.UtcCreationDate); + Assert.AreEqual("UNIQUE", responseReplay.Nonce); + } + + [TestMethod] + public void ResponseNonceGetter() { + var responseAccessor = PositiveAssertionResponse_Accessor.AttachShadow(this.response); + IReplayProtectedProtocolMessage responseReplay = this.response; + responseReplay.Nonce = "UnIqUe"; + responseReplay.UtcCreationDate = this.creationDate; + + Assert.AreEqual(CreationDateString + "UnIqUe", responseAccessor.ResponseNonce); + Assert.AreEqual("UnIqUe", responseReplay.Nonce); + Assert.AreEqual(this.creationDate, responseReplay.UtcCreationDate); + } + + [TestMethod] + public void UtcCreationDateConvertsToUniversal() + { + IReplayProtectedProtocolMessage responseReplay = this.response; + DateTime local = DateTime.Parse("1982-01-01", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal); + if (local.Kind != DateTimeKind.Local) { + Assert.Inconclusive("Test cannot manage to create a local time."); + } + + responseReplay.UtcCreationDate = local; + DateTime utcCreationDate = responseReplay.UtcCreationDate; + Assert.AreEqual(DateTimeKind.Utc, utcCreationDate.Kind, "Local time should have been converted to universal time."); + Assert.AreNotEqual(local.Hour, utcCreationDate.Hour, "The hour was expected to change (unless local time _is_ UTC time for this PC!)"); + + // Now try setting UTC time just to make sure it DOESN'T mangle the hour + if (this.creationDate.Kind != DateTimeKind.Utc) { + Assert.Inconclusive("We need a UTC datetime to set with."); + } + responseReplay.UtcCreationDate = this.creationDate; + utcCreationDate = responseReplay.UtcCreationDate; + Assert.AreEqual(DateTimeKind.Utc, utcCreationDate.Kind); + Assert.AreEqual(this.creationDate.Hour, utcCreationDate.Hour, "The hour should match since both times are UTC time."); + } + } +} diff --git a/src/DotNetOpenAuth.Test/OpenId/OpenIdTestBase.cs b/src/DotNetOpenAuth.Test/OpenId/OpenIdTestBase.cs index be795c3..2a1b7bd 100644 --- a/src/DotNetOpenAuth.Test/OpenId/OpenIdTestBase.cs +++ b/src/DotNetOpenAuth.Test/OpenId/OpenIdTestBase.cs @@ -5,6 +5,7 @@ //----------------------------------------------------------------------- namespace DotNetOpenAuth.Test.OpenId { + using System; using DotNetOpenAuth.Configuration; using DotNetOpenAuth.Messaging; using DotNetOpenAuth.OpenId.Provider; @@ -17,6 +18,9 @@ namespace DotNetOpenAuth.Test.OpenId { internal MockHttpRequest MockResponder; + protected static readonly Uri ProviderUri = new Uri("http://provider"); + protected static readonly Uri RPUri = new Uri("http://rp"); + protected RelyingPartySecuritySettings RelyingPartySecuritySettings { get; private set; } protected ProviderSecuritySettings ProviderSecuritySettings { get; private set; } diff --git a/src/DotNetOpenAuth/OpenId/Messages/PositiveAssertionResponse.cs b/src/DotNetOpenAuth/OpenId/Messages/PositiveAssertionResponse.cs index 4f45cee..4937f37 100644 --- a/src/DotNetOpenAuth/OpenId/Messages/PositiveAssertionResponse.cs +++ b/src/DotNetOpenAuth/OpenId/Messages/PositiveAssertionResponse.cs @@ -46,6 +46,7 @@ namespace DotNetOpenAuth.OpenId.Messages { ErrorUtilities.VerifyArgumentNotNull(request, "request"); this.ReturnTo = request.ReturnTo; + this.ProviderEndpoint = request.Recipient; } /// @@ -186,7 +187,7 @@ namespace DotNetOpenAuth.OpenId.Messages { } else { int indexOfZ = value.IndexOf("Z", StringComparison.Ordinal); ErrorUtilities.VerifyProtocol(indexOfZ >= 0, MessagingStrings.UnexpectedMessagePartValue, Protocol.openid.response_nonce, value); - this.creationDateUtc = DateTime.Parse(value.Substring(0, indexOfZ + 1), CultureInfo.InvariantCulture); + this.creationDateUtc = DateTime.Parse(value.Substring(0, indexOfZ + 1), CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal); ((IReplayProtectedProtocolMessage)this).Nonce = value.Substring(indexOfZ + 1); } } -- 2.11.4.GIT