1 use futures::join;
2 use test_programs::p3::wasi::sockets::types::{
3     IpAddress, IpAddressFamily, IpSocketAddress, Ipv4SocketAddress, Ipv6SocketAddress, UdpSocket,
4 };
5 
6 struct Component;
7 
8 test_programs::p3::export!(Component);
9 
10 async fn test_udp_sample_application(family: IpAddressFamily, bind_address: IpSocketAddress) {
11     let unspecified_addr = IpSocketAddress::new(IpAddress::new_unspecified(family), 0);
12 
13     let first_message = &[];
14     let second_message = b"Hello, world!";
15     let third_message = b"Greetings, planet!";
16 
17     let server = UdpSocket::create(family).unwrap();
18 
19     server.bind(bind_address).unwrap();
20     let addr = server.get_local_address().unwrap();
21 
22     let client = UdpSocket::create(family).unwrap();
23     client.bind(unspecified_addr).unwrap();
24     client.connect(addr).unwrap();
25     let client_addr = client.get_local_address().unwrap();
26     join!(
27         async {
28             client.send(first_message.to_vec(), None).await.unwrap();
29             client
30                 .send(second_message.to_vec(), Some(addr))
31                 .await
32                 .unwrap();
33         },
34         async {
35             // Check that we've received our sent messages.
36             let (buf, addr) = server.receive().await.unwrap();
37             assert_eq!(buf, first_message);
38             assert_eq!(addr, client_addr);
39 
40             let (buf, addr) = server.receive().await.unwrap();
41             assert_eq!(buf, second_message);
42             assert_eq!(addr, client_addr);
43         }
44     );
45     join!(
46         async {
47             // Another client
48             let client = UdpSocket::create(family).unwrap();
49             client.bind(unspecified_addr).unwrap();
50             client
51                 .send(third_message.to_vec(), Some(addr))
52                 .await
53                 .unwrap();
54         },
55         async {
56             // Check that we sent and received our message!
57             let (buf, _) = server.receive().await.unwrap();
58             assert_eq!(buf, third_message);
59         },
60     );
61 }
62 
63 impl test_programs::p3::exports::wasi::cli::run::Guest for Component {
64     async fn run() -> Result<(), ()> {
65         test_udp_sample_application(
66             IpAddressFamily::Ipv4,
67             IpSocketAddress::Ipv4(Ipv4SocketAddress {
68                 port: 0,                 // use any free port
69                 address: (127, 0, 0, 1), // localhost
70             }),
71         )
72         .await;
73         test_udp_sample_application(
74             IpAddressFamily::Ipv6,
75             IpSocketAddress::Ipv6(Ipv6SocketAddress {
76                 port: 0,                           // use any free port
77                 address: (0, 0, 0, 0, 0, 0, 0, 1), // localhost
78                 flow_info: 0,
79                 scope_id: 0,
80             }),
81         )
82         .await;
83         Ok(())
84     }
85 }
86 
87 fn main() {}
88