Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 19 | import static org.mockito.Mockito.doReturn; |
| 20 | import static org.mockito.Mockito.mock; |
| 21 | import static org.mockito.Mockito.reset; |
| 22 | import static org.mockito.Mockito.timeout; |
| 23 | import static org.mockito.Mockito.verify; |
| 24 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 25 | |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 26 | import android.content.Context; |
| 27 | import android.net.INetd; |
| 28 | import android.net.LinkAddress; |
| 29 | import android.net.LocalSocket; |
| 30 | import android.net.LocalServerSocket; |
| 31 | import android.os.BatteryStats; |
| 32 | import android.os.Binder; |
| 33 | import android.os.IBinder; |
| 34 | import android.support.test.runner.AndroidJUnit4; |
| 35 | import android.test.suitebuilder.annotation.SmallTest; |
| 36 | |
| 37 | import com.android.internal.app.IBatteryStats; |
| 38 | import com.android.server.NetworkManagementService.SystemServices; |
| 39 | import com.android.server.net.BaseNetworkObserver; |
| 40 | |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 41 | import java.io.IOException; |
| 42 | import java.io.OutputStream; |
| 43 | |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 44 | import org.junit.After; |
| 45 | import org.junit.Before; |
| 46 | import org.junit.Test; |
| 47 | import org.junit.runner.RunWith; |
| 48 | import org.mockito.Mock; |
| 49 | import org.mockito.MockitoAnnotations; |
| 50 | |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 51 | /** |
| 52 | * Tests for {@link NetworkManagementService}. |
| 53 | */ |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 54 | @RunWith(AndroidJUnit4.class) |
| 55 | @SmallTest |
| 56 | public class NetworkManagementServiceTest { |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 57 | |
| 58 | private static final String SOCKET_NAME = "__test__NetworkManagementServiceTest"; |
| 59 | private NetworkManagementService mNMService; |
| 60 | private LocalServerSocket mServerSocket; |
| 61 | private LocalSocket mSocket; |
| 62 | private OutputStream mOutputStream; |
| 63 | |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 64 | @Mock private Context mContext; |
| 65 | @Mock private IBatteryStats.Stub mBatteryStatsService; |
| 66 | @Mock private INetd.Stub mNetdService; |
| 67 | |
| 68 | private final SystemServices mServices = new SystemServices() { |
| 69 | @Override |
| 70 | public IBinder getService(String name) { |
| 71 | switch (name) { |
| 72 | case BatteryStats.SERVICE_NAME: |
| 73 | return mBatteryStatsService; |
| 74 | default: |
| 75 | throw new UnsupportedOperationException("Unknown service " + name); |
| 76 | } |
| 77 | } |
| 78 | @Override |
| 79 | public void registerLocalService(NetworkManagementInternal nmi) { |
| 80 | } |
| 81 | @Override |
| 82 | public INetd getNetd() { |
| 83 | return mNetdService; |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | @Before |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 88 | public void setUp() throws Exception { |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 89 | MockitoAnnotations.initMocks(this); |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 90 | |
| 91 | // Set up a sheltered test environment. |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 92 | mServerSocket = new LocalServerSocket(SOCKET_NAME); |
| 93 | |
| 94 | // Start the service and wait until it connects to our socket. |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 95 | mNMService = NetworkManagementService.create(mContext, SOCKET_NAME, mServices); |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 96 | mSocket = mServerSocket.accept(); |
| 97 | mOutputStream = mSocket.getOutputStream(); |
| 98 | } |
| 99 | |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 100 | @After |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 101 | public void tearDown() throws Exception { |
| 102 | if (mSocket != null) mSocket.close(); |
| 103 | if (mServerSocket != null) mServerSocket.close(); |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Sends a message on the netd socket and gives the events some time to make it back. |
| 108 | */ |
| 109 | private void sendMessage(String message) throws IOException { |
| 110 | // Strings are null-terminated, so add "\0" at the end. |
| 111 | mOutputStream.write((message + "\0").getBytes()); |
| 112 | } |
| 113 | |
| 114 | private static <T> T expectSoon(T mock) { |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 115 | return verify(mock, timeout(200)); |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Tests that network observers work properly. |
| 120 | */ |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 121 | @Test |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 122 | public void testNetworkObservers() throws Exception { |
| 123 | BaseNetworkObserver observer = mock(BaseNetworkObserver.class); |
| 124 | doReturn(new Binder()).when(observer).asBinder(); // Used by registerObserver. |
| 125 | mNMService.registerObserver(observer); |
| 126 | |
| 127 | // Forget everything that happened to the mock so far, so we can explicitly verify |
| 128 | // everything that happens and does not happen to it from now on. |
| 129 | reset(observer); |
| 130 | |
| 131 | // Now send NetworkManagementService messages and ensure that the observer methods are |
| 132 | // called. After every valid message we expect a callback soon after; to ensure that |
| 133 | // invalid messages don't cause any callbacks, we call verifyNoMoreInteractions at the end. |
| 134 | |
| 135 | /** |
| 136 | * Interface changes. |
| 137 | */ |
| 138 | sendMessage("600 Iface added rmnet12"); |
| 139 | expectSoon(observer).interfaceAdded("rmnet12"); |
| 140 | |
| 141 | sendMessage("600 Iface removed eth1"); |
| 142 | expectSoon(observer).interfaceRemoved("eth1"); |
| 143 | |
| 144 | sendMessage("607 Iface removed eth1"); |
| 145 | // Invalid code. |
| 146 | |
| 147 | sendMessage("600 Iface borked lo down"); |
| 148 | // Invalid event. |
| 149 | |
| 150 | sendMessage("600 Iface changed clat4 up again"); |
| 151 | // Extra tokens. |
| 152 | |
| 153 | sendMessage("600 Iface changed clat4 up"); |
| 154 | expectSoon(observer).interfaceStatusChanged("clat4", true); |
| 155 | |
| 156 | sendMessage("600 Iface linkstate rmnet0 down"); |
| 157 | expectSoon(observer).interfaceLinkStateChanged("rmnet0", false); |
| 158 | |
| 159 | sendMessage("600 IFACE linkstate clat4 up"); |
| 160 | // Invalid group. |
| 161 | |
| 162 | /** |
| 163 | * Bandwidth control events. |
| 164 | */ |
| 165 | sendMessage("601 limit alert data rmnet_usb0"); |
| 166 | expectSoon(observer).limitReached("data", "rmnet_usb0"); |
| 167 | |
| 168 | sendMessage("601 invalid alert data rmnet0"); |
| 169 | // Invalid group. |
| 170 | |
| 171 | sendMessage("601 limit increased data rmnet0"); |
| 172 | // Invalid event. |
| 173 | |
| 174 | |
| 175 | /** |
| 176 | * Interface class activity. |
| 177 | */ |
Ashish Sharma | a3f828b | 2014-03-18 16:38:58 -0700 | [diff] [blame] | 178 | |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 179 | sendMessage("613 IfaceClass active 1 1234 10012"); |
| 180 | expectSoon(observer).interfaceClassDataActivityChanged("1", true, 1234); |
Ashish Sharma | a3f828b | 2014-03-18 16:38:58 -0700 | [diff] [blame] | 181 | |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 182 | sendMessage("613 IfaceClass idle 9 5678"); |
| 183 | expectSoon(observer).interfaceClassDataActivityChanged("9", false, 5678); |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 184 | |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 185 | sendMessage("613 IfaceClass reallyactive 9 4321"); |
| 186 | expectSoon(observer).interfaceClassDataActivityChanged("9", false, 4321); |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 187 | |
Lorenzo Colitti | a086800 | 2017-07-11 02:29:28 +0900 | [diff] [blame] | 188 | sendMessage("613 InterfaceClass reallyactive 1"); |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 189 | // Invalid group. |
| 190 | |
| 191 | |
| 192 | /** |
| 193 | * IP address changes. |
| 194 | */ |
| 195 | sendMessage("614 Address updated fe80::1/64 wlan0 128 253"); |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 196 | expectSoon(observer).addressUpdated("wlan0", new LinkAddress("fe80::1/64", 128, 253)); |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 197 | |
Lorenzo Colitti | 5ad421a | 2013-11-17 15:05:02 +0900 | [diff] [blame] | 198 | // There is no "added", so we take this as "removed". |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 199 | sendMessage("614 Address added fe80::1/64 wlan0 128 253"); |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 200 | expectSoon(observer).addressRemoved("wlan0", new LinkAddress("fe80::1/64", 128, 253)); |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 201 | |
| 202 | sendMessage("614 Address removed 2001:db8::1/64 wlan0 1 0"); |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 203 | expectSoon(observer).addressRemoved("wlan0", new LinkAddress("2001:db8::1/64", 1, 0)); |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 204 | |
Lorenzo Colitti | a9626c1 | 2013-11-04 17:44:09 +0900 | [diff] [blame] | 205 | sendMessage("614 Address removed 2001:db8::1/64 wlan0 1"); |
| 206 | // Not enough arguments. |
| 207 | |
Lorenzo Colitti | 5ad421a | 2013-11-17 15:05:02 +0900 | [diff] [blame] | 208 | sendMessage("666 Address removed 2001:db8::1/64 wlan0 1 0"); |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 209 | // Invalid code. |
| 210 | |
Lorenzo Colitti | 5ae4a53 | 2013-10-31 11:59:46 +0900 | [diff] [blame] | 211 | |
| 212 | /** |
| 213 | * DNS information broadcasts. |
| 214 | */ |
| 215 | sendMessage("615 DnsInfo servers rmnet_usb0 3600 2001:db8::1"); |
| 216 | expectSoon(observer).interfaceDnsServerInfo("rmnet_usb0", 3600, |
| 217 | new String[]{"2001:db8::1"}); |
| 218 | |
| 219 | sendMessage("615 DnsInfo servers wlan0 14400 2001:db8::1,2001:db8::2"); |
| 220 | expectSoon(observer).interfaceDnsServerInfo("wlan0", 14400, |
| 221 | new String[]{"2001:db8::1", "2001:db8::2"}); |
| 222 | |
| 223 | // We don't check for negative lifetimes, only for parse errors. |
| 224 | sendMessage("615 DnsInfo servers wlan0 -3600 ::1"); |
| 225 | expectSoon(observer).interfaceDnsServerInfo("wlan0", -3600, |
| 226 | new String[]{"::1"}); |
| 227 | |
| 228 | sendMessage("615 DnsInfo servers wlan0 SIXHUNDRED ::1"); |
| 229 | // Non-numeric lifetime. |
| 230 | |
| 231 | sendMessage("615 DnsInfo servers wlan0 2001:db8::1"); |
| 232 | // Missing lifetime. |
| 233 | |
| 234 | sendMessage("615 DnsInfo servers wlan0 3600"); |
| 235 | // No servers. |
| 236 | |
| 237 | sendMessage("615 DnsInfo servers 3600 wlan0 2001:db8::1,2001:db8::2"); |
| 238 | // Non-numeric lifetime. |
| 239 | |
| 240 | sendMessage("615 DnsInfo wlan0 7200 2001:db8::1,2001:db8::2"); |
| 241 | // Invalid tokens. |
| 242 | |
| 243 | sendMessage("666 DnsInfo servers wlan0 5400 2001:db8::1"); |
| 244 | // Invalid code. |
| 245 | |
| 246 | // No syntax checking on the addresses. |
| 247 | sendMessage("615 DnsInfo servers wlan0 600 ,::,,foo,::1,"); |
| 248 | expectSoon(observer).interfaceDnsServerInfo("wlan0", 600, |
| 249 | new String[]{"", "::", "", "foo", "::1"}); |
| 250 | |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 251 | // Make sure nothing else was called. |
| 252 | verifyNoMoreInteractions(observer); |
| 253 | } |
| 254 | } |