diff options
Diffstat (limited to 'src/DotNetOpenAuth.Test/OpenId/ChannelElements')
-rw-r--r-- | src/DotNetOpenAuth.Test/OpenId/ChannelElements/ExtensionsBindingElementTests.cs | 94 | ||||
-rw-r--r-- | src/DotNetOpenAuth.Test/OpenId/ChannelElements/KeyValueFormEncodingTests.cs | 59 |
2 files changed, 95 insertions, 58 deletions
diff --git a/src/DotNetOpenAuth.Test/OpenId/ChannelElements/ExtensionsBindingElementTests.cs b/src/DotNetOpenAuth.Test/OpenId/ChannelElements/ExtensionsBindingElementTests.cs index dd47782..e60ac3c 100644 --- a/src/DotNetOpenAuth.Test/OpenId/ChannelElements/ExtensionsBindingElementTests.cs +++ b/src/DotNetOpenAuth.Test/OpenId/ChannelElements/ExtensionsBindingElementTests.cs @@ -8,12 +8,17 @@ namespace DotNetOpenAuth.Test.OpenId.ChannelElements { using System; using System.Collections.Generic; using System.Linq; + using System.Net.Http; using System.Text.RegularExpressions; + using System.Threading; + using System.Threading.Tasks; + using DotNetOpenAuth.Messaging; using DotNetOpenAuth.OpenId; using DotNetOpenAuth.OpenId.ChannelElements; using DotNetOpenAuth.OpenId.Extensions; using DotNetOpenAuth.OpenId.Messages; + using DotNetOpenAuth.OpenId.Provider; using DotNetOpenAuth.OpenId.RelyingParty; using DotNetOpenAuth.Test.Mocks; using DotNetOpenAuth.Test.OpenId.Extensions; @@ -53,23 +58,23 @@ namespace DotNetOpenAuth.Test.OpenId.ChannelElements { } [Test] - public void PrepareMessageForSendingNull() { - Assert.IsNull(this.rpElement.ProcessOutgoingMessage(null)); + public async Task PrepareMessageForSendingNull() { + Assert.IsNull(await this.rpElement.ProcessOutgoingMessageAsync(null, CancellationToken.None)); } /// <summary> /// Verifies that false is returned when a non-extendable message is sent. /// </summary> [Test] - public void PrepareMessageForSendingNonExtendableMessage() { + public async Task PrepareMessageForSendingNonExtendableMessage() { IProtocolMessage request = new AssociateDiffieHellmanRequest(Protocol.Default.Version, OpenIdTestBase.OPUri); - Assert.IsNull(this.rpElement.ProcessOutgoingMessage(request)); + Assert.IsNull(await this.rpElement.ProcessOutgoingMessageAsync(request, CancellationToken.None)); } [Test] - public void PrepareMessageForSending() { + public async Task PrepareMessageForSending() { this.request.Extensions.Add(new MockOpenIdExtension("part", "extra")); - Assert.IsNotNull(this.rpElement.ProcessOutgoingMessage(this.request)); + Assert.IsNotNull(await this.rpElement.ProcessOutgoingMessageAsync(this.request, CancellationToken.None)); string alias = GetAliases(this.request.ExtraData).Single(); Assert.AreEqual(MockOpenIdExtension.MockTypeUri, this.request.ExtraData["openid.ns." + alias]); @@ -78,11 +83,11 @@ namespace DotNetOpenAuth.Test.OpenId.ChannelElements { } [Test] - public void PrepareMessageForReceiving() { + public async Task PrepareMessageForReceiving() { this.request.ExtraData["openid.ns.mock"] = MockOpenIdExtension.MockTypeUri; this.request.ExtraData["openid.mock.Part"] = "part"; this.request.ExtraData["openid.mock.data"] = "extra"; - Assert.IsNotNull(this.rpElement.ProcessIncomingMessage(this.request)); + Assert.IsNotNull(await this.rpElement.ProcessIncomingMessageAsync(this.request, CancellationToken.None)); MockOpenIdExtension ext = this.request.Extensions.OfType<MockOpenIdExtension>().Single(); Assert.AreEqual("part", ext.Part); Assert.AreEqual("extra", ext.Data); @@ -92,12 +97,12 @@ namespace DotNetOpenAuth.Test.OpenId.ChannelElements { /// Verifies that extension responses are included in the OP's signature. /// </summary> [Test] - public void ExtensionResponsesAreSigned() { + public async Task ExtensionResponsesAreSigned() { Protocol protocol = Protocol.Default; var op = this.CreateProvider(); IndirectSignedResponse response = this.CreateResponseWithExtensions(protocol); - op.Channel.PrepareResponse(response); - ITamperResistantOpenIdMessage signedResponse = (ITamperResistantOpenIdMessage)response; + await op.Channel.PrepareResponseAsync(response); + ITamperResistantOpenIdMessage signedResponse = response; string extensionAliasKey = signedResponse.ExtraData.Single(kv => kv.Value == MockOpenIdExtension.MockTypeUri).Key; Assert.IsTrue(extensionAliasKey.StartsWith("openid.ns.")); string extensionAlias = extensionAliasKey.Substring("openid.ns.".Length); @@ -114,27 +119,56 @@ namespace DotNetOpenAuth.Test.OpenId.ChannelElements { /// Verifies that unsigned extension responses (where any or all fields are unsigned) are ignored. /// </summary> [Test] - public void ExtensionsAreIdentifiedAsSignedOrUnsigned() { + public async Task ExtensionsAreIdentifiedAsSignedOrUnsigned() { Protocol protocol = Protocol.Default; - OpenIdCoordinator coordinator = new OpenIdCoordinator( - rp => { - RegisterMockExtension(rp.Channel); - var response = rp.Channel.ReadFromRequest<IndirectSignedResponse>(); - Assert.AreEqual(1, response.SignedExtensions.Count(), "Signed extension should have been received."); - Assert.AreEqual(0, response.UnsignedExtensions.Count(), "No unsigned extension should be present."); - response = rp.Channel.ReadFromRequest<IndirectSignedResponse>(); - Assert.AreEqual(0, response.SignedExtensions.Count(), "No signed extension should have been received."); - Assert.AreEqual(1, response.UnsignedExtensions.Count(), "Unsigned extension should have been received."); - }, - op => { + var opStore = new StandardProviderApplicationStore(); + int rpStep = 0; + var coordinator = new CoordinatorBase( + async (hostFactories, ct) => { + var op = new OpenIdProvider(opStore); RegisterMockExtension(op.Channel); - op.Channel.Respond(CreateResponseWithExtensions(protocol)); - op.Respond(op.GetRequest()); // check_auth + var redirectingResponse = await op.Channel.PrepareResponseAsync(CreateResponseWithExtensions(protocol)); + using (var httpClient = hostFactories.CreateHttpClient()) { + using (var response = await httpClient.GetAsync(redirectingResponse.Headers.Location)) { + response.EnsureSuccessStatusCode(); + } + } + op.SecuritySettings.SignOutgoingExtensions = false; - op.Channel.Respond(CreateResponseWithExtensions(protocol)); - op.Respond(op.GetRequest()); // check_auth - }); - coordinator.Run(); + redirectingResponse = await op.Channel.PrepareResponseAsync(CreateResponseWithExtensions(protocol)); + using (var httpClient = hostFactories.CreateHttpClient()) { + using (var response = await httpClient.GetAsync(redirectingResponse.Headers.Location)) { + response.EnsureSuccessStatusCode(); + } + } + }, + CoordinatorBase.Handle(RPRealmUri).By(async (hostFactories, req, ct) => { + var rp = new OpenIdRelyingParty(new StandardRelyingPartyApplicationStore(), hostFactories); + RegisterMockExtension(rp.Channel); + + switch (++rpStep) { + case 1: + var response = await rp.Channel.ReadFromRequestAsync<IndirectSignedResponse>(req, ct); + Assert.AreEqual(1, response.SignedExtensions.Count(), "Signed extension should have been received."); + Assert.AreEqual(0, response.UnsignedExtensions.Count(), "No unsigned extension should be present."); + break; + case 2: + response = await rp.Channel.ReadFromRequestAsync<IndirectSignedResponse>(req, ct); + Assert.AreEqual(0, response.SignedExtensions.Count(), "No signed extension should have been received."); + Assert.AreEqual(1, response.UnsignedExtensions.Count(), "Unsigned extension should have been received."); + break; + + default: + throw Assumes.NotReachable(); + } + + return new HttpResponseMessage(); + }), + CoordinatorBase.Handle(OPUri).By(async (hostFactories, req, ct) => { + var op = new OpenIdProvider(opStore); + return await AutoProviderActionAsync(op, req, ct); + })); + await coordinator.RunAsync(); } /// <summary> @@ -181,7 +215,7 @@ namespace DotNetOpenAuth.Test.OpenId.ChannelElements { private IndirectSignedResponse CreateResponseWithExtensions(Protocol protocol) { Requires.NotNull(protocol, "protocol"); - IndirectSignedResponse response = new IndirectSignedResponse(protocol.Version, RPUri); + var response = new IndirectSignedResponse(protocol.Version, RPUri); response.ProviderEndpoint = OPUri; response.Extensions.Add(new MockOpenIdExtension("pv", "ev")); return response; diff --git a/src/DotNetOpenAuth.Test/OpenId/ChannelElements/KeyValueFormEncodingTests.cs b/src/DotNetOpenAuth.Test/OpenId/ChannelElements/KeyValueFormEncodingTests.cs index 93ad028..b64701d 100644 --- a/src/DotNetOpenAuth.Test/OpenId/ChannelElements/KeyValueFormEncodingTests.cs +++ b/src/DotNetOpenAuth.Test/OpenId/ChannelElements/KeyValueFormEncodingTests.cs @@ -11,6 +11,9 @@ namespace DotNetOpenAuth.Test.OpenId.ChannelElements { using System.Linq; using System.Net; using System.Text; + using System.Threading; + using System.Threading.Tasks; + using DotNetOpenAuth.Messaging; using DotNetOpenAuth.Messaging.Reflection; using DotNetOpenAuth.OpenId.ChannelElements; @@ -56,9 +59,9 @@ namespace DotNetOpenAuth.Test.OpenId.ChannelElements { Assert.AreEqual(this.sampleData.Count, count); } - public void KVDictTest(byte[] kvform, IDictionary<string, string> dict, TestMode mode) { + public async Task KVDictTestAsync(byte[] kvform, IDictionary<string, string> dict, TestMode mode) { if ((mode & TestMode.Decoder) == TestMode.Decoder) { - var d = this.keyValueForm.GetDictionary(new MemoryStream(kvform)); + var d = await this.keyValueForm.GetDictionaryAsync(new MemoryStream(kvform), CancellationToken.None); foreach (string key in dict.Keys) { Assert.AreEqual(d[key], dict[key], "Decoder fault: " + d[key] + " and " + dict[key] + " do not match."); } @@ -70,91 +73,91 @@ namespace DotNetOpenAuth.Test.OpenId.ChannelElements { } [Test] - public void EncodeDecode() { - this.KVDictTest(UTF8Encoding.UTF8.GetBytes(string.Empty), new Dictionary<string, string>(), TestMode.Both); + public async Task EncodeDecode() { + await this.KVDictTestAsync(UTF8Encoding.UTF8.GetBytes(string.Empty), new Dictionary<string, string>(), TestMode.Both); Dictionary<string, string> d1 = new Dictionary<string, string>(); d1.Add("college", "harvey mudd"); - this.KVDictTest(UTF8Encoding.UTF8.GetBytes("college:harvey mudd\n"), d1, TestMode.Both); + await this.KVDictTestAsync(UTF8Encoding.UTF8.GetBytes("college:harvey mudd\n"), d1, TestMode.Both); Dictionary<string, string> d2 = new Dictionary<string, string>(); d2.Add("city", "claremont"); d2.Add("state", "CA"); - this.KVDictTest(UTF8Encoding.UTF8.GetBytes("city:claremont\nstate:CA\n"), d2, TestMode.Both); + await this.KVDictTestAsync(UTF8Encoding.UTF8.GetBytes("city:claremont\nstate:CA\n"), d2, TestMode.Both); Dictionary<string, string> d3 = new Dictionary<string, string>(); d3.Add("is_valid", "true"); d3.Add("invalidate_handle", "{HMAC-SHA1:2398410938412093}"); - this.KVDictTest(UTF8Encoding.UTF8.GetBytes("is_valid:true\ninvalidate_handle:{HMAC-SHA1:2398410938412093}\n"), d3, TestMode.Both); + await this.KVDictTestAsync(UTF8Encoding.UTF8.GetBytes("is_valid:true\ninvalidate_handle:{HMAC-SHA1:2398410938412093}\n"), d3, TestMode.Both); Dictionary<string, string> d4 = new Dictionary<string, string>(); d4.Add(string.Empty, string.Empty); - this.KVDictTest(UTF8Encoding.UTF8.GetBytes(":\n"), d4, TestMode.Both); + await this.KVDictTestAsync(UTF8Encoding.UTF8.GetBytes(":\n"), d4, TestMode.Both); Dictionary<string, string> d5 = new Dictionary<string, string>(); d5.Add(string.Empty, "missingkey"); - this.KVDictTest(UTF8Encoding.UTF8.GetBytes(":missingkey\n"), d5, TestMode.Both); + await this.KVDictTestAsync(UTF8Encoding.UTF8.GetBytes(":missingkey\n"), d5, TestMode.Both); Dictionary<string, string> d6 = new Dictionary<string, string>(); d6.Add("street", "foothill blvd"); - this.KVDictTest(UTF8Encoding.UTF8.GetBytes("street:foothill blvd\n"), d6, TestMode.Both); + await this.KVDictTestAsync(UTF8Encoding.UTF8.GetBytes("street:foothill blvd\n"), d6, TestMode.Both); Dictionary<string, string> d7 = new Dictionary<string, string>(); d7.Add("major", "computer science"); - this.KVDictTest(UTF8Encoding.UTF8.GetBytes("major:computer science\n"), d7, TestMode.Both); + await this.KVDictTestAsync(UTF8Encoding.UTF8.GetBytes("major:computer science\n"), d7, TestMode.Both); Dictionary<string, string> d8 = new Dictionary<string, string>(); d8.Add("dorm", "east"); - this.KVDictTest(UTF8Encoding.UTF8.GetBytes(" dorm : east \n"), d8, TestMode.Decoder); + await this.KVDictTestAsync(UTF8Encoding.UTF8.GetBytes(" dorm : east \n"), d8, TestMode.Decoder); Dictionary<string, string> d9 = new Dictionary<string, string>(); d9.Add("e^(i*pi)+1", "0"); - this.KVDictTest(UTF8Encoding.UTF8.GetBytes("e^(i*pi)+1:0"), d9, TestMode.Decoder); + await this.KVDictTestAsync(UTF8Encoding.UTF8.GetBytes("e^(i*pi)+1:0"), d9, TestMode.Decoder); Dictionary<string, string> d10 = new Dictionary<string, string>(); d10.Add("east", "west"); d10.Add("north", "south"); - this.KVDictTest(UTF8Encoding.UTF8.GetBytes("east:west\nnorth:south"), d10, TestMode.Decoder); + await this.KVDictTestAsync(UTF8Encoding.UTF8.GetBytes("east:west\nnorth:south"), d10, TestMode.Decoder); } [Test, ExpectedException(typeof(FormatException))] - public void NoValue() { - this.Illegal("x\n", KeyValueFormConformanceLevel.OpenId11); + public async Task NoValue() { + await this.IllegalAsync("x\n", KeyValueFormConformanceLevel.OpenId11); } [Test, ExpectedException(typeof(FormatException))] - public void NoValueLoose() { + public async Task NoValueLoose() { Dictionary<string, string> d = new Dictionary<string, string>(); - this.KVDictTest(Encoding.UTF8.GetBytes("x\n"), d, TestMode.Decoder); + await this.KVDictTestAsync(Encoding.UTF8.GetBytes("x\n"), d, TestMode.Decoder); } [Test, ExpectedException(typeof(FormatException))] - public void EmptyLine() { - this.Illegal("x:b\n\n", KeyValueFormConformanceLevel.OpenId20); + public async Task EmptyLine() { + await this.IllegalAsync("x:b\n\n", KeyValueFormConformanceLevel.OpenId20); } [Test] - public void EmptyLineLoose() { + public async Task EmptyLineLoose() { Dictionary<string, string> d = new Dictionary<string, string>(); d.Add("x", "b"); - this.KVDictTest(Encoding.UTF8.GetBytes("x:b\n\n"), d, TestMode.Decoder); + await this.KVDictTestAsync(Encoding.UTF8.GetBytes("x:b\n\n"), d, TestMode.Decoder); } [Test, ExpectedException(typeof(FormatException))] - public void LastLineNotTerminated() { - this.Illegal("x:y\na:b", KeyValueFormConformanceLevel.OpenId11); + public async Task LastLineNotTerminated() { + await this.IllegalAsync("x:y\na:b", KeyValueFormConformanceLevel.OpenId11); } [Test] - public void LastLineNotTerminatedLoose() { + public async Task LastLineNotTerminatedLoose() { Dictionary<string, string> d = new Dictionary<string, string>(); d.Add("x", "y"); d.Add("a", "b"); - this.KVDictTest(Encoding.UTF8.GetBytes("x:y\na:b"), d, TestMode.Decoder); + await this.KVDictTestAsync(Encoding.UTF8.GetBytes("x:y\na:b"), d, TestMode.Decoder); } - private void Illegal(string s, KeyValueFormConformanceLevel level) { - new KeyValueFormEncoding(level).GetDictionary(new MemoryStream(Encoding.UTF8.GetBytes(s))); + private async Task IllegalAsync(string s, KeyValueFormConformanceLevel level) { + await new KeyValueFormEncoding(level).GetDictionaryAsync(new MemoryStream(Encoding.UTF8.GetBytes(s)), CancellationToken.None); } } } |