diff options
Diffstat (limited to 'TwoStepsAuthenticator.UnitTests')
7 files changed, 296 insertions, 0 deletions
diff --git a/TwoStepsAuthenticator.UnitTests/CounterAuthenticatorTests.cs b/TwoStepsAuthenticator.UnitTests/CounterAuthenticatorTests.cs new file mode 100644 index 0000000..f834178 --- /dev/null +++ b/TwoStepsAuthenticator.UnitTests/CounterAuthenticatorTests.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using NUnit.Framework; + +namespace TwoStepsAuthenticator.UnitTests { + + [TestFixture] + public class CounterAuthenticatorTests { + + [Test] + public void CreateKey() { + var authenticator = new CounterAuthenticator(); + var secret = Authenticator.GenerateKey(); + var code = authenticator.GetCode(secret, 0uL); + + Assert.IsTrue(authenticator.CheckCode(secret, code, 0uL), "Generated Code doesn't verify"); + } + + // Test Values from http://www.ietf.org/rfc/rfc4226.txt - Appendix D + [TestCase("12345678901234567890", 0uL, "755224")] + [TestCase("12345678901234567890", 1uL, "287082")] + [TestCase("12345678901234567890", 2uL, "359152")] + [TestCase("12345678901234567890", 3uL, "969429")] + [TestCase("12345678901234567890", 4uL, "338314")] + [TestCase("12345678901234567890", 5uL, "254676")] + [TestCase("12345678901234567890", 6uL, "287922")] + [TestCase("12345678901234567890", 7uL, "162583")] + [TestCase("12345678901234567890", 8uL, "399871")] + [TestCase("12345678901234567890", 9uL, "520489")] + public void VerifyKeys(string secret, ulong counter, string code) { + var authenticator = new CounterAuthenticator(); + var base32Secret = Base32Encoding.ToString(Encoding.ASCII.GetBytes(secret)); + + Assert.IsTrue(authenticator.CheckCode(base32Secret, code, counter)); + + } + + [Test] + public void VerifyUsedCounter() { + var authenticator = new CounterAuthenticator(); + + // Test Values from http://www.ietf.org/rfc/rfc4226.txt - Appendix D + var base32Secret = Base32Encoding.ToString(Encoding.ASCII.GetBytes("12345678901234567890")); + + ulong usedCounter; + Assert.True(authenticator.CheckCode(base32Secret, "520489", 0uL, out usedCounter)); + + Assert.AreEqual(usedCounter, 9uL); + } + } +} diff --git a/TwoStepsAuthenticator.UnitTests/MockUsedCodesManager.cs b/TwoStepsAuthenticator.UnitTests/MockUsedCodesManager.cs new file mode 100644 index 0000000..cb0065f --- /dev/null +++ b/TwoStepsAuthenticator.UnitTests/MockUsedCodesManager.cs @@ -0,0 +1,16 @@ +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; + } + } +}
\ No newline at end of file diff --git a/TwoStepsAuthenticator.UnitTests/Properties/AssemblyInfo.cs b/TwoStepsAuthenticator.UnitTests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..9654c60 --- /dev/null +++ b/TwoStepsAuthenticator.UnitTests/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("TwoStepsAuthenticator.UnitTests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyProduct("TwoStepsAuthenticator.UnitTests")] +[assembly: AssemblyCopyright("Copyright © Microsoft 2013")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("257d7270-7a19-44d8-afdf-b21267fc0dda")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/TwoStepsAuthenticator.UnitTests/TimeAuthenticatorTests.cs b/TwoStepsAuthenticator.UnitTests/TimeAuthenticatorTests.cs new file mode 100644 index 0000000..99d1957 --- /dev/null +++ b/TwoStepsAuthenticator.UnitTests/TimeAuthenticatorTests.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; + +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(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(mockUsedCodesManager, () => date); + var secret = Authenticator.GenerateKey(); + var code = authenticator.GetCode(secret); + + authenticator.CheckCode(secret, code); + + Assert.AreEqual(mockUsedCodesManager.LastChallenge, 0uL); + Assert.AreEqual(mockUsedCodesManager.LastCode, code); + } + + [Test] + public void Prevent_code_reuse() { + var date = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + var usedCodesManager = new UsedCodesManager(); + var authenticator = new TimeAuthenticator(() => date, usedCodeManager: usedCodesManager); + var secret = Authenticator.GenerateKey(); + var code = authenticator.GetCode(secret); + + Assert.IsTrue(authenticator.CheckCode(secret, code)); + Assert.IsFalse(authenticator.CheckCode(secret, 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")] + [TestCase("EQOGSM3XZUH6SE2Y", "2013-12-04 18:34:56 +0100", "256804")] + [TestCase("4VU7EQACVDMFJSBG", "2013-12-04 18:36:16 +0100", "800872")] + public void VerifyKeys(string secret, string timeString, string code) + { + var date = DateTime.Parse(timeString); + + var authenticator = new TimeAuthenticator(mockUsedCodesManager, () => date); + Assert.IsTrue(authenticator.CheckCode(secret, code)); + + } + + [Test] + public void VerifyUsedTime() + { + var date = DateTime.Parse("2013-12-05 17:23:50 +0100"); + var authenticator = new TimeAuthenticator(mockUsedCodesManager, () => date); + + DateTime usedTime; + + Assert.True(authenticator.CheckCode("H22Q7WAMQYFZOJ2Q", "696227", out usedTime)); + + // 17:23:50 - 30s + Assert.AreEqual(usedTime.Hour, 17); + Assert.AreEqual(usedTime.Minute, 23); + Assert.AreEqual(usedTime.Second, 20); + } + } +} diff --git a/TwoStepsAuthenticator.UnitTests/TwoStepsAuthenticator.UnitTests.csproj b/TwoStepsAuthenticator.UnitTests/TwoStepsAuthenticator.UnitTests.csproj new file mode 100644 index 0000000..38fce5d --- /dev/null +++ b/TwoStepsAuthenticator.UnitTests/TwoStepsAuthenticator.UnitTests.csproj @@ -0,0 +1,76 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProjectGuid>{E936FFA0-2E6E-4CA7-9841-FB844A817E0C}</ProjectGuid> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>TwoStepsAuthenticator.UnitTests</RootNamespace> + <AssemblyName>TwoStepsAuthenticator.UnitTests</AssemblyName> + <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + <TargetFrameworkProfile /> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + <Prefer32Bit>false</Prefer32Bit> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + <Prefer32Bit>false</Prefer32Bit> + </PropertyGroup> + <PropertyGroup> + <StartupObject /> + </PropertyGroup> + <ItemGroup> + <Reference Include="nunit.framework"> + <HintPath>..\packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath> + </Reference> + <Reference Include="System" /> + <Reference Include="System.Core" /> + <Reference Include="System.Xml.Linq" /> + <Reference Include="System.Data.DataSetExtensions" /> + <Reference Include="Microsoft.CSharp" /> + <Reference Include="System.Data" /> + <Reference Include="System.Xml" /> + </ItemGroup> + <ItemGroup> + <Compile Include="CounterAuthenticatorTests.cs" /> + <Compile Include="MockUsedCodesManager.cs" /> + <Compile Include="TimeAuthenticatorTests.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + <Compile Include="UsedCodesManagerTests.cs" /> + </ItemGroup> + <ItemGroup> + <None Include="packages.config" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\TwoStepsAuthenticator\TwoStepsAuthenticator.csproj"> + <Project>{6c898cd1-0bf3-4711-847e-ad7dac815cd8}</Project> + <Name>TwoStepsAuthenticator</Name> + </ProjectReference> + </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <!-- To modify your build process, add your task inside one of the targets below and uncomment it. + Other similar extension points exist, see Microsoft.Common.targets. + <Target Name="BeforeBuild"> + </Target> + <Target Name="AfterBuild"> + </Target> + --> +</Project>
\ No newline at end of file diff --git a/TwoStepsAuthenticator.UnitTests/UsedCodesManagerTests.cs b/TwoStepsAuthenticator.UnitTests/UsedCodesManagerTests.cs new file mode 100644 index 0000000..d72474c --- /dev/null +++ b/TwoStepsAuthenticator.UnitTests/UsedCodesManagerTests.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using NUnit.Framework; + +namespace TwoStepsAuthenticator.UnitTests { + + [TestFixture] + public class UsedCodesManagerTests { + + [Test] + public void Can_add_codes() { + var manager = new UsedCodesManager(); + + Assert.IsFalse(manager.IsCodeUsed(42L, "def")); + manager.AddCode(42L, "def"); + Assert.IsTrue(manager.IsCodeUsed(42L, "def")); + } + + } +} diff --git a/TwoStepsAuthenticator.UnitTests/packages.config b/TwoStepsAuthenticator.UnitTests/packages.config new file mode 100644 index 0000000..967502d --- /dev/null +++ b/TwoStepsAuthenticator.UnitTests/packages.config @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="NUnit" version="2.6.3" targetFramework="net40" /> +</packages>
\ No newline at end of file |