xref: /libuv/test/test-udp-dgram-too-big.c (revision 011a1ac1)
1 /* Copyright Joyent, Inc. and other Node 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 #define CHECK_HANDLE(handle) \
30   ASSERT_PTR_EQ((uv_udp_t*)(handle), &handle_)
31 
32 #define CHECK_REQ(req) \
33   ASSERT_PTR_EQ((req), &req_);
34 
35 static uv_udp_t handle_;
36 static uv_udp_send_t req_;
37 
38 static int send_cb_called;
39 static int close_cb_called;
40 
41 
close_cb(uv_handle_t * handle)42 static void close_cb(uv_handle_t* handle) {
43   CHECK_HANDLE(handle);
44   close_cb_called++;
45 }
46 
47 
send_cb(uv_udp_send_t * req,int status)48 static void send_cb(uv_udp_send_t* req, int status) {
49   CHECK_REQ(req);
50   CHECK_HANDLE(req->handle);
51 
52   ASSERT_EQ(status, UV_EMSGSIZE);
53 
54   uv_close((uv_handle_t*)req->handle, close_cb);
55   send_cb_called++;
56 }
57 
58 
TEST_IMPL(udp_dgram_too_big)59 TEST_IMPL(udp_dgram_too_big) {
60   char dgram[65536]; /* 64K MTU is unlikely, even on localhost */
61   struct sockaddr_in addr;
62   uv_buf_t buf;
63   int r;
64 
65   memset(dgram, 42, sizeof dgram); /* silence valgrind */
66 
67   r = uv_udp_init(uv_default_loop(), &handle_);
68   ASSERT_OK(r);
69 
70   buf = uv_buf_init(dgram, sizeof dgram);
71   ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
72 
73   r = uv_udp_send(&req_,
74                   &handle_,
75                   &buf,
76                   1,
77                   (const struct sockaddr*) &addr,
78                   send_cb);
79   ASSERT_OK(r);
80 
81   ASSERT_OK(close_cb_called);
82   ASSERT_OK(send_cb_called);
83 
84   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
85 
86   ASSERT_EQ(1, send_cb_called);
87   ASSERT_EQ(1, close_cb_called);
88 
89   MAKE_VALGRIND_HAPPY(uv_default_loop());
90   return 0;
91 }
92