summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/Mocks
diff options
context:
space:
mode:
authorAndrew Arnott <andrewarnott@gmail.com>2008-11-03 17:22:00 -0800
committerAndrew <andrewarnott@gmail.com>2008-11-04 08:12:52 -0800
commit462e19abd9034c11a12cad30e9899740f2bef8ff (patch)
treee08667f1d69249f8daa6c348a919bd0fd5434415 /src/DotNetOpenAuth.Test/Mocks
parent6a79be0eca3929d8fb4e797799dac8d6f7875475 (diff)
downloadDotNetOpenAuth-462e19abd9034c11a12cad30e9899740f2bef8ff.zip
DotNetOpenAuth-462e19abd9034c11a12cad30e9899740f2bef8ff.tar.gz
DotNetOpenAuth-462e19abd9034c11a12cad30e9899740f2bef8ff.tar.bz2
Changed namepace and project names in preparation for merge with DotNetOpenId.
Diffstat (limited to 'src/DotNetOpenAuth.Test/Mocks')
-rw-r--r--src/DotNetOpenAuth.Test/Mocks/InMemoryTokenManager.cs106
-rw-r--r--src/DotNetOpenAuth.Test/Mocks/MockReplayProtectionBindingElement.cs48
-rw-r--r--src/DotNetOpenAuth.Test/Mocks/MockSigningBindingElement.cs48
-rw-r--r--src/DotNetOpenAuth.Test/Mocks/MockTransformationBindingElement.cs55
-rw-r--r--src/DotNetOpenAuth.Test/Mocks/TestBadChannel.cs54
-rw-r--r--src/DotNetOpenAuth.Test/Mocks/TestBaseMessage.cs56
-rw-r--r--src/DotNetOpenAuth.Test/Mocks/TestChannel.cs35
-rw-r--r--src/DotNetOpenAuth.Test/Mocks/TestDerivedMessage.cs33
-rw-r--r--src/DotNetOpenAuth.Test/Mocks/TestDirectedMessage.cs43
-rw-r--r--src/DotNetOpenAuth.Test/Mocks/TestExpiringMessage.cs35
-rw-r--r--src/DotNetOpenAuth.Test/Mocks/TestMessage.cs63
-rw-r--r--src/DotNetOpenAuth.Test/Mocks/TestMessageTypeProvider.cs56
-rw-r--r--src/DotNetOpenAuth.Test/Mocks/TestReplayProtectedMessage.cs30
-rw-r--r--src/DotNetOpenAuth.Test/Mocks/TestSignedDirectedMessage.cs33
-rw-r--r--src/DotNetOpenAuth.Test/Mocks/TestWebRequestHandler.cs46
15 files changed, 741 insertions, 0 deletions
diff --git a/src/DotNetOpenAuth.Test/Mocks/InMemoryTokenManager.cs b/src/DotNetOpenAuth.Test/Mocks/InMemoryTokenManager.cs
new file mode 100644
index 0000000..571bba7
--- /dev/null
+++ b/src/DotNetOpenAuth.Test/Mocks/InMemoryTokenManager.cs
@@ -0,0 +1,106 @@
+//-----------------------------------------------------------------------
+// <copyright file="InMemoryTokenManager.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOpenAuth.Test.Mocks {
+ using System;
+ using System.Collections.Generic;
+ using System.Diagnostics;
+ using DotNetOpenAuth.OAuth.ChannelElements;
+ using DotNetOpenAuth.OAuth.Messages;
+
+ internal class InMemoryTokenManager : ITokenManager {
+ private Dictionary<string, string> consumersAndSecrets = new Dictionary<string, string>();
+ private Dictionary<string, string> tokensAndSecrets = new Dictionary<string, string>();
+
+ /// <summary>
+ /// Request tokens that have been issued, and whether they have been authorized yet.
+ /// </summary>
+ private Dictionary<string, bool> requestTokens = new Dictionary<string, bool>();
+
+ /// <summary>
+ /// Access tokens that have been issued and have not yet expired.
+ /// </summary>
+ private List<string> accessTokens = new List<string>();
+
+ #region ITokenManager Members
+
+ public string GetConsumerSecret(string consumerKey) {
+ return this.consumersAndSecrets[consumerKey];
+ }
+
+ public string GetTokenSecret(string token) {
+ return this.tokensAndSecrets[token];
+ }
+
+ public void StoreNewRequestToken(UnauthorizedTokenRequest request, ITokenSecretContainingMessage response) {
+ this.tokensAndSecrets[response.Token] = response.TokenSecret;
+ this.requestTokens.Add(response.Token, false);
+ }
+
+ /// <summary>
+ /// Checks whether a given request token has already been authorized
+ /// by some user for use by the Consumer that requested it.
+ /// </summary>
+ /// <param name="requestToken">The Consumer's request token.</param>
+ /// <returns>
+ /// True if the request token has already been fully authorized by the user
+ /// who owns the relevant protected resources. False if the token has not yet
+ /// been authorized, has expired or does not exist.
+ /// </returns>
+ public bool IsRequestTokenAuthorized(string requestToken) {
+ return this.requestTokens[requestToken];
+ }
+
+ public void ExpireRequestTokenAndStoreNewAccessToken(string consumerKey, string requestToken, string accessToken, string accessTokenSecret) {
+ // The following line is commented out because consumers don't mark their own tokens
+ // as authorized... only the SPs do. And since we multi-purpose this test class for
+ // both SPs and Consumers, we won't do this extra check.
+ ////Debug.Assert(this.requestTokens[requestToken], "Unauthorized token should not be exchanged for access token.");
+ this.requestTokens.Remove(requestToken);
+ this.accessTokens.Add(accessToken);
+ this.tokensAndSecrets.Remove(requestToken);
+ this.tokensAndSecrets[accessToken] = accessTokenSecret;
+ }
+
+ /// <summary>
+ /// Classifies a token as a request token or an access token.
+ /// </summary>
+ /// <param name="token">The token to classify.</param>
+ /// <returns>Request or Access token, or invalid if the token is not recognized.</returns>
+ public TokenType GetTokenType(string token) {
+ if (this.requestTokens.ContainsKey(token)) {
+ return TokenType.RequestToken;
+ } else if (this.accessTokens.Contains(token)) {
+ return TokenType.AccessToken;
+ } else {
+ return TokenType.InvalidToken;
+ }
+ }
+
+ #endregion
+
+ /// <summary>
+ /// Tells a Service Provider's token manager about a consumer and its secret
+ /// so that the SP can verify the Consumer's signed messages.
+ /// </summary>
+ /// <param name="consumerDescription">The consumer description.</param>
+ internal void AddConsumer(ConsumerDescription consumerDescription) {
+ this.consumersAndSecrets.Add(consumerDescription.ConsumerKey, consumerDescription.ConsumerSecret);
+ }
+
+ /// <summary>
+ /// Marks an existing token as authorized.
+ /// </summary>
+ /// <param name="requestToken">The request token.</param>
+ internal void AuthorizeRequestToken(string requestToken) {
+ if (requestToken == null) {
+ throw new ArgumentNullException("requestToken");
+ }
+
+ this.requestTokens[requestToken] = true;
+ }
+ }
+}
diff --git a/src/DotNetOpenAuth.Test/Mocks/MockReplayProtectionBindingElement.cs b/src/DotNetOpenAuth.Test/Mocks/MockReplayProtectionBindingElement.cs
new file mode 100644
index 0000000..5e65a59
--- /dev/null
+++ b/src/DotNetOpenAuth.Test/Mocks/MockReplayProtectionBindingElement.cs
@@ -0,0 +1,48 @@
+//-----------------------------------------------------------------------
+// <copyright file="MockReplayProtectionBindingElement.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOpenAuth.Test.Mocks {
+ using DotNetOpenAuth.Messaging;
+ using DotNetOpenAuth.Messaging.Bindings;
+ using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+ internal class MockReplayProtectionBindingElement : IChannelBindingElement {
+ private bool messageReceived;
+
+ #region IChannelBindingElement Members
+
+ MessageProtections IChannelBindingElement.Protection {
+ get { return MessageProtections.ReplayProtection; }
+ }
+
+ bool IChannelBindingElement.PrepareMessageForSending(IProtocolMessage message) {
+ var replayMessage = message as IReplayProtectedProtocolMessage;
+ if (replayMessage != null) {
+ replayMessage.Nonce = "someNonce";
+ return true;
+ }
+
+ return false;
+ }
+
+ bool IChannelBindingElement.PrepareMessageForReceiving(IProtocolMessage message) {
+ var replayMessage = message as IReplayProtectedProtocolMessage;
+ if (replayMessage != null) {
+ Assert.AreEqual("someNonce", replayMessage.Nonce, "The nonce didn't serialize correctly, or something");
+ // this mock implementation passes the first time and fails subsequent times.
+ if (this.messageReceived) {
+ throw new ReplayedMessageException(message);
+ }
+ this.messageReceived = true;
+ return true;
+ }
+
+ return false;
+ }
+
+ #endregion
+ }
+}
diff --git a/src/DotNetOpenAuth.Test/Mocks/MockSigningBindingElement.cs b/src/DotNetOpenAuth.Test/Mocks/MockSigningBindingElement.cs
new file mode 100644
index 0000000..eab9a39
--- /dev/null
+++ b/src/DotNetOpenAuth.Test/Mocks/MockSigningBindingElement.cs
@@ -0,0 +1,48 @@
+//-----------------------------------------------------------------------
+// <copyright file="MockSigningBindingElement.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOpenAuth.Test.Mocks {
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using DotNetOpenAuth.Messaging;
+ using DotNetOpenAuth.Messaging.Bindings;
+
+ internal class MockSigningBindingElement : IChannelBindingElement {
+ internal const string MessageSignature = "mocksignature";
+
+ #region IChannelBindingElement Members
+
+ MessageProtections IChannelBindingElement.Protection {
+ get { return MessageProtections.TamperProtection; }
+ }
+
+ bool IChannelBindingElement.PrepareMessageForSending(IProtocolMessage message) {
+ ITamperResistantProtocolMessage signedMessage = message as ITamperResistantProtocolMessage;
+ if (signedMessage != null) {
+ signedMessage.Signature = MessageSignature;
+ return true;
+ }
+
+ return false;
+ }
+
+ bool IChannelBindingElement.PrepareMessageForReceiving(IProtocolMessage message) {
+ ITamperResistantProtocolMessage signedMessage = message as ITamperResistantProtocolMessage;
+ if (signedMessage != null) {
+ if (signedMessage.Signature != MessageSignature) {
+ throw new InvalidSignatureException(message);
+ }
+ return true;
+ }
+
+ return false;
+ }
+
+ #endregion
+ }
+}
diff --git a/src/DotNetOpenAuth.Test/Mocks/MockTransformationBindingElement.cs b/src/DotNetOpenAuth.Test/Mocks/MockTransformationBindingElement.cs
new file mode 100644
index 0000000..7c5a240
--- /dev/null
+++ b/src/DotNetOpenAuth.Test/Mocks/MockTransformationBindingElement.cs
@@ -0,0 +1,55 @@
+//-----------------------------------------------------------------------
+// <copyright file="MockTransformationBindingElement.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOpenAuth.Test.Mocks {
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using DotNetOpenAuth.Messaging;
+ using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+ internal class MockTransformationBindingElement : IChannelBindingElement {
+ private string transform;
+
+ internal MockTransformationBindingElement(string transform) {
+ if (transform == null) {
+ throw new ArgumentNullException("transform");
+ }
+
+ this.transform = transform;
+ }
+
+ #region IChannelBindingElement Members
+
+ MessageProtections IChannelBindingElement.Protection {
+ get { return MessageProtections.None; }
+ }
+
+ bool IChannelBindingElement.PrepareMessageForSending(IProtocolMessage message) {
+ var testMessage = message as TestMessage;
+ if (testMessage != null) {
+ testMessage.Name = this.transform + testMessage.Name;
+ return true;
+ }
+
+ return false;
+ }
+
+ bool IChannelBindingElement.PrepareMessageForReceiving(IProtocolMessage message) {
+ var testMessage = message as TestMessage;
+ if (testMessage != null) {
+ StringAssert.StartsWith(testMessage.Name, this.transform);
+ testMessage.Name = testMessage.Name.Substring(this.transform.Length);
+ return true;
+ }
+
+ return false;
+ }
+
+ #endregion
+ }
+}
diff --git a/src/DotNetOpenAuth.Test/Mocks/TestBadChannel.cs b/src/DotNetOpenAuth.Test/Mocks/TestBadChannel.cs
new file mode 100644
index 0000000..8fbfaf9
--- /dev/null
+++ b/src/DotNetOpenAuth.Test/Mocks/TestBadChannel.cs
@@ -0,0 +1,54 @@
+//-----------------------------------------------------------------------
+// <copyright file="TestBadChannel.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOpenAuth.Test.Mocks {
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using DotNetOpenAuth.Messaging;
+
+ /// <summary>
+ /// A Channel derived type that passes null to the protected constructor.
+ /// </summary>
+ internal class TestBadChannel : Channel {
+ internal TestBadChannel(bool badConstructorParam)
+ : base(badConstructorParam ? null : new TestMessageTypeProvider()) {
+ }
+
+ internal new void Create301RedirectResponse(IDirectedProtocolMessage message, IDictionary<string, string> fields) {
+ base.Create301RedirectResponse(message, fields);
+ }
+
+ internal new void CreateFormPostResponse(IDirectedProtocolMessage message, IDictionary<string, string> fields) {
+ base.CreateFormPostResponse(message, fields);
+ }
+
+ internal new void SendIndirectMessage(IDirectedProtocolMessage message) {
+ base.SendIndirectMessage(message);
+ }
+
+ internal new IProtocolMessage Receive(Dictionary<string, string> fields, MessageReceivingEndpoint recipient) {
+ return base.Receive(fields, recipient);
+ }
+
+ internal new IProtocolMessage ReadFromRequest(HttpRequestInfo request) {
+ return base.ReadFromRequest(request);
+ }
+
+ protected override IProtocolMessage RequestInternal(IDirectedProtocolMessage request) {
+ throw new NotImplementedException();
+ }
+
+ protected override IProtocolMessage ReadFromResponseInternal(System.IO.Stream responseStream) {
+ throw new NotImplementedException();
+ }
+
+ protected override Response SendDirectMessageResponse(IProtocolMessage response) {
+ throw new NotImplementedException();
+ }
+ }
+}
diff --git a/src/DotNetOpenAuth.Test/Mocks/TestBaseMessage.cs b/src/DotNetOpenAuth.Test/Mocks/TestBaseMessage.cs
new file mode 100644
index 0000000..ef0693c
--- /dev/null
+++ b/src/DotNetOpenAuth.Test/Mocks/TestBaseMessage.cs
@@ -0,0 +1,56 @@
+//-----------------------------------------------------------------------
+// <copyright file="TestBaseMessage.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOpenAuth.Test.Mocks {
+ using System;
+ using System.Collections.Generic;
+ using System.Runtime.Serialization;
+ using DotNetOpenAuth.Messaging;
+ using DotNetOpenAuth.Messaging.Reflection;
+
+ internal interface IBaseMessageExplicitMembers {
+ string ExplicitProperty { get; set; }
+ }
+
+ internal class TestBaseMessage : IProtocolMessage, IBaseMessageExplicitMembers {
+ private Dictionary<string, string> extraData = new Dictionary<string, string>();
+
+ [MessagePart("age", IsRequired = true)]
+ public int Age { get; set; }
+
+ [MessagePart]
+ public string Name { get; set; }
+
+ [MessagePart("explicit")]
+ string IBaseMessageExplicitMembers.ExplicitProperty { get; set; }
+
+ Version IProtocolMessage.ProtocolVersion {
+ get { return new Version(1, 0); }
+ }
+
+ MessageProtections IProtocolMessage.RequiredProtection {
+ get { return MessageProtections.None; }
+ }
+
+ MessageTransport IProtocolMessage.Transport {
+ get { return MessageTransport.Indirect; }
+ }
+
+ IDictionary<string, string> IProtocolMessage.ExtraData {
+ get { return this.extraData; }
+ }
+
+ internal string PrivatePropertyAccessor {
+ get { return this.PrivateProperty; }
+ set { this.PrivateProperty = value; }
+ }
+
+ [MessagePart("private")]
+ private string PrivateProperty { get; set; }
+
+ void IProtocolMessage.EnsureValidMessage() { }
+ }
+}
diff --git a/src/DotNetOpenAuth.Test/Mocks/TestChannel.cs b/src/DotNetOpenAuth.Test/Mocks/TestChannel.cs
new file mode 100644
index 0000000..ebb5858
--- /dev/null
+++ b/src/DotNetOpenAuth.Test/Mocks/TestChannel.cs
@@ -0,0 +1,35 @@
+//-----------------------------------------------------------------------
+// <copyright file="TestChannel.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOpenAuth.Test.Mocks {
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using DotNetOpenAuth.Messaging;
+
+ internal class TestChannel : Channel {
+ internal TestChannel()
+ : this(new TestMessageTypeProvider()) {
+ }
+
+ internal TestChannel(IMessageTypeProvider messageTypeProvider, params IChannelBindingElement[] bindingElements)
+ : base(messageTypeProvider, bindingElements) {
+ }
+
+ protected override IProtocolMessage RequestInternal(IDirectedProtocolMessage request) {
+ throw new NotImplementedException("Request");
+ }
+
+ protected override IProtocolMessage ReadFromResponseInternal(System.IO.Stream responseStream) {
+ throw new NotImplementedException("ReadFromResponse");
+ }
+
+ protected override Response SendDirectMessageResponse(IProtocolMessage response) {
+ throw new NotImplementedException("SendDirectMessageResponse");
+ }
+ }
+}
diff --git a/src/DotNetOpenAuth.Test/Mocks/TestDerivedMessage.cs b/src/DotNetOpenAuth.Test/Mocks/TestDerivedMessage.cs
new file mode 100644
index 0000000..de34329
--- /dev/null
+++ b/src/DotNetOpenAuth.Test/Mocks/TestDerivedMessage.cs
@@ -0,0 +1,33 @@
+//-----------------------------------------------------------------------
+// <copyright file="TestDerivedMessage.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOpenAuth.Test.Mocks {
+ using System.Runtime.Serialization;
+ using DotNetOpenAuth.Messaging;
+
+ internal class TestDerivedMessage : TestBaseMessage {
+ /// <summary>
+ /// Gets or sets the first value.
+ /// </summary>
+ /// <remarks>
+ /// This element should appear AFTER <see cref="SecondDerivedElement"/>
+ /// due to alphabetical ordering rules, but after all the elements in the
+ /// base class due to inheritance rules.
+ /// </remarks>
+ [MessagePart]
+ public string TheFirstDerivedElement { get; set; }
+
+ /// <summary>
+ /// Gets or sets the second value.
+ /// </summary>
+ /// <remarks>
+ /// This element should appear BEFORE <see cref="TheFirstDerivedElement"/>,
+ /// but after all the elements in the base class.
+ /// </remarks>
+ [MessagePart]
+ public string SecondDerivedElement { get; set; }
+ }
+}
diff --git a/src/DotNetOpenAuth.Test/Mocks/TestDirectedMessage.cs b/src/DotNetOpenAuth.Test/Mocks/TestDirectedMessage.cs
new file mode 100644
index 0000000..67d0eb0
--- /dev/null
+++ b/src/DotNetOpenAuth.Test/Mocks/TestDirectedMessage.cs
@@ -0,0 +1,43 @@
+//-----------------------------------------------------------------------
+// <copyright file="TestDirectedMessage.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOpenAuth.Test.Mocks {
+ using System;
+ using DotNetOpenAuth.Messaging;
+ using DotNetOpenAuth.OAuth.ChannelElements;
+
+ internal class TestDirectedMessage : TestMessage, IOAuthDirectedMessage {
+ internal TestDirectedMessage() {
+ }
+
+ internal TestDirectedMessage(MessageTransport transport) : base(transport) {
+ }
+
+ #region IDirectedProtocolMessage Members
+
+ public Uri Recipient { get; set; }
+
+ #endregion
+
+ #region IProtocolMessage Properties
+
+ MessageProtections IProtocolMessage.RequiredProtection {
+ get { return this.RequiredProtection; }
+ }
+
+ #endregion
+
+ #region IOAuthDirectedMessage Members
+
+ public HttpDeliveryMethods HttpMethods { get; internal set; }
+
+ #endregion
+
+ protected virtual MessageProtections RequiredProtection {
+ get { return MessageProtections.None; }
+ }
+ }
+}
diff --git a/src/DotNetOpenAuth.Test/Mocks/TestExpiringMessage.cs b/src/DotNetOpenAuth.Test/Mocks/TestExpiringMessage.cs
new file mode 100644
index 0000000..0aae6ae
--- /dev/null
+++ b/src/DotNetOpenAuth.Test/Mocks/TestExpiringMessage.cs
@@ -0,0 +1,35 @@
+//-----------------------------------------------------------------------
+// <copyright file="TestExpiringMessage.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOpenAuth.Test.Mocks {
+ using System;
+ using System.Diagnostics;
+ using System.Runtime.Serialization;
+ using DotNetOpenAuth.Messaging;
+ using DotNetOpenAuth.Messaging.Bindings;
+ using DotNetOpenAuth.Messaging.Reflection;
+
+ internal class TestExpiringMessage : TestSignedDirectedMessage, IExpiringProtocolMessage {
+ [DebuggerBrowsable(DebuggerBrowsableState.Never)]
+ private DateTime utcCreationDate;
+
+ internal TestExpiringMessage() { }
+
+ internal TestExpiringMessage(MessageTransport transport)
+ : base(transport) {
+ }
+
+ #region IExpiringProtocolMessage Members
+
+ [MessagePart("created_on", IsRequired = true)]
+ DateTime IExpiringProtocolMessage.UtcCreationDate {
+ get { return this.utcCreationDate; }
+ set { this.utcCreationDate = value.ToUniversalTime(); }
+ }
+
+ #endregion
+ }
+}
diff --git a/src/DotNetOpenAuth.Test/Mocks/TestMessage.cs b/src/DotNetOpenAuth.Test/Mocks/TestMessage.cs
new file mode 100644
index 0000000..301e70d
--- /dev/null
+++ b/src/DotNetOpenAuth.Test/Mocks/TestMessage.cs
@@ -0,0 +1,63 @@
+//-----------------------------------------------------------------------
+// <copyright file="TestMessage.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOpenAuth.Test.Mocks {
+ using System;
+ using System.Collections.Generic;
+ using System.Runtime.Serialization;
+ using DotNetOpenAuth.Messaging;
+ using DotNetOpenAuth.Messaging.Reflection;
+
+ internal class TestMessage : IProtocolMessage {
+ private MessageTransport transport;
+ private Dictionary<string, string> extraData = new Dictionary<string, string>();
+
+ internal TestMessage()
+ : this(MessageTransport.Direct) {
+ }
+
+ internal TestMessage(MessageTransport transport) {
+ this.transport = transport;
+ }
+
+ [MessagePart("age", IsRequired = true)]
+ public int Age { get; set; }
+ [MessagePart("Name")]
+ public string Name { get; set; }
+ [MessagePart]
+ public string EmptyMember { get; set; }
+ [MessagePart(null)] // null name tests that Location is still the name.
+ public Uri Location { get; set; }
+ [MessagePart(IsRequired = true)]
+ public DateTime Timestamp { get; set; }
+
+ #region IProtocolMessage Members
+
+ Version IProtocolMessage.ProtocolVersion {
+ get { return new Version(1, 0); }
+ }
+
+ MessageProtections IProtocolMessage.RequiredProtection {
+ get { return MessageProtections.None; }
+ }
+
+ MessageTransport IProtocolMessage.Transport {
+ get { return this.transport; }
+ }
+
+ IDictionary<string, string> IProtocolMessage.ExtraData {
+ get { return this.extraData; }
+ }
+
+ void IProtocolMessage.EnsureValidMessage() {
+ if (this.EmptyMember != null || this.Age < 0) {
+ throw new ProtocolException();
+ }
+ }
+
+ #endregion
+ }
+}
diff --git a/src/DotNetOpenAuth.Test/Mocks/TestMessageTypeProvider.cs b/src/DotNetOpenAuth.Test/Mocks/TestMessageTypeProvider.cs
new file mode 100644
index 0000000..c6bb0c7
--- /dev/null
+++ b/src/DotNetOpenAuth.Test/Mocks/TestMessageTypeProvider.cs
@@ -0,0 +1,56 @@
+//-----------------------------------------------------------------------
+// <copyright file="TestMessageTypeProvider.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOpenAuth.Test.Mocks {
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using DotNetOpenAuth.Messaging;
+
+ internal class TestMessageTypeProvider : IMessageTypeProvider {
+ private bool signedMessages;
+ private bool expiringMessages;
+ private bool replayMessages;
+
+ internal TestMessageTypeProvider()
+ : this(false, false, false) {
+ }
+
+ internal TestMessageTypeProvider(bool signed, bool expiring, bool replay) {
+ if ((!signed && expiring) || (!expiring && replay)) {
+ throw new ArgumentException("Invalid combination of protection.");
+ }
+ this.signedMessages = signed;
+ this.expiringMessages = expiring;
+ this.replayMessages = replay;
+ }
+
+ #region IMessageTypeProvider Members
+
+ public Type GetRequestMessageType(IDictionary<string, string> fields) {
+ if (fields.ContainsKey("age")) {
+ if (this.signedMessages) {
+ if (this.expiringMessages) {
+ if (this.replayMessages) {
+ return typeof(TestReplayProtectedMessage);
+ }
+ return typeof(TestExpiringMessage);
+ }
+ return typeof(TestSignedDirectedMessage);
+ }
+ return typeof(TestMessage);
+ }
+ return null;
+ }
+
+ public Type GetResponseMessageType(IProtocolMessage request, IDictionary<string, string> fields) {
+ return this.GetRequestMessageType(fields);
+ }
+
+ #endregion
+ }
+}
diff --git a/src/DotNetOpenAuth.Test/Mocks/TestReplayProtectedMessage.cs b/src/DotNetOpenAuth.Test/Mocks/TestReplayProtectedMessage.cs
new file mode 100644
index 0000000..396db44
--- /dev/null
+++ b/src/DotNetOpenAuth.Test/Mocks/TestReplayProtectedMessage.cs
@@ -0,0 +1,30 @@
+//-----------------------------------------------------------------------
+// <copyright file="TestReplayProtectedMessage.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOpenAuth.Test.Mocks {
+ using System.Runtime.Serialization;
+ using DotNetOpenAuth.Messaging;
+ using DotNetOpenAuth.Messaging.Bindings;
+ using DotNetOpenAuth.Messaging.Reflection;
+
+ internal class TestReplayProtectedMessage : TestExpiringMessage, IReplayProtectedProtocolMessage {
+ internal TestReplayProtectedMessage() { }
+
+ internal TestReplayProtectedMessage(MessageTransport transport)
+ : base(transport) {
+ }
+
+ #region IReplayProtectedProtocolMessage Members
+
+ [MessagePart("Nonce")]
+ string IReplayProtectedProtocolMessage.Nonce {
+ get;
+ set;
+ }
+
+ #endregion
+ }
+}
diff --git a/src/DotNetOpenAuth.Test/Mocks/TestSignedDirectedMessage.cs b/src/DotNetOpenAuth.Test/Mocks/TestSignedDirectedMessage.cs
new file mode 100644
index 0000000..d665db8
--- /dev/null
+++ b/src/DotNetOpenAuth.Test/Mocks/TestSignedDirectedMessage.cs
@@ -0,0 +1,33 @@
+//-----------------------------------------------------------------------
+// <copyright file="TestSignedDirectedMessage.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOpenAuth.Test.Mocks {
+ using DotNetOpenAuth.Messaging;
+ using DotNetOpenAuth.Messaging.Bindings;
+ using DotNetOpenAuth.Messaging.Reflection;
+
+ internal class TestSignedDirectedMessage : TestDirectedMessage, ITamperResistantProtocolMessage {
+ internal TestSignedDirectedMessage() { }
+
+ internal TestSignedDirectedMessage(MessageTransport transport)
+ : base(transport) {
+ }
+
+ #region ISignedProtocolMessage Members
+
+ [MessagePart]
+ public string Signature {
+ get;
+ set;
+ }
+
+ #endregion
+
+ protected override MessageProtections RequiredProtection {
+ get { return MessageProtections.TamperProtection; }
+ }
+ }
+}
diff --git a/src/DotNetOpenAuth.Test/Mocks/TestWebRequestHandler.cs b/src/DotNetOpenAuth.Test/Mocks/TestWebRequestHandler.cs
new file mode 100644
index 0000000..d7092b4
--- /dev/null
+++ b/src/DotNetOpenAuth.Test/Mocks/TestWebRequestHandler.cs
@@ -0,0 +1,46 @@
+//-----------------------------------------------------------------------
+// <copyright file="TestWebRequestHandler.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOpenAuth.Test.Mocks {
+ using System;
+ using System.IO;
+ using System.Net;
+ using System.Text;
+ using DotNetOpenAuth.Messaging;
+ using DotNetOpenAuth.OAuth.ChannelElements;
+
+ internal class TestWebRequestHandler : IWebRequestHandler {
+ private StringBuilder postEntity;
+
+ internal Func<HttpWebRequest, Response> Callback { get; set; }
+
+ internal Stream RequestEntityStream {
+ get {
+ if (this.postEntity == null) {
+ return null;
+ }
+ return new MemoryStream(Encoding.UTF8.GetBytes(this.postEntity.ToString()));
+ }
+ }
+
+ #region IWebRequestHandler Members
+
+ public TextWriter GetRequestStream(HttpWebRequest request) {
+ this.postEntity = new StringBuilder();
+ return new StringWriter(this.postEntity);
+ }
+
+ public Response GetResponse(HttpWebRequest request) {
+ if (this.Callback == null) {
+ throw new InvalidOperationException("Set the Callback property first.");
+ }
+
+ return this.Callback(request);
+ }
+
+ #endregion
+ }
+}