diff --git a/appcheck/firebase-appcheck/firebase-appcheck.gradle b/appcheck/firebase-appcheck/firebase-appcheck.gradle index 3f04fff97ad..6ed2202dd30 100644 --- a/appcheck/firebase-appcheck/firebase-appcheck.gradle +++ b/appcheck/firebase-appcheck/firebase-appcheck.gradle @@ -56,4 +56,14 @@ dependencies { testImplementation "com.google.truth:truth:$googleTruthVersion" testImplementation 'androidx.test:core:1.2.0' testImplementation 'androidx.test:rules:1.2.0' + + androidTestImplementation project(':appcheck:firebase-appcheck') + androidTestImplementation project(':integ-testing') + androidTestImplementation 'androidx.test.ext:junit:1.1.1' + androidTestImplementation 'androidx.test:runner:1.2.0' + androidTestImplementation "com.google.truth:truth:$googleTruthVersion" + androidTestImplementation 'junit:junit:4.12' + androidTestImplementation "androidx.annotation:annotation:1.0.0" + androidTestImplementation 'org.mockito:mockito-core:2.25.0' + androidTestImplementation 'org.mockito:mockito-inline:2.25.0' } diff --git a/appcheck/firebase-appcheck/src/androidTest/java/AndroidManifest.xml b/appcheck/firebase-appcheck/src/androidTest/java/AndroidManifest.xml new file mode 100644 index 00000000000..688d908c063 --- /dev/null +++ b/appcheck/firebase-appcheck/src/androidTest/java/AndroidManifest.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/appcheck/firebase-appcheck/src/androidTest/java/com/google/firebase/appcheck/StrictModeTest.java b/appcheck/firebase-appcheck/src/androidTest/java/com/google/firebase/appcheck/StrictModeTest.java new file mode 100644 index 00000000000..adacf695279 --- /dev/null +++ b/appcheck/firebase-appcheck/src/androidTest/java/com/google/firebase/appcheck/StrictModeTest.java @@ -0,0 +1,49 @@ +// Copyright 2022 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.appcheck; + +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import com.google.firebase.FirebaseApp; +import com.google.firebase.FirebaseOptions; +import com.google.firebase.appcheck.interop.InternalAppCheckTokenProvider; +import com.google.firebase.testing.integ.StrictModeRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +public class StrictModeTest { + + @Rule public StrictModeRule strictMode = new StrictModeRule(); + + @Test + public void initializingFirebaseAppcheck_shouldNotViolateStrictMode() { + strictMode.runOnMainThread( + () -> { + FirebaseApp app = + FirebaseApp.initializeApp( + ApplicationProvider.getApplicationContext(), + new FirebaseOptions.Builder() + .setApiKey("api") + .setProjectId("123") + .setApplicationId("appId") + .build(), + "hello"); + app.get(FirebaseAppCheck.class); + app.get(InternalAppCheckTokenProvider.class); + }); + } +} diff --git a/appcheck/firebase-appcheck/src/main/java/com/google/firebase/appcheck/internal/StorageHelper.java b/appcheck/firebase-appcheck/src/main/java/com/google/firebase/appcheck/internal/StorageHelper.java index 5537b636cf9..ae821de0d39 100644 --- a/appcheck/firebase-appcheck/src/main/java/com/google/firebase/appcheck/internal/StorageHelper.java +++ b/appcheck/firebase-appcheck/src/main/java/com/google/firebase/appcheck/internal/StorageHelper.java @@ -24,6 +24,7 @@ import androidx.annotation.VisibleForTesting; import com.google.firebase.appcheck.AppCheckToken; import com.google.firebase.appcheck.internal.util.Logger; +import com.google.firebase.components.Lazy; /** * Internal class used to persist {@link com.google.firebase.appcheck.AppCheckToken}s. Uses {@link @@ -47,24 +48,27 @@ enum TokenType { UNKNOWN_APP_CHECK_TOKEN } - private SharedPreferences sharedPreferences; + private Lazy sharedPreferences; public StorageHelper(@NonNull Context context, @NonNull String persistenceKey) { checkNotNull(context); checkNotEmpty(persistenceKey); String prefsName = String.format(PREFS_TEMPLATE, persistenceKey); - this.sharedPreferences = context.getSharedPreferences(prefsName, Context.MODE_PRIVATE); + this.sharedPreferences = + new Lazy(() -> context.getSharedPreferences(prefsName, Context.MODE_PRIVATE)); } public void saveAppCheckToken(@NonNull AppCheckToken appCheckToken) { if (appCheckToken instanceof DefaultAppCheckToken) { sharedPreferences + .get() .edit() .putString(TOKEN_KEY, ((DefaultAppCheckToken) appCheckToken).serializeTokenToString()) .putString(TOKEN_TYPE_KEY, TokenType.DEFAULT_APP_CHECK_TOKEN.name()) .apply(); } else { sharedPreferences + .get() .edit() .putString(TOKEN_KEY, appCheckToken.getToken()) .putString(TOKEN_TYPE_KEY, TokenType.UNKNOWN_APP_CHECK_TOKEN.name()) @@ -74,8 +78,8 @@ public void saveAppCheckToken(@NonNull AppCheckToken appCheckToken) { @Nullable public AppCheckToken retrieveAppCheckToken() { - String tokenType = sharedPreferences.getString(TOKEN_TYPE_KEY, null); - String serializedToken = sharedPreferences.getString(TOKEN_KEY, null); + String tokenType = sharedPreferences.get().getString(TOKEN_TYPE_KEY, null); + String serializedToken = sharedPreferences.get().getString(TOKEN_KEY, null); if (tokenType == null || serializedToken == null) { return null; } @@ -101,6 +105,6 @@ public AppCheckToken retrieveAppCheckToken() { } void clearSharedPrefs() { - sharedPreferences.edit().remove(TOKEN_KEY).remove(TOKEN_TYPE_KEY).apply(); + sharedPreferences.get().edit().remove(TOKEN_KEY).remove(TOKEN_TYPE_KEY).apply(); } }