-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathHotpTests.cs
More file actions
55 lines (50 loc) · 3.36 KB
/
HotpTests.cs
File metadata and controls
55 lines (50 loc) · 3.36 KB
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
using Waher.Runtime.Counters;
namespace Waher.Security.TOTP.Test
{
[TestClass]
public sealed class HotpTests
{
[TestMethod]
[DataRow(HashFunction.SHA1, 6, "3132333435363738393031323334353637383930", 0L, 755224)]
[DataRow(HashFunction.SHA1, 6, "3132333435363738393031323334353637383930", 1L, 287082)]
[DataRow(HashFunction.SHA1, 6, "3132333435363738393031323334353637383930", 2L, 359152)]
[DataRow(HashFunction.SHA1, 6, "3132333435363738393031323334353637383930", 3L, 969429)]
[DataRow(HashFunction.SHA1, 6, "3132333435363738393031323334353637383930", 4L, 338314)]
[DataRow(HashFunction.SHA1, 6, "3132333435363738393031323334353637383930", 5L, 254676)]
[DataRow(HashFunction.SHA1, 6, "3132333435363738393031323334353637383930", 6L, 287922)]
[DataRow(HashFunction.SHA1, 6, "3132333435363738393031323334353637383930", 7L, 162583)]
[DataRow(HashFunction.SHA1, 6, "3132333435363738393031323334353637383930", 8L, 399871)]
[DataRow(HashFunction.SHA1, 6, "3132333435363738393031323334353637383930", 9L, 520489)]
public void Test_01_Calculate(HashFunction HashFunction, int NrDigits, string Secret,
long Counter, int Password)
{
HotpCalculator Hotp = new(NrDigits, Hashes.StringToBinary(Secret), HashFunction);
int ComputedPassword = Hotp.Compute(Counter);
Assert.AreEqual(Password, ComputedPassword);
}
[TestMethod]
[DataRow(HashFunction.SHA1, 6, "3132333435363738393031323334353637383930", -1L, 0L, 755224, OtpValidationResult.Valid)]
[DataRow(HashFunction.SHA1, 6, "3132333435363738393031323334353637383930", 0L, 1L, 287082, OtpValidationResult.Valid)]
[DataRow(HashFunction.SHA1, 6, "3132333435363738393031323334353637383930", 1L, 2L, 359152, OtpValidationResult.Valid)]
[DataRow(HashFunction.SHA1, 6, "3132333435363738393031323334353637383930", 2L, 3L, 969429, OtpValidationResult.Valid)]
[DataRow(HashFunction.SHA1, 6, "3132333435363738393031323334353637383930", 3L, 4L, 338314, OtpValidationResult.Valid)]
[DataRow(HashFunction.SHA1, 6, "3132333435363738393031323334353637383930", 4L, 5L, 254676, OtpValidationResult.Valid)]
[DataRow(HashFunction.SHA1, 6, "3132333435363738393031323334353637383930", 5L, 6L, 287922, OtpValidationResult.Valid)]
[DataRow(HashFunction.SHA1, 6, "3132333435363738393031323334353637383930", 6L, 7L, 162583, OtpValidationResult.Valid)]
[DataRow(HashFunction.SHA1, 6, "3132333435363738393031323334353637383930", 7L, 8L, 399871, OtpValidationResult.Valid)]
[DataRow(HashFunction.SHA1, 6, "3132333435363738393031323334353637383930", 8L, 9L, 520489, OtpValidationResult.Valid)]
[DataRow(HashFunction.SHA1, 6, "3132333435363738393031323334353637383930", 9L, 9L, 520489, OtpValidationResult.CounterInvalid)]
[DataRow(HashFunction.SHA1, 6, "3132333435363738393031323334353637383930", 9L, 10L, 520489, OtpValidationResult.Invalid)]
public async Task Test_02_Validate(HashFunction HashFunction, int NrDigits,
string Secret, long PrevCounter, long Counter, int Password,
OtpValidationResult ExpectedResult)
{
long UsedCounter = await RuntimeCounters.GetCount("HOTP.UnitTest");
await RuntimeCounters.IncrementCounter("HOTP.UnitTest", PrevCounter - UsedCounter);
HotpValidator Hotp = new(NrDigits, Hashes.StringToBinary(Secret), HashFunction,
"UnitTest", TotpTests.Auditor);
ValidationResult Result = await Hotp.Validate("UnitTest", "EP", Counter, Password);
Assert.AreEqual(ExpectedResult, Result.Result);
}
}
}