summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/OpenId/AssociationsTests.cs
blob: b3d7e4d68b693b42043bd0c1338ae23e480b2bf7 (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
//-----------------------------------------------------------------------
// <copyright file="AssociationsTests.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Test.OpenId {
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Security.Cryptography;
	using System.Text;
	using DotNetOpenAuth.OpenId;
	using NUnit.Framework;

	[TestFixture]
	public class AssociationsTests : OpenIdTestBase {
		private static readonly HashAlgorithm sha1 = DiffieHellmanUtilities.Lookup(Protocol.Default, Protocol.Default.Args.SessionType.DH_SHA1);
		private byte[] sha1Secret;
		private Associations assocs;

		[TestFixtureSetUp]
		public override void SetUp() {
			this.sha1Secret = new byte[sha1.HashSize / 8];
			this.assocs = new Associations();
		}

		[TestCase]
		public void GetNonexistentHandle() {
			Assert.IsNull(this.assocs.Get("someinvalidhandle"));
		}

		[TestCase]
		public void RemoveNonexistentHandle() {
			Assert.IsFalse(this.assocs.Remove("someinvalidhandle"));
		}

		[TestCase]
		public void HandleLifecycle() {
			Association a = HmacShaAssociation.Create(
				Protocol.Default,
				Protocol.Default.Args.SignatureAlgorithm.HMAC_SHA1,
				"somehandle",
				this.sha1Secret,
				TimeSpan.FromDays(1));
			this.assocs.Set(a);
			Assert.AreSame(a, this.assocs.Get(a.Handle));
			Assert.IsTrue(this.assocs.Remove(a.Handle));
			Assert.IsNull(this.assocs.Get(a.Handle));
			Assert.IsFalse(this.assocs.Remove(a.Handle));
		}

		[TestCase]
		public void Best() {
			Association a = HmacShaAssociation.Create(
				Protocol.Default,
				Protocol.Default.Args.SignatureAlgorithm.HMAC_SHA1,
				"h1",
				this.sha1Secret,
				TimeSpan.FromHours(1));
			Association b = HmacShaAssociation.Create(
				Protocol.Default,
				Protocol.Default.Args.SignatureAlgorithm.HMAC_SHA1,
				"h2",
				this.sha1Secret,
				TimeSpan.FromHours(1));

			this.assocs.Set(a);
			this.assocs.Set(b);

			// make b the best by making a older
			a.Issued -= TimeSpan.FromHours(1);
			Assert.AreSame(b, this.assocs.Best.FirstOrDefault());
			// now make a the best
			b.Issued -= TimeSpan.FromHours(2);
			Assert.AreSame(a, this.assocs.Best.FirstOrDefault());
		}
	}
}