Skip to content

Commit

Permalink
increase code coverage of DefaultCodeVerifier
Browse files Browse the repository at this point in the history
  • Loading branch information
samdjstevens committed Dec 11, 2019
1 parent 65c80eb commit d1d5a60
Showing 1 changed file with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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.*;
Expand All @@ -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);
}
Expand Down

0 comments on commit d1d5a60

Please sign in to comment.