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