wlancfg_lib/client/connection_selection/
network_selection.rs

1// Copyright 2023 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::client::types;
6use std::collections::HashSet;
7
8// Returns zero or more selected networks. Multiple selected networks indicates no preference
9// between them.
10pub fn select_networks(
11    available_networks: HashSet<types::NetworkIdentifier>,
12    network: &Option<types::NetworkIdentifier>,
13) -> HashSet<types::NetworkIdentifier> {
14    match network {
15        Some(network) => HashSet::from([network.clone()]),
16        None => {
17            // TODO(https://fxbug.dev/42064323): Add network selection logic.
18            // Currently, the connection selection is determined solely based on the BSS. All available
19            // networks are allowed.
20            available_networks
21        }
22    }
23}
24
25#[cfg(test)]
26mod test {
27
28    use super::*;
29    #[fuchsia::test]
30    fn select_networks_selects_specified_network() {
31        let ssid = "foo";
32        let all_networks = vec![
33            types::NetworkIdentifier {
34                ssid: ssid.try_into().unwrap(),
35                security_type: types::SecurityType::Wpa3,
36            },
37            types::NetworkIdentifier {
38                ssid: ssid.try_into().unwrap(),
39                security_type: types::SecurityType::Wpa2,
40            },
41        ];
42        let all_network_set = HashSet::from_iter(all_networks.clone());
43
44        // Specifying a network filters to just that network
45        let desired_network = all_networks[0].clone();
46        let selected_network =
47            select_networks(all_network_set.clone(), &Some(desired_network.clone()));
48        assert_eq!(selected_network, HashSet::from([desired_network]));
49
50        // No specified network returns all networks
51        assert_eq!(select_networks(all_network_set.clone(), &None), all_network_set);
52    }
53}