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