Skip to content

Use gzip #3316

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
Jan 26, 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 @@ -26,13 +26,15 @@
import com.google.firebase.components.Dependency;
import com.google.firebase.inject.Provider;
import com.google.firebase.platforminfo.UserAgentPublisher;
import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPOutputStream;
import org.json.JSONArray;
import org.json.JSONObject;

Expand Down Expand Up @@ -99,7 +101,11 @@ public Task<String> getHeartBeatsHeader() {
JSONObject output = new JSONObject();
output.put("heartbeats", array);
output.put("version", "2");
return Base64.encodeToString(output.toString().getBytes(), Base64.DEFAULT);
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);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion firebase-common/src/test/java/android/util/Base64.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@

public class Base64 {
public static String encodeToString(byte[] input, int flags) {
return java.util.Base64.getEncoder().encodeToString(input);
return java.util.Base64.getUrlEncoder().encodeToString(input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import androidx.test.core.app.ApplicationProvider;
import androidx.test.runner.AndroidJUnit4;
import com.google.firebase.platforminfo.UserAgentPublisher;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
Expand All @@ -37,6 +39,7 @@
import java.util.concurrent.LinkedBlockingQueue;
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;
Expand Down Expand Up @@ -96,7 +99,7 @@ public void getHeartBeatCode_noHeartBeat() {

@Test
public void generateHeartBeat_oneHeartBeat()
throws ExecutionException, InterruptedException, JSONException {
throws ExecutionException, InterruptedException, JSONException, IOException {
ArrayList<HeartBeatResult> returnResults = new ArrayList<>();
returnResults.add(
HeartBeatResult.create(
Expand All @@ -111,9 +114,10 @@ public void generateHeartBeat_oneHeartBeat()
.getHeartBeatsHeader()
.addOnCompleteListener(executor, getOnCompleteListener);
String expected =
Base64.getEncoder()
Base64.getUrlEncoder()
.encodeToString(
"{\"heartbeats\":[{\"agent\":\"test-agent\",\"date\":\"[2015-02-03]\"}],\"version\":\"2\"}"
compress(
"{\"heartbeats\":[{\"agent\":\"test-agent\",\"date\":\"[2015-02-03]\"}],\"version\":\"2\"}")
.getBytes());
assertThat(getOnCompleteListener.await().replace("\n", "")).isEqualTo(expected);
}
Expand Down Expand Up @@ -142,7 +146,7 @@ public void firstNewThenOld_synchronizedCorrectly()

@Test
public void firstOldThenNew_synchronizedCorrectly()
throws ExecutionException, InterruptedException {
throws ExecutionException, InterruptedException, IOException {
Context context = ApplicationProvider.getApplicationContext();
SharedPreferences heartBeatSharedPreferences =
context.getSharedPreferences("testHeartBeat", Context.MODE_PRIVATE);
Expand All @@ -152,7 +156,8 @@ public void firstOldThenNew_synchronizedCorrectly()
new DefaultHeartBeatController(
() -> heartBeatInfoStorage, logSources, executor, () -> publisher, context);
String emptyString =
Base64.getEncoder().encodeToString("{\"heartbeats\":[],\"version\":\"2\"}".getBytes());
Base64.getUrlEncoder()
.encodeToString(compress("{\"heartbeats\":[],\"version\":\"2\"}").getBytes());
controller.registerHeartBeat().addOnCompleteListener(executor, storeOnCompleteListener);
storeOnCompleteListener.await();
int heartBeatCode = controller.getHeartBeatCode("test").getCode();
Expand All @@ -169,7 +174,7 @@ public void firstOldThenNew_synchronizedCorrectly()

@Test
public void generateHeartBeat_twoHeartBeatsSameUserAgent()
throws ExecutionException, InterruptedException, JSONException {
throws ExecutionException, InterruptedException, JSONException, IOException {
ArrayList<HeartBeatResult> returnResults = new ArrayList<>();
ArrayList<String> dateList = new ArrayList<>();
dateList.add("2015-03-02");
Expand All @@ -191,19 +196,26 @@ public void generateHeartBeat_twoHeartBeatsSameUserAgent()
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.getEncoder().encodeToString(output.toString().getBytes());
String expected = Base64.getUrlEncoder().encodeToString(compress(output.toString()).getBytes());
assertThat(getOnCompleteListener.await().replace("\n", "")).isEqualTo(expected);
}

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");
}

@Test
public void generateHeartBeat_twoHeartBeatstwoUserAgents()
throws ExecutionException, InterruptedException, JSONException {
throws ExecutionException, InterruptedException, JSONException, IOException {
ArrayList<HeartBeatResult> returnResults = new ArrayList<>();
returnResults.add(
HeartBeatResult.create(
Expand All @@ -225,9 +237,10 @@ public void generateHeartBeat_twoHeartBeatstwoUserAgents()
.getHeartBeatsHeader()
.addOnCompleteListener(executor, getOnCompleteListener);
String expected =
Base64.getEncoder()
Base64.getUrlEncoder()
.encodeToString(
"{\"heartbeats\":[{\"agent\":\"test-agent\",\"date\":\"[2015-03-02]\"},{\"agent\":\"test-agent-1\",\"date\":\"[2015-03-03]\"}],\"version\":\"2\"}"
compress(
"{\"heartbeats\":[{\"agent\":\"test-agent\",\"date\":\"[2015-03-02]\"},{\"agent\":\"test-agent-1\",\"date\":\"[2015-03-03]\"}],\"version\":\"2\"}")
.getBytes());
assertThat(getOnCompleteListener.await().replace("\n", "")).isEqualTo(expected);
}
Expand Down