Remi NGUYEN VAN | 95dc87e | 2019-06-13 16:12:02 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN; |
| 20 | import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; |
| 21 | import static android.net.NetworkCapabilities.TRANSPORT_ETHERNET; |
| 22 | import static android.net.NetworkCapabilities.TRANSPORT_VPN; |
| 23 | import static android.net.NetworkCapabilities.TRANSPORT_WIFI; |
| 24 | import static android.net.NetworkCapabilities.TRANSPORT_WIFI_AWARE; |
| 25 | |
| 26 | import static com.android.server.ConnectivityServiceTestUtilsKt.transportToLegacyType; |
| 27 | |
| 28 | import static junit.framework.Assert.assertTrue; |
| 29 | |
| 30 | import static org.junit.Assert.assertEquals; |
| 31 | import static org.junit.Assert.assertNotEquals; |
| 32 | |
| 33 | import android.content.Context; |
| 34 | import android.net.ConnectivityManager; |
| 35 | import android.net.LinkProperties; |
| 36 | import android.net.Network; |
| 37 | import android.net.NetworkAgent; |
| 38 | import android.net.NetworkCapabilities; |
Remi NGUYEN VAN | 95dc87e | 2019-06-13 16:12:02 +0900 | [diff] [blame] | 39 | import android.net.NetworkInfo; |
| 40 | import android.net.NetworkMisc; |
Lorenzo Colitti | 6654b08 | 2020-01-10 00:40:28 +0900 | [diff] [blame] | 41 | import android.net.NetworkProvider; |
Remi NGUYEN VAN | 95dc87e | 2019-06-13 16:12:02 +0900 | [diff] [blame] | 42 | import android.net.NetworkSpecifier; |
| 43 | import android.net.SocketKeepalive; |
| 44 | import android.net.UidRange; |
| 45 | import android.os.ConditionVariable; |
| 46 | import android.os.HandlerThread; |
| 47 | import android.os.Message; |
| 48 | import android.util.Log; |
| 49 | |
| 50 | import com.android.server.connectivity.ConnectivityConstants; |
| 51 | import com.android.testutils.HandlerUtilsKt; |
| 52 | import com.android.testutils.TestableNetworkCallback; |
| 53 | |
| 54 | import java.util.Set; |
| 55 | |
| 56 | public class NetworkAgentWrapper implements TestableNetworkCallback.HasNetwork { |
| 57 | private final NetworkInfo mNetworkInfo; |
| 58 | private final NetworkCapabilities mNetworkCapabilities; |
| 59 | private final HandlerThread mHandlerThread; |
| 60 | private final Context mContext; |
| 61 | private final String mLogTag; |
| 62 | |
| 63 | private final ConditionVariable mDisconnected = new ConditionVariable(); |
| 64 | private final ConditionVariable mPreventReconnectReceived = new ConditionVariable(); |
| 65 | private int mScore; |
| 66 | private NetworkAgent mNetworkAgent; |
| 67 | private int mStartKeepaliveError = SocketKeepalive.ERROR_UNSUPPORTED; |
| 68 | private int mStopKeepaliveError = SocketKeepalive.NO_KEEPALIVE; |
| 69 | private Integer mExpectedKeepaliveSlot = null; |
| 70 | |
| 71 | public NetworkAgentWrapper(int transport, LinkProperties linkProperties, Context context) |
| 72 | throws Exception { |
| 73 | final int type = transportToLegacyType(transport); |
| 74 | final String typeName = ConnectivityManager.getNetworkTypeName(type); |
| 75 | mNetworkInfo = new NetworkInfo(type, 0, typeName, "Mock"); |
| 76 | mNetworkCapabilities = new NetworkCapabilities(); |
| 77 | mNetworkCapabilities.addTransportType(transport); |
| 78 | switch (transport) { |
| 79 | case TRANSPORT_ETHERNET: |
| 80 | mScore = 70; |
| 81 | break; |
| 82 | case TRANSPORT_WIFI: |
| 83 | mScore = 60; |
| 84 | break; |
| 85 | case TRANSPORT_CELLULAR: |
| 86 | mScore = 50; |
| 87 | break; |
| 88 | case TRANSPORT_WIFI_AWARE: |
| 89 | mScore = 20; |
| 90 | break; |
| 91 | case TRANSPORT_VPN: |
| 92 | mNetworkCapabilities.removeCapability(NET_CAPABILITY_NOT_VPN); |
| 93 | mScore = ConnectivityConstants.VPN_DEFAULT_SCORE; |
| 94 | break; |
| 95 | default: |
| 96 | throw new UnsupportedOperationException("unimplemented network type"); |
| 97 | } |
| 98 | mContext = context; |
| 99 | mLogTag = "Mock-" + typeName; |
| 100 | mHandlerThread = new HandlerThread(mLogTag); |
| 101 | mHandlerThread.start(); |
| 102 | |
| 103 | mNetworkAgent = makeNetworkAgent(linkProperties); |
| 104 | } |
| 105 | |
| 106 | protected InstrumentedNetworkAgent makeNetworkAgent(LinkProperties linkProperties) |
| 107 | throws Exception { |
| 108 | return new InstrumentedNetworkAgent(this, linkProperties); |
| 109 | } |
| 110 | |
| 111 | public static class InstrumentedNetworkAgent extends NetworkAgent { |
| 112 | private final NetworkAgentWrapper mWrapper; |
| 113 | |
| 114 | public InstrumentedNetworkAgent(NetworkAgentWrapper wrapper, LinkProperties lp) { |
| 115 | super(wrapper.mHandlerThread.getLooper(), wrapper.mContext, wrapper.mLogTag, |
| 116 | wrapper.mNetworkInfo, wrapper.mNetworkCapabilities, lp, wrapper.mScore, |
Lorenzo Colitti | 6654b08 | 2020-01-10 00:40:28 +0900 | [diff] [blame] | 117 | new NetworkMisc(), NetworkProvider.ID_NONE); |
Remi NGUYEN VAN | 95dc87e | 2019-06-13 16:12:02 +0900 | [diff] [blame] | 118 | mWrapper = wrapper; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void unwanted() { |
| 123 | mWrapper.mDisconnected.open(); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void startSocketKeepalive(Message msg) { |
| 128 | int slot = msg.arg1; |
| 129 | if (mWrapper.mExpectedKeepaliveSlot != null) { |
| 130 | assertEquals((int) mWrapper.mExpectedKeepaliveSlot, slot); |
| 131 | } |
| 132 | onSocketKeepaliveEvent(slot, mWrapper.mStartKeepaliveError); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void stopSocketKeepalive(Message msg) { |
| 137 | onSocketKeepaliveEvent(msg.arg1, mWrapper.mStopKeepaliveError); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | protected void preventAutomaticReconnect() { |
| 142 | mWrapper.mPreventReconnectReceived.open(); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | protected void addKeepalivePacketFilter(Message msg) { |
| 147 | Log.i(mWrapper.mLogTag, "Add keepalive packet filter."); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | protected void removeKeepalivePacketFilter(Message msg) { |
| 152 | Log.i(mWrapper.mLogTag, "Remove keepalive packet filter."); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | public void adjustScore(int change) { |
| 157 | mScore += change; |
| 158 | mNetworkAgent.sendNetworkScore(mScore); |
| 159 | } |
| 160 | |
| 161 | public int getScore() { |
| 162 | return mScore; |
| 163 | } |
| 164 | |
| 165 | public void explicitlySelected(boolean explicitlySelected, boolean acceptUnvalidated) { |
| 166 | mNetworkAgent.explicitlySelected(explicitlySelected, acceptUnvalidated); |
| 167 | } |
| 168 | |
| 169 | public void addCapability(int capability) { |
| 170 | mNetworkCapabilities.addCapability(capability); |
| 171 | mNetworkAgent.sendNetworkCapabilities(mNetworkCapabilities); |
| 172 | } |
| 173 | |
| 174 | public void removeCapability(int capability) { |
| 175 | mNetworkCapabilities.removeCapability(capability); |
| 176 | mNetworkAgent.sendNetworkCapabilities(mNetworkCapabilities); |
| 177 | } |
| 178 | |
| 179 | public void setUids(Set<UidRange> uids) { |
| 180 | mNetworkCapabilities.setUids(uids); |
| 181 | mNetworkAgent.sendNetworkCapabilities(mNetworkCapabilities); |
| 182 | } |
| 183 | |
| 184 | public void setSignalStrength(int signalStrength) { |
| 185 | mNetworkCapabilities.setSignalStrength(signalStrength); |
| 186 | mNetworkAgent.sendNetworkCapabilities(mNetworkCapabilities); |
| 187 | } |
| 188 | |
| 189 | public void setNetworkSpecifier(NetworkSpecifier networkSpecifier) { |
| 190 | mNetworkCapabilities.setNetworkSpecifier(networkSpecifier); |
| 191 | mNetworkAgent.sendNetworkCapabilities(mNetworkCapabilities); |
| 192 | } |
| 193 | |
| 194 | public void setNetworkCapabilities(NetworkCapabilities nc, boolean sendToConnectivityService) { |
| 195 | mNetworkCapabilities.set(nc); |
| 196 | if (sendToConnectivityService) { |
| 197 | mNetworkAgent.sendNetworkCapabilities(mNetworkCapabilities); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | public void connect() { |
| 202 | assertNotEquals("MockNetworkAgents can only be connected once", |
| 203 | getNetworkInfo().getDetailedState(), NetworkInfo.DetailedState.CONNECTED); |
| 204 | mNetworkInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTED, null, null); |
| 205 | mNetworkAgent.sendNetworkInfo(mNetworkInfo); |
| 206 | } |
| 207 | |
| 208 | public void suspend() { |
| 209 | mNetworkInfo.setDetailedState(NetworkInfo.DetailedState.SUSPENDED, null, null); |
| 210 | mNetworkAgent.sendNetworkInfo(mNetworkInfo); |
| 211 | } |
| 212 | |
| 213 | public void resume() { |
| 214 | mNetworkInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTED, null, null); |
| 215 | mNetworkAgent.sendNetworkInfo(mNetworkInfo); |
| 216 | } |
| 217 | |
| 218 | public void disconnect() { |
| 219 | mNetworkInfo.setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, null, null); |
| 220 | mNetworkAgent.sendNetworkInfo(mNetworkInfo); |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public Network getNetwork() { |
| 225 | return new Network(mNetworkAgent.netId); |
| 226 | } |
| 227 | |
| 228 | public void expectPreventReconnectReceived(long timeoutMs) { |
| 229 | assertTrue(mPreventReconnectReceived.block(timeoutMs)); |
| 230 | } |
| 231 | |
| 232 | public void expectDisconnected(long timeoutMs) { |
| 233 | assertTrue(mDisconnected.block(timeoutMs)); |
| 234 | } |
| 235 | |
| 236 | public void sendLinkProperties(LinkProperties lp) { |
| 237 | mNetworkAgent.sendLinkProperties(lp); |
| 238 | } |
| 239 | |
| 240 | public void setStartKeepaliveEvent(int reason) { |
| 241 | mStartKeepaliveError = reason; |
| 242 | } |
| 243 | |
| 244 | public void setStopKeepaliveEvent(int reason) { |
| 245 | mStopKeepaliveError = reason; |
| 246 | } |
| 247 | |
| 248 | public void setExpectedKeepaliveSlot(Integer slot) { |
| 249 | mExpectedKeepaliveSlot = slot; |
| 250 | } |
| 251 | |
| 252 | public NetworkAgent getNetworkAgent() { |
| 253 | return mNetworkAgent; |
| 254 | } |
| 255 | |
| 256 | public NetworkInfo getNetworkInfo() { |
| 257 | return mNetworkInfo; |
| 258 | } |
| 259 | |
| 260 | public NetworkCapabilities getNetworkCapabilities() { |
| 261 | return mNetworkCapabilities; |
| 262 | } |
| 263 | |
| 264 | public void waitForIdle(long timeoutMs) { |
| 265 | HandlerUtilsKt.waitForIdle(mHandlerThread, timeoutMs); |
| 266 | } |
| 267 | } |