PageRenderTime 31ms CodeModel.GetById 17ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/test/test-pipe-bind-error.c

http://github.com/joyent/libuv
C | 144 lines | 82 code | 42 blank | 20 comment | 23 complexity | f0ffebc971e65cbc925cb374daf32f0d MD5 | raw file
  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
 27
 28#ifdef _WIN32
 29# define BAD_PIPENAME "bad-pipe"
 30#else
 31# define BAD_PIPENAME "/path/to/unix/socket/that/really/should/not/be/there"
 32#endif
 33
 34
 35static int close_cb_called = 0;
 36
 37
 38static void close_cb(uv_handle_t* handle) {
 39  ASSERT(handle != NULL);
 40  close_cb_called++;
 41}
 42
 43
 44TEST_IMPL(pipe_bind_error_addrinuse) {
 45  uv_pipe_t server1, server2;
 46  int r;
 47
 48  r = uv_pipe_init(uv_default_loop(), &server1, 0);
 49  ASSERT(r == 0);
 50  r = uv_pipe_bind(&server1, TEST_PIPENAME);
 51  ASSERT(r == 0);
 52
 53  r = uv_pipe_init(uv_default_loop(), &server2, 0);
 54  ASSERT(r == 0);
 55  r = uv_pipe_bind(&server2, TEST_PIPENAME);
 56  ASSERT(r == -1);
 57
 58  ASSERT(uv_last_error(uv_default_loop()).code == UV_EADDRINUSE);
 59
 60  r = uv_listen((uv_stream_t*)&server1, SOMAXCONN, NULL);
 61  ASSERT(r == 0);
 62  r = uv_listen((uv_stream_t*)&server2, SOMAXCONN, NULL);
 63  ASSERT(r == -1);
 64
 65  ASSERT(uv_last_error(uv_default_loop()).code == UV_EINVAL);
 66
 67  uv_close((uv_handle_t*)&server1, close_cb);
 68  uv_close((uv_handle_t*)&server2, close_cb);
 69
 70  uv_run(uv_default_loop());
 71
 72  ASSERT(close_cb_called == 2);
 73
 74  MAKE_VALGRIND_HAPPY();
 75  return 0;
 76}
 77
 78
 79TEST_IMPL(pipe_bind_error_addrnotavail) {
 80  uv_pipe_t server;
 81  int r;
 82
 83  r = uv_pipe_init(uv_default_loop(), &server, 0);
 84  ASSERT(r == 0);
 85  r = uv_pipe_bind(&server, BAD_PIPENAME);
 86
 87  ASSERT(r == -1);
 88  ASSERT(uv_last_error(uv_default_loop()).code == UV_EACCES);
 89
 90  uv_close((uv_handle_t*)&server, close_cb);
 91
 92  uv_run(uv_default_loop());
 93
 94  ASSERT(close_cb_called == 1);
 95
 96  MAKE_VALGRIND_HAPPY();
 97  return 0;
 98}
 99
100
101TEST_IMPL(pipe_bind_error_inval) {
102  uv_pipe_t server;
103  int r;
104
105  r = uv_pipe_init(uv_default_loop(), &server, 0);
106  ASSERT(r == 0);
107  r = uv_pipe_bind(&server, TEST_PIPENAME);
108  ASSERT(r == 0);
109  r = uv_pipe_bind(&server, TEST_PIPENAME_2);
110  ASSERT(r == -1);
111
112  ASSERT(uv_last_error(uv_default_loop()).code == UV_EINVAL);
113
114  uv_close((uv_handle_t*)&server, close_cb);
115
116  uv_run(uv_default_loop());
117
118  ASSERT(close_cb_called == 1);
119
120  MAKE_VALGRIND_HAPPY();
121  return 0;
122}
123
124
125TEST_IMPL(pipe_listen_without_bind) {
126  uv_pipe_t server;
127  int r;
128
129  r = uv_pipe_init(uv_default_loop(), &server, 0);
130  ASSERT(r == 0);
131  r = uv_listen((uv_stream_t*)&server, SOMAXCONN, NULL);
132  ASSERT(r == -1);
133
134  ASSERT(uv_last_error(uv_default_loop()).code == UV_EINVAL);
135
136  uv_close((uv_handle_t*)&server, close_cb);
137
138  uv_run(uv_default_loop());
139
140  ASSERT(close_cb_called == 1);
141
142  MAKE_VALGRIND_HAPPY();
143  return 0;
144}