unittest_udp_address.cpp 9.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
Copyright (c) 2018 Contributors as noted in the AUTHORS file

This file is part of 0MQ.

0MQ is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

0MQ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <unity.h>
#include "../tests/testutil.hpp"
22
#include "../unittests/unittest_resolver_common.hpp"
23 24 25 26 27 28 29 30 31 32 33 34

#include <ip.hpp>
#include <udp_address.hpp>

void setUp ()
{
}

void tearDown ()
{
}

35
//  Test an UDP address resolution. If 'dest_addr_' is NULL assume the
36
//  resolution is supposed to fail.
37 38 39 40
static void test_resolve (bool bind_,
                          int family_,
                          const char *name_,
                          const char *target_addr_,
41 42 43
                          uint16_t expected_port_,
                          const char *bind_addr_,
                          bool multicast_)
44
{
45 46 47 48
    if (family_ == AF_INET6 && !is_ipv6_available ()) {
        TEST_IGNORE_MESSAGE ("ipv6 is not available");
    }

49 50
    zmq::udp_address_t addr;

51
    int rc = addr.resolve (name_, bind_, family_ == AF_INET6);
52

53
    if (target_addr_ == NULL) {
54 55 56 57 58 59 60 61 62 63 64
        TEST_ASSERT_EQUAL (-1, rc);
        TEST_ASSERT_EQUAL (EINVAL, errno);
        return;
    } else {
        TEST_ASSERT_EQUAL (0, rc);
    }

    TEST_ASSERT_EQUAL (multicast_, addr.is_mcast ());

    if (bind_addr_ == NULL) {
        // Bind ANY
65 66 67 68 69
        if (family_ == AF_INET) {
            bind_addr_ = "0.0.0.0";
        } else {
            bind_addr_ = "::";
        }
70 71
    }

72 73
    validate_address (family_, addr.target_addr(), target_addr_, expected_port_);
    validate_address (family_, addr.bind_addr(), bind_addr_, expected_port_);
74 75
}

76 77 78
static void test_resolve_bind (int family_,
                               const char *name_, const char *dest_addr_,
                                uint16_t expected_port_ = 0,
79 80 81
                               const char *bind_addr_ = NULL,
                               bool multicast_ = false)
{
82
test_resolve (true, family_, name_, dest_addr_, expected_port_, bind_addr_,
83 84 85
                  multicast_);
}

86
static void test_resolve_connect (int family_, const char *name_, const char *dest_addr_,
87 88 89 90
                                  uint16_t expected_port_ = 0,
                                  const char *bind_addr_ = NULL,
                                  bool multicast_ = false)
{
91
test_resolve (false, family_, name_, dest_addr_, expected_port_, bind_addr_,
92 93 94
                  multicast_);
}

95 96
static void test_resolve_ipv4_simple ()
{
97 98 99 100 101 102
test_resolve_connect (AF_INET, "127.0.0.1:5555", "127.0.0.1", 5555);
}

static void test_resolve_ipv6_simple ()
{
    test_resolve_connect (AF_INET6, "[::1]:123", "::1", 123);
103 104 105 106
}

static void test_resolve_ipv4_bind ()
{
107 108 109 110 111 112
    test_resolve_bind (AF_INET, "127.0.0.1:5555", "127.0.0.1", 5555, "127.0.0.1");
}

static void test_resolve_ipv6_bind ()
{
    test_resolve_bind (AF_INET6, "[abcd::1234:1]:5555", "abcd::1234:1", 5555, "abcd::1234:1");
113 114 115 116
}

static void test_resolve_ipv4_bind_any ()
{
117 118 119 120 121 122
    test_resolve_bind (AF_INET, "*:*", "0.0.0.0", 0, "0.0.0.0");
}

static void test_resolve_ipv6_bind_any ()
{
    test_resolve_bind (AF_INET6, "*:*", "::", 0, "::");
123 124 125 126
}

static void test_resolve_ipv4_bind_anyport ()
{
127 128 129 130 131 132
    test_resolve_bind (AF_INET, "127.0.0.1:*", "127.0.0.1", 0, "127.0.0.1");
}

static void test_resolve_ipv6_bind_anyport ()
{
    test_resolve_bind (AF_INET6, "[1:2:3:4::5]:*", "1:2:3:4::5", 0, "1:2:3:4::5");
133 134 135 136
}

static void test_resolve_ipv4_bind_any_port ()
{
137 138 139 140 141 142
    test_resolve_bind (AF_INET, "*:5555", "0.0.0.0", 5555, "0.0.0.0");
}

static void test_resolve_ipv6_bind_any_port ()
{
    test_resolve_bind (AF_INET6, "*:5555", "::", 5555, "::");
143 144 145 146 147
}

static void test_resolve_ipv4_connect_any ()
{
    //  Cannot use wildcard for connection
148 149 150 151 152 153 154
    test_resolve_connect (AF_INET, "*:5555", NULL);
}

static void test_resolve_ipv6_connect_any ()
{
    //  Cannot use wildcard for connection
    test_resolve_connect (AF_INET6, "*:5555", NULL);
155 156 157 158
}

static void test_resolve_ipv4_connect_anyport ()
{
159 160 161 162 163 164
    test_resolve_connect (AF_INET, "127.0.0.1:*", NULL);
}

static void test_resolve_ipv6_connect_anyport ()
{
    test_resolve_connect (AF_INET6, "[::1]:*", NULL);
165 166
}

167 168
static void test_resolve_ipv4_connect_port0 ()
{
169 170 171 172 173 174
    test_resolve_connect (AF_INET, "127.0.0.1:0", "127.0.0.1", 0);
}

static void test_resolve_ipv6_connect_port0 ()
{
    test_resolve_connect (AF_INET6, "[2000:abcd::1]:0", "2000:abcd::1", 0);
175 176
}

177 178
static void test_resolve_ipv4_bind_mcast ()
{
179
    test_resolve_bind (AF_INET, "239.0.0.1:1234", "239.0.0.1", 1234, "0.0.0.0", true);
180 181
}

182
static void test_resolve_ipv6_bind_mcast ()
183
{
184
    test_resolve_bind (AF_INET6, "[ff00::1]:1234", "ff00::1", 1234, "::", true);
185 186
}

187
static void test_resolve_ipv4_connect_mcast ()
188
{
189 190
    test_resolve_connect (AF_INET, "239.0.0.1:2222", "239.0.0.1", 2222, NULL, true);
}
191

192 193 194
static void test_resolve_ipv6_connect_mcast ()
{
    test_resolve_connect (AF_INET6, "[ff00::1]:2222", "ff00::1", 2222, NULL, true);
195 196 197 198
}

static void test_resolve_ipv4_mcast_src_bind ()
{
199
    test_resolve_bind (AF_INET, "127.0.0.1;230.2.8.12:5555", "230.2.8.12", 5555,
200 201 202
                       "127.0.0.1", true);
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
static void test_resolve_ipv6_mcast_src_bind ()
{
    if (!is_ipv6_available ()) {
        TEST_IGNORE_MESSAGE ("ipv6 is not available");
    }

    zmq::udp_address_t addr;
    int rc = addr.resolve ("[::1];[ffab::4]:5555", true, true);

    //  For the time being this fails because we only support binding multicast
    //  by interface name, not interface IP
    TEST_ASSERT_EQUAL (-1, rc);
    TEST_ASSERT_EQUAL (ENODEV, errno);
}

218 219
static void test_resolve_ipv4_mcast_src_bind_any ()
{
220
    test_resolve_bind (AF_INET, "*;230.2.8.12:5555", "230.2.8.12", 5555,
221 222 223
                       "0.0.0.0", true);
}

224 225 226 227 228 229
static void test_resolve_ipv6_mcast_src_bind_any ()
{
    test_resolve_bind (AF_INET6, "*;[ffff::]:5555", "ffff::", 5555,
                       "::", true);
}

230 231
static void test_resolve_ipv4_mcast_src_connect ()
{
232
    test_resolve_connect (AF_INET, "8.9.10.11;230.2.8.12:5555", "230.2.8.12", 5555,
233 234 235
                          "8.9.10.11", true);
}

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
static void test_resolve_ipv6_mcast_src_connect ()
{
    if (!is_ipv6_available ()) {
        TEST_IGNORE_MESSAGE ("ipv6 is not available");
    }

    zmq::udp_address_t addr;
    int rc = addr.resolve ("[1:2:3::4];[ff01::1]:5555", false, true);

    //  For the time being this fails because we only support binding multicast
    //  by interface name, not interface IP
    TEST_ASSERT_EQUAL (-1, rc);
    TEST_ASSERT_EQUAL (ENODEV, errno);
}

251 252
static void test_resolve_ipv4_mcast_src_connect_any ()
{
253
    test_resolve_connect (AF_INET, "*;230.2.8.12:5555", "230.2.8.12", 5555,
254 255 256
                          "0.0.0.0", true);
}

257 258 259 260 261 262
static void test_resolve_ipv6_mcast_src_connect_any ()
{
    test_resolve_connect (AF_INET6, "*;[ff10::1]:5555", "ff10::1", 5555,
                          "::", true);
}

263 264
static void test_resolve_ipv4_mcast_src_bind_bad ()
{
265 266 267 268 269 270
    test_resolve_bind (AF_INET, "127.0.0.1;1.2.3.4:5555", NULL);
}

static void test_resolve_ipv6_mcast_src_bind_bad ()
{
    test_resolve_bind (AF_INET6, "[::1];[fe00::1]:5555", NULL);
271 272 273 274
}

static void test_resolve_ipv4_mcast_src_connect_bad ()
{
275 276 277 278 279 280
    test_resolve_connect (AF_INET, "127.0.0.1;1.2.3.4:5555", NULL);
}

static void test_resolve_ipv6_mcast_src_connect_bad ()
{
    test_resolve_connect (AF_INET6, "[::1];[fe00:1]:5555", NULL);
281 282 283 284 285 286 287 288 289 290
}

int main (void)
{
    zmq::initialize_network ();
    setup_test_environment ();

    UNITY_BEGIN ();

    RUN_TEST (test_resolve_ipv4_simple);
291
    RUN_TEST (test_resolve_ipv6_simple);
292
    RUN_TEST (test_resolve_ipv4_bind);
293
    RUN_TEST (test_resolve_ipv6_bind);
294
    RUN_TEST (test_resolve_ipv4_bind_any);
295
    RUN_TEST (test_resolve_ipv6_bind_any);
296
    RUN_TEST (test_resolve_ipv4_bind_anyport);
297
    RUN_TEST (test_resolve_ipv6_bind_anyport);
298
    RUN_TEST (test_resolve_ipv4_bind_any_port);
299
    RUN_TEST (test_resolve_ipv6_bind_any_port);
300
    RUN_TEST (test_resolve_ipv4_connect_any);
301
    RUN_TEST (test_resolve_ipv6_connect_any);
302
    RUN_TEST (test_resolve_ipv4_connect_anyport);
303
    RUN_TEST (test_resolve_ipv6_connect_anyport);
304
    RUN_TEST (test_resolve_ipv4_connect_port0);
305
    RUN_TEST (test_resolve_ipv6_connect_port0);
306
    RUN_TEST (test_resolve_ipv4_bind_mcast);
307
    RUN_TEST (test_resolve_ipv6_bind_mcast);
308
    RUN_TEST (test_resolve_ipv4_connect_mcast);
309
    RUN_TEST (test_resolve_ipv6_connect_mcast);
310
    RUN_TEST (test_resolve_ipv4_mcast_src_bind);
311
    RUN_TEST (test_resolve_ipv6_mcast_src_bind);
312
    RUN_TEST (test_resolve_ipv4_mcast_src_bind_any);
313
    RUN_TEST (test_resolve_ipv6_mcast_src_bind_any);
314
    RUN_TEST (test_resolve_ipv4_mcast_src_connect);
315
    RUN_TEST (test_resolve_ipv6_mcast_src_connect);
316
    RUN_TEST (test_resolve_ipv4_mcast_src_connect_any);
317
    RUN_TEST (test_resolve_ipv6_mcast_src_connect_any);
318
    RUN_TEST (test_resolve_ipv4_mcast_src_bind_bad);
319
    RUN_TEST (test_resolve_ipv6_mcast_src_bind_bad);
320
    RUN_TEST (test_resolve_ipv4_mcast_src_connect_bad);
321
    RUN_TEST (test_resolve_ipv6_mcast_src_connect_bad);
322 323 324 325 326

    zmq::shutdown_network ();

    return UNITY_END ();
}