sockettest.c 13.6 KB
Newer Older
1 2 3
/*
 * sockettest.c: Testing for src/util/network.c APIs
 *
4
 * Copyright (C) 2010-2011, 2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library 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 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22 23 24 25 26 27
 *
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

28
#include "virsocketaddr.h"
29
#include "testutils.h"
30
#include "virlog.h"
31
#include "viralloc.h"
32

33 34
VIR_LOG_INIT("tests.sockettest");

35 36 37 38
static int testParse(virSocketAddr *addr, const char *addrstr, int family, bool pass)
{
    int rc;

39
    rc = virSocketAddrParse(addr, addrstr, family);
40 41 42 43 44 45 46 47 48 49 50

    if (rc < 0)
        return pass ? -1 : 0;
    else
        return pass ? 0 : -1;
}

static int testFormat(virSocketAddr *addr, const char *addrstr, bool pass)
{
    char *newaddrstr;

51
    newaddrstr = virSocketAddrFormat(addr);
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    if (!newaddrstr)
        return pass ? -1 : 0;

    if (STRNEQ(newaddrstr, addrstr)) {
        virtTestDifference(stderr, newaddrstr, addrstr);
        VIR_FREE(newaddrstr);
        return pass ? -1 : 0;
    } else {
        VIR_FREE(newaddrstr);
        return pass ? 0 : -1;
    }
}

struct testParseData {
    virSocketAddr *addr;
    const char *addrstr;
    int family;
    bool pass;
};
static int testParseHelper(const void *opaque)
{
    const struct testParseData *data = opaque;
    return testParse(data->addr, data->addrstr, data->family, data->pass);
}

struct testFormatData {
    virSocketAddr *addr;
    const char *addrstr;
    bool pass;
};
static int testFormatHelper(const void *opaque)
{
    const struct testFormatData *data = opaque;
    return testFormat(data->addr, data->addrstr, data->pass);
}


static int testRange(const char *saddrstr, const char *eaddrstr, int size, bool pass)
{
    virSocketAddr saddr;
    virSocketAddr eaddr;

94
    if (virSocketAddrParse(&saddr, saddrstr, AF_UNSPEC) < 0)
95
        return -1;
96
    if (virSocketAddrParse(&eaddr, eaddrstr, AF_UNSPEC) < 0)
97 98
        return -1;

99
    int gotsize = virSocketAddrGetRange(&saddr, &eaddr);
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    VIR_DEBUG("Size want %d vs got %d", size, gotsize);
    if (gotsize < 0 || gotsize != size) {
        return pass ? -1 : 0;
    } else {
        return pass ? 0 : -1;
    }
}

struct testRangeData {
    const char *saddr;
    const char *eaddr;
    int size;
    bool pass;
};
static int testRangeHelper(const void *opaque)
{
    const struct testRangeData *data = opaque;
    return testRange(data->saddr, data->eaddr, data->size, data->pass);
}


static int testNetmask(const char *addr1str, const char *addr2str,
                       const char *netmaskstr, bool pass)
{
    virSocketAddr addr1;
    virSocketAddr addr2;
    virSocketAddr netmask;

128
    if (virSocketAddrParse(&addr1, addr1str, AF_UNSPEC) < 0)
129
        return -1;
130
    if (virSocketAddrParse(&addr2, addr2str, AF_UNSPEC) < 0)
131
        return -1;
132
    if (virSocketAddrParse(&netmask, netmaskstr, AF_UNSPEC) < 0)
133 134
        return -1;

135
    int ret = virSocketAddrCheckNetmask(&addr1, &addr2, &netmask);
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

    if (ret <= 0) {
        return pass ? -1 : 0;
    } else {
        return pass ? 0 : -1;
    }
}

struct testNetmaskData {
    const char *addr1;
    const char *addr2;
    const char *netmask;
    bool pass;
};
static int testNetmaskHelper(const void *opaque)
{
    const struct testNetmaskData *data = opaque;
    return testNetmask(data->addr1, data->addr2, data->netmask, data->pass);
}

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198


static int testMaskNetwork(const char *addrstr,
                           int prefix,
                           const char *networkstr)
{
    virSocketAddr addr;
    virSocketAddr network;
    char *gotnet = NULL;

    /* Intentionally fill with garbage */
    memset(&network, 1, sizeof(network));

    if (virSocketAddrParse(&addr, addrstr, AF_UNSPEC) < 0)
        return -1;

    if (virSocketAddrMaskByPrefix(&addr, prefix, &network) < 0)
        return -1;

    if (!(gotnet = virSocketAddrFormat(&network)))
        return -1;

    if (STRNEQ(networkstr, gotnet)) {
        VIR_FREE(gotnet);
        fprintf(stderr, "Expected %s, got %s\n", networkstr, gotnet);
        return -1;
    }
    VIR_FREE(gotnet);
    return 0;
}

struct testMaskNetworkData {
    const char *addr1;
    int prefix;
    const char *network;
};
static int testMaskNetworkHelper(const void *opaque)
{
    const struct testMaskNetworkData *data = opaque;
    return testMaskNetwork(data->addr1, data->prefix, data->network);
}


199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
static int testWildcard(const char *addrstr,
                        bool pass)
{
    virSocketAddr addr;

    if (virSocketAddrParse(&addr, addrstr, AF_UNSPEC) < 0)
        return -1;

    if (virSocketAddrIsWildcard(&addr))
        return pass ? 0 : -1;
    return pass ? -1 : 0;
}

struct testWildcardData {
    const char *addr;
    bool pass;
};
static int testWildcardHelper(const void *opaque)
{
    const struct testWildcardData *data = opaque;
    return testWildcard(data->addr, data->pass);
}
221

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
struct testIsNumericData {
    const char *addr;
    bool pass;
};

static int
testIsNumericHelper(const void *opaque)
{
    const struct testIsNumericData *data = opaque;

    if (virSocketAddrIsNumeric(data->addr))
        return data->pass ? 0 : -1;
    return data->pass ? -1 : 0;
}

237
static int
E
Eric Blake 已提交
238
mymain(void)
239 240 241 242 243 244
{
    int ret = 0;
    /* Some of our tests deliberately test failure cases, so
     * register a handler to stop error messages cluttering
     * up display
     */
245
    virtTestQuiesceLibvirtErrors(false);
246 247 248 249 250 251 252

#define DO_TEST_PARSE(addrstr, family, pass)                            \
    do {                                                                \
        virSocketAddr addr;                                             \
        struct testParseData data = { &addr, addrstr, family, pass };   \
        memset(&addr, 0, sizeof(addr));                                 \
        if (virtTestRun("Test parse " addrstr,                          \
253
                        testParseHelper, &data) < 0)                    \
254 255 256 257 258 259 260 261 262
            ret = -1;                                                   \
    } while (0)

#define DO_TEST_PARSE_AND_FORMAT(addrstr, family, pass)                 \
    do {                                                                \
        virSocketAddr addr;                                             \
        struct testParseData data = { &addr, addrstr, family, pass };   \
        memset(&addr, 0, sizeof(addr));                                 \
        if (virtTestRun("Test parse " addrstr " family " #family,       \
263
                        testParseHelper, &data) < 0)                    \
264 265 266
            ret = -1;                                                   \
        struct testFormatData data2 = { &addr, addrstr, pass };         \
        if (virtTestRun("Test format " addrstr " family " #family,      \
267
                        testFormatHelper, &data2) < 0)                  \
268 269 270
            ret = -1;                                                   \
    } while (0)

271 272 273 274 275 276
#define DO_TEST_PARSE_AND_CHECK_FORMAT(addrstr, addrformated, family, pass) \
    do {                                                                \
        virSocketAddr addr;                                             \
        struct testParseData data = { &addr, addrstr, family, true};    \
        memset(&addr, 0, sizeof(addr));                                 \
        if (virtTestRun("Test parse " addrstr " family " #family,       \
277
                        testParseHelper, &data) < 0)                    \
278 279 280
            ret = -1;                                                   \
        struct testFormatData data2 = { &addr, addrformated, pass };    \
        if (virtTestRun("Test format " addrstr " family " #family,      \
281
                        testFormatHelper, &data2) < 0)                  \
282 283 284
            ret = -1;                                                   \
    } while (0)

285 286 287 288
#define DO_TEST_RANGE(saddr, eaddr, size, pass)                         \
    do {                                                                \
        struct testRangeData data = { saddr, eaddr, size, pass };       \
        if (virtTestRun("Test range " saddr " -> " eaddr " size " #size, \
289
                        testRangeHelper, &data) < 0)                    \
290 291 292 293 294 295 296
            ret = -1;                                                   \
    } while (0)

#define DO_TEST_NETMASK(addr1, addr2, netmask, pass)                    \
    do {                                                                \
        struct testNetmaskData data = { addr1, addr2, netmask, pass };  \
        if (virtTestRun("Test netmask " addr1 " + " addr2 " in " netmask, \
297
                        testNetmaskHelper, &data) < 0)                  \
298 299 300
            ret = -1;                                                   \
    } while (0)

301 302 303 304 305 306 307 308
#define DO_TEST_MASK_NETWORK(addr1, prefix, network)                    \
    do {                                                                \
        struct testMaskNetworkData data = { addr1, prefix, network };   \
        if (virtTestRun("Test mask network " addr1 " / " #prefix " == " network, \
                        testMaskNetworkHelper, &data) < 0)              \
            ret = -1;                                                   \
    } while (0)

309 310 311
#define DO_TEST_WILDCARD(addr, pass)                                    \
    do {                                                                \
        struct testWildcardData data = { addr, pass};                   \
312
        if (virtTestRun("Test wildcard " addr,                          \
313 314 315 316
                        testWildcardHelper, &data) < 0)                 \
            ret = -1;                                                   \
    } while (0)

317 318 319 320 321 322 323 324
#define DO_TEST_IS_NUMERIC(addr, pass)                                  \
    do {                                                                \
        struct testIsNumericData data = { addr, pass};                  \
        if (virtTestRun("Test isNumeric " addr,                         \
                       testIsNumericHelper, &data) < 0)                 \
            ret = -1;                                                   \
    } while (0)

325 326 327 328 329 330 331

    DO_TEST_PARSE_AND_FORMAT("127.0.0.1", AF_UNSPEC, true);
    DO_TEST_PARSE_AND_FORMAT("127.0.0.1", AF_INET, true);
    DO_TEST_PARSE_AND_FORMAT("127.0.0.1", AF_INET6, false);
    DO_TEST_PARSE_AND_FORMAT("127.0.0.1", AF_UNIX, false);
    DO_TEST_PARSE_AND_FORMAT("127.0.0.256", AF_UNSPEC, false);

332 333 334 335 336 337 338 339 340 341
    DO_TEST_PARSE_AND_CHECK_FORMAT("127.0.0.2", "127.0.0.2", AF_INET, true);
    DO_TEST_PARSE_AND_CHECK_FORMAT("127.0.0.2", "127.0.0.3", AF_INET, false);
    DO_TEST_PARSE_AND_CHECK_FORMAT("0", "0.0.0.0", AF_INET, true);
    DO_TEST_PARSE_AND_CHECK_FORMAT("127", "0.0.0.127", AF_INET, true);
    DO_TEST_PARSE_AND_CHECK_FORMAT("127", "127.0.0.0", AF_INET, false);
    DO_TEST_PARSE_AND_CHECK_FORMAT("127.2", "127.0.0.2", AF_INET, true);
    DO_TEST_PARSE_AND_CHECK_FORMAT("127.2", "127.2.0.0", AF_INET, false);
    DO_TEST_PARSE_AND_CHECK_FORMAT("1.2.3", "1.2.0.3", AF_INET, true);
    DO_TEST_PARSE_AND_CHECK_FORMAT("1.2.3", "1.2.3.0", AF_INET, false);

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
    DO_TEST_PARSE_AND_FORMAT("::1", AF_UNSPEC, true);
    DO_TEST_PARSE_AND_FORMAT("::1", AF_INET, false);
    DO_TEST_PARSE_AND_FORMAT("::1", AF_INET6, true);
    DO_TEST_PARSE_AND_FORMAT("::1", AF_UNIX, false);
    DO_TEST_PARSE_AND_FORMAT("::ffff", AF_UNSPEC, true);

    DO_TEST_RANGE("192.168.122.1", "192.168.122.1", 1, true);
    DO_TEST_RANGE("192.168.122.1", "192.168.122.20", 20, true);
    DO_TEST_RANGE("192.168.122.0", "192.168.122.255", 256, true);
    DO_TEST_RANGE("192.168.122.20", "192.168.122.1", -1, false);
    DO_TEST_RANGE("10.0.0.1", "192.168.122.20", -1, false);
    DO_TEST_RANGE("192.168.122.20", "10.0.0.1", -1, false);

    DO_TEST_RANGE("2000::1", "2000::1", 1, true);
    DO_TEST_RANGE("2000::1", "2000::2", 2, true);
    DO_TEST_RANGE("2000::2", "2000::1", -1, false);
    DO_TEST_RANGE("2000::1", "9001::1", -1, false);

    DO_TEST_NETMASK("192.168.122.1", "192.168.122.2",
                    "255.255.255.0", true);
    DO_TEST_NETMASK("192.168.122.1", "192.168.122.4",
                    "255.255.255.248", true);
    DO_TEST_NETMASK("192.168.122.1", "192.168.123.2",
                    "255.255.255.0", false);
    DO_TEST_NETMASK("192.168.122.1", "192.168.123.2",
                    "255.255.0.0", true);

    DO_TEST_NETMASK("2000::1:1", "2000::1:1",
                    "ffff:ffff:ffff:ffff:ffff:ffff:ffff:0", true);
    DO_TEST_NETMASK("2000::1:1", "2000::2:1",
                    "ffff:ffff:ffff:ffff:ffff:ffff:ffff:0", false);
    DO_TEST_NETMASK("2000::1:1", "2000::2:1",
                    "ffff:ffff:ffff:ffff:ffff:ffff:fff8:0", true);
    DO_TEST_NETMASK("2000::1:1", "9000::1:1",
                    "ffff:ffff:ffff:ffff:ffff:ffff:ffff:0", false);

378 379
    DO_TEST_MASK_NETWORK("2001:db8:ca2:2::1", 64, "2001:db8:ca2:2::");

380 381 382 383 384 385 386 387
    DO_TEST_WILDCARD("0.0.0.0", true);
    DO_TEST_WILDCARD("::", true);
    DO_TEST_WILDCARD("0", true);
    DO_TEST_WILDCARD("0.0", true);
    DO_TEST_WILDCARD("0.0.0", true);
    DO_TEST_WILDCARD("1", false);
    DO_TEST_WILDCARD("0.1", false);

388 389 390 391 392 393
    DO_TEST_IS_NUMERIC("0.0.0.0", true);
    DO_TEST_IS_NUMERIC("::", true);
    DO_TEST_IS_NUMERIC("1", true);
    DO_TEST_IS_NUMERIC("::ffff", true);
    DO_TEST_IS_NUMERIC("examplehost", false);

394
    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
395 396 397
}

VIRT_TEST_MAIN(mymain)