Skip to content

Commit 6bf1959

Browse files
committed
Adding null checks for InstrHttpInputStream/InstrHttpOutputStream
1 parent 41e38cb commit 6bf1959

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

firebase-perf/src/main/java/com/google/firebase/perf/network/InstrURLConnectionBase.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ public InputStream getInputStream() throws IOException {
140140

141141
try {
142142
final InputStream inputStream = httpUrlConnection.getInputStream();
143-
return new InstrHttpInputStream(inputStream, networkMetricBuilder, timer);
143+
// Make sure we don't pass in a null into InstrHttpInputStream, since InstrHttpInputStream is
144+
// not null-safe.
145+
if (inputStream != null) {
146+
return new InstrHttpInputStream(inputStream, networkMetricBuilder, timer);
147+
}
148+
return null;
144149
} catch (final IOException e) {
145150
networkMetricBuilder.setTimeToResponseCompletedMicros(timer.getDurationMicros());
146151
NetworkRequestMetricBuilderUtil.logError(networkMetricBuilder);
@@ -156,8 +161,13 @@ public long getLastModified() {
156161

157162
public OutputStream getOutputStream() throws IOException {
158163
try {
159-
return new InstrHttpOutputStream(
160-
httpUrlConnection.getOutputStream(), networkMetricBuilder, timer);
164+
final OutputStream outputStream = httpUrlConnection.getOutputStream();
165+
// Make sure we don't pass in a null into InstrHttpOutputStream, since InstrHttpOutputStream
166+
// is not null-safe.
167+
if (outputStream != null) {
168+
return new InstrHttpOutputStream(outputStream, networkMetricBuilder, timer);
169+
}
170+
return null;
161171
} catch (final IOException e) {
162172
networkMetricBuilder.setTimeToResponseCompletedMicros(timer.getDurationMicros());
163173
NetworkRequestMetricBuilderUtil.logError(networkMetricBuilder);

firebase-perf/src/test/java/com/google/firebase/perf/network/InstrURLConnectionBaseTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ public void testGetInputStreamThrowsIOException() throws IOException {
107107
assertThat(metric.getTimeToResponseCompletedUs()).isEqualTo(2000);
108108
}
109109

110+
@Test
111+
public void testGetInputStreamWithNullInputStreamReturnsNull() throws IOException {
112+
HttpURLConnection urlConnection = mockHttpUrlConnection();
113+
when(urlConnection.getInputStream()).thenReturn(null);
114+
assertThat(
115+
new InstrURLConnectionBase(urlConnection, timer, networkMetricBuilder).getInputStream())
116+
.isNull();
117+
}
118+
110119
@Test
111120
public void testGetOutputStreamThrowsIOException() throws IOException {
112121
HttpURLConnection urlConnection = mockHttpUrlConnection();
@@ -122,6 +131,16 @@ public void testGetOutputStreamThrowsIOException() throws IOException {
122131
assertThat(metric.getTimeToResponseCompletedUs()).isEqualTo(2000);
123132
}
124133

134+
@Test
135+
public void testGetOutputStreamWithNullOuputStreamReturnsNull() throws IOException {
136+
HttpURLConnection urlConnection = mockHttpUrlConnection();
137+
when(urlConnection.getOutputStream()).thenReturn(null);
138+
assertThat(
139+
new InstrURLConnectionBase(urlConnection, timer, networkMetricBuilder)
140+
.getOutputStream())
141+
.isNull();
142+
}
143+
125144
@Test
126145
public void testGetPermissionThrowsIOException() throws IOException {
127146
HttpURLConnection urlConnection = mockHttpUrlConnection();

0 commit comments

Comments
 (0)