Minor cleanups to NetdCallbackReceiver.onEvent.
- Clean up identical error messages.
- Fix the array length check for InterfaceAddressChange.
Bug: 9180552
Change-Id: Id871f481445b530c3ad749725f1548df0e3a1228
diff --git a/services/java/com/android/server/NetworkManagementService.java b/services/java/com/android/server/NetworkManagementService.java
index 92f99c2..9a0d648 100644
--- a/services/java/com/android/server/NetworkManagementService.java
+++ b/services/java/com/android/server/NetworkManagementService.java
@@ -455,6 +455,7 @@
@Override
public boolean onEvent(int code, String raw, String[] cooked) {
+ String errorMessage = String.format("Invalid event from daemon (%s)", raw);
switch (code) {
case NetdResponseCode.InterfaceChange:
/*
@@ -465,8 +466,7 @@
* "NNN Iface linkstatus <name> <up/down>"
*/
if (cooked.length < 4 || !cooked[1].equals("Iface")) {
- throw new IllegalStateException(
- String.format("Invalid event from daemon (%s)", raw));
+ throw new IllegalStateException(errorMessage);
}
if (cooked[2].equals("added")) {
notifyInterfaceAdded(cooked[3]);
@@ -481,8 +481,7 @@
notifyInterfaceLinkStateChanged(cooked[3], cooked[4].equals("up"));
return true;
}
- throw new IllegalStateException(
- String.format("Invalid event from daemon (%s)", raw));
+ throw new IllegalStateException(errorMessage);
// break;
case NetdResponseCode.BandwidthControl:
/*
@@ -490,15 +489,13 @@
* Format: "NNN limit alert <alertName> <ifaceName>"
*/
if (cooked.length < 5 || !cooked[1].equals("limit")) {
- throw new IllegalStateException(
- String.format("Invalid event from daemon (%s)", raw));
+ throw new IllegalStateException(errorMessage);
}
if (cooked[2].equals("alert")) {
notifyLimitReached(cooked[3], cooked[4]);
return true;
}
- throw new IllegalStateException(
- String.format("Invalid event from daemon (%s)", raw));
+ throw new IllegalStateException(errorMessage);
// break;
case NetdResponseCode.InterfaceClassActivity:
/*
@@ -506,8 +503,7 @@
* Format: "NNN IfaceClass <active/idle> <label>"
*/
if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
- throw new IllegalStateException(
- String.format("Invalid event from daemon (%s)", raw));
+ throw new IllegalStateException(errorMessage);
}
boolean isActive = cooked[2].equals("active");
notifyInterfaceClassActivity(cooked[3], isActive);
@@ -519,9 +515,8 @@
* Format: "NNN Address updated <addr> <iface> <flags> <scope>"
* "NNN Address removed <addr> <iface> <flags> <scope>"
*/
- String msg = String.format("Invalid event from daemon (%s)", raw);
- if (cooked.length < 6 || !cooked[1].equals("Address")) {
- throw new IllegalStateException(msg);
+ if (cooked.length < 7 || !cooked[1].equals("Address")) {
+ throw new IllegalStateException(errorMessage);
}
int flags;
@@ -530,7 +525,7 @@
flags = Integer.parseInt(cooked[5]);
scope = Integer.parseInt(cooked[6]);
} catch(NumberFormatException e) {
- throw new IllegalStateException(msg);
+ throw new IllegalStateException(errorMessage);
}
if (cooked[2].equals("updated")) {
diff --git a/services/tests/servicestests/src/com/android/server/NetworkManagementServiceTest.java b/services/tests/servicestests/src/com/android/server/NetworkManagementServiceTest.java
index 56dd7c4..30d4eff 100644
--- a/services/tests/servicestests/src/com/android/server/NetworkManagementServiceTest.java
+++ b/services/tests/servicestests/src/com/android/server/NetworkManagementServiceTest.java
@@ -166,6 +166,9 @@
sendMessage("614 Address removed 2001:db8::1/64 wlan0 1 0");
expectSoon(observer).addressRemoved("2001:db8::1/64", "wlan0", 1, 0);
+ sendMessage("614 Address removed 2001:db8::1/64 wlan0 1");
+ // Not enough arguments.
+
sendMessage("666 Address added 2001:db8::1/64 wlan0 1 0");
// Invalid code.