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        let mut rng = rand::thread_rng();
26        rng.gen::<[u8; 6]>()
27    }
28
29    #[test]
30    fn ssid_to_string_shape() {
31        let mut rng = rand::thread_rng();
32        let ssid = Ssid::from_bytes_unchecked(
33            (0..(rng.gen::<usize>() % fidl_ieee80211::MAX_SSID_BYTE_LEN as usize))
34                .map(|_| rng.gen::<u8>())
35                .collect::<Vec<u8>>(),
36        );
37        assert!(SSID_REGEX.is_match(&ssid.to_string()));
38    }
39
40    #[test]
41    fn test_printed_mac_addr_shape() {
42        let mac_addr = MacAddr::from(random_six_byte_array());
43        assert!(MAC_ADDR_REGEX.is_match(&format!("{}", mac_addr)));
44    }
45
46    #[test]
47    fn test_debug_printed_mac_addr_shape() {
48        let mac_addr = MacAddr::from(random_six_byte_array());
49
50        let mac_addr_debug_string = format!("{:?}", mac_addr);
51        assert!(Regex::new(r#"^MacAddr\([^)]*\)$"#).unwrap().is_match(&mac_addr_debug_string));
52        assert!(MAC_ADDR_REGEX.is_match(&mac_addr_debug_string[8..25]));
53    }
54
55    #[test]
56    fn test_printed_bssid_shape() {
57        let bssid = Bssid::from(random_six_byte_array());
58
59        assert!(BSSID_REGEX.is_match(&format!("{}", bssid)));
60    }
61
62    #[test]
63    fn test_debug_printed_bssid_shape() {
64        let bssid = Bssid::from(random_six_byte_array());
65
66        let bssid_debug_string = format!("{:?}", bssid);
67        assert!(Regex::new(r#"^Bssid\([^)]*\)$"#).unwrap().is_match(&bssid_debug_string));
68        assert!(BSSID_REGEX.is_match(&bssid_debug_string[6..23]));
69    }
70}