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: |
Daniel P. Berrange | b3db211 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 7 | * Paolo Bonzini <pbonzini@redhat.com> (based on test-qobject-input-visitor) |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 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 | |
Paolo Bonzini | 79ee7df | 2012-12-06 11:22:34 +0100 | [diff] [blame] | 15 | #include "qemu-common.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 16 | #include "qapi/error.h" |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 17 | #include "qapi/string-input-visitor.h" |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 18 | #include "test-qapi-visit.h" |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 19 | |
| 20 | typedef struct TestInputVisitorData { |
Eric Blake | 7a0525c | 2016-06-09 10:48:37 -0600 | [diff] [blame] | 21 | Visitor *v; |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 22 | } TestInputVisitorData; |
| 23 | |
| 24 | static void visitor_input_teardown(TestInputVisitorData *data, |
| 25 | const void *unused) |
| 26 | { |
Eric Blake | 7a0525c | 2016-06-09 10:48:37 -0600 | [diff] [blame] | 27 | if (data->v) { |
| 28 | visit_free(data->v); |
| 29 | data->v = NULL; |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | |
| 33 | /* This is provided instead of a test setup function so that the JSON |
| 34 | string used by the tests are kept in the test functions (and not |
| 35 | int main()) */ |
| 36 | static |
| 37 | Visitor *visitor_input_test_init(TestInputVisitorData *data, |
| 38 | const char *string) |
| 39 | { |
Markus Armbruster | 0f721d1 | 2017-03-03 13:32:41 +0100 | [diff] [blame] | 40 | visitor_input_teardown(data, NULL); |
| 41 | |
Eric Blake | 7a0525c | 2016-06-09 10:48:37 -0600 | [diff] [blame] | 42 | data->v = string_input_visitor_new(string); |
| 43 | g_assert(data->v); |
| 44 | return data->v; |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static void test_visitor_in_int(TestInputVisitorData *data, |
| 48 | const void *unused) |
| 49 | { |
| 50 | int64_t res = 0, value = -42; |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 51 | Error *err = NULL; |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 52 | Visitor *v; |
| 53 | |
| 54 | v = visitor_input_test_init(data, "-42"); |
| 55 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 56 | visit_type_int(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 57 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 58 | g_assert_cmpint(res, ==, value); |
Markus Armbruster | 7337468 | 2016-04-28 15:45:29 -0600 | [diff] [blame] | 59 | |
Markus Armbruster | 7337468 | 2016-04-28 15:45:29 -0600 | [diff] [blame] | 60 | v = visitor_input_test_init(data, "not an int"); |
| 61 | |
| 62 | visit_type_int(v, NULL, &res, &err); |
| 63 | error_free_or_abort(&err); |
Markus Armbruster | d278822 | 2017-03-20 17:13:43 +0100 | [diff] [blame] | 64 | |
| 65 | v = visitor_input_test_init(data, ""); |
| 66 | |
| 67 | visit_type_int(v, NULL, &res, &err); |
| 68 | error_free_or_abort(&err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 69 | } |
| 70 | |
Markus Armbruster | 3d089ce | 2017-03-03 13:32:42 +0100 | [diff] [blame] | 71 | static void check_ilist(Visitor *v, int64_t *expected, size_t n) |
| 72 | { |
| 73 | int64List *res = NULL; |
| 74 | int64List *tail; |
| 75 | int i; |
| 76 | |
| 77 | visit_type_int64List(v, NULL, &res, &error_abort); |
| 78 | tail = res; |
| 79 | for (i = 0; i < n; i++) { |
| 80 | g_assert(tail); |
| 81 | g_assert_cmpint(tail->value, ==, expected[i]); |
| 82 | tail = tail->next; |
| 83 | } |
| 84 | g_assert(!tail); |
| 85 | |
| 86 | qapi_free_int64List(res); |
| 87 | } |
| 88 | |
| 89 | static void check_ulist(Visitor *v, uint64_t *expected, size_t n) |
| 90 | { |
| 91 | uint64List *res = NULL; |
| 92 | uint64List *tail; |
| 93 | int i; |
| 94 | |
Markus Armbruster | 3d089ce | 2017-03-03 13:32:42 +0100 | [diff] [blame] | 95 | visit_type_uint64List(v, NULL, &res, &error_abort); |
| 96 | tail = res; |
| 97 | for (i = 0; i < n; i++) { |
| 98 | g_assert(tail); |
| 99 | g_assert_cmpuint(tail->value, ==, expected[i]); |
| 100 | tail = tail->next; |
| 101 | } |
| 102 | g_assert(!tail); |
| 103 | |
| 104 | qapi_free_uint64List(res); |
| 105 | } |
| 106 | |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 107 | static void test_visitor_in_intList(TestInputVisitorData *data, |
| 108 | const void *unused) |
| 109 | { |
David Hildenbrand | c9fba9d | 2018-11-21 17:44:18 +0100 | [diff] [blame] | 110 | int64_t expect1[] = { 1, 2, 0, 2, 3, 4, 20, 5, 6, 7, |
| 111 | 8, 9, 1, 2, 3, 4, 5, 6, 7, 8 }; |
Markus Armbruster | 3d089ce | 2017-03-03 13:32:42 +0100 | [diff] [blame] | 112 | int64_t expect2[] = { 32767, -32768, -32767 }; |
David Hildenbrand | c9fba9d | 2018-11-21 17:44:18 +0100 | [diff] [blame] | 113 | int64_t expect3[] = { INT64_MIN, INT64_MAX }; |
David Hildenbrand | eac4754 | 2018-11-21 17:44:17 +0100 | [diff] [blame] | 114 | int64_t expect4[] = { 1 }; |
David Hildenbrand | 345e401 | 2018-11-21 17:44:21 +0100 | [diff] [blame] | 115 | int64_t expect5[] = { INT64_MAX - 2, INT64_MAX - 1, INT64_MAX }; |
Markus Armbruster | 7337468 | 2016-04-28 15:45:29 -0600 | [diff] [blame] | 116 | Error *err = NULL; |
Markus Armbruster | 3d089ce | 2017-03-03 13:32:42 +0100 | [diff] [blame] | 117 | int64List *res = NULL; |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 118 | Visitor *v; |
Markus Armbruster | a9416dc | 2017-03-03 13:32:46 +0100 | [diff] [blame] | 119 | int64_t val; |
Markus Armbruster | 3d089ce | 2017-03-03 13:32:42 +0100 | [diff] [blame] | 120 | |
| 121 | /* Valid lists */ |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 122 | |
| 123 | v = visitor_input_test_init(data, "1,2,0,2-4,20,5-9,1-8"); |
Markus Armbruster | 3d089ce | 2017-03-03 13:32:42 +0100 | [diff] [blame] | 124 | check_ilist(v, expect1, ARRAY_SIZE(expect1)); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 125 | |
Markus Armbruster | 3d089ce | 2017-03-03 13:32:42 +0100 | [diff] [blame] | 126 | v = visitor_input_test_init(data, "32767,-32768--32767"); |
| 127 | check_ilist(v, expect2, ARRAY_SIZE(expect2)); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 128 | |
Markus Armbruster | 3d089ce | 2017-03-03 13:32:42 +0100 | [diff] [blame] | 129 | v = visitor_input_test_init(data, |
| 130 | "-9223372036854775808,9223372036854775807"); |
| 131 | check_ilist(v, expect3, ARRAY_SIZE(expect3)); |
| 132 | |
David Hildenbrand | eac4754 | 2018-11-21 17:44:17 +0100 | [diff] [blame] | 133 | v = visitor_input_test_init(data, "1-1"); |
| 134 | check_ilist(v, expect4, ARRAY_SIZE(expect4)); |
| 135 | |
David Hildenbrand | 345e401 | 2018-11-21 17:44:21 +0100 | [diff] [blame] | 136 | v = visitor_input_test_init(data, |
| 137 | "9223372036854775805-9223372036854775807"); |
| 138 | check_ilist(v, expect5, ARRAY_SIZE(expect5)); |
| 139 | |
David Hildenbrand | eac4754 | 2018-11-21 17:44:17 +0100 | [diff] [blame] | 140 | /* Value too large */ |
| 141 | |
| 142 | v = visitor_input_test_init(data, "9223372036854775808"); |
| 143 | visit_type_int64List(v, NULL, &res, &err); |
| 144 | error_free_or_abort(&err); |
| 145 | g_assert(!res); |
| 146 | |
| 147 | /* Value too small */ |
| 148 | |
| 149 | v = visitor_input_test_init(data, "-9223372036854775809"); |
| 150 | visit_type_int64List(v, NULL, &res, &err); |
| 151 | error_free_or_abort(&err); |
| 152 | g_assert(!res); |
| 153 | |
| 154 | /* Range not ascending */ |
| 155 | |
| 156 | v = visitor_input_test_init(data, "3-1"); |
| 157 | visit_type_int64List(v, NULL, &res, &err); |
| 158 | error_free_or_abort(&err); |
| 159 | g_assert(!res); |
| 160 | |
| 161 | v = visitor_input_test_init(data, "9223372036854775807-0"); |
| 162 | visit_type_int64List(v, NULL, &res, &err); |
| 163 | error_free_or_abort(&err); |
| 164 | g_assert(!res); |
| 165 | |
| 166 | /* Range too big (65536 is the limit against DOS attacks) */ |
| 167 | |
| 168 | v = visitor_input_test_init(data, "0-65536"); |
| 169 | visit_type_int64List(v, NULL, &res, &err); |
| 170 | error_free_or_abort(&err); |
| 171 | g_assert(!res); |
Markus Armbruster | 3d089ce | 2017-03-03 13:32:42 +0100 | [diff] [blame] | 172 | |
Markus Armbruster | d278822 | 2017-03-20 17:13:43 +0100 | [diff] [blame] | 173 | /* Empty list */ |
Markus Armbruster | 3d089ce | 2017-03-03 13:32:42 +0100 | [diff] [blame] | 174 | |
| 175 | v = visitor_input_test_init(data, ""); |
Markus Armbruster | d278822 | 2017-03-20 17:13:43 +0100 | [diff] [blame] | 176 | visit_type_int64List(v, NULL, &res, &error_abort); |
| 177 | g_assert(!res); |
Markus Armbruster | 3d089ce | 2017-03-03 13:32:42 +0100 | [diff] [blame] | 178 | |
| 179 | /* Not a list */ |
Markus Armbruster | 7337468 | 2016-04-28 15:45:29 -0600 | [diff] [blame] | 180 | |
Markus Armbruster | 7337468 | 2016-04-28 15:45:29 -0600 | [diff] [blame] | 181 | v = visitor_input_test_init(data, "not an int list"); |
| 182 | |
Markus Armbruster | 3d089ce | 2017-03-03 13:32:42 +0100 | [diff] [blame] | 183 | visit_type_int64List(v, NULL, &res, &err); |
Eric Blake | 74f24cb | 2016-04-28 15:45:30 -0600 | [diff] [blame] | 184 | error_free_or_abort(&err); |
| 185 | g_assert(!res); |
Markus Armbruster | 9cb8ef3 | 2017-03-03 13:32:43 +0100 | [diff] [blame] | 186 | |
| 187 | /* Unvisited list tail */ |
| 188 | |
| 189 | v = visitor_input_test_init(data, "0,2-3"); |
| 190 | |
David Hildenbrand | 1308859 | 2018-11-21 17:44:19 +0100 | [diff] [blame] | 191 | visit_start_list(v, NULL, NULL, 0, &error_abort); |
| 192 | visit_type_int64(v, NULL, &val, &error_abort); |
| 193 | g_assert_cmpint(val, ==, 0); |
| 194 | visit_type_int64(v, NULL, &val, &error_abort); |
| 195 | g_assert_cmpint(val, ==, 2); |
Markus Armbruster | a4a1c70 | 2017-03-03 13:32:45 +0100 | [diff] [blame] | 196 | |
| 197 | visit_check_list(v, &err); |
| 198 | error_free_or_abort(&err); |
David Hildenbrand | 1308859 | 2018-11-21 17:44:19 +0100 | [diff] [blame] | 199 | visit_end_list(v, NULL); |
Markus Armbruster | a9416dc | 2017-03-03 13:32:46 +0100 | [diff] [blame] | 200 | |
| 201 | /* Visit beyond end of list */ |
David Hildenbrand | 1308859 | 2018-11-21 17:44:19 +0100 | [diff] [blame] | 202 | |
Markus Armbruster | a9416dc | 2017-03-03 13:32:46 +0100 | [diff] [blame] | 203 | v = visitor_input_test_init(data, "0"); |
| 204 | |
David Hildenbrand | 1308859 | 2018-11-21 17:44:19 +0100 | [diff] [blame] | 205 | visit_start_list(v, NULL, NULL, 0, &error_abort); |
| 206 | visit_type_int64(v, NULL, &val, &err); |
| 207 | g_assert_cmpint(val, ==, 0); |
Markus Armbruster | a9416dc | 2017-03-03 13:32:46 +0100 | [diff] [blame] | 208 | visit_type_int64(v, NULL, &val, &err); |
David Hildenbrand | c9fba9d | 2018-11-21 17:44:18 +0100 | [diff] [blame] | 209 | error_free_or_abort(&err); |
Markus Armbruster | a9416dc | 2017-03-03 13:32:46 +0100 | [diff] [blame] | 210 | |
David Hildenbrand | 1308859 | 2018-11-21 17:44:19 +0100 | [diff] [blame] | 211 | visit_check_list(v, &error_abort); |
| 212 | visit_end_list(v, NULL); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 213 | } |
| 214 | |
David Hildenbrand | cc871b1 | 2018-11-21 17:44:20 +0100 | [diff] [blame] | 215 | static void test_visitor_in_uintList(TestInputVisitorData *data, |
| 216 | const void *unused) |
| 217 | { |
| 218 | uint64_t expect1[] = { 1, 2, 0, 2, 3, 4, 20, 5, 6, 7, |
| 219 | 8, 9, 1, 2, 3, 4, 5, 6, 7, 8 }; |
| 220 | uint64_t expect2[] = { 32767, -32768, -32767 }; |
| 221 | uint64_t expect3[] = { INT64_MIN, INT64_MAX }; |
| 222 | uint64_t expect4[] = { 1 }; |
| 223 | uint64_t expect5[] = { UINT64_MAX }; |
David Hildenbrand | 345e401 | 2018-11-21 17:44:21 +0100 | [diff] [blame] | 224 | uint64_t expect6[] = { UINT64_MAX - 2, UINT64_MAX - 1, UINT64_MAX }; |
David Hildenbrand | cc871b1 | 2018-11-21 17:44:20 +0100 | [diff] [blame] | 225 | Error *err = NULL; |
| 226 | uint64List *res = NULL; |
| 227 | Visitor *v; |
| 228 | uint64_t val; |
| 229 | |
| 230 | /* Valid lists */ |
| 231 | |
| 232 | v = visitor_input_test_init(data, "1,2,0,2-4,20,5-9,1-8"); |
| 233 | check_ulist(v, expect1, ARRAY_SIZE(expect1)); |
| 234 | |
| 235 | v = visitor_input_test_init(data, "32767,-32768--32767"); |
| 236 | check_ulist(v, expect2, ARRAY_SIZE(expect2)); |
| 237 | |
| 238 | v = visitor_input_test_init(data, |
| 239 | "-9223372036854775808,9223372036854775807"); |
| 240 | check_ulist(v, expect3, ARRAY_SIZE(expect3)); |
| 241 | |
| 242 | v = visitor_input_test_init(data, "1-1"); |
| 243 | check_ulist(v, expect4, ARRAY_SIZE(expect4)); |
| 244 | |
| 245 | v = visitor_input_test_init(data, "18446744073709551615"); |
| 246 | check_ulist(v, expect5, ARRAY_SIZE(expect5)); |
| 247 | |
David Hildenbrand | 345e401 | 2018-11-21 17:44:21 +0100 | [diff] [blame] | 248 | v = visitor_input_test_init(data, |
| 249 | "18446744073709551613-18446744073709551615"); |
| 250 | check_ulist(v, expect6, ARRAY_SIZE(expect6)); |
| 251 | |
David Hildenbrand | cc871b1 | 2018-11-21 17:44:20 +0100 | [diff] [blame] | 252 | /* Value too large */ |
| 253 | |
| 254 | v = visitor_input_test_init(data, "18446744073709551616"); |
| 255 | visit_type_uint64List(v, NULL, &res, &err); |
| 256 | error_free_or_abort(&err); |
| 257 | g_assert(!res); |
| 258 | |
| 259 | /* Value too small */ |
| 260 | |
| 261 | v = visitor_input_test_init(data, "-18446744073709551616"); |
| 262 | visit_type_uint64List(v, NULL, &res, &err); |
| 263 | error_free_or_abort(&err); |
| 264 | g_assert(!res); |
| 265 | |
| 266 | /* Range not ascending */ |
| 267 | |
| 268 | v = visitor_input_test_init(data, "3-1"); |
| 269 | visit_type_uint64List(v, NULL, &res, &err); |
| 270 | error_free_or_abort(&err); |
| 271 | g_assert(!res); |
| 272 | |
| 273 | v = visitor_input_test_init(data, "18446744073709551615-0"); |
| 274 | visit_type_uint64List(v, NULL, &res, &err); |
| 275 | error_free_or_abort(&err); |
| 276 | g_assert(!res); |
| 277 | |
| 278 | /* Range too big (65536 is the limit against DOS attacks) */ |
| 279 | |
| 280 | v = visitor_input_test_init(data, "0-65536"); |
| 281 | visit_type_uint64List(v, NULL, &res, &err); |
| 282 | error_free_or_abort(&err); |
| 283 | g_assert(!res); |
| 284 | |
| 285 | /* Empty list */ |
| 286 | |
| 287 | v = visitor_input_test_init(data, ""); |
| 288 | visit_type_uint64List(v, NULL, &res, &error_abort); |
| 289 | g_assert(!res); |
| 290 | |
| 291 | /* Not a list */ |
| 292 | |
| 293 | v = visitor_input_test_init(data, "not an uint list"); |
| 294 | |
| 295 | visit_type_uint64List(v, NULL, &res, &err); |
| 296 | error_free_or_abort(&err); |
| 297 | g_assert(!res); |
| 298 | |
| 299 | /* Unvisited list tail */ |
| 300 | |
| 301 | v = visitor_input_test_init(data, "0,2-3"); |
| 302 | |
| 303 | visit_start_list(v, NULL, NULL, 0, &error_abort); |
| 304 | visit_type_uint64(v, NULL, &val, &error_abort); |
| 305 | g_assert_cmpuint(val, ==, 0); |
| 306 | visit_type_uint64(v, NULL, &val, &error_abort); |
| 307 | g_assert_cmpuint(val, ==, 2); |
| 308 | |
| 309 | visit_check_list(v, &err); |
| 310 | error_free_or_abort(&err); |
| 311 | visit_end_list(v, NULL); |
| 312 | |
| 313 | /* Visit beyond end of list */ |
| 314 | |
| 315 | v = visitor_input_test_init(data, "0"); |
| 316 | |
| 317 | visit_start_list(v, NULL, NULL, 0, &error_abort); |
| 318 | visit_type_uint64(v, NULL, &val, &err); |
| 319 | g_assert_cmpuint(val, ==, 0); |
| 320 | visit_type_uint64(v, NULL, &val, &err); |
| 321 | error_free_or_abort(&err); |
| 322 | |
| 323 | visit_check_list(v, &error_abort); |
| 324 | visit_end_list(v, NULL); |
| 325 | } |
| 326 | |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 327 | static void test_visitor_in_bool(TestInputVisitorData *data, |
| 328 | const void *unused) |
| 329 | { |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 330 | Error *err = NULL; |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 331 | bool res = false; |
| 332 | Visitor *v; |
| 333 | |
| 334 | v = visitor_input_test_init(data, "true"); |
| 335 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 336 | visit_type_bool(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 337 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 338 | g_assert_cmpint(res, ==, true); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 339 | |
| 340 | v = visitor_input_test_init(data, "yes"); |
| 341 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 342 | visit_type_bool(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 343 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 344 | g_assert_cmpint(res, ==, true); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 345 | |
| 346 | v = visitor_input_test_init(data, "on"); |
| 347 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 348 | visit_type_bool(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 349 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 350 | g_assert_cmpint(res, ==, true); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 351 | |
| 352 | v = visitor_input_test_init(data, "false"); |
| 353 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 354 | visit_type_bool(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 355 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 356 | g_assert_cmpint(res, ==, false); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 357 | |
| 358 | v = visitor_input_test_init(data, "no"); |
| 359 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 360 | visit_type_bool(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 361 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 362 | g_assert_cmpint(res, ==, false); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 363 | |
| 364 | v = visitor_input_test_init(data, "off"); |
| 365 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 366 | visit_type_bool(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 367 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 368 | g_assert_cmpint(res, ==, false); |
| 369 | } |
| 370 | |
| 371 | static void test_visitor_in_number(TestInputVisitorData *data, |
| 372 | const void *unused) |
| 373 | { |
| 374 | double res = 0, value = 3.14; |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 375 | Error *err = NULL; |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 376 | Visitor *v; |
| 377 | |
| 378 | v = visitor_input_test_init(data, "3.14"); |
| 379 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 380 | visit_type_number(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 381 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 382 | g_assert_cmpfloat(res, ==, value); |
David Hildenbrand | 4b69d4c | 2018-11-21 17:44:15 +0100 | [diff] [blame] | 383 | |
| 384 | /* NaN and infinity has to be rejected */ |
| 385 | |
| 386 | v = visitor_input_test_init(data, "NaN"); |
| 387 | |
| 388 | visit_type_number(v, NULL, &res, &err); |
| 389 | error_free_or_abort(&err); |
| 390 | |
| 391 | v = visitor_input_test_init(data, "inf"); |
| 392 | |
| 393 | visit_type_number(v, NULL, &res, &err); |
| 394 | error_free_or_abort(&err); |
| 395 | |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | static void test_visitor_in_string(TestInputVisitorData *data, |
| 399 | const void *unused) |
| 400 | { |
| 401 | char *res = NULL, *value = (char *) "Q E M U"; |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 402 | Error *err = NULL; |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 403 | Visitor *v; |
| 404 | |
| 405 | v = visitor_input_test_init(data, value); |
| 406 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 407 | visit_type_str(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 408 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 409 | g_assert_cmpstr(res, ==, value); |
| 410 | |
| 411 | g_free(res); |
| 412 | } |
| 413 | |
| 414 | static void test_visitor_in_enum(TestInputVisitorData *data, |
| 415 | const void *unused) |
| 416 | { |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 417 | Error *err = NULL; |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 418 | Visitor *v; |
| 419 | EnumOne i; |
| 420 | |
Markus Armbruster | 1c236ba | 2017-08-24 10:46:06 +0200 | [diff] [blame] | 421 | for (i = 0; i < ENUM_ONE__MAX; i++) { |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 422 | EnumOne res = -1; |
| 423 | |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 424 | v = visitor_input_test_init(data, EnumOne_str(i)); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 425 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 426 | visit_type_EnumOne(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 427 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 428 | g_assert_cmpint(i, ==, res); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 429 | } |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 430 | } |
| 431 | |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 432 | /* Try to crash the visitors */ |
| 433 | static void test_visitor_in_fuzz(TestInputVisitorData *data, |
| 434 | const void *unused) |
| 435 | { |
| 436 | int64_t ires; |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 437 | intList *ilres; |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 438 | bool bres; |
| 439 | double nres; |
| 440 | char *sres; |
| 441 | EnumOne eres; |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 442 | Visitor *v; |
| 443 | unsigned int i; |
| 444 | char buf[10000]; |
| 445 | |
| 446 | for (i = 0; i < 100; i++) { |
Andrey Shinkevich | f673174 | 2019-07-30 19:01:37 +0300 | [diff] [blame] | 447 | unsigned int j, k; |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 448 | |
| 449 | j = g_test_rand_int_range(0, sizeof(buf) - 1); |
| 450 | |
| 451 | buf[j] = '\0'; |
| 452 | |
Andrey Shinkevich | f673174 | 2019-07-30 19:01:37 +0300 | [diff] [blame] | 453 | for (k = 0; k != j; k++) { |
| 454 | buf[k] = (char)g_test_rand_int_range(0, 256); |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | v = visitor_input_test_init(data, buf); |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 458 | visit_type_int(v, NULL, &ires, NULL); |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 459 | |
| 460 | v = visitor_input_test_init(data, buf); |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 461 | visit_type_intList(v, NULL, &ilres, NULL); |
Marc-André Lureau | bd79406 | 2016-07-12 16:08:47 +0200 | [diff] [blame] | 462 | qapi_free_intList(ilres); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 463 | |
| 464 | v = visitor_input_test_init(data, buf); |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 465 | visit_type_bool(v, NULL, &bres, NULL); |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 466 | |
| 467 | v = visitor_input_test_init(data, buf); |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 468 | visit_type_number(v, NULL, &nres, NULL); |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 469 | |
| 470 | v = visitor_input_test_init(data, buf); |
Peter Maydell | 0184543 | 2013-02-05 20:44:23 +0000 | [diff] [blame] | 471 | sres = NULL; |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 472 | visit_type_str(v, NULL, &sres, NULL); |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 473 | g_free(sres); |
| 474 | |
| 475 | v = visitor_input_test_init(data, buf); |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 476 | visit_type_EnumOne(v, NULL, &eres, NULL); |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 480 | static void input_visitor_test_add(const char *testpath, |
| 481 | TestInputVisitorData *data, |
| 482 | void (*test_func)(TestInputVisitorData *data, const void *user_data)) |
| 483 | { |
| 484 | g_test_add(testpath, TestInputVisitorData, data, NULL, test_func, |
| 485 | visitor_input_teardown); |
| 486 | } |
| 487 | |
| 488 | int main(int argc, char **argv) |
| 489 | { |
| 490 | TestInputVisitorData in_visitor_data; |
| 491 | |
| 492 | g_test_init(&argc, &argv, NULL); |
| 493 | |
| 494 | input_visitor_test_add("/string-visitor/input/int", |
| 495 | &in_visitor_data, test_visitor_in_int); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 496 | input_visitor_test_add("/string-visitor/input/intList", |
| 497 | &in_visitor_data, test_visitor_in_intList); |
David Hildenbrand | cc871b1 | 2018-11-21 17:44:20 +0100 | [diff] [blame] | 498 | input_visitor_test_add("/string-visitor/input/uintList", |
| 499 | &in_visitor_data, test_visitor_in_uintList); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 500 | input_visitor_test_add("/string-visitor/input/bool", |
| 501 | &in_visitor_data, test_visitor_in_bool); |
| 502 | input_visitor_test_add("/string-visitor/input/number", |
| 503 | &in_visitor_data, test_visitor_in_number); |
| 504 | input_visitor_test_add("/string-visitor/input/string", |
| 505 | &in_visitor_data, test_visitor_in_string); |
| 506 | input_visitor_test_add("/string-visitor/input/enum", |
| 507 | &in_visitor_data, test_visitor_in_enum); |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 508 | input_visitor_test_add("/string-visitor/input/fuzz", |
| 509 | &in_visitor_data, test_visitor_in_fuzz); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 510 | |
| 511 | g_test_run(); |
| 512 | |
| 513 | return 0; |
| 514 | } |