Cleanup NetworkManagementService for Netd commnads binder migrartion
remove dead code used for netd UNIX sokcet
Bug: 65862741
Test: atest NetworkManagementServiceTest FrameworksNetTests
Change-Id: I5db61cb86325b71ac8a8fce7b15dbf569abc5935
diff --git a/tests/net/java/com/android/server/NetworkManagementServiceTest.java b/tests/net/java/com/android/server/NetworkManagementServiceTest.java
index 6fb3225..968b307 100644
--- a/tests/net/java/com/android/server/NetworkManagementServiceTest.java
+++ b/tests/net/java/com/android/server/NetworkManagementServiceTest.java
@@ -16,6 +16,7 @@
package com.android.server;
+import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
@@ -23,11 +24,11 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
+import android.annotation.NonNull;
import android.content.Context;
import android.net.INetd;
+import android.net.INetdUnsolicitedEventListener;
import android.net.LinkAddress;
-import android.net.LocalServerSocket;
-import android.net.LocalSocket;
import android.os.BatteryStats;
import android.os.Binder;
import android.os.IBinder;
@@ -43,12 +44,11 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
-import java.io.IOException;
-import java.io.OutputStream;
-
/**
* Tests for {@link NetworkManagementService}.
*/
@@ -56,16 +56,16 @@
@SmallTest
public class NetworkManagementServiceTest {
- private static final String SOCKET_NAME = "__test__NetworkManagementServiceTest";
private NetworkManagementService mNMService;
- private LocalServerSocket mServerSocket;
- private LocalSocket mSocket;
- private OutputStream mOutputStream;
@Mock private Context mContext;
@Mock private IBatteryStats.Stub mBatteryStatsService;
@Mock private INetd.Stub mNetdService;
+ @NonNull
+ @Captor
+ private ArgumentCaptor<INetdUnsolicitedEventListener> mUnsolListenerCaptor;
+
private final SystemServices mServices = new SystemServices() {
@Override
public IBinder getService(String name) {
@@ -88,32 +88,15 @@
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
-
- // Set up a sheltered test environment.
- mServerSocket = new LocalServerSocket(SOCKET_NAME);
-
+ doNothing().when(mNetdService)
+ .registerUnsolicitedEventListener(mUnsolListenerCaptor.capture());
// Start the service and wait until it connects to our socket.
- mNMService = NetworkManagementService.create(mContext, SOCKET_NAME, mServices);
- mSocket = mServerSocket.accept();
- mOutputStream = mSocket.getOutputStream();
+ mNMService = NetworkManagementService.create(mContext, mServices);
}
@After
public void tearDown() throws Exception {
mNMService.shutdown();
- // Once NetworkManagementService#shutdown() actually does something and shutdowns
- // the underlying NativeDaemonConnector, the block below should be uncommented.
- // if (mOutputStream != null) mOutputStream.close();
- // if (mSocket != null) mSocket.close();
- // if (mServerSocket != null) mServerSocket.close();
- }
-
- /**
- * Sends a message on the netd socket and gives the events some time to make it back.
- */
- private void sendMessage(String message) throws IOException {
- // Strings are null-terminated, so add "\0" at the end.
- mOutputStream.write((message + "\0").getBytes());
}
private static <T> T expectSoon(T mock) {
@@ -131,125 +114,78 @@
// Forget everything that happened to the mock so far, so we can explicitly verify
// everything that happens and does not happen to it from now on.
- reset(observer);
- // Now send NetworkManagementService messages and ensure that the observer methods are
- // called. After every valid message we expect a callback soon after; to ensure that
+ INetdUnsolicitedEventListener unsolListener = mUnsolListenerCaptor.getValue();
+ reset(observer);
+ // Now call unsolListener methods and ensure that the observer methods are
+ // called. After every method we expect a callback soon after; to ensure that
// invalid messages don't cause any callbacks, we call verifyNoMoreInteractions at the end.
/**
* Interface changes.
*/
- sendMessage("600 Iface added rmnet12");
+ unsolListener.onInterfaceAdded("rmnet12");
expectSoon(observer).interfaceAdded("rmnet12");
- sendMessage("600 Iface removed eth1");
+ unsolListener.onInterfaceRemoved("eth1");
expectSoon(observer).interfaceRemoved("eth1");
- sendMessage("607 Iface removed eth1");
- // Invalid code.
-
- sendMessage("600 Iface borked lo down");
- // Invalid event.
-
- sendMessage("600 Iface changed clat4 up again");
- // Extra tokens.
-
- sendMessage("600 Iface changed clat4 up");
+ unsolListener.onInterfaceChanged("clat4", true);
expectSoon(observer).interfaceStatusChanged("clat4", true);
- sendMessage("600 Iface linkstate rmnet0 down");
+ unsolListener.onInterfaceLinkStateChanged("rmnet0", false);
expectSoon(observer).interfaceLinkStateChanged("rmnet0", false);
- sendMessage("600 IFACE linkstate clat4 up");
- // Invalid group.
-
/**
* Bandwidth control events.
*/
- sendMessage("601 limit alert data rmnet_usb0");
+ unsolListener.onQuotaLimitReached("data", "rmnet_usb0");
expectSoon(observer).limitReached("data", "rmnet_usb0");
- sendMessage("601 invalid alert data rmnet0");
- // Invalid group.
-
- sendMessage("601 limit increased data rmnet0");
- // Invalid event.
-
-
/**
* Interface class activity.
*/
-
- sendMessage("613 IfaceClass active 1 1234 10012");
+ unsolListener.onInterfaceClassActivityChanged(true, 1, 1234, 0);
expectSoon(observer).interfaceClassDataActivityChanged("1", true, 1234);
- sendMessage("613 IfaceClass idle 9 5678");
+ unsolListener.onInterfaceClassActivityChanged(false, 9, 5678, 0);
expectSoon(observer).interfaceClassDataActivityChanged("9", false, 5678);
- sendMessage("613 IfaceClass reallyactive 9 4321");
+ unsolListener.onInterfaceClassActivityChanged(false, 9, 4321, 0);
expectSoon(observer).interfaceClassDataActivityChanged("9", false, 4321);
- sendMessage("613 InterfaceClass reallyactive 1");
- // Invalid group.
-
-
/**
* IP address changes.
*/
- sendMessage("614 Address updated fe80::1/64 wlan0 128 253");
+ unsolListener.onInterfaceAddressUpdated("fe80::1/64", "wlan0", 128, 253);
expectSoon(observer).addressUpdated("wlan0", new LinkAddress("fe80::1/64", 128, 253));
- // There is no "added", so we take this as "removed".
- sendMessage("614 Address added fe80::1/64 wlan0 128 253");
+ unsolListener.onInterfaceAddressRemoved("fe80::1/64", "wlan0", 128, 253);
expectSoon(observer).addressRemoved("wlan0", new LinkAddress("fe80::1/64", 128, 253));
- sendMessage("614 Address removed 2001:db8::1/64 wlan0 1 0");
+ unsolListener.onInterfaceAddressRemoved("2001:db8::1/64", "wlan0", 1, 0);
expectSoon(observer).addressRemoved("wlan0", new LinkAddress("2001:db8::1/64", 1, 0));
- sendMessage("614 Address removed 2001:db8::1/64 wlan0 1");
- // Not enough arguments.
-
- sendMessage("666 Address removed 2001:db8::1/64 wlan0 1 0");
- // Invalid code.
-
-
/**
* DNS information broadcasts.
*/
- sendMessage("615 DnsInfo servers rmnet_usb0 3600 2001:db8::1");
+ unsolListener.onInterfaceDnsServerInfo("rmnet_usb0", 3600, new String[]{"2001:db8::1"});
expectSoon(observer).interfaceDnsServerInfo("rmnet_usb0", 3600,
new String[]{"2001:db8::1"});
- sendMessage("615 DnsInfo servers wlan0 14400 2001:db8::1,2001:db8::2");
+ unsolListener.onInterfaceDnsServerInfo("wlan0", 14400,
+ new String[]{"2001:db8::1", "2001:db8::2"});
expectSoon(observer).interfaceDnsServerInfo("wlan0", 14400,
new String[]{"2001:db8::1", "2001:db8::2"});
// We don't check for negative lifetimes, only for parse errors.
- sendMessage("615 DnsInfo servers wlan0 -3600 ::1");
+ unsolListener.onInterfaceDnsServerInfo("wlan0", -3600, new String[]{"::1"});
expectSoon(observer).interfaceDnsServerInfo("wlan0", -3600,
new String[]{"::1"});
- sendMessage("615 DnsInfo servers wlan0 SIXHUNDRED ::1");
- // Non-numeric lifetime.
-
- sendMessage("615 DnsInfo servers wlan0 2001:db8::1");
- // Missing lifetime.
-
- sendMessage("615 DnsInfo servers wlan0 3600");
- // No servers.
-
- sendMessage("615 DnsInfo servers 3600 wlan0 2001:db8::1,2001:db8::2");
- // Non-numeric lifetime.
-
- sendMessage("615 DnsInfo wlan0 7200 2001:db8::1,2001:db8::2");
- // Invalid tokens.
-
- sendMessage("666 DnsInfo servers wlan0 5400 2001:db8::1");
- // Invalid code.
-
// No syntax checking on the addresses.
- sendMessage("615 DnsInfo servers wlan0 600 ,::,,foo,::1,");
+ unsolListener.onInterfaceDnsServerInfo("wlan0", 600,
+ new String[]{"", "::", "", "foo", "::1"});
expectSoon(observer).interfaceDnsServerInfo("wlan0", 600,
new String[]{"", "::", "", "foo", "::1"});