xref: /webrtc/util/src/vnet/net/net_test.rs (revision 5d8fe953)
1 use super::*;
2 use crate::vnet::chunk::ChunkUdp;
3 
4 use tokio::sync::{broadcast, mpsc};
5 
6 const DEMO_IP: &str = "1.2.3.4";
7 
8 #[derive(Default)]
9 struct DummyObserver;
10 
11 #[async_trait]
12 impl ConnObserver for DummyObserver {
write(&self, _c: Box<dyn Chunk + Send + Sync>) -> Result<()>13     async fn write(&self, _c: Box<dyn Chunk + Send + Sync>) -> Result<()> {
14         Ok(())
15     }
16 
on_closed(&self, _addr: SocketAddr)17     async fn on_closed(&self, _addr: SocketAddr) {}
18 
determine_source_ip(&self, loc_ip: IpAddr, _dst_ip: IpAddr) -> Option<IpAddr>19     fn determine_source_ip(&self, loc_ip: IpAddr, _dst_ip: IpAddr) -> Option<IpAddr> {
20         Some(loc_ip)
21     }
22 }
23 
24 #[tokio::test]
test_net_native_interfaces() -> Result<()>25 async fn test_net_native_interfaces() -> Result<()> {
26     let nw = Net::new(None);
27     assert!(!nw.is_virtual(), "should be false");
28 
29     let interfaces = nw.get_interfaces().await;
30     log::debug!("interfaces: {:?}", interfaces);
31     for ifc in interfaces {
32         let addrs = ifc.addrs();
33         for addr in addrs {
34             log::debug!("{}", addr)
35         }
36     }
37 
38     Ok(())
39 }
40 
41 #[tokio::test]
test_net_native_resolve_addr() -> Result<()>42 async fn test_net_native_resolve_addr() -> Result<()> {
43     let nw = Net::new(None);
44     assert!(!nw.is_virtual(), "should be false");
45 
46     let udp_addr = nw.resolve_addr(true, "localhost:1234").await?;
47     assert_eq!(udp_addr.ip().to_string(), "127.0.0.1", "should match");
48     assert_eq!(udp_addr.port(), 1234, "should match");
49 
50     let result = nw.resolve_addr(false, "127.0.0.1:1234").await;
51     assert!(result.is_err(), "should not match");
52 
53     Ok(())
54 }
55 
56 #[tokio::test]
test_net_native_bind() -> Result<()>57 async fn test_net_native_bind() -> Result<()> {
58     let nw = Net::new(None);
59     assert!(!nw.is_virtual(), "should be false");
60 
61     let conn = nw.bind(SocketAddr::from_str("127.0.0.1:0")?).await?;
62     let laddr = conn.local_addr()?;
63     assert_eq!(
64         laddr.ip().to_string(),
65         "127.0.0.1",
66         "local_addr ip should match 127.0.0.1"
67     );
68     log::debug!("laddr: {}", laddr);
69 
70     Ok(())
71 }
72 
73 #[tokio::test]
test_net_native_dail() -> Result<()>74 async fn test_net_native_dail() -> Result<()> {
75     let nw = Net::new(None);
76     assert!(!nw.is_virtual(), "should be false");
77 
78     let conn = nw.dail(true, "127.0.0.1:1234").await?;
79     let laddr = conn.local_addr()?;
80     assert_eq!(
81         laddr.ip().to_string(),
82         "127.0.0.1",
83         "local_addr should match 127.0.0.1"
84     );
85     assert_ne!(laddr.port(), 1234, "local_addr port should match 1234");
86     log::debug!("laddr: {}", laddr);
87 
88     Ok(())
89 }
90 
91 #[tokio::test]
test_net_native_loopback() -> Result<()>92 async fn test_net_native_loopback() -> Result<()> {
93     let nw = Net::new(None);
94     assert!(!nw.is_virtual(), "should be false");
95 
96     let conn = nw.bind(SocketAddr::from_str("127.0.0.1:0")?).await?;
97     let laddr = conn.local_addr()?;
98 
99     let msg = "PING!";
100     let n = conn.send_to(msg.as_bytes(), laddr).await?;
101     assert_eq!(n, msg.len(), "should match msg size {}", msg.len());
102 
103     let mut buf = vec![0u8; 1000];
104     let (n, raddr) = conn.recv_from(&mut buf).await?;
105     assert_eq!(n, msg.len(), "should match msg size {}", msg.len());
106     assert_eq!(&buf[..n], msg.as_bytes(), "should match msg content {msg}");
107     assert_eq!(laddr, raddr, "should match addr {laddr}");
108 
109     Ok(())
110 }
111 
112 #[tokio::test]
test_net_native_unexpected_operations() -> Result<()>113 async fn test_net_native_unexpected_operations() -> Result<()> {
114     let mut lo_name = String::new();
115     let ifcs = ifaces::ifaces()?;
116     for ifc in &ifcs {
117         if let Some(addr) = ifc.addr {
118             if addr.ip().is_loopback() {
119                 lo_name = ifc.name.clone();
120                 break;
121             }
122         }
123     }
124 
125     let nw = Net::new(None);
126     assert!(!nw.is_virtual(), "should be false");
127 
128     if !lo_name.is_empty() {
129         if let Some(ifc) = nw.get_interface(&lo_name).await {
130             assert_eq!(ifc.name, lo_name, "should match ifc name");
131         } else {
132             panic!("should succeed");
133         }
134     }
135 
136     let result = nw.get_interface("foo0").await;
137     assert!(result.is_none(), "should be none");
138 
139     //let ips = nw.get_static_ips();
140     //assert!(ips.is_empty(), "should empty");
141 
142     Ok(())
143 }
144 
145 #[tokio::test]
test_net_virtual_interfaces() -> Result<()>146 async fn test_net_virtual_interfaces() -> Result<()> {
147     let nw = Net::new(Some(NetConfig::default()));
148     assert!(nw.is_virtual(), "should be true");
149 
150     let interfaces = nw.get_interfaces().await;
151     assert_eq!(2, interfaces.len(), "should be one interface");
152 
153     for ifc in interfaces {
154         match ifc.name.as_str() {
155             LO0_STR => {
156                 let addrs = ifc.addrs();
157                 assert_eq!(addrs.len(), 1, "should be one address");
158             }
159             "eth0" => {
160                 let addrs = ifc.addrs();
161                 assert!(addrs.is_empty(), "should empty");
162             }
163             _ => {
164                 panic!("unknown interface: {}", ifc.name);
165             }
166         }
167     }
168 
169     Ok(())
170 }
171 
172 #[tokio::test]
test_net_virtual_interface_by_name() -> Result<()>173 async fn test_net_virtual_interface_by_name() -> Result<()> {
174     let nw = Net::new(Some(NetConfig::default()));
175     assert!(nw.is_virtual(), "should be true");
176 
177     let interfaces = nw.get_interfaces().await;
178     assert_eq!(2, interfaces.len(), "should be one interface");
179 
180     let nic = nw.get_nic()?;
181     let nic = nic.lock().await;
182     if let Some(ifc) = nic.get_interface(LO0_STR).await {
183         assert_eq!(ifc.name.as_str(), LO0_STR, "should match");
184         let addrs = ifc.addrs();
185         assert_eq!(addrs.len(), 1, "should be one address");
186     } else {
187         panic!("should got ifc");
188     }
189 
190     if let Some(ifc) = nic.get_interface("eth0").await {
191         assert_eq!(ifc.name.as_str(), "eth0", "should match");
192         let addrs = ifc.addrs();
193         assert!(addrs.is_empty(), "should empty");
194     } else {
195         panic!("should got ifc");
196     }
197 
198     let result = nic.get_interface("foo0").await;
199     assert!(result.is_none(), "should fail");
200 
201     Ok(())
202 }
203 
204 #[tokio::test]
test_net_virtual_has_ipaddr() -> Result<()>205 async fn test_net_virtual_has_ipaddr() -> Result<()> {
206     let nw = Net::new(Some(NetConfig::default()));
207     assert!(nw.is_virtual(), "should be true");
208 
209     let interfaces = nw.get_interfaces().await;
210     assert_eq!(interfaces.len(), 2, "should be one interface");
211 
212     {
213         let nic = nw.get_nic()?;
214         let mut nic = nic.lock().await;
215         let ipnet = IpNet::from_str("10.1.2.3/24")?;
216         nic.add_addrs_to_interface("eth0", &[ipnet]).await?;
217 
218         if let Some(ifc) = nic.get_interface("eth0").await {
219             let addrs = ifc.addrs();
220             assert!(!addrs.is_empty(), "should not empty");
221         }
222     }
223 
224     if let Net::VNet(vnet) = &nw {
225         let net = vnet.lock().await;
226         let ip = Ipv4Addr::from_str("127.0.0.1")?.into();
227         assert!(net.has_ipaddr(ip), "the IP addr {ip} should exist");
228 
229         let ip = Ipv4Addr::from_str("10.1.2.3")?.into();
230         assert!(net.has_ipaddr(ip), "the IP addr {ip} should exist");
231 
232         let ip = Ipv4Addr::from_str("192.168.1.1")?.into();
233         assert!(!net.has_ipaddr(ip), "the IP addr {ip} should exist");
234     }
235     Ok(())
236 }
237 
238 #[tokio::test]
test_net_virtual_get_all_ipaddrs() -> Result<()>239 async fn test_net_virtual_get_all_ipaddrs() -> Result<()> {
240     let nw = Net::new(Some(NetConfig::default()));
241     assert!(nw.is_virtual(), "should be true");
242 
243     let interfaces = nw.get_interfaces().await;
244     assert_eq!(interfaces.len(), 2, "should be one interface");
245 
246     {
247         let nic = nw.get_nic()?;
248         let mut nic = nic.lock().await;
249         let ipnet = IpNet::from_str("10.1.2.3/24")?;
250         nic.add_addrs_to_interface("eth0", &[ipnet]).await?;
251 
252         if let Some(ifc) = nic.get_interface("eth0").await {
253             let addrs = ifc.addrs();
254             assert!(!addrs.is_empty(), "should not empty");
255         }
256     }
257 
258     if let Net::VNet(vnet) = &nw {
259         let net = vnet.lock().await;
260         let ips = net.get_all_ipaddrs(false);
261         assert_eq!(ips.len(), 2, "ips should match size {} == 2", ips.len())
262     }
263 
264     Ok(())
265 }
266 
267 #[tokio::test]
test_net_virtual_assign_port() -> Result<()>268 async fn test_net_virtual_assign_port() -> Result<()> {
269     let mut nw = Net::new(Some(NetConfig::default()));
270     assert!(nw.is_virtual(), "should be true");
271 
272     let addr = DEMO_IP;
273     let start = 1000u16;
274     let end = 1002u16;
275     let space = end + 1 - start;
276 
277     let interfaces = nw.get_interfaces().await;
278     assert_eq!(interfaces.len(), 2, "should be one interface");
279 
280     {
281         let nic = nw.get_nic()?;
282         let mut nic = nic.lock().await;
283         let ipnet = IpNet::from_str(&format!("{addr}/24"))?;
284         nic.add_addrs_to_interface("eth0", &[ipnet]).await?;
285     }
286 
287     if let Net::VNet(vnet) = &mut nw {
288         let vnet = vnet.lock().await;
289         // attempt to assign port with start > end should fail
290         let ip = IpAddr::from_str(addr)?;
291         let result = vnet.assign_port(ip, 3000, 2999).await;
292         assert!(result.is_err(), "assign_port should fail");
293 
294         for i in 0..space {
295             let port = vnet.assign_port(ip, start, end).await?;
296             log::debug!("{} got port: {}", i, port);
297 
298             let obs: Arc<Mutex<dyn ConnObserver + Send + Sync>> =
299                 Arc::new(Mutex::new(DummyObserver::default()));
300 
301             let conn = Arc::new(UdpConn::new(SocketAddr::new(ip, port), None, obs));
302 
303             let vi = vnet.vi.lock().await;
304             let _ = vi.udp_conns.insert(conn).await;
305         }
306 
307         {
308             let vi = vnet.vi.lock().await;
309             assert_eq!(
310                 vi.udp_conns.len().await,
311                 space as usize,
312                 "udp_conns should match"
313             );
314         }
315 
316         // attempt to assign again should fail
317         let result = vnet.assign_port(ip, start, end).await;
318         assert!(result.is_err(), "assign_port should fail");
319     }
320 
321     Ok(())
322 }
323 
324 #[tokio::test]
test_net_virtual_determine_source_ip() -> Result<()>325 async fn test_net_virtual_determine_source_ip() -> Result<()> {
326     let mut nw = Net::new(Some(NetConfig::default()));
327     assert!(nw.is_virtual(), "should be true");
328 
329     let interfaces = nw.get_interfaces().await;
330     assert_eq!(interfaces.len(), 2, "should be one interface");
331 
332     {
333         let nic = nw.get_nic()?;
334         let mut nic = nic.lock().await;
335         let ipnet = IpNet::from_str(&format!("{DEMO_IP}/24"))?;
336         nic.add_addrs_to_interface("eth0", &[ipnet]).await?;
337     }
338 
339     // Any IP turned into non-loopback IP
340     let any_ip = IpAddr::from_str("0.0.0.0")?;
341     let dst_ip = IpAddr::from_str("27.1.7.135")?;
342     if let Net::VNet(vnet) = &mut nw {
343         let vnet = vnet.lock().await;
344         let vi = vnet.vi.lock().await;
345         let src_ip = vi.determine_source_ip(any_ip, dst_ip);
346         log::debug!("any_ip: {} => {:?}", any_ip, src_ip);
347         assert!(src_ip.is_some(), "shouldn't be none");
348         if let Some(src_ip) = src_ip {
349             assert_eq!(src_ip.to_string().as_str(), DEMO_IP, "use non-loopback IP");
350         }
351     }
352 
353     // Any IP turned into loopback IP
354     let any_ip = IpAddr::from_str("0.0.0.0")?;
355     let dst_ip = IpAddr::from_str("127.0.0.2")?;
356     if let Net::VNet(vnet) = &mut nw {
357         let vnet = vnet.lock().await;
358         let vi = vnet.vi.lock().await;
359         let src_ip = vi.determine_source_ip(any_ip, dst_ip);
360         log::debug!("any_ip: {} => {:?}", any_ip, src_ip);
361         assert!(src_ip.is_some(), "shouldn't be none");
362         if let Some(src_ip) = src_ip {
363             assert_eq!(src_ip.to_string().as_str(), "127.0.0.1", "use loopback IP");
364         }
365     }
366 
367     // Non any IP won't change
368     let any_ip = IpAddr::from_str(DEMO_IP)?;
369     let dst_ip = IpAddr::from_str("127.0.0.2")?;
370     if let Net::VNet(vnet) = &mut nw {
371         let vnet = vnet.lock().await;
372         let vi = vnet.vi.lock().await;
373         let src_ip = vi.determine_source_ip(any_ip, dst_ip);
374         log::debug!("any_ip: {} => {:?}", any_ip, src_ip);
375         assert!(src_ip.is_some(), "shouldn't be none");
376         if let Some(src_ip) = src_ip {
377             assert_eq!(src_ip, any_ip, "IP change");
378         }
379     }
380 
381     Ok(())
382 }
383 
384 #[tokio::test]
test_net_virtual_resolve_addr() -> Result<()>385 async fn test_net_virtual_resolve_addr() -> Result<()> {
386     let nw = Net::new(Some(NetConfig::default()));
387     assert!(nw.is_virtual(), "should be true");
388 
389     let udp_addr = nw.resolve_addr(true, "localhost:1234").await?;
390     assert_eq!(
391         udp_addr.ip().to_string().as_str(),
392         "127.0.0.1",
393         "udp addr {} should match 127.0.0.1",
394         udp_addr.ip(),
395     );
396     assert_eq!(
397         udp_addr.port(),
398         1234,
399         "udp addr {} should match 1234",
400         udp_addr.port()
401     );
402 
403     Ok(())
404 }
405 
406 #[tokio::test]
test_net_virtual_loopback1() -> Result<()>407 async fn test_net_virtual_loopback1() -> Result<()> {
408     let nw = Net::new(Some(NetConfig::default()));
409     assert!(nw.is_virtual(), "should be true");
410 
411     let conn = nw.bind(SocketAddr::from_str("127.0.0.1:0")?).await?;
412     let laddr = conn.local_addr()?;
413 
414     let msg = "PING!";
415     let n = conn.send_to(msg.as_bytes(), laddr).await?;
416     assert_eq!(n, msg.len(), "should match msg size {}", msg.len());
417 
418     let mut buf = vec![0u8; 1000];
419     let (n, raddr) = conn.recv_from(&mut buf).await?;
420     assert_eq!(n, msg.len(), "should match msg size {}", msg.len());
421     assert_eq!(&buf[..n], msg.as_bytes(), "should match msg content {msg}");
422     assert_eq!(laddr, raddr, "should match addr {laddr}");
423 
424     Ok(())
425 }
426 
427 #[tokio::test]
test_net_virtual_bind_specific_port() -> Result<()>428 async fn test_net_virtual_bind_specific_port() -> Result<()> {
429     let nw = Net::new(Some(NetConfig::default()));
430     assert!(nw.is_virtual(), "should be true");
431 
432     let conn = nw.bind(SocketAddr::from_str("127.0.0.1:50916")?).await?;
433     let laddr = conn.local_addr()?;
434     assert_eq!(
435         laddr.ip().to_string().as_str(),
436         "127.0.0.1",
437         "{} should match 127.0.0.1",
438         laddr.ip()
439     );
440     assert_eq!(laddr.port(), 50916, "{} should match 50916", laddr.port());
441 
442     Ok(())
443 }
444 
445 #[tokio::test]
test_net_virtual_dail_lo0() -> Result<()>446 async fn test_net_virtual_dail_lo0() -> Result<()> {
447     let nw = Net::new(Some(NetConfig::default()));
448     assert!(nw.is_virtual(), "should be true");
449 
450     let conn = nw.dail(true, "127.0.0.1:1234").await?;
451     let laddr = conn.local_addr()?;
452     assert_eq!(
453         laddr.ip().to_string().as_str(),
454         "127.0.0.1",
455         "{} should match 127.0.0.1",
456         laddr.ip()
457     );
458     assert_ne!(laddr.port(), 1234, "{} should != 1234", laddr.port());
459 
460     Ok(())
461 }
462 
463 #[tokio::test]
test_net_virtual_dail_eth0() -> Result<()>464 async fn test_net_virtual_dail_eth0() -> Result<()> {
465     let wan = Arc::new(Mutex::new(Router::new(RouterConfig {
466         cidr: "1.2.3.0/24".to_string(),
467         ..Default::default()
468     })?));
469 
470     let nw = Net::new(Some(NetConfig::default()));
471 
472     {
473         let nic = nw.get_nic()?;
474 
475         let mut w = wan.lock().await;
476         w.add_net(Arc::clone(&nic)).await?;
477 
478         let n = nic.lock().await;
479         n.set_router(Arc::clone(&wan)).await?;
480     };
481 
482     let conn = nw.dail(true, "27.3.4.5:1234").await?;
483     let laddr = conn.local_addr()?;
484     assert_eq!(
485         laddr.ip().to_string().as_str(),
486         "1.2.3.1",
487         "{} should match 1.2.3.1",
488         laddr.ip()
489     );
490     assert!(laddr.port() != 0, "{} should != 0", laddr.port());
491 
492     Ok(())
493 }
494 
495 #[tokio::test]
test_net_virtual_resolver() -> Result<()>496 async fn test_net_virtual_resolver() -> Result<()> {
497     let wan = Arc::new(Mutex::new(Router::new(RouterConfig {
498         cidr: "1.2.3.0/24".to_string(),
499         ..Default::default()
500     })?));
501 
502     let nw = Net::new(Some(NetConfig::default()));
503 
504     let remote_addr = nw.resolve_addr(true, "127.0.0.1:1234").await?;
505     assert_eq!(remote_addr.to_string(), "127.0.0.1:1234", "should match");
506 
507     let result = nw.resolve_addr(false, "127.0.0.1:1234").await;
508     assert!(result.is_err(), "should not match");
509 
510     {
511         let nic = nw.get_nic()?;
512 
513         let mut w = wan.lock().await;
514         w.add_net(Arc::clone(&nic)).await?;
515         w.add_host("test.webrtc.rs".to_owned(), "30.31.32.33".to_owned())
516             .await?;
517 
518         let n = nic.lock().await;
519         n.set_router(Arc::clone(&wan)).await?;
520     }
521 
522     let (done_tx, mut done_rx) = mpsc::channel::<()>(1);
523     tokio::spawn(async move {
524         let (conn, raddr) = {
525             let raddr = nw.resolve_addr(true, "test.webrtc.rs:1234").await?;
526             (nw.dail(true, "test.webrtc.rs:1234").await?, raddr)
527         };
528 
529         let laddr = conn.local_addr()?;
530         assert_eq!(
531             laddr.ip().to_string().as_str(),
532             "1.2.3.1",
533             "{} should match  1.2.3.1",
534             laddr.ip()
535         );
536 
537         assert_eq!(
538             raddr.to_string(),
539             "30.31.32.33:1234",
540             "{raddr} should match 30.31.32.33:1234"
541         );
542 
543         drop(done_tx);
544 
545         Result::<()>::Ok(())
546     });
547 
548     let _ = done_rx.recv().await;
549 
550     Ok(())
551 }
552 
553 #[tokio::test]
test_net_virtual_loopback2() -> Result<()>554 async fn test_net_virtual_loopback2() -> Result<()> {
555     let nw = Net::new(Some(NetConfig::default()));
556 
557     let conn = nw.bind(SocketAddr::from_str("127.0.0.1:50916")?).await?;
558     let laddr = conn.local_addr()?;
559     assert_eq!(
560         laddr.to_string().as_str(),
561         "127.0.0.1:50916",
562         "{laddr} should match 127.0.0.1:50916"
563     );
564 
565     let mut c = ChunkUdp::new(
566         SocketAddr::from_str("127.0.0.1:4000")?,
567         SocketAddr::from_str("127.0.0.1:50916")?,
568     );
569     c.user_data = b"Hello!".to_vec();
570 
571     let (recv_ch_tx, mut recv_ch_rx) = mpsc::channel(1);
572     let (done_ch_tx, mut done_ch_rx) = mpsc::channel::<bool>(1);
573     let (close_ch_tx, mut close_ch_rx) = mpsc::channel::<bool>(1);
574     let conn_rx = Arc::clone(&conn);
575 
576     tokio::spawn(async move {
577         let mut buf = vec![0u8; 1500];
578         loop {
579             tokio::select! {
580                 result = conn_rx.recv_from(&mut buf) => {
581                     let (n, addr) = match result {
582                         Ok((n, addr)) => (n, addr),
583                         Err(err) => {
584                             log::debug!("ReadFrom returned: {}", err);
585                             break;
586                         }
587                     };
588 
589                     assert_eq!(n, 6, "{n} should match 6");
590                     assert_eq!(addr.to_string(), "127.0.0.1:4000", "addr should match");
591                     assert_eq!(&buf[..n], b"Hello!", "buf should match");
592 
593                     let _ = recv_ch_tx.send(true).await;
594                 }
595                 _ = close_ch_rx.recv() => {
596                     break;
597                 }
598             }
599         }
600 
601         drop(done_ch_tx);
602     });
603 
604     if let Net::VNet(vnet) = &nw {
605         let vnet = vnet.lock().await;
606         vnet.on_inbound_chunk(Box::new(c)).await;
607     } else {
608         panic!("must be virtual net");
609     }
610 
611     let _ = recv_ch_rx.recv().await;
612     drop(close_ch_tx);
613 
614     let _ = done_ch_rx.recv().await;
615 
616     Ok(())
617 }
618 
get_ipaddr(nic: &Arc<Mutex<dyn Nic + Send + Sync>>) -> Result<IpAddr>619 async fn get_ipaddr(nic: &Arc<Mutex<dyn Nic + Send + Sync>>) -> Result<IpAddr> {
620     let n = nic.lock().await;
621     let eth0 = n.get_interface("eth0").await.ok_or(Error::ErrNoInterface)?;
622     let addrs = eth0.addrs();
623     if addrs.is_empty() {
624         Err(Error::ErrNoAddressAssigned)
625     } else {
626         Ok(addrs[0].addr())
627     }
628 }
629 
630 //use std::io::Write;
631 
632 #[tokio::test]
test_net_virtual_end2end() -> Result<()>633 async fn test_net_virtual_end2end() -> Result<()> {
634     /*env_logger::Builder::new()
635     .format(|buf, record| {
636         writeln!(
637             buf,
638             "{}:{} [{}] {} - {}",
639             record.file().unwrap_or("unknown"),
640             record.line().unwrap_or(0),
641             record.level(),
642             chrono::Local::now().format("%H:%M:%S.%6f"),
643             record.args()
644         )
645     })
646     .filter(None, log::LevelFilter::Trace)
647     .init();*/
648 
649     let wan = Arc::new(Mutex::new(Router::new(RouterConfig {
650         cidr: "1.2.3.0/24".to_string(),
651         ..Default::default()
652     })?));
653 
654     let net1 = Net::new(Some(NetConfig::default()));
655     let ip1 = {
656         let nic = net1.get_nic()?;
657 
658         let mut w = wan.lock().await;
659         w.add_net(Arc::clone(&nic)).await?;
660 
661         {
662             let n = nic.lock().await;
663             n.set_router(Arc::clone(&wan)).await?;
664         }
665 
666         get_ipaddr(&nic).await?
667     };
668 
669     let net2 = Net::new(Some(NetConfig::default()));
670     let ip2 = {
671         let nic = net2.get_nic()?;
672 
673         let mut w = wan.lock().await;
674         w.add_net(Arc::clone(&nic)).await?;
675 
676         {
677             let n = nic.lock().await;
678             n.set_router(Arc::clone(&wan)).await?;
679         }
680 
681         get_ipaddr(&nic).await?
682     };
683 
684     let conn1 = net1.bind(SocketAddr::new(ip1, 1234)).await?;
685     let conn2 = net2.bind(SocketAddr::new(ip2, 5678)).await?;
686 
687     {
688         let mut w = wan.lock().await;
689         w.start().await?;
690     }
691 
692     let (close_ch_tx, mut close_ch_rx1) = broadcast::channel::<bool>(1);
693     let (done_ch_tx, mut done_ch_rx) = mpsc::channel::<bool>(1);
694     let (conn1_recv_ch_tx, mut conn1_recv_ch_rx) = mpsc::channel(1);
695     let conn1_rx = Arc::clone(&conn1);
696     let conn2_tr = Arc::clone(&conn2);
697     let mut close_ch_rx2 = close_ch_tx.subscribe();
698 
699     // conn1
700     tokio::spawn(async move {
701         let mut buf = vec![0u8; 1500];
702         loop {
703             log::debug!("conn1: wait for a message..");
704             tokio::select! {
705                 result = conn1_rx.recv_from(&mut buf) =>{
706                     let n = match result{
707                         Ok((n, _)) => n,
708                         Err(err) => {
709                             log::debug!("ReadFrom returned: {}", err);
710                             break;
711                         }
712                     };
713 
714                     log::debug!("conn1 received {:?}", &buf[..n]);
715                     let _ = conn1_recv_ch_tx.send(true).await;
716                 }
717                 _ = close_ch_rx1.recv() => {
718                     log::debug!("conn1 received close_ch_rx1");
719                     break;
720                 }
721             }
722         }
723         drop(done_ch_tx);
724         log::debug!("conn1 drop done_ch_tx, exit spawn");
725     });
726 
727     // conn2
728     tokio::spawn(async move {
729         let mut buf = vec![0u8; 1500];
730         loop {
731             log::debug!("conn2: wait for a message..");
732             tokio::select! {
733                 result = conn2_tr.recv_from(&mut buf) =>{
734                     let (n, addr) = match result{
735                         Ok((n, addr)) => (n, addr),
736                         Err(err) => {
737                             log::debug!("ReadFrom returned: {}", err);
738                             break;
739                         }
740                     };
741 
742                     log::debug!("conn2 received {:?}", &buf[..n]);
743 
744                     // echo back to conn1
745                     let n = conn2_tr.send_to(b"Good-bye!", addr).await?;
746                     assert_eq!( 9, n, "should match");
747                 }
748                 _ = close_ch_rx2.recv() => {
749                     log::debug!("conn1 received close_ch_rx2");
750                     break;
751                 }
752             }
753         }
754 
755         log::debug!("conn2 exit spawn");
756 
757         Result::<()>::Ok(())
758     });
759 
760     log::debug!("conn1: sending");
761     let n = conn1.send_to(b"Hello!", conn2.local_addr()?).await?;
762     assert_eq!(n, 6, "should match");
763 
764     let _ = conn1_recv_ch_rx.recv().await;
765     log::debug!("main recv conn1_recv_ch_rx");
766     drop(close_ch_tx);
767     log::debug!("main drop close_ch_tx");
768     let _ = done_ch_rx.recv().await;
769     log::debug!("main recv done_ch_rx");
770     Ok(())
771 }
772 
773 //use std::io::Write;
774 
775 #[tokio::test]
test_net_virtual_two_ips_on_a_nic() -> Result<()>776 async fn test_net_virtual_two_ips_on_a_nic() -> Result<()> {
777     /*env_logger::Builder::new()
778     .format(|buf, record| {
779         writeln!(
780             buf,
781             "{}:{} [{}] {} - {}",
782             record.file().unwrap_or("unknown"),
783             record.line().unwrap_or(0),
784             record.level(),
785             chrono::Local::now().format("%H:%M:%S.%6f"),
786             record.args()
787         )
788     })
789     .filter(None, log::LevelFilter::Trace)
790     .init();*/
791 
792     let wan = Arc::new(Mutex::new(Router::new(RouterConfig {
793         cidr: "1.2.3.0/24".to_string(),
794         ..Default::default()
795     })?));
796 
797     let net = Net::new(Some(NetConfig {
798         static_ips: vec![DEMO_IP.to_owned(), "1.2.3.5".to_owned()],
799         ..Default::default()
800     }));
801     {
802         let nic = net.get_nic()?;
803 
804         let mut w = wan.lock().await;
805         w.add_net(Arc::clone(&nic)).await?;
806 
807         let n = nic.lock().await;
808         n.set_router(Arc::clone(&wan)).await?;
809     }
810 
811     // start the router
812     {
813         let mut w = wan.lock().await;
814         w.start().await?;
815     }
816 
817     let (conn1, conn2) = (
818         net.bind(SocketAddr::new(Ipv4Addr::from_str(DEMO_IP)?.into(), 1234))
819             .await?,
820         net.bind(SocketAddr::new(Ipv4Addr::from_str("1.2.3.5")?.into(), 1234))
821             .await?,
822     );
823 
824     let (close_ch_tx, mut close_ch_rx1) = broadcast::channel::<bool>(1);
825     let (done_ch_tx, mut done_ch_rx) = mpsc::channel::<bool>(1);
826     let (conn1_recv_ch_tx, mut conn1_recv_ch_rx) = mpsc::channel(1);
827     let conn1_rx = Arc::clone(&conn1);
828     let conn2_tr = Arc::clone(&conn2);
829     let mut close_ch_rx2 = close_ch_tx.subscribe();
830 
831     // conn1
832     tokio::spawn(async move {
833         let mut buf = vec![0u8; 1500];
834         loop {
835             log::debug!("conn1: wait for a message..");
836             tokio::select! {
837                 result = conn1_rx.recv_from(&mut buf) =>{
838                     let n = match result{
839                         Ok((n, _)) => n,
840                         Err(err) => {
841                             log::debug!("ReadFrom returned: {}", err);
842                             break;
843                         }
844                     };
845 
846                     log::debug!("conn1 received {:?}", &buf[..n]);
847                     let _ = conn1_recv_ch_tx.send(true).await;
848                 }
849                 _ = close_ch_rx1.recv() => {
850                     log::debug!("conn1 received close_ch_rx1");
851                     break;
852                 }
853             }
854         }
855         drop(done_ch_tx);
856         log::debug!("conn1 drop done_ch_tx, exit spawn");
857     });
858 
859     // conn2
860     tokio::spawn(async move {
861         let mut buf = vec![0u8; 1500];
862         loop {
863             log::debug!("conn2: wait for a message..");
864             tokio::select! {
865                 result = conn2_tr.recv_from(&mut buf) =>{
866                     let (n, addr) = match result{
867                         Ok((n, addr)) => (n, addr),
868                         Err(err) => {
869                             log::debug!("ReadFrom returned: {}", err);
870                             break;
871                         }
872                     };
873 
874                     log::debug!("conn2 received {:?}", &buf[..n]);
875 
876                     // echo back to conn1
877                     let n = conn2_tr.send_to(b"Good-bye!", addr).await?;
878                     assert_eq!(n, 9, "should match");
879                 }
880                 _ = close_ch_rx2.recv() => {
881                     log::debug!("conn1 received close_ch_rx2");
882                     break;
883                 }
884             }
885         }
886 
887         log::debug!("conn2 exit spawn");
888 
889         Result::<()>::Ok(())
890     });
891 
892     log::debug!("conn1: sending");
893     let n = conn1.send_to(b"Hello!", conn2.local_addr()?).await?;
894     assert_eq!(n, 6, "should match");
895 
896     let _ = conn1_recv_ch_rx.recv().await;
897     log::debug!("main recv conn1_recv_ch_rx");
898     drop(close_ch_tx);
899     log::debug!("main drop close_ch_tx");
900     let _ = done_ch_rx.recv().await;
901     log::debug!("main recv done_ch_rx");
902     Ok(())
903 }
904