Debian Codegraph @

crt/s2n/tests/pcap/src/lib.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 pub mod client_hello;
5 pub mod handshake_message;
6 pub mod packet;
7
8 use std::path::Path;
9
10 fn pcaps_from<P: AsRef<Path>>(path: P) -> impl Iterator<Item = String> {
11 std::fs::read_dir(path.as_ref())
12 .unwrap_or_else(|_| panic!("Unable to read pcap folder: {}", path.as_ref().display()))
13 .filter_map(Result::ok)
14 .filter_map(|entry| {
15 let path = entry.path();
16 path.to_str().map(std::string::ToString::to_string)
17 })
18 }
19
20 /// Creates an iterator over all test pcap file paths
21 pub fn all_pcaps() -> impl Iterator<Item = String> {
22 let local_pcaps = pcaps_from("data");
23
24 let downloaded_pcaps_path = std::env!("DOWNLOADED_PCAPS_PATH");
25 let downloaded_pcaps = pcaps_from(downloaded_pcaps_path);
26
27 local_pcaps.chain(downloaded_pcaps)
28 }
29
30 #[cfg(test)]
31 mod tests {
32 use super::*;
33 use anyhow::*;
34 use std::collections::HashSet;
35
36 #[test]
37 fn pcaps_source() -> Result<()> {
38 let pcaps = all_pcaps();
39 println!("All pcaps: ");
40 let paths: HashSet<String> = pcaps
41 .map(|file_str| {
42 println!("{file_str}");
43 let (path, _file) = file_str.rsplit_once("/").expect("No path");
44 path.to_owned()
45 })
46 .collect();
47 // If we're also using downloaded pcaps, we'd expected to be reading
48 // pcaps from at least two sources (the local data and the downloaded data).
49 if cfg!(feature = "download") {
50 assert!(paths.len() > 1);
51 } else {
52 assert!(paths.len() == 1);
53 }
54 Ok(())
55 }
56 }