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/>.
21 #include "testutils.h"
27 #if defined(__linux__) && defined(RTLD_NEXT)
30 # include "virerror.h"
31 # include "viralloc.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);
44 unsigned short p1
= 0, p2
= 0, p3
= 0, p4
= 0, p5
= 0, p6
= 0, p7
= 0;
49 if (virPortAllocatorAcquire(ports
, &p1
) < 0)
52 VIR_TEST_DEBUG("Expected 5901, got %d", p1
);
56 if (virPortAllocatorAcquire(ports
, &p2
) < 0)
59 VIR_TEST_DEBUG("Expected 5902, got %d", p2
);
63 if (virPortAllocatorAcquire(ports
, &p3
) < 0)
66 VIR_TEST_DEBUG("Expected 5903, got %d", p3
);
70 if (virPortAllocatorAcquire(ports
, &p4
) < 0)
73 VIR_TEST_DEBUG("Expected 5907, got %d", p4
);
77 if (virPortAllocatorAcquire(ports
, &p5
) < 0)
80 VIR_TEST_DEBUG("Expected 5908, got %d", p5
);
84 if (virPortAllocatorAcquire(ports
, &p6
) < 0)
87 VIR_TEST_DEBUG("Expected 5909, got %d", p6
);
91 if (virPortAllocatorAcquire(ports
, &p7
) == 0) {
92 VIR_TEST_DEBUG("Expected error, got %d", p7
);
98 virPortAllocatorRelease(p1
);
99 virPortAllocatorRelease(p2
);
100 virPortAllocatorRelease(p3
);
101 virPortAllocatorRelease(p4
);
102 virPortAllocatorRelease(p5
);
103 virPortAllocatorRelease(p6
);
104 virPortAllocatorRelease(p7
);
106 virPortAllocatorRangeFree(ports
);
112 static int testAllocReuse(const void *args ATTRIBUTE_UNUSED
)
114 virPortAllocatorRangePtr ports
= virPortAllocatorRangeNew("test", 5900, 5910);
116 unsigned short p1
= 0, p2
= 0, p3
= 0, p4
= 0;
121 if (virPortAllocatorAcquire(ports
, &p1
) < 0)
124 VIR_TEST_DEBUG("Expected 5901, got %d", p1
);
128 if (virPortAllocatorAcquire(ports
, &p2
) < 0)
131 VIR_TEST_DEBUG("Expected 5902, got %d", p2
);
135 if (virPortAllocatorAcquire(ports
, &p3
) < 0)
138 VIR_TEST_DEBUG("Expected 5903, got %d", p3
);
142 if (virPortAllocatorRelease(p2
) < 0)
145 if (virPortAllocatorAcquire(ports
, &p4
) < 0)
148 VIR_TEST_DEBUG("Expected 5902, got %d", p4
);
154 virPortAllocatorRelease(p1
);
155 virPortAllocatorRelease(p3
);
156 virPortAllocatorRelease(p4
);
158 virPortAllocatorRangeFree(ports
);
168 if (virTestRun("Test alloc all", testAllocAll
, NULL
) < 0)
171 if (virTestRun("Test alloc reuse", testAllocReuse
, NULL
) < 0)
174 setenv("LIBVIRT_TEST_IPV4ONLY", "really", 1);
176 if (virTestRun("Test IPv4-only alloc all", testAllocAll
, NULL
) < 0)
179 if (virTestRun("Test IPv4-only alloc reuse", testAllocReuse
, NULL
) < 0)
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) */