2 * Copyright (C) 2015-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/>.
22 #include "util/virjson.h"
23 #include "util/virqemu.h"
24 #include "testutils.h"
25 #include "testutilsqemu.h"
27 #define VIR_FROM_THIS VIR_FROM_NONE
32 const char *expectprops
;
33 virQEMUBuildCommandLineJSONArrayFormatFunc arrayfunc
;
34 } testQemuCommandBuildObjectFromJSONData
;
37 testQemuCommandBuildFromJSON(const void *opaque
)
39 const testQemuCommandBuildObjectFromJSONData
*data
= opaque
;
40 virJSONValuePtr val
= NULL
;
41 virBuffer buf
= VIR_BUFFER_INITIALIZER
;
45 if (!(val
= virJSONValueFromString(data
->props
))) {
46 fprintf(stderr
, "Failed to parse JSON string '%s'", data
->props
);
50 if (virQEMUBuildCommandLineJSON(val
, &buf
, data
->arrayfunc
) < 0) {
52 "\nvirQEMUBuildCommandlineJSON failed process JSON:\n%s\n",
57 result
= virBufferContentAndReset(&buf
);
59 if (STRNEQ_NULLABLE(data
->expectprops
, result
)) {
60 fprintf(stderr
, "\nFailed to create object string. "
61 "\nExpected:\n'%s'\nGot:\n'%s'",
62 NULLSTR(data
->expectprops
), NULLSTR(result
));
68 virJSONValueFree(val
);
77 testQemuCommandBuildObjectFromJSONData data1
;
80 fputs("libvirt not compiled with JSON support, skipping this test\n", stderr
);
84 virTestCounterReset("testQemuCommandBuildFromJSON");
86 #define DO_TEST_COMMAND_FROM_JSON(PROPS, ARRAYFUNC, EXPECT) \
88 data1.props = PROPS; \
89 data1.expectprops = EXPECT; \
90 data1.arrayfunc = ARRAYFUNC; \
91 if (virTestRun(virTestCounterNext(), \
92 testQemuCommandBuildFromJSON, \
97 #define DO_TEST_COMMAND_OBJECT_FROM_JSON(PROPS, EXPECT) \
98 DO_TEST_COMMAND_FROM_JSON(PROPS, virQEMUBuildCommandLineJSONArrayBitmap, EXPECT)
100 #define DO_TEST_COMMAND_DRIVE_FROM_JSON(PROPS, EXPECT) \
101 DO_TEST_COMMAND_FROM_JSON(PROPS, virQEMUBuildCommandLineJSONArrayNumbered, EXPECT)
103 DO_TEST_COMMAND_OBJECT_FROM_JSON("{}", NULL
);
104 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"string\":\"qwer\"}", "string=qwer");
105 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"string\":\"qw,e,r\"}", "string=qw,,e,,r");
106 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"number\":1234}", "number=1234");
107 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"boolean\":true}", "boolean=yes");
108 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"boolean\":false}", "boolean=no");
109 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[]}", NULL
);
110 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[0]}", "bitmap=0");
111 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[1,3,5]}",
112 "bitmap=1,bitmap=3,bitmap=5");
113 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[0,1,2,3]}", "bitmap=0-3");
114 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[1,2,3,5]}",
115 "bitmap=1-3,bitmap=5");
116 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[1,2,3,5,7,8,9]}",
117 "bitmap=1-3,bitmap=5,bitmap=7-9");
118 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"array\":[\"bleah\",\"qwerty\",1]}",
119 "array=bleah,array=qwerty,array=1");
120 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"boolean\":true,\"hyphen-name\":1234,\"some_string\":\"bleah\"}",
121 "boolean=yes,hyphen-name=1234,some_string=bleah");
122 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"nest\": {\"boolean\":true,"
123 "\"hyphen-name\":1234,"
124 "\"some_string\":\"bleah\","
125 "\"bleah\":\"bl,eah\""
128 "nest.boolean=yes,nest.hyphen-name=1234,"
129 "nest.some_string=bleah,nest.bleah=bl,,eah");
130 DO_TEST_COMMAND_DRIVE_FROM_JSON("{\"driver\":\"gluster\","
131 "\"volume\":\"test\","
133 "\"server\":[ { \"type\":\"tcp\","
134 "\"host\":\"example.com\","
137 "{ \"type\":\"unix\","
138 "\"socket\":\"/path/socket\""
140 "{ \"type\":\"tcp\","
141 "\"host\":\"example.com\""
145 "driver=gluster,volume=test,path=img,"
147 "server.0.host=example.com,"
148 "server.0.port=1234,"
149 "server.1.type=unix,"
150 "server.1.socket=/path/socket,"
152 "server.2.host=example.com");
158 VIR_TEST_MAIN(mymain
)