summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenId.Test/RelyingParty/TokenTest.cs
blob: c57f68aefced5cd72630afa5628a22c3c53e45cc (plain)
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
using DotNetOpenId.RelyingParty;
using NUnit.Framework;

namespace DotNetOpenId.Test.RelyingParty {
	[TestFixture]
	public class TokenTest {
		static ServiceEndpoint getServiceEndpoint(TestSupport.Scenarios scenario, ProtocolVersion version) {
			Protocol protocol = Protocol.Lookup(version);
			ServiceEndpoint ep = ServiceEndpoint.CreateForClaimedIdentifier(
				TestSupport.GetIdentityUrl(scenario, version),
				TestSupport.GetDelegateUrl(scenario),
				TestSupport.GetFullUrl(TestSupport.ProviderPage),
				new[] { protocol.ClaimedIdentifierServiceTypeURI },
				10,
				10
				);
			return ep;
		}

		/// <summary>
		/// Tests token creation, serialization, and conditional nonce serialization.
		/// </summary>
		void tokenBasics(ProtocolVersion version) {
			ServiceEndpoint ep = getServiceEndpoint(TestSupport.Scenarios.AutoApproval, version);
			Token token = new Token(ep);
			Assert.AreSame(ep, token.Endpoint);
			Assert.IsNotNull(token.Nonce);

			INonceStore store = new ApplicationMemoryStore();
			string serializedToken = token.Serialize(store);

			Token token2 = Token.Deserialize(serializedToken, store);

			Assert.AreEqual(token.Endpoint, token2.Endpoint);
			if (ep.Protocol.Version.Major < 2) {
				Assert.AreEqual(token.Nonce, token2.Nonce);
				Assert.IsNotNull(token2.Nonce);
			} else {
				Assert.IsNull(token2.Nonce);
			}
		}
		[Test]
		public void TokenBasics11() {
			tokenBasics(ProtocolVersion.V11);
		}
		[Test]
		public void TokenBasics20() {
			tokenBasics(ProtocolVersion.V20);
		}

		void replayAttackPrevention(ProtocolVersion version) {
			ServiceEndpoint ep = getServiceEndpoint(TestSupport.Scenarios.AutoApproval, version);
			Token token = new Token(ep);

			INonceStore store = new ApplicationMemoryStore();
			string serializedToken = token.Serialize(store);
			Token.Deserialize(serializedToken, store);
			Token.Deserialize(serializedToken, store);
		}
		[Test, ExpectedException(typeof(OpenIdException))]
		public void ReplayAttackPrevention() {
			replayAttackPrevention(ProtocolVersion.V11);
			// We don't test on V2.0 because tokens are not used for replay attack prevention in OpenID 2.0.
		}

		[Test]
		public void EqualsTest() {
			ServiceEndpoint ep1 = getServiceEndpoint(TestSupport.Scenarios.AutoApproval, ProtocolVersion.V20);
			Token token1a = new Token(ep1);
			Token token1b = new Token(ep1);
			Assert.AreEqual(token1a, token1a, "It's the same object!");
			Assert.AreNotEqual(token1a, token1b, "Two tokens generated for the same service endpoint should have unique nonces.");

			ServiceEndpoint ep2 = getServiceEndpoint(TestSupport.Scenarios.AlwaysDeny, ProtocolVersion.V20);
			Token token2 = new Token(ep2);
			Assert.AreNotEqual(token1a, token2);
		}
	}
}