Skip to content

Commit e285f26

Browse files
authored
Merge a9ab58b into b2cbd70
2 parents b2cbd70 + a9ab58b commit e285f26

File tree

4 files changed

+94
-5
lines changed

4 files changed

+94
-5
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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.appcheck;
16+
17+
import androidx.test.core.app.ApplicationProvider;
18+
import androidx.test.ext.junit.runners.AndroidJUnit4;
19+
import com.google.firebase.FirebaseApp;
20+
import com.google.firebase.FirebaseOptions;
21+
import com.google.firebase.appcheck.interop.InternalAppCheckTokenProvider;
22+
import com.google.firebase.testing.integ.StrictModeRule;
23+
import org.junit.Rule;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
27+
@RunWith(AndroidJUnit4.class)
28+
public class StrictModeTest {
29+
30+
@Rule public StrictModeRule strictMode = new StrictModeRule();
31+
32+
@Test
33+
public void initializingFirebaseAppcheck_shouldNotViolateStrictMode() {
34+
strictMode.runOnMainThread(
35+
() -> {
36+
FirebaseApp app =
37+
FirebaseApp.initializeApp(
38+
ApplicationProvider.getApplicationContext(),
39+
new FirebaseOptions.Builder()
40+
.setApiKey("api")
41+
.setProjectId("123")
42+
.setApplicationId("appId")
43+
.build(),
44+
"hello");
45+
app.get(FirebaseAppCheck.class);
46+
app.get(InternalAppCheckTokenProvider.class);
47+
});
48+
}
49+
}

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

Lines changed: 9 additions & 5 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,27 @@ 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 =
58+
new Lazy(() -> context.getSharedPreferences(prefsName, Context.MODE_PRIVATE));
5759
}
5860

5961
public void saveAppCheckToken(@NonNull AppCheckToken appCheckToken) {
6062
if (appCheckToken instanceof DefaultAppCheckToken) {
6163
sharedPreferences
64+
.get()
6265
.edit()
6366
.putString(TOKEN_KEY, ((DefaultAppCheckToken) appCheckToken).serializeTokenToString())
6467
.putString(TOKEN_TYPE_KEY, TokenType.DEFAULT_APP_CHECK_TOKEN.name())
6568
.apply();
6669
} else {
6770
sharedPreferences
71+
.get()
6872
.edit()
6973
.putString(TOKEN_KEY, appCheckToken.getToken())
7074
.putString(TOKEN_TYPE_KEY, TokenType.UNKNOWN_APP_CHECK_TOKEN.name())
@@ -74,8 +78,8 @@ public void saveAppCheckToken(@NonNull AppCheckToken appCheckToken) {
7478

7579
@Nullable
7680
public AppCheckToken retrieveAppCheckToken() {
77-
String tokenType = sharedPreferences.getString(TOKEN_TYPE_KEY, null);
78-
String serializedToken = sharedPreferences.getString(TOKEN_KEY, null);
81+
String tokenType = sharedPreferences.get().getString(TOKEN_TYPE_KEY, null);
82+
String serializedToken = sharedPreferences.get().getString(TOKEN_KEY, null);
7983
if (tokenType == null || serializedToken == null) {
8084
return null;
8185
}
@@ -101,6 +105,6 @@ public AppCheckToken retrieveAppCheckToken() {
101105
}
102106

103107
void clearSharedPrefs() {
104-
sharedPreferences.edit().remove(TOKEN_KEY).remove(TOKEN_TYPE_KEY).apply();
108+
sharedPreferences.get().edit().remove(TOKEN_KEY).remove(TOKEN_TYPE_KEY).apply();
105109
}
106110
}

0 commit comments

Comments
 (0)