From 5cd9288b039a425ddef0061a711b92451544db9f Mon Sep 17 00:00:00 2001 From: Patrick Rohr Date: Wed, 4 Nov 2020 17:04:21 +0100 Subject: Add Tests for EthernetNetworkFactory Test: atest EthernetNetworkFactoryTest Bug: 191635995 Change-Id: I1c07bb6d30706c4e13002eb402fadfecb97b36d1 --- .../server/ethernet/EthernetNetworkAgent.java | 65 +++++++++++++++++++ .../server/ethernet/EthernetNetworkFactory.java | 72 ++++++++++++++++------ 2 files changed, 117 insertions(+), 20 deletions(-) create mode 100644 java/com/android/server/ethernet/EthernetNetworkAgent.java (limited to 'java') diff --git a/java/com/android/server/ethernet/EthernetNetworkAgent.java b/java/com/android/server/ethernet/EthernetNetworkAgent.java new file mode 100644 index 0000000..5598fc6 --- /dev/null +++ b/java/com/android/server/ethernet/EthernetNetworkAgent.java @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2021 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.ethernet; + +import android.content.Context; +import android.net.LinkProperties; +import android.net.NetworkAgent; +import android.net.NetworkAgentConfig; +import android.net.NetworkCapabilities; +import android.net.NetworkProvider; +import android.os.Looper; +import android.annotation.NonNull; +import android.annotation.Nullable; + +public class EthernetNetworkAgent extends NetworkAgent { + + private static final String TAG = "EthernetNetworkAgent"; + + public interface Callbacks { + void onNetworkUnwanted(); + } + + private final Callbacks mCallbacks; + + EthernetNetworkAgent( + @NonNull Context context, + @NonNull Looper looper, + @NonNull NetworkCapabilities nc, + @NonNull LinkProperties lp, + int networkScore, + @NonNull NetworkAgentConfig config, + @Nullable NetworkProvider provider, + @NonNull Callbacks cb) { + super(context, looper, TAG, nc, lp, networkScore, config, provider); + mCallbacks = cb; + } + + @Override + public void onNetworkUnwanted() { + mCallbacks.onNetworkUnwanted(); + } + + // sendLinkProperties is final in NetworkAgent, so it cannot be mocked. + public void sendLinkPropertiesImpl(LinkProperties lp) { + sendLinkProperties(lp); + } + + public Callbacks getCallbacks() { + return mCallbacks; + } +} diff --git a/java/com/android/server/ethernet/EthernetNetworkFactory.java b/java/com/android/server/ethernet/EthernetNetworkFactory.java index 28b24f1..f4de23d 100644 --- a/java/com/android/server/ethernet/EthernetNetworkFactory.java +++ b/java/com/android/server/ethernet/EthernetNetworkFactory.java @@ -27,10 +27,10 @@ import android.net.IpConfiguration; import android.net.IpConfiguration.IpAssignment; import android.net.IpConfiguration.ProxySettings; import android.net.LinkProperties; -import android.net.NetworkAgent; import android.net.NetworkAgentConfig; import android.net.NetworkCapabilities; import android.net.NetworkFactory; +import android.net.NetworkProvider; import android.net.NetworkRequest; import android.net.NetworkSpecifier; import android.net.ip.IIpClient; @@ -40,12 +40,14 @@ import android.net.shared.ProvisioningConfiguration; import android.net.util.InterfaceParams; import android.os.ConditionVariable; import android.os.Handler; +import android.os.Looper; import android.os.RemoteException; import android.text.TextUtils; import android.util.AndroidRuntimeException; import android.util.Log; import android.util.SparseArray; +import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.IndentingPrintWriter; import java.io.FileDescriptor; @@ -69,6 +71,25 @@ public class EthernetNetworkFactory extends NetworkFactory { new ConcurrentHashMap<>(); private final Handler mHandler; private final Context mContext; + final Dependencies mDeps; + + public static class Dependencies { + public void makeIpClient(Context context, String iface, IpClientCallbacks callbacks) { + IpClientUtil.makeIpClient(context, iface, callbacks); + } + + public EthernetNetworkAgent makeEthernetNetworkAgent(Context context, Looper looper, + NetworkCapabilities nc, LinkProperties lp, int networkScore, + NetworkAgentConfig config, NetworkProvider provider, + EthernetNetworkAgent.Callbacks cb) { + return new EthernetNetworkAgent(context, looper, nc, lp, networkScore, config, provider, + cb); + } + + public InterfaceParams getNetworkInterfaceByName(String name) { + return InterfaceParams.getByName(name); + } + } public static class ConfigurationException extends AndroidRuntimeException { public ConfigurationException(String msg) { @@ -77,10 +98,17 @@ public class EthernetNetworkFactory extends NetworkFactory { } public EthernetNetworkFactory(Handler handler, Context context, NetworkCapabilities filter) { + this(handler, context, filter, new Dependencies()); + } + + @VisibleForTesting + EthernetNetworkFactory(Handler handler, Context context, NetworkCapabilities filter, + Dependencies deps) { super(handler.getLooper(), context, NETWORK_TYPE, filter); mHandler = handler; mContext = context; + mDeps = deps; setScoreFilter(NETWORK_SCORE); } @@ -149,7 +177,7 @@ public class EthernetNetworkFactory extends NetworkFactory { } NetworkInterfaceState iface = new NetworkInterfaceState( - ifaceName, hwAddress, mHandler, mContext, capabilities, this); + ifaceName, hwAddress, mHandler, mContext, capabilities, this, mDeps); iface.setIpConfig(ipConfiguration); mTrackingInterfaces.put(ifaceName, iface); @@ -251,6 +279,7 @@ public class EthernetNetworkFactory extends NetworkFactory { private final Handler mHandler; private final Context mContext; private final NetworkFactory mNetworkFactory; + private final Dependencies mDeps; private final int mLegacyType; private static String sTcpBufferSizes = null; // Lazy initialized. @@ -260,7 +289,7 @@ public class EthernetNetworkFactory extends NetworkFactory { private volatile @Nullable IIpClient mIpClient; private @Nullable IpClientCallbacksImpl mIpClientCallback; - private @Nullable NetworkAgent mNetworkAgent; + private @Nullable EthernetNetworkAgent mNetworkAgent; private @Nullable IpConfiguration mIpConfig; /** @@ -360,12 +389,14 @@ public class EthernetNetworkFactory extends NetworkFactory { } NetworkInterfaceState(String ifaceName, String hwAddress, Handler handler, Context context, - @NonNull NetworkCapabilities capabilities, NetworkFactory networkFactory) { + @NonNull NetworkCapabilities capabilities, NetworkFactory networkFactory, + Dependencies deps) { name = ifaceName; mCapabilities = checkNotNull(capabilities); mHandler = handler; mContext = context; mNetworkFactory = networkFactory; + mDeps = deps; int legacyType = ConnectivityManager.TYPE_NONE; int[] transportTypes = mCapabilities.getTransportTypes(); @@ -451,7 +482,7 @@ public class EthernetNetworkFactory extends NetworkFactory { } mIpClientCallback = new IpClientCallbacksImpl(); - IpClientUtil.makeIpClient(mContext, name, mIpClientCallback); + mDeps.makeIpClient(mContext, name, mIpClientCallback); mIpClientCallback.awaitIpClientStart(); if (sTcpBufferSizes == null) { sTcpBufferSizes = mContext.getResources().getString( @@ -474,18 +505,19 @@ public class EthernetNetworkFactory extends NetworkFactory { .setLegacyTypeName(NETWORK_TYPE) .setLegacyExtraInfo(mHwAddress) .build(); - mNetworkAgent = new NetworkAgent(mContext, mHandler.getLooper(), - NETWORK_TYPE, mCapabilities, mLinkProperties, - getNetworkScore(), config, mNetworkFactory.getProvider()) { - public void unwanted() { - if (this == mNetworkAgent) { - stop(); - } else if (mNetworkAgent != null) { - Log.d(TAG, "Ignoring unwanted as we have a more modern " + - "instance"); - } // Otherwise, we've already called stop. - } - }; + mNetworkAgent = mDeps.makeEthernetNetworkAgent(mContext, mHandler.getLooper(), + mCapabilities, mLinkProperties, getNetworkScore(), config, + mNetworkFactory.getProvider(), new EthernetNetworkAgent.Callbacks() { + @Override + public void onNetworkUnwanted() { + if (this == mNetworkAgent.getCallbacks()) { + stop(); + } else if (mNetworkAgent != null) { + Log.d(TAG, "Ignoring unwanted as we have a more modern " + + "instance"); + } // Otherwise, we've already called stop. + } + }); mNetworkAgent.register(); mNetworkAgent.markConnected(); } @@ -496,7 +528,7 @@ public class EthernetNetworkFactory extends NetworkFactory { stop(); // If the interface has disappeared provisioning will fail over and over again, so // there is no point in starting again - if (null != InterfaceParams.getByName(name)) { + if (null != mDeps.getNetworkInterfaceByName(name)) { start(); } } @@ -504,7 +536,7 @@ public class EthernetNetworkFactory extends NetworkFactory { void updateLinkProperties(LinkProperties linkProperties) { mLinkProperties = linkProperties; if (mNetworkAgent != null) { - mNetworkAgent.sendLinkProperties(linkProperties); + mNetworkAgent.sendLinkPropertiesImpl(linkProperties); } } @@ -545,7 +577,7 @@ public class EthernetNetworkFactory extends NetworkFactory { mLinkProperties); } mNetworkAgent.sendNetworkCapabilities(mCapabilities); - mNetworkAgent.sendLinkProperties(mLinkProperties); + mNetworkAgent.sendLinkPropertiesImpl(mLinkProperties); // As a note, getNetworkScore() is fairly expensive to calculate. This is fine for now // since the agent isn't updated frequently. Consider caching the score in the future if -- cgit v1.2.3