~webapps/unity-js-scopes/node.js

« back to all changes in this revision

Viewing changes to deps/uv/test/test-tcp-squelch-connreset.c

  • Committer: Marcus Tomlinson
  • Date: 2015-11-13 07:59:04 UTC
  • Revision ID: marcus.tomlinson@canonical.com-20151113075904-h0swczmoq1rvstfc
Node v4 (stable)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2015, Santiago Gimeno <santiago.gimeno@gmail.com>
 
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
 
 
27
 
 
28
static uv_tcp_t tcp_server;
 
29
static uv_tcp_t tcp_client;
 
30
static uv_tcp_t tcp_server_client;
 
31
static uv_connect_t connect_req;
 
32
static uv_write_t write_req;
 
33
 
 
34
static void alloc_cb(uv_handle_t* handle,
 
35
                     size_t size,
 
36
                     uv_buf_t* buf) {
 
37
  buf->base = malloc(size);
 
38
  buf->len = size;
 
39
}
 
40
 
 
41
static void read_cb(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) {
 
42
  free(buf->base);
 
43
  ASSERT(nread == UV_EOF);
 
44
}
 
45
 
 
46
static void on_connect(uv_connect_t* req, int status) {
 
47
  int r;
 
48
  uv_buf_t outbuf;
 
49
 
 
50
  ASSERT(req != NULL);
 
51
  ASSERT(status == 0);
 
52
 
 
53
  outbuf = uv_buf_init("ping", 4);
 
54
  r = uv_write(&write_req, (uv_stream_t*) req->handle, &outbuf, 1, NULL);
 
55
  ASSERT(r == 0);
 
56
 
 
57
  r = uv_read_start((uv_stream_t*) req->handle, alloc_cb, read_cb);
 
58
  ASSERT(r == 0);
 
59
}
 
60
 
 
61
static void on_connection(uv_stream_t* server, int status) {
 
62
  int r;
 
63
 
 
64
  ASSERT(status == 0);
 
65
 
 
66
  r = uv_tcp_init(uv_default_loop(), &tcp_server_client);
 
67
  ASSERT(r == 0);
 
68
 
 
69
  r = uv_accept(server, (uv_stream_t*) &tcp_server_client);
 
70
  ASSERT(r == 0);
 
71
 
 
72
  uv_close((uv_handle_t*) &tcp_server_client, NULL);
 
73
  uv_close((uv_handle_t*) &tcp_server, NULL);
 
74
}
 
75
 
 
76
static void start_server(void) {
 
77
  struct sockaddr_in addr;
 
78
  int r;
 
79
 
 
80
  ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
 
81
 
 
82
  r = uv_tcp_init(uv_default_loop(), &tcp_server);
 
83
  ASSERT(r == 0);
 
84
 
 
85
  r = uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0);
 
86
  ASSERT(r == 0);
 
87
 
 
88
  r = uv_listen((uv_stream_t*) &tcp_server, SOMAXCONN, on_connection);
 
89
  ASSERT(r == 0);
 
90
}
 
91
 
 
92
static void start_client(void) {
 
93
  struct sockaddr_in addr;
 
94
  int r;
 
95
 
 
96
  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
 
97
 
 
98
  r = uv_tcp_init(uv_default_loop(), &tcp_client);
 
99
  ASSERT(r == 0);
 
100
 
 
101
  r = uv_tcp_connect(&connect_req,
 
102
                     &tcp_client,
 
103
                     (const struct sockaddr*) &addr,
 
104
                     on_connect);
 
105
  ASSERT(r == 0);
 
106
}
 
107
 
 
108
 
 
109
TEST_IMPL(tcp_squelch_connreset) {
 
110
 
 
111
  start_server();
 
112
 
 
113
  start_client();
 
114
 
 
115
  uv_run(uv_default_loop(), UV_RUN_DEFAULT);
 
116
 
 
117
  MAKE_VALGRIND_HAPPY();
 
118
  return 0;
 
119
}