Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * String Input Visitor unit-tests. |
| 3 | * |
| 4 | * Copyright (C) 2012 Red Hat Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Paolo Bonzini <pbonzini@redhat.com> (based on test-qmp-input-visitor) |
| 8 | * |
| 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. |
| 11 | */ |
| 12 | |
Peter Maydell | 681c28a | 2016-02-08 18:08:51 +0000 | [diff] [blame] | 13 | #include "qemu/osdep.h" |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 14 | #include <glib.h> |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 15 | |
Paolo Bonzini | 79ee7df | 2012-12-06 11:22:34 +0100 | [diff] [blame] | 16 | #include "qemu-common.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 17 | #include "qapi/error.h" |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 18 | #include "qapi/string-input-visitor.h" |
| 19 | #include "test-qapi-types.h" |
| 20 | #include "test-qapi-visit.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 21 | #include "qapi/qmp/types.h" |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 22 | |
| 23 | typedef struct TestInputVisitorData { |
| 24 | StringInputVisitor *siv; |
| 25 | } TestInputVisitorData; |
| 26 | |
| 27 | static void visitor_input_teardown(TestInputVisitorData *data, |
| 28 | const void *unused) |
| 29 | { |
| 30 | if (data->siv) { |
| 31 | string_input_visitor_cleanup(data->siv); |
| 32 | data->siv = NULL; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /* This is provided instead of a test setup function so that the JSON |
| 37 | string used by the tests are kept in the test functions (and not |
| 38 | int main()) */ |
| 39 | static |
| 40 | Visitor *visitor_input_test_init(TestInputVisitorData *data, |
| 41 | const char *string) |
| 42 | { |
| 43 | Visitor *v; |
| 44 | |
| 45 | data->siv = string_input_visitor_new(string); |
| 46 | g_assert(data->siv != NULL); |
| 47 | |
| 48 | v = string_input_get_visitor(data->siv); |
| 49 | g_assert(v != NULL); |
| 50 | |
| 51 | return v; |
| 52 | } |
| 53 | |
| 54 | static void test_visitor_in_int(TestInputVisitorData *data, |
| 55 | const void *unused) |
| 56 | { |
| 57 | int64_t res = 0, value = -42; |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 58 | Error *err = NULL; |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 59 | Visitor *v; |
| 60 | |
| 61 | v = visitor_input_test_init(data, "-42"); |
| 62 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 63 | visit_type_int(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 64 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 65 | g_assert_cmpint(res, ==, value); |
Markus Armbruster | 7337468 | 2016-04-28 15:45:29 -0600 | [diff] [blame] | 66 | |
| 67 | visitor_input_teardown(data, unused); |
| 68 | |
| 69 | v = visitor_input_test_init(data, "not an int"); |
| 70 | |
| 71 | visit_type_int(v, NULL, &res, &err); |
| 72 | error_free_or_abort(&err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 75 | static void test_visitor_in_intList(TestInputVisitorData *data, |
| 76 | const void *unused) |
| 77 | { |
| 78 | int64_t value[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20}; |
| 79 | int16List *res = NULL, *tmp; |
Markus Armbruster | 7337468 | 2016-04-28 15:45:29 -0600 | [diff] [blame] | 80 | Error *err = NULL; |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 81 | Visitor *v; |
| 82 | int i = 0; |
| 83 | |
| 84 | v = visitor_input_test_init(data, "1,2,0,2-4,20,5-9,1-8"); |
| 85 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 86 | visit_type_int16List(v, NULL, &res, &error_abort); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 87 | tmp = res; |
| 88 | while (i < sizeof(value) / sizeof(value[0])) { |
| 89 | g_assert(tmp); |
| 90 | g_assert_cmpint(tmp->value, ==, value[i++]); |
| 91 | tmp = tmp->next; |
| 92 | } |
| 93 | g_assert(!tmp); |
| 94 | |
Eric Blake | 74f24cb | 2016-04-28 15:45:30 -0600 | [diff] [blame] | 95 | qapi_free_int16List(res); |
Markus Armbruster | 7337468 | 2016-04-28 15:45:29 -0600 | [diff] [blame] | 96 | |
| 97 | visitor_input_teardown(data, unused); |
| 98 | |
| 99 | v = visitor_input_test_init(data, "not an int list"); |
| 100 | |
| 101 | visit_type_int16List(v, NULL, &res, &err); |
Eric Blake | 74f24cb | 2016-04-28 15:45:30 -0600 | [diff] [blame] | 102 | error_free_or_abort(&err); |
| 103 | g_assert(!res); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 104 | } |
| 105 | |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 106 | static void test_visitor_in_bool(TestInputVisitorData *data, |
| 107 | const void *unused) |
| 108 | { |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 109 | Error *err = NULL; |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 110 | bool res = false; |
| 111 | Visitor *v; |
| 112 | |
| 113 | v = visitor_input_test_init(data, "true"); |
| 114 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 115 | visit_type_bool(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 116 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 117 | g_assert_cmpint(res, ==, true); |
| 118 | visitor_input_teardown(data, unused); |
| 119 | |
| 120 | v = visitor_input_test_init(data, "yes"); |
| 121 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 122 | visit_type_bool(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 123 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 124 | g_assert_cmpint(res, ==, true); |
| 125 | visitor_input_teardown(data, unused); |
| 126 | |
| 127 | v = visitor_input_test_init(data, "on"); |
| 128 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 129 | visit_type_bool(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 130 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 131 | g_assert_cmpint(res, ==, true); |
| 132 | visitor_input_teardown(data, unused); |
| 133 | |
| 134 | v = visitor_input_test_init(data, "false"); |
| 135 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 136 | visit_type_bool(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 137 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 138 | g_assert_cmpint(res, ==, false); |
| 139 | visitor_input_teardown(data, unused); |
| 140 | |
| 141 | v = visitor_input_test_init(data, "no"); |
| 142 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 143 | visit_type_bool(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 144 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 145 | g_assert_cmpint(res, ==, false); |
| 146 | visitor_input_teardown(data, unused); |
| 147 | |
| 148 | v = visitor_input_test_init(data, "off"); |
| 149 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 150 | visit_type_bool(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 151 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 152 | g_assert_cmpint(res, ==, false); |
| 153 | } |
| 154 | |
| 155 | static void test_visitor_in_number(TestInputVisitorData *data, |
| 156 | const void *unused) |
| 157 | { |
| 158 | double res = 0, value = 3.14; |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 159 | Error *err = NULL; |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 160 | Visitor *v; |
| 161 | |
| 162 | v = visitor_input_test_init(data, "3.14"); |
| 163 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 164 | visit_type_number(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 165 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 166 | g_assert_cmpfloat(res, ==, value); |
| 167 | } |
| 168 | |
| 169 | static void test_visitor_in_string(TestInputVisitorData *data, |
| 170 | const void *unused) |
| 171 | { |
| 172 | char *res = NULL, *value = (char *) "Q E M U"; |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 173 | Error *err = NULL; |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 174 | Visitor *v; |
| 175 | |
| 176 | v = visitor_input_test_init(data, value); |
| 177 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 178 | visit_type_str(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 179 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 180 | g_assert_cmpstr(res, ==, value); |
| 181 | |
| 182 | g_free(res); |
| 183 | } |
| 184 | |
| 185 | static void test_visitor_in_enum(TestInputVisitorData *data, |
| 186 | const void *unused) |
| 187 | { |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 188 | Error *err = NULL; |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 189 | Visitor *v; |
| 190 | EnumOne i; |
| 191 | |
| 192 | for (i = 0; EnumOne_lookup[i]; i++) { |
| 193 | EnumOne res = -1; |
| 194 | |
| 195 | v = visitor_input_test_init(data, EnumOne_lookup[i]); |
| 196 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 197 | visit_type_EnumOne(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 198 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 199 | g_assert_cmpint(i, ==, res); |
| 200 | |
| 201 | visitor_input_teardown(data, NULL); |
| 202 | } |
| 203 | |
| 204 | data->siv = NULL; |
| 205 | } |
| 206 | |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 207 | /* Try to crash the visitors */ |
| 208 | static void test_visitor_in_fuzz(TestInputVisitorData *data, |
| 209 | const void *unused) |
| 210 | { |
| 211 | int64_t ires; |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 212 | intList *ilres; |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 213 | bool bres; |
| 214 | double nres; |
| 215 | char *sres; |
| 216 | EnumOne eres; |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 217 | Visitor *v; |
| 218 | unsigned int i; |
| 219 | char buf[10000]; |
| 220 | |
| 221 | for (i = 0; i < 100; i++) { |
| 222 | unsigned int j; |
| 223 | |
| 224 | j = g_test_rand_int_range(0, sizeof(buf) - 1); |
| 225 | |
| 226 | buf[j] = '\0'; |
| 227 | |
| 228 | if (j != 0) { |
| 229 | for (j--; j != 0; j--) { |
| 230 | buf[j - 1] = (char)g_test_rand_int_range(0, 256); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | v = visitor_input_test_init(data, buf); |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 235 | visit_type_int(v, NULL, &ires, NULL); |
Hu Tao | cac124d | 2014-06-10 19:15:26 +0800 | [diff] [blame] | 236 | visitor_input_teardown(data, NULL); |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 237 | |
| 238 | v = visitor_input_test_init(data, buf); |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 239 | visit_type_intList(v, NULL, &ilres, NULL); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 240 | visitor_input_teardown(data, NULL); |
| 241 | |
| 242 | v = visitor_input_test_init(data, buf); |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 243 | visit_type_bool(v, NULL, &bres, NULL); |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 244 | visitor_input_teardown(data, NULL); |
| 245 | |
| 246 | v = visitor_input_test_init(data, buf); |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 247 | visit_type_number(v, NULL, &nres, NULL); |
Hu Tao | cac124d | 2014-06-10 19:15:26 +0800 | [diff] [blame] | 248 | visitor_input_teardown(data, NULL); |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 249 | |
| 250 | v = visitor_input_test_init(data, buf); |
Peter Maydell | 0184543 | 2013-02-05 20:44:23 +0000 | [diff] [blame] | 251 | sres = NULL; |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 252 | visit_type_str(v, NULL, &sres, NULL); |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 253 | g_free(sres); |
Hu Tao | cac124d | 2014-06-10 19:15:26 +0800 | [diff] [blame] | 254 | visitor_input_teardown(data, NULL); |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 255 | |
| 256 | v = visitor_input_test_init(data, buf); |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 257 | visit_type_EnumOne(v, NULL, &eres, NULL); |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 258 | visitor_input_teardown(data, NULL); |
| 259 | } |
| 260 | } |
| 261 | |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 262 | static void input_visitor_test_add(const char *testpath, |
| 263 | TestInputVisitorData *data, |
| 264 | void (*test_func)(TestInputVisitorData *data, const void *user_data)) |
| 265 | { |
| 266 | g_test_add(testpath, TestInputVisitorData, data, NULL, test_func, |
| 267 | visitor_input_teardown); |
| 268 | } |
| 269 | |
| 270 | int main(int argc, char **argv) |
| 271 | { |
| 272 | TestInputVisitorData in_visitor_data; |
| 273 | |
| 274 | g_test_init(&argc, &argv, NULL); |
| 275 | |
| 276 | input_visitor_test_add("/string-visitor/input/int", |
| 277 | &in_visitor_data, test_visitor_in_int); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 278 | input_visitor_test_add("/string-visitor/input/intList", |
| 279 | &in_visitor_data, test_visitor_in_intList); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 280 | input_visitor_test_add("/string-visitor/input/bool", |
| 281 | &in_visitor_data, test_visitor_in_bool); |
| 282 | input_visitor_test_add("/string-visitor/input/number", |
| 283 | &in_visitor_data, test_visitor_in_number); |
| 284 | input_visitor_test_add("/string-visitor/input/string", |
| 285 | &in_visitor_data, test_visitor_in_string); |
| 286 | input_visitor_test_add("/string-visitor/input/enum", |
| 287 | &in_visitor_data, test_visitor_in_enum); |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 288 | input_visitor_test_add("/string-visitor/input/fuzz", |
| 289 | &in_visitor_data, test_visitor_in_fuzz); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 290 | |
| 291 | g_test_run(); |
| 292 | |
| 293 | return 0; |
| 294 | } |