Skip to content

Commit 72e7ffd

Browse files
authored
Fixed issue where Storage reported the wrong SDK version (#5353)
1 parent 5e974c4 commit 72e7ffd

File tree

5 files changed

+47
-59
lines changed

5 files changed

+47
-59
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@ To run formatting on your entire project you can run
255255
./gradlew :<firebase-project>:googleJavaFormat
256256
```
257257

258+
To auto-format, just run
259+
```bash
260+
./gradlew :<firebase-project>:gJf
261+
```
262+
258263
#### Kotlin
259264

260265
Kotlin code in this repo is formatted with the `ktfmt` tool. You can enable

firebase-storage/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Unreleased
2+
* [fixed] Fixed an issue where the wrong SDK version was being reported to the backend.
23

34
# 20.2.1
45
* [changed] Migrated `firebase-storage` SDK to use standard Firebase executors.

firebase-storage/src/androidTest/java/com/google/firebase/storage/IntegrationTest.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
import static com.google.common.truth.Truth.assertThat;
1919

2020
import android.net.Uri;
21-
import android.os.Environment;
2221
import androidx.annotation.NonNull;
23-
import androidx.test.InstrumentationRegistry;
2422
import androidx.test.rule.GrantPermissionRule;
2523
import androidx.test.runner.AndroidJUnit4;
2624
import com.google.android.gms.tasks.Task;
@@ -40,23 +38,23 @@
4038
/** Integration tests for {@link FirebaseStorage}. */
4139
@RunWith(AndroidJUnit4.class)
4240
public class IntegrationTest {
41+
// The file size in bytes of "1.1mb.dat"
42+
private static final int LARGE_FILE_SIZE_BYTES = 10 * 1024;
43+
private final String randomPrefix = UUID.randomUUID().toString();
44+
private final String unicodePrefix = "prefix/\\%:😊 ";
45+
4346
@Rule
4447
public GrantPermissionRule grantPermissionRule =
4548
GrantPermissionRule.grant(WRITE_EXTERNAL_STORAGE);
4649

47-
// The file size in bytes of "1.1mb.dat"
48-
private static final int LARGE_FILE_SIZE_BYTES = 10 * 1024;
49-
5050
private FirebaseStorage storageClient;
5151

52-
private final String randomPrefix = UUID.randomUUID().toString();
53-
54-
private final String unicodePrefix = "prefix/\\%:😊 ";
55-
5652
@Before
5753
public void before() throws ExecutionException, InterruptedException {
5854
if (storageClient == null) {
59-
FirebaseApp app = FirebaseApp.initializeApp(InstrumentationRegistry.getContext());
55+
FirebaseApp app =
56+
FirebaseApp.initializeApp(
57+
androidx.test.platform.app.InstrumentationRegistry.getInstrumentation().getContext());
6058
storageClient = FirebaseStorage.getInstance(app);
6159

6260
Tasks.await(getReference("metadata.dat").putBytes(new byte[0]));
@@ -66,12 +64,20 @@ public void before() throws ExecutionException, InterruptedException {
6664
}
6765
}
6866

67+
public File createFile(String fileName) {
68+
return new File(
69+
androidx.test.platform.app.InstrumentationRegistry.getInstrumentation()
70+
.getContext()
71+
.getFilesDir(),
72+
fileName);
73+
}
74+
6975
@Test
7076
public void downloadFile() throws ExecutionException, InterruptedException, IOException {
71-
File tempFile = new File(Environment.getExternalStorageDirectory(), "download.dat");
77+
String fileName = "download.dat";
78+
File tempFile = createFile(fileName);
7279

73-
FileDownloadTask.TaskSnapshot fileTask =
74-
Tasks.await(getReference("download.dat").getFile(tempFile));
80+
FileDownloadTask.TaskSnapshot fileTask = Tasks.await(getReference(fileName).getFile(tempFile));
7581

7682
assertThat(tempFile.exists()).isTrue();
7783
assertThat(tempFile.length()).isEqualTo(LARGE_FILE_SIZE_BYTES);
@@ -80,9 +86,10 @@ public void downloadFile() throws ExecutionException, InterruptedException, IOEx
8086

8187
@Test
8288
public void downloadUnicodeFile() throws ExecutionException, InterruptedException, IOException {
83-
File tempFile = new File(Environment.getExternalStorageDirectory(), "empty.dat");
89+
String fileName = "empty.dat";
90+
File tempFile = createFile(fileName);
8491

85-
Tasks.await(getReference(unicodePrefix + "/empty.dat").getFile(tempFile));
92+
Tasks.await(getReference(unicodePrefix + "/" + fileName).getFile(tempFile));
8693

8794
assertThat(tempFile.exists()).isTrue();
8895
}

firebase-storage/src/main/java/com/google/firebase/storage/network/NetworkRequest.java

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
package com.google.firebase.storage.network;
1616

1717
import android.content.Context;
18-
import android.content.pm.PackageInfo;
19-
import android.content.pm.PackageManager;
2018
import android.net.ConnectivityManager;
2119
import android.net.Uri;
2220
import android.text.TextUtils;
@@ -28,6 +26,7 @@
2826
import com.google.android.gms.tasks.TaskCompletionSource;
2927
import com.google.firebase.FirebaseApp;
3028
import com.google.firebase.emulators.EmulatedServiceSettings;
29+
import com.google.firebase.storage.BuildConfig;
3130
import com.google.firebase.storage.StorageException;
3231
import com.google.firebase.storage.internal.StorageReferenceUri;
3332
import com.google.firebase.storage.network.connection.HttpURLConnectionFactory;
@@ -50,23 +49,18 @@
5049
/** Encapsulates a single network request and response */
5150
@SuppressWarnings("unused")
5251
public abstract class NetworkRequest {
53-
private static final String TAG = "NetworkRequest";
54-
55-
private static final String X_FIREBASE_GMPID = "x-firebase-gmpid";
56-
private static final String X_FIREBASE_APPCHECK = "x-firebase-appcheck";
57-
5852
public static final Uri PROD_BASE_URL = Uri.parse("https://firebasestorage.googleapis.com/v0");
59-
6053
/* Do not change these values without changing corresponding logic on the SDK side*/
6154
public static final int INITIALIZATION_EXCEPTION = -1;
6255
public static final int NETWORK_UNAVAILABLE = -2;
63-
6456
/*package*/ static final String GET = "GET";
6557
/*package*/ static final String DELETE = "DELETE";
6658
/*package*/ static final String POST = "POST";
6759
/*package*/ static final String PATCH = "PATCH";
6860
/*package*/ static final String PUT = "PUT";
69-
61+
private static final String TAG = "NetworkRequest";
62+
private static final String X_FIREBASE_GMPID = "x-firebase-gmpid";
63+
private static final String X_FIREBASE_APPCHECK = "x-firebase-appcheck";
7064
private static final int MAXIMUM_TOKEN_WAIT_TIME_MS = 30000;
7165
private static final String CONTENT_TYPE = "Content-Type";
7266
private static final String APPLICATION_JSON = "application/json";
@@ -80,7 +74,6 @@ public abstract class NetworkRequest {
8074

8175
private StorageReferenceUri storageReferenceUri;
8276

83-
private static String gmsCoreVersion;
8477
private Context context;
8578
private Map<String, List<String>> resultHeaders;
8679
private int resultCode;
@@ -321,23 +314,6 @@ private HttpURLConnection createConnection() throws IOException {
321314
return conn;
322315
}
323316

324-
@NonNull
325-
private static String getGmsCoreVersion(Context context) {
326-
if (gmsCoreVersion == null) {
327-
PackageManager packageManager = context.getPackageManager();
328-
try {
329-
PackageInfo info = packageManager.getPackageInfo("com.google.android.gms", 0);
330-
gmsCoreVersion = info.versionName;
331-
} catch (PackageManager.NameNotFoundException e) {
332-
Log.e(TAG, "Unable to find gmscore in package manager", e);
333-
}
334-
if (gmsCoreVersion == null) {
335-
gmsCoreVersion = "[No Gmscore]";
336-
}
337-
}
338-
return gmsCoreVersion;
339-
}
340-
341317
@SuppressWarnings("TryFinallyCanBeTryWithResources")
342318
private void constructMessage(
343319
@NonNull HttpURLConnection conn, @Nullable String authToken, @Nullable String appCheckToken)
@@ -355,14 +331,8 @@ private void constructMessage(
355331
} else {
356332
Log.w(TAG, "No App Check token for request.");
357333
}
358-
359-
StringBuilder userAgent = new StringBuilder("Android/");
360-
String gmsCore = getGmsCoreVersion(context);
361-
if (!TextUtils.isEmpty(gmsCore)) {
362-
userAgent.append(gmsCore);
363-
}
364-
conn.setRequestProperty("X-Firebase-Storage-Version", userAgent.toString());
365-
334+
String userAgent = "Android/" + BuildConfig.VERSION_NAME;
335+
conn.setRequestProperty("X-Firebase-Storage-Version", userAgent);
366336
Map<String, String> requestProperties = requestHeaders;
367337
for (Map.Entry<String, String> entry : requestProperties.entrySet()) {
368338
conn.setRequestProperty(entry.getKey(), entry.getValue());

firebase-storage/src/test/java/com/google/firebase/storage/network/MockConnectionFactory.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import android.util.Base64;
2222
import androidx.annotation.NonNull;
23+
import com.google.firebase.storage.BuildConfig;
2324
import com.google.firebase.storage.network.connection.HttpURLConnectionFactory;
2425
import java.io.BufferedReader;
2526
import java.io.ByteArrayOutputStream;
@@ -44,10 +45,10 @@
4445

4546
public class MockConnectionFactory implements HttpURLConnectionFactory {
4647
private final boolean binaryBody;
47-
private HttpURLConnection oldMock;
48-
private List<String> verifications = new ArrayList<>();
4948
private final BufferedReader br;
5049
private final Semaphore pauseSemaphore = new Semaphore(0);
50+
private HttpURLConnection oldMock;
51+
private List<String> verifications = new ArrayList<>();
5152
private int lineCount = 0;
5253
private int pauseRecord = Integer.MAX_VALUE;
5354
private int currentRecord = 0;
@@ -69,6 +70,11 @@ public MockConnectionFactory(String testName, boolean binaryBody) {
6970
}
7071
}
7172

73+
private static InputStream getResFile(String fileName) {
74+
ClassLoader classLoader = MockConnectionFactory.class.getClassLoader();
75+
return classLoader.getResourceAsStream("activitylogs/" + fileName);
76+
}
77+
7278
public void setPauseRecord(int pauseRecord) {
7379
this.pauseRecord = pauseRecord;
7480
}
@@ -77,11 +83,6 @@ public Semaphore getSemaphore() {
7783
return pauseSemaphore;
7884
}
7985

80-
private static InputStream getResFile(String fileName) {
81-
ClassLoader classLoader = MockConnectionFactory.class.getClassLoader();
82-
return classLoader.getResourceAsStream("activitylogs/" + fileName);
83-
}
84-
8586
@Override
8687
public synchronized HttpURLConnection createInstance(@NonNull URL url) throws IOException {
8788
verifyOldMock();
@@ -206,7 +207,11 @@ public synchronized void verifyOldMock() {
206207
key = value.substring(0, comma);
207208
value = value.substring(comma + 1);
208209
requestPropertyKeys.add(key);
209-
requestPropertyValues.add(value);
210+
if (key.equals("X-Firebase-Storage-Version")) {
211+
requestPropertyValues.add("Android/" + BuildConfig.VERSION_NAME);
212+
} else {
213+
requestPropertyValues.add(value);
214+
}
210215
}
211216
} else if (key.equalsIgnoreCase("setDoOutput")) {
212217
verify(oldMock).setDoOutput("true".equals(value));

0 commit comments

Comments
 (0)