backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / tests / vshtabletest.c
blobb07db3cf2339cd0c6699ec7743a0b2169205a52b
1 /*
2 * Copyright (C) 2018 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 <locale.h>
22 #include <wctype.h>
24 #include "internal.h"
25 #include "testutils.h"
26 #include "viralloc.h"
27 #include "../tools/vsh-table.h"
29 static int
30 testVshTableNew(const void *opaque ATTRIBUTE_UNUSED)
32 if (vshTableNew(NULL)) {
33 fprintf(stderr, "expected failure when passing null to vshTableNew\n");
34 return -1;
37 return 0;
40 static int
41 testVshTableHeader(const void *opaque ATTRIBUTE_UNUSED)
43 int ret = 0;
44 char *act = NULL;
45 const char *exp =
46 " 1 fedora28 running\n"
47 " 2 rhel7.5 running\n";
48 const char *exp2 =
49 " Id Name State\n"
50 "--------------------------\n"
51 " 1 fedora28 running\n"
52 " 2 rhel7.5 running\n";
54 vshTablePtr table = vshTableNew("Id", "Name", "State",
55 NULL); //to ask about return
56 if (!table)
57 goto cleanup;
59 vshTableRowAppend(table, "1", "fedora28", "running", NULL);
60 vshTableRowAppend(table, "2", "rhel7.5", "running",
61 NULL);
63 act = vshTablePrintToString(table, false);
64 if (virTestCompareToString(exp, act) < 0)
65 ret = -1;
67 VIR_FREE(act);
68 act = vshTablePrintToString(table, true);
69 if (virTestCompareToString(exp2, act) < 0)
70 ret = -1;
72 cleanup:
73 VIR_FREE(act);
74 vshTableFree(table);
75 return ret;
78 static int
79 testVshTableRowAppend(const void *opaque ATTRIBUTE_UNUSED)
81 int ret = 0;
83 vshTablePtr table = vshTableNew("Id", "Name", NULL);
84 if (!table)
85 goto cleanup;
87 if (vshTableRowAppend(table, NULL) >= 0) {
88 fprintf(stderr, "Appending NULL shouldn't work\n");
89 ret = -1;
92 if (vshTableRowAppend(table, "2", NULL) >= 0) {
93 fprintf(stderr, "Appending less items than in header\n");
94 ret = -1;
97 if (vshTableRowAppend(table, "2", "rhel7.5", "running",
98 NULL) >= 0) {
99 fprintf(stderr, "Appending more items than in header\n");
100 ret = -1;
103 if (vshTableRowAppend(table, "2", "rhel7.5", NULL) < 0) {
104 fprintf(stderr, "Appending same number of items as in header"
105 " should not return NULL\n");
106 ret = -1;
109 cleanup:
110 vshTableFree(table);
111 return ret;
114 static int
115 testUnicode(const void *opaque ATTRIBUTE_UNUSED)
117 int ret = 0;
118 char *act = NULL;
120 const char *exp =
121 " Id 名稱 государство\n"
122 "-----------------------------------------\n"
123 " 1 fedora28 running\n"
124 " 2 つへソrhel7.5つへソ running\n";
125 vshTablePtr table;
127 table = vshTableNew("Id", "名稱", "государство", NULL);
128 if (!table)
129 goto cleanup;
131 vshTableRowAppend(table, "1", "fedora28", "running", NULL);
132 vshTableRowAppend(table, "2", "つへソrhel7.5つへソ", "running",
133 NULL);
135 act = vshTablePrintToString(table, true);
136 if (virTestCompareToString(exp, act) < 0)
137 ret = -1;
139 cleanup:
140 VIR_FREE(act);
141 vshTableFree(table);
142 return ret;
145 /* Point of this test is to see how table behaves with right to left writing*/
146 static int
147 testUnicodeArabic(const void *opaque ATTRIBUTE_UNUSED)
149 int ret = 0;
150 char *act = NULL;
152 const char *exp =
153 " ﻡﺍ ﻢﻣﺍ ﻕﺎﺌﻣﺓ ﺓ ﺎﻠﺼﻋ ﺍﻸﺜﻧﺎﻧ\n"
154 "-------------------------------------------------------------------------------------------\n"
155 " 1 ﻉﺪﻴﻟ ﺎﻠﺜﻘﻴﻟ ﻕﺎﻣ ﻊﻧ, ٣٠ ﻎﻴﻨﻳﺍ ﻮﺘﻧﺎﻤﺗ ﺎﻠﺛﺎﻠﺛ، ﺄﺳﺭ, ﺩﻮﻟ ﺩﻮﻟ. ﺄﻣﺎﻣ ﺍ ﺎﻧ ﻲﻜﻧ\n"
156 " ﺺﻔﺣﺓ ﺖﻜﺘﻴﻛﺍً ﻊﻟ, ﺎﻠﺠﻧﻭﺩ ﻭﺎﻠﻌﺗﺍﺩ ﺵﺭ\n";
157 vshTablePtr table;
158 wchar_t wc;
160 /* If this char is not classed as printable, the actual
161 * output won't match what this test expects. The code
162 * is still operating correctly, but we have different
163 * layout */
164 mbrtowc(&wc, "،", MB_CUR_MAX, NULL);
165 if (!iswprint(wc))
166 return EXIT_AM_SKIP;
168 table = vshTableNew("ﻡﺍ ﻢﻣﺍ ﻕﺎﺌﻣﺓ", "ﺓ ﺎﻠﺼﻋ", "ﺍﻸﺜﻧﺎﻧ", NULL);
169 if (!table)
170 goto cleanup;
171 vshTableRowAppend(table,
172 "1",
173 "ﻉﺪﻴﻟ ﺎﻠﺜﻘﻴﻟ ﻕﺎﻣ ﻊﻧ, ٣٠ ﻎﻴﻨﻳﺍ ﻮﺘﻧﺎﻤﺗ ﺎﻠﺛﺎﻠﺛ، ﺄﺳﺭ, ﺩﻮﻟ",
174 "ﺩﻮﻟ. ﺄﻣﺎﻣ ﺍ ﺎﻧ ﻲﻜﻧ",
175 NULL);
176 vshTableRowAppend(table, "ﺺﻔﺣﺓ", "ﺖﻜﺘﻴﻛﺍً ﻊﻟ, ﺎﻠﺠﻧﻭﺩ ﻭﺎﻠﻌﺗﺍﺩ", "ﺵﺭ",
177 NULL);
178 act = vshTablePrintToString(table, true);
179 if (virTestCompareToString(exp, act) < 0)
180 ret = -1;
182 cleanup:
183 VIR_FREE(act);
184 vshTableFree(table);
185 return ret;
188 /* Testing zero-width characters by inserting few zero-width spaces */
189 static int
190 testUnicodeZeroWidthChar(const void *opaque ATTRIBUTE_UNUSED)
192 int ret = 0;
193 vshTablePtr table = NULL;
194 const char *exp =
195 " I\u200Bd Name \u200BStatus\n"
196 "--------------------------\n"
197 " 1\u200B fedora28 run\u200Bning\n"
198 " 2 rhel7.5 running\n";
199 char *act = NULL;
200 wchar_t wc;
202 /* If this char is not classed as printable, the actual
203 * output won't match what this test expects. The code
204 * is still operating correctly, but we have different
205 * layout */
206 mbrtowc(&wc, "\u200B", MB_CUR_MAX, NULL);
207 if (!iswprint(wc))
208 return EXIT_AM_SKIP;
210 table = vshTableNew("I\u200Bd", "Name", "\u200BStatus", NULL);
211 if (!table)
212 goto cleanup;
213 vshTableRowAppend(table, "1\u200B", "fedora28", "run\u200Bning", NULL);
214 vshTableRowAppend(table, "2", "rhel7.5", "running", NULL);
215 act = vshTablePrintToString(table, true);
217 if (virTestCompareToString(exp, act) < 0)
218 ret = -1;
220 cleanup:
221 VIR_FREE(act);
222 vshTableFree(table);
223 return ret;
226 static int
227 testUnicodeCombiningChar(const void *opaque ATTRIBUTE_UNUSED)
229 int ret = 0;
230 vshTablePtr table = NULL;
231 const char *exp =
232 " Id Náme Ⓢtatus\n"
233 "--------------------------\n"
234 " 1 fědora28 running\n"
235 " 2 rhel running\n";
236 char *act = NULL;
238 table = vshTableNew("Id", "Náme", "Ⓢtatus", NULL);
239 if (!table)
240 goto cleanup;
241 vshTableRowAppend(table, "1", "fědora28", "running", NULL);
242 vshTableRowAppend(table, "2", "rhel", "running", NULL);
243 act = vshTablePrintToString(table, true);
245 if (virTestCompareToString(exp, act) < 0)
246 ret = -1;
248 cleanup:
249 VIR_FREE(act);
250 vshTableFree(table);
251 return ret;
254 /* Testing zero-width characters by inserting few zero-width spaces */
255 static int
256 testUnicodeNonPrintableChar(const void *opaque ATTRIBUTE_UNUSED)
258 int ret = 0;
259 vshTablePtr table = NULL;
260 const char *exp =
261 " I\\x09d Name Status\n"
262 "----------------------------------\n"
263 " 1 f\\x07edora28 running\n"
264 " 2 rhel7.5 running\n";
265 char *act = NULL;
267 table = vshTableNew("I\td", "Name", "Status", NULL);
268 if (!table)
269 goto cleanup;
270 vshTableRowAppend(table, "1", "f\aedora28", "running", NULL);
271 vshTableRowAppend(table, "2", "rhel7.5", "running", NULL);
272 act = vshTablePrintToString(table, true);
274 if (virTestCompareToString(exp, act) < 0)
275 ret = -1;
277 cleanup:
278 VIR_FREE(act);
279 vshTableFree(table);
280 return ret;
283 static int
284 testNTables(const void *opaque ATTRIBUTE_UNUSED)
286 int ret = 0;
287 vshTablePtr table1 = NULL;
288 vshTablePtr table2 = NULL;
289 vshTablePtr table3 = NULL;
290 const char *exp1 =
291 " Id Name Status\n"
292 "--------------------------\n"
293 " 1 fedora28 running\n"
294 " 2 rhel7.5 running\n"
295 " 3 gazpacho \n";
296 const char *exp2 =
297 " Id Name Status\n"
298 "---------------------\n";
299 const char *exp3 =
300 " Id\n"
301 "-----\n"
302 " 1\n"
303 " 2\n"
304 " 3\n"
305 " 4\n";
306 char *act1 = NULL;
307 char *act2 = NULL;
308 char *act3 = NULL;
310 table1 = vshTableNew("Id", "Name", "Status", NULL);
311 if (!table1)
312 goto cleanup;
313 vshTableRowAppend(table1, "1", "fedora28", "running", NULL);
314 vshTableRowAppend(table1, "2", "rhel7.5", "running", NULL);
315 vshTableRowAppend(table1, "3", "gazpacho", "", NULL);
316 act1 = vshTablePrintToString(table1, true);
318 table2 = vshTableNew("Id", "Name", "Status", NULL);
319 if (!table2)
320 goto cleanup;
321 act2 = vshTablePrintToString(table2, true);
323 table3 = vshTableNew("Id", NULL);
324 if (!table3)
325 goto cleanup;
326 vshTableRowAppend(table3, "1", NULL);
327 vshTableRowAppend(table3, "2", NULL);
328 vshTableRowAppend(table3, "3", NULL);
329 vshTableRowAppend(table3, "4", NULL);
330 act3 = vshTablePrintToString(table3, true);
332 if (virTestCompareToString(exp1, act1) < 0)
333 ret = -1;
334 if (virTestCompareToString(exp2, act2) < 0)
335 ret = -1;
336 if (virTestCompareToString(exp3, act3) < 0)
337 ret = -1;
339 cleanup:
340 VIR_FREE(act1);
341 VIR_FREE(act2);
342 VIR_FREE(act3);
343 vshTableFree(table1);
344 vshTableFree(table2);
345 vshTableFree(table3);
346 return ret;
349 static int
350 mymain(void)
352 int ret = 0;
354 if (!setlocale(LC_CTYPE, "en_US.UTF-8"))
355 return EXIT_AM_SKIP;
357 if (virTestRun("testVshTableNew", testVshTableNew, NULL) < 0)
358 ret = -1;
360 if (virTestRun("testVshTableHeader", testVshTableHeader, NULL) < 0)
361 ret = -1;
363 if (virTestRun("testVshTableRowAppend", testVshTableRowAppend, NULL) < 0)
364 ret = -1;
366 if (virTestRun("testUnicode", testUnicode, NULL) < 0)
367 ret = -1;
369 if (virTestRun("testUnicodeArabic", testUnicodeArabic, NULL) < 0)
370 ret = -1;
372 if (virTestRun("testUnicodeZeroWidthChar",
373 testUnicodeZeroWidthChar,
374 NULL) < 0)
375 ret = -1;
377 if (virTestRun("testUnicodeCombiningChar",
378 testUnicodeCombiningChar,
379 NULL) < 0)
380 ret = -1;
382 if (virTestRun("testUnicodeNonPrintableChar",
383 testUnicodeNonPrintableChar,
384 NULL) < 0)
385 ret = -1;
387 if (virTestRun("testNTables", testNTables, NULL) < 0)
388 ret = -1;
390 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
393 VIR_TEST_MAIN(mymain)