virportallocatortest.c 4.6 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
 *
 * 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/>.
 */

#include <config.h>
J
Ján Tomko 已提交
20
#include "virfile.h"
21
#include "testutils.h"
D
Daniel P. Berrange 已提交
22

23 24 25
#if HAVE_DLFCN_H
# include <dlfcn.h>
#endif
26

27 28
#if defined(__linux__) && defined(RTLD_NEXT)

29 30 31 32 33 34
# include "virutil.h"
# include "virerror.h"
# include "viralloc.h"
# include "virlog.h"
# include "virportallocator.h"
# include "virstring.h"
J
Ján Tomko 已提交
35

36
# define VIR_FROM_THIS VIR_FROM_RPC
D
Daniel P. Berrange 已提交
37

38
VIR_LOG_INIT("tests.portallocatortest");
D
Daniel P. Berrange 已提交
39 40 41

static int testAllocAll(const void *args ATTRIBUTE_UNUSED)
{
42
    virPortAllocatorRangePtr ports = virPortAllocatorRangeNew("test", 5900, 5909);
D
Daniel P. Berrange 已提交
43
    int ret = -1;
44
    unsigned short p1 = 0, p2 = 0, p3 = 0, p4 = 0, p5 = 0, p6 = 0, p7 = 0;
D
Daniel P. Berrange 已提交
45

46
    if (!ports)
47 48
        return -1;

49
    if (virPortAllocatorAcquire(ports, &p1) < 0)
D
Daniel P. Berrange 已提交
50 51
        goto cleanup;
    if (p1 != 5901) {
52
        VIR_TEST_DEBUG("Expected 5901, got %d", p1);
D
Daniel P. Berrange 已提交
53 54 55
        goto cleanup;
    }

56
    if (virPortAllocatorAcquire(ports, &p2) < 0)
D
Daniel P. Berrange 已提交
57 58
        goto cleanup;
    if (p2 != 5902) {
59
        VIR_TEST_DEBUG("Expected 5902, got %d", p2);
D
Daniel P. Berrange 已提交
60 61 62
        goto cleanup;
    }

63
    if (virPortAllocatorAcquire(ports, &p3) < 0)
D
Daniel P. Berrange 已提交
64 65
        goto cleanup;
    if (p3 != 5903) {
66
        VIR_TEST_DEBUG("Expected 5903, got %d", p3);
D
Daniel P. Berrange 已提交
67 68 69
        goto cleanup;
    }

70
    if (virPortAllocatorAcquire(ports, &p4) < 0)
D
Daniel P. Berrange 已提交
71 72
        goto cleanup;
    if (p4 != 5907) {
73
        VIR_TEST_DEBUG("Expected 5907, got %d", p4);
D
Daniel P. Berrange 已提交
74 75 76
        goto cleanup;
    }

77
    if (virPortAllocatorAcquire(ports, &p5) < 0)
D
Daniel P. Berrange 已提交
78 79
        goto cleanup;
    if (p5 != 5908) {
80
        VIR_TEST_DEBUG("Expected 5908, got %d", p5);
D
Daniel P. Berrange 已提交
81 82 83
        goto cleanup;
    }

84
    if (virPortAllocatorAcquire(ports, &p6) < 0)
D
Daniel P. Berrange 已提交
85 86
        goto cleanup;
    if (p6 != 5909) {
87
        VIR_TEST_DEBUG("Expected 5909, got %d", p6);
D
Daniel P. Berrange 已提交
88 89 90
        goto cleanup;
    }

91
    if (virPortAllocatorAcquire(ports, &p7) == 0) {
92
        VIR_TEST_DEBUG("Expected error, got %d", p7);
D
Daniel P. Berrange 已提交
93 94 95 96
        goto cleanup;
    }

    ret = 0;
97
 cleanup:
98 99 100 101 102 103 104
    virPortAllocatorRelease(p1);
    virPortAllocatorRelease(p2);
    virPortAllocatorRelease(p3);
    virPortAllocatorRelease(p4);
    virPortAllocatorRelease(p5);
    virPortAllocatorRelease(p6);
    virPortAllocatorRelease(p7);
105 106

    virPortAllocatorRangeFree(ports);
D
Daniel P. Berrange 已提交
107 108 109 110 111 112 113
    return ret;
}



static int testAllocReuse(const void *args ATTRIBUTE_UNUSED)
{
114
    virPortAllocatorRangePtr ports = virPortAllocatorRangeNew("test", 5900, 5910);
D
Daniel P. Berrange 已提交
115
    int ret = -1;
116
    unsigned short p1 = 0, p2 = 0, p3 = 0, p4 = 0;
D
Daniel P. Berrange 已提交
117

118
    if (!ports)
119 120
        return -1;

121
    if (virPortAllocatorAcquire(ports, &p1) < 0)
D
Daniel P. Berrange 已提交
122 123
        goto cleanup;
    if (p1 != 5901) {
124
        VIR_TEST_DEBUG("Expected 5901, got %d", p1);
D
Daniel P. Berrange 已提交
125 126 127
        goto cleanup;
    }

128
    if (virPortAllocatorAcquire(ports, &p2) < 0)
D
Daniel P. Berrange 已提交
129 130
        goto cleanup;
    if (p2 != 5902) {
131
        VIR_TEST_DEBUG("Expected 5902, got %d", p2);
D
Daniel P. Berrange 已提交
132 133 134
        goto cleanup;
    }

135
    if (virPortAllocatorAcquire(ports, &p3) < 0)
D
Daniel P. Berrange 已提交
136 137
        goto cleanup;
    if (p3 != 5903) {
138
        VIR_TEST_DEBUG("Expected 5903, got %d", p3);
D
Daniel P. Berrange 已提交
139 140 141
        goto cleanup;
    }

142
    if (virPortAllocatorRelease(p2) < 0)
D
Daniel P. Berrange 已提交
143 144
        goto cleanup;

145
    if (virPortAllocatorAcquire(ports, &p4) < 0)
D
Daniel P. Berrange 已提交
146 147
        goto cleanup;
    if (p4 != 5902) {
148
        VIR_TEST_DEBUG("Expected 5902, got %d", p4);
D
Daniel P. Berrange 已提交
149 150 151 152
        goto cleanup;
    }

    ret = 0;
153
 cleanup:
154 155 156
    virPortAllocatorRelease(p1);
    virPortAllocatorRelease(p3);
    virPortAllocatorRelease(p4);
157 158

    virPortAllocatorRangeFree(ports);
D
Daniel P. Berrange 已提交
159 160 161 162 163 164 165 166 167
    return ret;
}


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

168
    if (virTestRun("Test alloc all", testAllocAll, NULL) < 0)
D
Daniel P. Berrange 已提交
169 170
        ret = -1;

171
    if (virTestRun("Test alloc reuse", testAllocReuse, NULL) < 0)
D
Daniel P. Berrange 已提交
172 173
        ret = -1;

J
Ján Tomko 已提交
174 175
    setenv("LIBVIRT_TEST_IPV4ONLY", "really", 1);

176
    if (virTestRun("Test IPv4-only alloc all", testAllocAll, NULL) < 0)
J
Ján Tomko 已提交
177 178
        ret = -1;

179
    if (virTestRun("Test IPv4-only alloc reuse", testAllocReuse, NULL) < 0)
J
Ján Tomko 已提交
180 181
        ret = -1;

182
    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
D
Daniel P. Berrange 已提交
183 184
}

185
VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("virportallocator"))
186
#else /* defined(__linux__) && defined(RTLD_NEXT) */
187 188 189 190 191
int
main(void)
{
    return EXIT_AM_SKIP;
}
D
Daniel P. Berrange 已提交
192
#endif