diff options
author | android-build-team Robot <android-build-team-robot@google.com> | 2020-06-12 01:06:24 +0000 |
---|---|---|
committer | android-build-team Robot <android-build-team-robot@google.com> | 2020-06-12 01:06:24 +0000 |
commit | ff7f5df30232ed7d7197cd35035365463a561a44 (patch) | |
tree | 615fffaf851ee11dcfcbc8e408f92599937e2904 | |
parent | 74ceeb115e450c773044af4a8b851bc5812745db (diff) | |
parent | 9f5fc7ac7400f64ba6c5f1a376c3abd548a16caa (diff) |
Snap for 6582636 from 9f5fc7ac7400f64ba6c5f1a376c3abd548a16caa to rvc-release
Change-Id: Id07010e4d3a1751806a2399e426bc0a10f8fb0e4
13 files changed, 281 insertions, 110 deletions
diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointNetworkDetailsTracker.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointNetworkDetailsTracker.java index 9bd2a5905..e2d7acc8e 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointNetworkDetailsTracker.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointNetworkDetailsTracker.java @@ -26,6 +26,7 @@ import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.LinkProperties; +import android.net.Network; import android.net.NetworkInfo; import android.net.NetworkScoreManager; import android.net.wifi.ScanResult; @@ -57,6 +58,7 @@ class PasspointNetworkDetailsTracker extends NetworkDetailsTracker { private final PasspointWifiEntry mChosenEntry; private OsuWifiEntry mOsuWifiEntry; + private NetworkInfo mCurrentNetworkInfo; PasspointNetworkDetailsTracker(@NonNull Lifecycle lifecycle, @NonNull Context context, @@ -104,8 +106,9 @@ class PasspointNetworkDetailsTracker extends NetworkDetailsTracker { conditionallyUpdateScanResults(true /* lastScanSucceeded */); conditionallyUpdateConfig(); final WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); - final NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo(); - mChosenEntry.updateConnectionInfo(wifiInfo, networkInfo); + final Network currentNetwork = mWifiManager.getCurrentNetwork(); + mCurrentNetworkInfo = mConnectivityManager.getNetworkInfo(currentNetwork); + mChosenEntry.updateConnectionInfo(wifiInfo, mCurrentNetworkInfo); handleLinkPropertiesChanged(mConnectivityManager.getLinkProperties( mWifiManager.getCurrentNetwork())); } @@ -141,16 +144,15 @@ class PasspointNetworkDetailsTracker extends NetworkDetailsTracker { @WorkerThread @Override protected void handleRssiChangedAction() { - mChosenEntry.updateConnectionInfo(mWifiManager.getConnectionInfo(), - mConnectivityManager.getActiveNetworkInfo()); + mChosenEntry.updateConnectionInfo(mWifiManager.getConnectionInfo(), mCurrentNetworkInfo); } @WorkerThread @Override protected void handleNetworkStateChangedAction(@NonNull Intent intent) { checkNotNull(intent, "Intent cannot be null!"); - mChosenEntry.updateConnectionInfo(mWifiManager.getConnectionInfo(), - (NetworkInfo) intent.getExtra(WifiManager.EXTRA_NETWORK_INFO)); + mCurrentNetworkInfo = (NetworkInfo) intent.getExtra(WifiManager.EXTRA_NETWORK_INFO); + mChosenEntry.updateConnectionInfo(mWifiManager.getConnectionInfo(), mCurrentNetworkInfo); } @WorkerThread diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardNetworkDetailsTracker.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardNetworkDetailsTracker.java index b89c8ab9b..3ef1b4727 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardNetworkDetailsTracker.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardNetworkDetailsTracker.java @@ -31,6 +31,7 @@ import android.net.ConnectivityManager; import android.net.LinkProperties; import android.net.Network; import android.net.NetworkCapabilities; +import android.net.NetworkInfo; import android.net.NetworkScoreManager; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiInfo; @@ -55,6 +56,7 @@ class StandardNetworkDetailsTracker extends NetworkDetailsTracker { private final StandardWifiEntry mChosenEntry; private final boolean mIsNetworkRequest; + private NetworkInfo mCurrentNetworkInfo; StandardNetworkDetailsTracker(@NonNull Lifecycle lifecycle, @NonNull Context context, @@ -84,8 +86,8 @@ class StandardNetworkDetailsTracker extends NetworkDetailsTracker { conditionallyUpdateConfig(); final WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); final Network currentNetwork = mWifiManager.getCurrentNetwork(); - mChosenEntry.updateConnectionInfo(wifiInfo, mConnectivityManager - .getNetworkInfo(currentNetwork)); + mCurrentNetworkInfo = mConnectivityManager.getNetworkInfo(currentNetwork); + mChosenEntry.updateConnectionInfo(wifiInfo, mCurrentNetworkInfo); handleLinkPropertiesChanged(mConnectivityManager.getLinkProperties(currentNetwork)); } @@ -134,15 +136,14 @@ class StandardNetworkDetailsTracker extends NetworkDetailsTracker { @Override protected void handleNetworkStateChangedAction(@NonNull Intent intent) { checkNotNull(intent, "Intent cannot be null!"); - mChosenEntry.updateConnectionInfo(mWifiManager.getConnectionInfo(), - mConnectivityManager.getNetworkInfo(mWifiManager.getCurrentNetwork())); + mCurrentNetworkInfo = (NetworkInfo) intent.getExtra(WifiManager.EXTRA_NETWORK_INFO); + mChosenEntry.updateConnectionInfo(mWifiManager.getConnectionInfo(), mCurrentNetworkInfo); } @WorkerThread @Override protected void handleRssiChangedAction() { - mChosenEntry.updateConnectionInfo(mWifiManager.getConnectionInfo(), - mConnectivityManager.getNetworkInfo(mWifiManager.getCurrentNetwork())); + mChosenEntry.updateConnectionInfo(mWifiManager.getConnectionInfo(), mCurrentNetworkInfo); } @WorkerThread diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiPickerTracker.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiPickerTracker.java index 2b433bb76..bb3a2246a 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiPickerTracker.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiPickerTracker.java @@ -96,6 +96,7 @@ public class WifiPickerTracker extends BaseWifiTracker { // NetworkRequestEntry representing a network that was connected through the NetworkRequest API private NetworkRequestEntry mNetworkRequestEntry; + private NetworkInfo mCurrentNetworkInfo; // Cache containing saved WifiConfigurations mapped by StandardWifiEntry key private final Map<String, WifiConfiguration> mWifiConfigCache = new HashMap<>(); // Cache containing suggested WifiConfigurations mapped by StandardWifiEntry key @@ -192,7 +193,8 @@ public class WifiPickerTracker extends BaseWifiTracker { conditionallyUpdateScanResults(true /* lastScanSucceeded */); final WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); final Network currentNetwork = mWifiManager.getCurrentNetwork(); - updateConnectionInfo(wifiInfo, mConnectivityManager.getNetworkInfo(currentNetwork)); + mCurrentNetworkInfo = mConnectivityManager.getNetworkInfo(currentNetwork); + updateConnectionInfo(wifiInfo, mCurrentNetworkInfo); handleLinkPropertiesChanged(mConnectivityManager.getLinkProperties(currentNetwork)); notifyOnNumSavedNetworksChanged(); notifyOnNumSavedSubscriptionsChanged(); @@ -245,9 +247,8 @@ public class WifiPickerTracker extends BaseWifiTracker { protected void handleNetworkStateChangedAction(@NonNull Intent intent) { checkNotNull(intent, "Intent cannot be null!"); final WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); - final NetworkInfo networkInfo = - (NetworkInfo) intent.getExtra(WifiManager.EXTRA_NETWORK_INFO); - updateConnectionInfo(wifiInfo, networkInfo); + mCurrentNetworkInfo = (NetworkInfo) intent.getExtra(WifiManager.EXTRA_NETWORK_INFO); + updateConnectionInfo(wifiInfo, mCurrentNetworkInfo); updateWifiEntries(); } @@ -256,9 +257,7 @@ public class WifiPickerTracker extends BaseWifiTracker { protected void handleRssiChangedAction() { if (mConnectedWifiEntry != null) { final WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); - final NetworkInfo networkInfo = mConnectivityManager.getNetworkInfo( - mWifiManager.getCurrentNetwork()); - mConnectedWifiEntry.updateConnectionInfo(wifiInfo, networkInfo); + mConnectedWifiEntry.updateConnectionInfo(wifiInfo, mCurrentNetworkInfo); } } diff --git a/service/Android.bp b/service/Android.bp index d7c16dfc1..5becbe21d 100644 --- a/service/Android.bp +++ b/service/Android.bp @@ -15,7 +15,7 @@ // java_defaults { name: "wifi-service-common", - min_sdk_version: "29", + defaults: ["wifi-module-sdk-version-defaults"], errorprone: { javacflags: ["-Xep:CheckReturnValue:ERROR"], }, diff --git a/service/java/com/android/server/wifi/ClientModeImpl.java b/service/java/com/android/server/wifi/ClientModeImpl.java index 10a41f5dc..6b718bb32 100644 --- a/service/java/com/android/server/wifi/ClientModeImpl.java +++ b/service/java/com/android/server/wifi/ClientModeImpl.java @@ -107,7 +107,6 @@ import android.util.Log; import android.util.Pair; import android.util.SparseArray; -import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.AsyncChannel; import com.android.internal.util.MessageUtils; @@ -445,9 +444,7 @@ public class ClientModeImpl extends StateMachine { private WifiNetworkFactory mNetworkFactory; private UntrustedWifiNetworkFactory mUntrustedNetworkFactory; - @GuardedBy("mNetworkAgentLock") private WifiNetworkAgent mNetworkAgent; - private final Object mNetworkAgentLock = new Object(); private byte[] mRssiRanges; @@ -630,6 +627,9 @@ public class ClientModeImpl extends StateMachine { /* Start connection to FILS AP*/ static final int CMD_START_FILS_CONNECTION = BASE + 262; + + private static final int CMD_GET_CURRENT_NETWORK = BASE + 263; + // For message logging. private static final Class[] sMessageClasses = { AsyncChannel.class, ClientModeImpl.class }; @@ -1785,20 +1785,30 @@ public class ClientModeImpl extends StateMachine { } /** - * Get Network object of current wifi network - * @return Network object of current wifi network + * Should only be used internally. + * External callers should use {@link #syncGetCurrentNetwork(AsyncChannel)}. */ - public Network getCurrentNetwork() { - synchronized (mNetworkAgentLock) { - if (mNetworkAgent != null) { - return mNetworkAgent.getNetwork(); - } else { - return null; - } + private Network getCurrentNetwork() { + if (mNetworkAgent != null) { + return mNetworkAgent.getNetwork(); + } else { + return null; } } /** + * Get Network object of currently connected wifi network, or null if not connected. + * @return Network object of current wifi network + */ + public Network syncGetCurrentNetwork(AsyncChannel channel) { + Message resultMsg = channel.sendMessageSynchronously(CMD_GET_CURRENT_NETWORK); + if (messageIsNull(resultMsg)) return null; + Network network = (Network) resultMsg.obj; + resultMsg.recycle(); + return network; + } + + /** * Enable TDLS for a specific MAC address */ public void enableTdls(String remoteMacAddress, boolean enable) { @@ -2757,11 +2767,9 @@ public class ClientModeImpl extends StateMachine { mIsAutoRoaming = false; sendNetworkChangeBroadcast(DetailedState.DISCONNECTED); - synchronized (mNetworkAgentLock) { - if (mNetworkAgent != null) { - mNetworkAgent.unregister(); - mNetworkAgent = null; - } + if (mNetworkAgent != null) { + mNetworkAgent.unregister(); + mNetworkAgent = null; } /* Clear network properties */ @@ -3492,7 +3500,8 @@ public class ClientModeImpl extends StateMachine { replyToMessage(message, message.what, Long.valueOf(featureSet)); break; case CMD_GET_LINK_LAYER_STATS: - // Not supported hence reply with error message + case CMD_GET_CURRENT_NETWORK: + // Not supported hence reply with null message.obj replyToMessage(message, message.what, null); break; case WifiP2pServiceImpl.P2P_CONNECTION_CHANGED: @@ -4752,16 +4761,14 @@ public class ClientModeImpl extends StateMachine { .setPartialConnectivityAcceptable(config.noInternetAccessExpected) .build(); final NetworkCapabilities nc = getCapabilities(getCurrentWifiConfiguration()); - synchronized (mNetworkAgentLock) { - // This should never happen. - if (mNetworkAgent != null) { - Log.wtf(TAG, "mNetworkAgent is not null: " + mNetworkAgent); - mNetworkAgent.unregister(); - } - mNetworkAgent = new WifiNetworkAgent(mContext, getHandler().getLooper(), - "WifiNetworkAgent", nc, mLinkProperties, 60, naConfig, - mNetworkFactory.getProvider()); + // This should never happen. + if (mNetworkAgent != null) { + Log.wtf(TAG, "mNetworkAgent is not null: " + mNetworkAgent); + mNetworkAgent.unregister(); } + mNetworkAgent = new WifiNetworkAgent(mContext, getHandler().getLooper(), + "WifiNetworkAgent", nc, mLinkProperties, 60, naConfig, + mNetworkFactory.getProvider()); mWifiScoreReport.setNetworkAgent(mNetworkAgent); // We must clear the config BSSID, as the wifi chipset may decide to roam @@ -5317,6 +5324,9 @@ public class ClientModeImpl extends StateMachine { transitionTo(mDisconnectedState); } break; + case CMD_GET_CURRENT_NETWORK: + replyToMessage(message, message.what, getCurrentNetwork()); + break; default: handleStatus = NOT_HANDLED; break; @@ -5541,6 +5551,9 @@ public class ClientModeImpl extends StateMachine { mWifiMetrics.incrementIpRenewalFailure(); handleStatus = NOT_HANDLED; break; + case CMD_GET_CURRENT_NETWORK: + replyToMessage(message, message.what, getCurrentNetwork()); + break; default: handleStatus = NOT_HANDLED; break; diff --git a/service/java/com/android/server/wifi/Clock.java b/service/java/com/android/server/wifi/Clock.java index bb2f21e2d..b1d45961b 100644 --- a/service/java/com/android/server/wifi/Clock.java +++ b/service/java/com/android/server/wifi/Clock.java @@ -58,4 +58,10 @@ public class Clock { return SystemClock.uptimeMillis(); } + /** + * Waits a given number of milliseconds (of uptimeMillis) before returning. + */ + public void sleep(long ms) { + SystemClock.sleep(ms); + } } diff --git a/service/java/com/android/server/wifi/WifiDiagnostics.java b/service/java/com/android/server/wifi/WifiDiagnostics.java index 2ba90c968..a2328cd26 100644 --- a/service/java/com/android/server/wifi/WifiDiagnostics.java +++ b/service/java/com/android/server/wifi/WifiDiagnostics.java @@ -43,6 +43,7 @@ import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; +import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -108,8 +109,11 @@ class WifiDiagnostics extends BaseWifiDiagnostics { /** Minimum dump period with same error code */ public static final long MIN_DUMP_TIME_WINDOW_MILLIS = 10 * 60 * 1000; // 10 mins - // Timeout for logcat - private static final int LOGCAT_TIMEOUT_MILLIS = 500; + // Timeout for logcat process termination + private static final long LOGCAT_PROC_TIMEOUT_MILLIS = 500; + // Timeout for logcat read from input/error stream each. + @VisibleForTesting + public static final long LOGCAT_READ_TIMEOUT_MILLIS = 500; private long mLastBugReportTime; @@ -708,23 +712,30 @@ class WifiDiagnostics extends BaseWifiDiagnostics { return result; } + private void readLogcatStreamLinesWithTimeout( + BufferedReader inReader, List<String> outLinesList) throws IOException { + long startTimeMs = mClock.getElapsedSinceBootMillis(); + while (mClock.getElapsedSinceBootMillis() < startTimeMs + LOGCAT_READ_TIMEOUT_MILLIS) { + // If there is a burst of data, continue reading without checking for timeout. + while (inReader.ready()) { + String line = inReader.readLine(); + if (line == null) return; // end of stream. + outLinesList.add(line); + } + mClock.sleep(LOGCAT_READ_TIMEOUT_MILLIS / 10); + } + } + private ArrayList<String> getLogcat(String logcatSections, int maxLines) { ArrayList<String> lines = new ArrayList<>(maxLines); try { Process process = mJavaRuntime.exec( String.format("logcat -b %s -t %d", logcatSections, maxLines)); - BufferedReader reader = new BufferedReader( - new InputStreamReader(process.getInputStream())); - String line; - while ((line = reader.readLine()) != null) { - lines.add(line); - } - reader = new BufferedReader( - new InputStreamReader(process.getErrorStream())); - while ((line = reader.readLine()) != null) { - lines.add(line); - } - process.waitFor(LOGCAT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); + readLogcatStreamLinesWithTimeout( + new BufferedReader(new InputStreamReader(process.getInputStream())), lines); + readLogcatStreamLinesWithTimeout( + new BufferedReader(new InputStreamReader(process.getErrorStream())), lines); + process.waitFor(LOGCAT_PROC_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); } catch (InterruptedException|IOException e) { mLog.dump("Exception while capturing logcat: %").c(e.toString()).flush(); } diff --git a/service/java/com/android/server/wifi/WifiServiceImpl.java b/service/java/com/android/server/wifi/WifiServiceImpl.java index 52fb61fdb..070422775 100644 --- a/service/java/com/android/server/wifi/WifiServiceImpl.java +++ b/service/java/com/android/server/wifi/WifiServiceImpl.java @@ -3497,7 +3497,7 @@ public class WifiServiceImpl extends BaseWifiService { if (mVerboseLoggingEnabled) { mLog.info("getCurrentNetwork uid=%").c(Binder.getCallingUid()).flush(); } - return mClientModeImpl.getCurrentNetwork(); + return mClientModeImpl.syncGetCurrentNetwork(mClientModeImplChannel); } public static String toHexString(String s) { diff --git a/tests/wifitests/Android.bp b/tests/wifitests/Android.bp index 851a6016e..6c48c3b60 100644 --- a/tests/wifitests/Android.bp +++ b/tests/wifitests/Android.bp @@ -17,13 +17,7 @@ android_test { name: "FrameworksWifiTests", - srcs: [ - "**/*.java", - // wifi-service sources must be included here so that the latest changes - // will be used when tests. Otherwise the tests would run against the installed - // system. - ":wifi-service-srcs", - ], + srcs: [ "**/*.java" ], dxflags: ["--multi-dex"], @@ -34,35 +28,14 @@ android_test { "hamcrest-library", "mockito-target-extended-minus-junit4", "frameworks-base-testutils", - "truth-prebuilt", + "truth-prebuilt", - // Libraries needed by wifi-service - "android.hardware.wifi-V1.0-java", - "android.hardware.wifi-V1.1-java", - "android.hardware.wifi-V1.2-java", - "android.hardware.wifi-V1.3-java", - "android.hardware.wifi-V1.4-java", - "android.hardware.wifi.hostapd-V1.0-java", - "android.hardware.wifi.hostapd-V1.1-java", - "android.hardware.wifi.hostapd-V1.2-java", - "android.hardware.wifi.supplicant-V1.0-java", - "android.hardware.wifi.supplicant-V1.1-java", - "android.hardware.wifi.supplicant-V1.2-java", - "android.hardware.wifi.supplicant-V1.3-java", - "android.hidl.manager-V1.2-java", - "androidx.annotation_annotation", - "bouncycastle-unbundled", - "ksoap2", - // Note: libprotobuf-java-lite uses a few core platform APIs which - // does show up as @hide API usage. But, this can be safely ignored - // since the library uses reflection to ensure that the OS does provide - // the necessary core platform APIs. - "libprotobuf-java-lite", - "libnanohttpd", - "services.net-module-wifi", - "services.core", - "wifi-lite-protos", - "wifi-nano-protos", + // Statically link wifi-service-pre-jarjar since we want to test the working copy of + // service-wifi, not the on-device copy. + // Use pre-jarjar version so that we can reference symbols before they are renamed. + // Then, the jarjar_rules here will perform the rename for the entire APK + // i.e. service-wifi + test code + "wifi-service-pre-jarjar", ], jarjar_rules: ":wifi-jarjar-rules", @@ -80,14 +53,8 @@ android_test { "android.test.runner", "android.test.base", "android.test.mock", - - // Libraries needed by wifi-service - "error_prone_annotations", - "jsr305", - "services", // load the resources from the resources APK. "ServiceWifiResources", - "unsupportedappusage", ], // These must be explicitly included because they are not normally accessible diff --git a/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java b/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java index 20766898d..58f5b1ccc 100644 --- a/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java @@ -421,6 +421,7 @@ public class ClientModeImplTest extends WifiBaseTest { @Mock ThroughputPredictor mThroughputPredictor; @Mock ScanRequestProxy mScanRequestProxy; @Mock DeviceConfigFacade mDeviceConfigFacade; + @Mock Network mNetwork; final ArgumentCaptor<WifiConfigManager.OnNetworkUpdateListener> mConfigUpdateListenerCaptor = ArgumentCaptor.forClass(WifiConfigManager.OnNetworkUpdateListener.class); @@ -533,10 +534,8 @@ public class ClientModeImplTest extends WifiBaseTest { return null; }).when(mIpClient).shutdown(); when(mConnectivityManager.registerNetworkAgent(any(), any(), any(), any(), anyInt(), any(), - anyInt())).thenReturn(mock(Network.class)); - List<SubscriptionInfo> subList = new ArrayList<>() {{ - add(mock(SubscriptionInfo.class)); - }}; + anyInt())).thenReturn(mNetwork); + List<SubscriptionInfo> subList = Arrays.asList(mock(SubscriptionInfo.class)); when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(subList); when(mSubscriptionManager.getActiveSubscriptionIdList()) .thenReturn(new int[]{DATA_SUBID}); @@ -5266,4 +5265,19 @@ public class ClientModeImplTest extends WifiBaseTest { verifyNoMoreInteractions(mNetworkAgentHandler); } + + @Test + public void testSyncGetCurrentNetwork() throws Exception { + // syncGetCurrentNetwork() returns null when disconnected + mLooper.startAutoDispatch(); + assertNull(mCmi.syncGetCurrentNetwork(mCmiAsyncChannel)); + mLooper.stopAutoDispatch(); + + connect(); + + // syncGetCurrentNetwork() returns non-null Network when connected + mLooper.startAutoDispatch(); + assertEquals(mNetwork, mCmi.syncGetCurrentNetwork(mCmiAsyncChannel)); + mLooper.stopAutoDispatch(); + } } diff --git a/tests/wifitests/src/com/android/server/wifi/IpConfigStoreTestWriter.java b/tests/wifitests/src/com/android/server/wifi/IpConfigStoreTestWriter.java new file mode 100644 index 000000000..f5a81bcb4 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/IpConfigStoreTestWriter.java @@ -0,0 +1,147 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wifi; + +import android.net.IpConfiguration; +import android.net.LinkAddress; +import android.net.ProxyInfo; +import android.net.StaticIpConfiguration; +import android.util.Log; + +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.InetAddress; + +/** + * Copied from frameworks/base/services/core/java/com/android/server/net/IpConfigStore.java + */ +public class IpConfigStoreTestWriter { + private static final String TAG = "IpConfigStoreTestWriter"; + + private static final String ID_KEY = "id"; + private static final String IP_ASSIGNMENT_KEY = "ipAssignment"; + private static final String LINK_ADDRESS_KEY = "linkAddress"; + private static final String GATEWAY_KEY = "gateway"; + private static final String DNS_KEY = "dns"; + private static final String PROXY_SETTINGS_KEY = "proxySettings"; + private static final String PROXY_HOST_KEY = "proxyHost"; + private static final String PROXY_PORT_KEY = "proxyPort"; + private static final String PROXY_PAC_FILE = "proxyPac"; + private static final String EXCLUSION_LIST_KEY = "exclusionList"; + private static final String EOS = "eos"; + + /** Serialize an IpConfiguration object */ + public static boolean writeConfig(DataOutputStream out, String configKey, + IpConfiguration config, int version) throws IOException { + boolean written = false; + + try { + switch (config.ipAssignment) { + case STATIC: + out.writeUTF(IP_ASSIGNMENT_KEY); + out.writeUTF(config.ipAssignment.toString()); + StaticIpConfiguration staticIpConfiguration = config.staticIpConfiguration; + if (staticIpConfiguration != null) { + if (staticIpConfiguration.ipAddress != null) { + LinkAddress ipAddress = staticIpConfiguration.ipAddress; + out.writeUTF(LINK_ADDRESS_KEY); + out.writeUTF(ipAddress.getAddress().getHostAddress()); + out.writeInt(ipAddress.getPrefixLength()); + } + if (staticIpConfiguration.gateway != null) { + out.writeUTF(GATEWAY_KEY); + out.writeInt(0); // Default route. + out.writeInt(1); // Have a gateway. + out.writeUTF(staticIpConfiguration.gateway.getHostAddress()); + } + for (InetAddress inetAddr : staticIpConfiguration.dnsServers) { + out.writeUTF(DNS_KEY); + out.writeUTF(inetAddr.getHostAddress()); + } + } + written = true; + break; + case DHCP: + out.writeUTF(IP_ASSIGNMENT_KEY); + out.writeUTF(config.ipAssignment.toString()); + written = true; + break; + case UNASSIGNED: + /* Ignore */ + break; + default: + loge("Ignore invalid ip assignment while writing"); + break; + } + + switch (config.proxySettings) { + case STATIC: + ProxyInfo proxyProperties = config.httpProxy; + String exclusionList = proxyProperties.getExclusionListAsString(); + out.writeUTF(PROXY_SETTINGS_KEY); + out.writeUTF(config.proxySettings.toString()); + out.writeUTF(PROXY_HOST_KEY); + out.writeUTF(proxyProperties.getHost()); + out.writeUTF(PROXY_PORT_KEY); + out.writeInt(proxyProperties.getPort()); + if (exclusionList != null) { + out.writeUTF(EXCLUSION_LIST_KEY); + out.writeUTF(exclusionList); + } + written = true; + break; + case PAC: + ProxyInfo proxyPacProperties = config.httpProxy; + out.writeUTF(PROXY_SETTINGS_KEY); + out.writeUTF(config.proxySettings.toString()); + out.writeUTF(PROXY_PAC_FILE); + out.writeUTF(proxyPacProperties.getPacFileUrl().toString()); + written = true; + break; + case NONE: + out.writeUTF(PROXY_SETTINGS_KEY); + out.writeUTF(config.proxySettings.toString()); + written = true; + break; + case UNASSIGNED: + /* Ignore */ + break; + default: + loge("Ignore invalid proxy settings while writing"); + break; + } + + if (written) { + out.writeUTF(ID_KEY); + if (version < 3) { + out.writeInt(Integer.valueOf(configKey)); + } else { + out.writeUTF(configKey); + } + } + } catch (NullPointerException e) { + loge("Failure in writing " + config + e); + } + out.writeUTF(EOS); + + return written; + } + + private static void loge(String s) { + Log.e(TAG, s); + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/WifiBackupRestoreTest.java b/tests/wifitests/src/com/android/server/wifi/WifiBackupRestoreTest.java index 6b01f8e45..e640c4232 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiBackupRestoreTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiBackupRestoreTest.java @@ -25,7 +25,6 @@ import android.os.Process; import androidx.test.filters.SmallTest; -import com.android.server.net.IpConfigStore; import com.android.server.wifi.util.WifiPermissionsUtil; import org.junit.After; @@ -1183,8 +1182,11 @@ public class WifiBackupRestoreTest extends WifiBaseTest { out.writeInt(configStoreVersion); for (WifiConfiguration configuration : configurations) { // TODO: store configKey as a string instead of calculating its hash - IpConfigStore.writeConfig(out, String.valueOf(configuration.getKey().hashCode()), - configuration.getIpConfiguration(), configStoreVersion); + IpConfigStoreTestWriter.writeConfig( + out, + String.valueOf(configuration.getKey().hashCode()), + configuration.getIpConfiguration(), + configStoreVersion); } out.flush(); return bos.toByteArray(); diff --git a/tests/wifitests/src/com/android/server/wifi/WifiDiagnosticsTest.java b/tests/wifitests/src/com/android/server/wifi/WifiDiagnosticsTest.java index 6b235b9f7..c89fe675a 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiDiagnosticsTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiDiagnosticsTest.java @@ -26,6 +26,7 @@ import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.contains; import static org.mockito.Mockito.anyInt; import static org.mockito.Mockito.anyObject; +import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.eq; import static org.mockito.Mockito.never; @@ -72,6 +73,7 @@ public class WifiDiagnosticsTest extends WifiBaseTest { @Mock WifiMetrics mWifiMetrics; @Mock Clock mClock; @Mock BugreportManager mBugreportManager; + private long mBootTimeMs = 0L; MockResources mResources; WifiDiagnostics mWifiDiagnostics; @@ -135,6 +137,13 @@ public class WifiDiagnosticsTest extends WifiBaseTest { when(mWifiInjector.getWifiMetrics()).thenReturn(mWifiMetrics); when(mWifiInjector.getDeviceConfigFacade()).thenReturn(mDeviceConfigFacade); when(mDeviceConfigFacade.getBugReportMinWindowMs()).thenReturn(BUG_REPORT_MIN_WINDOW_MS); + // needed to for the loop in WifiDiagnostics.readLogcatStreamLinesWithTimeout(). + doAnswer(new AnswerWithArguments() { + public long answer() throws Exception { + mBootTimeMs += WifiDiagnostics.LOGCAT_READ_TIMEOUT_MILLIS / 2; + return mBootTimeMs; + } + }).when(mClock).getElapsedSinceBootMillis(); mWifiDiagnostics = new WifiDiagnostics( mContext, mWifiInjector, mWifiNative, mBuildProperties, mLastMileLogger, mClock); mWifiNative.enableVerboseLogging(0); |