diff options
Diffstat (limited to 'src/DotNetOAuth.Test')
11 files changed, 351 insertions, 210 deletions
diff --git a/src/DotNetOAuth.Test/DotNetOAuth.Test.csproj b/src/DotNetOAuth.Test/DotNetOAuth.Test.csproj index 85957c8..32e17aa 100644 --- a/src/DotNetOAuth.Test/DotNetOAuth.Test.csproj +++ b/src/DotNetOAuth.Test/DotNetOAuth.Test.csproj @@ -58,12 +58,15 @@ </Reference>
</ItemGroup>
<ItemGroup>
+ <Compile Include="Messaging\MessagingTestBase.cs" />
<Compile Include="Messaging\MessagingUtilitiesTests.cs" />
<Compile Include="Messaging\ChannelTests.cs" />
<Compile Include="Messaging\DictionaryXmlReaderTests.cs" />
<Compile Include="Messaging\HttpRequestInfoTests.cs" />
<Compile Include="Messaging\ProtocolExceptionTests.cs" />
- <Compile Include="Mocks\TestReplayProtectedChannel.cs" />
+ <Compile Include="Messaging\StandardMessageExpirationBindingElementTests.cs" />
+ <Compile Include="Mocks\MockTransformationBindingElement.cs" />
+ <Compile Include="Mocks\MockReplayProtectionBindingElement.cs" />
<Compile Include="Mocks\TestBaseMessage.cs" />
<Compile Include="Mocks\TestDerivedMessage.cs" />
<Compile Include="Mocks\TestReplayProtectedMessage.cs" />
@@ -71,7 +74,7 @@ <Compile Include="Mocks\TestBadChannel.cs" />
<Compile Include="Mocks\TestExpiringMessage.cs" />
<Compile Include="Mocks\TestSignedDirectedMessage.cs" />
- <Compile Include="Mocks\TestSigningChannel.cs" />
+ <Compile Include="Mocks\MockSigningBindingElement.cs" />
<Compile Include="Mocks\TestWebRequestHandler.cs" />
<Compile Include="OAuthChannelTests.cs" />
<Compile Include="Messaging\MessageSerializerTests.cs" />
diff --git a/src/DotNetOAuth.Test/Messaging/ChannelTests.cs b/src/DotNetOAuth.Test/Messaging/ChannelTests.cs index ca80dba..6f58133 100644 --- a/src/DotNetOAuth.Test/Messaging/ChannelTests.cs +++ b/src/DotNetOAuth.Test/Messaging/ChannelTests.cs @@ -10,22 +10,12 @@ namespace DotNetOAuth.Test.Messaging { using System.IO;
using System.Net;
using System.Web;
- using System.Xml;
using DotNetOAuth.Messaging;
using DotNetOAuth.Test.Mocks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
- public class ChannelTests : TestBase {
- private Channel channel;
-
- [TestInitialize]
- public override void SetUp() {
- base.SetUp();
-
- this.channel = new TestChannel();
- }
-
+ public class ChannelTests : MessagingTestBase {
[TestMethod, ExpectedException(typeof(ArgumentNullException))]
public void CtorNull() {
// This bad channel is deliberately constructed to pass null to
@@ -35,7 +25,7 @@ namespace DotNetOAuth.Test.Messaging { [TestMethod]
public void DequeueIndirectOrResponseMessageReturnsNull() {
- Assert.IsNull(this.channel.DequeueIndirectOrResponseMessage());
+ Assert.IsNull(this.Channel.DequeueIndirectOrResponseMessage());
}
[TestMethod]
@@ -50,25 +40,25 @@ namespace DotNetOAuth.Test.Messaging { [TestMethod, ExpectedException(typeof(ArgumentNullException))]
public void SendNull() {
- this.channel.Send(null);
+ this.Channel.Send(null);
}
[TestMethod, ExpectedException(typeof(ArgumentException))]
public void SendIndirectedUndirectedMessage() {
IProtocolMessage message = new TestMessage(MessageTransport.Indirect);
- this.channel.Send(message);
+ this.Channel.Send(message);
}
[TestMethod, ExpectedException(typeof(ArgumentException))]
public void SendDirectedNoRecipientMessage() {
IProtocolMessage message = new TestDirectedMessage(MessageTransport.Indirect);
- this.channel.Send(message);
+ this.Channel.Send(message);
}
[TestMethod, ExpectedException(typeof(ArgumentException))]
public void SendInvalidMessageTransport() {
IProtocolMessage message = new TestDirectedMessage((MessageTransport)100);
- this.channel.Send(message);
+ this.Channel.Send(message);
}
[TestMethod]
@@ -79,8 +69,8 @@ namespace DotNetOAuth.Test.Messaging { Location = new Uri("http://host/path"),
Recipient = new Uri("http://provider/path"),
};
- this.channel.Send(message);
- Response response = this.channel.DequeueIndirectOrResponseMessage();
+ this.Channel.Send(message);
+ Response response = this.Channel.DequeueIndirectOrResponseMessage();
Assert.AreEqual(HttpStatusCode.Redirect, response.Status);
StringAssert.StartsWith(response.Headers[HttpResponseHeader.Location], "http://provider/path");
StringAssert.Contains(response.Headers[HttpResponseHeader.Location], "age=15");
@@ -120,8 +110,8 @@ namespace DotNetOAuth.Test.Messaging { Location = new Uri("http://host/path"),
Recipient = new Uri("http://provider/path"),
};
- this.channel.Send(message);
- Response response = this.channel.DequeueIndirectOrResponseMessage();
+ this.Channel.Send(message);
+ Response response = this.Channel.DequeueIndirectOrResponseMessage();
Assert.AreEqual(HttpStatusCode.OK, response.Status, "A form redirect should be an HTTP successful response.");
Assert.IsNull(response.Headers[HttpResponseHeader.Location], "There should not be a redirection header in the response.");
string body = response.Body;
@@ -169,7 +159,7 @@ namespace DotNetOAuth.Test.Messaging { Name = "Andrew",
Location = new Uri("http://host/path"),
};
- this.channel.Send(message);
+ this.Channel.Send(message);
}
[TestMethod, ExpectedException(typeof(ArgumentNullException))]
@@ -209,7 +199,7 @@ namespace DotNetOAuth.Test.Messaging { // TODO: make this a request with a message in it.
HttpRequest request = new HttpRequest("somefile", "http://someurl", "age=15");
HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter()));
- IProtocolMessage message = this.channel.ReadFromRequest();
+ IProtocolMessage message = this.Channel.ReadFromRequest();
Assert.IsNotNull(message);
Assert.IsInstanceOfType(message, typeof(TestMessage));
Assert.AreEqual(15, ((TestMessage)message).Age);
@@ -227,154 +217,72 @@ namespace DotNetOAuth.Test.Messaging { badChannel.ReadFromRequest(null);
}
- [TestMethod, ExpectedException(typeof(NotSupportedException))]
- public void SendSigningMessagesNotSupported() {
- TestSignedDirectedMessage message = new TestSignedDirectedMessage(MessageTransport.Direct);
- message.Recipient = new Uri("http://localtest");
- this.channel.Send(message);
- }
-
- [TestMethod]
- public void SendSetsTimestamp() {
- TestExpiringMessage message = new TestExpiringMessage(MessageTransport.Indirect);
- message.Recipient = new Uri("http://localtest");
- ((IExpiringProtocolMessage)message).UtcCreationDate = DateTime.Parse("1/1/1990");
-
- Channel channel = new TestSigningChannel(true, false);
- channel.Send(message);
- Assert.IsTrue(DateTime.UtcNow - ((IExpiringProtocolMessage)message).UtcCreationDate < TimeSpan.FromSeconds(3), "The timestamp on the message was not set on send.");
- }
-
- [TestMethod, ExpectedException(typeof(NotSupportedException))]
- public void SendReplayProtectedMessageNotSupported() {
- TestReplayProtectedMessage message = new TestReplayProtectedMessage(MessageTransport.Indirect);
- message.Recipient = new Uri("http://localtest");
-
- Channel channel = new TestSigningChannel(true, false); // use this one to get passed signing NotSupportedException
- channel.Send(message);
- }
-
[TestMethod]
public void SendReplayProtectedMessageSetsNonce() {
TestReplayProtectedMessage message = new TestReplayProtectedMessage(MessageTransport.Indirect);
message.Recipient = new Uri("http://localtest");
- Channel channel = new TestReplayProtectedChannel();
- channel.Send(message);
+ this.Channel = CreateChannel(ChannelProtection.ReplayProtection, ChannelProtection.ReplayProtection);
+ this.Channel.Send(message);
Assert.IsNotNull(((IReplayProtectedProtocolMessage)message).Nonce);
}
- [TestMethod, ExpectedException(typeof(NotSupportedException))]
- public void ReceivedSignedMessagesNotSupported() {
- // Create a channel that doesn't support signed messages, but will recognize one.
- this.channel = new TestChannel(new TestMessageTypeProvider(true, false, false));
- this.ParameterizedReceiveTest("GET");
- }
-
[TestMethod, ExpectedException(typeof(InvalidSignatureException))]
public void ReceivedInvalidSignature() {
- this.channel = new TestSigningChannel(false, false);
+ this.Channel = CreateChannel(ChannelProtection.TamperProtection, ChannelProtection.TamperProtection);
this.ParameterizedReceiveProtectedTest(DateTime.UtcNow, true);
}
[TestMethod]
- public void VerifyGoodTimestampIsAccepted() {
- // Create a channel that supports and recognizes signed messages.
- this.channel = new TestSigningChannel(true, false);
- this.ParameterizedReceiveProtectedTest(DateTime.UtcNow, false);
- }
-
- [TestMethod, ExpectedException(typeof(ExpiredMessageException))]
- public void VerifyBadTimestampIsRejected() {
- // Create a channel that supports and recognizes signed messages.
- this.channel = new TestSigningChannel(true, false);
- this.ParameterizedReceiveProtectedTest(DateTime.UtcNow - this.channel.MaximumMessageAge - TimeSpan.FromSeconds(1), false);
- }
-
- [TestMethod]
public void ReceivedReplayProtectedMessageJustOnce() {
- this.channel = new TestReplayProtectedChannel();
+ this.Channel = CreateChannel(ChannelProtection.ReplayProtection, ChannelProtection.ReplayProtection);
this.ParameterizedReceiveProtectedTest(DateTime.UtcNow, false);
}
[TestMethod, ExpectedException(typeof(ReplayedMessageException))]
public void ReceivedReplayProtectedMessageTwice() {
- this.channel = new TestReplayProtectedChannel();
+ this.Channel = CreateChannel(ChannelProtection.ReplayProtection, ChannelProtection.ReplayProtection);
this.ParameterizedReceiveProtectedTest(DateTime.UtcNow, false);
this.ParameterizedReceiveProtectedTest(DateTime.UtcNow, false);
}
- [TestMethod, ExpectedException(typeof(NotSupportedException))]
- public void ReceivedReplayProtectedMessagesNotSupported() {
- // Create a channel that doesn't support replay protected messages, but will recognize one.
- this.channel = new TestSigningChannel(true, true);
- this.ParameterizedReceiveProtectedTest(DateTime.UtcNow, false);
+ [TestMethod, ExpectedException(typeof(ProtocolException))]
+ public void MessageExpirationWithoutTamperResistance() {
+ new TestChannel(
+ new TestMessageTypeProvider(),
+ new StandardMessageExpirationBindingElement());
}
- private static HttpRequestInfo CreateHttpRequestInfo(string method, IDictionary<string, string> fields) {
- string query = MessagingUtilities.CreateQueryString(fields);
- UriBuilder requestUri = new UriBuilder("http://localhost/path");
- WebHeaderCollection headers = new WebHeaderCollection();
- MemoryStream ms = new MemoryStream();
- if (method == "POST") {
- headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
- StreamWriter sw = new StreamWriter(ms);
- sw.Write(query);
- sw.Flush();
- ms.Position = 0;
- } else if (method == "GET") {
- requestUri.Query = query;
- } else {
- throw new ArgumentOutOfRangeException("method", method, "Expected POST or GET");
- }
- HttpRequestInfo request = new HttpRequestInfo {
- HttpMethod = method,
- Url = requestUri.Uri,
- Headers = headers,
- InputStream = ms,
- };
-
- return request;
+ [TestMethod, ExpectedException(typeof(ProtocolException))]
+ public void TooManyBindingElementsProvidingSameProtection() {
+ new TestChannel(
+ new TestMessageTypeProvider(),
+ new MockSigningBindingElement(),
+ new MockSigningBindingElement());
}
- private void ParameterizedReceiveTest(string method) {
- var fields = new Dictionary<string, string> {
- { "age", "15" },
- { "Name", "Andrew" },
- { "Location", "http://hostb/pathB" },
- };
- IProtocolMessage requestMessage = this.channel.ReadFromRequest(CreateHttpRequestInfo(method, fields));
- Assert.IsNotNull(requestMessage);
- Assert.IsInstanceOfType(requestMessage, typeof(TestMessage));
- TestMessage testMessage = (TestMessage)requestMessage;
- Assert.AreEqual(15, testMessage.Age);
- Assert.AreEqual("Andrew", testMessage.Name);
- Assert.AreEqual("http://hostb/pathB", testMessage.Location.AbsoluteUri);
- }
-
- private void ParameterizedReceiveProtectedTest(DateTime? utcCreatedDate, bool invalidSignature) {
- var fields = new Dictionary<string, string> {
- { "age", "15" },
- { "Name", "Andrew" },
- { "Location", "http://hostb/pathB" },
- { "Signature", invalidSignature ? "badsig" : TestSigningChannel.MessageSignature },
- { "Nonce", "someNonce" },
- };
- if (utcCreatedDate.HasValue) {
- utcCreatedDate = DateTime.Parse(utcCreatedDate.Value.ToUniversalTime().ToString()); // round off the milliseconds so comparisons work later
- fields.Add("created_on", XmlConvert.ToString(utcCreatedDate.Value, XmlDateTimeSerializationMode.Utc));
- }
- IProtocolMessage requestMessage = this.channel.ReadFromRequest(CreateHttpRequestInfo("GET", fields));
- Assert.IsNotNull(requestMessage);
- Assert.IsInstanceOfType(requestMessage, typeof(TestSignedDirectedMessage));
- TestSignedDirectedMessage testMessage = (TestSignedDirectedMessage)requestMessage;
- Assert.AreEqual(15, testMessage.Age);
- Assert.AreEqual("Andrew", testMessage.Name);
- Assert.AreEqual("http://hostb/pathB", testMessage.Location.AbsoluteUri);
- if (utcCreatedDate.HasValue) {
- IExpiringProtocolMessage expiringMessage = (IExpiringProtocolMessage)requestMessage;
- Assert.AreEqual(utcCreatedDate.Value, expiringMessage.UtcCreationDate);
- }
+ [TestMethod]
+ public void BindingElementsOrdering() {
+ IChannelBindingElement transformA = new MockTransformationBindingElement("a");
+ IChannelBindingElement transformB = new MockTransformationBindingElement("b");
+ IChannelBindingElement sign = new MockSigningBindingElement();
+ IChannelBindingElement replay = new MockReplayProtectionBindingElement();
+ IChannelBindingElement expire = new StandardMessageExpirationBindingElement();
+
+ Channel channel = new TestChannel(
+ new TestMessageTypeProvider(),
+ sign,
+ replay,
+ expire,
+ transformB,
+ transformA);
+
+ Assert.AreEqual(5, channel.BindingElements.Count);
+ Assert.AreSame(transformB, channel.BindingElements[0]);
+ Assert.AreSame(transformA, channel.BindingElements[1]);
+ Assert.AreSame(replay, channel.BindingElements[2]);
+ Assert.AreSame(expire, channel.BindingElements[3]);
+ Assert.AreSame(sign, channel.BindingElements[4]);
}
}
}
diff --git a/src/DotNetOAuth.Test/Messaging/MessagingTestBase.cs b/src/DotNetOAuth.Test/Messaging/MessagingTestBase.cs new file mode 100644 index 0000000..61be6a7 --- /dev/null +++ b/src/DotNetOAuth.Test/Messaging/MessagingTestBase.cs @@ -0,0 +1,120 @@ +//-----------------------------------------------------------------------
+// <copyright file="MessagingTestBase.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOAuth.Test {
+ using System;
+ using System.Collections.Generic;
+ using System.IO;
+ using System.Net;
+ using System.Xml;
+ using DotNetOAuth.Messaging;
+ using DotNetOAuth.Test.Mocks;
+ using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+ /// <summary>
+ /// The base class that all messaging test classes inherit from.
+ /// </summary>
+ public class MessagingTestBase : TestBase {
+ internal Channel Channel { get; set; }
+
+ [TestInitialize]
+ public override void SetUp() {
+ base.SetUp();
+
+ this.Channel = new TestChannel();
+ }
+
+ internal static HttpRequestInfo CreateHttpRequestInfo(string method, IDictionary<string, string> fields) {
+ string query = MessagingUtilities.CreateQueryString(fields);
+ UriBuilder requestUri = new UriBuilder("http://localhost/path");
+ WebHeaderCollection headers = new WebHeaderCollection();
+ MemoryStream ms = new MemoryStream();
+ if (method == "POST") {
+ headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
+ StreamWriter sw = new StreamWriter(ms);
+ sw.Write(query);
+ sw.Flush();
+ ms.Position = 0;
+ } else if (method == "GET") {
+ requestUri.Query = query;
+ } else {
+ throw new ArgumentOutOfRangeException("method", method, "Expected POST or GET");
+ }
+ HttpRequestInfo request = new HttpRequestInfo {
+ HttpMethod = method,
+ Url = requestUri.Uri,
+ Headers = headers,
+ InputStream = ms,
+ };
+
+ return request;
+ }
+
+ internal static Channel CreateChannel(ChannelProtection capabilityAndRecognition) {
+ return CreateChannel(capabilityAndRecognition, capabilityAndRecognition);
+ }
+
+ internal static Channel CreateChannel(ChannelProtection capability, ChannelProtection recognition) {
+ bool signing = false, expiration = false, replay = false;
+ var bindingElements = new List<IChannelBindingElement>();
+ if (capability >= ChannelProtection.TamperProtection) {
+ bindingElements.Add(new MockSigningBindingElement());
+ signing = true;
+ }
+ if (capability >= ChannelProtection.Expiration) {
+ bindingElements.Add(new StandardMessageExpirationBindingElement());
+ expiration = true;
+ }
+ if (capability >= ChannelProtection.ReplayProtection) {
+ bindingElements.Add(new MockReplayProtectionBindingElement());
+ replay = true;
+ }
+
+ var typeProvider = new TestMessageTypeProvider(signing, expiration, replay);
+ return new TestChannel(typeProvider, bindingElements.ToArray());
+ }
+
+ internal void ParameterizedReceiveTest(string method) {
+ var fields = new Dictionary<string, string> {
+ { "age", "15" },
+ { "Name", "Andrew" },
+ { "Location", "http://hostb/pathB" },
+ };
+ IProtocolMessage requestMessage = this.Channel.ReadFromRequest(CreateHttpRequestInfo(method, fields));
+ Assert.IsNotNull(requestMessage);
+ Assert.IsInstanceOfType(requestMessage, typeof(TestMessage));
+ TestMessage testMessage = (TestMessage)requestMessage;
+ Assert.AreEqual(15, testMessage.Age);
+ Assert.AreEqual("Andrew", testMessage.Name);
+ Assert.AreEqual("http://hostb/pathB", testMessage.Location.AbsoluteUri);
+ }
+
+ internal void ParameterizedReceiveProtectedTest(DateTime? utcCreatedDate, bool invalidSignature) {
+ var fields = new Dictionary<string, string> {
+ { "age", "15" },
+ { "Name", "Andrew" },
+ { "Location", "http://hostb/pathB" },
+ { "Signature", invalidSignature ? "badsig" : MockSigningBindingElement.MessageSignature },
+ { "Nonce", "someNonce" },
+ };
+ if (utcCreatedDate.HasValue) {
+ utcCreatedDate = DateTime.Parse(utcCreatedDate.Value.ToUniversalTime().ToString()); // round off the milliseconds so comparisons work later
+ fields.Add("created_on", XmlConvert.ToString(utcCreatedDate.Value, XmlDateTimeSerializationMode.Utc));
+ }
+ IProtocolMessage requestMessage = this.Channel.ReadFromRequest(CreateHttpRequestInfo("GET", fields));
+ Assert.IsNotNull(requestMessage);
+ Assert.IsInstanceOfType(requestMessage, typeof(TestSignedDirectedMessage));
+ TestSignedDirectedMessage testMessage = (TestSignedDirectedMessage)requestMessage;
+ Assert.AreEqual(15, testMessage.Age);
+ Assert.AreEqual("Andrew", testMessage.Name);
+ Assert.AreEqual("http://hostb/pathB", testMessage.Location.AbsoluteUri);
+ if (utcCreatedDate.HasValue) {
+ IExpiringProtocolMessage expiringMessage = (IExpiringProtocolMessage)requestMessage;
+ Assert.AreEqual(utcCreatedDate.Value, expiringMessage.UtcCreationDate);
+ }
+ }
+ }
+}
diff --git a/src/DotNetOAuth.Test/Messaging/StandardMessageExpirationBindingElementTests.cs b/src/DotNetOAuth.Test/Messaging/StandardMessageExpirationBindingElementTests.cs new file mode 100644 index 0000000..5a664a4 --- /dev/null +++ b/src/DotNetOAuth.Test/Messaging/StandardMessageExpirationBindingElementTests.cs @@ -0,0 +1,38 @@ +//-----------------------------------------------------------------------
+// <copyright file="StandardMessageExpirationBindingElementTests.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOAuth.Test.Messaging {
+ using System;
+ using DotNetOAuth.Messaging;
+ using DotNetOAuth.Test.Mocks;
+ using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+ [TestClass]
+ public class StandardMessageExpirationBindingElementTests : MessagingTestBase {
+ [TestMethod]
+ public void SendSetsTimestamp() {
+ TestExpiringMessage message = new TestExpiringMessage(MessageTransport.Indirect);
+ message.Recipient = new Uri("http://localtest");
+ ((IExpiringProtocolMessage)message).UtcCreationDate = DateTime.Parse("1/1/1990");
+
+ Channel channel = CreateChannel(ChannelProtection.Expiration, ChannelProtection.Expiration);
+ channel.Send(message);
+ Assert.IsTrue(DateTime.UtcNow - ((IExpiringProtocolMessage)message).UtcCreationDate < TimeSpan.FromSeconds(3), "The timestamp on the message was not set on send.");
+ }
+
+ [TestMethod]
+ public void VerifyGoodTimestampIsAccepted() {
+ this.Channel = CreateChannel(ChannelProtection.Expiration, ChannelProtection.Expiration);
+ this.ParameterizedReceiveProtectedTest(DateTime.UtcNow, false);
+ }
+
+ [TestMethod, ExpectedException(typeof(ExpiredMessageException))]
+ public void VerifyBadTimestampIsRejected() {
+ this.Channel = CreateChannel(ChannelProtection.Expiration, ChannelProtection.Expiration);
+ this.ParameterizedReceiveProtectedTest(DateTime.UtcNow - StandardMessageExpirationBindingElement.DefaultMaximumMessageAge - TimeSpan.FromSeconds(1), false);
+ }
+ }
+}
diff --git a/src/DotNetOAuth.Test/Mocks/MockReplayProtectionBindingElement.cs b/src/DotNetOAuth.Test/Mocks/MockReplayProtectionBindingElement.cs new file mode 100644 index 0000000..ff1d709 --- /dev/null +++ b/src/DotNetOAuth.Test/Mocks/MockReplayProtectionBindingElement.cs @@ -0,0 +1,45 @@ +//-----------------------------------------------------------------------
+// <copyright file="MockReplayProtectionBindingElement.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOAuth.Test.Mocks {
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using DotNetOAuth.Messaging;
+ using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+ internal class MockReplayProtectionBindingElement : IChannelBindingElement {
+ private bool messageReceived;
+
+ #region IChannelBindingElement Members
+
+ ChannelProtection IChannelBindingElement.Protection {
+ get { return ChannelProtection.ReplayProtection; }
+ }
+
+ void IChannelBindingElement.PrepareMessageForSending(IProtocolMessage message) {
+ var replayMessage = message as IReplayProtectedProtocolMessage;
+ if (replayMessage != null) {
+ replayMessage.Nonce = "someNonce";
+ }
+ }
+
+ void 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;
+ }
+ }
+
+ #endregion
+ }
+}
diff --git a/src/DotNetOAuth.Test/Mocks/MockSigningBindingElement.cs b/src/DotNetOAuth.Test/Mocks/MockSigningBindingElement.cs new file mode 100644 index 0000000..5cf8be6 --- /dev/null +++ b/src/DotNetOAuth.Test/Mocks/MockSigningBindingElement.cs @@ -0,0 +1,41 @@ +//-----------------------------------------------------------------------
+// <copyright file="MockSigningBindingElement.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOAuth.Test.Mocks {
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using DotNetOAuth.Messaging;
+
+ internal class MockSigningBindingElement : IChannelBindingElement {
+ internal const string MessageSignature = "mocksignature";
+
+ #region IChannelBindingElement Members
+
+ ChannelProtection IChannelBindingElement.Protection {
+ get { return ChannelProtection.TamperProtection; }
+ }
+
+ void IChannelBindingElement.PrepareMessageForSending(IProtocolMessage message) {
+ ISignedProtocolMessage signedMessage = message as ISignedProtocolMessage;
+ if (signedMessage != null) {
+ signedMessage.Signature = MessageSignature;
+ }
+ }
+
+ void IChannelBindingElement.PrepareMessageForReceiving(IProtocolMessage message) {
+ ISignedProtocolMessage signedMessage = message as ISignedProtocolMessage;
+ if (signedMessage != null) {
+ if (signedMessage.Signature != MessageSignature) {
+ throw new InvalidSignatureException(message);
+ }
+ }
+ }
+
+ #endregion
+ }
+}
diff --git a/src/DotNetOAuth.Test/Mocks/MockTransformationBindingElement.cs b/src/DotNetOAuth.Test/Mocks/MockTransformationBindingElement.cs new file mode 100644 index 0000000..7a1320b --- /dev/null +++ b/src/DotNetOAuth.Test/Mocks/MockTransformationBindingElement.cs @@ -0,0 +1,49 @@ +//-----------------------------------------------------------------------
+// <copyright file="MockTransformationBindingElement.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOAuth.Test.Mocks {
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using DotNetOAuth.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
+
+ ChannelProtection IChannelBindingElement.Protection {
+ get { return ChannelProtection.None; }
+ }
+
+ void IChannelBindingElement.PrepareMessageForSending(IProtocolMessage message) {
+ var testMessage = message as TestMessage;
+ if (testMessage != null) {
+ testMessage.Name = this.transform + testMessage.Name;
+ }
+ }
+
+ void 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);
+ }
+ }
+
+ #endregion
+ }
+}
diff --git a/src/DotNetOAuth.Test/Mocks/TestChannel.cs b/src/DotNetOAuth.Test/Mocks/TestChannel.cs index 2431af1..b69b756 100644 --- a/src/DotNetOAuth.Test/Mocks/TestChannel.cs +++ b/src/DotNetOAuth.Test/Mocks/TestChannel.cs @@ -16,8 +16,8 @@ namespace DotNetOAuth.Test.Mocks { : this(new TestMessageTypeProvider()) {
}
- internal TestChannel(IMessageTypeProvider messageTypeProvider)
- : base(messageTypeProvider) {
+ internal TestChannel(IMessageTypeProvider messageTypeProvider, params IChannelBindingElement[] bindingElements)
+ : base(messageTypeProvider, bindingElements) {
}
protected override IProtocolMessage RequestInternal(IDirectedProtocolMessage request) {
diff --git a/src/DotNetOAuth.Test/Mocks/TestReplayProtectedChannel.cs b/src/DotNetOAuth.Test/Mocks/TestReplayProtectedChannel.cs deleted file mode 100644 index d57b72c..0000000 --- a/src/DotNetOAuth.Test/Mocks/TestReplayProtectedChannel.cs +++ /dev/null @@ -1,35 +0,0 @@ -//-----------------------------------------------------------------------
-// <copyright file="TestReplayProtectedChannel.cs" company="Andrew Arnott">
-// Copyright (c) Andrew Arnott. All rights reserved.
-// </copyright>
-//-----------------------------------------------------------------------
-
-namespace DotNetOAuth.Test.Mocks {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using DotNetOAuth.Messaging;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
-
- internal class TestReplayProtectedChannel : TestSigningChannel {
- private bool messageReceived;
-
- internal TestReplayProtectedChannel()
- : base(true, true) {
- }
-
- protected override bool IsMessageReplayed(DotNetOAuth.Messaging.IReplayProtectedProtocolMessage message) {
- Assert.AreEqual("someNonce", message.Nonce, "The nonce didn't serialize correctly, or something");
- // this mock implementation passes the first time and fails subsequent times.
- bool replay = this.messageReceived;
- this.messageReceived = true;
- return replay;
- }
-
- protected override void ApplyReplayProtection(IReplayProtectedProtocolMessage message) {
- message.Nonce = "someNonce";
- // no-op
- }
- }
-}
diff --git a/src/DotNetOAuth.Test/Mocks/TestSigningChannel.cs b/src/DotNetOAuth.Test/Mocks/TestSigningChannel.cs deleted file mode 100644 index 60037ee..0000000 --- a/src/DotNetOAuth.Test/Mocks/TestSigningChannel.cs +++ /dev/null @@ -1,29 +0,0 @@ -//-----------------------------------------------------------------------
-// <copyright file="TestSigningChannel.cs" company="Andrew Arnott">
-// Copyright (c) Andrew Arnott. All rights reserved.
-// </copyright>
-//-----------------------------------------------------------------------
-
-namespace DotNetOAuth.Test.Mocks {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using DotNetOAuth.Messaging;
-
- internal class TestSigningChannel : TestChannel {
- internal const string MessageSignature = "mocksignature";
-
- internal TestSigningChannel(bool expiring, bool replay)
- : base(new TestMessageTypeProvider(true, expiring, replay)) {
- }
-
- protected override void Sign(ISignedProtocolMessage message) {
- message.Signature = MessageSignature;
- }
-
- protected override bool IsSignatureValid(ISignedProtocolMessage message) {
- return message.Signature == MessageSignature;
- }
- }
-}
diff --git a/src/DotNetOAuth.Test/TestBase.cs b/src/DotNetOAuth.Test/TestBase.cs index e41b01c..9fce27c 100644 --- a/src/DotNetOAuth.Test/TestBase.cs +++ b/src/DotNetOAuth.Test/TestBase.cs @@ -3,6 +3,7 @@ // Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
+
namespace DotNetOAuth.Test {
using System.Reflection;
using log4net;
|