xref: /libuv/test/test-udp-recv-in-a-row.c (revision 011a1ac1)
1 /* Copyright The libuv project and contributors. All rights reserved.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21 
22 #include "uv.h"
23 #include "task.h"
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 static uv_udp_t server;
30 static uv_udp_t client;
31 static uv_check_t check_handle;
32 static uv_buf_t buf;
33 static struct sockaddr_in addr;
34 static char send_data[10];
35 static int check_cb_called;
36 
37 #define N 5
38 static int recv_cnt;
39 
alloc_cb(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)40 static void alloc_cb(uv_handle_t* handle,
41                      size_t suggested_size,
42                      uv_buf_t* buf) {
43   static char slab[sizeof(send_data)];
44   buf->base = slab;
45   buf->len = sizeof(slab);
46 }
47 
sv_recv_cb(uv_udp_t * handle,ssize_t nread,const uv_buf_t * rcvbuf,const struct sockaddr * addr,unsigned flags)48 static void sv_recv_cb(uv_udp_t* handle,
49                        ssize_t nread,
50                        const uv_buf_t* rcvbuf,
51                        const struct sockaddr* addr,
52                        unsigned flags) {
53   if (++ recv_cnt < N) {
54     ASSERT_EQ(sizeof(send_data), nread);
55   } else {
56     ASSERT_OK(nread);
57   }
58 }
59 
check_cb(uv_check_t * handle)60 static void check_cb(uv_check_t* handle) {
61   ASSERT_PTR_EQ(&check_handle, handle);
62 
63   /**
64    * sv_recv_cb() is called with nread set to zero to indicate
65    * there is no more udp packet in the kernel, so the actual
66    * recv_cnt is one larger than N.
67    */
68   ASSERT_EQ(N+1, recv_cnt);
69   check_cb_called = 1;
70 
71   /* we are done */
72   ASSERT_OK(uv_check_stop(handle));
73   uv_close((uv_handle_t*) &client, NULL);
74   uv_close((uv_handle_t*) &check_handle, NULL);
75   uv_close((uv_handle_t*) &server, NULL);
76 }
77 
78 
TEST_IMPL(udp_recv_in_a_row)79 TEST_IMPL(udp_recv_in_a_row) {
80   int i, r;
81 
82   ASSERT_OK(uv_check_init(uv_default_loop(), &check_handle));
83   ASSERT_OK(uv_check_start(&check_handle, check_cb));
84 
85   ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
86 
87   ASSERT_OK(uv_udp_init(uv_default_loop(), &server));
88   ASSERT_OK(uv_udp_bind(&server, (const struct sockaddr*) &addr, 0));
89   ASSERT_OK(uv_udp_recv_start(&server, alloc_cb, sv_recv_cb));
90 
91   ASSERT_OK(uv_udp_init(uv_default_loop(), &client));
92 
93   /* send N-1 udp packets */
94   buf = uv_buf_init(send_data, sizeof(send_data));
95   for (i = 0; i < N - 1; i ++) {
96     r = uv_udp_try_send(&client,
97                         &buf,
98                         1,
99                         (const struct sockaddr*) &addr);
100     ASSERT_EQ(sizeof(send_data), r);
101   }
102 
103   /* send an empty udp packet */
104   buf = uv_buf_init(NULL, 0);
105   r = uv_udp_try_send(&client,
106                       &buf,
107                       1,
108                       (const struct sockaddr*) &addr);
109   ASSERT_OK(r);
110 
111   /* check_cb() asserts that the N packets can be received
112    * before it gets called.
113    */
114 
115   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
116 
117   ASSERT(check_cb_called);
118 
119   MAKE_VALGRIND_HAPPY(uv_default_loop());
120   return 0;
121 }
122