forked from DotNetOpenAuth/DotNetOpenAuth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssociationTests.cs
78 lines (66 loc) · 3.12 KB
/
AssociationTests.cs
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
//-----------------------------------------------------------------------
// <copyright file="AssociationTests.cs" company="Outercurve Foundation">
// Copyright (c) Outercurve Foundation. 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.Messaging;
using DotNetOpenAuth.OpenId;
using NUnit.Framework;
[TestFixture]
public class AssociationTests : OpenIdTestBase {
private static readonly TimeSpan deltaDateTime = TimeSpan.FromSeconds(2);
private static readonly HashAlgorithm sha1 = DiffieHellmanUtilities.Lookup(Protocol.Default, Protocol.Default.Args.SessionType.DH_SHA1);
private byte[] sha1Secret;
private byte[] sha1Secret2;
[SetUp]
public override void SetUp() {
base.SetUp();
// just a little something to make it at all interesting.
this.sha1Secret = new byte[sha1.HashSize / 8];
this.sha1Secret[0] = 0x33;
this.sha1Secret[1] = 0x55;
this.sha1Secret2 = new byte[sha1.HashSize / 8];
this.sha1Secret2[0] = 0x88;
this.sha1Secret2[1] = 0xcc;
}
[Test]
public void Properties() {
string handle = "somehandle";
TimeSpan lifetime = TimeSpan.FromMinutes(2);
Association assoc = HmacShaAssociation.Create(Protocol.Default, Protocol.Default.Args.SignatureAlgorithm.HMAC_SHA1, handle, this.sha1Secret, lifetime);
Assert.IsFalse(assoc.IsExpired);
Assert.That(assoc.Issued, Is.EqualTo(DateTime.UtcNow).Within(deltaDateTime));
Assert.That(assoc.Expires, Is.EqualTo(DateTime.UtcNow + lifetime).Within(deltaDateTime));
Assert.That(assoc.Handle, Is.EqualTo(handle));
Assert.That(assoc.SecondsTillExpiration, Is.EqualTo(lifetime.TotalSeconds).Within(deltaDateTime.TotalSeconds));
Assert.That(assoc.SecretKey, Is.EqualTo(this.sha1Secret));
Assert.That(assoc.Issued.Millisecond, Is.EqualTo(0), "No milliseconds because this can be cut off in conversions.");
}
[Test]
public void Sign() {
Association assoc1 = HmacShaAssociation.Create(Protocol.Default, Protocol.Default.Args.SignatureAlgorithm.HMAC_SHA1, "h1", this.sha1Secret, TimeSpan.FromMinutes(2));
Association assoc2 = HmacShaAssociation.Create(Protocol.Default, Protocol.Default.Args.SignatureAlgorithm.HMAC_SHA1, "h2", this.sha1Secret2, TimeSpan.FromMinutes(2));
var data = new byte[] { 0xdd, 0xcc };
// sign once and verify that it's sane
byte[] signature1 = assoc1.Sign(data);
Assert.That(signature1, Is.Not.Null);
Assert.That(signature1.Length, Is.Not.EqualTo(0));
// sign again and make sure it's different
byte[] signature2 = assoc2.Sign(data);
Assert.That(signature2, Is.Not.Null);
Assert.That(signature2.Length, Is.Not.EqualTo(0));
Assert.That(signature1, Is.Not.EqualTo(signature2));
// sign again with the same secret and make sure it's the same.
Assert.That(assoc1.Sign(data), Is.EqualTo(signature1));
// now change the data and make sure signature changes
data[1] = 0xee;
Assert.That(assoc1.Sign(data), Is.Not.EqualTo(signature1));
}
}
}