Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8996b2d6 authored by Nate(Qiang) Jiang's avatar Nate(Qiang) Jiang
Browse files

Add set untrusted API for networkSuggestion

Bug: 147766061
Test: atest android.net.wifi
Change-Id: I122643b07447d9514390fd0654bba77ed4dc4a0f
parent 5b4390df
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -30816,6 +30816,7 @@ package android.net.wifi {
    field public static final String SCAN_RESULTS_AVAILABLE_ACTION = "android.net.wifi.SCAN_RESULTS";
    field public static final int STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_DUPLICATE = 3; // 0x3
    field public static final int STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_EXCEEDS_MAX_PER_APP = 4; // 0x4
    field public static final int STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_NOT_ALLOWED = 6; // 0x6
    field public static final int STATUS_NETWORK_SUGGESTIONS_ERROR_APP_DISALLOWED = 2; // 0x2
    field public static final int STATUS_NETWORK_SUGGESTIONS_ERROR_INTERNAL = 1; // 0x1
    field public static final int STATUS_NETWORK_SUGGESTIONS_ERROR_REMOVE_INVALID = 5; // 0x5
@@ -30933,6 +30934,7 @@ package android.net.wifi {
    method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setPasspointConfig(@NonNull android.net.wifi.hotspot2.PasspointConfiguration);
    method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setPriority(@IntRange(from=0) int);
    method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setSsid(@NonNull String);
    method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setUntrusted(boolean);
    method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setWapiEnterpriseConfig(@NonNull android.net.wifi.WifiEnterpriseConfig);
    method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setWapiPassphrase(@NonNull String);
    method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setWpa2EnterpriseConfig(@NonNull android.net.wifi.WifiEnterpriseConfig);
+3 −1
Original line number Diff line number Diff line
@@ -938,8 +938,10 @@ public class WifiConfiguration implements Parcelable {
    }

    /**
     * Indicate whther the network is trusted or not. Networks are considered trusted
     * Indicate whether the network is trusted or not. Networks are considered trusted
     * if the user explicitly allowed this network connection.
     * This bit can be used by suggestion network, see
     * {@link WifiNetworkSuggestion.Builder#setUnTrusted(boolean)}
     * @hide
     */
    public boolean trusted;
+9 −0
Original line number Diff line number Diff line
@@ -193,6 +193,14 @@ public class WifiManager {
     */
    public static final int STATUS_NETWORK_SUGGESTIONS_ERROR_REMOVE_INVALID = 5;

    /**
     * Reason code if one or more of the network suggestions added is not allowed.
     *
     * This error may be caused by suggestion is using SIM-based encryption method, but calling app
     * is not carrier privileged.
     */
    public static final int STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_NOT_ALLOWED = 6;

    /** @hide */
    @IntDef(prefix = { "STATUS_NETWORK_SUGGESTIONS_" }, value = {
            STATUS_NETWORK_SUGGESTIONS_SUCCESS,
@@ -201,6 +209,7 @@ public class WifiManager {
            STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_DUPLICATE,
            STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_EXCEEDS_MAX_PER_APP,
            STATUS_NETWORK_SUGGESTIONS_ERROR_REMOVE_INVALID,
            STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_NOT_ALLOWED,
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface NetworkSuggestionsStatusCode {}
+54 −4
Original line number Diff line number Diff line
@@ -139,6 +139,11 @@ public final class WifiNetworkSuggestion implements Parcelable {
         */
        private @Nullable WifiEnterpriseConfig mWapiEnterpriseConfig;

        /**
         * Whether this network will be brought up as untrusted (TRUSTED capability bit removed).
         */
        private boolean mIsNetworkUntrusted;

        public Builder() {
            mSsid = null;
            mBssid =  null;
@@ -159,6 +164,7 @@ public final class WifiNetworkSuggestion implements Parcelable {
            mCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
            mWapiPskPassphrase = null;
            mWapiEnterpriseConfig = null;
            mIsNetworkUntrusted = false;
        }

        /**
@@ -468,6 +474,27 @@ public final class WifiNetworkSuggestion implements Parcelable {
            return this;
        }

        /**
         * Specifies whether the system will bring up the network (if selected) as untrusted. An
         * untrusted network has its {@link android.net.NetworkCapabilities#NET_CAPABILITY_TRUSTED}
         * capability removed. The Wi-Fi network selection process may use this information to
         * influence priority of the suggested network for Wi-Fi network selection (most likely to
         * reduce it). The connectivity service may use this information to influence the overall
         * network configuration of the device.
         * <p>
         * <li> An untrusted network's credentials may not be shared with the user using
         * {@link #setCredentialSharedWithUser(boolean)}.</li>
         * <li> If not set, defaults to false (i.e. network is trusted).</li>
         *
         * @param isUntrusted Boolean indicating whether the network should be brought up untrusted
         *                    (if true) or trusted (if false).
         * @return Instance of {@link Builder} to enable chaining of the builder method.
         */
        public @NonNull Builder setUntrusted(boolean isUntrusted) {
            mIsNetworkUntrusted = isUntrusted;
            return this;
        }

        private void setSecurityParamsInWifiConfiguration(
                @NonNull WifiConfiguration configuration) {
            if (!TextUtils.isEmpty(mWpa2PskPassphrase)) { // WPA-PSK network.
@@ -546,6 +573,7 @@ public final class WifiNetworkSuggestion implements Parcelable {
            wifiConfiguration.meteredOverride =
                    mIsMetered ? WifiConfiguration.METERED_OVERRIDE_METERED
                            : WifiConfiguration.METERED_OVERRIDE_NONE;
            wifiConfiguration.trusted = !mIsNetworkUntrusted;
            mPasspointConfiguration.setCarrierId(mCarrierId);
            return wifiConfiguration;
        }
@@ -641,13 +669,22 @@ public final class WifiNetworkSuggestion implements Parcelable {
                        + "setCredentialSharedWithUser and "
                        + "setIsAutoJoinEnabled set to false");
            }
            if (mIsNetworkUntrusted) {
                if (mIsSharedWithUserSet && mIsSharedWithUser) {
                    throw new IllegalStateException("Should not be both"
                            + "setCredentialSharedWithUser and +"
                            + "setIsNetworkAsUntrusted to true");
                }
                mIsSharedWithUser = false;
            }
            return new WifiNetworkSuggestion(
                    wifiConfiguration,
                    mPasspointConfiguration,
                    mIsAppInteractionRequired,
                    mIsUserInteractionRequired,
                    mIsSharedWithUser,
                    mIsInitialAutoJoinEnabled);
                    mIsInitialAutoJoinEnabled,
                    mIsNetworkUntrusted);
        }
    }

@@ -688,6 +725,13 @@ public final class WifiNetworkSuggestion implements Parcelable {
     */
    public final boolean isInitialAutoJoinEnabled;

    /**
     * Whether this network will be brought up as untrusted (TRUSTED capability bit removed).
     * @hide
     */
    public final boolean isNetworkUntrusted;


    /** @hide */
    public WifiNetworkSuggestion() {
        this.wifiConfiguration = null;
@@ -696,6 +740,7 @@ public final class WifiNetworkSuggestion implements Parcelable {
        this.isUserInteractionRequired = false;
        this.isUserAllowedToManuallyConnect = true;
        this.isInitialAutoJoinEnabled = true;
        this.isNetworkUntrusted = false;
    }

    /** @hide */
@@ -704,7 +749,8 @@ public final class WifiNetworkSuggestion implements Parcelable {
                                 boolean isAppInteractionRequired,
                                 boolean isUserInteractionRequired,
                                 boolean isUserAllowedToManuallyConnect,
                                 boolean isInitialAutoJoinEnabled) {
                                 boolean isInitialAutoJoinEnabled,
            boolean isNetworkUntrusted) {
        checkNotNull(networkConfiguration);
        this.wifiConfiguration = networkConfiguration;
        this.passpointConfiguration = passpointConfiguration;
@@ -713,6 +759,7 @@ public final class WifiNetworkSuggestion implements Parcelable {
        this.isUserInteractionRequired = isUserInteractionRequired;
        this.isUserAllowedToManuallyConnect = isUserAllowedToManuallyConnect;
        this.isInitialAutoJoinEnabled = isInitialAutoJoinEnabled;
        this.isNetworkUntrusted = isNetworkUntrusted;
    }

    public static final @NonNull Creator<WifiNetworkSuggestion> CREATOR =
@@ -725,7 +772,8 @@ public final class WifiNetworkSuggestion implements Parcelable {
                            in.readBoolean(), // isAppInteractionRequired
                            in.readBoolean(), // isUserInteractionRequired
                            in.readBoolean(), // isSharedCredentialWithUser
                            in.readBoolean()  // isAutoJoinEnabled
                            in.readBoolean(),  // isAutoJoinEnabled
                            in.readBoolean()
                    );
                }

@@ -748,6 +796,7 @@ public final class WifiNetworkSuggestion implements Parcelable {
        dest.writeBoolean(isUserInteractionRequired);
        dest.writeBoolean(isUserAllowedToManuallyConnect);
        dest.writeBoolean(isInitialAutoJoinEnabled);
        dest.writeBoolean(isNetworkUntrusted);
    }

    @Override
@@ -787,8 +836,9 @@ public final class WifiNetworkSuggestion implements Parcelable {
                .append(", FQDN=").append(wifiConfiguration.FQDN)
                .append(", isAppInteractionRequired=").append(isAppInteractionRequired)
                .append(", isUserInteractionRequired=").append(isUserInteractionRequired)
                .append(", isUserAllowedToManuallyConnect=").append(isUserAllowedToManuallyConnect)
                .append(", isCredentialSharedWithUser=").append(isUserAllowedToManuallyConnect)
                .append(", isInitialAutoJoinEnabled=").append(isInitialAutoJoinEnabled)
                .append(", isUnTrusted=").append(isNetworkUntrusted)
                .append(" ]");
        return sb.toString();
    }
+58 −11
Original line number Diff line number Diff line
@@ -532,7 +532,7 @@ public class WifiNetworkSuggestionTest {
        configuration.BSSID = TEST_BSSID;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        WifiNetworkSuggestion suggestion = new WifiNetworkSuggestion(
                configuration, null, false, true, true, false);
                configuration, null, false, true, true, true, false);

        Parcel parcelW = Parcel.obtain();
        suggestion.writeToParcel(parcelW, 0);
@@ -603,14 +603,14 @@ public class WifiNetworkSuggestionTest {
        configuration.BSSID = TEST_BSSID;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        WifiNetworkSuggestion suggestion =
                new WifiNetworkSuggestion(configuration, null, true, false, true, true);
                new WifiNetworkSuggestion(configuration, null, true, false, true, true, false);

        WifiConfiguration configuration1 = new WifiConfiguration();
        configuration1.SSID = TEST_SSID;
        configuration1.BSSID = TEST_BSSID;
        configuration1.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        WifiNetworkSuggestion suggestion1 =
                new WifiNetworkSuggestion(configuration1, null, false, true, true, false);
                new WifiNetworkSuggestion(configuration1, null, false, true, true, true, false);

        assertEquals(suggestion, suggestion1);
        assertEquals(suggestion.hashCode(), suggestion1.hashCode());
@@ -626,13 +626,13 @@ public class WifiNetworkSuggestionTest {
        configuration.SSID = TEST_SSID;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        WifiNetworkSuggestion suggestion =
                new WifiNetworkSuggestion(configuration, null, false, false, true, false);
                new WifiNetworkSuggestion(configuration, null, false, false, true, true, false);

        WifiConfiguration configuration1 = new WifiConfiguration();
        configuration1.SSID = TEST_SSID_1;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        WifiNetworkSuggestion suggestion1 =
                new WifiNetworkSuggestion(configuration1, null, false, false, true, false);
                new WifiNetworkSuggestion(configuration1, null, false, false, true, true, false);

        assertNotEquals(suggestion, suggestion1);
    }
@@ -648,13 +648,13 @@ public class WifiNetworkSuggestionTest {
        configuration.BSSID = TEST_BSSID;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        WifiNetworkSuggestion suggestion =
                new WifiNetworkSuggestion(configuration, null,  false, false, true, true);
                new WifiNetworkSuggestion(configuration, null,  false, false, true, true, false);

        WifiConfiguration configuration1 = new WifiConfiguration();
        configuration1.SSID = TEST_SSID;
        configuration1.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        WifiNetworkSuggestion suggestion1 =
                new WifiNetworkSuggestion(configuration1, null, false, false, true, true);
                new WifiNetworkSuggestion(configuration1, null, false, false, true, true, false);

        assertNotEquals(suggestion, suggestion1);
    }
@@ -669,13 +669,13 @@ public class WifiNetworkSuggestionTest {
        configuration.SSID = TEST_SSID;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        WifiNetworkSuggestion suggestion =
                new WifiNetworkSuggestion(configuration, null, false, false, true, true);
                new WifiNetworkSuggestion(configuration, null, false, false, true, true, false);

        WifiConfiguration configuration1 = new WifiConfiguration();
        configuration1.SSID = TEST_SSID;
        configuration1.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        WifiNetworkSuggestion suggestion1 =
                new WifiNetworkSuggestion(configuration1, null, false, false, true, true);
                new WifiNetworkSuggestion(configuration1, null, false, false, true, true, false);

        assertNotEquals(suggestion, suggestion1);
    }
@@ -723,8 +723,8 @@ public class WifiNetworkSuggestionTest {
     * true on a open network suggestion.
     */
    @Test(expected = IllegalStateException.class)
    public void testSetIsUserAllowedToManuallyConnectToWithOpenNetwork() {
        new WifiNetworkSuggestion.Builder()
    public void testSetCredentialSharedWithUserWithOpenNetwork() {
        WifiNetworkSuggestion suggestion = new WifiNetworkSuggestion.Builder()
                .setSsid(TEST_SSID)
                .setCredentialSharedWithUser(true)
                .build();
@@ -759,6 +759,53 @@ public class WifiNetworkSuggestionTest {
                .build();
    }

    /**
     * Validate {@link WifiNetworkSuggestion.Builder#setCredentialSharedWithUser(boolean)} set the
     * correct value to the WifiConfiguration.
     */
    @Test
    public void testSetIsNetworkAsUntrusted() {
        WifiNetworkSuggestion suggestion = new WifiNetworkSuggestion.Builder()
                .setSsid(TEST_SSID)
                .setWpa2Passphrase(TEST_PRESHARED_KEY)
                .setUntrusted(true)
                .build();
        assertTrue(suggestion.isNetworkUntrusted);
        assertFalse(suggestion.isUserAllowedToManuallyConnect);
    }

    /**
     * Validate {@link WifiNetworkSuggestion.Builder#setCredentialSharedWithUser(boolean)} set the
     * correct value to the WifiConfiguration.
     * Also the {@link WifiNetworkSuggestion#isUserAllowedToManuallyConnect} should be false;
     */
    @Test
    public void testSetIsNetworkAsUntrustedOnPasspointNetwork() {
        PasspointConfiguration passpointConfiguration = PasspointTestUtils.createConfig();
        WifiNetworkSuggestion suggestion = new WifiNetworkSuggestion.Builder()
                .setPasspointConfig(passpointConfiguration)
                .setUntrusted(true)
                .build();
        assertTrue(suggestion.isNetworkUntrusted);
        assertFalse(suggestion.isUserAllowedToManuallyConnect);
    }

    /**
     * Ensure {@link WifiNetworkSuggestion.Builder#build()} throws an exception
     * when set {@link WifiNetworkSuggestion.Builder#setUntrusted(boolean)} to true and
     * set {@link WifiNetworkSuggestion.Builder#setCredentialSharedWithUser(boolean)} to true
     * together.
     */
    @Test(expected = IllegalStateException.class)
    public void testSetCredentialSharedWithUserWithSetIsNetworkAsUntrusted() {
        new WifiNetworkSuggestion.Builder()
                .setSsid(TEST_SSID)
                .setWpa2Passphrase(TEST_PRESHARED_KEY)
                .setCredentialSharedWithUser(true)
                .setUntrusted(true)
                .build();
    }

    /**
     * Ensure {@link WifiNetworkSuggestion.Builder#build()} throws an exception
     * when set both {@link WifiNetworkSuggestion.Builder#setIsInitialAutoJoinEnabled(boolean)}