xref: /libuv/test/test-udp-bind.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 
TEST_IMPL(udp_bind)30 TEST_IMPL(udp_bind) {
31   struct sockaddr_in addr;
32   uv_loop_t* loop;
33   uv_udp_t h1, h2;
34   int r;
35 
36   ASSERT_OK(uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
37 
38   loop = uv_default_loop();
39 
40   r = uv_udp_init(loop, &h1);
41   ASSERT_OK(r);
42 
43   r = uv_udp_init(loop, &h2);
44   ASSERT_OK(r);
45 
46   r = uv_udp_bind(&h1, (const struct sockaddr*) &addr, 0);
47   ASSERT_OK(r);
48 
49   r = uv_udp_bind(&h2, (const struct sockaddr*) &addr, 0);
50   ASSERT_EQ(r, UV_EADDRINUSE);
51 
52   uv_close((uv_handle_t*) &h1, NULL);
53   uv_close((uv_handle_t*) &h2, NULL);
54 
55   r = uv_run(loop, UV_RUN_DEFAULT);
56   ASSERT_OK(r);
57 
58   MAKE_VALGRIND_HAPPY(loop);
59   return 0;
60 }
61 
62 
TEST_IMPL(udp_bind_reuseaddr)63 TEST_IMPL(udp_bind_reuseaddr) {
64   struct sockaddr_in addr;
65   uv_loop_t* loop;
66   uv_udp_t h1, h2;
67   int r;
68 
69   ASSERT_OK(uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
70 
71   loop = uv_default_loop();
72 
73   r = uv_udp_init(loop, &h1);
74   ASSERT_OK(r);
75 
76   r = uv_udp_init(loop, &h2);
77   ASSERT_OK(r);
78 
79   r = uv_udp_bind(&h1, (const struct sockaddr*) &addr, UV_UDP_REUSEADDR);
80   ASSERT_OK(r);
81 
82   r = uv_udp_bind(&h2, (const struct sockaddr*) &addr, UV_UDP_REUSEADDR);
83   ASSERT_OK(r);
84 
85   uv_close((uv_handle_t*) &h1, NULL);
86   uv_close((uv_handle_t*) &h2, NULL);
87 
88   r = uv_run(loop, UV_RUN_DEFAULT);
89   ASSERT_OK(r);
90 
91   MAKE_VALGRIND_HAPPY(loop);
92   return 0;
93 }
94