summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenId.Test/AssociationsTest.cs
blob: 4d6dcfbd7a38d324077efd47f28e6e136e13fe6b (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
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using System.Threading;

namespace DotNetOpenId.Test {
	[TestFixture]
	public class AssociationsTest {
		byte[] sha1Secret = new byte[CryptUtil.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);
		}
	}
}