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#include <string.h>
27
28
29static int connect_cb_called = 0;
30static int close_cb_called = 0;
31
32
33static void connect_cb(uv_connect_t* handle, int status) {
34  ASSERT_NOT_NULL(handle);
35  connect_cb_called++;
36}
37
38
39static void close_cb(uv_handle_t* handle) {
40  ASSERT_NOT_NULL(handle);
41  close_cb_called++;
42}
43
44
45TEST_IMPL(tcp_connect6_error_fault) {
46  const char garbage[] =
47      "blah blah blah blah blah blah blah blah blah blah blah blah";
48  const struct sockaddr_in6* garbage_addr;
49  uv_tcp_t server;
50  int r;
51  uv_connect_t req;
52
53  if (!can_ipv6())
54    RETURN_SKIP("IPv6 not supported");
55
56  garbage_addr = (const struct sockaddr_in6*) &garbage;
57
58  r = uv_tcp_init(uv_default_loop(), &server);
59  ASSERT_OK(r);
60  r = uv_tcp_connect(&req,
61                     &server,
62                     (const struct sockaddr*) garbage_addr,
63                     connect_cb);
64  ASSERT_EQ(r, UV_EINVAL);
65
66  uv_close((uv_handle_t*)&server, close_cb);
67
68  uv_run(uv_default_loop(), UV_RUN_DEFAULT);
69
70  ASSERT_OK(connect_cb_called);
71  ASSERT_EQ(1, close_cb_called);
72
73  MAKE_VALGRIND_HAPPY(uv_default_loop());
74  return 0;
75}
76
77
78TEST_IMPL(tcp_connect6_link_local) {
79  uv_interface_address_t* ifs;
80  uv_interface_address_t* p;
81  struct sockaddr_in6 addr;
82  uv_connect_t req;
83  uv_tcp_t server;
84  int ok;
85  int n;
86
87  if (!can_ipv6())
88    RETURN_SKIP("IPv6 not supported");
89
90#if defined(__QEMU__)
91  /* qemu's sockaddr_in6 translation is broken pre-qemu 8.0.0
92   * when host endianness != guest endiannes.
93   * Fixed in https://github.com/qemu/qemu/commit/44cf6731d6b.
94   */
95  RETURN_SKIP("Test does not currently work in QEMU");
96#endif  /* defined(__QEMU__) */
97
98  /* Check there's an interface that routes link-local (fe80::/10) traffic. */
99  ASSERT_OK(uv_interface_addresses(&ifs, &n));
100  for (p = ifs; p < &ifs[n]; p++)
101    if (p->address.address6.sin6_family == AF_INET6)
102      if (!memcmp(&p->address.address6.sin6_addr, "\xfe\x80", 2))
103        break;
104  ok = (p < &ifs[n]);
105  uv_free_interface_addresses(ifs, n);
106
107  if (!ok)
108    RETURN_SKIP("IPv6 link-local traffic not supported");
109
110  ASSERT_OK(uv_ip6_addr("fe80::0bad:babe", 1337, &addr));
111  ASSERT_OK(uv_tcp_init(uv_default_loop(), &server));
112
113  /* We're making two shaky assumptions here:
114   * 1. There is a network interface that routes IPv6 link-local traffic, and
115   * 2. There is no firewall rule that blackholes or otherwise hard-kills the
116   *    connection attempt to the address above, i.e., we don't expect the
117   *    connect() system call to fail synchronously.
118   */
119  ASSERT_OK(uv_tcp_connect(&req,
120                           &server,
121                           (struct sockaddr*) &addr,
122                           connect_cb));
123
124  uv_close((uv_handle_t*) &server, NULL);
125  ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
126
127  MAKE_VALGRIND_HAPPY(uv_default_loop());
128  return 0;
129}
130