xref: /libuv/test/test-udp-sendmmsg-error.c (revision 011a1ac1)
1 /* Copyright 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 
28 #define DATAGRAMS 6
29 
30 static uv_udp_t client;
31 static uv_udp_send_t req[DATAGRAMS];
32 
33 static int send_cb_called;
34 static int close_cb_called;
35 
36 
close_cb(uv_handle_t * handle)37 static void close_cb(uv_handle_t* handle) {
38   ASSERT_PTR_EQ(handle, &client);
39   ASSERT(uv_is_closing(handle));
40   close_cb_called++;
41 }
42 
43 
send_cb(uv_udp_send_t * req,int status)44 static void send_cb(uv_udp_send_t* req, int status) {
45   if (status != 0)
46     ASSERT_EQ(status, UV_ECONNREFUSED);
47 
48   if (++send_cb_called == DATAGRAMS)
49     uv_close((uv_handle_t*)&client, close_cb);
50 }
51 
52 
TEST_IMPL(udp_sendmmsg_error)53 TEST_IMPL(udp_sendmmsg_error) {
54   struct sockaddr_in addr;
55   uv_buf_t buf;
56   int i;
57 
58   ASSERT_OK(uv_udp_init(uv_default_loop(), &client));
59   ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
60   ASSERT_OK(uv_udp_connect(&client, (const struct sockaddr*)&addr));
61 
62   buf = uv_buf_init("TEST", 4);
63   for (i = 0; i < DATAGRAMS; ++i)
64     ASSERT_OK(uv_udp_send(&req[i], &client, &buf, 1, NULL, send_cb));
65 
66   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
67 
68   ASSERT_EQ(1, close_cb_called);
69   ASSERT_EQ(DATAGRAMS, send_cb_called);
70 
71   ASSERT_OK(client.send_queue_size);
72 
73   MAKE_VALGRIND_HAPPY(uv_default_loop());
74   return 0;
75 }
76