qemu: Validate spapr-vio addresses
[libvirt/ericb.git] / tests / virmacmaptest.c
blob884dabab82b222b404dcc4d6ea7e6c21348d0afe
1 /*
2 * Copyright (C) 2016 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>
21 #include "testutils.h"
22 #include "virmacmap.h"
24 #define VIR_FROM_THIS VIR_FROM_NONE
26 struct testData {
27 const char *file;
28 const char *domain;
29 const char * const * macs;
30 virMacMapPtr mgr;
34 static int
35 testMACLookup(const void *opaque)
37 const struct testData *data = opaque;
38 virMacMapPtr mgr = NULL;
39 const char * const * macs;
40 size_t i, j;
41 char *file = NULL;
42 int ret = -1;
44 if (virAsprintf(&file, "%s/virmacmaptestdata/%s.json",
45 abs_srcdir, data->file) < 0)
46 goto cleanup;
48 if (!(mgr = virMacMapNew(file)))
49 goto cleanup;
51 macs = virMacMapLookup(mgr, data->domain);
53 for (i = 0; macs && macs[i]; i++) {
54 for (j = 0; data->macs && data->macs[j]; j++) {
55 if (STREQ(macs[i], data->macs[j]))
56 break;
59 if (!data->macs || !data->macs[j]) {
60 fprintf(stderr,
61 "Unexpected %s in the returned list of MACs\n", macs[i]);
62 goto cleanup;
66 for (i = 0; data->macs && data->macs[i]; i++) {
67 for (j = 0; macs && macs[j]; j++) {
68 if (STREQ(data->macs[i], macs[j]))
69 break;
72 if (!macs || !macs[j]) {
73 fprintf(stderr,
74 "Expected %s in the returned list of MACs\n", data->macs[i]);
75 goto cleanup;
79 ret = 0;
80 cleanup:
81 VIR_FREE(file);
82 virObjectUnref(mgr);
83 return ret;
87 static int
88 testMACRemove(const void *opaque)
90 const struct testData *data = opaque;
91 virMacMapPtr mgr = NULL;
92 const char * const * macs;
93 size_t i;
94 char *file = NULL;
95 int ret = -1;
97 if (virAsprintf(&file, "%s/virmacmaptestdata/%s.json",
98 abs_srcdir, data->file) < 0)
99 goto cleanup;
101 if (!(mgr = virMacMapNew(file)))
102 goto cleanup;
104 for (i = 0; data->macs && data->macs[i]; i++) {
105 if (virMacMapRemove(mgr, data->domain, data->macs[i]) < 0) {
106 fprintf(stderr,
107 "Error when removing %s from the list of MACs\n", data->macs[i]);
108 goto cleanup;
112 if ((macs = virMacMapLookup(mgr, data->domain))) {
113 fprintf(stderr,
114 "Not removed all MACs for domain %s: %s\n", data->domain, macs[0]);
115 goto cleanup;
118 ret = 0;
119 cleanup:
120 VIR_FREE(file);
121 virObjectUnref(mgr);
122 return ret;
126 static int
127 testMACFlush(const void *opaque)
129 const struct testData *data = opaque;
130 char *file = NULL;
131 char *str = NULL;
132 int ret = -1;
134 if (virAsprintf(&file, "%s/virmacmaptestdata/%s.json",
135 abs_srcdir, data->file) < 0)
136 goto cleanup;
138 if (virMacMapDumpStr(data->mgr, &str) < 0)
139 goto cleanup;
141 if (virTestCompareToFile(str, file) < 0)
142 goto cleanup;
144 ret = 0;
145 cleanup:
146 VIR_FREE(file);
147 VIR_FREE(str);
148 return ret;
152 static int
153 mymain(void)
155 int ret = 0;
156 virMacMapPtr mgr = NULL;
158 #define DO_TEST_BASIC(f, d, ...) \
159 do { \
160 const char * const m[] = {__VA_ARGS__, NULL }; \
161 struct testData data = {.file = f, .domain = d, .macs = m}; \
162 if (virTestRun("Lookup " #d " in " #f, \
163 testMACLookup, &data) < 0) \
164 ret = -1; \
165 if (virTestRun("Remove " #d " in " #f, \
166 testMACRemove, &data) < 0) \
167 ret = -1; \
168 } while (0)
170 #define DO_TEST_FLUSH_PROLOGUE \
171 do { \
172 if (!(mgr = virMacMapNew(NULL))) { \
173 ret = -1; \
174 goto cleanup; \
176 } while (0)
178 #define DO_TEST_FLUSH(d, ...) \
179 do { \
180 const char * const m[] = {__VA_ARGS__, NULL }; \
181 size_t i; \
182 for (i = 0; m[i]; i++) { \
183 if (virMacMapAdd(mgr, d, m[i]) < 0) { \
184 virObjectUnref(mgr); \
185 mgr = NULL; \
186 ret = -1; \
189 } while (0)
192 #define DO_TEST_FLUSH_EPILOGUE(f) \
193 do { \
194 struct testData data = {.file = f, .mgr = mgr}; \
195 if (virTestRun("Flush " #f, testMACFlush, &data) < 0) \
196 ret = -1; \
197 virObjectUnref(mgr); \
198 mgr = NULL; \
199 } while (0)
201 DO_TEST_BASIC("empty", "none", NULL);
202 DO_TEST_BASIC("simple", "f24", "aa:bb:cc:dd:ee:ff");
203 DO_TEST_BASIC("simple2", "f24", "aa:bb:cc:dd:ee:ff", "a1:b2:c3:d4:e5:f6");
204 DO_TEST_BASIC("simple2", "f25", "00:11:22:33:44:55", "aa:bb:cc:00:11:22");
206 DO_TEST_FLUSH_PROLOGUE;
207 DO_TEST_FLUSH_EPILOGUE("empty");
209 DO_TEST_FLUSH_PROLOGUE;
210 DO_TEST_FLUSH("f24", "aa:bb:cc:dd:ee:ff");
211 DO_TEST_FLUSH_EPILOGUE("simple");
213 DO_TEST_FLUSH_PROLOGUE;
214 DO_TEST_FLUSH("f24", "aa:bb:cc:dd:ee:ff", "a1:b2:c3:d4:e5:f6");
215 DO_TEST_FLUSH("f25", "00:11:22:33:44:55", "aa:bb:cc:00:11:22");
216 DO_TEST_FLUSH_EPILOGUE("simple2");
218 DO_TEST_FLUSH_PROLOGUE;
219 DO_TEST_FLUSH("dom0", "e1:81:5d:f3:41:57", "76:0a:2a:a0:51:86", "01:c7:fc:01:c7:fc");
220 DO_TEST_FLUSH("dom0", "8e:82:53:60:32:4a", "14:7a:25:dc:7d:a0", "f8:d7:75:f8:d7:75");
221 DO_TEST_FLUSH("dom0", "73:d2:50:fb:0f:b1", "82:ee:a7:9b:e3:69", "a8:b4:cb:a8:b4:cb");
222 DO_TEST_FLUSH("dom0", "7e:81:86:0f:0b:fb", "94:e2:00:d9:4c:70", "dc:7b:83:dc:7b:83");
223 DO_TEST_FLUSH("dom0", "d1:19:a5:a1:52:a8", "22:03:a0:bf:cb:4a", "e3:c7:f8:e3:c7:f8");
224 DO_TEST_FLUSH("dom0", "aa:bf:3f:4f:21:8d", "28:67:45:72:8f:47", "eb:08:cd:eb:08:cd");
225 DO_TEST_FLUSH("dom0", "bd:f8:a7:e5:e2:bd", "c7:80:e3:b9:18:4d", "ce:da:c0:ce:da:c0");
226 DO_TEST_FLUSH("dom1", "8b:51:1d:9f:2f:29", "7c:ae:4c:3e:e1:11", "c6:68:4e:98:ff:6a");
227 DO_TEST_FLUSH("dom1", "43:0e:33:a1:3f:0f", "7a:3e:ed:bb:15:27", "b1:17:fd:95:d2:1b");
228 DO_TEST_FLUSH("dom1", "9e:89:49:99:51:0e", "89:b4:3f:08:88:2c", "54:0b:4c:e2:0a:39");
229 DO_TEST_FLUSH("dom1", "bb:88:07:19:51:9d", "b7:f1:1a:40:a2:95", "88:94:39:a3:90:b4");
230 DO_TEST_FLUSH_EPILOGUE("complex");
231 cleanup:
232 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
235 VIR_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/virdeterministichashmock.so")