public void testSharedCache()
      throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
          InvalidKeySpecException, KeyStoreException, CertificateException, NoSuchProviderException,
          InvalidAlgorithmParameterException, UnrecoverableEntryException, DigestException,
          IllegalBlockSizeException, BadPaddingException, IOException, NameNotFoundException,
          NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    AuthenticationSettings.INSTANCE.setSharedPrefPackageName("mockpackage");
    StorageHelper mockSecure = mock(StorageHelper.class);
    Context mockContext = mock(Context.class);
    Context packageContext = mock(Context.class);
    SharedPreferences prefs = mock(SharedPreferences.class);
    when(prefs.contains("testkey")).thenReturn(true);
    when(prefs.getString("testkey", "")).thenReturn("test_encrypted");
    when(mockSecure.decrypt("test_encrypted")).thenReturn("{\"mClientId\":\"clientId23\"}");
    when(mockContext.createPackageContext("mockpackage", Context.MODE_PRIVATE))
        .thenReturn(packageContext);
    when(packageContext.getSharedPreferences("com.microsoft.aad.adal.cache", Activity.MODE_PRIVATE))
        .thenReturn(prefs);
    Class<?> c = DefaultTokenCacheStore.class;
    Field encryptHelper = c.getDeclaredField("sHelper");
    encryptHelper.setAccessible(true);
    encryptHelper.set(null, mockSecure);
    DefaultTokenCacheStore cache = new DefaultTokenCacheStore(mockContext);
    TokenCacheItem item = cache.getItem("testkey");

    // Verify returned item
    assertEquals("Same item as mock", "clientId23", item.getClientId());
    encryptHelper.set(null, null);
  }
  public void testDateTimeFormatterOldFormat()
      throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
          InvalidKeySpecException, KeyStoreException, CertificateException, NoSuchProviderException,
          InvalidAlgorithmParameterException, UnrecoverableEntryException, DigestException,
          IllegalBlockSizeException, BadPaddingException, IOException, NameNotFoundException,
          NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    StorageHelper mockSecure = mock(StorageHelper.class);
    Context mockContext = mock(Context.class);
    SharedPreferences prefs = mock(SharedPreferences.class);
    when(prefs.contains("testkey")).thenReturn(true);
    when(prefs.getString("testkey", "")).thenReturn("test_encrypted");
    when(mockSecure.decrypt("test_encrypted"))
        .thenReturn("{\"mClientId\":\"clientId23\",\"mExpiresOn\":\"Apr 28, 2015 1:09:57 PM\"}");
    when(mockContext.getSharedPreferences("com.microsoft.aad.adal.cache", Activity.MODE_PRIVATE))
        .thenReturn(prefs);
    Class<?> c = DefaultTokenCacheStore.class;
    Field encryptHelper = c.getDeclaredField("sHelper");
    encryptHelper.setAccessible(true);
    encryptHelper.set(null, mockSecure);
    DefaultTokenCacheStore cache = new DefaultTokenCacheStore(mockContext);
    TokenCacheItem item = cache.getItem("testkey");

    // Verify returned item
    assertNotNull(item.getExpiresOn());
    assertNotNull(item.getExpiresOn().after(new Date()));
    encryptHelper.set(null, null);
  }
  public void testExpireBuffer() throws NoSuchAlgorithmException, NoSuchPaddingException {
    DefaultTokenCacheStore store = (DefaultTokenCacheStore) setupItems();

    ArrayList<TokenCacheItem> tokens = store.getTokensForUser("userid1");
    Calendar expireTime = Calendar.getInstance();
    Logger.d(TAG, "Time now: " + expireTime.getTime());
    expireTime.add(Calendar.SECOND, 240);
    Logger.d(TAG, "Time modified: " + expireTime.getTime());

    // Sets token to expire if less than this buffer
    AuthenticationSettings.INSTANCE.setExpirationBuffer(300);
    for (TokenCacheItem item : tokens) {
      item.setExpiresOn(expireTime.getTime());
      assertTrue("Should say expired", TokenCacheItem.isTokenExpired(item.getExpiresOn()));
    }

    // Set expire time ahead of buffer 240 +100 secs more than 300secs
    // buffer
    expireTime.add(Calendar.SECOND, 100);
    for (TokenCacheItem item : tokens) {
      item.setExpiresOn(expireTime.getTime());
      assertFalse(
          "Should not say expired since time is more than buffer",
          TokenCacheItem.isTokenExpired(item.getExpiresOn()));
    }
  }
  public void testExpiringTokens() throws NoSuchAlgorithmException, NoSuchPaddingException {
    DefaultTokenCacheStore store = (DefaultTokenCacheStore) setupItems();

    ArrayList<TokenCacheItem> tokens = store.getTokensForUser("userid1");
    ArrayList<TokenCacheItem> expireTokenList = store.getTokensAboutToExpire();
    assertEquals("token size", 0, expireTokenList.size());
    assertEquals("token size", 2, tokens.size());

    TokenCacheItem expire = tokens.get(0);

    Calendar timeAhead = Calendar.getInstance();
    timeAhead.add(Calendar.MINUTE, -10);
    expire.setExpiresOn(timeAhead.getTime());

    store.setItem(CacheKey.createCacheKey(expire), expire);

    expireTokenList = store.getTokensAboutToExpire();
    assertEquals("token size", 1, expireTokenList.size());
  }
  public void testDateTimeFormatterLocaleChange()
      throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
          InvalidKeySpecException, KeyStoreException, CertificateException, NoSuchProviderException,
          InvalidAlgorithmParameterException, UnrecoverableEntryException, DigestException,
          IllegalBlockSizeException, BadPaddingException, IOException, NameNotFoundException,
          NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    DefaultTokenCacheStore store = (DefaultTokenCacheStore) setupItems();
    ArrayList<TokenCacheItem> tokens = store.getTokensForResource("resource");
    // Serializing without miliseconds
    long precision = 1000;
    TokenCacheItem item = tokens.get(0);
    String cacheKey = CacheKey.createCacheKey(item);
    Calendar time = Calendar.getInstance();
    Date dateTimeNow = time.getTime();
    long timeNowMiliSeconds = dateTimeNow.getTime();
    item.setExpiresOn(dateTimeNow);
    store.setItem(cacheKey, item);
    TokenCacheItem fromCache = store.getItem(cacheKey);
    assertTrue(Math.abs(timeNowMiliSeconds - fromCache.getExpiresOn().getTime()) < precision);

    // Parse for different settings
    Locale.setDefault(Locale.FRANCE);
    fromCache = store.getItem(cacheKey);
    assertTrue(Math.abs(timeNowMiliSeconds - fromCache.getExpiresOn().getTime()) < precision);

    Locale.setDefault(Locale.US);
    fromCache = store.getItem(cacheKey);
    assertTrue(Math.abs(timeNowMiliSeconds - fromCache.getExpiresOn().getTime()) < precision);

    TimeZone.setDefault(TimeZone.getTimeZone("GMT+03:00"));
    fromCache = store.getItem(cacheKey);
    assertTrue(Math.abs(timeNowMiliSeconds - fromCache.getExpiresOn().getTime()) < precision);

    TimeZone.setDefault(TimeZone.getTimeZone("GMT+05:00"));
    fromCache = store.getItem(cacheKey);
    assertTrue(Math.abs(timeNowMiliSeconds - fromCache.getExpiresOn().getTime()) < precision);
  }