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); }