diff --git a/src/Tests/AliasVault.UnitTests/AliasVault.UnitTests.csproj b/src/Tests/AliasVault.UnitTests/AliasVault.UnitTests.csproj index 14e1ed894..48194f5f5 100644 --- a/src/Tests/AliasVault.UnitTests/AliasVault.UnitTests.csproj +++ b/src/Tests/AliasVault.UnitTests/AliasVault.UnitTests.csproj @@ -55,6 +55,7 @@ + diff --git a/src/Tests/AliasVault.UnitTests/Utilities/FaviconExtractorTests.cs b/src/Tests/AliasVault.UnitTests/Utilities/FaviconExtractorTests.cs index f62b49fb6..c2734e81d 100644 --- a/src/Tests/AliasVault.UnitTests/Utilities/FaviconExtractorTests.cs +++ b/src/Tests/AliasVault.UnitTests/Utilities/FaviconExtractorTests.cs @@ -17,7 +17,7 @@ public class FaviconExtractorTests /// /// Task. [Test] - public async Task ExtractFaviconSpamOK() + public async Task ExtractFaviconSpamOk() { var faviconBytes = await FaviconExtractor.FaviconExtractor.GetFaviconAsync("https://spamok.com"); Assert.That(faviconBytes, Is.Not.Null); diff --git a/src/Tests/AliasVault.UnitTests/Utilities/TotpGeneratorTests.cs b/src/Tests/AliasVault.UnitTests/Utilities/TotpGeneratorTests.cs new file mode 100644 index 000000000..b276f3520 --- /dev/null +++ b/src/Tests/AliasVault.UnitTests/Utilities/TotpGeneratorTests.cs @@ -0,0 +1,82 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) lanedirt. All rights reserved. +// Licensed under the MIT license. See LICENSE.md file in the project root for full license information. +// +//----------------------------------------------------------------------- + +namespace AliasVault.Tests.Utilities; + +/// +/// Tests for the TotpGeneratorTests class. +/// +public class TotpGeneratorTests +{ + private const string TestSecretKey = "JBSWY3DPEHPK3PXP"; + + /// + /// Tests if the GenerateTotpCode method returns a valid code. + /// + [Test] + public void GenerateTotpCode_ReturnsValidCode() + { + string code = TotpGenerator.TotpGenerator.GenerateTotpCode(TestSecretKey); + Assert.That(code, Has.Length.EqualTo(6)); + Assert.That(code, Does.Match(@"^\d{6}$")); + } + + /// + /// Tests if the GenerateTotpCode method returns a code with the correct length. + /// + [Test] + public void GenerateTotpCode_WithCustomDigits_ReturnsCodeWithCorrectLength() + { + string code = TotpGenerator.TotpGenerator.GenerateTotpCode(TestSecretKey, digits: 8); + Assert.That(code, Has.Length.EqualTo(8)); + Assert.That(code, Does.Match(@"^\d{8}$")); + } + + /// + /// Tests if the GenerateTotpCode method returns a code when the secret key contains spaces and hyphens. + /// + [Test] + public void GenerateTotpCode_WithSpacesAndHyphens_ReturnsValidCode() + { + string secretWithSpacesAndHyphens = "JBSW Y3DP-EHPK 3PXP"; + string code = TotpGenerator.TotpGenerator.GenerateTotpCode(secretWithSpacesAndHyphens); + Assert.That(code, Has.Length.EqualTo(6)); + Assert.That(code, Does.Match(@"^\d{6}$")); + } + + /// + /// Tests if the VerifyTotpCode method returns true for a valid code. + /// + [Test] + public void VerifyTotpCode_WithValidCode_ReturnsTrue() + { + string code = TotpGenerator.TotpGenerator.GenerateTotpCode(TestSecretKey); + bool isValid = TotpGenerator.TotpGenerator.VerifyTotpCode(TestSecretKey, code); + Assert.That(isValid, Is.True); + } + + /// + /// Tests if the VerifyTotpCode method returns false for an invalid code. + /// + [Test] + public void VerifyTotpCode_WithInvalidCode_ReturnsFalse() + { + string invalidCode = "000000"; + bool isValid = TotpGenerator.TotpGenerator.VerifyTotpCode(TestSecretKey, invalidCode); + Assert.That(isValid, Is.False); + } + + /// + /// Tests if the VerifyTotpCode method throws an exception for an invalid secret key. + /// + [Test] + public void GenerateTotpCode_WithInvalidSecretKey_ThrowsException() + { + string invalidSecret = "INVALID!@#"; + Assert.Throws(() => TotpGenerator.TotpGenerator.GenerateTotpCode(invalidSecret)); + } +} diff --git a/src/Utilities/TotpGenerator/TotpGenerator.cs b/src/Utilities/TotpGenerator/TotpGenerator.cs index 7d74357c6..ec11b7d75 100644 --- a/src/Utilities/TotpGenerator/TotpGenerator.cs +++ b/src/Utilities/TotpGenerator/TotpGenerator.cs @@ -14,7 +14,7 @@ using OtpNet; /// public class TotpGenerator { - /// + /// /// Generates a Time-based One-Time Password (TOTP) for the given secret key. /// /// The secret key in Base32 encoding. diff --git a/src/Utilities/TotpGenerator/TotpGenerator.csproj b/src/Utilities/TotpGenerator/TotpGenerator.csproj index ea4f23bc5..ecc045c64 100644 --- a/src/Utilities/TotpGenerator/TotpGenerator.csproj +++ b/src/Utilities/TotpGenerator/TotpGenerator.csproj @@ -8,10 +8,12 @@ true + bin\Debug\net8.0\TotpGenerator.xml true + bin\Release\net8.0\TotpGenerator.xml