Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [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 | |
Erik Kline | f851d6d | 2015-04-20 16:03:48 +0900 | [diff] [blame] | 19 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 20 | import static android.net.ConnectivityManager.TYPE_MOBILE; |
| 21 | import static android.net.ConnectivityManager.TYPE_WIFI; |
| 22 | import static android.net.ConnectivityManager.getNetworkTypeName; |
Jeff Sharkey | 3671b1e | 2013-01-31 17:22:26 -0800 | [diff] [blame] | 23 | import static org.mockito.Matchers.anyInt; |
| 24 | import static org.mockito.Matchers.eq; |
| 25 | import static org.mockito.Matchers.isA; |
| 26 | import static org.mockito.Mockito.doNothing; |
| 27 | import static org.mockito.Mockito.doReturn; |
| 28 | import static org.mockito.Mockito.doThrow; |
| 29 | import static org.mockito.Mockito.mock; |
| 30 | import static org.mockito.Mockito.reset; |
| 31 | import static org.mockito.Mockito.verify; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 32 | |
| 33 | import android.content.Context; |
| 34 | import android.net.INetworkPolicyManager; |
| 35 | import android.net.INetworkStatsService; |
| 36 | import android.net.LinkProperties; |
| 37 | import android.net.NetworkConfig; |
| 38 | import android.net.NetworkInfo; |
| 39 | import android.net.NetworkInfo.DetailedState; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 40 | import android.net.RouteInfo; |
| 41 | import android.os.Handler; |
| 42 | import android.os.INetworkManagementService; |
| 43 | import android.test.AndroidTestCase; |
| 44 | import android.test.suitebuilder.annotation.LargeTest; |
| 45 | import android.util.Log; |
| 46 | import android.util.LogPrinter; |
| 47 | |
Jeff Sharkey | 3671b1e | 2013-01-31 17:22:26 -0800 | [diff] [blame] | 48 | import org.mockito.ArgumentCaptor; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 49 | |
| 50 | import java.net.InetAddress; |
| 51 | import java.util.concurrent.Future; |
| 52 | |
| 53 | /** |
| 54 | * Tests for {@link ConnectivityService}. |
| 55 | */ |
| 56 | @LargeTest |
| 57 | public class ConnectivityServiceTest extends AndroidTestCase { |
| 58 | private static final String TAG = "ConnectivityServiceTest"; |
| 59 | |
| 60 | private static final String MOBILE_IFACE = "rmnet3"; |
| 61 | private static final String WIFI_IFACE = "wlan6"; |
| 62 | |
Lorenzo Colitti | e167135 | 2013-03-08 12:30:44 -0800 | [diff] [blame] | 63 | private static final RouteInfo MOBILE_ROUTE_V4 = RouteInfo.makeHostRoute(parse("10.0.0.33"), |
| 64 | MOBILE_IFACE); |
| 65 | private static final RouteInfo MOBILE_ROUTE_V6 = RouteInfo.makeHostRoute(parse("fd00::33"), |
| 66 | MOBILE_IFACE); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 67 | |
Lorenzo Colitti | e167135 | 2013-03-08 12:30:44 -0800 | [diff] [blame] | 68 | private static final RouteInfo WIFI_ROUTE_V4 = RouteInfo.makeHostRoute(parse("192.168.0.66"), |
| 69 | parse("192.168.0.1"), |
| 70 | WIFI_IFACE); |
| 71 | private static final RouteInfo WIFI_ROUTE_V6 = RouteInfo.makeHostRoute(parse("fd00::66"), |
| 72 | parse("fd00::"), |
| 73 | WIFI_IFACE); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 74 | |
| 75 | private INetworkManagementService mNetManager; |
| 76 | private INetworkStatsService mStatsService; |
| 77 | private INetworkPolicyManager mPolicyService; |
Robert Greenwalt | 6831f1d | 2014-07-27 12:06:40 -0700 | [diff] [blame] | 78 | // private ConnectivityService.NetworkFactory mNetFactory; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 79 | |
| 80 | private BroadcastInterceptingContext mServiceContext; |
| 81 | private ConnectivityService mService; |
| 82 | |
Robert Greenwalt | 6831f1d | 2014-07-27 12:06:40 -0700 | [diff] [blame] | 83 | // TODO: rework with network factory |
| 84 | // private MockNetwork mMobile; |
| 85 | // private MockNetwork mWifi; |
| 86 | // |
| 87 | // private Handler mTrackerHandler; |
| 88 | // |
| 89 | // private static class MockNetwork { |
| 90 | // public NetworkStateTracker tracker; |
| 91 | // public NetworkInfo info; |
| 92 | // public LinkProperties link; |
| 93 | // |
| 94 | // public MockNetwork(int type) { |
| 95 | // tracker = mock(NetworkStateTracker.class); |
| 96 | // info = new NetworkInfo(type, -1, getNetworkTypeName(type), null); |
| 97 | // link = new LinkProperties(); |
| 98 | // } |
| 99 | // |
| 100 | // public void doReturnDefaults() { |
| 101 | // // TODO: eventually CS should make defensive copies |
| 102 | // doReturn(new NetworkInfo(info)).when(tracker).getNetworkInfo(); |
| 103 | // doReturn(new LinkProperties(link)).when(tracker).getLinkProperties(); |
| 104 | // |
| 105 | // // fallback to default TCP buffers |
| 106 | // doReturn("").when(tracker).getTcpBufferSizesPropName(); |
| 107 | // } |
| 108 | // } |
| 109 | // |
| 110 | // @Override |
| 111 | // public void setUp() throws Exception { |
| 112 | // super.setUp(); |
| 113 | // |
| 114 | // mServiceContext = new BroadcastInterceptingContext(getContext()); |
| 115 | // |
| 116 | // mNetManager = mock(INetworkManagementService.class); |
| 117 | // mStatsService = mock(INetworkStatsService.class); |
| 118 | // mPolicyService = mock(INetworkPolicyManager.class); |
| 119 | // mNetFactory = mock(ConnectivityService.NetworkFactory.class); |
| 120 | // |
| 121 | // mMobile = new MockNetwork(TYPE_MOBILE); |
| 122 | // mWifi = new MockNetwork(TYPE_WIFI); |
| 123 | // |
| 124 | // // omit most network trackers |
| 125 | // doThrow(new IllegalArgumentException("Not supported in test environment")) |
| 126 | // .when(mNetFactory).createTracker(anyInt(), isA(NetworkConfig.class)); |
| 127 | // |
| 128 | // doReturn(mMobile.tracker) |
| 129 | // .when(mNetFactory).createTracker(eq(TYPE_MOBILE), isA(NetworkConfig.class)); |
| 130 | // doReturn(mWifi.tracker) |
| 131 | // .when(mNetFactory).createTracker(eq(TYPE_WIFI), isA(NetworkConfig.class)); |
| 132 | // |
| 133 | // final ArgumentCaptor<Handler> trackerHandler = ArgumentCaptor.forClass(Handler.class); |
| 134 | // doNothing().when(mMobile.tracker) |
| 135 | // .startMonitoring(isA(Context.class), trackerHandler.capture()); |
| 136 | // |
| 137 | // mService = new ConnectivityService( |
| 138 | // mServiceContext, mNetManager, mStatsService, mPolicyService); |
| 139 | // mService.systemReady(); |
| 140 | // |
| 141 | // mTrackerHandler = trackerHandler.getValue(); |
| 142 | // mTrackerHandler.getLooper().setMessageLogging(new LogPrinter(Log.INFO, TAG)); |
| 143 | // } |
| 144 | // |
| 145 | // @Override |
| 146 | // public void tearDown() throws Exception { |
| 147 | // super.tearDown(); |
| 148 | // } |
| 149 | // |
| 150 | // public void testMobileConnectedAddedRoutes() throws Exception { |
| 151 | // Future<?> nextConnBroadcast; |
| 152 | // |
| 153 | // // bring up mobile network |
| 154 | // mMobile.info.setDetailedState(DetailedState.CONNECTED, null, null); |
| 155 | // mMobile.link.setInterfaceName(MOBILE_IFACE); |
| 156 | // mMobile.link.addRoute(MOBILE_ROUTE_V4); |
| 157 | // mMobile.link.addRoute(MOBILE_ROUTE_V6); |
| 158 | // mMobile.doReturnDefaults(); |
| 159 | // |
Erik Kline | f851d6d | 2015-04-20 16:03:48 +0900 | [diff] [blame] | 160 | // nextConnBroadcast = mServiceContext.nextBroadcastIntent(CONNECTIVITY_ACTION); |
Robert Greenwalt | 6831f1d | 2014-07-27 12:06:40 -0700 | [diff] [blame] | 161 | // mTrackerHandler.obtainMessage(EVENT_STATE_CHANGED, mMobile.info).sendToTarget(); |
| 162 | // nextConnBroadcast.get(); |
| 163 | // |
| 164 | // // verify that both routes were added |
| 165 | // int mobileNetId = mMobile.tracker.getNetwork().netId; |
| 166 | // verify(mNetManager).addRoute(eq(mobileNetId), eq(MOBILE_ROUTE_V4)); |
| 167 | // verify(mNetManager).addRoute(eq(mobileNetId), eq(MOBILE_ROUTE_V6)); |
| 168 | // } |
| 169 | // |
| 170 | // public void testMobileWifiHandoff() throws Exception { |
| 171 | // Future<?> nextConnBroadcast; |
| 172 | // |
| 173 | // // bring up mobile network |
| 174 | // mMobile.info.setDetailedState(DetailedState.CONNECTED, null, null); |
| 175 | // mMobile.link.setInterfaceName(MOBILE_IFACE); |
| 176 | // mMobile.link.addRoute(MOBILE_ROUTE_V4); |
| 177 | // mMobile.link.addRoute(MOBILE_ROUTE_V6); |
| 178 | // mMobile.doReturnDefaults(); |
| 179 | // |
Erik Kline | f851d6d | 2015-04-20 16:03:48 +0900 | [diff] [blame] | 180 | // nextConnBroadcast = mServiceContext.nextBroadcastIntent(CONNECTIVITY_ACTION); |
Robert Greenwalt | 6831f1d | 2014-07-27 12:06:40 -0700 | [diff] [blame] | 181 | // mTrackerHandler.obtainMessage(EVENT_STATE_CHANGED, mMobile.info).sendToTarget(); |
| 182 | // nextConnBroadcast.get(); |
| 183 | // |
| 184 | // reset(mNetManager); |
| 185 | // |
| 186 | // // now bring up wifi network |
| 187 | // mWifi.info.setDetailedState(DetailedState.CONNECTED, null, null); |
| 188 | // mWifi.link.setInterfaceName(WIFI_IFACE); |
| 189 | // mWifi.link.addRoute(WIFI_ROUTE_V4); |
| 190 | // mWifi.link.addRoute(WIFI_ROUTE_V6); |
| 191 | // mWifi.doReturnDefaults(); |
| 192 | // |
| 193 | // // expect that mobile will be torn down |
| 194 | // doReturn(true).when(mMobile.tracker).teardown(); |
| 195 | // |
Erik Kline | f851d6d | 2015-04-20 16:03:48 +0900 | [diff] [blame] | 196 | // nextConnBroadcast = mServiceContext.nextBroadcastIntent(CONNECTIVITY_ACTION); |
Robert Greenwalt | 6831f1d | 2014-07-27 12:06:40 -0700 | [diff] [blame] | 197 | // mTrackerHandler.obtainMessage(EVENT_STATE_CHANGED, mWifi.info).sendToTarget(); |
| 198 | // nextConnBroadcast.get(); |
| 199 | // |
| 200 | // // verify that wifi routes added, and teardown requested |
| 201 | // int wifiNetId = mWifi.tracker.getNetwork().netId; |
| 202 | // verify(mNetManager).addRoute(eq(wifiNetId), eq(WIFI_ROUTE_V4)); |
| 203 | // verify(mNetManager).addRoute(eq(wifiNetId), eq(WIFI_ROUTE_V6)); |
| 204 | // verify(mMobile.tracker).teardown(); |
| 205 | // |
| 206 | // int mobileNetId = mMobile.tracker.getNetwork().netId; |
| 207 | // |
| 208 | // reset(mNetManager, mMobile.tracker); |
| 209 | // |
| 210 | // // tear down mobile network, as requested |
| 211 | // mMobile.info.setDetailedState(DetailedState.DISCONNECTED, null, null); |
| 212 | // mMobile.link.clear(); |
| 213 | // mMobile.doReturnDefaults(); |
| 214 | // |
Erik Kline | f851d6d | 2015-04-20 16:03:48 +0900 | [diff] [blame] | 215 | // nextConnBroadcast = mServiceContext.nextBroadcastIntent(CONNECTIVITY_ACTION); |
Robert Greenwalt | 6831f1d | 2014-07-27 12:06:40 -0700 | [diff] [blame] | 216 | // mTrackerHandler.obtainMessage(EVENT_STATE_CHANGED, mMobile.info).sendToTarget(); |
| 217 | // nextConnBroadcast.get(); |
| 218 | // |
| 219 | // verify(mNetManager).removeRoute(eq(mobileNetId), eq(MOBILE_ROUTE_V4)); |
| 220 | // verify(mNetManager).removeRoute(eq(mobileNetId), eq(MOBILE_ROUTE_V6)); |
| 221 | // |
| 222 | // } |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 223 | |
| 224 | private static InetAddress parse(String addr) { |
| 225 | return InetAddress.parseNumericAddress(addr); |
| 226 | } |
Robert Greenwalt | 6831f1d | 2014-07-27 12:06:40 -0700 | [diff] [blame] | 227 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 228 | } |