Skip to main content

wlan_common/
bss.rs

1// Copyright 2021 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::channel::Channel;
6use crate::ie::owe_transition::{OweTransition, parse_owe_transition};
7use crate::ie::rsn::suite_filter;
8use crate::ie::wsc::{ProbeRespWsc, parse_probe_resp_wsc};
9use crate::ie::{self, IeType};
10use crate::mac::CapabilityInfo;
11use anyhow::format_err;
12use fidl_fuchsia_wlan_ieee80211 as fidl_ieee80211;
13use fidl_fuchsia_wlan_sme as fidl_sme;
14use ieee80211::{Bssid, MacAddrBytes, Ssid};
15use static_assertions::assert_eq_size;
16use std::cmp::Ordering;
17use std::collections::HashMap;
18use std::fmt;
19use std::hash::Hash;
20use std::ops::Range;
21use zerocopy::{IntoBytes, Ref};
22
23#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)]
24pub enum Protection {
25    Unknown,
26    Open,
27    OpenOweTransition,
28    Owe,
29    Wep,
30    Wpa1,
31    Wpa1Wpa2PersonalTkipOnly,
32    Wpa2PersonalTkipOnly,
33    Wpa1Wpa2Personal,
34    Wpa2Personal,
35    Wpa2Wpa3Personal,
36    Wpa3Personal,
37    Wpa2Enterprise,
38    /// WPA3 Enterprise 192-bit mode. WPA3 spec specifies an optional 192-bit mode but says nothing
39    /// about a non 192-bit version. Thus, colloquially, it's likely that the term WPA3 Enterprise
40    /// will be used to refer to WPA3 Enterprise 192-bit mode.
41    Wpa3Enterprise,
42}
43
44impl From<Protection> for fidl_sme::Protection {
45    fn from(protection: Protection) -> fidl_sme::Protection {
46        match protection {
47            Protection::Unknown => fidl_sme::Protection::Unknown,
48            Protection::Open => fidl_sme::Protection::Open,
49            Protection::OpenOweTransition => fidl_sme::Protection::OpenOweTransition,
50            Protection::Owe => fidl_sme::Protection::Owe,
51            Protection::Wep => fidl_sme::Protection::Wep,
52            Protection::Wpa1 => fidl_sme::Protection::Wpa1,
53            Protection::Wpa1Wpa2PersonalTkipOnly => fidl_sme::Protection::Wpa1Wpa2PersonalTkipOnly,
54            Protection::Wpa2PersonalTkipOnly => fidl_sme::Protection::Wpa2PersonalTkipOnly,
55            Protection::Wpa1Wpa2Personal => fidl_sme::Protection::Wpa1Wpa2Personal,
56            Protection::Wpa2Personal => fidl_sme::Protection::Wpa2Personal,
57            Protection::Wpa2Wpa3Personal => fidl_sme::Protection::Wpa2Wpa3Personal,
58            Protection::Wpa3Personal => fidl_sme::Protection::Wpa3Personal,
59            Protection::Wpa2Enterprise => fidl_sme::Protection::Wpa2Enterprise,
60            Protection::Wpa3Enterprise => fidl_sme::Protection::Wpa3Enterprise,
61        }
62    }
63}
64
65impl From<fidl_sme::Protection> for Protection {
66    fn from(protection: fidl_sme::Protection) -> Self {
67        match protection {
68            fidl_sme::Protection::Unknown => Protection::Unknown,
69            fidl_sme::Protection::Open => Protection::Open,
70            fidl_sme::Protection::OpenOweTransition => Protection::OpenOweTransition,
71            fidl_sme::Protection::Owe => Protection::Owe,
72            fidl_sme::Protection::Wep => Protection::Wep,
73            fidl_sme::Protection::Wpa1 => Protection::Wpa1,
74            fidl_sme::Protection::Wpa1Wpa2PersonalTkipOnly => Protection::Wpa1Wpa2PersonalTkipOnly,
75            fidl_sme::Protection::Wpa2PersonalTkipOnly => Protection::Wpa2PersonalTkipOnly,
76            fidl_sme::Protection::Wpa1Wpa2Personal => Protection::Wpa1Wpa2Personal,
77            fidl_sme::Protection::Wpa2Personal => Protection::Wpa2Personal,
78            fidl_sme::Protection::Wpa2Wpa3Personal => Protection::Wpa2Wpa3Personal,
79            fidl_sme::Protection::Wpa3Personal => Protection::Wpa3Personal,
80            fidl_sme::Protection::Wpa2Enterprise => Protection::Wpa2Enterprise,
81            fidl_sme::Protection::Wpa3Enterprise => Protection::Wpa3Enterprise,
82        }
83    }
84}
85
86impl fmt::Display for Protection {
87    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88        match self {
89            Protection::Unknown => write!(f, "{}", "Unknown"),
90            Protection::Open => write!(f, "{}", "Open"),
91            Protection::OpenOweTransition => write!(f, "{}", "Open OWE Transition"),
92            Protection::Owe => write!(f, "{}", "OWE"),
93            Protection::Wep => write!(f, "{}", "WEP"),
94            Protection::Wpa1 => write!(f, "{}", "WPA1"),
95            Protection::Wpa1Wpa2PersonalTkipOnly => write!(f, "{}", "WPA1/2 PSK TKIP"),
96            Protection::Wpa2PersonalTkipOnly => write!(f, "{}", "WPA2 PSK TKIP"),
97            Protection::Wpa1Wpa2Personal => write!(f, "{}", "WPA1/2 PSK"),
98            Protection::Wpa2Personal => write!(f, "{}", "WPA2 PSK"),
99            Protection::Wpa2Wpa3Personal => write!(f, "{}", "WPA2/3 PSK"),
100            Protection::Wpa3Personal => write!(f, "{}", "WPA3 PSK"),
101            Protection::Wpa2Enterprise => write!(f, "{}", "WPA2 802.1X"),
102            Protection::Wpa3Enterprise => write!(f, "{}", "WPA3 802.1X"),
103        }
104    }
105}
106
107#[derive(Clone, Debug, Eq, Hash, PartialEq)]
108pub enum Standard {
109    Dot11A,
110    Dot11B,
111    Dot11G,
112    Dot11N,
113    Dot11Ac,
114}
115
116#[derive(Debug, Clone, PartialEq)]
117pub struct BssDescription {
118    // *** Fields originally in fidl_common::BssDescription
119    pub ssid: Ssid,
120    pub bssid: Bssid,
121    pub bss_type: fidl_ieee80211::BssType,
122    pub beacon_period: u16,
123    pub capability_info: u16,
124    pub channel: Channel,
125    pub rssi_dbm: i8,
126    pub snr_db: i8,
127    // Private because the parsed information reference the IEs
128    ies: Vec<u8>,
129
130    // *** Fields parsed out of fidl_common::BssDescription IEs
131    // IEEE Std 802.11-2016 9.4.2.3
132    // in 0.5 Mbps, with MSB indicating basic rate. See Table 9-78 for 126, 127.
133    // The rates here may include both the basic rates and extended rates, which are not
134    // continuous slices, hence we cannot use `Range`.
135    rates: Vec<ie::SupportedRate>,
136    tim_range: Option<Range<usize>>,
137    country_range: Option<Range<usize>>,
138    rsne_range: Option<Range<usize>>,
139    ht_cap_range: Option<Range<usize>>,
140    ht_op_range: Option<Range<usize>>,
141    rm_enabled_cap_range: Option<Range<usize>>,
142    ext_cap_range: Option<Range<usize>>,
143    vht_cap_range: Option<Range<usize>>,
144    vht_op_range: Option<Range<usize>>,
145    rsnxe_range: Option<Range<usize>>,
146    owe_transition_range: Option<Range<usize>>,
147}
148
149impl BssDescription {
150    pub fn rates(&self) -> &[ie::SupportedRate] {
151        &self.rates[..]
152    }
153
154    pub fn dtim_period(&self) -> u8 {
155        self.tim_range
156            .as_ref()
157            .map(|range|
158            // Safe to unwrap because we made sure TIM is parseable in `from_fidl`
159            ie::parse_tim(&self.ies[range.clone()]).unwrap().header.dtim_period)
160            .unwrap_or(0)
161    }
162
163    pub fn country(&self) -> Option<&[u8]> {
164        self.country_range.as_ref().map(|range| &self.ies[range.clone()])
165    }
166
167    pub fn rsne(&self) -> Option<&[u8]> {
168        self.rsne_range.as_ref().map(|range| &self.ies[range.clone()])
169    }
170
171    pub fn ht_cap(&self) -> Option<Ref<&[u8], ie::HtCapabilities>> {
172        self.ht_cap_range.clone().map(|range| {
173            // Safe to unwrap because we already verified HT caps is parseable in `from_fidl`
174            ie::parse_ht_capabilities(&self.ies[range]).unwrap()
175        })
176    }
177
178    pub fn raw_ht_cap(&self) -> Option<fidl_ieee80211::HtCapabilities> {
179        type HtCapArray = [u8; fidl_ieee80211::HT_CAP_LEN as usize];
180        self.ht_cap().map(|ht_cap| {
181            assert_eq_size!(ie::HtCapabilities, HtCapArray);
182            let bytes: HtCapArray = ht_cap.as_bytes().try_into().unwrap();
183            fidl_ieee80211::HtCapabilities { bytes }
184        })
185    }
186
187    pub fn ht_op(&self) -> Option<Ref<&[u8], ie::HtOperation>> {
188        self.ht_op_range.clone().map(|range| {
189            // Safe to unwrap because we already verified HT op is parseable in `from_fidl`
190            ie::parse_ht_operation(&self.ies[range]).unwrap()
191        })
192    }
193
194    pub fn rm_enabled_cap(&self) -> Option<Ref<&[u8], ie::RmEnabledCapabilities>> {
195        self.rm_enabled_cap_range.clone().map(|range| {
196            // Safe to unwrap because we already verified RM enabled cap is parseable in `from_fidl`
197            ie::parse_rm_enabled_capabilities(&self.ies[range]).unwrap()
198        })
199    }
200
201    pub fn ext_cap(&self) -> Option<ie::ExtCapabilitiesView<&[u8]>> {
202        self.ext_cap_range.clone().map(|range| ie::parse_ext_capabilities(&self.ies[range]))
203    }
204
205    pub fn raw_ht_op(&self) -> Option<fidl_ieee80211::HtOperation> {
206        type HtOpArray = [u8; fidl_ieee80211::HT_OP_LEN as usize];
207        self.ht_op().map(|ht_op| {
208            assert_eq_size!(ie::HtOperation, HtOpArray);
209            let bytes: HtOpArray = ht_op.as_bytes().try_into().unwrap();
210            fidl_ieee80211::HtOperation { bytes }
211        })
212    }
213
214    pub fn vht_cap(&self) -> Option<Ref<&[u8], ie::VhtCapabilities>> {
215        self.vht_cap_range.clone().map(|range| {
216            // Safe to unwrap because we already verified VHT caps is parseable in `from_fidl`
217            ie::parse_vht_capabilities(&self.ies[range]).unwrap()
218        })
219    }
220
221    pub fn raw_vht_cap(&self) -> Option<fidl_ieee80211::VhtCapabilities> {
222        type VhtCapArray = [u8; fidl_ieee80211::VHT_CAP_LEN as usize];
223        self.vht_cap().map(|vht_cap| {
224            assert_eq_size!(ie::VhtCapabilities, VhtCapArray);
225            let bytes: VhtCapArray = vht_cap.as_bytes().try_into().unwrap();
226            fidl_ieee80211::VhtCapabilities { bytes }
227        })
228    }
229
230    pub fn vht_op(&self) -> Option<Ref<&[u8], ie::VhtOperation>> {
231        self.vht_op_range.clone().map(|range| {
232            // Safe to unwrap because we already verified VHT op is parseable in `from_fidl`
233            ie::parse_vht_operation(&self.ies[range]).unwrap()
234        })
235    }
236
237    pub fn raw_vht_op(&self) -> Option<fidl_ieee80211::VhtOperation> {
238        type VhtOpArray = [u8; fidl_ieee80211::VHT_OP_LEN as usize];
239        self.vht_op().map(|vht_op| {
240            assert_eq_size!(ie::VhtOperation, VhtOpArray);
241            let bytes: VhtOpArray = vht_op.as_bytes().try_into().unwrap();
242            fidl_ieee80211::VhtOperation { bytes }
243        })
244    }
245
246    pub fn rsnxe(&self) -> Option<ie::RsnxeView<&[u8]>> {
247        self.rsnxe_range.clone().map(|range| ie::parse_rsnxe(&self.ies[range]))
248    }
249
250    pub fn ies(&self) -> &[u8] {
251        &self.ies[..]
252    }
253
254    /// Return bool on whether BSS is protected.
255    pub fn is_protected(&self) -> bool {
256        self.protection() != Protection::Open && self.protection() != Protection::OpenOweTransition
257    }
258
259    /// Return bool on whether BSS has security type that would require exchanging EAPOL frames.
260    pub fn needs_eapol_exchange(&self) -> bool {
261        match self.protection() {
262            Protection::Unknown
263            | Protection::Open
264            | Protection::OpenOweTransition
265            | Protection::Wep => false,
266            _ => true,
267        }
268    }
269
270    /// Categorize BSS on what protection it supports.
271    pub fn protection(&self) -> Protection {
272        if !CapabilityInfo(self.capability_info).privacy() {
273            if self.owe_transition_range.is_some() {
274                return Protection::OpenOweTransition;
275            } else {
276                return Protection::Open;
277            }
278        }
279
280        let supports_wpa_1 = self
281            .wpa_ie()
282            .map(|wpa_ie| {
283                let rsne = ie::rsn::rsne::Rsne {
284                    group_data_cipher_suite: Some(wpa_ie.multicast_cipher),
285                    pairwise_cipher_suites: wpa_ie.unicast_cipher_list,
286                    akm_suites: wpa_ie.akm_list,
287                    ..Default::default()
288                };
289                suite_filter::WPA1_PERSONAL.is_satisfied(&rsne)
290            })
291            .unwrap_or(false);
292
293        let rsne = match self.rsne() {
294            Some(rsne) => match ie::rsn::rsne::from_bytes(rsne) {
295                Ok((_, rsne)) => rsne,
296                Err(_e) => {
297                    return Protection::Unknown;
298                }
299            },
300            None if self.find_wpa_ie().is_some() => {
301                if supports_wpa_1 {
302                    return Protection::Wpa1;
303                } else {
304                    return Protection::Unknown;
305                }
306            }
307            None => return Protection::Wep,
308        };
309
310        let rsn_caps = rsne.rsn_capabilities.as_ref().unwrap_or(&ie::rsn::rsne::RsnCapabilities(0));
311        let mfp_req = rsn_caps.mgmt_frame_protection_req();
312        let mfp_cap = rsn_caps.mgmt_frame_protection_cap();
313
314        if suite_filter::WPA3_PERSONAL.is_satisfied(&rsne) {
315            if suite_filter::WPA2_PERSONAL.is_satisfied(&rsne) {
316                if mfp_cap {
317                    return Protection::Wpa2Wpa3Personal;
318                }
319            } else if mfp_cap && mfp_req {
320                return Protection::Wpa3Personal;
321            }
322        }
323        if suite_filter::WPA2_PERSONAL.is_satisfied(&rsne) {
324            if supports_wpa_1 {
325                return Protection::Wpa1Wpa2Personal;
326            } else {
327                return Protection::Wpa2Personal;
328            }
329        }
330        if suite_filter::WPA2_PERSONAL_TKIP_ONLY.is_satisfied(&rsne) {
331            if supports_wpa_1 {
332                return Protection::Wpa1Wpa2PersonalTkipOnly;
333            } else {
334                return Protection::Wpa2PersonalTkipOnly;
335            }
336        }
337        if supports_wpa_1 {
338            return Protection::Wpa1;
339        }
340        if suite_filter::WPA3_ENTERPRISE_192_BIT.is_satisfied(&rsne) {
341            if mfp_cap && mfp_req {
342                return Protection::Wpa3Enterprise;
343            }
344        }
345        if suite_filter::WPA2_ENTERPRISE.is_satisfied(&rsne) {
346            return Protection::Wpa2Enterprise;
347        }
348        if suite_filter::OWE.is_satisfied(&rsne) {
349            return Protection::Owe;
350        }
351        Protection::Unknown
352    }
353
354    /// Get the latest WLAN standard that the BSS supports.
355    pub fn latest_standard(&self) -> Standard {
356        if self.vht_cap().is_some() && self.vht_op().is_some() {
357            Standard::Dot11Ac
358        } else if self.ht_cap().is_some() && self.ht_op().is_some() {
359            Standard::Dot11N
360        } else if self.channel.primary <= 14 {
361            if self.rates.iter().any(|r| match r.rate() {
362                12 | 18 | 24 | 36 | 48 | 72 | 96 | 108 => true,
363                _ => false,
364            }) {
365                Standard::Dot11G
366            } else {
367                Standard::Dot11B
368            }
369        } else {
370            Standard::Dot11A
371        }
372    }
373
374    /// Search for vendor-specific Info Element for WPA. If found, return the body.
375    pub fn find_wpa_ie(&self) -> Option<&[u8]> {
376        ie::Reader::new(&self.ies[..])
377            .filter_map(|(id, ie)| match id {
378                ie::Id::VENDOR_SPECIFIC => match ie::parse_vendor_ie(ie) {
379                    Ok(ie::VendorIe::MsftLegacyWpa(body)) => Some(&body[..]),
380                    _ => None,
381                },
382                _ => None,
383            })
384            .next()
385    }
386
387    /// Search for WPA Info Element and parse it. If no WPA Info Element is found, or a WPA Info
388    /// Element is found but is not valid, return an error.
389    pub fn wpa_ie(&self) -> Result<ie::wpa::WpaIe, anyhow::Error> {
390        ie::parse_wpa_ie(self.find_wpa_ie().ok_or_else(|| format_err!("no wpa ie found"))?)
391            .map_err(|e| e.into())
392    }
393
394    /// Search for vendor-specific Info Element for WMM Parameter. If found, return the body.
395    pub fn find_wmm_param(&self) -> Option<&[u8]> {
396        ie::Reader::new(&self.ies[..])
397            .filter_map(|(id, ie)| match id {
398                ie::Id::VENDOR_SPECIFIC => match ie::parse_vendor_ie(ie) {
399                    Ok(ie::VendorIe::WmmParam(body)) => Some(&body[..]),
400                    _ => None,
401                },
402                _ => None,
403            })
404            .next()
405    }
406
407    /// Search for WMM Parameter Element and parse it. If no WMM Parameter Element is found,
408    /// return an error.
409    pub fn wmm_param(&self) -> Result<Ref<&[u8], ie::WmmParam>, anyhow::Error> {
410        ie::parse_wmm_param(
411            self.find_wmm_param().ok_or_else(|| format_err!("no wmm parameter found"))?,
412        )
413        .map_err(|e| e.into())
414    }
415
416    /// Search for the WiFi Simple Configuration Info Element. If found, return the body.
417    pub fn find_wsc_ie(&self) -> Option<&[u8]> {
418        ie::Reader::new(&self.ies[..])
419            .filter_map(|(id, ie)| match id {
420                ie::Id::VENDOR_SPECIFIC => match ie::parse_vendor_ie(ie) {
421                    Ok(ie::VendorIe::Wsc(body)) => Some(&body[..]),
422                    _ => None,
423                },
424                _ => None,
425            })
426            .next()
427    }
428
429    pub fn probe_resp_wsc(&self) -> Option<ProbeRespWsc> {
430        match self.find_wsc_ie() {
431            Some(ie) => match parse_probe_resp_wsc(ie) {
432                Ok(wsc) => Some(wsc),
433                // Parsing could fail because the WSC IE comes from a beacon, which does
434                // not contain all the information that a probe response WSC is expected
435                // to have. We don't have the information to distinguish between a beacon
436                // and a probe response, so we let this case fail silently.
437                Err(_) => None,
438            },
439            None => None,
440        }
441    }
442
443    pub fn owe_transition(&self) -> Option<OweTransition> {
444        self.owe_transition_range.clone().map(|range| {
445            // Safe to unwrap because we already verified OWE Transition is parseable in TryFrom
446            parse_owe_transition(&self.ies[range]).unwrap()
447        })
448    }
449
450    pub fn supports_uapsd(&self) -> bool {
451        let wmm_info = ie::Reader::new(&self.ies[..])
452            .filter_map(|(id, ie)| match id {
453                ie::Id::VENDOR_SPECIFIC => match ie::parse_vendor_ie(ie) {
454                    Ok(ie::VendorIe::WmmInfo(body)) => {
455                        ie::parse_wmm_info(body).map(|wmm_info| *wmm_info).ok()
456                    }
457                    Ok(ie::VendorIe::WmmParam(body)) => {
458                        ie::parse_wmm_param(body).map(|wmm_param| wmm_param.wmm_info).ok()
459                    }
460                    _ => None,
461                },
462                _ => None,
463            })
464            .next();
465        wmm_info.map(|wmm_info| wmm_info.ap_wmm_info().uapsd()).unwrap_or(false)
466    }
467
468    /// IEEE 802.11-2016 4.5.4.8
469    pub fn supports_ft(&self) -> bool {
470        ie::Reader::new(&self.ies[..]).any(|(id, _ie)| id == ie::Id::MOBILITY_DOMAIN)
471    }
472
473    /// Returns a simplified BssCandidacy which implements PartialOrd.
474    pub fn candidacy(&self) -> BssCandidacy {
475        let rssi_dbm = self.rssi_dbm;
476        match rssi_dbm {
477            // The value 0 is considered a marker for an invalid RSSI and is therefore
478            // transformed to the minimum RSSI value.
479            0 => BssCandidacy { protection: self.protection(), rssi_dbm: i8::MIN },
480            _ => BssCandidacy { protection: self.protection(), rssi_dbm },
481        }
482    }
483
484    /// Returns a string representation of the BssDescriptionExt. This representation
485    /// is not suitable for protecting the privacy of an SSID and BSSID.
486    pub fn to_non_obfuscated_string(&self) -> String {
487        format!(
488            "SSID: {}, BSSID: {}, Protection: {}, Pri Chan: {}, Rx dBm: {}",
489            self.ssid.to_string_not_redactable(),
490            self.bssid,
491            self.protection(),
492            self.channel.primary,
493            self.rssi_dbm,
494        )
495    }
496
497    pub fn is_open(&self) -> bool {
498        matches!(self.protection(), Protection::Open | Protection::OpenOweTransition)
499    }
500
501    pub fn has_owe_configured(&self) -> bool {
502        matches!(self.protection(), Protection::Owe)
503    }
504
505    pub fn has_wep_configured(&self) -> bool {
506        matches!(self.protection(), Protection::Wep)
507    }
508
509    pub fn has_wpa1_configured(&self) -> bool {
510        matches!(
511            self.protection(),
512            Protection::Wpa1 | Protection::Wpa1Wpa2PersonalTkipOnly | Protection::Wpa1Wpa2Personal
513        )
514    }
515
516    pub fn has_wpa2_personal_configured(&self) -> bool {
517        matches!(
518            self.protection(),
519            Protection::Wpa1Wpa2PersonalTkipOnly
520                | Protection::Wpa1Wpa2Personal
521                | Protection::Wpa2PersonalTkipOnly
522                | Protection::Wpa2Personal
523                | Protection::Wpa2Wpa3Personal
524        )
525    }
526
527    pub fn has_wpa3_personal_configured(&self) -> bool {
528        matches!(self.protection(), Protection::Wpa2Wpa3Personal | Protection::Wpa3Personal)
529    }
530}
531
532impl From<BssDescription> for fidl_ieee80211::BssDescription {
533    fn from(bss: BssDescription) -> fidl_ieee80211::BssDescription {
534        fidl_ieee80211::BssDescription {
535            bssid: bss.bssid.to_array(),
536            bss_type: bss.bss_type,
537            beacon_period: bss.beacon_period,
538            capability_info: bss.capability_info,
539            channel: bss.channel.into(),
540            rssi_dbm: bss.rssi_dbm,
541            snr_db: bss.snr_db,
542            ies: bss.ies,
543        }
544    }
545}
546
547impl fmt::Display for BssDescription {
548    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
549        write!(
550            f,
551            "SSID: {}, BSSID: {}, Protection: {}, Pri Chan: {}, Rx dBm: {}",
552            self.ssid,
553            self.bssid,
554            self.protection(),
555            self.channel.primary,
556            self.rssi_dbm,
557        )
558    }
559}
560// TODO(https://fxbug.dev/42164415): The error printed should include a minimal amount of information
561// about the BSS Description that could not be converted to aid debugging.
562impl TryFrom<fidl_ieee80211::BssDescription> for BssDescription {
563    type Error = anyhow::Error;
564
565    fn try_from(bss: fidl_ieee80211::BssDescription) -> Result<BssDescription, Self::Error> {
566        let mut ssid_range = None;
567        let mut rates = None;
568        let mut tim_range = None;
569        let mut country_range = None;
570        let mut rsne_range = None;
571        let mut ht_cap_range = None;
572        let mut ht_op_range = None;
573        let mut rm_enabled_cap_range = None;
574        let mut ext_cap_range = None;
575        let mut vht_cap_range = None;
576        let mut vht_op_range = None;
577        let mut rsnxe_range = None;
578        let mut owe_transition_range = None;
579
580        for (ie_type, range) in ie::IeSummaryIter::new(&bss.ies[..]) {
581            let body = &bss.ies[range.clone()];
582            match ie_type {
583                IeType::SSID => {
584                    ie::parse_ssid(body)?;
585                    ssid_range = Some(range);
586                }
587                IeType::SUPPORTED_RATES => {
588                    rates.get_or_insert(vec![]).extend(&*ie::parse_supported_rates(body)?);
589                }
590                IeType::EXTENDED_SUPPORTED_RATES => {
591                    rates.get_or_insert(vec![]).extend(&*ie::parse_extended_supported_rates(body)?);
592                }
593                IeType::TIM => {
594                    ie::parse_tim(body)?;
595                    tim_range = Some(range);
596                }
597                IeType::COUNTRY => country_range = Some(range),
598                // Decrement start of range by two to include the IE header.
599                IeType::RSNE => rsne_range = Some(range.start - 2..range.end),
600                IeType::HT_CAPABILITIES => {
601                    ie::parse_ht_capabilities(body)?;
602                    ht_cap_range = Some(range);
603                }
604                IeType::HT_OPERATION => {
605                    ie::parse_ht_operation(body)?;
606                    ht_op_range = Some(range);
607                }
608                IeType::RM_ENABLED_CAPABILITIES => {
609                    if let Ok(_) = ie::parse_rm_enabled_capabilities(body) {
610                        rm_enabled_cap_range = Some(range);
611                    }
612                }
613                IeType::EXT_CAPABILITIES => {
614                    // Parsing ExtCapabilities always succeeds, so no need to test parsing it here
615                    ext_cap_range = Some(range);
616                }
617                IeType::VHT_CAPABILITIES => {
618                    ie::parse_vht_capabilities(body)?;
619                    vht_cap_range = Some(range);
620                }
621                IeType::VHT_OPERATION => {
622                    ie::parse_vht_operation(body)?;
623                    vht_op_range = Some(range);
624                }
625                IeType::RSNXE => {
626                    rsnxe_range = Some(range);
627                }
628                IeType::OWE_TRANSITION => {
629                    if let Ok(_) = parse_owe_transition(body) {
630                        owe_transition_range = Some(range);
631                    }
632                }
633                _ => (),
634            }
635        }
636
637        let ssid_range = ssid_range.ok_or_else(|| format_err!("Missing SSID IE"))?;
638        let rates = rates.ok_or_else(|| format_err!("Missing rates IE"))?;
639
640        Ok(Self {
641            ssid: Ssid::from_bytes_unchecked(bss.ies[ssid_range].to_vec()),
642            bssid: Bssid::from(bss.bssid),
643            bss_type: bss.bss_type,
644            beacon_period: bss.beacon_period,
645            capability_info: bss.capability_info,
646            channel: bss.channel.try_into()?,
647            rssi_dbm: bss.rssi_dbm,
648            snr_db: bss.snr_db,
649            ies: bss.ies,
650
651            rates,
652            tim_range,
653            country_range,
654            rsne_range,
655            ht_cap_range,
656            ht_op_range,
657            rm_enabled_cap_range,
658            ext_cap_range,
659            vht_cap_range,
660            vht_op_range,
661            rsnxe_range,
662            owe_transition_range,
663        })
664    }
665}
666
667/// The BssCandidacy type is used to rank fidl_common::BssDescription values. It is ordered
668/// first by Protection and then by Dbm.
669#[derive(Debug, Eq, PartialEq)]
670pub struct BssCandidacy {
671    protection: Protection,
672    rssi_dbm: i8,
673}
674
675impl PartialOrd for BssCandidacy {
676    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
677        Some(self.cmp(other))
678    }
679}
680
681impl Ord for BssCandidacy {
682    fn cmp(&self, other: &Self) -> Ordering {
683        self.protection.cmp(&other.protection).then(self.rssi_dbm.cmp(&other.rssi_dbm))
684    }
685}
686
687/// Given a list of BssDescription, categorize each one based on the latest PHY standard it
688/// supports and return a mapping from Standard to number of BSS.
689pub fn phy_standard_map(bss_list: &Vec<BssDescription>) -> HashMap<Standard, usize> {
690    info_map(bss_list, |bss| bss.latest_standard())
691}
692
693/// Given a list of BssDescription, return a mapping from channel to the number of BSS using
694/// that channel.
695pub fn channel_map(bss_list: &Vec<BssDescription>) -> HashMap<u8, usize> {
696    info_map(bss_list, |bss| bss.channel.primary)
697}
698
699fn info_map<F, T>(bss_list: &Vec<BssDescription>, f: F) -> HashMap<T, usize>
700where
701    T: Eq + Hash,
702    F: Fn(&BssDescription) -> T,
703{
704    let mut info_map: HashMap<T, usize> = HashMap::new();
705    for bss in bss_list {
706        *info_map.entry(f(&bss)).or_insert(0) += 1
707    }
708    info_map
709}
710
711#[cfg(test)]
712mod tests {
713    use super::*;
714    use crate::channel::Cbw;
715    use crate::fake_bss_description;
716    use crate::ie::IeType;
717    use crate::ie::fake_ies::{fake_owe_transition, fake_wmm_param};
718    use crate::test_utils::fake_frames::{
719        fake_unknown_rsne, fake_wmm_param_body, fake_wpa1_ie_body, fake_wpa2_mfpc_rsne,
720        fake_wpa2_mfpr_rsne, fake_wpa2_rsne, fake_wpa2_wpa3_mfpr_rsne, fake_wpa2_wpa3_no_mfp_rsne,
721        invalid_wpa3_enterprise_192_bit_rsne, invalid_wpa3_rsne,
722    };
723    use crate::test_utils::fake_stas::IesOverrides;
724    use assert_matches::assert_matches;
725    use test_case::test_case;
726
727    #[test_case(fake_bss_description!(
728        Wpa1Wpa2,
729        channel: Channel::new(36, Cbw::Cbw80P80{ secondary80: 106 }),
730        rssi_dbm: -20,
731        short_preamble: true,
732        ies_overrides: IesOverrides::new()
733            .set(IeType::DSSS_PARAM_SET, [136].to_vec())
734    ))]
735    #[test_case(fake_bss_description!(
736        Open,
737        channel: Channel::new(1, Cbw::Cbw20),
738        beacon_period: 110,
739        short_preamble: true,
740        radio_measurement: true,
741        rates: vec![0x02, 0x04, 0x0c],
742    ))]
743    fn test_bss_lossless_conversion(bss: BssDescription) {
744        let fidl_bss = fidl_ieee80211::BssDescription::from(bss.clone());
745        assert_eq!(bss, BssDescription::try_from(fidl_bss.clone()).unwrap());
746        assert_eq!(
747            fidl_bss,
748            fidl_ieee80211::BssDescription::from(
749                BssDescription::try_from(fidl_bss.clone()).unwrap()
750            )
751        );
752    }
753
754    #[test]
755    fn test_known_protection() {
756        assert_eq!(Protection::Open, fake_bss_description!(Open).protection());
757        assert_eq!(
758            Protection::OpenOweTransition,
759            fake_bss_description!(OpenOweTransition).protection()
760        );
761        assert_eq!(Protection::Owe, fake_bss_description!(Owe).protection());
762        assert_eq!(Protection::Wep, fake_bss_description!(Wep).protection());
763        assert_eq!(Protection::Wpa1, fake_bss_description!(Wpa1).protection());
764        assert_eq!(Protection::Wpa1, fake_bss_description!(Wpa1Enhanced).protection());
765        assert_eq!(
766            Protection::Wpa1Wpa2PersonalTkipOnly,
767            fake_bss_description!(Wpa1Wpa2TkipOnly).protection()
768        );
769        assert_eq!(
770            Protection::Wpa2PersonalTkipOnly,
771            fake_bss_description!(Wpa2TkipOnly).protection()
772        );
773        assert_eq!(Protection::Wpa1Wpa2Personal, fake_bss_description!(Wpa1Wpa2).protection());
774        assert_eq!(Protection::Wpa2Personal, fake_bss_description!(Wpa2TkipCcmp).protection());
775        assert_eq!(Protection::Wpa2Personal, fake_bss_description!(Wpa2).protection());
776        assert_eq!(Protection::Wpa2Wpa3Personal, fake_bss_description!(Wpa2Wpa3).protection());
777        assert_eq!(Protection::Wpa3Personal, fake_bss_description!(Wpa3).protection());
778        assert_eq!(Protection::Wpa2Enterprise, fake_bss_description!(Wpa2Enterprise).protection());
779        assert_eq!(Protection::Wpa3Enterprise, fake_bss_description!(Wpa3Enterprise).protection());
780    }
781
782    #[test]
783    fn test_pmf_configs_supported() {
784        let bss = fake_bss_description!(Wpa2,
785            ies_overrides: IesOverrides::new()
786                .set(IeType::RSNE, fake_wpa2_mfpc_rsne()[2..].to_vec())
787        );
788        assert_eq!(Protection::Wpa2Personal, bss.protection());
789
790        let bss = fake_bss_description!(Wpa2,
791            ies_overrides: IesOverrides::new()
792                .set(IeType::RSNE, fake_wpa2_mfpr_rsne()[2..].to_vec())
793        );
794        assert_eq!(Protection::Wpa2Personal, bss.protection());
795
796        let bss = fake_bss_description!(Wpa2,
797            ies_overrides: IesOverrides::new()
798                .set(IeType::RSNE, fake_wpa2_wpa3_mfpr_rsne()[2..].to_vec())
799        );
800        assert_eq!(Protection::Wpa2Wpa3Personal, bss.protection());
801    }
802
803    #[test]
804    fn test_downgrade() {
805        // If Wpa3 doesn't use MFP, ignore it and use Wpa2 instead.
806        let bss = fake_bss_description!(Wpa2,
807            ies_overrides: IesOverrides::new()
808                .set(IeType::RSNE, fake_wpa2_wpa3_no_mfp_rsne()[2..].to_vec())
809        );
810        assert_eq!(Protection::Wpa2Personal, bss.protection());
811
812        // Downgrade to Wpa1 as well.
813        let bss = fake_bss_description!(Wpa1,
814            ies_overrides: IesOverrides::new()
815                .set(IeType::RSNE, invalid_wpa3_rsne()[2..].to_vec())
816        );
817        assert_eq!(Protection::Wpa1, bss.protection());
818    }
819
820    #[test]
821    fn test_unknown_protection() {
822        let bss = fake_bss_description!(Wpa2,
823            ies_overrides: IesOverrides::new()
824                .set(IeType::RSNE, fake_unknown_rsne()[2..].to_vec())
825        );
826        assert_eq!(Protection::Unknown, bss.protection());
827
828        let bss = fake_bss_description!(Wpa2,
829            ies_overrides: IesOverrides::new()
830                .set(IeType::RSNE, invalid_wpa3_rsne()[2..].to_vec())
831        );
832        assert_eq!(Protection::Unknown, bss.protection());
833
834        let bss = fake_bss_description!(Wpa2,
835            ies_overrides: IesOverrides::new()
836                .set(IeType::RSNE, invalid_wpa3_enterprise_192_bit_rsne()[2..].to_vec())
837        );
838        assert_eq!(Protection::Unknown, bss.protection());
839    }
840
841    #[test]
842    fn test_needs_eapol_exchange() {
843        assert!(fake_bss_description!(Owe).needs_eapol_exchange());
844        assert!(fake_bss_description!(Wpa1).needs_eapol_exchange());
845        assert!(fake_bss_description!(Wpa2).needs_eapol_exchange());
846
847        assert!(!fake_bss_description!(Open).needs_eapol_exchange());
848        assert!(!fake_bss_description!(OpenOweTransition).needs_eapol_exchange());
849        assert!(!fake_bss_description!(Wep).needs_eapol_exchange());
850    }
851
852    #[test]
853    fn test_rm_enabled_cap_ie() {
854        let bss = fake_bss_description!(Wpa2,
855            ies_overrides: IesOverrides::new()
856                .remove(IeType::RM_ENABLED_CAPABILITIES)
857        );
858        assert!(bss.rm_enabled_cap().is_none());
859
860        #[rustfmt::skip]
861        let rm_enabled_capabilities = vec![
862            0x03, // link measurement and neighbor report enabled
863            0x00, 0x00, 0x00, 0x00,
864        ];
865        let bss = fake_bss_description!(Wpa2,
866            ies_overrides: IesOverrides::new()
867                .remove(IeType::RM_ENABLED_CAPABILITIES)
868                .set(IeType::RM_ENABLED_CAPABILITIES, rm_enabled_capabilities.clone())
869        );
870        assert_matches!(bss.rm_enabled_cap(), Some(cap) => {
871            assert_eq!(cap.as_bytes(), &rm_enabled_capabilities[..]);
872        });
873    }
874
875    #[test]
876    fn test_ext_cap_ie() {
877        let bss = fake_bss_description!(Wpa2,
878            ies_overrides: IesOverrides::new()
879                .remove(IeType::EXT_CAPABILITIES)
880        );
881        assert!(bss.ext_cap().is_none());
882
883        #[rustfmt::skip]
884        let ext_capabilities = vec![
885            0x04, 0x00,
886            0x08, // BSS transition supported
887            0x00, 0x00, 0x00, 0x00, 0x40
888        ];
889        let bss = fake_bss_description!(Wpa2,
890            ies_overrides: IesOverrides::new()
891                .remove(IeType::EXT_CAPABILITIES)
892                .set(IeType::EXT_CAPABILITIES, ext_capabilities.clone())
893        );
894        let ext_cap = bss.ext_cap().expect("expect bss.ext_cap() to be Some");
895        assert_eq!(ext_cap.ext_caps_octet_1.map(|o| o.0), Some(0x04));
896        assert_eq!(ext_cap.ext_caps_octet_2.map(|o| o.0), Some(0x00));
897        assert_eq!(ext_cap.ext_caps_octet_3.map(|o| o.0), Some(0x08));
898        assert_eq!(ext_cap.remaining, &[0x00, 0x00, 0x00, 0x00, 0x40]);
899    }
900
901    #[test]
902    fn test_wpa_ie() {
903        let buf =
904            fake_bss_description!(Wpa1).wpa_ie().expect("failed to find WPA1 IE").into_bytes();
905        assert_eq!(&fake_wpa1_ie_body(false)[..], &buf[..]);
906        fake_bss_description!(Wpa2).wpa_ie().expect_err("found unexpected WPA1 IE");
907    }
908
909    #[test]
910    fn test_wmm_param() {
911        let bss = fake_bss_description!(Wpa2, qos: true, wmm_param: Some(fake_wmm_param()));
912        let wmm_param = bss.wmm_param().expect("failed to find wmm param");
913        assert_eq!(fake_wmm_param_body(), wmm_param.as_bytes());
914    }
915
916    #[test]
917    fn test_owe_transition() {
918        let bss = fake_bss_description!(OpenOweTransition);
919        let owe_transition = bss.owe_transition().expect("failed to find owe transition ie");
920        assert_eq!(owe_transition, fake_owe_transition());
921    }
922
923    #[test]
924    fn test_latest_standard_ac() {
925        let bss = fake_bss_description!(Open,
926            ies_overrides: IesOverrides::new()
927                .set(IeType::VHT_CAPABILITIES, vec![0; fidl_ieee80211::VHT_CAP_LEN as usize])
928                .set(IeType::VHT_OPERATION, vec![0; fidl_ieee80211::VHT_OP_LEN as usize]),
929        );
930        assert_eq!(Standard::Dot11Ac, bss.latest_standard());
931    }
932
933    #[test]
934    fn test_latest_standard_n() {
935        let bss = fake_bss_description!(Open,
936            ies_overrides: IesOverrides::new()
937                .set(IeType::HT_CAPABILITIES, vec![0; fidl_ieee80211::HT_CAP_LEN as usize])
938                .set(IeType::HT_OPERATION, vec![0; fidl_ieee80211::HT_OP_LEN as usize])
939                .remove(IeType::VHT_CAPABILITIES)
940                .remove(IeType::VHT_OPERATION),
941        );
942        assert_eq!(Standard::Dot11N, bss.latest_standard());
943    }
944
945    #[test]
946    fn test_latest_standard_g() {
947        let bss = fake_bss_description!(Open,
948            channel: Channel::new(1, Cbw::Cbw20),
949            rates: vec![12],
950            ies_overrides: IesOverrides::new()
951                .remove(IeType::HT_CAPABILITIES)
952                .remove(IeType::HT_OPERATION)
953                .remove(IeType::VHT_CAPABILITIES)
954                .remove(IeType::VHT_OPERATION),
955        );
956        assert_eq!(Standard::Dot11G, bss.latest_standard());
957    }
958
959    #[test]
960    fn test_latest_standard_b() {
961        let bss = fake_bss_description!(Open,
962            channel: Channel::new(1, Cbw::Cbw20),
963            rates: vec![2],
964            ies_overrides: IesOverrides::new()
965                .remove(IeType::HT_CAPABILITIES)
966                .remove(IeType::HT_OPERATION)
967                .remove(IeType::VHT_CAPABILITIES)
968                .remove(IeType::VHT_OPERATION),
969        );
970        assert_eq!(Standard::Dot11B, bss.latest_standard());
971    }
972
973    #[test]
974    fn test_latest_standard_b_with_basic() {
975        let bss = fake_bss_description!(Open,
976            channel: Channel::new(1, Cbw::Cbw20),
977            rates: vec![ie::SupportedRate(2).with_basic(true).0],
978            ies_overrides: IesOverrides::new()
979                .remove(IeType::HT_CAPABILITIES)
980                .remove(IeType::HT_OPERATION)
981                .remove(IeType::VHT_CAPABILITIES)
982                .remove(IeType::VHT_OPERATION),
983        );
984        assert_eq!(Standard::Dot11B, bss.latest_standard());
985    }
986
987    #[test]
988    fn test_latest_standard_a() {
989        let bss = fake_bss_description!(Open,
990            channel: Channel::new(36, Cbw::Cbw20),
991            rates: vec![48],
992            ies_overrides: IesOverrides::new()
993                .remove(IeType::HT_CAPABILITIES)
994                .remove(IeType::HT_OPERATION)
995                .remove(IeType::VHT_CAPABILITIES)
996                .remove(IeType::VHT_OPERATION),
997        );
998        assert_eq!(Standard::Dot11A, bss.latest_standard());
999    }
1000
1001    #[test]
1002    fn test_supports_uapsd() {
1003        let bss = fake_bss_description!(Wpa2,
1004            ies_overrides: IesOverrides::new()
1005                .remove(IeType::WMM_INFO)
1006                .remove(IeType::WMM_PARAM)
1007        );
1008        assert!(!bss.supports_uapsd());
1009
1010        let mut wmm_info = vec![0x80]; // U-APSD enabled
1011        let bss = fake_bss_description!(Wpa2,
1012            ies_overrides: IesOverrides::new()
1013                .remove(IeType::WMM_INFO)
1014                .remove(IeType::WMM_PARAM)
1015                .set(IeType::WMM_INFO, wmm_info.clone())
1016        );
1017        assert!(bss.supports_uapsd());
1018
1019        wmm_info = vec![0x00]; // U-APSD not enabled
1020        let bss = fake_bss_description!(Wpa2,
1021            ies_overrides: IesOverrides::new()
1022                .remove(IeType::WMM_INFO)
1023                .remove(IeType::WMM_PARAM)
1024                .set(IeType::WMM_INFO, wmm_info)
1025        );
1026        assert!(!bss.supports_uapsd());
1027
1028        #[rustfmt::skip]
1029        let mut wmm_param = vec![
1030            0x80, // U-APSD enabled
1031            0x00, // reserved
1032            0x03, 0xa4, 0x00, 0x00, // AC_BE parameters
1033            0x27, 0xa4, 0x00, 0x00, // AC_BK parameters
1034            0x42, 0x43, 0x5e, 0x00, // AC_VI parameters
1035            0x62, 0x32, 0x2f, 0x00, // AC_VO parameters
1036        ];
1037        let bss = fake_bss_description!(Wpa2,
1038            ies_overrides: IesOverrides::new()
1039                .remove(IeType::WMM_INFO)
1040                .remove(IeType::WMM_PARAM)
1041                .set(IeType::WMM_PARAM, wmm_param.clone())
1042        );
1043        assert!(bss.supports_uapsd());
1044
1045        wmm_param[0] = 0x00; // U-APSD not enabled
1046        let bss = fake_bss_description!(Wpa2,
1047            ies_overrides: IesOverrides::new()
1048                .remove(IeType::WMM_INFO)
1049                .remove(IeType::WMM_PARAM)
1050                .set(IeType::WMM_PARAM, wmm_param)
1051        );
1052        assert!(!bss.supports_uapsd());
1053    }
1054
1055    #[test]
1056    fn test_supports_ft() {
1057        let bss = fake_bss_description!(Wpa2,
1058            ies_overrides: IesOverrides::new()
1059                .remove(IeType::MOBILITY_DOMAIN)
1060        );
1061        assert!(!bss.supports_ft());
1062
1063        let bss = fake_bss_description!(Wpa2,
1064            ies_overrides: IesOverrides::new()
1065                .remove(IeType::MOBILITY_DOMAIN)
1066                // We only check that the IE exists, so just set the content to bytes 0's.
1067                .set(IeType::MOBILITY_DOMAIN, vec![0x00; 3])
1068        );
1069        assert!(bss.supports_ft());
1070    }
1071
1072    #[test]
1073    fn test_candidacy() {
1074        let bss_candidacy = fake_bss_description!(Wpa2, rssi_dbm: -10).candidacy();
1075        assert_eq!(
1076            bss_candidacy,
1077            BssCandidacy { protection: Protection::Wpa2Personal, rssi_dbm: -10 }
1078        );
1079
1080        let bss_candidacy = fake_bss_description!(Open, rssi_dbm: -10).candidacy();
1081        assert_eq!(bss_candidacy, BssCandidacy { protection: Protection::Open, rssi_dbm: -10 });
1082
1083        let bss_candidacy = fake_bss_description!(Wpa2, rssi_dbm: -20).candidacy();
1084        assert_eq!(
1085            bss_candidacy,
1086            BssCandidacy { protection: Protection::Wpa2Personal, rssi_dbm: -20 }
1087        );
1088
1089        let bss_candidacy = fake_bss_description!(Wpa2, rssi_dbm: 0).candidacy();
1090        assert_eq!(
1091            bss_candidacy,
1092            BssCandidacy { protection: Protection::Wpa2Personal, rssi_dbm: i8::MIN }
1093        );
1094    }
1095
1096    fn assert_bss_comparison(worse: &BssDescription, better: &BssDescription) {
1097        assert_eq!(Ordering::Less, worse.candidacy().cmp(&better.candidacy()));
1098        assert_eq!(Ordering::Greater, better.candidacy().cmp(&worse.candidacy()));
1099    }
1100
1101    #[test]
1102    fn test_bss_comparison() {
1103        //  Two BSSDescription values with the same protection and RSSI are equivalent.
1104        assert_eq!(
1105            Ordering::Equal,
1106            fake_bss_description!(Wpa2, rssi_dbm: -10)
1107                .candidacy()
1108                .cmp(&fake_bss_description!(Wpa2, rssi_dbm: -10).candidacy())
1109        );
1110
1111        // Higher security is better.
1112        assert_bss_comparison(
1113            &fake_bss_description!(Wpa1, rssi_dbm: -10),
1114            &fake_bss_description!(Wpa2, rssi_dbm: -50),
1115        );
1116        assert_bss_comparison(
1117            &fake_bss_description!(Open, rssi_dbm: -10),
1118            &fake_bss_description!(Wpa2, rssi_dbm: -50),
1119        );
1120        // Higher RSSI is better if security is equivalent.
1121        assert_bss_comparison(
1122            &fake_bss_description!(Wpa2, rssi_dbm: -50),
1123            &fake_bss_description!(Wpa2, rssi_dbm: -10),
1124        );
1125        // Having an RSSI measurement is always better than not having any measurement
1126        assert_bss_comparison(
1127            &fake_bss_description!(Wpa2, rssi_dbm: 0),
1128            &fake_bss_description!(Wpa2, rssi_dbm: -100),
1129        );
1130    }
1131
1132    #[test]
1133    fn test_bss_ie_fields() {
1134        #[rustfmt::skip]
1135        let ht_cap = vec![
1136            0xef, 0x09, // HT Capabilities Info
1137            0x1b, // A-MPDU Parameters: 0x1b
1138            0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // MCS Set
1139            0x00, 0x00, // HT Extended Capabilities
1140            0x00, 0x00, 0x00, 0x00, // Transmit Beamforming Capabilities
1141            0x00
1142        ];
1143        #[rustfmt::skip]
1144        let ht_op = vec![
1145            0x9d, // Primary Channel: 157
1146            0x0d, // HT Info Subset - secondary channel above, any channel width, RIFS permitted
1147            0x00, 0x00, 0x00, 0x00, // HT Info Subsets
1148            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Basic MCS Set
1149        ];
1150        #[rustfmt::skip]
1151        let vht_cap = vec![
1152            0xb2, 0x01, 0x80, 0x33, // VHT Capabilities Info
1153            0xea, 0xff, 0x00, 0x00, 0xea, 0xff, 0x00, 0x00, // VHT Supported MCS Set
1154        ];
1155        let vht_op = vec![0x01, 0x9b, 0x00, 0xfc, 0xff];
1156        let rsnxe = vec![0b00100001];
1157
1158        let bss = fake_bss_description!(Wpa2,
1159            ies_overrides: IesOverrides::new()
1160                .set(IeType::SSID, b"ssidie".to_vec())
1161                .set(IeType::SUPPORTED_RATES, vec![0x81, 0x82, 0x83])
1162                .set(IeType::EXTENDED_SUPPORTED_RATES, vec![4, 5, 6])
1163                .set(IeType::COUNTRY, vec![1, 2, 3])
1164                .set(IeType::HT_CAPABILITIES, ht_cap.clone())
1165                .set(IeType::HT_OPERATION, ht_op.clone())
1166                .set(IeType::VHT_CAPABILITIES, vht_cap.clone())
1167                .set(IeType::VHT_OPERATION, vht_op.clone())
1168                .set(IeType::RSNXE, rsnxe.clone())
1169        );
1170        assert_eq!(bss.ssid, Ssid::try_from("ssidie").unwrap());
1171        assert_eq!(
1172            bss.rates(),
1173            &[
1174                ie::SupportedRate(0x81),
1175                ie::SupportedRate(0x82),
1176                ie::SupportedRate(0x83),
1177                ie::SupportedRate(4),
1178                ie::SupportedRate(5),
1179                ie::SupportedRate(6)
1180            ]
1181        );
1182        assert_eq!(bss.country(), Some(&[1, 2, 3][..]));
1183        assert_eq!(bss.rsne(), Some(&fake_wpa2_rsne()[..]));
1184        assert_matches!(bss.ht_cap(), Some(capability_info) => {
1185            assert_eq!(Ref::bytes(&capability_info), &ht_cap[..]);
1186        });
1187        assert_eq!(
1188            bss.raw_ht_cap().map(|capability_info| capability_info.bytes.to_vec()),
1189            Some(ht_cap)
1190        );
1191        assert_matches!(bss.ht_op(), Some(op) => {
1192            assert_eq!(Ref::bytes(&op), &ht_op[..]);
1193        });
1194        assert_eq!(bss.raw_ht_op().map(|op| op.bytes.to_vec()), Some(ht_op));
1195        assert_matches!(bss.vht_cap(), Some(capability_info) => {
1196            assert_eq!(Ref::bytes(&capability_info), &vht_cap[..]);
1197        });
1198        assert_eq!(
1199            bss.raw_vht_cap().map(|capability_info| capability_info.bytes.to_vec()),
1200            Some(vht_cap)
1201        );
1202        assert_matches!(bss.vht_op(), Some(op) => {
1203            assert_eq!(Ref::bytes(&op), &vht_op[..]);
1204        });
1205        assert_eq!(bss.raw_vht_op().map(|op| op.bytes.to_vec()), Some(vht_op));
1206        assert_matches!(bss.rsnxe(), Some(r) => {
1207            assert_eq!(Ref::bytes(&r.rsnxe_octet_1.expect("no octet 1")), &rsnxe[..]);
1208        });
1209    }
1210
1211    #[test]
1212    fn test_protection_conversions() {
1213        assert_eq!(
1214            Protection::Unknown,
1215            Protection::from(fidl_sme::Protection::from(Protection::Unknown))
1216        );
1217        assert_eq!(
1218            Protection::Open,
1219            Protection::from(fidl_sme::Protection::from(Protection::Open))
1220        );
1221        assert_eq!(
1222            Protection::OpenOweTransition,
1223            Protection::from(fidl_sme::Protection::from(Protection::OpenOweTransition))
1224        );
1225        assert_eq!(Protection::Owe, Protection::from(fidl_sme::Protection::from(Protection::Owe)));
1226        assert_eq!(Protection::Wep, Protection::from(fidl_sme::Protection::from(Protection::Wep)));
1227        assert_eq!(
1228            Protection::Wpa1,
1229            Protection::from(fidl_sme::Protection::from(Protection::Wpa1))
1230        );
1231        assert_eq!(
1232            Protection::Wpa1Wpa2PersonalTkipOnly,
1233            Protection::from(fidl_sme::Protection::from(Protection::Wpa1Wpa2PersonalTkipOnly))
1234        );
1235        assert_eq!(
1236            Protection::Wpa2PersonalTkipOnly,
1237            Protection::from(fidl_sme::Protection::from(Protection::Wpa2PersonalTkipOnly))
1238        );
1239        assert_eq!(
1240            Protection::Wpa1Wpa2Personal,
1241            Protection::from(fidl_sme::Protection::from(Protection::Wpa1Wpa2Personal))
1242        );
1243        assert_eq!(
1244            Protection::Wpa2Personal,
1245            Protection::from(fidl_sme::Protection::from(Protection::Wpa2Personal))
1246        );
1247        assert_eq!(
1248            Protection::Wpa2Wpa3Personal,
1249            Protection::from(fidl_sme::Protection::from(Protection::Wpa2Wpa3Personal))
1250        );
1251        assert_eq!(
1252            Protection::Wpa3Personal,
1253            Protection::from(fidl_sme::Protection::from(Protection::Wpa3Personal))
1254        );
1255        assert_eq!(
1256            Protection::Wpa2Enterprise,
1257            Protection::from(fidl_sme::Protection::from(Protection::Wpa2Enterprise))
1258        );
1259        assert_eq!(
1260            Protection::Wpa3Enterprise,
1261            Protection::from(fidl_sme::Protection::from(Protection::Wpa3Enterprise))
1262        );
1263    }
1264}