virportallocatortest.c 6.4 KB
Newer Older
D
Daniel P. Berrange 已提交
1
/*
J
Ján Tomko 已提交
2
 * Copyright (C) 2013-2014 Red Hat, Inc.
D
Daniel P. Berrange 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>
J
Ján Tomko 已提交
22 23
#include <stdlib.h>
#include "virfile.h"
24
#include "testutils.h"
D
Daniel P. Berrange 已提交
25

26 27 28
#if HAVE_DLFCN_H
# include <dlfcn.h>
#endif
29

30
#if defined(RTLD_NEXT)
31 32 33 34 35 36 37
# ifdef MOCK_HELPER
#  include "internal.h"
#  include <sys/socket.h>
#  include <errno.h>
#  include <arpa/inet.h>
#  include <netinet/in.h>
#  include <stdio.h>
J
Ján Tomko 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 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

static bool host_has_ipv6 = false;
static int (*realsocket)(int domain, int type, int protocol);

static void init_syms(void)
{
    int fd;

    if (realsocket)
        return;

    realsocket = dlsym(RTLD_NEXT, "socket");

    if (!realsocket) {
        fprintf(stderr, "Unable to find 'socket' symbol\n");
        abort();
    }

    fd = realsocket(AF_INET6, SOCK_STREAM, 0);
    if (fd < 0)
        return;

    host_has_ipv6 = true;
    VIR_FORCE_CLOSE(fd);
}

int socket(int domain,
           int type,
           int protocol)
{
    init_syms();

    if (getenv("LIBVIRT_TEST_IPV4ONLY") && domain == AF_INET6) {
        errno = EAFNOSUPPORT;
        return -1;
    }

    return realsocket(domain, type, protocol);
}
D
Daniel P. Berrange 已提交
77 78 79 80 81

int bind(int sockfd ATTRIBUTE_UNUSED,
         const struct sockaddr *addr,
         socklen_t addrlen ATTRIBUTE_UNUSED)
{
82
    struct sockaddr_in saddr;
D
Daniel P. Berrange 已提交
83

84 85
    memcpy(&saddr, addr, sizeof(saddr));

J
Ján Tomko 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98
    if (host_has_ipv6 && !getenv("LIBVIRT_TEST_IPV4ONLY")) {
        if (saddr.sin_port == htons(5900) ||
            (saddr.sin_family == AF_INET &&
             saddr.sin_port == htons(5904)) ||
            (saddr.sin_family == AF_INET6 &&
             (saddr.sin_port == htons(5905) ||
              saddr.sin_port == htons(5906)))) {
            errno = EADDRINUSE;
            return -1;
        }
        return 0;
    }

99 100 101 102
    if (saddr.sin_port == htons(5900) ||
        saddr.sin_port == htons(5904) ||
        saddr.sin_port == htons(5905) ||
        saddr.sin_port == htons(5906)) {
D
Daniel P. Berrange 已提交
103 104 105 106 107 108 109
        errno = EADDRINUSE;
        return -1;
    }

    return 0;
}

110
# else
D
Daniel P. Berrange 已提交
111

112 113 114 115 116 117
#  include "virutil.h"
#  include "virerror.h"
#  include "viralloc.h"
#  include "virlog.h"
#  include "virportallocator.h"
#  include "virstring.h"
D
Daniel P. Berrange 已提交
118

119
#  define VIR_FROM_THIS VIR_FROM_RPC
D
Daniel P. Berrange 已提交
120

121
VIR_LOG_INIT("tests.portallocatortest");
D
Daniel P. Berrange 已提交
122 123 124

static int testAllocAll(const void *args ATTRIBUTE_UNUSED)
{
125
    virPortAllocatorPtr alloc = virPortAllocatorNew("test", 5900, 5909, 0);
D
Daniel P. Berrange 已提交
126 127 128
    int ret = -1;
    unsigned short p1, p2, p3, p4, p5, p6, p7;

129 130 131
    if (!alloc)
        return -1;

D
Daniel P. Berrange 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    if (virPortAllocatorAcquire(alloc, &p1) < 0)
        goto cleanup;
    if (p1 != 5901) {
        if (virTestGetDebug())
            fprintf(stderr, "Expected 5901, got %d", p1);
        goto cleanup;
    }

    if (virPortAllocatorAcquire(alloc, &p2) < 0)
        goto cleanup;
    if (p2 != 5902) {
        if (virTestGetDebug())
            fprintf(stderr, "Expected 5902, got %d", p2);
        goto cleanup;
    }

    if (virPortAllocatorAcquire(alloc, &p3) < 0)
        goto cleanup;
    if (p3 != 5903) {
        if (virTestGetDebug())
            fprintf(stderr, "Expected 5903, got %d", p3);
        goto cleanup;
    }

    if (virPortAllocatorAcquire(alloc, &p4) < 0)
        goto cleanup;
    if (p4 != 5907) {
        if (virTestGetDebug())
            fprintf(stderr, "Expected 5907, got %d", p4);
        goto cleanup;
    }

    if (virPortAllocatorAcquire(alloc, &p5) < 0)
        goto cleanup;
    if (p5 != 5908) {
        if (virTestGetDebug())
            fprintf(stderr, "Expected 5908, got %d", p5);
        goto cleanup;
    }

    if (virPortAllocatorAcquire(alloc, &p6) < 0)
        goto cleanup;
    if (p6 != 5909) {
        if (virTestGetDebug())
            fprintf(stderr, "Expected 5909, got %d", p6);
        goto cleanup;
    }

180
    if (virPortAllocatorAcquire(alloc, &p7) == 0) {
D
Daniel P. Berrange 已提交
181
        if (virTestGetDebug())
182
            fprintf(stderr, "Expected error, got %d", p7);
D
Daniel P. Berrange 已提交
183 184 185 186
        goto cleanup;
    }

    ret = 0;
187
 cleanup:
D
Daniel P. Berrange 已提交
188 189 190 191 192 193 194 195
    virObjectUnref(alloc);
    return ret;
}



static int testAllocReuse(const void *args ATTRIBUTE_UNUSED)
{
196
    virPortAllocatorPtr alloc = virPortAllocatorNew("test", 5900, 5910, 0);
D
Daniel P. Berrange 已提交
197 198 199
    int ret = -1;
    unsigned short p1, p2, p3, p4;

200 201 202
    if (!alloc)
        return -1;

D
Daniel P. Berrange 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    if (virPortAllocatorAcquire(alloc, &p1) < 0)
        goto cleanup;
    if (p1 != 5901) {
        if (virTestGetDebug())
            fprintf(stderr, "Expected 5901, got %d", p1);
        goto cleanup;
    }

    if (virPortAllocatorAcquire(alloc, &p2) < 0)
        goto cleanup;
    if (p2 != 5902) {
        if (virTestGetDebug())
            fprintf(stderr, "Expected 5902, got %d", p2);
        goto cleanup;
    }

    if (virPortAllocatorAcquire(alloc, &p3) < 0)
        goto cleanup;
    if (p3 != 5903) {
        if (virTestGetDebug())
            fprintf(stderr, "Expected 5903, got %d", p3);
        goto cleanup;
    }


    if (virPortAllocatorRelease(alloc, p2) < 0)
        goto cleanup;

    if (virPortAllocatorAcquire(alloc, &p4) < 0)
        goto cleanup;
    if (p4 != 5902) {
        if (virTestGetDebug())
            fprintf(stderr, "Expected 5902, got %d", p4);
        goto cleanup;
    }

    ret = 0;
240
 cleanup:
D
Daniel P. Berrange 已提交
241 242 243 244 245 246 247 248 249 250
    virObjectUnref(alloc);
    return ret;
}


static int
mymain(void)
{
    int ret = 0;

251
    if (virtTestRun("Test alloc all", testAllocAll, NULL) < 0)
D
Daniel P. Berrange 已提交
252 253
        ret = -1;

254
    if (virtTestRun("Test alloc reuse", testAllocReuse, NULL) < 0)
D
Daniel P. Berrange 已提交
255 256
        ret = -1;

J
Ján Tomko 已提交
257 258 259 260 261 262 263 264
    setenv("LIBVIRT_TEST_IPV4ONLY", "really", 1);

    if (virtTestRun("Test IPv4-only alloc all", testAllocAll, NULL) < 0)
        ret = -1;

    if (virtTestRun("Test IPv4-only alloc reuse", testAllocReuse, NULL) < 0)
        ret = -1;

265
    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
D
Daniel P. Berrange 已提交
266 267 268
}

VIRT_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/libvirportallocatormock.so")
269 270
# endif

271
#else /* ! defined(RTLD_NEXT) */
272 273 274 275 276
int
main(void)
{
    return EXIT_AM_SKIP;
}
D
Daniel P. Berrange 已提交
277
#endif