xref: /libuv/test/test-socket-buffer-size.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 static uv_udp_t udp;
30 static uv_tcp_t tcp;
31 static int close_cb_called;
32 
33 
close_cb(uv_handle_t * handle)34 static void close_cb(uv_handle_t* handle) {
35   close_cb_called++;
36 }
37 
38 
check_buffer_size(uv_handle_t * handle)39 static void check_buffer_size(uv_handle_t* handle) {
40   int value;
41 
42   value = 0;
43   ASSERT_OK(uv_recv_buffer_size(handle, &value));
44   ASSERT_GT(value, 0);
45 
46   value = 10000;
47   ASSERT_OK(uv_recv_buffer_size(handle, &value));
48 
49   value = 0;
50   ASSERT_OK(uv_recv_buffer_size(handle, &value));
51   /* linux sets double the value */
52   ASSERT(value == 10000 || value == 20000);
53 }
54 
55 
TEST_IMPL(socket_buffer_size)56 TEST_IMPL(socket_buffer_size) {
57   struct sockaddr_in addr;
58 
59   ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
60 
61   ASSERT_OK(uv_tcp_init(uv_default_loop(), &tcp));
62   ASSERT_OK(uv_tcp_bind(&tcp, (struct sockaddr*) &addr, 0));
63   check_buffer_size((uv_handle_t*) &tcp);
64   uv_close((uv_handle_t*) &tcp, close_cb);
65 
66   ASSERT_OK(uv_udp_init(uv_default_loop(), &udp));
67   ASSERT_OK(uv_udp_bind(&udp, (struct sockaddr*) &addr, 0));
68   check_buffer_size((uv_handle_t*) &udp);
69   uv_close((uv_handle_t*) &udp, close_cb);
70 
71   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
72 
73   ASSERT_EQ(2, close_cb_called);
74 
75   MAKE_VALGRIND_HAPPY(uv_default_loop());
76   return 0;
77 }
78