public void GetCodeBadHashAlgotithm()
        {
            var options = GetOptions();
            var service = new AuthenticatorService(options, new DefaultSystemTime());

            Assert.Throws <ArgumentException>(() => service.GetCode((HashAlgorithmType)100, Encoding.UTF8.GetBytes("test"), 8, 30));
        }
        public void GetCodeInvalidPeriodInSeconds(byte periodInSeconds)
        {
            var options = GetOptions();
            var service = new AuthenticatorService(options, new DefaultSystemTime());

            var ex = Assert.Throws <ArgumentException>(() => service.GetCode(HashAlgorithmType.SHA1, Encoding.UTF8.GetBytes("12345678901234567890"), 6, periodInSeconds));

            Assert.Equal("The period must be at least 30 seconds.", ex.Message);
        }
        public void GetCodeInvalidNumberOfDigits(byte numberOfDigit)
        {
            var options = GetOptions();
            var service = new AuthenticatorService(options, new DefaultSystemTime());

            var ex = Assert.Throws <ArgumentException>(() => service.GetCode(HashAlgorithmType.SHA1, Encoding.UTF8.GetBytes("12345678901234567890"), numberOfDigit, 30));

            Assert.Equal("The number of digits must be between 6 and 8.", ex.Message);
        }
        public void GetCodeTest(long time, int expectedCode, HashAlgorithmType hashAlgorithm, string secret)
        {
            //https://tools.ietf.org/html/rfc6238#appendix-B

            var systemTime = new Mock <ISystemTime>(MockBehavior.Strict);

            systemTime
            .Setup(a => a.GetUtcNow())
            .Returns(UnixEpoch.AddSeconds(time))
            .Verifiable();

            var options = GetOptions();
            var service = new AuthenticatorService(options, systemTime.Object);
            var code    = service.GetCode(hashAlgorithm, Encoding.UTF8.GetBytes(secret), 8, 30);

            Assert.Equal(expectedCode, code);

            systemTime.Verify();
        }