backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / tests / virportallocatortest.c
blob211de705a4767a3b19eececf9c43359418b246a1
1 /*
2 * Copyright (C) 2013-2014 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
19 #include <config.h>
20 #include "virfile.h"
21 #include "testutils.h"
23 #if HAVE_DLFCN_H
24 # include <dlfcn.h>
25 #endif
27 #if defined(__linux__) && defined(RTLD_NEXT)
29 # include "virutil.h"
30 # include "virerror.h"
31 # include "viralloc.h"
32 # include "virlog.h"
33 # include "virportallocator.h"
34 # include "virstring.h"
36 # define VIR_FROM_THIS VIR_FROM_RPC
38 VIR_LOG_INIT("tests.portallocatortest");
40 static int testAllocAll(const void *args ATTRIBUTE_UNUSED)
42 virPortAllocatorRangePtr ports = virPortAllocatorRangeNew("test", 5900, 5909);
43 int ret = -1;
44 unsigned short p1 = 0, p2 = 0, p3 = 0, p4 = 0, p5 = 0, p6 = 0, p7 = 0;
46 if (!ports)
47 return -1;
49 if (virPortAllocatorAcquire(ports, &p1) < 0)
50 goto cleanup;
51 if (p1 != 5901) {
52 VIR_TEST_DEBUG("Expected 5901, got %d", p1);
53 goto cleanup;
56 if (virPortAllocatorAcquire(ports, &p2) < 0)
57 goto cleanup;
58 if (p2 != 5902) {
59 VIR_TEST_DEBUG("Expected 5902, got %d", p2);
60 goto cleanup;
63 if (virPortAllocatorAcquire(ports, &p3) < 0)
64 goto cleanup;
65 if (p3 != 5903) {
66 VIR_TEST_DEBUG("Expected 5903, got %d", p3);
67 goto cleanup;
70 if (virPortAllocatorAcquire(ports, &p4) < 0)
71 goto cleanup;
72 if (p4 != 5907) {
73 VIR_TEST_DEBUG("Expected 5907, got %d", p4);
74 goto cleanup;
77 if (virPortAllocatorAcquire(ports, &p5) < 0)
78 goto cleanup;
79 if (p5 != 5908) {
80 VIR_TEST_DEBUG("Expected 5908, got %d", p5);
81 goto cleanup;
84 if (virPortAllocatorAcquire(ports, &p6) < 0)
85 goto cleanup;
86 if (p6 != 5909) {
87 VIR_TEST_DEBUG("Expected 5909, got %d", p6);
88 goto cleanup;
91 if (virPortAllocatorAcquire(ports, &p7) == 0) {
92 VIR_TEST_DEBUG("Expected error, got %d", p7);
93 goto cleanup;
96 ret = 0;
97 cleanup:
98 virPortAllocatorRelease(p1);
99 virPortAllocatorRelease(p2);
100 virPortAllocatorRelease(p3);
101 virPortAllocatorRelease(p4);
102 virPortAllocatorRelease(p5);
103 virPortAllocatorRelease(p6);
104 virPortAllocatorRelease(p7);
106 virPortAllocatorRangeFree(ports);
107 return ret;
112 static int testAllocReuse(const void *args ATTRIBUTE_UNUSED)
114 virPortAllocatorRangePtr ports = virPortAllocatorRangeNew("test", 5900, 5910);
115 int ret = -1;
116 unsigned short p1 = 0, p2 = 0, p3 = 0, p4 = 0;
118 if (!ports)
119 return -1;
121 if (virPortAllocatorAcquire(ports, &p1) < 0)
122 goto cleanup;
123 if (p1 != 5901) {
124 VIR_TEST_DEBUG("Expected 5901, got %d", p1);
125 goto cleanup;
128 if (virPortAllocatorAcquire(ports, &p2) < 0)
129 goto cleanup;
130 if (p2 != 5902) {
131 VIR_TEST_DEBUG("Expected 5902, got %d", p2);
132 goto cleanup;
135 if (virPortAllocatorAcquire(ports, &p3) < 0)
136 goto cleanup;
137 if (p3 != 5903) {
138 VIR_TEST_DEBUG("Expected 5903, got %d", p3);
139 goto cleanup;
142 if (virPortAllocatorRelease(p2) < 0)
143 goto cleanup;
145 if (virPortAllocatorAcquire(ports, &p4) < 0)
146 goto cleanup;
147 if (p4 != 5902) {
148 VIR_TEST_DEBUG("Expected 5902, got %d", p4);
149 goto cleanup;
152 ret = 0;
153 cleanup:
154 virPortAllocatorRelease(p1);
155 virPortAllocatorRelease(p3);
156 virPortAllocatorRelease(p4);
158 virPortAllocatorRangeFree(ports);
159 return ret;
163 static int
164 mymain(void)
166 int ret = 0;
168 if (virTestRun("Test alloc all", testAllocAll, NULL) < 0)
169 ret = -1;
171 if (virTestRun("Test alloc reuse", testAllocReuse, NULL) < 0)
172 ret = -1;
174 setenv("LIBVIRT_TEST_IPV4ONLY", "really", 1);
176 if (virTestRun("Test IPv4-only alloc all", testAllocAll, NULL) < 0)
177 ret = -1;
179 if (virTestRun("Test IPv4-only alloc reuse", testAllocReuse, NULL) < 0)
180 ret = -1;
182 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
185 VIR_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/virportallocatormock.so")
186 #else /* defined(__linux__) && defined(RTLD_NEXT) */
188 main(void)
190 return EXIT_AM_SKIP;
192 #endif