Skip to content

Commit 38fbc57

Browse files
committed
Fix strict mode violations for appcheck
1 parent 1d344e8 commit 38fbc57

File tree

4 files changed

+81
-7
lines changed

4 files changed

+81
-7
lines changed

appcheck/firebase-appcheck/firebase-appcheck.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,14 @@ dependencies {
5656
testImplementation "com.google.truth:truth:$googleTruthVersion"
5757
testImplementation 'androidx.test:core:1.2.0'
5858
testImplementation 'androidx.test:rules:1.2.0'
59+
60+
androidTestImplementation project(':appcheck:firebase-appcheck')
61+
androidTestImplementation project(':integ-testing')
62+
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
63+
androidTestImplementation 'androidx.test:runner:1.2.0'
64+
androidTestImplementation "com.google.truth:truth:$googleTruthVersion"
65+
androidTestImplementation 'junit:junit:4.12'
66+
androidTestImplementation "androidx.annotation:annotation:1.0.0"
67+
androidTestImplementation 'org.mockito:mockito-core:2.25.0'
68+
androidTestImplementation 'org.mockito:mockito-inline:2.25.0'
5969
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Copyright 2022 Google LLC -->
3+
<!-- -->
4+
<!-- Licensed under the Apache License, Version 2.0 (the "License"); -->
5+
<!-- you may not use this file except in compliance with the License. -->
6+
<!-- You may obtain a copy of the License at -->
7+
<!-- -->
8+
<!-- http://www.apache.org/licenses/LICENSE-2.0 -->
9+
<!-- -->
10+
<!-- Unless required by applicable law or agreed to in writing, software -->
11+
<!-- distributed under the License is distributed on an "AS IS" BASIS, -->
12+
<!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -->
13+
<!-- See the License for the specific language governing permissions and -->
14+
<!-- limitations under the License. -->
15+
16+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
17+
package="com.google.firebase.appcheck">
18+
19+
<application>
20+
<uses-library android:name="android.test.runner"/>
21+
</application>
22+
23+
<instrumentation
24+
android:name="androidx.test.runner.AndroidJUnitRunner"
25+
android:targetPackage="com.google.firebase.appcheck" />
26+
</manifest>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.google.firebase.appcheck;
2+
3+
import androidx.test.core.app.ApplicationProvider;
4+
import androidx.test.ext.junit.runners.AndroidJUnit4;
5+
import androidx.test.platform.app.InstrumentationRegistry;
6+
7+
import com.google.firebase.FirebaseApp;
8+
import com.google.firebase.FirebaseOptions;
9+
import com.google.firebase.appcheck.interop.InternalAppCheckTokenProvider;
10+
import com.google.firebase.testing.integ.StrictModeRule;
11+
import org.junit.Rule;
12+
import org.junit.Test;
13+
import org.junit.runner.RunWith;
14+
15+
@RunWith(AndroidJUnit4.class)
16+
public class StrictModeTest {
17+
18+
@Rule public StrictModeRule strictMode = new StrictModeRule();
19+
20+
@Test
21+
public void initializingFirebaseAppcheck_shouldNotViolateStrictMode() {
22+
strictMode.runOnMainThread(
23+
() -> {
24+
FirebaseApp app =
25+
FirebaseApp.initializeApp(
26+
ApplicationProvider.getApplicationContext(),
27+
new FirebaseOptions.Builder()
28+
.setApiKey("api")
29+
.setProjectId("123")
30+
.setApplicationId("appId")
31+
.build(),
32+
"hello");
33+
app.get(FirebaseAppCheck.class);
34+
app.get(InternalAppCheckTokenProvider.class);
35+
});
36+
}
37+
}

appcheck/firebase-appcheck/src/main/java/com/google/firebase/appcheck/internal/StorageHelper.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import androidx.annotation.VisibleForTesting;
2525
import com.google.firebase.appcheck.AppCheckToken;
2626
import com.google.firebase.appcheck.internal.util.Logger;
27+
import com.google.firebase.components.Lazy;
2728

2829
/**
2930
* Internal class used to persist {@link com.google.firebase.appcheck.AppCheckToken}s. Uses {@link
@@ -47,24 +48,24 @@ enum TokenType {
4748
UNKNOWN_APP_CHECK_TOKEN
4849
}
4950

50-
private SharedPreferences sharedPreferences;
51+
private Lazy<SharedPreferences> sharedPreferences;
5152

5253
public StorageHelper(@NonNull Context context, @NonNull String persistenceKey) {
5354
checkNotNull(context);
5455
checkNotEmpty(persistenceKey);
5556
String prefsName = String.format(PREFS_TEMPLATE, persistenceKey);
56-
this.sharedPreferences = context.getSharedPreferences(prefsName, Context.MODE_PRIVATE);
57+
this.sharedPreferences = new Lazy(() -> context.getSharedPreferences(prefsName, Context.MODE_PRIVATE));
5758
}
5859

5960
public void saveAppCheckToken(@NonNull AppCheckToken appCheckToken) {
6061
if (appCheckToken instanceof DefaultAppCheckToken) {
61-
sharedPreferences
62+
sharedPreferences.get()
6263
.edit()
6364
.putString(TOKEN_KEY, ((DefaultAppCheckToken) appCheckToken).serializeTokenToString())
6465
.putString(TOKEN_TYPE_KEY, TokenType.DEFAULT_APP_CHECK_TOKEN.name())
6566
.apply();
6667
} else {
67-
sharedPreferences
68+
sharedPreferences.get()
6869
.edit()
6970
.putString(TOKEN_KEY, appCheckToken.getToken())
7071
.putString(TOKEN_TYPE_KEY, TokenType.UNKNOWN_APP_CHECK_TOKEN.name())
@@ -74,8 +75,8 @@ public void saveAppCheckToken(@NonNull AppCheckToken appCheckToken) {
7475

7576
@Nullable
7677
public AppCheckToken retrieveAppCheckToken() {
77-
String tokenType = sharedPreferences.getString(TOKEN_TYPE_KEY, null);
78-
String serializedToken = sharedPreferences.getString(TOKEN_KEY, null);
78+
String tokenType = sharedPreferences.get().getString(TOKEN_TYPE_KEY, null);
79+
String serializedToken = sharedPreferences.get().getString(TOKEN_KEY, null);
7980
if (tokenType == null || serializedToken == null) {
8081
return null;
8182
}
@@ -101,6 +102,6 @@ public AppCheckToken retrieveAppCheckToken() {
101102
}
102103

103104
void clearSharedPrefs() {
104-
sharedPreferences.edit().remove(TOKEN_KEY).remove(TOKEN_TYPE_KEY).apply();
105+
sharedPreferences.get().edit().remove(TOKEN_KEY).remove(TOKEN_TYPE_KEY).apply();
105106
}
106107
}

0 commit comments

Comments
 (0)