Skip to content

Commit 3f5146f

Browse files
Send NDK reports through DataTransport (#1356)
Co-authored-by: Matt Willis <[email protected]>
1 parent 1d836a1 commit 3f5146f

31 files changed

+1216
-261
lines changed

firebase-crashlytics/src/androidTest/assets/firebase_settings.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"app": {
99
"status": "activated",
1010
"report_upload_variant": 2,
11+
"native_report_upload_variant": 2,
1112
"update_required": true
1213
},
1314
"fabric": {

firebase-crashlytics/src/androidTest/assets/firebase_settings_new.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"app": {
99
"status": "new",
1010
"report_upload_variant": 2,
11+
"native_report_upload_variant": 2,
1112
"update_required": true
1213
},
1314
"fabric": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2020 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.crashlytics.internal.common;
16+
17+
import static org.junit.Assert.assertArrayEquals;
18+
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertNotNull;
20+
import static org.junit.Assert.assertNull;
21+
22+
import com.google.firebase.crashlytics.internal.model.CrashlyticsReport;
23+
import java.io.IOException;
24+
import org.junit.Test;
25+
26+
public class BytesBackedNativeSessionFileTest {
27+
byte[] testBytes = {0, 2, 20, 10};
28+
byte[] emptyBytes = {};
29+
30+
@Test
31+
public void testAsStream_convertsToStream() throws IOException {
32+
BytesBackedNativeSessionFile nativeSessionFile =
33+
new BytesBackedNativeSessionFile("file_name", "file", testBytes);
34+
byte[] readBytes = new byte[4];
35+
nativeSessionFile.getStream().read(readBytes);
36+
assertArrayEquals(testBytes, readBytes);
37+
}
38+
39+
@Test
40+
public void testAsStreamWhenEmpty_returnsNull() {
41+
BytesBackedNativeSessionFile nativeSessionFile =
42+
new BytesBackedNativeSessionFile("file_name", "file", emptyBytes);
43+
assertNull(nativeSessionFile.getStream());
44+
}
45+
46+
@Test
47+
public void testAsFilePayload_convertsToFilePayload() {
48+
BytesBackedNativeSessionFile nativeSessionFile =
49+
new BytesBackedNativeSessionFile("file_name", "file", testBytes);
50+
CrashlyticsReport.FilesPayload.File filesPayload = nativeSessionFile.asFilePayload();
51+
assertNotNull(filesPayload);
52+
assertArrayEquals(testBytes, filesPayload.getContents());
53+
assertEquals("file_name", filesPayload.getFilename());
54+
}
55+
56+
@Test
57+
public void testAsFilePayloadWhenEmpty_convertsToNull() {
58+
BytesBackedNativeSessionFile nativeSessionFile =
59+
new BytesBackedNativeSessionFile("file_name", "file", emptyBytes);
60+
assertNull(nativeSessionFile.asFilePayload());
61+
}
62+
}

firebase-crashlytics/src/androidTest/java/com/google/firebase/crashlytics/internal/common/CrashlyticsControllerTest.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ protected void setUp() throws Exception {
116116
when(mockFileStore.getFilesDirPath()).thenReturn(testFilesDirectory.getPath());
117117

118118
final SettingsData testSettingsData =
119-
new TestSettingsData(3, CrashlyticsController.REPORT_UPLOAD_VARIANT_LEGACY);
119+
new TestSettingsData(
120+
3,
121+
DataTransportState.REPORT_UPLOAD_VARIANT_LEGACY,
122+
DataTransportState.REPORT_UPLOAD_VARIANT_LEGACY);
120123
appSettingsData = testSettingsData.appData;
121124
sessionSettingsData = testSettingsData.sessionData;
122125

firebase-crashlytics/src/androidTest/java/com/google/firebase/crashlytics/internal/common/CrashlyticsCoreTest.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,10 @@ private Task<CrashlyticsCore> startCoreAsync(CrashlyticsCore crashlyticsCore) {
610610

611611
SettingsController mockSettingsController = mock(SettingsController.class);
612612
final SettingsData settings =
613-
new TestSettingsData(3, CrashlyticsController.REPORT_UPLOAD_VARIANT_LEGACY);
613+
new TestSettingsData(
614+
3,
615+
DataTransportState.REPORT_UPLOAD_VARIANT_LEGACY,
616+
DataTransportState.REPORT_UPLOAD_VARIANT_LEGACY);
614617
when(mockSettingsController.getSettings()).thenReturn(settings);
615618
when(mockSettingsController.getAppSettings()).thenReturn(Tasks.forResult(settings.appData));
616619

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright 2020 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.crashlytics.internal.common;
16+
17+
import static org.junit.Assert.assertArrayEquals;
18+
19+
import android.content.Context;
20+
import com.google.firebase.crashlytics.internal.CrashlyticsTestCase;
21+
import com.google.firebase.crashlytics.internal.model.CrashlyticsReport;
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.File;
24+
import java.io.FileOutputStream;
25+
import java.io.IOException;
26+
import org.junit.Test;
27+
28+
public class FileBackedNativeSessionFileTest extends CrashlyticsTestCase {
29+
byte[] testContents = {0, 2, 20, 10};
30+
byte[] emptyContents = {};
31+
File testFile;
32+
File emptyFile;
33+
File missingFile;
34+
35+
@Override
36+
protected void setUp() throws Exception {
37+
super.setUp();
38+
final Context context = getContext();
39+
testFile = new File(context.getFilesDir(), "testFile");
40+
try (FileOutputStream fout = new FileOutputStream(testFile);
41+
ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
42+
stream.write(testContents);
43+
stream.writeTo(fout);
44+
}
45+
emptyFile = new File(context.getFilesDir(), "emptyFile");
46+
emptyFile.createNewFile();
47+
missingFile = new File(context.getFilesDir(), "missingFile");
48+
}
49+
50+
@Override
51+
protected void tearDown() throws Exception {
52+
super.tearDown();
53+
testFile.delete();
54+
emptyFile.delete();
55+
}
56+
57+
@Test
58+
public void testAsStream_convertsToStream() throws IOException {
59+
FileBackedNativeSessionFile nativeSessionFile =
60+
new FileBackedNativeSessionFile("file_name", "file", testFile);
61+
byte[] readBytes = new byte[4];
62+
nativeSessionFile.getStream().read(readBytes);
63+
assertArrayEquals(testContents, readBytes);
64+
}
65+
66+
@Test
67+
public void testAsStreamWhenEmpty_returnsEmpty() throws IOException {
68+
FileBackedNativeSessionFile nativeSessionFile =
69+
new FileBackedNativeSessionFile("file_name", "file", emptyFile);
70+
byte[] readBytes = new byte[0];
71+
nativeSessionFile.getStream().read(readBytes);
72+
assertArrayEquals(emptyContents, readBytes);
73+
}
74+
75+
@Test
76+
public void testAsStreamWhenMissing_returnsNull() {
77+
FileBackedNativeSessionFile nativeSessionFile =
78+
new FileBackedNativeSessionFile("file_name", "file", missingFile);
79+
assertNull(nativeSessionFile.getStream());
80+
}
81+
82+
@Test
83+
public void testAsFilePayload_convertsToFilePayload() {
84+
FileBackedNativeSessionFile nativeSessionFile =
85+
new FileBackedNativeSessionFile("file_name", "file", testFile);
86+
CrashlyticsReport.FilesPayload.File filesPayload = nativeSessionFile.asFilePayload();
87+
assertNotNull(filesPayload);
88+
assertArrayEquals(testContents, filesPayload.getContents());
89+
assertEquals("file_name", filesPayload.getFilename());
90+
}
91+
92+
@Test
93+
public void testAsFilePayloadWhenEmpty_returnsEmptyPayload() {
94+
FileBackedNativeSessionFile nativeSessionFile =
95+
new FileBackedNativeSessionFile("file_name", "file", emptyFile);
96+
CrashlyticsReport.FilesPayload.File filesPayload = nativeSessionFile.asFilePayload();
97+
assertNotNull(filesPayload);
98+
assertArrayEquals(emptyContents, filesPayload.getContents());
99+
assertEquals("file_name", filesPayload.getFilename());
100+
}
101+
102+
@Test
103+
public void testAsFilePayloadWhenMissing_convertsToNull() {
104+
FileBackedNativeSessionFile nativeSessionFile =
105+
new FileBackedNativeSessionFile("file_name", "file", missingFile);
106+
assertNull(nativeSessionFile.asFilePayload());
107+
}
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2020 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.crashlytics.internal.common;
16+
17+
import android.content.Context;
18+
import com.google.firebase.crashlytics.internal.CrashlyticsTestCase;
19+
import java.io.ByteArrayOutputStream;
20+
import java.io.File;
21+
import java.io.FileOutputStream;
22+
import java.io.IOException;
23+
import java.util.Arrays;
24+
import java.util.List;
25+
import org.junit.Test;
26+
import org.mockito.internal.util.collections.Sets;
27+
28+
public class NativeSessionFileGzipperTest extends CrashlyticsTestCase {
29+
byte[] testContents = {0, 2, 20, 10};
30+
File testFile;
31+
File missingFile;
32+
File gzipDir;
33+
34+
@Override
35+
protected void setUp() throws Exception {
36+
super.setUp();
37+
final Context context = getContext();
38+
testFile = new File(context.getFilesDir(), "testFile");
39+
try (FileOutputStream fout = new FileOutputStream(testFile);
40+
ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
41+
stream.write(testContents);
42+
stream.writeTo(fout);
43+
}
44+
File baseDirectory = context.getFilesDir();
45+
missingFile = new File(baseDirectory, "missingFile");
46+
gzipDir = new File(baseDirectory, "gzip");
47+
gzipDir.mkdirs();
48+
}
49+
50+
@Test
51+
public void testProcessNativeSessions_putsFilesInCorrectLocation() throws IOException {
52+
String fileBackedSessionName = "file";
53+
String byteBackedSessionName = "byte";
54+
FileBackedNativeSessionFile fileSession =
55+
new FileBackedNativeSessionFile("not_applicable", fileBackedSessionName, testFile);
56+
BytesBackedNativeSessionFile byteSession =
57+
new BytesBackedNativeSessionFile("not_applicable", byteBackedSessionName, testContents);
58+
List<NativeSessionFile> files = Arrays.asList(fileSession, byteSession);
59+
NativeSessionFileGzipper.processNativeSessions(gzipDir, files);
60+
61+
assertEquals(
62+
Sets.newSet(
63+
new File(gzipDir, fileBackedSessionName), new File(gzipDir, byteBackedSessionName)),
64+
Sets.newSet(gzipDir.listFiles()));
65+
}
66+
67+
@Test
68+
public void testProcessNativeSessionsWhenDataIsNull_putsFilesInCorrectLocation() {
69+
String fileBackedSessionName = "file";
70+
String byteBackedSessionName = "byte";
71+
FileBackedNativeSessionFile fileSession =
72+
new FileBackedNativeSessionFile("not_applicable", fileBackedSessionName, missingFile);
73+
BytesBackedNativeSessionFile byteSession =
74+
new BytesBackedNativeSessionFile("not_applicable", byteBackedSessionName, testContents);
75+
List<NativeSessionFile> files = Arrays.asList(fileSession, byteSession);
76+
NativeSessionFileGzipper.processNativeSessions(gzipDir, files);
77+
78+
assertEquals(
79+
Sets.newSet(new File(gzipDir, byteBackedSessionName)), Sets.newSet(gzipDir.listFiles()));
80+
}
81+
}

0 commit comments

Comments
 (0)