1 // Copyright 2024 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use capture::pcap;
16 use std::io::Cursor;
17 use std::net::{IpAddr, Ipv6Addr};
18 use std::str::FromStr;
19 use tokio::io::BufReader;
20
ipv6_from_str(addr: &str) -> Result<IpAddr, std::io::Error>21 fn ipv6_from_str(addr: &str) -> Result<IpAddr, std::io::Error> {
22 match Ipv6Addr::from_str(addr) {
23 Ok(addr) => Ok(addr.into()),
24 Err(err) => Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, err.to_string())),
25 }
26 }
27
28 #[tokio::test]
dns_manager() -> Result<(), std::io::Error>29 async fn dns_manager() -> Result<(), std::io::Error> {
30 const DATA: &[u8] = include_bytes!("../../capture/data/dns.cap");
31
32 let mut reader = BufReader::new(Cursor::new(DATA));
33 let header = pcap::read_file_header(&mut reader).await?;
34 assert_eq!(header.linktype, pcap::LinkType::Ethernet.into());
35 let mut dns_manager = http_proxy::DnsManager::new();
36 loop {
37 match pcap::read_record(&mut reader).await {
38 Ok((_hdr, record)) => {
39 dns_manager.add_from_ethernet_slice(&record);
40 }
41 Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => {
42 break;
43 }
44 Err(e) => {
45 println!("Error: {:?}", e);
46 assert!(false);
47 }
48 }
49 }
50 assert_eq!(dns_manager.len(), 4);
51
52 // 0xf0d4 AAAA www.netbsd.org AAAA
53 assert_eq!(
54 dns_manager.get(&ipv6_from_str("2001:4f8:4:7:2e0:81ff:fe52:9a6b")?),
55 Some("www.netbsd.org".into())
56 );
57
58 Ok(())
59 }
60