Fixed creationdate bug and added more tests.
[dotnetoauth.git] / src / DotNetOpenAuth.Test / OpenId / Messages / IndirectSignedResponseTests.cs
blob8900e7677281183fb743f02d7e69d5398eb47998
1 //-----------------------------------------------------------------------
2 // <copyright file="IndirectSignedResponseTests.cs" company="Andrew Arnott">
3 // Copyright (c) Andrew Arnott. All rights reserved.
4 // </copyright>
5 //-----------------------------------------------------------------------
7 namespace DotNetOpenAuth.Test.OpenId.Messages {
8 using System;
9 using System.Collections.Generic;
10 using System.Globalization;
11 using System.Linq;
12 using System.Text;
13 using DotNetOpenAuth.Messaging;
14 using DotNetOpenAuth.Messaging.Bindings;
15 using DotNetOpenAuth.OpenId;
16 using DotNetOpenAuth.OpenId.ChannelElements;
17 using DotNetOpenAuth.OpenId.Messages;
18 using Microsoft.VisualStudio.TestTools.UnitTesting;
20 [TestClass]
21 public class IndirectSignedResponseTests : OpenIdTestBase {
22 private const string CreationDateString = "2005-05-15T17:11:51Z";
23 private readonly DateTime creationDate = DateTime.Parse(CreationDateString, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
24 private CheckIdRequest request;
25 private IndirectSignedResponse response;
26 private IndirectSignedResponse unsolicited;
27 private Protocol protocol;
29 [TestInitialize]
30 public override void SetUp() {
31 base.SetUp();
33 this.protocol = Protocol.V20;
34 this.request = new CheckIdRequest(this.protocol.Version, ProviderUri, false);
35 this.request.ReturnTo = RPUri;
36 this.response = new IndirectSignedResponse(this.request);
38 this.unsolicited = new IndirectSignedResponse(this.protocol.Version, RPUri);
41 [TestMethod]
42 public void CtorFromRequest() {
43 Assert.AreEqual(this.protocol.Args.Mode.id_res, this.response.Mode);
44 Assert.AreEqual(this.request.ProtocolVersion, this.response.ProtocolVersion);
45 Assert.AreEqual(this.request.ReturnTo, this.response.Recipient);
46 Assert.AreEqual(ProviderUri, this.response.ProviderEndpoint);
47 Assert.IsTrue(DateTime.UtcNow - ((ITamperResistantOpenIdMessage)this.response).UtcCreationDate < TimeSpan.FromSeconds(5));
50 [TestMethod]
51 public void CtorUnsolicited() {
52 Assert.AreEqual(this.protocol.Args.Mode.id_res, this.unsolicited.Mode);
53 Assert.AreEqual(this.protocol.Version, this.unsolicited.ProtocolVersion);
54 Assert.AreEqual(RPUri, this.unsolicited.Recipient);
55 Assert.IsTrue(DateTime.UtcNow - ((ITamperResistantOpenIdMessage)this.unsolicited).UtcCreationDate < TimeSpan.FromSeconds(5));
57 Assert.IsNull(this.unsolicited.ProviderEndpoint);
58 this.unsolicited.ProviderEndpoint = ProviderUri;
59 Assert.AreEqual(ProviderUri, this.unsolicited.ProviderEndpoint);
62 [TestMethod]
63 public void ResponseNonceSetter() {
64 const string HybridValue = CreationDateString + "UNIQUE";
65 var responseAccessor = IndirectSignedResponse_Accessor.AttachShadow(this.response);
66 IReplayProtectedProtocolMessage responseReplay = this.response;
67 responseAccessor.ResponseNonce = HybridValue;
68 Assert.AreEqual(HybridValue, responseAccessor.ResponseNonce);
69 Assert.AreEqual(this.creationDate, responseReplay.UtcCreationDate);
70 Assert.AreEqual("UNIQUE", responseReplay.Nonce);
72 responseAccessor.ResponseNonce = null;
73 Assert.IsNull(responseReplay.Nonce);
76 [TestMethod]
77 public void ResponseNonceGetter() {
78 var responseAccessor = IndirectSignedResponse_Accessor.AttachShadow(this.response);
79 IReplayProtectedProtocolMessage responseReplay = this.response;
80 responseReplay.Nonce = "UnIqUe";
81 responseReplay.UtcCreationDate = this.creationDate;
83 Assert.AreEqual(CreationDateString + "UnIqUe", responseAccessor.ResponseNonce);
84 Assert.AreEqual("UnIqUe", responseReplay.Nonce);
85 Assert.AreEqual(this.creationDate, responseReplay.UtcCreationDate);
88 [TestMethod]
89 public void UtcCreationDateConvertsToUniversal() {
90 IReplayProtectedProtocolMessage responseReplay = this.response;
91 DateTime local = DateTime.Parse("1982-01-01", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal);
92 if (local.Kind != DateTimeKind.Local) {
93 Assert.Inconclusive("Test cannot manage to create a local time.");
96 responseReplay.UtcCreationDate = local;
97 DateTime utcCreationDate = responseReplay.UtcCreationDate;
98 Assert.AreEqual(DateTimeKind.Utc, utcCreationDate.Kind, "Local time should have been converted to universal time.");
99 Assert.AreNotEqual(local.Hour, utcCreationDate.Hour, "The hour was expected to change (unless local time _is_ UTC time for this PC!)");
101 // Now try setting UTC time just to make sure it DOESN'T mangle the hour
102 if (this.creationDate.Kind != DateTimeKind.Utc) {
103 Assert.Inconclusive("We need a UTC datetime to set with.");
105 responseReplay.UtcCreationDate = this.creationDate;
106 utcCreationDate = responseReplay.UtcCreationDate;
107 Assert.AreEqual(DateTimeKind.Utc, utcCreationDate.Kind);
108 Assert.AreEqual(this.creationDate.Hour, utcCreationDate.Hour, "The hour should match since both times are UTC time.");