()
| 838 | |
| 839 | #[test] |
| 840 | fn test_write() { |
| 841 | let tap_ip_guard = TAP_IP_LOCK.lock().unwrap(); |
| 842 | |
| 843 | let mut tap = Tap::new(1).unwrap(); |
| 844 | let ip_addr = IpAddr::V4((*tap_ip_guard).parse().unwrap()); |
| 845 | let netmask = IpAddr::V4(SUBNET_MASK.parse().unwrap()); |
| 846 | tap.set_ip_addr(ip_addr, Some(netmask)).unwrap(); |
| 847 | tap.enable().unwrap(); |
| 848 | |
| 849 | let (mac, _, mut rx) = pnet_get_mac_tx_rx(tap.if_name_as_str()); |
| 850 | |
| 851 | let payload = DATA_STRING.as_bytes(); |
| 852 | |
| 853 | // vnet hdr + eth hdr + ip hdr + udp hdr + payload len |
| 854 | let buf_size = 10 + 14 + 20 + 8 + payload.len(); |
| 855 | |
| 856 | let mut buf = vec![0u8; buf_size]; |
| 857 | // leave the vnet hdr as is |
| 858 | pnet_build_packet(&mut buf[10..], mac, payload); |
| 859 | |
| 860 | tap.write_all(&buf).unwrap(); |
| 861 | tap.flush().unwrap(); |
| 862 | |
| 863 | let (channel_tx, channel_rx) = mpsc::channel(); |
| 864 | |
| 865 | // We use a separate thread to wait for the test packet because the API exposed by pnet is |
| 866 | // blocking. This thread will be killed when the main thread exits. |
| 867 | let _handle = thread::spawn(move || { |
| 868 | loop { |
| 869 | let buf = rx.next().unwrap(); |
| 870 | let p = ParsedPkt::new(buf); |
| 871 | p.print(); |
| 872 | |
| 873 | if let Some(ref udp) = p.udp |
| 874 | && payload == udp.payload() |
| 875 | { |
| 876 | channel_tx.send(true).unwrap(); |
| 877 | break; |
| 878 | } |
| 879 | } |
| 880 | }); |
| 881 | |
| 882 | // We wait for at most SLEEP_MILLIS * SLEEP_ITERS milliseconds for the reception of the |
| 883 | // test packet to be detected. |
| 884 | static SLEEP_MILLIS: u64 = 500; |
| 885 | static SLEEP_ITERS: u32 = 6; |
| 886 | |
| 887 | let mut found_test_packet = false; |
| 888 | |
| 889 | for _ in 0..SLEEP_ITERS { |
| 890 | thread::sleep(Duration::from_millis(SLEEP_MILLIS)); |
| 891 | if let Ok(true) = channel_rx.try_recv() { |
| 892 | found_test_packet = true; |
| 893 | break; |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | assert!(found_test_packet); |
nothing calls this directly
no test coverage detected