libuv-svace-build
123 строки · 3.5 Кб
1/* Copyright libuv project and 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 <stdio.h>23#include <stdlib.h>24#include <string.h>25
26#include "uv.h"27#include "task.h"28
29static uv_tcp_t server;30static uv_tcp_t client;31static uv_tcp_t incoming;32static int connect_cb_called;33static int close_cb_called;34static int connection_cb_called;35static uv_write_t write_req;36
37static char hello[] = "HELLO!";38
39
40static void close_cb(uv_handle_t* handle) {41close_cb_called++;42}
43
44static void write_cb(uv_write_t* req, int status) {45ASSERT_OK(status);46}
47
48static void conn_alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {49/* Do nothing, read_cb should be called with UV_ENOBUFS. */50}
51
52static void conn_read_cb(uv_stream_t* stream,53ssize_t nread,54const uv_buf_t* buf) {55ASSERT_EQ(nread, UV_ENOBUFS);56ASSERT_NULL(buf->base);57ASSERT_OK(buf->len);58
59uv_close((uv_handle_t*) &incoming, close_cb);60uv_close((uv_handle_t*) &client, close_cb);61uv_close((uv_handle_t*) &server, close_cb);62}
63
64static void connect_cb(uv_connect_t* req, int status) {65int r;66uv_buf_t buf;67
68ASSERT_OK(status);69connect_cb_called++;70
71buf = uv_buf_init(hello, sizeof(hello));72r = uv_write(&write_req, req->handle, &buf, 1, write_cb);73ASSERT_OK(r);74}
75
76
77static void connection_cb(uv_stream_t* tcp, int status) {78ASSERT_OK(status);79
80ASSERT_OK(uv_tcp_init(tcp->loop, &incoming));81ASSERT_OK(uv_accept(tcp, (uv_stream_t*) &incoming));82ASSERT_OK(uv_read_start((uv_stream_t*) &incoming,83conn_alloc_cb,84conn_read_cb));85
86connection_cb_called++;87}
88
89
90static void start_server(void) {91struct sockaddr_in addr;92
93ASSERT_OK(uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));94
95ASSERT_OK(uv_tcp_init(uv_default_loop(), &server));96ASSERT_OK(uv_tcp_bind(&server, (struct sockaddr*) &addr, 0));97ASSERT_OK(uv_listen((uv_stream_t*) &server, 128, connection_cb));98}
99
100
101TEST_IMPL(tcp_alloc_cb_fail) {102uv_connect_t connect_req;103struct sockaddr_in addr;104
105start_server();106
107ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));108
109ASSERT_OK(uv_tcp_init(uv_default_loop(), &client));110ASSERT_OK(uv_tcp_connect(&connect_req,111&client,112(struct sockaddr*) &addr,113connect_cb));114
115ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));116
117ASSERT_EQ(1, connect_cb_called);118ASSERT_EQ(1, connection_cb_called);119ASSERT_EQ(3, close_cb_called);120
121MAKE_VALGRIND_HAPPY(uv_default_loop());122return 0;123}
124