Use java.time classes for stored verification code expiration; add tests.

This commit is contained in:
Jon Chambers
2021-06-18 11:50:28 -04:00
committed by Jon Chambers
parent ce3fb7fa99
commit 111f5ba024
2 changed files with 48 additions and 2 deletions

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2013-2021 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.textsecuregcm.auth;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.time.Duration;
import java.time.Instant;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.*;
class StoredVerificationCodeTest {
@ParameterizedTest
@MethodSource
void isValid(final StoredVerificationCode storedVerificationCode, final String code, final Instant currentTime, final boolean expectValid) {
assertEquals(expectValid, storedVerificationCode.isValid(code, currentTime));
}
private static Stream<Arguments> isValid() {
final Instant now = Instant.now();
return Stream.of(
Arguments.of(new StoredVerificationCode("code", now.toEpochMilli(), null, null), "code", now, true),
Arguments.of(new StoredVerificationCode("code", now.toEpochMilli(), null, null), "incorrect", now, false),
Arguments.of(new StoredVerificationCode("code", now.toEpochMilli(), null, null), "code", now.plus(Duration.ofHours(1)), false),
Arguments.of(new StoredVerificationCode("", now.toEpochMilli(), null, null), "", now, false)
);
}
}