diff options
Diffstat (limited to 'src/DotNetOpenAuth.Test/OpenId')
7 files changed, 643 insertions, 643 deletions
diff --git a/src/DotNetOpenAuth.Test/OpenId/ChannelElements/OpenIdChannelTests.cs b/src/DotNetOpenAuth.Test/OpenId/ChannelElements/OpenIdChannelTests.cs index a548969..60cbe3e 100644 --- a/src/DotNetOpenAuth.Test/OpenId/ChannelElements/OpenIdChannelTests.cs +++ b/src/DotNetOpenAuth.Test/OpenId/ChannelElements/OpenIdChannelTests.cs @@ -1,102 +1,102 @@ -//-----------------------------------------------------------------------
-// <copyright file="OpenIdChannelTests.cs" company="Andrew Arnott">
-// Copyright (c) Andrew Arnott. All rights reserved.
-// </copyright>
-//-----------------------------------------------------------------------
-
-namespace DotNetOpenAuth.Test.OpenId.ChannelElements {
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using DotNetOpenAuth.Messaging;
- using DotNetOpenAuth.Messaging.Bindings;
- using DotNetOpenAuth.Messaging.Reflection;
- using DotNetOpenAuth.OpenId;
- using DotNetOpenAuth.OpenId.ChannelElements;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
-
- [TestClass]
- public class OpenIdChannelTests : TestBase {
- private static readonly TimeSpan maximumMessageAge = TimeSpan.FromHours(3); // good for tests, too long for production
- private OpenIdChannel channel;
- private OpenIdChannel_Accessor accessor;
- private Mocks.TestWebRequestHandler webHandler;
-
- [TestInitialize]
- public void Setup() {
- this.webHandler = new Mocks.TestWebRequestHandler();
- this.channel = new OpenIdChannel(new AssociationMemoryStore<Uri>(), new NonceMemoryStore(maximumMessageAge), new PrivateSecretMemoryStore());
- this.accessor = OpenIdChannel_Accessor.AttachShadow(this.channel);
- this.channel.WebRequestHandler = this.webHandler;
- }
-
- [TestMethod]
- public void Ctor() {
- // Verify that the channel stack includes the expected types.
- // While other binding elements may be substituted for these, we'd then have
- // to test them. Since we're not testing them in the OpenID battery of tests,
- // we make sure they are the standard ones so that we trust they are tested
- // elsewhere by the testing library.
- var replayElement = (StandardReplayProtectionBindingElement)this.channel.BindingElements.SingleOrDefault(el => el is StandardReplayProtectionBindingElement);
- Assert.IsTrue(this.channel.BindingElements.Any(el => el is StandardExpirationBindingElement));
- Assert.IsNotNull(replayElement);
-
- // Verify that empty nonces are allowed, since OpenID 2.0 allows this.
- Assert.IsTrue(replayElement.AllowZeroLengthNonce);
- }
-
- /// <summary>
- /// Verifies that the channel sends direct message requests as HTTP POST requests.
- /// </summary>
- [TestMethod]
- public void DirectRequestsUsePost() {
- IDirectedProtocolMessage requestMessage = new Mocks.TestDirectedMessage(MessageTransport.Direct) {
- Recipient = new Uri("http://host"),
- Name = "Andrew",
- };
- HttpWebRequest httpRequest = this.accessor.CreateHttpRequest(requestMessage);
- Assert.AreEqual("POST", httpRequest.Method);
- StringAssert.Contains(this.webHandler.RequestEntityAsString, "Name=Andrew");
- }
-
- /// <summary>
- /// Verifies that direct response messages are encoded using Key Value Form.
- /// </summary>
- /// <remarks>
- /// The validity of the actual KVF encoding is not checked here. We assume that the KVF encoding
- /// class is verified elsewhere. We're only checking that the KVF class is being used by the
- /// <see cref="OpenIdChannel.SendDirectMessageResponse"/> method.
- /// </remarks>
- [TestMethod]
- public void DirectResponsesSentUsingKeyValueForm() {
- IProtocolMessage message = MessagingTestBase.GetStandardTestMessage(MessagingTestBase.FieldFill.AllRequired);
- MessageDictionary messageFields = new MessageDictionary(message);
- byte[] expectedBytes = KeyValueFormEncoding.GetBytes(messageFields);
- string expectedContentType = OpenIdChannel_Accessor.KeyValueFormContentType;
-
- UserAgentResponse directResponse = this.accessor.SendDirectMessageResponse(message);
- Assert.AreEqual(expectedContentType, directResponse.Headers[HttpResponseHeader.ContentType]);
- byte[] actualBytes = new byte[directResponse.ResponseStream.Length];
- directResponse.ResponseStream.Read(actualBytes, 0, actualBytes.Length);
- Assert.IsTrue(MessagingUtilities.AreEquivalent(expectedBytes, actualBytes));
- }
-
- /// <summary>
- /// Verifies that direct message responses are read in using the Key Value Form decoder.
- /// </summary>
- [TestMethod]
- public void DirectResponsesReceivedAsKeyValueForm() {
- var fields = new Dictionary<string, string> {
- { "var1", "value1" },
- { "var2", "value2" },
- };
- var response = new DirectWebResponse {
- ResponseStream = new MemoryStream(KeyValueFormEncoding.GetBytes(fields)),
- };
- Assert.IsTrue(MessagingUtilities.AreEquivalent(fields, this.accessor.ReadFromResponseInternal(response)));
- }
- }
-}
+//----------------------------------------------------------------------- +// <copyright file="OpenIdChannelTests.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test.OpenId.ChannelElements { + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Net; + using System.Text; + using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.Messaging.Bindings; + using DotNetOpenAuth.Messaging.Reflection; + using DotNetOpenAuth.OpenId; + using DotNetOpenAuth.OpenId.ChannelElements; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class OpenIdChannelTests : TestBase { + private static readonly TimeSpan maximumMessageAge = TimeSpan.FromHours(3); // good for tests, too long for production + private OpenIdChannel channel; + private OpenIdChannel_Accessor accessor; + private Mocks.TestWebRequestHandler webHandler; + + [TestInitialize] + public void Setup() { + this.webHandler = new Mocks.TestWebRequestHandler(); + this.channel = new OpenIdChannel(new AssociationMemoryStore<Uri>(), new NonceMemoryStore(maximumMessageAge), new PrivateSecretMemoryStore()); + this.accessor = OpenIdChannel_Accessor.AttachShadow(this.channel); + this.channel.WebRequestHandler = this.webHandler; + } + + [TestMethod] + public void Ctor() { + // Verify that the channel stack includes the expected types. + // While other binding elements may be substituted for these, we'd then have + // to test them. Since we're not testing them in the OpenID battery of tests, + // we make sure they are the standard ones so that we trust they are tested + // elsewhere by the testing library. + var replayElement = (StandardReplayProtectionBindingElement)this.channel.BindingElements.SingleOrDefault(el => el is StandardReplayProtectionBindingElement); + Assert.IsTrue(this.channel.BindingElements.Any(el => el is StandardExpirationBindingElement)); + Assert.IsNotNull(replayElement); + + // Verify that empty nonces are allowed, since OpenID 2.0 allows this. + Assert.IsTrue(replayElement.AllowZeroLengthNonce); + } + + /// <summary> + /// Verifies that the channel sends direct message requests as HTTP POST requests. + /// </summary> + [TestMethod] + public void DirectRequestsUsePost() { + IDirectedProtocolMessage requestMessage = new Mocks.TestDirectedMessage(MessageTransport.Direct) { + Recipient = new Uri("http://host"), + Name = "Andrew", + }; + HttpWebRequest httpRequest = this.accessor.CreateHttpRequest(requestMessage); + Assert.AreEqual("POST", httpRequest.Method); + StringAssert.Contains(this.webHandler.RequestEntityAsString, "Name=Andrew"); + } + + /// <summary> + /// Verifies that direct response messages are encoded using Key Value Form. + /// </summary> + /// <remarks> + /// The validity of the actual KVF encoding is not checked here. We assume that the KVF encoding + /// class is verified elsewhere. We're only checking that the KVF class is being used by the + /// <see cref="OpenIdChannel.SendDirectMessageResponse"/> method. + /// </remarks> + [TestMethod] + public void DirectResponsesSentUsingKeyValueForm() { + IProtocolMessage message = MessagingTestBase.GetStandardTestMessage(MessagingTestBase.FieldFill.AllRequired); + MessageDictionary messageFields = new MessageDictionary(message); + byte[] expectedBytes = KeyValueFormEncoding.GetBytes(messageFields); + string expectedContentType = OpenIdChannel_Accessor.KeyValueFormContentType; + + UserAgentResponse directResponse = this.accessor.SendDirectMessageResponse(message); + Assert.AreEqual(expectedContentType, directResponse.Headers[HttpResponseHeader.ContentType]); + byte[] actualBytes = new byte[directResponse.ResponseStream.Length]; + directResponse.ResponseStream.Read(actualBytes, 0, actualBytes.Length); + Assert.IsTrue(MessagingUtilities.AreEquivalent(expectedBytes, actualBytes)); + } + + /// <summary> + /// Verifies that direct message responses are read in using the Key Value Form decoder. + /// </summary> + [TestMethod] + public void DirectResponsesReceivedAsKeyValueForm() { + var fields = new Dictionary<string, string> { + { "var1", "value1" }, + { "var2", "value2" }, + }; + var response = new DirectWebResponse { + ResponseStream = new MemoryStream(KeyValueFormEncoding.GetBytes(fields)), + }; + Assert.IsTrue(MessagingUtilities.AreEquivalent(fields, this.accessor.ReadFromResponseInternal(response))); + } + } +} diff --git a/src/DotNetOpenAuth.Test/OpenId/DiffieHellmanTests.cs b/src/DotNetOpenAuth.Test/OpenId/DiffieHellmanTests.cs index b60631b..7ffaf1b 100644 --- a/src/DotNetOpenAuth.Test/OpenId/DiffieHellmanTests.cs +++ b/src/DotNetOpenAuth.Test/OpenId/DiffieHellmanTests.cs @@ -1,64 +1,64 @@ -//-----------------------------------------------------------------------
-// <copyright file="DiffieHellmanTests.cs" company="Jason Alexander, Andrew Arnott">
-// Copyright (c) Jason Alexander, Andrew Arnott. All rights reserved.
-// </copyright>
-//-----------------------------------------------------------------------
-
-namespace DotNetOpenAuth.Test.OpenId {
- using System;
- using System.IO;
- using DotNetOpenAuth.OpenId;
- using DotNetOpenAuth.OpenId.Messages;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using Org.Mentalis.Security.Cryptography;
-
- [TestClass]
- public class DiffieHellmanTests {
- [TestMethod]
- public void Test() {
- string s1 = Test1();
- string s2 = Test1();
-
- Assert.AreNotEqual(s1, s2, "Secret keys should NOT be the same.");
- }
-
- [TestMethod]
- public void TestPublic() {
- TextReader reader = new StringReader(TestSupport.LoadEmbeddedFile("dhpriv.txt"));
-
- try {
- string line;
- while ((line = reader.ReadLine()) != null) {
- string[] parts = line.Trim().Split(' ');
- byte[] x = Convert.FromBase64String(parts[0]);
- DiffieHellmanManaged dh = new DiffieHellmanManaged(AssociateDiffieHellmanRequest.DefaultMod, AssociateDiffieHellmanRequest.DefaultGen, x);
- byte[] pub = dh.CreateKeyExchange();
- byte[] y = Convert.FromBase64String(parts[1]);
-
- if (y[0] == 0 && y[1] <= 127) {
- y.CopyTo(y, 1);
- }
-
- Assert.AreEqual(
- Convert.ToBase64String(y),
- Convert.ToBase64String(DiffieHellmanUtilities.EnsurePositive(pub)),
- line);
- }
- } finally {
- reader.Close();
- }
- }
-
- private static string Test1() {
- DiffieHellman dh1 = new DiffieHellmanManaged();
- DiffieHellman dh2 = new DiffieHellmanManaged();
-
- string secret1 = Convert.ToBase64String(dh1.DecryptKeyExchange(dh2.CreateKeyExchange()));
- string secret2 = Convert.ToBase64String(dh2.DecryptKeyExchange(dh1.CreateKeyExchange()));
-
- Assert.AreEqual(secret1, secret2, "Secret keys do not match for some reason.");
-
- return secret1;
- }
- }
-}
+//----------------------------------------------------------------------- +// <copyright file="DiffieHellmanTests.cs" company="Jason Alexander, Andrew Arnott"> +// Copyright (c) Jason Alexander, Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test.OpenId { + using System; + using System.IO; + using DotNetOpenAuth.OpenId; + using DotNetOpenAuth.OpenId.Messages; + using Microsoft.VisualStudio.TestTools.UnitTesting; + using Org.Mentalis.Security.Cryptography; + + [TestClass] + public class DiffieHellmanTests { + [TestMethod] + public void Test() { + string s1 = Test1(); + string s2 = Test1(); + + Assert.AreNotEqual(s1, s2, "Secret keys should NOT be the same."); + } + + [TestMethod] + public void TestPublic() { + TextReader reader = new StringReader(TestSupport.LoadEmbeddedFile("dhpriv.txt")); + + try { + string line; + while ((line = reader.ReadLine()) != null) { + string[] parts = line.Trim().Split(' '); + byte[] x = Convert.FromBase64String(parts[0]); + DiffieHellmanManaged dh = new DiffieHellmanManaged(AssociateDiffieHellmanRequest.DefaultMod, AssociateDiffieHellmanRequest.DefaultGen, x); + byte[] pub = dh.CreateKeyExchange(); + byte[] y = Convert.FromBase64String(parts[1]); + + if (y[0] == 0 && y[1] <= 127) { + y.CopyTo(y, 1); + } + + Assert.AreEqual( + Convert.ToBase64String(y), + Convert.ToBase64String(DiffieHellmanUtilities.EnsurePositive(pub)), + line); + } + } finally { + reader.Close(); + } + } + + private static string Test1() { + DiffieHellman dh1 = new DiffieHellmanManaged(); + DiffieHellman dh2 = new DiffieHellmanManaged(); + + string secret1 = Convert.ToBase64String(dh1.DecryptKeyExchange(dh2.CreateKeyExchange())); + string secret2 = Convert.ToBase64String(dh2.DecryptKeyExchange(dh1.CreateKeyExchange())); + + Assert.AreEqual(secret1, secret2, "Secret keys do not match for some reason."); + + return secret1; + } + } +} diff --git a/src/DotNetOpenAuth.Test/OpenId/Extensions/SimpleRegistration/ClaimsRequestTests.cs b/src/DotNetOpenAuth.Test/OpenId/Extensions/SimpleRegistration/ClaimsRequestTests.cs index 8da2c7a..feb1450 100644 --- a/src/DotNetOpenAuth.Test/OpenId/Extensions/SimpleRegistration/ClaimsRequestTests.cs +++ b/src/DotNetOpenAuth.Test/OpenId/Extensions/SimpleRegistration/ClaimsRequestTests.cs @@ -1,62 +1,62 @@ -//-----------------------------------------------------------------------
-// <copyright file="ClaimsRequestTests.cs" company="Andrew Arnott">
-// Copyright (c) Andrew Arnott. All rights reserved.
-// </copyright>
-//-----------------------------------------------------------------------
-
-namespace DotNetOpenAuth.Test.OpenId.Extensions {
- using DotNetOpenAuth.Messaging.Reflection;
- using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration;
- using DotNetOpenAuth.OpenId.Messages;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
-
- [TestClass]
- public class ClaimsRequestTests : OpenIdTestBase {
- [TestMethod]
- public void CreateResponse() {
- // some unofficial type URIs...
- this.ParameterizedTypeUriPreservedTest("http://openid.net/sreg/1.0");
- this.ParameterizedTypeUriPreservedTest("http://openid.net/sreg/1.1");
- // and the official one.
- this.ParameterizedTypeUriPreservedTest("http://openid.net/extensions/sreg/1.1");
- }
-
- [TestMethod]
- public void RequiredOptionalLists() {
- ClaimsRequest req = new ClaimsRequest();
- MessageDictionary dictionary = new MessageDictionary(req);
- Assert.AreEqual(string.Empty, dictionary["required"]);
- Assert.AreEqual(string.Empty, dictionary["optional"]);
-
- req.BirthDate = DemandLevel.Request;
- req.Nickname = DemandLevel.Require;
- Assert.AreEqual("dob", dictionary["optional"]);
- Assert.AreEqual("nickname", dictionary["required"]);
-
- req.PostalCode = DemandLevel.Require;
- req.Gender = DemandLevel.Request;
- Assert.AreEqual("dob,gender", dictionary["optional"]);
- Assert.AreEqual("nickname,postcode", dictionary["required"]);
- }
-
- [TestMethod]
- public void EqualityTests() {
- ClaimsRequest req1 = new ClaimsRequest();
- ClaimsRequest req2 = new ClaimsRequest();
- Assert.AreEqual(req1, req2);
-
- req1.BirthDate = DemandLevel.Request;
- Assert.AreNotEqual(req1, req2);
-
- req2.BirthDate = DemandLevel.Request;
- req1.Country = DemandLevel.Request;
- Assert.AreNotEqual(req1, req2);
- }
-
- private void ParameterizedTypeUriPreservedTest(string typeUri) {
- ClaimsRequest request = new ClaimsRequest(typeUri);
- ClaimsResponse response = request.CreateResponse();
- Assert.AreEqual(typeUri, ((IOpenIdMessageExtension)response).TypeUri);
- }
- }
-}
+//----------------------------------------------------------------------- +// <copyright file="ClaimsRequestTests.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test.OpenId.Extensions { + using DotNetOpenAuth.Messaging.Reflection; + using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration; + using DotNetOpenAuth.OpenId.Messages; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class ClaimsRequestTests : OpenIdTestBase { + [TestMethod] + public void CreateResponse() { + // some unofficial type URIs... + this.ParameterizedTypeUriPreservedTest("http://openid.net/sreg/1.0"); + this.ParameterizedTypeUriPreservedTest("http://openid.net/sreg/1.1"); + // and the official one. + this.ParameterizedTypeUriPreservedTest("http://openid.net/extensions/sreg/1.1"); + } + + [TestMethod] + public void RequiredOptionalLists() { + ClaimsRequest req = new ClaimsRequest(); + MessageDictionary dictionary = new MessageDictionary(req); + Assert.AreEqual(string.Empty, dictionary["required"]); + Assert.AreEqual(string.Empty, dictionary["optional"]); + + req.BirthDate = DemandLevel.Request; + req.Nickname = DemandLevel.Require; + Assert.AreEqual("dob", dictionary["optional"]); + Assert.AreEqual("nickname", dictionary["required"]); + + req.PostalCode = DemandLevel.Require; + req.Gender = DemandLevel.Request; + Assert.AreEqual("dob,gender", dictionary["optional"]); + Assert.AreEqual("nickname,postcode", dictionary["required"]); + } + + [TestMethod] + public void EqualityTests() { + ClaimsRequest req1 = new ClaimsRequest(); + ClaimsRequest req2 = new ClaimsRequest(); + Assert.AreEqual(req1, req2); + + req1.BirthDate = DemandLevel.Request; + Assert.AreNotEqual(req1, req2); + + req2.BirthDate = DemandLevel.Request; + req1.Country = DemandLevel.Request; + Assert.AreNotEqual(req1, req2); + } + + private void ParameterizedTypeUriPreservedTest(string typeUri) { + ClaimsRequest request = new ClaimsRequest(typeUri); + ClaimsResponse response = request.CreateResponse(); + Assert.AreEqual(typeUri, ((IOpenIdMessageExtension)response).TypeUri); + } + } +} diff --git a/src/DotNetOpenAuth.Test/OpenId/Extensions/SimpleRegistration/ClaimsResponseTests.cs b/src/DotNetOpenAuth.Test/OpenId/Extensions/SimpleRegistration/ClaimsResponseTests.cs index 94cf55b..6dbfa4f 100644 --- a/src/DotNetOpenAuth.Test/OpenId/Extensions/SimpleRegistration/ClaimsResponseTests.cs +++ b/src/DotNetOpenAuth.Test/OpenId/Extensions/SimpleRegistration/ClaimsResponseTests.cs @@ -1,146 +1,146 @@ -//-----------------------------------------------------------------------
-// <copyright file="ClaimsResponseTests.cs" company="Andrew Arnott">
-// Copyright (c) Andrew Arnott. All rights reserved.
-// </copyright>
-//-----------------------------------------------------------------------
-
-namespace DotNetOpenAuth.Test.OpenId.Extensions {
- using System;
- using System.Globalization;
- using System.IO;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Xml.Serialization;
- using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
-
- [TestClass]
- public class ClaimsResponseTests {
- [TestMethod]
- public void EmptyMailAddress() {
- ClaimsResponse response = new ClaimsResponse(Constants.sreg_ns);
- response.Email = string.Empty;
- Assert.IsNull(response.MailAddress);
- }
-
- [TestMethod, Ignore] // serialization no longer supported
- public void BinarySerialization() {
- ClaimsResponse fields = this.GetFilledData();
- MemoryStream ms = new MemoryStream();
- IFormatter formatter = new BinaryFormatter();
- formatter.Serialize(ms, fields);
-
- ms.Position = 0;
- ClaimsResponse fields2 = (ClaimsResponse)formatter.Deserialize(ms);
- Assert.AreEqual(fields, fields2);
- }
-
- [TestMethod, Ignore] // serialization no longer supported
- public void XmlSerialization() {
- ClaimsResponse fields = this.GetFilledData();
- MemoryStream ms = new MemoryStream();
- XmlSerializer xs = new XmlSerializer(typeof(ClaimsResponse));
- xs.Serialize(ms, fields);
-
- ms.Position = 0;
- ClaimsResponse fields2 = (ClaimsResponse)xs.Deserialize(ms);
- Assert.AreEqual(fields, fields2);
- }
-
- [TestMethod]
- public void EqualityTest() {
- ClaimsResponse fields1 = this.GetFilledData();
-
- Assert.AreNotEqual(fields1, null);
- Assert.AreNotEqual(fields1, "string");
-
- ClaimsResponse fields2 = this.GetFilledData();
- Assert.AreNotSame(fields1, fields2, "Test sanity check.");
- Assert.AreEqual(fields1, fields2);
-
- // go through each property and change it slightly and make sure it causes inequality.
- fields2.Email += "q";
- Assert.AreNotEqual(fields1, fields2);
- fields1.Email = fields2.Email;
- Assert.AreEqual(fields1, fields2, "Test sanity check.");
- fields2.BirthDate = DateTime.Now;
- Assert.AreNotEqual(fields1, fields2);
- fields2.BirthDate = fields1.BirthDate;
- Assert.AreEqual(fields1, fields2, "Test sanity check.");
- fields2.Country += "q";
- Assert.AreNotEqual(fields1, fields2);
- fields2.Country = fields1.Country;
- Assert.AreEqual(fields1, fields2, "Test sanity check.");
- fields2.FullName += "q";
- Assert.AreNotEqual(fields1, fields2);
- fields2.FullName = fields1.FullName;
- Assert.AreEqual(fields1, fields2, "Test sanity check.");
- fields2.Gender = Gender.Female;
- Assert.AreNotEqual(fields1, fields2);
- fields2.Gender = fields1.Gender;
- Assert.AreEqual(fields1, fields2, "Test sanity check.");
- fields2.Language = "gb";
- Assert.AreNotEqual(fields1, fields2);
- fields2.Language = fields1.Language;
- Assert.AreEqual(fields1, fields2, "Test sanity check.");
- fields2.Nickname += "q";
- Assert.AreNotEqual(fields1, fields2);
- fields2.Nickname = fields1.Nickname;
- Assert.AreEqual(fields1, fields2, "Test sanity check.");
- fields2.PostalCode += "q";
- Assert.AreNotEqual(fields1, fields2);
- fields2.PostalCode = fields1.PostalCode;
- Assert.AreEqual(fields1, fields2, "Test sanity check.");
- fields2.TimeZone += "q";
- Assert.AreNotEqual(fields1, fields2);
- }
-
- [TestMethod]
- public void Birthdates() {
- var response = new ClaimsResponse();
- // Verify that they both start out as null
- Assert.IsNull(response.BirthDateRaw);
- Assert.IsFalse(response.BirthDate.HasValue);
-
- // Verify that null can be set.
- response.BirthDate = null;
- response.BirthDateRaw = null;
- Assert.IsNull(response.BirthDateRaw);
- Assert.IsFalse(response.BirthDate.HasValue);
-
- // Verify that the strong-typed BirthDate property can be set and that it affects the raw property.
- response.BirthDate = DateTime.Parse("April 4, 1984");
- Assert.AreEqual(4, response.BirthDate.Value.Month);
- Assert.AreEqual("1984-04-04", response.BirthDateRaw);
-
- // Verify that the raw property can be set with a complete birthdate and that it affects the strong-typed property.
- response.BirthDateRaw = "1998-05-08";
- Assert.AreEqual("1998-05-08", response.BirthDateRaw);
- Assert.AreEqual(DateTime.Parse("May 8, 1998", CultureInfo.InvariantCulture), response.BirthDate);
-
- // Verify that an partial raw birthdate works, and sets the strong-typed property to null since it cannot be represented.
- response.BirthDateRaw = "2000-00-00";
- Assert.AreEqual("2000-00-00", response.BirthDateRaw);
- Assert.IsFalse(response.BirthDate.HasValue);
- }
-
- [TestMethod, ExpectedException(typeof(ArgumentException))]
- public void InvalidRawBirthdate() {
- var response = new ClaimsResponse();
- response.BirthDateRaw = "2008";
- }
-
- private ClaimsResponse GetFilledData() {
- return new ClaimsResponse(Constants.sreg_ns) {
- BirthDate = new DateTime(2005, 2, 3),
- Culture = new System.Globalization.CultureInfo("en-US"),
- Email = "a@b.com",
- FullName = "Jimmy buffet",
- Gender = Gender.Male,
- Nickname = "Jimbo",
- PostalCode = "12345",
- TimeZone = "PST",
- };
- }
- }
-}
+//----------------------------------------------------------------------- +// <copyright file="ClaimsResponseTests.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test.OpenId.Extensions { + using System; + using System.Globalization; + using System.IO; + using System.Runtime.Serialization; + using System.Runtime.Serialization.Formatters.Binary; + using System.Xml.Serialization; + using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class ClaimsResponseTests { + [TestMethod] + public void EmptyMailAddress() { + ClaimsResponse response = new ClaimsResponse(Constants.sreg_ns); + response.Email = string.Empty; + Assert.IsNull(response.MailAddress); + } + + [TestMethod, Ignore] // serialization no longer supported + public void BinarySerialization() { + ClaimsResponse fields = this.GetFilledData(); + MemoryStream ms = new MemoryStream(); + IFormatter formatter = new BinaryFormatter(); + formatter.Serialize(ms, fields); + + ms.Position = 0; + ClaimsResponse fields2 = (ClaimsResponse)formatter.Deserialize(ms); + Assert.AreEqual(fields, fields2); + } + + [TestMethod, Ignore] // serialization no longer supported + public void XmlSerialization() { + ClaimsResponse fields = this.GetFilledData(); + MemoryStream ms = new MemoryStream(); + XmlSerializer xs = new XmlSerializer(typeof(ClaimsResponse)); + xs.Serialize(ms, fields); + + ms.Position = 0; + ClaimsResponse fields2 = (ClaimsResponse)xs.Deserialize(ms); + Assert.AreEqual(fields, fields2); + } + + [TestMethod] + public void EqualityTest() { + ClaimsResponse fields1 = this.GetFilledData(); + + Assert.AreNotEqual(fields1, null); + Assert.AreNotEqual(fields1, "string"); + + ClaimsResponse fields2 = this.GetFilledData(); + Assert.AreNotSame(fields1, fields2, "Test sanity check."); + Assert.AreEqual(fields1, fields2); + + // go through each property and change it slightly and make sure it causes inequality. + fields2.Email += "q"; + Assert.AreNotEqual(fields1, fields2); + fields1.Email = fields2.Email; + Assert.AreEqual(fields1, fields2, "Test sanity check."); + fields2.BirthDate = DateTime.Now; + Assert.AreNotEqual(fields1, fields2); + fields2.BirthDate = fields1.BirthDate; + Assert.AreEqual(fields1, fields2, "Test sanity check."); + fields2.Country += "q"; + Assert.AreNotEqual(fields1, fields2); + fields2.Country = fields1.Country; + Assert.AreEqual(fields1, fields2, "Test sanity check."); + fields2.FullName += "q"; + Assert.AreNotEqual(fields1, fields2); + fields2.FullName = fields1.FullName; + Assert.AreEqual(fields1, fields2, "Test sanity check."); + fields2.Gender = Gender.Female; + Assert.AreNotEqual(fields1, fields2); + fields2.Gender = fields1.Gender; + Assert.AreEqual(fields1, fields2, "Test sanity check."); + fields2.Language = "gb"; + Assert.AreNotEqual(fields1, fields2); + fields2.Language = fields1.Language; + Assert.AreEqual(fields1, fields2, "Test sanity check."); + fields2.Nickname += "q"; + Assert.AreNotEqual(fields1, fields2); + fields2.Nickname = fields1.Nickname; + Assert.AreEqual(fields1, fields2, "Test sanity check."); + fields2.PostalCode += "q"; + Assert.AreNotEqual(fields1, fields2); + fields2.PostalCode = fields1.PostalCode; + Assert.AreEqual(fields1, fields2, "Test sanity check."); + fields2.TimeZone += "q"; + Assert.AreNotEqual(fields1, fields2); + } + + [TestMethod] + public void Birthdates() { + var response = new ClaimsResponse(); + // Verify that they both start out as null + Assert.IsNull(response.BirthDateRaw); + Assert.IsFalse(response.BirthDate.HasValue); + + // Verify that null can be set. + response.BirthDate = null; + response.BirthDateRaw = null; + Assert.IsNull(response.BirthDateRaw); + Assert.IsFalse(response.BirthDate.HasValue); + + // Verify that the strong-typed BirthDate property can be set and that it affects the raw property. + response.BirthDate = DateTime.Parse("April 4, 1984"); + Assert.AreEqual(4, response.BirthDate.Value.Month); + Assert.AreEqual("1984-04-04", response.BirthDateRaw); + + // Verify that the raw property can be set with a complete birthdate and that it affects the strong-typed property. + response.BirthDateRaw = "1998-05-08"; + Assert.AreEqual("1998-05-08", response.BirthDateRaw); + Assert.AreEqual(DateTime.Parse("May 8, 1998", CultureInfo.InvariantCulture), response.BirthDate); + + // Verify that an partial raw birthdate works, and sets the strong-typed property to null since it cannot be represented. + response.BirthDateRaw = "2000-00-00"; + Assert.AreEqual("2000-00-00", response.BirthDateRaw); + Assert.IsFalse(response.BirthDate.HasValue); + } + + [TestMethod, ExpectedException(typeof(ArgumentException))] + public void InvalidRawBirthdate() { + var response = new ClaimsResponse(); + response.BirthDateRaw = "2008"; + } + + private ClaimsResponse GetFilledData() { + return new ClaimsResponse(Constants.sreg_ns) { + BirthDate = new DateTime(2005, 2, 3), + Culture = new System.Globalization.CultureInfo("en-US"), + Email = "a@b.com", + FullName = "Jimmy buffet", + Gender = Gender.Male, + Nickname = "Jimbo", + PostalCode = "12345", + TimeZone = "PST", + }; + } + } +} diff --git a/src/DotNetOpenAuth.Test/OpenId/RelyingParty/OpenIdRelyingPartyTests.cs b/src/DotNetOpenAuth.Test/OpenId/RelyingParty/OpenIdRelyingPartyTests.cs index d53810a..3f8ff22 100644 --- a/src/DotNetOpenAuth.Test/OpenId/RelyingParty/OpenIdRelyingPartyTests.cs +++ b/src/DotNetOpenAuth.Test/OpenId/RelyingParty/OpenIdRelyingPartyTests.cs @@ -1,35 +1,35 @@ -//-----------------------------------------------------------------------
-// <copyright file="OpenIdRelyingPartyTests.cs" company="Andrew Arnott">
-// Copyright (c) Andrew Arnott. All rights reserved.
-// </copyright>
-//-----------------------------------------------------------------------
-
-namespace DotNetOpenAuth.Test.OpenId.RelyingParty {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using DotNetOpenAuth.Messaging.Bindings;
- using DotNetOpenAuth.OpenId;
- using DotNetOpenAuth.OpenId.RelyingParty;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
-
- [TestClass]
- public class OpenIdRelyingPartyTests : OpenIdTestBase {
- [TestInitialize]
- public override void SetUp() {
- base.SetUp();
- }
-
- [TestMethod, Ignore] // ignored, pending work to make dumb mode a supported scenario.
- public void CtorNullAssociationStore() {
- new OpenIdRelyingParty(null, null, null);
- }
-
- [TestMethod, ExpectedException(typeof(ArgumentNullException))]
- public void SecuritySettingsSetNull() {
- var rp = new OpenIdRelyingParty(new AssociationMemoryStore<Uri>(), new NonceMemoryStore(TimeSpan.FromMinutes(5)), new PrivateSecretMemoryStore());
- rp.SecuritySettings = null;
- }
- }
-}
+//----------------------------------------------------------------------- +// <copyright file="OpenIdRelyingPartyTests.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test.OpenId.RelyingParty { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using DotNetOpenAuth.Messaging.Bindings; + using DotNetOpenAuth.OpenId; + using DotNetOpenAuth.OpenId.RelyingParty; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class OpenIdRelyingPartyTests : OpenIdTestBase { + [TestInitialize] + public override void SetUp() { + base.SetUp(); + } + + [TestMethod, Ignore] // ignored, pending work to make dumb mode a supported scenario. + public void CtorNullAssociationStore() { + new OpenIdRelyingParty(null, null, null); + } + + [TestMethod, ExpectedException(typeof(ArgumentNullException))] + public void SecuritySettingsSetNull() { + var rp = new OpenIdRelyingParty(new AssociationMemoryStore<Uri>(), new NonceMemoryStore(TimeSpan.FromMinutes(5)), new PrivateSecretMemoryStore()); + rp.SecuritySettings = null; + } + } +} diff --git a/src/DotNetOpenAuth.Test/OpenId/RelyingParty/PositiveAuthenticationResponseTests.cs b/src/DotNetOpenAuth.Test/OpenId/RelyingParty/PositiveAuthenticationResponseTests.cs index 7a194b7..048deba 100644 --- a/src/DotNetOpenAuth.Test/OpenId/RelyingParty/PositiveAuthenticationResponseTests.cs +++ b/src/DotNetOpenAuth.Test/OpenId/RelyingParty/PositiveAuthenticationResponseTests.cs @@ -1,60 +1,60 @@ -//-----------------------------------------------------------------------
-// <copyright file="PositiveAuthenticationResponseTests.cs" company="Andrew Arnott">
-// Copyright (c) Andrew Arnott. All rights reserved.
-// </copyright>
-//-----------------------------------------------------------------------
-
-namespace DotNetOpenAuth.Test.OpenId.RelyingParty {
- using System;
- using DotNetOpenAuth.Messaging;
- using DotNetOpenAuth.OpenId;
- using DotNetOpenAuth.OpenId.Messages;
- using DotNetOpenAuth.OpenId.RelyingParty;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
-
- [TestClass]
- public class PositiveAuthenticationResponseTests : OpenIdTestBase {
- private readonly Realm realm = new Realm(TestSupport.GetFullUrl(TestSupport.ConsumerPage).AbsoluteUri);
- private readonly Uri returnTo = TestSupport.GetFullUrl(TestSupport.ConsumerPage);
-
- [TestInitialize]
- public override void SetUp() {
- base.SetUp();
- }
-
- /// <summary>
- /// Verifies good, positive assertions are accepted.
- /// </summary>
- [TestMethod]
- public void Valid() {
- PositiveAssertionResponse assertion = this.GetPositiveAssertion();
- var rp = CreateRelyingParty();
- var authResponse = new PositiveAuthenticationResponse(assertion, rp);
- Assert.AreEqual(AuthenticationStatus.Authenticated, authResponse.Status);
- }
-
- /// <summary>
- /// Verifies that the RP rejects signed solicited assertions by an OP that
- /// makes up a claimed Id that was not part of the original request, and
- /// that the OP has no authority to assert positively regarding.
- /// </summary>
- [TestMethod, ExpectedException(typeof(ProtocolException))]
- public void SpoofedClaimedIdDetectionSolicited() {
- PositiveAssertionResponse assertion = this.GetPositiveAssertion();
- assertion.ProviderEndpoint = new Uri("http://rogueOP");
- var rp = CreateRelyingParty();
- var authResponse = new PositiveAuthenticationResponse(assertion, rp);
- Assert.AreEqual(AuthenticationStatus.Failed, authResponse.Status);
- }
-
- private PositiveAssertionResponse GetPositiveAssertion() {
- Protocol protocol = Protocol.Default;
- PositiveAssertionResponse assertion = new PositiveAssertionResponse(protocol.Version, this.returnTo);
- assertion.ClaimedIdentifier = TestSupport.GetMockIdentifier(TestSupport.Scenarios.AutoApproval, this.MockResponder, protocol.ProtocolVersion);
- assertion.LocalIdentifier = TestSupport.GetDelegateUrl(TestSupport.Scenarios.AutoApproval);
- assertion.ReturnTo = this.returnTo;
- assertion.ProviderEndpoint = TestSupport.GetFullUrl("/" + TestSupport.ProviderPage, null, false);
- return assertion;
- }
- }
-}
+//----------------------------------------------------------------------- +// <copyright file="PositiveAuthenticationResponseTests.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test.OpenId.RelyingParty { + using System; + using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.OpenId; + using DotNetOpenAuth.OpenId.Messages; + using DotNetOpenAuth.OpenId.RelyingParty; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class PositiveAuthenticationResponseTests : OpenIdTestBase { + private readonly Realm realm = new Realm(TestSupport.GetFullUrl(TestSupport.ConsumerPage).AbsoluteUri); + private readonly Uri returnTo = TestSupport.GetFullUrl(TestSupport.ConsumerPage); + + [TestInitialize] + public override void SetUp() { + base.SetUp(); + } + + /// <summary> + /// Verifies good, positive assertions are accepted. + /// </summary> + [TestMethod] + public void Valid() { + PositiveAssertionResponse assertion = this.GetPositiveAssertion(); + var rp = CreateRelyingParty(); + var authResponse = new PositiveAuthenticationResponse(assertion, rp); + Assert.AreEqual(AuthenticationStatus.Authenticated, authResponse.Status); + } + + /// <summary> + /// Verifies that the RP rejects signed solicited assertions by an OP that + /// makes up a claimed Id that was not part of the original request, and + /// that the OP has no authority to assert positively regarding. + /// </summary> + [TestMethod, ExpectedException(typeof(ProtocolException))] + public void SpoofedClaimedIdDetectionSolicited() { + PositiveAssertionResponse assertion = this.GetPositiveAssertion(); + assertion.ProviderEndpoint = new Uri("http://rogueOP"); + var rp = CreateRelyingParty(); + var authResponse = new PositiveAuthenticationResponse(assertion, rp); + Assert.AreEqual(AuthenticationStatus.Failed, authResponse.Status); + } + + private PositiveAssertionResponse GetPositiveAssertion() { + Protocol protocol = Protocol.Default; + PositiveAssertionResponse assertion = new PositiveAssertionResponse(protocol.Version, this.returnTo); + assertion.ClaimedIdentifier = TestSupport.GetMockIdentifier(TestSupport.Scenarios.AutoApproval, this.MockResponder, protocol.ProtocolVersion); + assertion.LocalIdentifier = TestSupport.GetDelegateUrl(TestSupport.Scenarios.AutoApproval); + assertion.ReturnTo = this.returnTo; + assertion.ProviderEndpoint = TestSupport.GetFullUrl("/" + TestSupport.ProviderPage, null, false); + return assertion; + } + } +} diff --git a/src/DotNetOpenAuth.Test/OpenId/RelyingParty/ServiceEndpointTests.cs b/src/DotNetOpenAuth.Test/OpenId/RelyingParty/ServiceEndpointTests.cs index 897904c..3d41623 100644 --- a/src/DotNetOpenAuth.Test/OpenId/RelyingParty/ServiceEndpointTests.cs +++ b/src/DotNetOpenAuth.Test/OpenId/RelyingParty/ServiceEndpointTests.cs @@ -1,174 +1,174 @@ -//-----------------------------------------------------------------------
-// <copyright file="ServiceEndpointTests.cs" company="Andrew Arnott">
-// Copyright (c) Andrew Arnott. All rights reserved.
-// </copyright>
-//-----------------------------------------------------------------------
-
-namespace DotNetOpenAuth.Test.OpenId.RelyingParty {
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Text;
- using DotNetOpenAuth.Messaging;
- using DotNetOpenAuth.OpenId;
- using DotNetOpenAuth.OpenId.RelyingParty;
- using DotNetOpenAuth.Test.Messaging;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
-
- [TestClass]
- public class ServiceEndpointTests : OpenIdTestBase {
- private UriIdentifier claimedId = new UriIdentifier("http://claimedid.justatest.com");
- private XriIdentifier claimedXri = new XriIdentifier("=!9B72.7DD1.50A9.5CCD");
- private XriIdentifier userSuppliedXri = new XriIdentifier("=Arnot");
- private Uri providerEndpoint = new Uri("http://someprovider.com");
- private Identifier localId = "http://localid.someprovider.com";
- private string[] v20TypeUris = { Protocol.V20.ClaimedIdentifierServiceTypeURI };
- private string[] v11TypeUris = { Protocol.V11.ClaimedIdentifierServiceTypeURI };
- private int servicePriority = 10;
- private int uriPriority = 10;
-
- [TestMethod]
- public void Ctor() {
- ServiceEndpoint se = ServiceEndpoint.CreateForClaimedIdentifier(this.claimedId, this.localId, new ProviderEndpointDescription(this.providerEndpoint, this.v20TypeUris), this.servicePriority, this.uriPriority);
- Assert.AreSame(this.claimedId, se.ClaimedIdentifier);
- Assert.AreSame(this.providerEndpoint, se.ProviderEndpoint);
- Assert.AreSame(this.localId, se.ProviderLocalIdentifier);
- CollectionAssert<string>.AreEquivalent(this.v20TypeUris, se.ProviderSupportedServiceTypeUris);
- Assert.AreEqual(this.servicePriority, ((IXrdsProviderEndpoint)se).ServicePriority);
- }
-
- [TestMethod]
- public void CtorImpliedLocalIdentifier() {
- ServiceEndpoint se = ServiceEndpoint.CreateForClaimedIdentifier(this.claimedId, null, new ProviderEndpointDescription(this.providerEndpoint, this.v20TypeUris), this.servicePriority, this.uriPriority);
- Assert.AreSame(this.claimedId, se.ClaimedIdentifier);
- Assert.AreSame(this.providerEndpoint, se.ProviderEndpoint);
- Assert.AreSame(this.claimedId, se.ProviderLocalIdentifier);
- CollectionAssert<string>.AreEquivalent(this.v20TypeUris, se.ProviderSupportedServiceTypeUris);
- }
-
- [TestMethod]
- public void ProtocolDetection() {
- ServiceEndpoint se = ServiceEndpoint.CreateForClaimedIdentifier(this.claimedId, this.localId, new ProviderEndpointDescription(this.providerEndpoint, this.v20TypeUris), this.servicePriority, this.uriPriority);
- Assert.AreSame(Protocol.V20, se.Protocol);
- se = ServiceEndpoint.CreateForClaimedIdentifier(
- this.claimedId,
- this.localId,
- new ProviderEndpointDescription(this.providerEndpoint, new[] { Protocol.V20.OPIdentifierServiceTypeURI }),
- this.servicePriority,
- this.uriPriority);
- Assert.AreSame(Protocol.V20, se.Protocol);
- se = ServiceEndpoint.CreateForClaimedIdentifier(this.claimedId, this.localId, new ProviderEndpointDescription(this.providerEndpoint, this.v11TypeUris), this.servicePriority, this.uriPriority);
- Assert.AreSame(Protocol.V11, se.Protocol);
- }
-
- [TestMethod, ExpectedException(typeof(ProtocolException))]
- public void ProtocolDetectionWithoutClues() {
- ServiceEndpoint se = ServiceEndpoint.CreateForClaimedIdentifier(
- this.claimedId,
- this.localId,
- new ProviderEndpointDescription(this.providerEndpoint, new[] { Protocol.V20.HtmlDiscoveryLocalIdKey }), // random type URI irrelevant to detection
- this.servicePriority,
- this.uriPriority);
- }
-
- [TestMethod]
- public void SerializationWithUri() {
- ServiceEndpoint se = ServiceEndpoint.CreateForClaimedIdentifier(this.claimedId, this.localId, new ProviderEndpointDescription(this.providerEndpoint, this.v20TypeUris), this.servicePriority, this.uriPriority);
- StringBuilder sb = new StringBuilder();
- using (StringWriter sw = new StringWriter(sb)) {
- se.Serialize(sw);
- }
- using (StringReader sr = new StringReader(sb.ToString())) {
- ServiceEndpoint se2 = ServiceEndpoint.Deserialize(sr);
- Assert.AreEqual(se, se2);
- Assert.AreEqual(se.Protocol.Version, se2.Protocol.Version, "Particularly interested in this, since type URIs are not serialized but version info is.");
- Assert.AreEqual(se.UserSuppliedIdentifier, se2.UserSuppliedIdentifier);
- Assert.AreEqual(se.FriendlyIdentifierForDisplay, se2.FriendlyIdentifierForDisplay);
- }
- }
-
- [TestMethod]
- public void SerializationWithXri() {
- ServiceEndpoint se = ServiceEndpoint.CreateForClaimedIdentifier(this.claimedXri, this.userSuppliedXri, this.localId, new ProviderEndpointDescription(this.providerEndpoint, this.v20TypeUris), this.servicePriority, this.uriPriority);
- StringBuilder sb = new StringBuilder();
- using (StringWriter sw = new StringWriter(sb)) {
- se.Serialize(sw);
- }
- using (StringReader sr = new StringReader(sb.ToString())) {
- ServiceEndpoint se2 = ServiceEndpoint.Deserialize(sr);
- Assert.AreEqual(se, se2);
- Assert.AreEqual(se.Protocol.Version, se2.Protocol.Version, "Particularly interested in this, since type URIs are not serialized but version info is.");
- Assert.AreEqual(se.UserSuppliedIdentifier, se2.UserSuppliedIdentifier);
- Assert.AreEqual(se.FriendlyIdentifierForDisplay, se2.FriendlyIdentifierForDisplay);
- }
- }
-
- [TestMethod]
- public void EqualsTests() {
- ServiceEndpoint se = ServiceEndpoint.CreateForClaimedIdentifier(this.claimedId, this.localId, new ProviderEndpointDescription(this.providerEndpoint, this.v20TypeUris), this.servicePriority, this.uriPriority);
- ServiceEndpoint se2 = ServiceEndpoint.CreateForClaimedIdentifier(this.claimedId, this.localId, new ProviderEndpointDescription(this.providerEndpoint, this.v20TypeUris), (int?)null, (int?)null);
- Assert.AreEqual(se2, se);
- Assert.AreNotEqual(se, null);
- Assert.AreNotEqual(null, se);
-
- ServiceEndpoint se3 = ServiceEndpoint.CreateForClaimedIdentifier(new UriIdentifier(this.claimedId + "a"), this.localId, new ProviderEndpointDescription(this.providerEndpoint, this.v20TypeUris), this.servicePriority, this.uriPriority);
- Assert.AreNotEqual(se, se3);
- se3 = ServiceEndpoint.CreateForClaimedIdentifier(this.claimedId, this.localId, new ProviderEndpointDescription(new Uri(this.providerEndpoint.AbsoluteUri + "a"), this.v20TypeUris), this.servicePriority, this.uriPriority);
- Assert.AreNotEqual(se, se3);
- se3 = ServiceEndpoint.CreateForClaimedIdentifier(this.claimedId, this.localId + "a", new ProviderEndpointDescription(this.providerEndpoint, this.v20TypeUris), this.servicePriority, this.uriPriority);
- Assert.AreNotEqual(se, se3);
- se3 = ServiceEndpoint.CreateForClaimedIdentifier(this.claimedId, this.localId, new ProviderEndpointDescription(this.providerEndpoint, this.v11TypeUris), this.servicePriority, this.uriPriority);
- Assert.AreNotEqual(se, se3);
-
- // make sure that Collection<T>.Contains works as desired.
- List<ServiceEndpoint> list = new List<ServiceEndpoint>();
- list.Add(se);
- Assert.IsTrue(list.Contains(se2));
- }
-
- [TestMethod]
- public void FriendlyIdentifierForDisplay() {
- Uri providerEndpoint = new Uri("http://someprovider");
- Identifier localId = "someuser";
- string[] serviceTypeUris = new string[] {
- Protocol.V20.ClaimedIdentifierServiceTypeURI,
- };
- ServiceEndpoint se;
-
- // strip of protocol and fragment
- se = ServiceEndpoint.CreateForClaimedIdentifier(
- "http://someprovider.somedomain.com:79/someuser#frag",
- localId,
- new ProviderEndpointDescription(providerEndpoint, serviceTypeUris),
- null,
- null);
- Assert.AreEqual("someprovider.somedomain.com:79/someuser", se.FriendlyIdentifierForDisplay);
-
- // unescape characters
- Uri foreignUri = new Uri("http://server崎/村");
- se = ServiceEndpoint.CreateForClaimedIdentifier(foreignUri, localId, new ProviderEndpointDescription(providerEndpoint, serviceTypeUris), null, null);
- Assert.AreEqual("server崎/村", se.FriendlyIdentifierForDisplay);
-
- // restore user supplied identifier to XRIs
- se = ServiceEndpoint.CreateForClaimedIdentifier(
- new XriIdentifier("=!9B72.7DD1.50A9.5CCD"),
- new XriIdentifier("=Arnott崎村"),
- localId,
- new ProviderEndpointDescription(providerEndpoint, serviceTypeUris),
- null,
- null);
- Assert.AreEqual("=Arnott崎村", se.FriendlyIdentifierForDisplay);
-
- // If UserSuppliedIdentifier is the same as the ClaimedIdentifier, don't display it twice...
- se = ServiceEndpoint.CreateForClaimedIdentifier(
- new XriIdentifier("=!9B72.7DD1.50A9.5CCD"),
- new XriIdentifier("=!9B72.7DD1.50A9.5CCD"),
- localId,
- new ProviderEndpointDescription(providerEndpoint, serviceTypeUris),
- null,
- null);
- Assert.AreEqual("=!9B72.7DD1.50A9.5CCD", se.FriendlyIdentifierForDisplay);
- }
- }
-}
+//----------------------------------------------------------------------- +// <copyright file="ServiceEndpointTests.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test.OpenId.RelyingParty { + using System; + using System.Collections.Generic; + using System.Diagnostics; + using System.IO; + using System.Text; + using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.OpenId; + using DotNetOpenAuth.OpenId.RelyingParty; + using DotNetOpenAuth.Test.Messaging; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class ServiceEndpointTests : OpenIdTestBase { + private UriIdentifier claimedId = new UriIdentifier("http://claimedid.justatest.com"); + private XriIdentifier claimedXri = new XriIdentifier("=!9B72.7DD1.50A9.5CCD"); + private XriIdentifier userSuppliedXri = new XriIdentifier("=Arnot"); + private Uri providerEndpoint = new Uri("http://someprovider.com"); + private Identifier localId = "http://localid.someprovider.com"; + private string[] v20TypeUris = { Protocol.V20.ClaimedIdentifierServiceTypeURI }; + private string[] v11TypeUris = { Protocol.V11.ClaimedIdentifierServiceTypeURI }; + private int servicePriority = 10; + private int uriPriority = 10; + + [TestMethod] + public void Ctor() { + ServiceEndpoint se = ServiceEndpoint.CreateForClaimedIdentifier(this.claimedId, this.localId, new ProviderEndpointDescription(this.providerEndpoint, this.v20TypeUris), this.servicePriority, this.uriPriority); + Assert.AreSame(this.claimedId, se.ClaimedIdentifier); + Assert.AreSame(this.providerEndpoint, se.ProviderEndpoint); + Assert.AreSame(this.localId, se.ProviderLocalIdentifier); + CollectionAssert<string>.AreEquivalent(this.v20TypeUris, se.ProviderSupportedServiceTypeUris); + Assert.AreEqual(this.servicePriority, ((IXrdsProviderEndpoint)se).ServicePriority); + } + + [TestMethod] + public void CtorImpliedLocalIdentifier() { + ServiceEndpoint se = ServiceEndpoint.CreateForClaimedIdentifier(this.claimedId, null, new ProviderEndpointDescription(this.providerEndpoint, this.v20TypeUris), this.servicePriority, this.uriPriority); + Assert.AreSame(this.claimedId, se.ClaimedIdentifier); + Assert.AreSame(this.providerEndpoint, se.ProviderEndpoint); + Assert.AreSame(this.claimedId, se.ProviderLocalIdentifier); + CollectionAssert<string>.AreEquivalent(this.v20TypeUris, se.ProviderSupportedServiceTypeUris); + } + + [TestMethod] + public void ProtocolDetection() { + ServiceEndpoint se = ServiceEndpoint.CreateForClaimedIdentifier(this.claimedId, this.localId, new ProviderEndpointDescription(this.providerEndpoint, this.v20TypeUris), this.servicePriority, this.uriPriority); + Assert.AreSame(Protocol.V20, se.Protocol); + se = ServiceEndpoint.CreateForClaimedIdentifier( + this.claimedId, + this.localId, + new ProviderEndpointDescription(this.providerEndpoint, new[] { Protocol.V20.OPIdentifierServiceTypeURI }), + this.servicePriority, + this.uriPriority); + Assert.AreSame(Protocol.V20, se.Protocol); + se = ServiceEndpoint.CreateForClaimedIdentifier(this.claimedId, this.localId, new ProviderEndpointDescription(this.providerEndpoint, this.v11TypeUris), this.servicePriority, this.uriPriority); + Assert.AreSame(Protocol.V11, se.Protocol); + } + + [TestMethod, ExpectedException(typeof(ProtocolException))] + public void ProtocolDetectionWithoutClues() { + ServiceEndpoint se = ServiceEndpoint.CreateForClaimedIdentifier( + this.claimedId, + this.localId, + new ProviderEndpointDescription(this.providerEndpoint, new[] { Protocol.V20.HtmlDiscoveryLocalIdKey }), // random type URI irrelevant to detection + this.servicePriority, + this.uriPriority); + } + + [TestMethod] + public void SerializationWithUri() { + ServiceEndpoint se = ServiceEndpoint.CreateForClaimedIdentifier(this.claimedId, this.localId, new ProviderEndpointDescription(this.providerEndpoint, this.v20TypeUris), this.servicePriority, this.uriPriority); + StringBuilder sb = new StringBuilder(); + using (StringWriter sw = new StringWriter(sb)) { + se.Serialize(sw); + } + using (StringReader sr = new StringReader(sb.ToString())) { + ServiceEndpoint se2 = ServiceEndpoint.Deserialize(sr); + Assert.AreEqual(se, se2); + Assert.AreEqual(se.Protocol.Version, se2.Protocol.Version, "Particularly interested in this, since type URIs are not serialized but version info is."); + Assert.AreEqual(se.UserSuppliedIdentifier, se2.UserSuppliedIdentifier); + Assert.AreEqual(se.FriendlyIdentifierForDisplay, se2.FriendlyIdentifierForDisplay); + } + } + + [TestMethod] + public void SerializationWithXri() { + ServiceEndpoint se = ServiceEndpoint.CreateForClaimedIdentifier(this.claimedXri, this.userSuppliedXri, this.localId, new ProviderEndpointDescription(this.providerEndpoint, this.v20TypeUris), this.servicePriority, this.uriPriority); + StringBuilder sb = new StringBuilder(); + using (StringWriter sw = new StringWriter(sb)) { + se.Serialize(sw); + } + using (StringReader sr = new StringReader(sb.ToString())) { + ServiceEndpoint se2 = ServiceEndpoint.Deserialize(sr); + Assert.AreEqual(se, se2); + Assert.AreEqual(se.Protocol.Version, se2.Protocol.Version, "Particularly interested in this, since type URIs are not serialized but version info is."); + Assert.AreEqual(se.UserSuppliedIdentifier, se2.UserSuppliedIdentifier); + Assert.AreEqual(se.FriendlyIdentifierForDisplay, se2.FriendlyIdentifierForDisplay); + } + } + + [TestMethod] + public void EqualsTests() { + ServiceEndpoint se = ServiceEndpoint.CreateForClaimedIdentifier(this.claimedId, this.localId, new ProviderEndpointDescription(this.providerEndpoint, this.v20TypeUris), this.servicePriority, this.uriPriority); + ServiceEndpoint se2 = ServiceEndpoint.CreateForClaimedIdentifier(this.claimedId, this.localId, new ProviderEndpointDescription(this.providerEndpoint, this.v20TypeUris), (int?)null, (int?)null); + Assert.AreEqual(se2, se); + Assert.AreNotEqual(se, null); + Assert.AreNotEqual(null, se); + + ServiceEndpoint se3 = ServiceEndpoint.CreateForClaimedIdentifier(new UriIdentifier(this.claimedId + "a"), this.localId, new ProviderEndpointDescription(this.providerEndpoint, this.v20TypeUris), this.servicePriority, this.uriPriority); + Assert.AreNotEqual(se, se3); + se3 = ServiceEndpoint.CreateForClaimedIdentifier(this.claimedId, this.localId, new ProviderEndpointDescription(new Uri(this.providerEndpoint.AbsoluteUri + "a"), this.v20TypeUris), this.servicePriority, this.uriPriority); + Assert.AreNotEqual(se, se3); + se3 = ServiceEndpoint.CreateForClaimedIdentifier(this.claimedId, this.localId + "a", new ProviderEndpointDescription(this.providerEndpoint, this.v20TypeUris), this.servicePriority, this.uriPriority); + Assert.AreNotEqual(se, se3); + se3 = ServiceEndpoint.CreateForClaimedIdentifier(this.claimedId, this.localId, new ProviderEndpointDescription(this.providerEndpoint, this.v11TypeUris), this.servicePriority, this.uriPriority); + Assert.AreNotEqual(se, se3); + + // make sure that Collection<T>.Contains works as desired. + List<ServiceEndpoint> list = new List<ServiceEndpoint>(); + list.Add(se); + Assert.IsTrue(list.Contains(se2)); + } + + [TestMethod] + public void FriendlyIdentifierForDisplay() { + Uri providerEndpoint = new Uri("http://someprovider"); + Identifier localId = "someuser"; + string[] serviceTypeUris = new string[] { + Protocol.V20.ClaimedIdentifierServiceTypeURI, + }; + ServiceEndpoint se; + + // strip of protocol and fragment + se = ServiceEndpoint.CreateForClaimedIdentifier( + "http://someprovider.somedomain.com:79/someuser#frag", + localId, + new ProviderEndpointDescription(providerEndpoint, serviceTypeUris), + null, + null); + Assert.AreEqual("someprovider.somedomain.com:79/someuser", se.FriendlyIdentifierForDisplay); + + // unescape characters + Uri foreignUri = new Uri("http://server崎/村"); + se = ServiceEndpoint.CreateForClaimedIdentifier(foreignUri, localId, new ProviderEndpointDescription(providerEndpoint, serviceTypeUris), null, null); + Assert.AreEqual("server崎/村", se.FriendlyIdentifierForDisplay); + + // restore user supplied identifier to XRIs + se = ServiceEndpoint.CreateForClaimedIdentifier( + new XriIdentifier("=!9B72.7DD1.50A9.5CCD"), + new XriIdentifier("=Arnott崎村"), + localId, + new ProviderEndpointDescription(providerEndpoint, serviceTypeUris), + null, + null); + Assert.AreEqual("=Arnott崎村", se.FriendlyIdentifierForDisplay); + + // If UserSuppliedIdentifier is the same as the ClaimedIdentifier, don't display it twice... + se = ServiceEndpoint.CreateForClaimedIdentifier( + new XriIdentifier("=!9B72.7DD1.50A9.5CCD"), + new XriIdentifier("=!9B72.7DD1.50A9.5CCD"), + localId, + new ProviderEndpointDescription(providerEndpoint, serviceTypeUris), + null, + null); + Assert.AreEqual("=!9B72.7DD1.50A9.5CCD", se.FriendlyIdentifierForDisplay); + } + } +} |