xref: /webrtc/util/src/vnet/chunk/chunk_test.rs (revision 83f2d1bb)
1 use crate::error::Result;
2 
3 use super::*;
4 
5 #[test]
test_tcp_frag_string()6 fn test_tcp_frag_string() {
7     let f = TCP_FLAG_FIN;
8     assert_eq!(f.to_string(), "FIN", "should match");
9     let f = TCP_FLAG_SYN;
10     assert_eq!(f.to_string(), "SYN", "should match");
11     let f = TCP_FLAG_RST;
12     assert_eq!(f.to_string(), "RST", "should match");
13     let f = TCP_FLAG_PSH;
14     assert_eq!(f.to_string(), "PSH", "should match");
15     let f = TCP_FLAG_ACK;
16     assert_eq!(f.to_string(), "ACK", "should match");
17     let f = TCP_FLAG_SYN | TCP_FLAG_ACK;
18     assert_eq!(f.to_string(), "SYN-ACK", "should match");
19 }
20 
21 const DEMO_IP: &str = "1.2.3.4";
22 
23 #[test]
test_chunk_udp() -> Result<()>24 fn test_chunk_udp() -> Result<()> {
25     let src = SocketAddr::from_str("192.168.0.2:1234")?;
26     let dst = SocketAddr::from_str(&(DEMO_IP.to_owned() + ":5678"))?;
27 
28     let mut c = ChunkUdp::new(src, dst);
29     let s = c.to_string();
30     log::debug!("chunk: {}", s);
31     assert_eq!(c.network(), UDP_STR, "should match");
32     assert!(s.contains(&src.to_string()), "should include address");
33     assert!(s.contains(&dst.to_string()), "should include address");
34     assert_eq!(c.get_source_ip(), src.ip(), "ip should match");
35     assert_eq!(c.get_destination_ip(), dst.ip(), "ip should match");
36 
37     // Test timestamp
38     let ts = c.set_timestamp();
39     assert_eq!(ts, c.get_timestamp(), "timestamp should match");
40 
41     c.user_data = "Hello".as_bytes().to_vec();
42 
43     let cloned = c.clone_to();
44 
45     // Test setSourceAddr
46     c.set_source_addr("2.3.4.5:4000")?;
47     assert_eq!(c.source_addr().to_string(), "2.3.4.5:4000");
48 
49     // Test Tag()
50     assert!(!c.tag().is_empty(), "should not be empty");
51 
52     // Verify cloned chunk was not affected by the changes to original chunk
53     c.user_data[0] = b'!'; // oroginal: "Hello" -> "Hell!"
54     assert_eq!(cloned.user_data(), "Hello".as_bytes(), "should match");
55     assert_eq!(cloned.source_addr().to_string(), "192.168.0.2:1234");
56     assert_eq!(cloned.get_source_ip(), src.ip(), "ip should match");
57     assert_eq!(cloned.get_destination_ip(), dst.ip(), "ip should match");
58 
59     Ok(())
60 }
61