summaryrefslogtreecommitdiffstats
path: root/TwoStepsAuthenticator.UnitTests
diff options
context:
space:
mode:
authorChristoph Enzmann <christoph.enzmann@confer.ch>2013-12-06 15:20:07 +0100
committerChristoph Enzmann <christoph.enzmann@confer.ch>2013-12-06 15:20:07 +0100
commit41cb13ee263104981850a73d4abf4a583df31941 (patch)
tree46b14ab6b93d813e9f40aae760f4b3902e380800 /TwoStepsAuthenticator.UnitTests
parentff91ec6adbf7f1c816b645844e7f83f4be74e548 (diff)
downloadTwoStepsAuthenticator-41cb13ee263104981850a73d4abf4a583df31941.zip
TwoStepsAuthenticator-41cb13ee263104981850a73d4abf4a583df31941.tar.gz
TwoStepsAuthenticator-41cb13ee263104981850a73d4abf4a583df31941.tar.bz2
Use one global default UsedCodesManager
Diffstat (limited to 'TwoStepsAuthenticator.UnitTests')
-rw-r--r--TwoStepsAuthenticator.UnitTests/CounterAuthenticatorTests.cs23
-rw-r--r--TwoStepsAuthenticator.UnitTests/TimeAuthenticatorTests.cs24
-rw-r--r--TwoStepsAuthenticator.UnitTests/UsedCodesManagerTests.cs14
3 files changed, 55 insertions, 6 deletions
diff --git a/TwoStepsAuthenticator.UnitTests/CounterAuthenticatorTests.cs b/TwoStepsAuthenticator.UnitTests/CounterAuthenticatorTests.cs
index f834178..3a81a90 100644
--- a/TwoStepsAuthenticator.UnitTests/CounterAuthenticatorTests.cs
+++ b/TwoStepsAuthenticator.UnitTests/CounterAuthenticatorTests.cs
@@ -9,10 +9,27 @@ namespace TwoStepsAuthenticator.UnitTests {
[TestFixture]
public class CounterAuthenticatorTests {
+ private MockUsedCodesManager mockUsedCodesManager { get; set; }
+
+ [SetUp]
+ public void SetUp() {
+ this.mockUsedCodesManager = new MockUsedCodesManager();
+ }
+
+ [Test]
+ public void Uses_usedCodesManager() {
+ var authenticator = new CounterAuthenticator(usedCodeManager: mockUsedCodesManager);
+ var secret = Authenticator.GenerateKey();
+ var code = authenticator.GetCode(secret, 42uL);
+
+ authenticator.CheckCode(secret, code, 42uL);
+ Assert.AreEqual(mockUsedCodesManager.LastChallenge, 42uL);
+ Assert.AreEqual(mockUsedCodesManager.LastCode, code);
+ }
[Test]
public void CreateKey() {
- var authenticator = new CounterAuthenticator();
+ var authenticator = new CounterAuthenticator(usedCodeManager: mockUsedCodesManager);
var secret = Authenticator.GenerateKey();
var code = authenticator.GetCode(secret, 0uL);
@@ -31,7 +48,7 @@ namespace TwoStepsAuthenticator.UnitTests {
[TestCase("12345678901234567890", 8uL, "399871")]
[TestCase("12345678901234567890", 9uL, "520489")]
public void VerifyKeys(string secret, ulong counter, string code) {
- var authenticator = new CounterAuthenticator();
+ var authenticator = new CounterAuthenticator(usedCodeManager: mockUsedCodesManager);
var base32Secret = Base32Encoding.ToString(Encoding.ASCII.GetBytes(secret));
Assert.IsTrue(authenticator.CheckCode(base32Secret, code, counter));
@@ -40,7 +57,7 @@ namespace TwoStepsAuthenticator.UnitTests {
[Test]
public void VerifyUsedCounter() {
- var authenticator = new CounterAuthenticator();
+ var authenticator = new CounterAuthenticator(usedCodeManager: mockUsedCodesManager);
// Test Values from http://www.ietf.org/rfc/rfc4226.txt - Appendix D
var base32Secret = Base32Encoding.ToString(Encoding.ASCII.GetBytes("12345678901234567890"));
diff --git a/TwoStepsAuthenticator.UnitTests/TimeAuthenticatorTests.cs b/TwoStepsAuthenticator.UnitTests/TimeAuthenticatorTests.cs
index 3dc6d86..1a1ffc6 100644
--- a/TwoStepsAuthenticator.UnitTests/TimeAuthenticatorTests.cs
+++ b/TwoStepsAuthenticator.UnitTests/TimeAuthenticatorTests.cs
@@ -8,16 +8,34 @@ namespace TwoStepsAuthenticator.UnitTests {
[TestFixture]
public class TimeAuthenticatorTests {
+ private MockUsedCodesManager mockUsedCodesManager { get; set; }
+
+ [SetUp]
+ public void SetUp() {
+ this.mockUsedCodesManager = new MockUsedCodesManager();
+ }
[Test]
public void CreateKey() {
- var authenticator = new TimeAuthenticator();
+ var authenticator = new TimeAuthenticator(usedCodeManager: mockUsedCodesManager);
var secret = Authenticator.GenerateKey();
var code = authenticator.GetCode(secret);
Assert.IsTrue(authenticator.CheckCode(secret, code), "Generated Code doesn't verify");
}
+ [Test]
+ public void Uses_usedCodesManager() {
+ var date = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
+ var authenticator = new TimeAuthenticator(() => date, usedCodeManager: mockUsedCodesManager);
+ var secret = Authenticator.GenerateKey();
+ var code = authenticator.GetCode(secret);
+
+ authenticator.CheckCode(secret, code);
+ Assert.AreEqual(mockUsedCodesManager.LastChallenge, 0uL);
+ Assert.AreEqual(mockUsedCodesManager.LastCode, code);
+ }
+
// Test Vectors from http://tools.ietf.org/html/rfc6238#appendix-B have all length 8. We want a length of 6.
// This Test Vectors are from a Ruby implementation. They work with the Google Authentificator app.
[TestCase("DRMK64PPMMC7TDZF", "2013-12-04 18:33:01 +0100", "661188")]
@@ -26,7 +44,7 @@ namespace TwoStepsAuthenticator.UnitTests {
public void VerifyKeys(string secret, string timeString, string code) {
var date = DateTime.Parse(timeString);
- var authenticator = new TimeAuthenticator(() => date);
+ var authenticator = new TimeAuthenticator(() => date, usedCodeManager: mockUsedCodesManager);
Assert.IsTrue(authenticator.CheckCode(secret, code));
}
@@ -34,7 +52,7 @@ namespace TwoStepsAuthenticator.UnitTests {
[Test]
public void VerifyUsedTime() {
var date = DateTime.Parse("2013-12-05 17:23:50 +0100");
- var authenticator = new TimeAuthenticator(() => date);
+ var authenticator = new TimeAuthenticator(() => date, usedCodeManager: mockUsedCodesManager);
DateTime usedTime;
diff --git a/TwoStepsAuthenticator.UnitTests/UsedCodesManagerTests.cs b/TwoStepsAuthenticator.UnitTests/UsedCodesManagerTests.cs
index 15ae91e..1138f54 100644
--- a/TwoStepsAuthenticator.UnitTests/UsedCodesManagerTests.cs
+++ b/TwoStepsAuthenticator.UnitTests/UsedCodesManagerTests.cs
@@ -20,4 +20,18 @@ namespace TwoStepsAuthenticator.UnitTests {
}
}
+
+ internal class MockUsedCodesManager : IUsedCodesManager {
+ public ulong? LastChallenge { get; private set; }
+ public string LastCode { get; private set; }
+
+ public void AddCode(ulong challenge, string code) {
+ this.LastChallenge = challenge;
+ this.LastCode = code;
+ }
+
+ public bool IsCodeUsed(ulong challenge, string code) {
+ return false;
+ }
+ }
}