1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
//-----------------------------------------------------------------------
// <copyright file="ChannelTests.cs" company="Outercurve Foundation">
// Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.Test.Messaging {
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.Messaging.Bindings;
using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.Test.Mocks;
using NUnit.Framework;
[TestFixture]
public class ChannelTests : MessagingTestBase {
[Test, ExpectedException(typeof(ArgumentNullException))]
public void CtorNullFirstParameter() {
new TestBadChannel(null, new IChannelBindingElement[0], new DefaultOpenIdHostFactories());
}
[Test, ExpectedException(typeof(ArgumentNullException))]
public void CtorNullSecondParameter() {
new TestBadChannel(new TestMessageFactory(), null, new DefaultOpenIdHostFactories());
}
[Test, ExpectedException(typeof(ArgumentNullException))]
public void CtorNullThirdParameter() {
new TestBadChannel(new TestMessageFactory(), new IChannelBindingElement[0], null);
}
[Test]
public async Task ReadFromRequestQueryString() {
await this.ParameterizedReceiveTestAsync(HttpMethod.Get);
}
[Test]
public async Task ReadFromRequestForm() {
await this.ParameterizedReceiveTestAsync(HttpMethod.Post);
}
/// <summary>
/// Verifies compliance to OpenID 2.0 section 5.1.1 by verifying the channel
/// will reject messages that come with an unexpected HTTP verb.
/// </summary>
[Test, ExpectedException(typeof(ProtocolException))]
public async Task ReadFromRequestDisallowedHttpMethod() {
var fields = GetStandardTestFields(FieldFill.CompleteBeforeBindings);
fields["GetOnly"] = "true";
await this.Channel.ReadFromRequestAsync(CreateHttpRequestInfo(HttpMethod.Post, fields), CancellationToken.None);
}
[Test, ExpectedException(typeof(ArgumentNullException))]
public async Task SendNull() {
await this.Channel.PrepareResponseAsync(null);
}
[Test, ExpectedException(typeof(ArgumentException))]
public async Task SendIndirectedUndirectedMessage() {
IProtocolMessage message = new TestDirectedMessage(MessageTransport.Indirect);
await this.Channel.PrepareResponseAsync(message);
}
[Test, ExpectedException(typeof(ArgumentException))]
public async Task SendDirectedNoRecipientMessage() {
IProtocolMessage message = new TestDirectedMessage(MessageTransport.Indirect);
await this.Channel.PrepareResponseAsync(message);
}
[Test, ExpectedException(typeof(ArgumentException))]
public async Task SendInvalidMessageTransport() {
IProtocolMessage message = new TestDirectedMessage((MessageTransport)100);
await this.Channel.PrepareResponseAsync(message);
}
[Test]
public async Task SendIndirectMessage301Get() {
TestDirectedMessage message = new TestDirectedMessage(MessageTransport.Indirect);
GetStandardTestMessage(FieldFill.CompleteBeforeBindings, message);
message.Recipient = new Uri("http://provider/path");
var expected = GetStandardTestFields(FieldFill.CompleteBeforeBindings);
var response = await this.Channel.PrepareResponseAsync(message);
Assert.AreEqual(HttpStatusCode.Redirect, response.StatusCode);
Assert.AreEqual("text/html; charset=utf-8", response.Content.Headers.ContentType.ToString());
Assert.IsTrue(response.Content != null && response.Content.Headers.ContentLength > 0); // a non-empty body helps get passed filters like WebSense
StringAssert.StartsWith("http://provider/path", response.Headers.Location.AbsoluteUri);
foreach (var pair in expected) {
string key = MessagingUtilities.EscapeUriDataStringRfc3986(pair.Key);
string value = MessagingUtilities.EscapeUriDataStringRfc3986(pair.Value);
string substring = string.Format("{0}={1}", key, value);
StringAssert.Contains(substring, response.Headers.Location.AbsoluteUri);
}
}
[Test, ExpectedException(typeof(ArgumentNullException))]
public void SendIndirectMessage301GetNullMessage() {
TestBadChannel badChannel = new TestBadChannel();
badChannel.Create301RedirectResponse(null, new Dictionary<string, string>());
}
[Test, ExpectedException(typeof(ArgumentException))]
public void SendIndirectMessage301GetEmptyRecipient() {
TestBadChannel badChannel = new TestBadChannel();
var message = new TestDirectedMessage(MessageTransport.Indirect);
badChannel.Create301RedirectResponse(message, new Dictionary<string, string>());
}
[Test, ExpectedException(typeof(ArgumentNullException))]
public void SendIndirectMessage301GetNullFields() {
TestBadChannel badChannel = new TestBadChannel();
var message = new TestDirectedMessage(MessageTransport.Indirect);
message.Recipient = new Uri("http://someserver");
badChannel.Create301RedirectResponse(message, null);
}
[Test]
public async Task SendIndirectMessageFormPost() {
// We craft a very large message to force fallback to form POST.
// We'll also stick some HTML reserved characters in the string value
// to test proper character escaping.
var message = new TestDirectedMessage(MessageTransport.Indirect) {
Age = 15,
Name = "c<b" + new string('a', 10 * 1024),
Location = new Uri("http://host/path"),
Recipient = new Uri("http://provider/path"),
};
var response = await this.Channel.PrepareResponseAsync(message);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "A form redirect should be an HTTP successful response.");
Assert.IsNull(response.Headers.Location, "There should not be a redirection header in the response.");
string body = await response.Content.ReadAsStringAsync();
StringAssert.Contains("<form ", body);
StringAssert.Contains("action=\"http://provider/path\"", body);
StringAssert.Contains("method=\"post\"", body);
StringAssert.Contains("<input type=\"hidden\" name=\"age\" value=\"15\" />", body);
StringAssert.Contains("<input type=\"hidden\" name=\"Location\" value=\"http://host/path\" />", body);
StringAssert.Contains("<input type=\"hidden\" name=\"Name\" value=\"" + HttpUtility.HtmlEncode(message.Name) + "\" />", body);
StringAssert.Contains(".submit()", body, "There should be some javascript to automate form submission.");
}
[Test, ExpectedException(typeof(ArgumentNullException))]
public void SendIndirectMessageFormPostNullMessage() {
TestBadChannel badChannel = new TestBadChannel();
badChannel.CreateFormPostResponse(null, new Dictionary<string, string>());
}
[Test, ExpectedException(typeof(ArgumentException))]
public void SendIndirectMessageFormPostEmptyRecipient() {
TestBadChannel badChannel = new TestBadChannel();
var message = new TestDirectedMessage(MessageTransport.Indirect);
badChannel.CreateFormPostResponse(message, new Dictionary<string, string>());
}
[Test, ExpectedException(typeof(ArgumentNullException))]
public void SendIndirectMessageFormPostNullFields() {
TestBadChannel badChannel = new TestBadChannel();
var message = new TestDirectedMessage(MessageTransport.Indirect);
message.Recipient = new Uri("http://someserver");
badChannel.CreateFormPostResponse(message, null);
}
/// <summary>
/// Tests that a direct message is sent when the appropriate message type is provided.
/// </summary>
/// <remarks>
/// Since this is a mock channel that doesn't actually formulate a direct message response,
/// we just check that the right method was called.
/// </remarks>
[Test, ExpectedException(typeof(NotImplementedException))]
public async Task SendDirectMessageResponse() {
IProtocolMessage message = new TestDirectedMessage {
Age = 15,
Name = "Andrew",
Location = new Uri("http://host/path"),
};
await this.Channel.PrepareResponseAsync(message);
}
[Test, ExpectedException(typeof(ArgumentNullException))]
public void SendIndirectMessageNull() {
TestBadChannel badChannel = new TestBadChannel();
badChannel.PrepareIndirectResponse(null);
}
[Test, ExpectedException(typeof(ArgumentNullException))]
public void ReceiveNull() {
TestBadChannel badChannel = new TestBadChannel();
badChannel.Receive(null, null);
}
[Test]
public void ReceiveUnrecognizedMessage() {
TestBadChannel badChannel = new TestBadChannel();
Assert.IsNull(badChannel.Receive(new Dictionary<string, string>(), null));
}
[Test]
public async Task ReadFromRequestWithContext() {
var fields = GetStandardTestFields(FieldFill.AllRequired);
TestMessage expectedMessage = GetStandardTestMessage(FieldFill.AllRequired);
HttpRequest request = new HttpRequest("somefile", "http://someurl", MessagingUtilities.CreateQueryString(fields));
HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter()));
var requestBase = this.Channel.GetRequestFromContext();
IProtocolMessage message = await this.Channel.ReadFromRequestAsync(requestBase.AsHttpRequestMessage(), CancellationToken.None);
Assert.IsNotNull(message);
Assert.IsInstanceOf<TestMessage>(message);
Assert.AreEqual(expectedMessage.Age, ((TestMessage)message).Age);
}
[Test, ExpectedException(typeof(InvalidOperationException))]
public void GetRequestFromContextNoContext() {
HttpContext.Current = null;
TestBadChannel badChannel = new TestBadChannel();
badChannel.GetRequestFromContext();
}
[Test, ExpectedException(typeof(ArgumentNullException))]
public async Task ReadFromRequestNull() {
TestBadChannel badChannel = new TestBadChannel();
await badChannel.ReadFromRequestAsync(null, CancellationToken.None);
}
[Test]
public async Task SendReplayProtectedMessageSetsNonce() {
TestReplayProtectedMessage message = new TestReplayProtectedMessage(MessageTransport.Indirect);
message.Recipient = new Uri("http://localtest");
this.Channel = CreateChannel(MessageProtections.ReplayProtection);
await this.Channel.PrepareResponseAsync(message);
Assert.IsNotNull(((IReplayProtectedProtocolMessage)message).Nonce);
}
[Test, ExpectedException(typeof(InvalidSignatureException))]
public async Task ReceivedInvalidSignature() {
this.Channel = CreateChannel(MessageProtections.TamperProtection);
await this.ParameterizedReceiveProtectedTestAsync(DateTime.UtcNow, true);
}
[Test]
public async Task ReceivedReplayProtectedMessageJustOnce() {
this.Channel = CreateChannel(MessageProtections.ReplayProtection);
await this.ParameterizedReceiveProtectedTestAsync(DateTime.UtcNow, false);
}
[Test, ExpectedException(typeof(ReplayedMessageException))]
public async Task ReceivedReplayProtectedMessageTwice() {
this.Channel = CreateChannel(MessageProtections.ReplayProtection);
await this.ParameterizedReceiveProtectedTestAsync(DateTime.UtcNow, false);
await this.ParameterizedReceiveProtectedTestAsync(DateTime.UtcNow, false);
}
[Test, ExpectedException(typeof(ProtocolException))]
public void MessageExpirationWithoutTamperResistance() {
new TestChannel(
new TestMessageFactory(),
new IChannelBindingElement[] { new StandardExpirationBindingElement() },
new DefaultOpenIdHostFactories());
}
[Test, ExpectedException(typeof(ProtocolException))]
public async Task TooManyBindingElementsProvidingSameProtection() {
Channel channel = new TestChannel(
new TestMessageFactory(),
new IChannelBindingElement[] { new MockSigningBindingElement(), new MockSigningBindingElement() },
new DefaultOpenIdHostFactories());
await channel.ProcessOutgoingMessageTestHookAsync(new TestSignedDirectedMessage());
}
[Test]
public void BindingElementsOrdering() {
IChannelBindingElement transformA = new MockTransformationBindingElement("a");
IChannelBindingElement transformB = new MockTransformationBindingElement("b");
IChannelBindingElement sign = new MockSigningBindingElement();
IChannelBindingElement replay = new MockReplayProtectionBindingElement();
IChannelBindingElement expire = new StandardExpirationBindingElement();
Channel channel = new TestChannel(
new TestMessageFactory(),
new[] { sign, replay, expire, transformB, transformA },
new DefaultOpenIdHostFactories());
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]);
}
[Test, ExpectedException(typeof(UnprotectedMessageException))]
public async Task InsufficientlyProtectedMessageSent() {
var message = new TestSignedDirectedMessage(MessageTransport.Direct);
message.Recipient = new Uri("http://localtest");
await this.Channel.PrepareResponseAsync(message);
}
[Test, ExpectedException(typeof(UnprotectedMessageException))]
public async Task InsufficientlyProtectedMessageReceived() {
this.Channel = CreateChannel(MessageProtections.None, MessageProtections.TamperProtection);
await this.ParameterizedReceiveProtectedTestAsync(DateTime.Now, false);
}
[Test, ExpectedException(typeof(ProtocolException))]
public async Task IncomingMessageMissingRequiredParameters() {
var fields = GetStandardTestFields(FieldFill.IdentifiableButNotAllRequired);
await this.Channel.ReadFromRequestAsync(CreateHttpRequestInfo(HttpMethod.Get, fields), CancellationToken.None);
}
}
}
|