summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenId.Test/AssociationsTest.cs
blob: 01dad58f136e7d2e06ed9042e23d4a68860f8fca (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
using System;
using System.Security.Cryptography;
using NUnit.Framework;

namespace DotNetOpenId.Test {
	[TestFixture]
	public class AssociationsTest {
		static HashAlgorithm sha1 = DiffieHellmanUtil.Lookup(Protocol.Default, Protocol.Default.Args.SessionType.DH_SHA1);
		byte[] sha1Secret = new byte[sha1.HashSize / 8];

		Associations assocs;
		[SetUp]
		public void SetUp() {
			assocs = new Associations();
		}

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

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

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

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

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

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