-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for refresh attestiation token 10 mins before expiry
- Loading branch information
1 parent
3b1fd3c
commit 02ee851
Showing
5 changed files
with
126 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.uid2.shared; | ||
|
||
import java.time.Instant; | ||
public interface IClock { | ||
Long getEpochSecond(); | ||
Long getEpochMillis(); | ||
Instant now(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.uid2.shared; | ||
|
||
import java.time.Instant; | ||
|
||
public class InstantClock implements IClock { | ||
@Override | ||
public Long getEpochSecond() { | ||
return Instant.now().getEpochSecond(); | ||
} | ||
|
||
@Override | ||
public Long getEpochMillis() { | ||
return Instant.now().toEpochMilli(); | ||
} | ||
|
||
@Override | ||
public Instant now() { | ||
return Instant.now(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/test/java/com/uid2/shared/attest/AttestationTokenRetrieverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.uid2.shared.attest; | ||
|
||
import com.uid2.enclave.IAttestationProvider; | ||
import com.uid2.shared.ApplicationVersion; | ||
import com.uid2.shared.IClock; | ||
import io.vertx.core.Handler; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.net.Proxy; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
public class AttestationTokenRetrieverTest { | ||
private String attestationEndpoint; | ||
private String userToken; | ||
private ApplicationVersion appVersion = new ApplicationVersion("appName", "appVersion"); | ||
private Proxy proxy; | ||
private IAttestationProvider attestationProvider; | ||
private boolean enforceHttps; | ||
private boolean allowContentFromLocalFileSystem; | ||
private AtomicReference<Handler<Integer>> responseWatcher = mock(AtomicReference.class); | ||
private IClock clock = mock(IClock.class); | ||
private static final long A_HUNDRED_DAYS_IN_MILLI = 86400000000L; | ||
|
||
private AttestationTokenRetriever attestationTokenRetriever = | ||
new AttestationTokenRetriever(attestationEndpoint, userToken, appVersion, proxy, | ||
attestationProvider, enforceHttps, allowContentFromLocalFileSystem, responseWatcher, clock); | ||
|
||
@Test | ||
public void testCurrentTimeBeforeTenMinsBeforeAttestationTokenExpiry() throws UidCoreClientException, IOException { | ||
AttestationTokenRetriever attestationTokenRetrieverSpy = spy(this.attestationTokenRetriever); | ||
|
||
Instant fakeExpiresAt = Instant.ofEpochMilli(A_HUNDRED_DAYS_IN_MILLI); | ||
when(clock.now()).thenReturn(fakeExpiresAt.minusSeconds(600).minusSeconds(100)); | ||
attestationTokenRetrieverSpy.setAttestationTokenExpiresAt(fakeExpiresAt.toString()); | ||
|
||
attestationTokenRetrieverSpy.attestationExpirationCheck(); | ||
verify(attestationTokenRetrieverSpy, times(0)).attestInternal(); | ||
} | ||
|
||
@Test | ||
public void testCurrentTimeAfterTenMinsBeforeAttestationTokenExpiry() throws UidCoreClientException, IOException { | ||
AttestationTokenRetriever attestationTokenRetrieverSpy = spy(attestationTokenRetriever); | ||
|
||
Instant fakeExpiresAt = Instant.ofEpochMilli(A_HUNDRED_DAYS_IN_MILLI); | ||
when(clock.now()).thenReturn(fakeExpiresAt.minusSeconds(600).plusSeconds(100)); | ||
attestationTokenRetrieverSpy.setAttestationTokenExpiresAt(fakeExpiresAt.toString()); | ||
|
||
attestationTokenRetrieverSpy.attestationExpirationCheck(); | ||
verify(attestationTokenRetrieverSpy, times(1)).attestInternal(); | ||
} | ||
} |