@Test public void storeTickets() { assertNull(TicketCache.getTicket("key1")); TicketCache.putTicket("key1", "1"); assertEquals("1", TicketCache.getTicket("key1")); TicketCache.putTicket("key1", "2"); assertEquals("2", TicketCache.getTicket("key1")); TicketCache.putTicket("key2", "1"); assertEquals("2", TicketCache.getTicket("key1")); assertEquals("1", TicketCache.getTicket("key2")); } }
@Test public void coverInit() { new TicketCache(); }
@Before public void setUp() { TicketCache.clear(); }
@Test public void acquireTokenAsyncInvalidToken() { AuthenticationProvider.TokenProvider tokenProvider = mock(AuthenticationProvider.TokenProvider.class); ArgumentCaptor<AuthenticationProvider.AuthenticationCallback> callback = ArgumentCaptor.forClass(AuthenticationProvider.AuthenticationCallback.class); doNothing().when(tokenProvider).acquireToken(anyString(), callback.capture()); AuthenticationProvider authenticationProvider = new AuthenticationProvider(MSA_COMPACT, "key", tokenProvider); /* Verify acquireTokenAsync. */ authenticationProvider.acquireTokenAsync(); verify(tokenProvider).acquireToken(eq("key"), any(AuthenticationProvider.AuthenticationCallback.class)); /* When callback parameters are invalid, don't update cache. */ callback.getValue().onAuthenticationResult(null, new Date()); verifyStatic(never()); TicketCache.putTicket(anyString(), anyString()); /* Ignore calling callback more than once, even if parameters are valid the second time. */ long freshDate = System.currentTimeMillis() + 15 * 60 * 1000; callback.getValue().onAuthenticationResult("test", new Date(freshDate)); verifyStatic(never()); TicketCache.putTicket(eq(authenticationProvider.getTicketKeyHash()), eq("p:test")); }
if (ticketKeys != null) { for (String ticketKey : ticketKeys) { String token = TicketCache.getTicket(ticketKey); if (token != null) { try {
@Test public void acquireTokenAsyncInvalidDate() { AuthenticationProvider.TokenProvider tokenProvider = mock(AuthenticationProvider.TokenProvider.class); ArgumentCaptor<AuthenticationProvider.AuthenticationCallback> callback = ArgumentCaptor.forClass(AuthenticationProvider.AuthenticationCallback.class); doNothing().when(tokenProvider).acquireToken(anyString(), callback.capture()); AuthenticationProvider authenticationProvider = new AuthenticationProvider(MSA_COMPACT, "key", tokenProvider); /* Verify acquireTokenAsync. */ authenticationProvider.acquireTokenAsync(); verify(tokenProvider).acquireToken(eq("key"), any(AuthenticationProvider.AuthenticationCallback.class)); /* When callback parameters are invalid, don't update cache. */ callback.getValue().onAuthenticationResult("test", null); verifyStatic(never()); TicketCache.putTicket(anyString(), anyString()); /* Ignore calling callback more than once, even if parameters are valid the second time. */ long freshDate = System.currentTimeMillis() + 15 * 60 * 1000; callback.getValue().onAuthenticationResult("test", new Date(freshDate)); verifyStatic(never()); TicketCache.putTicket(eq(authenticationProvider.getTicketKeyHash()), eq("p:test")); }
@Test public void acquireTokenAsyncValid() { AuthenticationProvider.TokenProvider tokenProvider = mock(AuthenticationProvider.TokenProvider.class); ArgumentCaptor<AuthenticationProvider.AuthenticationCallback> callback = ArgumentCaptor.forClass(AuthenticationProvider.AuthenticationCallback.class); doNothing().when(tokenProvider).acquireToken(anyString(), callback.capture()); AuthenticationProvider authenticationProvider = new AuthenticationProvider(MSA_COMPACT, "key", tokenProvider); /* Verify acquireTokenAsync. */ authenticationProvider.acquireTokenAsync(); verify(tokenProvider).acquireToken(eq("key"), any(AuthenticationProvider.AuthenticationCallback.class)); /* When callback parameters are valid update cache. */ long freshDate = System.currentTimeMillis() + 15 * 60 * 1000; callback.getValue().onAuthenticationResult("test", new Date(freshDate)); verifyStatic(); TicketCache.putTicket(eq(authenticationProvider.getTicketKeyHash()), eq("p:test")); /* Duplicate calls are ignored. */ callback.getValue().onAuthenticationResult("test2", new Date(freshDate)); verifyStatic(never()); TicketCache.putTicket(eq(authenticationProvider.getTicketKeyHash()), eq("p:test2")); }
@After public void tearDown() { TicketCache.clear(); }
/** * Handle token callback update. * * @param token token value. * @param expiryDate token expiry date. * @param callback authentication callback. */ private synchronized void handleTokenUpdate(String token, Date expiryDate, AuthenticationCallback callback) { /* Prevent multiple calls. */ if (mCallback != callback) { AppCenterLog.debug(LOG_TAG, "Ignore duplicate authentication callback calls, provider=" + mType); return; } /* Clear callback state. */ mCallback = null; /* Check parameters. */ AppCenterLog.debug(LOG_TAG, "Got result back from token provider=" + mType); if (token == null) { AppCenterLog.error(LOG_TAG, "Authentication failed for ticketKey=" + mTicketKey); return; } if (expiryDate == null) { AppCenterLog.error(LOG_TAG, "No expiry date provided for ticketKey=" + mTicketKey); return; } /* Update shared cache. */ TicketCache.putTicket(mTicketKeyHash, mType.mTokenPrefix + token); /* Keep track of safe expiry time. */ mExpiryDate = expiryDate; }
@Before public void setUp() throws Exception { TicketCache.clear(); spy(HttpUtils.class); doReturn(mHttpClient).when(HttpUtils.class, "createHttpClient", any(Context.class)); /* Test JSONObject implementation. */ JSONObject json = mock(JSONObject.class); whenNew(JSONObject.class).withAnyArguments().thenReturn(json); final ArgumentCaptor<String> key = ArgumentCaptor.forClass(String.class); final ArgumentCaptor<String> value = ArgumentCaptor.forClass(String.class); when(json.put(key.capture(), value.capture())).thenReturn(json); when(json.length()).thenAnswer(new Answer<Integer>() { @Override public Integer answer(InvocationOnMock invocation) { return key.getAllValues().size(); } }); when(json.toString()).thenAnswer(new Answer<String>() { @Override public String answer(InvocationOnMock invocation) { int length = key.getAllValues().size(); String[] pairs = new String[length]; for (int i = 0; i < length; i++) { pairs[i] = String.format("\"%s\":\"%s\"", key.getAllValues().get(i), value.getAllValues().get(i)); } return String.format("{%s}", String.join(",", pairs)); } }); }
callback.getValue().onAuthenticationResult("test", expiryDate); verifyStatic(); TicketCache.putTicket(eq(authenticationProvider.getTicketKeyHash()), eq("d:test")); TicketCache.putTicket(eq(authenticationProvider.getTicketKeyHash()), eq("d:test")); callback.getValue().onAuthenticationResult("test", expiryDate); verifyStatic(times(3)); TicketCache.putTicket(eq(authenticationProvider.getTicketKeyHash()), eq("d:test"));
add("key1"); }}; TicketCache.putTicket("key1", "value1"); Extensions ext1 = new Extensions() {{ setProtocol(new ProtocolExtension() {{
add(null); }}; TicketCache.putTicket("key2", "value2"); Extensions ext1 = new Extensions() {{ setProtocol(new ProtocolExtension() {{