diff options
-rw-r--r-- | service/java/com/android/server/wifi/hotspot2/OsuNetworkConnection.java | 21 | ||||
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/hotspot2/OsuNetworkConnectionTest.java | 31 |
2 files changed, 47 insertions, 5 deletions
diff --git a/service/java/com/android/server/wifi/hotspot2/OsuNetworkConnection.java b/service/java/com/android/server/wifi/hotspot2/OsuNetworkConnection.java index 73dcca505..12a99899b 100644 --- a/service/java/com/android/server/wifi/hotspot2/OsuNetworkConnection.java +++ b/service/java/com/android/server/wifi/hotspot2/OsuNetworkConnection.java @@ -16,6 +16,8 @@ package com.android.server.wifi.hotspot2; +import static android.net.NetworkCapabilities.NET_CAPABILITY_TRUSTED; + import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -173,6 +175,12 @@ public class OsuNetworkConnection { } WifiConfiguration config = new WifiConfiguration(); config.SSID = "\"" + ssid.toString() + "\""; + + // To suppress Wi-Fi has no internet access notification. + config.noInternetAccessExpected = true; + + // Do not save this network + config.ephemeral = true; if (TextUtils.isEmpty(nai)) { config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); } else { @@ -185,11 +193,18 @@ public class OsuNetworkConnection { Log.e(TAG, "Unable to add network"); return false; } - NetworkRequest networkRequest = null; - networkRequest = new NetworkRequest.Builder() - .addTransportType(NetworkCapabilities.TRANSPORT_WIFI).build(); + + // NET_CAPABILITY_TRUSTED is added by builder by default. + // But for ephemeral network, the capability needs to be removed + // as wifi stack creates network agent without the capability. + // That could cause connectivity service not to find the matching agent. + NetworkRequest networkRequest = new NetworkRequest.Builder() + .addTransportType(NetworkCapabilities.TRANSPORT_WIFI).removeCapability( + NET_CAPABILITY_TRUSTED).build(); mConnectivityManager.requestNetwork(networkRequest, mConnectivityCallbacks, mHandler, TIMEOUT_MS); + + // TODO(b/112195429): replace it with new connectivity API. if (!mWifiManager.enableNetwork(mNetworkId, true)) { Log.e(TAG, "Unable to enable network " + mNetworkId); disconnectIfNeeded(); diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/OsuNetworkConnectionTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/OsuNetworkConnectionTest.java index a331e9887..ae1f6ce03 100644 --- a/tests/wifitests/src/com/android/server/wifi/hotspot2/OsuNetworkConnectionTest.java +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/OsuNetworkConnectionTest.java @@ -16,6 +16,8 @@ package com.android.server.wifi.hotspot2; +import static android.net.NetworkCapabilities.NET_CAPABILITY_TRUSTED; + import static org.junit.Assert.*; import static org.mockito.Mockito.any; import static org.mockito.Mockito.anyInt; @@ -55,7 +57,7 @@ import org.mockito.MockitoAnnotations; import java.net.InetAddress; /** - * Unit tests for {@link com.android.server.wifi.hotspot2.PasspointProvisioner}. + * Unit tests for {@link OsuNetworkConnection}. */ @SmallTest public class OsuNetworkConnectionTest { @@ -259,5 +261,30 @@ public class OsuNetworkConnectionTest { mNetworkConnection.disconnectIfNeeded(); verify(mWifiManager).removeNetwork(TEST_NETWORK_ID); } -} + /** + * Verifies that {@link WifiConfiguration} has been created properly for OSU network. + * It is supposed to create a network as ephemeral network and suppress no internet access + * notification. + */ + @Test + public void verifyWifiConfigurationForOsuNetwork() { + mNetworkConnection.init(mHandler); + + assertEquals(true, mNetworkConnection.connect(TEST_SSID, TEST_NAI)); + + ArgumentCaptor<WifiConfiguration> wifiConfigurationCaptor = ArgumentCaptor.forClass( + WifiConfiguration.class); + verify(mWifiManager, times(1)).addNetwork(wifiConfigurationCaptor.capture()); + WifiConfiguration wifiConfiguration = wifiConfigurationCaptor.getValue(); + assertTrue(wifiConfiguration.isNoInternetAccessExpected()); + assertTrue(wifiConfiguration.isEphemeral()); + + ArgumentCaptor<NetworkRequest> networkRequestCaptor = ArgumentCaptor.forClass( + NetworkRequest.class); + verify(mConnectivityManager, times(1)).requestNetwork(networkRequestCaptor.capture(), + any(ConnectivityManager.NetworkCallback.class), any(Handler.class), anyInt()); + assertFalse(networkRequestCaptor.getValue().hasCapability(NET_CAPABILITY_TRUSTED)); + + } +} |