ieee80211_testutils/
lib.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 lazy_static::lazy_static;
6use regex::Regex;
7
8// The following regular expressions match a strict subset of the strings
9// that match the regular expression defined in //src/developer/forensics/utils/redact/replacer.cc.
10lazy_static! {
11    pub static ref MAC_ADDR_REGEX: Regex =
12        Regex::new(r#"^(?:[0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"#).unwrap();
13    pub static ref BSSID_REGEX: Regex = MAC_ADDR_REGEX.clone();
14    pub static ref SSID_REGEX: Regex = Regex::new(r#"^<ssid-[0-9a-fA-F]{0,64}>$"#).unwrap();
15}
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20    use fidl_fuchsia_wlan_ieee80211 as fidl_ieee80211;
21    use ieee80211::{Bssid, MacAddr, Ssid};
22    use rand::Rng;
23
24    fn random_six_byte_array() -> [u8; 6] {
25        rand::random::<[u8; 6]>()
26    }
27
28    #[test]
29    fn ssid_to_string_shape() {
30        let mut rng = rand::rng();
31        let ssid = Ssid::from_bytes_unchecked(
32            (0..rng.random_range(0..fidl_ieee80211::MAX_SSID_BYTE_LEN as usize))
33                .map(|_| rng.random::<u8>())
34                .collect::<Vec<u8>>(),
35        );
36        assert!(SSID_REGEX.is_match(&ssid.to_string()));
37    }
38
39    #[test]
40    fn test_printed_mac_addr_shape() {
41        let mac_addr = MacAddr::from(random_six_byte_array());
42        assert!(MAC_ADDR_REGEX.is_match(&format!("{}", mac_addr)));
43    }
44
45    #[test]
46    fn test_debug_printed_mac_addr_shape() {
47        let mac_addr = MacAddr::from(random_six_byte_array());
48
49        let mac_addr_debug_string = format!("{:?}", mac_addr);
50        assert!(Regex::new(r#"^MacAddr\([^)]*\)$"#).unwrap().is_match(&mac_addr_debug_string));
51        assert!(MAC_ADDR_REGEX.is_match(&mac_addr_debug_string[8..25]));
52    }
53
54    #[test]
55    fn test_printed_bssid_shape() {
56        let bssid = Bssid::from(random_six_byte_array());
57
58        assert!(BSSID_REGEX.is_match(&format!("{}", bssid)));
59    }
60
61    #[test]
62    fn test_debug_printed_bssid_shape() {
63        let bssid = Bssid::from(random_six_byte_array());
64
65        let bssid_debug_string = format!("{:?}", bssid);
66        assert!(Regex::new(r#"^Bssid\([^)]*\)$"#).unwrap().is_match(&bssid_debug_string));
67        assert!(BSSID_REGEX.is_match(&bssid_debug_string[6..23]));
68    }
69}