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#include <stdio.h>
25#include <stdlib.h>
26
27static int connection_cb_called = 0;
28static int close_cb_called = 0;
29static int connect_cb_called = 0;
30static uv_tcp_t server;
31static uv_tcp_t client;
32
33
34static void close_cb(uv_handle_t* handle) {
35  ASSERT_NOT_NULL(handle);
36  close_cb_called++;
37}
38
39
40static void connection_cb(uv_stream_t* tcp, int status) {
41  ASSERT(status == 0);
42  uv_close((uv_handle_t*)&server, close_cb);
43  connection_cb_called++;
44}
45
46
47static void start_server(void) {
48  struct sockaddr_in addr;
49  int r;
50
51  ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
52
53  r = uv_tcp_init(uv_default_loop(), &server);
54  ASSERT(r == 0);
55
56  r = uv_tcp_bind(&server, (const struct sockaddr*) &addr, 0);
57  ASSERT(r == 0);
58
59  r = uv_listen((uv_stream_t*)&server, 128, connection_cb);
60  ASSERT(r == 0);
61
62  r = uv_listen((uv_stream_t*)&server, 128, connection_cb);
63  ASSERT(r == 0);
64}
65
66
67static void connect_cb(uv_connect_t* req, int status) {
68  ASSERT_NOT_NULL(req);
69  ASSERT(status == 0);
70  free(req);
71  uv_close((uv_handle_t*)&client, close_cb);
72  connect_cb_called++;
73}
74
75
76static void client_connect(void) {
77  struct sockaddr_in addr;
78  uv_connect_t* connect_req = malloc(sizeof *connect_req);
79  int r;
80
81  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
82  ASSERT_NOT_NULL(connect_req);
83
84  r = uv_tcp_init(uv_default_loop(), &client);
85  ASSERT(r == 0);
86
87  r = uv_tcp_connect(connect_req,
88                     &client,
89                     (const struct sockaddr*) &addr,
90                     connect_cb);
91  ASSERT(r == 0);
92}
93
94
95
96TEST_IMPL(multiple_listen) {
97  start_server();
98
99  client_connect();
100
101  uv_run(uv_default_loop(), UV_RUN_DEFAULT);
102
103  ASSERT(connection_cb_called == 1);
104  ASSERT(connect_cb_called == 1);
105  ASSERT(close_cb_called == 2);
106
107  MAKE_VALGRIND_HAPPY();
108  return 0;
109}
110