diff options
author | James Mattis <jmattis@google.com> | 2022-06-13 21:41:41 -0700 |
---|---|---|
committer | James Mattis <jmattis@google.com> | 2022-06-13 21:52:29 -0700 |
commit | 80c8f8ce5b660df8bddab7bfca57b886105c5749 (patch) | |
tree | 04fbf46f6e7200c413e12e8ee412a3d2fc756dab | |
parent | 770c198e3c72cd477d01c0ffa8a70635ebad3abd (diff) | |
download | Car-80c8f8ce5b660df8bddab7bfca57b886105c5749.tar.gz |
Fixing Vehicular Networking Ref App UpdateConfig
Fixing bugs in the vechiular network reference app updateConfiguration
call. Namely, allowing callers to set the IP and network capabilities
even if the package names are empty as previously it would always do an
app lookup for what was in the allowed packages field (even if it was
empty), causing it to error out.
Also, the default network capabilities are now cleared out. Previously,
as they were not cleared out it was not possible to update the
configuration with one of the capabilities removed. E.g., as
NOT_RESTRICTED is a default capability, it was not possible to remove it
in an update configuration call. Additionally, the default capabilities
were being sent even if the network capabilities text box was empty
which is now fixed to only send if it is populated.
Finally, adding better error messaging if reference app users don't
popuplate the "Interface Name" field as it was causing confusion.
Bug: 234506668
Bug: 234508130
Bug: 234611544
Test: manually tested using the ref app
Change-Id: I775da65890f8e9b513e2de0f45a00e64fd32a167
2 files changed, 58 insertions, 18 deletions
diff --git a/tests/RailwayReferenceApp/src/com/google/android/car/networking/railway/ConfigurationUpdater.java b/tests/RailwayReferenceApp/src/com/google/android/car/networking/railway/ConfigurationUpdater.java index 5164d1c851..bdf780f7a8 100644 --- a/tests/RailwayReferenceApp/src/com/google/android/car/networking/railway/ConfigurationUpdater.java +++ b/tests/RailwayReferenceApp/src/com/google/android/car/networking/railway/ConfigurationUpdater.java @@ -27,6 +27,8 @@ import android.net.NetworkCapabilities; import android.net.StaticIpConfiguration; import android.os.OutcomeReceiver; +import androidx.annotation.Nullable; + import com.google.common.base.Strings; import java.util.ArrayList; @@ -51,30 +53,47 @@ public final class ConfigurationUpdater { String interfaceName) throws IllegalArgumentException, PackageManager.NameNotFoundException { - IpConfiguration ipConfiguration = Strings.isNullOrEmpty(ipConfigurationText) ? null : + EthernetNetworkUpdateRequest request = new EthernetNetworkUpdateRequest.Builder() + .setIpConfiguration(getIpConfiguration(ipConfigurationText)) + .setNetworkCapabilities(getCapabilities(networkCapabilitiesText, packageNames)) + .build(); + + mEthernetManager.updateConfiguration(interfaceName, request, + mApplicationContext.getMainExecutor(), mCallback); + } + + @Nullable + private IpConfiguration getIpConfiguration(String ipConfigurationText) { + return Strings.isNullOrEmpty(ipConfigurationText) ? null : new IpConfiguration.Builder() .setStaticIpConfiguration(new StaticIpConfiguration.Builder() .setIpAddress(new LinkAddress(ipConfigurationText)).build()) .build(); + } - NetworkCapabilities.Builder networkCapabilitiesBuilder = new NetworkCapabilities.Builder() - .addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET); - - networkCapabilitiesBuilder.setAllowedUids( - UidToPackageNameConverter.convertToUids(mApplicationContext, packageNames)); + @Nullable + private NetworkCapabilities getCapabilities(String networkCapabilitiesText, + String packageNames) throws PackageManager.NameNotFoundException { + // TODO: Allow for setting package names without capabilities. In this case, the existing + // capabilities should be used. + if (Strings.isNullOrEmpty(networkCapabilitiesText)) { + return null; + } + NetworkCapabilities.Builder networkCapabilitiesBuilder = + NetworkCapabilities.Builder.withoutDefaultCapabilities() + .addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET); for (int capability : getCapabilitiesList(networkCapabilitiesText)) { networkCapabilitiesBuilder.addCapability(capability); } - EthernetNetworkUpdateRequest request = new EthernetNetworkUpdateRequest.Builder() - .setIpConfiguration(ipConfiguration) - .setNetworkCapabilities(networkCapabilitiesBuilder.build()) - .build(); + if (!Strings.isNullOrEmpty(packageNames)) { + networkCapabilitiesBuilder.setAllowedUids( + UidToPackageNameConverter.convertToUids(mApplicationContext, packageNames)); + } - mEthernetManager.updateConfiguration(interfaceName, request, - mApplicationContext.getMainExecutor(), mCallback); + return networkCapabilitiesBuilder.build(); } private static List<Integer> getCapabilitiesList(String capabilitiesText) diff --git a/tests/RailwayReferenceApp/src/com/google/android/car/networking/railway/MainActivity.java b/tests/RailwayReferenceApp/src/com/google/android/car/networking/railway/MainActivity.java index 3e79b257d7..a40042b7c5 100644 --- a/tests/RailwayReferenceApp/src/com/google/android/car/networking/railway/MainActivity.java +++ b/tests/RailwayReferenceApp/src/com/google/android/car/networking/railway/MainActivity.java @@ -39,6 +39,8 @@ import androidx.fragment.app.FragmentActivity; import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModelProvider; +import com.google.common.base.Strings; + import java.io.IOException; import java.net.Socket; import java.time.Duration; @@ -149,7 +151,16 @@ public final class MainActivity extends FragmentActivity { dialog.show(); } + private String getUpdateConfigurationIface() { + return mInterfaceName.getText().toString(); + } + + private String getEnableDisableConnectIface() { + return mInterfaceName2.getText().toString(); + } + private void onUpdateButtonClick() { + validateInterfaceName(getUpdateConfigurationIface()); setButtonsAndEditTextsEnabled(false); Log.d(TAG, "configuration update started"); try { @@ -157,8 +168,10 @@ public final class MainActivity extends FragmentActivity { mAllowedPackageNames.getText().toString(), mIpConfiguration.getText().toString(), mNetworkCapabilities.getText().toString(), - mInterfaceName.getText().toString()); - } catch (IllegalArgumentException | PackageManager.NameNotFoundException e) { + getUpdateConfigurationIface()); + } catch (IllegalStateException | IllegalArgumentException + | PackageManager.NameNotFoundException e) { + Log.e(TAG, "Error updating: " + e.getMessage()); showOperationResultDialog(e.getLocalizedMessage(), /* isSuccess= */ false); } } @@ -168,12 +181,12 @@ public final class MainActivity extends FragmentActivity { Log.d(TAG, "connect interface started"); try { + validateInterfaceName(getEnableDisableConnectIface()); NetworkRequest request = new NetworkRequest.Builder() .addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET) .setNetworkSpecifier( - new EthernetNetworkSpecifier( - mInterfaceName2.getText().toString())).build(); + new EthernetNetworkSpecifier(getEnableDisableConnectIface())).build(); mConnectivityManager.requestNetwork(request, new InterfaceConnectorCallback(), new Handler(Looper.getMainLooper()), @@ -184,15 +197,23 @@ public final class MainActivity extends FragmentActivity { } private void onEnableButtonClick() { + validateInterfaceName(getEnableDisableConnectIface()); setButtonsAndEditTextsEnabled(false); Log.d(TAG, "enable interface started"); - mInterfaceEnabler.enableInterface(mInterfaceName2.getText().toString()); + mInterfaceEnabler.enableInterface(getEnableDisableConnectIface()); } private void onDisableButtonClick() { + validateInterfaceName(getEnableDisableConnectIface()); setButtonsAndEditTextsEnabled(false); Log.d(TAG, "disable interface started"); - mInterfaceEnabler.disableInterface(mInterfaceName2.getText().toString()); + mInterfaceEnabler.disableInterface(getEnableDisableConnectIface()); + } + + private void validateInterfaceName(String iface) { + if (Strings.isNullOrEmpty(iface)) { + throw new IllegalArgumentException("Interface Name can't be empty."); + } } private class InterfaceConnectorCallback extends ConnectivityManager.NetworkCallback { |