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 | |
| 19 | import android.content.Context; |
Lorenzo Colitti | 5ad421a | 2013-11-17 15:05:02 +0900 | [diff] [blame] | 20 | import android.net.LinkAddress; |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 21 | import android.net.LocalSocket; |
| 22 | import android.net.LocalServerSocket; |
| 23 | import android.os.Binder; |
| 24 | import android.test.AndroidTestCase; |
| 25 | import android.test.suitebuilder.annotation.LargeTest; |
| 26 | import com.android.server.net.BaseNetworkObserver; |
| 27 | |
| 28 | import static org.mockito.Mockito.doReturn; |
| 29 | import static org.mockito.Mockito.mock; |
| 30 | import static org.mockito.Mockito.reset; |
| 31 | import static org.mockito.Mockito.timeout; |
| 32 | import static org.mockito.Mockito.verify; |
| 33 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 34 | |
| 35 | import java.io.IOException; |
| 36 | import java.io.OutputStream; |
| 37 | |
| 38 | /** |
| 39 | * Tests for {@link NetworkManagementService}. |
| 40 | */ |
| 41 | @LargeTest |
| 42 | public class NetworkManagementServiceTest extends AndroidTestCase { |
| 43 | |
| 44 | private static final String SOCKET_NAME = "__test__NetworkManagementServiceTest"; |
| 45 | private NetworkManagementService mNMService; |
| 46 | private LocalServerSocket mServerSocket; |
| 47 | private LocalSocket mSocket; |
| 48 | private OutputStream mOutputStream; |
| 49 | |
| 50 | @Override |
| 51 | public void setUp() throws Exception { |
| 52 | super.setUp(); |
| 53 | // TODO: make this unnecessary. runtest might already make it unnecessary. |
| 54 | System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString()); |
| 55 | |
| 56 | // Set up a sheltered test environment. |
| 57 | BroadcastInterceptingContext context = new BroadcastInterceptingContext(getContext()); |
| 58 | mServerSocket = new LocalServerSocket(SOCKET_NAME); |
| 59 | |
| 60 | // Start the service and wait until it connects to our socket. |
| 61 | mNMService = NetworkManagementService.create(context, SOCKET_NAME); |
| 62 | mSocket = mServerSocket.accept(); |
| 63 | mOutputStream = mSocket.getOutputStream(); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public void tearDown() throws Exception { |
| 68 | if (mSocket != null) mSocket.close(); |
| 69 | if (mServerSocket != null) mServerSocket.close(); |
| 70 | super.tearDown(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Sends a message on the netd socket and gives the events some time to make it back. |
| 75 | */ |
| 76 | private void sendMessage(String message) throws IOException { |
| 77 | // Strings are null-terminated, so add "\0" at the end. |
| 78 | mOutputStream.write((message + "\0").getBytes()); |
| 79 | } |
| 80 | |
| 81 | private static <T> T expectSoon(T mock) { |
| 82 | return verify(mock, timeout(100)); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Tests that network observers work properly. |
| 87 | */ |
| 88 | public void testNetworkObservers() throws Exception { |
| 89 | BaseNetworkObserver observer = mock(BaseNetworkObserver.class); |
| 90 | doReturn(new Binder()).when(observer).asBinder(); // Used by registerObserver. |
| 91 | mNMService.registerObserver(observer); |
| 92 | |
| 93 | // Forget everything that happened to the mock so far, so we can explicitly verify |
| 94 | // everything that happens and does not happen to it from now on. |
| 95 | reset(observer); |
| 96 | |
| 97 | // Now send NetworkManagementService messages and ensure that the observer methods are |
| 98 | // called. After every valid message we expect a callback soon after; to ensure that |
| 99 | // invalid messages don't cause any callbacks, we call verifyNoMoreInteractions at the end. |
| 100 | |
| 101 | /** |
| 102 | * Interface changes. |
| 103 | */ |
| 104 | sendMessage("600 Iface added rmnet12"); |
| 105 | expectSoon(observer).interfaceAdded("rmnet12"); |
| 106 | |
| 107 | sendMessage("600 Iface removed eth1"); |
| 108 | expectSoon(observer).interfaceRemoved("eth1"); |
| 109 | |
| 110 | sendMessage("607 Iface removed eth1"); |
| 111 | // Invalid code. |
| 112 | |
| 113 | sendMessage("600 Iface borked lo down"); |
| 114 | // Invalid event. |
| 115 | |
| 116 | sendMessage("600 Iface changed clat4 up again"); |
| 117 | // Extra tokens. |
| 118 | |
| 119 | sendMessage("600 Iface changed clat4 up"); |
| 120 | expectSoon(observer).interfaceStatusChanged("clat4", true); |
| 121 | |
| 122 | sendMessage("600 Iface linkstate rmnet0 down"); |
| 123 | expectSoon(observer).interfaceLinkStateChanged("rmnet0", false); |
| 124 | |
| 125 | sendMessage("600 IFACE linkstate clat4 up"); |
| 126 | // Invalid group. |
| 127 | |
| 128 | /** |
| 129 | * Bandwidth control events. |
| 130 | */ |
| 131 | sendMessage("601 limit alert data rmnet_usb0"); |
| 132 | expectSoon(observer).limitReached("data", "rmnet_usb0"); |
| 133 | |
| 134 | sendMessage("601 invalid alert data rmnet0"); |
| 135 | // Invalid group. |
| 136 | |
| 137 | sendMessage("601 limit increased data rmnet0"); |
| 138 | // Invalid event. |
| 139 | |
| 140 | |
| 141 | /** |
| 142 | * Interface class activity. |
| 143 | */ |
Ashish Sharma | a3f828b | 2014-03-18 16:38:58 -0700 | [diff] [blame] | 144 | |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 145 | sendMessage("613 IfaceClass active rmnet0"); |
Ashish Sharma | a3f828b | 2014-03-18 16:38:58 -0700 | [diff] [blame] | 146 | expectSoon(observer).interfaceClassDataActivityChanged("rmnet0", true, 0); |
| 147 | |
| 148 | sendMessage("613 IfaceClass active rmnet0 1234"); |
| 149 | expectSoon(observer).interfaceClassDataActivityChanged("rmnet0", true, 1234); |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 150 | |
| 151 | sendMessage("613 IfaceClass idle eth0"); |
Ashish Sharma | a3f828b | 2014-03-18 16:38:58 -0700 | [diff] [blame] | 152 | expectSoon(observer).interfaceClassDataActivityChanged("eth0", false, 0); |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 153 | |
Ashish Sharma | a3f828b | 2014-03-18 16:38:58 -0700 | [diff] [blame] | 154 | sendMessage("613 IfaceClass idle eth0 1234"); |
| 155 | expectSoon(observer).interfaceClassDataActivityChanged("eth0", false, 1234); |
| 156 | |
| 157 | sendMessage("613 IfaceClass reallyactive rmnet0 1234"); |
| 158 | expectSoon(observer).interfaceClassDataActivityChanged("rmnet0", false, 1234); |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 159 | |
| 160 | sendMessage("613 InterfaceClass reallyactive rmnet0"); |
| 161 | // Invalid group. |
| 162 | |
| 163 | |
| 164 | /** |
| 165 | * IP address changes. |
| 166 | */ |
| 167 | sendMessage("614 Address updated fe80::1/64 wlan0 128 253"); |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 168 | expectSoon(observer).addressUpdated("wlan0", new LinkAddress("fe80::1/64", 128, 253)); |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 169 | |
Lorenzo Colitti | 5ad421a | 2013-11-17 15:05:02 +0900 | [diff] [blame] | 170 | // There is no "added", so we take this as "removed". |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 171 | sendMessage("614 Address added fe80::1/64 wlan0 128 253"); |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 172 | expectSoon(observer).addressRemoved("wlan0", new LinkAddress("fe80::1/64", 128, 253)); |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 173 | |
| 174 | sendMessage("614 Address removed 2001:db8::1/64 wlan0 1 0"); |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 175 | expectSoon(observer).addressRemoved("wlan0", new LinkAddress("2001:db8::1/64", 1, 0)); |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 176 | |
Lorenzo Colitti | a9626c1 | 2013-11-04 17:44:09 +0900 | [diff] [blame] | 177 | sendMessage("614 Address removed 2001:db8::1/64 wlan0 1"); |
| 178 | // Not enough arguments. |
| 179 | |
Lorenzo Colitti | 5ad421a | 2013-11-17 15:05:02 +0900 | [diff] [blame] | 180 | sendMessage("666 Address removed 2001:db8::1/64 wlan0 1 0"); |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 181 | // Invalid code. |
| 182 | |
Lorenzo Colitti | 5ae4a53 | 2013-10-31 11:59:46 +0900 | [diff] [blame] | 183 | |
| 184 | /** |
| 185 | * DNS information broadcasts. |
| 186 | */ |
| 187 | sendMessage("615 DnsInfo servers rmnet_usb0 3600 2001:db8::1"); |
| 188 | expectSoon(observer).interfaceDnsServerInfo("rmnet_usb0", 3600, |
| 189 | new String[]{"2001:db8::1"}); |
| 190 | |
| 191 | sendMessage("615 DnsInfo servers wlan0 14400 2001:db8::1,2001:db8::2"); |
| 192 | expectSoon(observer).interfaceDnsServerInfo("wlan0", 14400, |
| 193 | new String[]{"2001:db8::1", "2001:db8::2"}); |
| 194 | |
| 195 | // We don't check for negative lifetimes, only for parse errors. |
| 196 | sendMessage("615 DnsInfo servers wlan0 -3600 ::1"); |
| 197 | expectSoon(observer).interfaceDnsServerInfo("wlan0", -3600, |
| 198 | new String[]{"::1"}); |
| 199 | |
| 200 | sendMessage("615 DnsInfo servers wlan0 SIXHUNDRED ::1"); |
| 201 | // Non-numeric lifetime. |
| 202 | |
| 203 | sendMessage("615 DnsInfo servers wlan0 2001:db8::1"); |
| 204 | // Missing lifetime. |
| 205 | |
| 206 | sendMessage("615 DnsInfo servers wlan0 3600"); |
| 207 | // No servers. |
| 208 | |
| 209 | sendMessage("615 DnsInfo servers 3600 wlan0 2001:db8::1,2001:db8::2"); |
| 210 | // Non-numeric lifetime. |
| 211 | |
| 212 | sendMessage("615 DnsInfo wlan0 7200 2001:db8::1,2001:db8::2"); |
| 213 | // Invalid tokens. |
| 214 | |
| 215 | sendMessage("666 DnsInfo servers wlan0 5400 2001:db8::1"); |
| 216 | // Invalid code. |
| 217 | |
| 218 | // No syntax checking on the addresses. |
| 219 | sendMessage("615 DnsInfo servers wlan0 600 ,::,,foo,::1,"); |
| 220 | expectSoon(observer).interfaceDnsServerInfo("wlan0", 600, |
| 221 | new String[]{"", "::", "", "foo", "::1"}); |
| 222 | |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 223 | // Make sure nothing else was called. |
| 224 | verifyNoMoreInteractions(observer); |
| 225 | } |
| 226 | } |