def test_connect_to_open_5g_while_connected_to_wpa_psk_2g(self):
        """
        Initiates a connection to network via network request with specific SSID

        Steps:
        1. Send a network specifier with the specific SSID of Open 5G network.
        2. Wait for platform to scan and find matching networks.
        3. Simulate user selecting the network.
        4. Ensure that the device connects to the network.
        5. Send another network specifier with the specific SSID & credentials
           of WPA-PSK 2G network.
        6. Ensure we disconnect from the previous network.
        7. Wait for platform to scan and find matching networks.
        8. Simulate user selecting the new network.
        9. Ensure that the device connects to the new network.
        """
        # Complete flow for the first request.
        wutils.wifi_connect_using_network_request(self.dut, self.wpa_psk_2g,
                                                  self.wpa_psk_2g)
        # Send the second request.
        self.dut.droid.wifiRequestNetworkWithSpecifier(self.open_5g)
        self.dut.log.info("Sent network request with %s", self.open_5g)
        # Ensure we do not disconnect from the previous network until the user
        # approves the new request.
        self.dut.ed.clear_all_events()
        wutils.ensure_no_disconnect(self.dut)

        # Now complete the flow and ensure we connected to second request.
        wutils.wait_for_wifi_connect_after_network_request(
            self.dut, self.open_5g)
    def test_connect_to_open_5g_after_connecting_to_wpa_psk_2g(self):
        """
        Initiates a connection to network via network request with SSID pattern

        Steps:
        1. Send a network specifier with the specific SSID of Open 5G network.
        2. Wait for platform to scan and find matching networks.
        3. Simulate user selecting the network.
        4. Ensure that the device connects to the network.
        5. Release the network request.
        6. Send another network specifier with the specific SSID & credentials
           of WPA-PSK 2G network.
        7. Ensure we disconnect from the previous network.
        8. Wait for platform to scan and find matching networks.
        9. Simulate user selecting the new network.
        10. Ensure that the device connects to the new network.
        """
        # Complete flow for the first request.
        wutils.wifi_connect_using_network_request(self.dut, self.wpa_psk_2g,
                                                  self.wpa_psk_2g)
        # Release the request.
        self.dut.droid.wifiReleaseNetwork(self.wpa_psk_2g)
        # Ensure we disconnected from the previous network.
        wutils.wait_for_disconnect(self.dut)
        self.dut.log.info("Disconnected from network %s", self.wpa_psk_2g)
        # Complete flow for the second request.
        wutils.wifi_connect_using_network_request(self.dut, self.open_5g,
                                                  self.open_5g)
    def test_connect_to_open_5g_with_ssid(self):
        """
        Initiates a connection to network via network request with specific SSID

        Steps:
        1. Send a network specifier with the specific SSID of Open 5G network.
        2. Wait for platform to scan and find matching networks.
        3. Simulate user selecting the network.
        4. Ensure that the device connects to the network.
        """
        wutils.wifi_connect_using_network_request(self.dut, self.open_5g,
                                                  self.open_5g)
    def test_connect_to_wpa_psk_2g_which_is_already_approved(self):
        """
        Initiates a connection to network via network request with specific SSID
        bypassing user approval.

        Steps:
        1. Send a network specifier with the specific SSID/credentials of
           WPA-PSK 2G network.
        2. Wait for platform to scan and find matching networks.
        3. Simulate user selecting the network.
        4. Ensure that the device connects to the network.
        5. Ensure we disconnect from the previous network.
        6. Send another network specifier with the specific
           SSID/BSSID/credentials of WPA-PSK 2G network.
        7. Ensure that the device bypasses user approval & connects to the
           same network.
        """
        # Complete flow for the first request.
        wutils.wifi_connect_using_network_request(self.dut, self.wpa_psk_2g,
                                                  self.wpa_psk_2g)
        # Release the request.
        self.dut.droid.wifiReleaseNetwork(self.wpa_psk_2g)
        # Ensure we disconnected from the network.
        wutils.wait_for_disconnect(self.dut)
        self.dut.log.info("Disconnected from network %s", self.wpa_psk_2g)
        self.dut.ed.clear_all_events()

        # Find bssid for the WPA-PSK 2G network.
        scan_results = self.dut.droid.wifiGetScanResults()
        match_results = wutils.match_networks(
            {WifiEnums.SSID_KEY: self.wpa_psk_2g[WifiEnums.SSID_KEY]},
            scan_results)
        asserts.assert_equal(len(match_results), 1,
                             "Cannot find bssid for WPA-PSK 2G network")
        bssid = match_results[0][WifiEnums.BSSID_KEY]
        # Send the second request with bssid.
        network_specifier_with_bssid = self.wpa_psk_2g.copy()
        network_specifier_with_bssid[WifiEnums.BSSID_KEY] = bssid
        self.dut.droid.wifiRequestNetworkWithSpecifier(
            network_specifier_with_bssid)
        self.dut.log.info("Sent network request with %r",
                          network_specifier_with_bssid)

        # Ensure we connected to second request without user approval.
        wutils.wait_for_connect(self.dut, self.wpa_psk_2g[WifiEnums.SSID_KEY])
    def test_connect_to_wpa_psk_5g_while_connecting_to_open_2g(self):
        """
        Initiates a connection to network via network request with specific SSID

        Steps:
        1. Send a network specifier with the specific SSID & credentials of
           WPA-PSK 5G network.
        2. Send another network specifier with the specific SSID of Open 2G
           network.
        3. Ensure we disconnect from the previous network.
        4. Wait for platform to scan and find matching networks.
        5. Simulate user selecting the new network.
        6. Ensure that the device connects to the new network.
        """
        # Make the first request.
        self.dut.droid.wifiRequestNetworkWithSpecifier(self.open_2g)
        self.dut.log.info("Sent network request with %s", self.open_2g)
        # Complete flow for the second request.
        wutils.wifi_connect_using_network_request(self.dut, self.wpa_psk_5g,
                                                  self.wpa_psk_5g)
    def test_connect_to_wpa_psk_5g_with_ssid_pattern(self):
        """
        Initiates a connection to network via network request with SSID pattern

        Steps:
        1. Send a network specifier with the SSID pattern/credentials of
           WPA-PSK 5G network.
        2. Wait for platform to scan and find matching networks.
        3. Simulate user selecting the network.
        4. Ensure that the device connects to the network.
        """
        network_specifier = self.wpa_psk_5g.copy()
        # Remove ssid & replace with ssid pattern.
        network_ssid = network_specifier.pop(WifiEnums.SSID_KEY)
        # Remove the last element of ssid & replace with .* to create a matching
        # pattern.
        network_specifier[
            WifiEnums.SSID_PATTERN_KEY] = network_ssid[:-1] + ".*"
        wutils.wifi_connect_using_network_request(self.dut, self.wpa_psk_5g,
                                                  network_specifier)
    def test_connect_to_wpa_psk_2g_which_is_already_approved_but_then_forgot(
            self):
        """
        Initiates a connection to network via network request with specific SSID
        with user approval.

        Steps:
        1. Send a network specifier with the specific SSID/credentials of
           WPA-PSK 2G network.
        2. Wait for platform to scan and find matching networks.
        3. Simulate user selecting the network.
        4. Ensure that the device connects to the network.
        4. Simulate user fogetting the network from the UI.
        6. Ensure we disconnect from the previous network.
        7. Send another network specifier with the specific
           SSID/BSSID/credentials of WPA-PSK 2G network.
        8. Ensure that the device does not bypass user approval & connects to the
           same network with user approval. (This should also clear the blacklist)
        9. Send the same network specifier with the specific
           SSID/BSSID/credentials of WPA-PSK 2G network.
        10.Ensure that the device bypasses user approval now & connects to the
           same network.
        """
        # Complete flow for the first request.
        wutils.wifi_connect_using_network_request(self.dut, self.wpa_psk_2g,
                                                  self.wpa_psk_2g)

        # Simulate user forgeting the ephemeral network.
        self.dut.droid.wifiDisableEphemeralNetwork(
            self.wpa_psk_2g[WifiEnums.SSID_KEY])
        # Ensure we disconnected from the network.
        wutils.wait_for_disconnect(self.dut)
        self.dut.log.info("Disconnected from network %s", self.wpa_psk_2g)
        self.dut.ed.clear_all_events()
        # Release the first request.
        self.dut.droid.wifiReleaseNetwork(self.wpa_psk_2g)

        # Find bssid for the WPA-PSK 2G network.
        scan_results = self.dut.droid.wifiGetScanResults()
        match_results = wutils.match_networks(
            {WifiEnums.SSID_KEY: self.wpa_psk_2g[WifiEnums.SSID_KEY]},
            scan_results)
        asserts.assert_equal(len(match_results), 1,
                             "Cannot find bssid for WPA-PSK 2G network")
        bssid = match_results[0][WifiEnums.BSSID_KEY]
        # Send the second request with bssid.
        network_specifier_with_bssid = self.wpa_psk_2g.copy()
        network_specifier_with_bssid[WifiEnums.BSSID_KEY] = bssid
        self.dut.droid.wifiRequestNetworkWithSpecifier(
            network_specifier_with_bssid)
        self.dut.log.info("Sent network request with %r",
                          network_specifier_with_bssid)

        # Ensure that we did not connect bypassing user approval.
        assert_msg = "Device should not connect without user approval"
        asserts.assert_false(
            wutils.wait_for_connect(self.dut,
                                    self.wpa_psk_2g[WifiEnums.SSID_KEY],
                                    assert_on_fail=False), assert_msg)

        # Now complete the flow and ensure we connected to second request.
        wutils.wait_for_wifi_connect_after_network_request(
            self.dut, self.wpa_psk_2g)

        # Now make the same request again & ensure that we connect without user
        # approval.
        self.dut.droid.wifiRequestNetworkWithSpecifier(
            network_specifier_with_bssid)
        self.dut.log.info("Sent network request with %r",
                          network_specifier_with_bssid)
        wutils.wait_for_connect(self.dut, self.wpa_psk_2g[WifiEnums.SSID_KEY])