Skip to content

Fix strict mode violations for appcheck #4148

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 3 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions appcheck/firebase-appcheck/firebase-appcheck.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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. -->

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

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

<instrumentation
android:name="androidx.test.runner.AndroidJUnitRunner"
android:targetPackage="com.google.firebase.appcheck" />
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.google.firebase.appcheck;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

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);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -47,24 +48,24 @@ enum TokenType {
UNKNOWN_APP_CHECK_TOKEN
}

private SharedPreferences sharedPreferences;
private Lazy<SharedPreferences> 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
sharedPreferences.get()
.edit()
.putString(TOKEN_KEY, ((DefaultAppCheckToken) appCheckToken).serializeTokenToString())
.putString(TOKEN_TYPE_KEY, TokenType.DEFAULT_APP_CHECK_TOKEN.name())
.apply();
} else {
sharedPreferences
sharedPreferences.get()
.edit()
.putString(TOKEN_KEY, appCheckToken.getToken())
.putString(TOKEN_TYPE_KEY, TokenType.UNKNOWN_APP_CHECK_TOKEN.name())
Expand All @@ -74,8 +75,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;
}
Expand All @@ -101,6 +102,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();
}
}