Skip to content

Implementing cache for FIS SDK #694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion firebase-installations/firebase-installations.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ dependencies {
testImplementation 'junit:junit:4.12'
testImplementation "org.robolectric:robolectric:$robolectricVersion"

androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:runner:1.2.0'
implementation 'com.google.guava:guava:16.0.+'
androidTestImplementation "com.google.truth:truth:$googleTruthVersion"
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation "androidx.annotation:annotation:1.0.0"
}
26 changes: 26 additions & 0 deletions firebase-installations/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2019 Google LLC -->
<!-- -->
<!-- Licensed under the Apache License, Version 2.0 (the "License"); -->
<!-- you may not use this file except in compliance with the License. -->
<!-- You may obtain a copy of the License at -->
<!-- -->
<!-- http://www.apache.org/licenses/LICENSE-2.0 -->
<!-- -->
<!-- Unless required by applicable law or agreed to in writing, software -->
<!-- distributed under the License is distributed on an "AS IS" BASIS, -->
<!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -->
<!-- See the License for the specific language governing permissions and -->
<!-- limitations under the License. -->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.firebase.installation">

<application>
<uses-library android:name="android.test.runner"/>
</application>

<instrumentation
android:name="androidx.test.runner.AndroidJUnitRunner"
android:targetPackage="com.google.firebase.installation" />
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.installation.local;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.runner.AndroidJUnit4;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.installations.local.FiidCache;
import com.google.firebase.installations.local.FiidCacheEntryValue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

/** Instrumented tests for {@link FiidCache} */
@RunWith(AndroidJUnit4.class)
public class FiidCacheTest {

private FirebaseApp firebaseApp0;
private FirebaseApp firebaseApp1;
private FiidCache cache0;
private FiidCache cache1;
private final String AUTH_TOKEN = "auth_token";
private final String REFRESH_TOKEN = "refresh_token";

private final long TIMESTAMP_IN_SECONDS = 100L;

@Before
public void setUp() {
FirebaseApp.clearInstancesForTest();
firebaseApp0 =
FirebaseApp.initializeApp(
ApplicationProvider.getApplicationContext(),
new FirebaseOptions.Builder().setApplicationId("1:123456789:android:abcdef").build());
firebaseApp1 =
FirebaseApp.initializeApp(
ApplicationProvider.getApplicationContext(),
new FirebaseOptions.Builder().setApplicationId("1:987654321:android:abcdef").build(),
"firebase_app_1");
cache0 = new FiidCache(firebaseApp0);
cache1 = new FiidCache(firebaseApp1);
}

@After
public void cleanUp() throws Exception {
cache0.clear();
cache1.clear();
}

@Test
public void testReadCacheEntry_Null() {
assertNull(cache0.readCacheEntryValue());
assertNull(cache1.readCacheEntryValue());
}

@Test
public void testUpdateAndReadCacheEntry() throws Exception {
assertTrue(
cache0.insertOrUpdateCacheEntry(
FiidCacheEntryValue.create(
"123456",
FiidCache.CacheStatus.UNREGISTERED,
AUTH_TOKEN,
REFRESH_TOKEN,
TIMESTAMP_IN_SECONDS,
TIMESTAMP_IN_SECONDS)));
FiidCacheEntryValue entryValue = cache0.readCacheEntryValue();
assertThat(entryValue.getFirebaseInstallationId()).isEqualTo("123456");
assertThat(entryValue.getAuthToken()).isEqualTo(AUTH_TOKEN);
assertThat(entryValue.getRefreshToken()).isEqualTo(REFRESH_TOKEN);
assertThat(entryValue.getCacheStatus()).isEqualTo(FiidCache.CacheStatus.UNREGISTERED);
assertThat(entryValue.getExpiresInSecs()).isEqualTo(TIMESTAMP_IN_SECONDS);
assertThat(entryValue.getTokenCreationEpochInSecs()).isEqualTo(TIMESTAMP_IN_SECONDS);
assertNull(cache1.readCacheEntryValue());

assertTrue(
cache0.insertOrUpdateCacheEntry(
FiidCacheEntryValue.create(
"123456",
FiidCache.CacheStatus.REGISTERED,
AUTH_TOKEN,
REFRESH_TOKEN,
200L,
TIMESTAMP_IN_SECONDS)));
entryValue = cache0.readCacheEntryValue();
assertThat(entryValue.getFirebaseInstallationId()).isEqualTo("123456");
assertThat(entryValue.getAuthToken()).isEqualTo(AUTH_TOKEN);
assertThat(entryValue.getRefreshToken()).isEqualTo(REFRESH_TOKEN);
assertThat(entryValue.getCacheStatus()).isEqualTo(FiidCache.CacheStatus.REGISTERED);
assertThat(entryValue.getExpiresInSecs()).isEqualTo(TIMESTAMP_IN_SECONDS);
assertThat(entryValue.getTokenCreationEpochInSecs()).isEqualTo(200L);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
package com.google.firebase.installations;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import com.google.android.gms.common.internal.Preconditions;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
import com.google.common.annotations.VisibleForTesting;
import com.google.firebase.FirebaseApp;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.installations.local;

import android.content.Context;
import android.content.SharedPreferences;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.firebase.FirebaseApp;

/**
* A layer that locally caches a few Firebase Installation attributes on top the Firebase
* Installation backend API.
*/
public class FiidCache {
// Status of each cache entry
// NOTE: never change the ordinal of the enum values because the enum values are stored in cache
// as their ordinal numbers.
public enum CacheStatus {
// Cache entry is synced to Firebase backend
REGISTERED,
// Cache entry is waiting for Firebase backend response or internal network retry
UNREGISTERED,
// Cache entry is in error state when syncing with Firebase backend
REGISTER_ERROR,
// Cache entry is in delete state before syncing with Firebase backend
DELETED
}

private static final String SHARED_PREFS_NAME = "FiidCache";

private static final String FIREBASE_INSTALLATION_ID_KEY = "Fid";
private static final String AUTH_TOKEN_KEY = "AuthToken";
private static final String REFRESH_TOKEN_KEY = "RefreshToken";
private static final String TOKEN_CREATION_TIME_IN_SECONDS_KEY = "TokenCreationEpochInSecs";
private static final String EXPIRES_IN_SECONDS_KEY = "ExpiresInSecs";
private static final String CACHE_STATUS_KEY = "Status";

private final SharedPreferences prefs;
private final String persistenceKey;

public FiidCache(@NonNull FirebaseApp firebaseApp) {
// Different FirebaseApp in the same Android application should have the same application
// context and same dir path
prefs =
firebaseApp
.getApplicationContext()
.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
persistenceKey = firebaseApp.getPersistenceKey();
}

@Nullable
public synchronized FiidCacheEntryValue readCacheEntryValue() {
String iid = prefs.getString(getSharedPreferencesKey(FIREBASE_INSTALLATION_ID_KEY), null);
int status = prefs.getInt(getSharedPreferencesKey(CACHE_STATUS_KEY), -1);
String authToken = prefs.getString(getSharedPreferencesKey(AUTH_TOKEN_KEY), null);
String refreshToken = prefs.getString(getSharedPreferencesKey(REFRESH_TOKEN_KEY), null);
long tokenCreationTime =
prefs.getLong(getSharedPreferencesKey(TOKEN_CREATION_TIME_IN_SECONDS_KEY), 0);
long expiresIn = prefs.getLong(getSharedPreferencesKey(EXPIRES_IN_SECONDS_KEY), 0);

if (iid == null || status == -1) {
return null;
}

return FiidCacheEntryValue.create(
iid, CacheStatus.values()[status], authToken, refreshToken, tokenCreationTime, expiresIn);
}

@NonNull
public synchronized boolean insertOrUpdateCacheEntry(@NonNull FiidCacheEntryValue entryValue) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString(
getSharedPreferencesKey(FIREBASE_INSTALLATION_ID_KEY),
entryValue.getFirebaseInstallationId());
editor.putInt(getSharedPreferencesKey(CACHE_STATUS_KEY), entryValue.getCacheStatus().ordinal());
editor.putString(getSharedPreferencesKey(AUTH_TOKEN_KEY), entryValue.getAuthToken());
editor.putString(getSharedPreferencesKey(REFRESH_TOKEN_KEY), entryValue.getRefreshToken());
editor.putLong(
getSharedPreferencesKey(TOKEN_CREATION_TIME_IN_SECONDS_KEY),
entryValue.getTokenCreationEpochInSecs());
editor.putLong(getSharedPreferencesKey(EXPIRES_IN_SECONDS_KEY), entryValue.getExpiresInSecs());
return editor.commit();
}

@NonNull
public synchronized boolean clear() {
SharedPreferences.Editor editor = prefs.edit();
editor.remove(getSharedPreferencesKey(FIREBASE_INSTALLATION_ID_KEY));
editor.remove(getSharedPreferencesKey(CACHE_STATUS_KEY));
editor.remove(getSharedPreferencesKey(AUTH_TOKEN_KEY));
editor.remove(getSharedPreferencesKey(REFRESH_TOKEN_KEY));
editor.remove(getSharedPreferencesKey(TOKEN_CREATION_TIME_IN_SECONDS_KEY));
editor.remove(getSharedPreferencesKey(EXPIRES_IN_SECONDS_KEY));
return editor.commit();
}

private String getSharedPreferencesKey(String key) {
return String.format("%s|%s", persistenceKey, key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.installations.local;

import androidx.annotation.NonNull;
import com.google.auto.value.AutoValue;

/**
* This class represents a cache entry value in {@link FiidCache}, which contains a Firebase
* instance id and the cache status of this entry.
*/
@AutoValue
public abstract class FiidCacheEntryValue {

@NonNull
public abstract String getFirebaseInstallationId();

@NonNull
public abstract FiidCache.CacheStatus getCacheStatus();

@NonNull
public abstract String getAuthToken();

@NonNull
public abstract String getRefreshToken();

public abstract long getExpiresInSecs();

public abstract long getTokenCreationEpochInSecs();

@NonNull
public static FiidCacheEntryValue create(
@NonNull String firebaseInstallationId,
@NonNull FiidCache.CacheStatus cacheStatus,
@NonNull String authToken,
@NonNull String refreshToken,
long tokenCreationEpochInSecs,
long expiresInSecs) {
return new AutoValue_FiidCacheEntryValue(
firebaseInstallationId,
cacheStatus,
authToken,
refreshToken,
expiresInSecs,
tokenCreationEpochInSecs);
}
}