summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrank Li <lifr@google.com>2020-06-24 14:09:54 +0000
committerFrank Li <lifr@google.com>2020-06-24 14:14:46 +0000
commitad90e1bfe13e6a5dc85a33d64c37981ce1ac4408 (patch)
tree6fd4eeb8c0f11386c01a74ef33945fb1960b1474
parent65e067f32e512a3aab6d7c01a83c8dd0d3e7f750 (diff)
Fix NullPointerException on addErrorCode when input is Invalid error code
Problem: 1. When the errocode is not defined in DhcpErrorCode enum. Then the DhcpErrorCode.forNumber(errorCode) will return null. 2. Then use null as the parameter cause NullPointerException on addErrorCode. (Because the addErrorCode need nonNullable parameter) Solution: If the errorcode is not defined in the Dhcp ErrorCode enumeration, please use ET_UNKNOWN instead as AddErrorCode parameter, not null. Bug: 151796056 Test: atest Original-Change: https://android-review.googlesource.com/1346103 Merged-In: I43b8415ab98b4cf6520c3240dc544a9f62730a4e Change-Id: I43b8415ab98b4cf6520c3240dc544a9f62730a4e
-rw-r--r--src/com/android/networkstack/metrics/IpProvisioningMetrics.java11
-rw-r--r--tests/unit/src/com/android/networkstack/metrics/NetworkIpProvisioningMetricsTest.java28
2 files changed, 24 insertions, 15 deletions
diff --git a/src/com/android/networkstack/metrics/IpProvisioningMetrics.java b/src/com/android/networkstack/metrics/IpProvisioningMetrics.java
index 1f969d4..64e173d 100644
--- a/src/com/android/networkstack/metrics/IpProvisioningMetrics.java
+++ b/src/com/android/networkstack/metrics/IpProvisioningMetrics.java
@@ -120,12 +120,21 @@ public class IpProvisioningMetrics {
transSuccess ? HostnameTransResult.HTR_SUCCESS : HostnameTransResult.HTR_FAILURE);
}
+ private static DhcpErrorCode dhcpErrorFromNumberSafe(int number) {
+ // See DhcpErrorCode.errorCodeWithOption
+ // TODO: add a DhcpErrorCode method to extract the code;
+ // or replace legacy error codes with the new metrics.
+ final DhcpErrorCode error = DhcpErrorCode.forNumber(number & 0xFFFF0000);
+ if (error == null) return DhcpErrorCode.ET_UNKNOWN;
+ return error;
+ }
+
/**
* write the DHCP error code into DhcpSession.
*/
public void addDhcpErrorCode(final int errorCode) {
if (mDhcpSessionBuilder.getErrorCodeCount() >= MAX_DHCP_ERROR_COUNT) return;
- mDhcpSessionBuilder.addErrorCode(DhcpErrorCode.forNumber(errorCode));
+ mDhcpSessionBuilder.addErrorCode(dhcpErrorFromNumberSafe(errorCode));
}
/**
diff --git a/tests/unit/src/com/android/networkstack/metrics/NetworkIpProvisioningMetricsTest.java b/tests/unit/src/com/android/networkstack/metrics/NetworkIpProvisioningMetricsTest.java
index 08812b7..01d94e2 100644
--- a/tests/unit/src/com/android/networkstack/metrics/NetworkIpProvisioningMetricsTest.java
+++ b/tests/unit/src/com/android/networkstack/metrics/NetworkIpProvisioningMetricsTest.java
@@ -16,6 +16,7 @@
package com.android.networkstack.metrics;
+import android.net.dhcp.DhcpPacket;
import android.net.metrics.DhcpErrorEvent;
import android.stats.connectivity.DhcpErrorCode;
import android.stats.connectivity.DhcpFeature;
@@ -64,25 +65,24 @@ public class NetworkIpProvisioningMetricsTest {
final NetworkIpProvisioningReported mStats;
final IpProvisioningMetrics mMetrics = new IpProvisioningMetrics();
mMetrics.reset();
- mMetrics.addDhcpErrorCode(DhcpErrorEvent.DHCP_ERROR);
- mMetrics.addDhcpErrorCode(DhcpErrorEvent.L2_WRONG_ETH_TYPE);
- mMetrics.addDhcpErrorCode(DhcpErrorEvent.L3_INVALID_IP);
- mMetrics.addDhcpErrorCode(DhcpErrorEvent.L4_WRONG_PORT);
mMetrics.addDhcpErrorCode(DhcpErrorEvent.BOOTP_TOO_SHORT);
- mMetrics.addDhcpErrorCode(DhcpErrorEvent.DHCP_NO_COOKIE);
+ mMetrics.addDhcpErrorCode(DhcpErrorEvent.errorCodeWithOption(
+ DhcpErrorEvent.BUFFER_UNDERFLOW, DhcpPacket.DHCP_HOST_NAME));
+ // Write the incorrect input number 50000 as DHCP error number
+ int incorrectErrorCodeNumber = 50000;
+ mMetrics.addDhcpErrorCode(incorrectErrorCodeNumber);
for (int i = 0; i < mMetrics.MAX_DHCP_ERROR_COUNT; i++) {
mMetrics.addDhcpErrorCode(DhcpErrorEvent.PARSING_ERROR);
}
mStats = mMetrics.statsWrite();
- assertEquals(DhcpErrorCode.ET_DHCP_ERROR, mStats.getDhcpSession().getErrorCode(0));
- assertEquals(DhcpErrorCode.ET_L2_WRONG_ETH_TYPE, mStats.getDhcpSession().getErrorCode(1));
- assertEquals(DhcpErrorCode.ET_L3_INVALID_IP, mStats.getDhcpSession().getErrorCode(2));
- assertEquals(DhcpErrorCode.ET_L4_WRONG_PORT, mStats.getDhcpSession().getErrorCode(3));
- assertEquals(DhcpErrorCode.ET_BOOTP_TOO_SHORT, mStats.getDhcpSession().getErrorCode(4));
- assertEquals(DhcpErrorCode.ET_DHCP_NO_COOKIE, mStats.getDhcpSession().getErrorCode(5));
- // Check can record the same error code
- assertEquals(DhcpErrorCode.ET_PARSING_ERROR, mStats.getDhcpSession().getErrorCode(6));
- assertEquals(DhcpErrorCode.ET_PARSING_ERROR, mStats.getDhcpSession().getErrorCode(6));
+ assertEquals(DhcpErrorCode.ET_BOOTP_TOO_SHORT, mStats.getDhcpSession().getErrorCode(0));
+ assertEquals(DhcpErrorCode.ET_BUFFER_UNDERFLOW, mStats.getDhcpSession().getErrorCode(1));
+ // When the input is an invalid integer value (such as incorrectErrorCodeNumber),
+ // the DhcpErrorCode will be ET_UNKNOWN
+ assertEquals(DhcpErrorCode.ET_UNKNOWN, mStats.getDhcpSession().getErrorCode(2));
+ // If the same error code appears, it must be recorded
+ assertEquals(DhcpErrorCode.ET_PARSING_ERROR, mStats.getDhcpSession().getErrorCode(3));
+ assertEquals(DhcpErrorCode.ET_PARSING_ERROR, mStats.getDhcpSession().getErrorCode(4));
// The maximum number of DHCP error code counts is MAX_DHCP_ERROR_COUNT
assertEquals(mMetrics.MAX_DHCP_ERROR_COUNT, mStats.getDhcpSession().getErrorCodeCount());
}