File: virportallocatortest.c

package info (click to toggle)
libvirt 1.2.9-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 173,536 kB
  • sloc: ansic: 463,129; xml: 68,283; sh: 16,393; makefile: 4,588; python: 3,705; perl: 3,675; ml: 470; sed: 16
file content (277 lines) | stat: -rw-r--r-- 6,561 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
128
129
130
131
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * Copyright (C) 2013-2014 Red Hat, Inc.
 *
 * 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>
#include <stdlib.h>
#include "virfile.h"
#include "testutils.h"

#if HAVE_DLFCN_H
# include <dlfcn.h>
#endif

#if defined(RTLD_NEXT)
# ifdef MOCK_HELPER
#  include "internal.h"
#  include <sys/socket.h>
#  include <errno.h>
#  include <arpa/inet.h>
#  include <netinet/in.h>
#  include <stdio.h>

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);
}

int bind(int sockfd ATTRIBUTE_UNUSED,
         const struct sockaddr *addr,
         socklen_t addrlen ATTRIBUTE_UNUSED)
{
    struct sockaddr_in saddr;

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

    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;
    }

    if (saddr.sin_port == htons(5900) ||
        saddr.sin_port == htons(5904) ||
        saddr.sin_port == htons(5905) ||
        saddr.sin_port == htons(5906)) {
        errno = EADDRINUSE;
        return -1;
    }

    return 0;
}

# else

#  include "virutil.h"
#  include "virerror.h"
#  include "viralloc.h"
#  include "virlog.h"
#  include "virportallocator.h"
#  include "virstring.h"

#  define VIR_FROM_THIS VIR_FROM_RPC

VIR_LOG_INIT("tests.portallocatortest");

static int testAllocAll(const void *args ATTRIBUTE_UNUSED)
{
    virPortAllocatorPtr alloc = virPortAllocatorNew("test", 5900, 5909, 0);
    int ret = -1;
    unsigned short p1, p2, p3, p4, p5, p6, p7;

    if (!alloc)
        return -1;

    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;
    }

    if (virPortAllocatorAcquire(alloc, &p7) == 0) {
        if (virTestGetDebug())
            fprintf(stderr, "Expected error, got %d", p7);
        goto cleanup;
    }

    ret = 0;
 cleanup:
    virObjectUnref(alloc);
    return ret;
}



static int testAllocReuse(const void *args ATTRIBUTE_UNUSED)
{
    virPortAllocatorPtr alloc = virPortAllocatorNew("test", 5900, 5910, 0);
    int ret = -1;
    unsigned short p1, p2, p3, p4;

    if (!alloc)
        return -1;

    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;
 cleanup:
    virObjectUnref(alloc);
    return ret;
}


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

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

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

    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;

    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

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

#else /* ! defined(RTLD_NEXT) */
int
main(void)
{
    return EXIT_AM_SKIP;
}
#endif