From d1d5a609907979bbb6d74b3d607ca3c58a16f33a Mon Sep 17 00:00:00 2001 From: Sam Stevens Date: Wed, 11 Dec 2019 23:42:21 +0000 Subject: [PATCH] increase code coverage of DefaultCodeVerifier --- .../totp/code/DefaultCodeVerifierTest.java | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/test/java/dev/samstevens/totp/code/DefaultCodeVerifierTest.java b/src/test/java/dev/samstevens/totp/code/DefaultCodeVerifierTest.java index ae71e99..b7e036f 100644 --- a/src/test/java/dev/samstevens/totp/code/DefaultCodeVerifierTest.java +++ b/src/test/java/dev/samstevens/totp/code/DefaultCodeVerifierTest.java @@ -1,5 +1,6 @@ package dev.samstevens.totp.code; +import dev.samstevens.totp.exceptions.CodeGenerationException; import dev.samstevens.totp.time.TimeProvider; import org.junit.Test; import static org.junit.Assert.*; @@ -12,21 +13,45 @@ public void testCodeIsValid() { String secret = "EX47GINFPBK5GNLYLILGD2H6ZLGJNNWB"; long timeToRunAt = 1567975936; String correctCode = "862707"; + int timePeriod = 30; // allow for a -/+ ~30 second discrepancy - assertTrue(isValidCode(secret, correctCode, timeToRunAt - 30)); - assertTrue(isValidCode(secret, correctCode, timeToRunAt)); - assertTrue(isValidCode(secret, correctCode, timeToRunAt + 30)); + assertTrue(isValidCode(secret, correctCode, timeToRunAt - timePeriod, timePeriod)); + assertTrue(isValidCode(secret, correctCode, timeToRunAt, timePeriod)); + assertTrue(isValidCode(secret, correctCode, timeToRunAt + timePeriod, timePeriod)); // but no more - assertFalse(isValidCode(secret, correctCode, timeToRunAt + 45)); + assertFalse(isValidCode(secret, correctCode, timeToRunAt + timePeriod + 15, timePeriod)); + + // test wrong code fails + assertFalse(isValidCode(secret, "123", timeToRunAt, timePeriod)); + } + + @Test + public void testCodeGenerationFailureReturnsFalse() throws CodeGenerationException { + + String secret = "EX47GINFPBK5GNLYLILGD2H6ZLGJNNWB"; +// long timeToRunAt = 1567975936; +// String correctCode = "862707"; + + TimeProvider timeProvider = mock(TimeProvider.class); + when(timeProvider.getTime()).thenReturn(1567975936L); + + CodeGenerator codeGenerator = mock(CodeGenerator.class); + when(codeGenerator.generate(anyString(), anyLong())).thenThrow(new CodeGenerationException("Test", new RuntimeException())); + + CodeVerifier verifier = new DefaultCodeVerifier(codeGenerator, timeProvider); + + + assertEquals(false, verifier.isValidCode(secret, "1234")); } - private boolean isValidCode(String secret, String code, long time) { + private boolean isValidCode(String secret, String code, long time, int timePeriod) { TimeProvider timeProvider = mock(TimeProvider.class); when(timeProvider.getTime()).thenReturn(time); - CodeVerifier verifier = new DefaultCodeVerifier(new DefaultCodeGenerator(), timeProvider); + DefaultCodeVerifier verifier = new DefaultCodeVerifier(new DefaultCodeGenerator(), timeProvider); + verifier.setTimePeriod(timePeriod); return verifier.isValidCode(secret, code); }