2 * String Input Visitor unit-tests.
4 * Copyright (C) 2012 Red Hat Inc.
7 * Paolo Bonzini <pbonzini@redhat.com> (based on test-qmp-input-visitor)
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
16 #include "qemu-common.h"
17 #include "qapi/string-input-visitor.h"
18 #include "test-qapi-types.h"
19 #include "test-qapi-visit.h"
20 #include "qapi/qmp/types.h"
22 typedef struct TestInputVisitorData
{
23 StringInputVisitor
*siv
;
24 } TestInputVisitorData
;
26 static void visitor_input_teardown(TestInputVisitorData
*data
,
30 string_input_visitor_cleanup(data
->siv
);
35 /* This is provided instead of a test setup function so that the JSON
36 string used by the tests are kept in the test functions (and not
39 Visitor
*visitor_input_test_init(TestInputVisitorData
*data
,
44 data
->siv
= string_input_visitor_new(string
);
45 g_assert(data
->siv
!= NULL
);
47 v
= string_input_get_visitor(data
->siv
);
53 static void test_visitor_in_int(TestInputVisitorData
*data
,
56 int64_t res
= 0, value
= -42;
60 v
= visitor_input_test_init(data
, "-42");
62 visit_type_int(v
, &res
, NULL
, &err
);
64 g_assert_cmpint(res
, ==, value
);
67 static void test_visitor_in_bool(TestInputVisitorData
*data
,
74 v
= visitor_input_test_init(data
, "true");
76 visit_type_bool(v
, &res
, NULL
, &err
);
78 g_assert_cmpint(res
, ==, true);
79 visitor_input_teardown(data
, unused
);
81 v
= visitor_input_test_init(data
, "yes");
83 visit_type_bool(v
, &res
, NULL
, &err
);
85 g_assert_cmpint(res
, ==, true);
86 visitor_input_teardown(data
, unused
);
88 v
= visitor_input_test_init(data
, "on");
90 visit_type_bool(v
, &res
, NULL
, &err
);
92 g_assert_cmpint(res
, ==, true);
93 visitor_input_teardown(data
, unused
);
95 v
= visitor_input_test_init(data
, "false");
97 visit_type_bool(v
, &res
, NULL
, &err
);
99 g_assert_cmpint(res
, ==, false);
100 visitor_input_teardown(data
, unused
);
102 v
= visitor_input_test_init(data
, "no");
104 visit_type_bool(v
, &res
, NULL
, &err
);
106 g_assert_cmpint(res
, ==, false);
107 visitor_input_teardown(data
, unused
);
109 v
= visitor_input_test_init(data
, "off");
111 visit_type_bool(v
, &res
, NULL
, &err
);
113 g_assert_cmpint(res
, ==, false);
116 static void test_visitor_in_number(TestInputVisitorData
*data
,
119 double res
= 0, value
= 3.14;
123 v
= visitor_input_test_init(data
, "3.14");
125 visit_type_number(v
, &res
, NULL
, &err
);
127 g_assert_cmpfloat(res
, ==, value
);
130 static void test_visitor_in_string(TestInputVisitorData
*data
,
133 char *res
= NULL
, *value
= (char *) "Q E M U";
137 v
= visitor_input_test_init(data
, value
);
139 visit_type_str(v
, &res
, NULL
, &err
);
141 g_assert_cmpstr(res
, ==, value
);
146 static void test_visitor_in_enum(TestInputVisitorData
*data
,
153 for (i
= 0; EnumOne_lookup
[i
]; i
++) {
156 v
= visitor_input_test_init(data
, EnumOne_lookup
[i
]);
158 visit_type_EnumOne(v
, &res
, NULL
, &err
);
160 g_assert_cmpint(i
, ==, res
);
162 visitor_input_teardown(data
, NULL
);
168 /* Try to crash the visitors */
169 static void test_visitor_in_fuzz(TestInputVisitorData
*data
,
181 for (i
= 0; i
< 100; i
++) {
184 j
= g_test_rand_int_range(0, sizeof(buf
) - 1);
189 for (j
--; j
!= 0; j
--) {
190 buf
[j
- 1] = (char)g_test_rand_int_range(0, 256);
194 v
= visitor_input_test_init(data
, buf
);
195 visit_type_int(v
, &ires
, NULL
, NULL
);
197 v
= visitor_input_test_init(data
, buf
);
198 visit_type_bool(v
, &bres
, NULL
, NULL
);
199 visitor_input_teardown(data
, NULL
);
201 v
= visitor_input_test_init(data
, buf
);
202 visit_type_number(v
, &nres
, NULL
, NULL
);
204 v
= visitor_input_test_init(data
, buf
);
206 visit_type_str(v
, &sres
, NULL
, NULL
);
209 v
= visitor_input_test_init(data
, buf
);
210 visit_type_EnumOne(v
, &eres
, NULL
, NULL
);
211 visitor_input_teardown(data
, NULL
);
215 static void input_visitor_test_add(const char *testpath
,
216 TestInputVisitorData
*data
,
217 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
219 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
220 visitor_input_teardown
);
223 int main(int argc
, char **argv
)
225 TestInputVisitorData in_visitor_data
;
227 g_test_init(&argc
, &argv
, NULL
);
229 input_visitor_test_add("/string-visitor/input/int",
230 &in_visitor_data
, test_visitor_in_int
);
231 input_visitor_test_add("/string-visitor/input/bool",
232 &in_visitor_data
, test_visitor_in_bool
);
233 input_visitor_test_add("/string-visitor/input/number",
234 &in_visitor_data
, test_visitor_in_number
);
235 input_visitor_test_add("/string-visitor/input/string",
236 &in_visitor_data
, test_visitor_in_string
);
237 input_visitor_test_add("/string-visitor/input/enum",
238 &in_visitor_data
, test_visitor_in_enum
);
239 input_visitor_test_add("/string-visitor/input/fuzz",
240 &in_visitor_data
, test_visitor_in_fuzz
);