Skip to content

Fix the way of gzipping and base64. This is now consistent with server #3414

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 8 commits into from
Feb 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import android.content.Context;
import android.util.Base64;
import android.util.Base64OutputStream;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.core.os.UserManagerCompat;
Expand Down Expand Up @@ -95,19 +96,20 @@ public Task<String> getHeartBeatsHeader() {
HeartBeatResult result = allHeartBeats.get(i);
JSONObject obj = new JSONObject();
obj.put("agent", result.getUserAgent());
obj.put("date", result.getUsedDates());
obj.put("dates", new JSONArray(result.getUsedDates()));
array.put(obj);
}
JSONObject output = new JSONObject();
output.put("heartbeats", array);
output.put("version", "2");
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(output.toString().getBytes());
gzip.close();
return Base64.encodeToString(
out.toString("UTF-8").getBytes(),
Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP);
try (Base64OutputStream b64os =
new Base64OutputStream(
out, Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP);
GZIPOutputStream gzip = new GZIPOutputStream(b64os); ) {
gzip.write(output.toString().getBytes("UTF-8"));
}
return out.toString("UTF-8");
}
});
}
Expand Down
21 changes: 0 additions & 21 deletions firebase-common/src/test/java/android/util/Base64.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.firebase.heartbeatinfo;

import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
Expand All @@ -40,9 +41,7 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPOutputStream;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -115,13 +114,9 @@ public void generateHeartBeat_oneHeartBeat()
heartBeatController
.getHeartBeatsHeader()
.addOnCompleteListener(executor, getOnCompleteListener);
String expected =
Base64.getUrlEncoder()
.withoutPadding()
.encodeToString(
compress(
"{\"heartbeats\":[{\"agent\":\"test-agent\",\"date\":\"[2015-02-03]\"}],\"version\":\"2\"}")
.getBytes());
String str =
"{\"heartbeats\":[{\"agent\":\"test-agent\",\"dates\":[\"2015-02-03\"]}],\"version\":\"2\"}";
String expected = compress(str);
assertThat(getOnCompleteListener.await().replace("\n", "")).isEqualTo(expected);
}

Expand Down Expand Up @@ -162,10 +157,7 @@ public void firstOldThenNew_synchronizedCorrectly()
DefaultHeartBeatController controller =
new DefaultHeartBeatController(
() -> heartBeatInfoStorage, logSources, executor, () -> publisher, context);
String emptyString =
Base64.getUrlEncoder()
.withoutPadding()
.encodeToString(compress("{\"heartbeats\":[],\"version\":\"2\"}").getBytes());
String emptyString = compress("{\"heartbeats\":[],\"version\":\"2\"}");
controller.registerHeartBeat().addOnCompleteListener(executor, storeOnCompleteListener);
storeOnCompleteListener.await();
int heartBeatCode = controller.getHeartBeatCode("test").getCode();
Expand Down Expand Up @@ -202,27 +194,32 @@ public void generateHeartBeat_twoHeartBeatsSameUserAgent()
heartBeatController
.getHeartBeatsHeader()
.addOnCompleteListener(executor, getOnCompleteListener);
JSONObject output = new JSONObject();
JSONArray array = new JSONArray();
JSONObject obj = new JSONObject();
obj.put("agent", "test-agent");
obj.put("date", dateList);
array.put(obj);
output.put("heartbeats", array);
output.put("version", "2");
String expected =
Base64.getUrlEncoder()
.withoutPadding()
.encodeToString(compress(output.toString()).getBytes());
String str =
"{\"heartbeats\":[{\"agent\":\"test-agent\",\"dates\":[\"2015-03-02\",\"2015-03-01\"]}],\"version\":\"2\"}";
String expected = compress(str);
assertThat(getOnCompleteListener.await().replace("\n", "")).isEqualTo(expected);
}

private static String base64Encode(byte[] input) {
return Base64.getUrlEncoder().withoutPadding().encodeToString(input);
}

private static byte[] gzip(String input) {
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
try {
try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteOutputStream)) {
gzipOutputStream.write(input.getBytes(UTF_8));
}
byte[] gzipped = byteOutputStream.toByteArray();
byteOutputStream.close();
Comment on lines +208 to +214
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you can use try with resources here as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it complains saying byte[] is not a proper type for try with resources.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK not closing it is OK.

https://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayOutputStream.html#close()

Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

return gzipped;
} catch (IOException e) {
return null;
}
}

private String compress(String str) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.toString().getBytes());
gzip.close();
return out.toString("UTF-8");
return base64Encode(gzip(str));
}

@Config(sdk = 29)
Expand Down Expand Up @@ -250,13 +247,9 @@ public void generateHeartBeat_twoHeartBeatstwoUserAgents()
heartBeatController
.getHeartBeatsHeader()
.addOnCompleteListener(executor, getOnCompleteListener);
String expected =
Base64.getUrlEncoder()
.withoutPadding()
.encodeToString(
compress(
"{\"heartbeats\":[{\"agent\":\"test-agent\",\"date\":\"[2015-03-02]\"},{\"agent\":\"test-agent-1\",\"date\":\"[2015-03-03]\"}],\"version\":\"2\"}")
.getBytes());
String str =
"{\"heartbeats\":[{\"agent\":\"test-agent\",\"dates\":[\"2015-03-02\"]},{\"agent\":\"test-agent-1\",\"dates\":[\"2015-03-03\"]}],\"version\":\"2\"}";
String expected = compress(str);
assertThat(getOnCompleteListener.await().replace("\n", "")).isEqualTo(expected);
}
}