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 | |
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" |
| 18 | #include "test-qapi-types.h" |
| 19 | #include "test-qapi-visit.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 20 | #include "qapi/qmp/types.h" |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 21 | |
| 22 | typedef struct TestInputVisitorData { |
Eric Blake | 7a0525c | 2016-06-09 10:48:37 -0600 | [diff] [blame] | 23 | Visitor *v; |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 24 | } TestInputVisitorData; |
| 25 | |
| 26 | static void visitor_input_teardown(TestInputVisitorData *data, |
| 27 | const void *unused) |
| 28 | { |
Eric Blake | 7a0525c | 2016-06-09 10:48:37 -0600 | [diff] [blame] | 29 | if (data->v) { |
| 30 | visit_free(data->v); |
| 31 | data->v = NULL; |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
| 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 |
| 37 | int main()) */ |
| 38 | static |
| 39 | Visitor *visitor_input_test_init(TestInputVisitorData *data, |
| 40 | const char *string) |
| 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 | |
| 60 | visitor_input_teardown(data, unused); |
| 61 | |
| 62 | v = visitor_input_test_init(data, "not an int"); |
| 63 | |
| 64 | visit_type_int(v, NULL, &res, &err); |
| 65 | error_free_or_abort(&err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 68 | static void test_visitor_in_intList(TestInputVisitorData *data, |
| 69 | const void *unused) |
| 70 | { |
| 71 | int64_t value[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20}; |
| 72 | int16List *res = NULL, *tmp; |
Markus Armbruster | 7337468 | 2016-04-28 15:45:29 -0600 | [diff] [blame] | 73 | Error *err = NULL; |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 74 | Visitor *v; |
| 75 | int i = 0; |
| 76 | |
| 77 | v = visitor_input_test_init(data, "1,2,0,2-4,20,5-9,1-8"); |
| 78 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 79 | visit_type_int16List(v, NULL, &res, &error_abort); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 80 | tmp = res; |
| 81 | while (i < sizeof(value) / sizeof(value[0])) { |
| 82 | g_assert(tmp); |
| 83 | g_assert_cmpint(tmp->value, ==, value[i++]); |
| 84 | tmp = tmp->next; |
| 85 | } |
| 86 | g_assert(!tmp); |
| 87 | |
Eric Blake | 74f24cb | 2016-04-28 15:45:30 -0600 | [diff] [blame] | 88 | qapi_free_int16List(res); |
Markus Armbruster | 7337468 | 2016-04-28 15:45:29 -0600 | [diff] [blame] | 89 | |
| 90 | visitor_input_teardown(data, unused); |
| 91 | |
| 92 | v = visitor_input_test_init(data, "not an int list"); |
| 93 | |
| 94 | visit_type_int16List(v, NULL, &res, &err); |
Eric Blake | 74f24cb | 2016-04-28 15:45:30 -0600 | [diff] [blame] | 95 | error_free_or_abort(&err); |
| 96 | g_assert(!res); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 97 | } |
| 98 | |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 99 | static void test_visitor_in_bool(TestInputVisitorData *data, |
| 100 | const void *unused) |
| 101 | { |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 102 | Error *err = NULL; |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 103 | bool res = false; |
| 104 | Visitor *v; |
| 105 | |
| 106 | v = visitor_input_test_init(data, "true"); |
| 107 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 108 | visit_type_bool(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 109 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 110 | g_assert_cmpint(res, ==, true); |
| 111 | visitor_input_teardown(data, unused); |
| 112 | |
| 113 | v = visitor_input_test_init(data, "yes"); |
| 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, "on"); |
| 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, "false"); |
| 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, ==, false); |
| 132 | visitor_input_teardown(data, unused); |
| 133 | |
| 134 | v = visitor_input_test_init(data, "no"); |
| 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, "off"); |
| 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 | } |
| 147 | |
| 148 | static void test_visitor_in_number(TestInputVisitorData *data, |
| 149 | const void *unused) |
| 150 | { |
| 151 | double res = 0, value = 3.14; |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 152 | Error *err = NULL; |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 153 | Visitor *v; |
| 154 | |
| 155 | v = visitor_input_test_init(data, "3.14"); |
| 156 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 157 | visit_type_number(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 158 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 159 | g_assert_cmpfloat(res, ==, value); |
| 160 | } |
| 161 | |
| 162 | static void test_visitor_in_string(TestInputVisitorData *data, |
| 163 | const void *unused) |
| 164 | { |
| 165 | char *res = NULL, *value = (char *) "Q E M U"; |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 166 | Error *err = NULL; |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 167 | Visitor *v; |
| 168 | |
| 169 | v = visitor_input_test_init(data, value); |
| 170 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 171 | visit_type_str(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 172 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 173 | g_assert_cmpstr(res, ==, value); |
| 174 | |
| 175 | g_free(res); |
| 176 | } |
| 177 | |
| 178 | static void test_visitor_in_enum(TestInputVisitorData *data, |
| 179 | const void *unused) |
| 180 | { |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 181 | Error *err = NULL; |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 182 | Visitor *v; |
| 183 | EnumOne i; |
| 184 | |
| 185 | for (i = 0; EnumOne_lookup[i]; i++) { |
| 186 | EnumOne res = -1; |
| 187 | |
| 188 | v = visitor_input_test_init(data, EnumOne_lookup[i]); |
| 189 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 190 | visit_type_EnumOne(v, NULL, &res, &err); |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 191 | g_assert(!err); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 192 | g_assert_cmpint(i, ==, res); |
| 193 | |
| 194 | visitor_input_teardown(data, NULL); |
| 195 | } |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 198 | /* Try to crash the visitors */ |
| 199 | static void test_visitor_in_fuzz(TestInputVisitorData *data, |
| 200 | const void *unused) |
| 201 | { |
| 202 | int64_t ires; |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 203 | intList *ilres; |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 204 | bool bres; |
| 205 | double nres; |
| 206 | char *sres; |
| 207 | EnumOne eres; |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 208 | Visitor *v; |
| 209 | unsigned int i; |
| 210 | char buf[10000]; |
| 211 | |
| 212 | for (i = 0; i < 100; i++) { |
| 213 | unsigned int j; |
| 214 | |
| 215 | j = g_test_rand_int_range(0, sizeof(buf) - 1); |
| 216 | |
| 217 | buf[j] = '\0'; |
| 218 | |
| 219 | if (j != 0) { |
| 220 | for (j--; j != 0; j--) { |
| 221 | buf[j - 1] = (char)g_test_rand_int_range(0, 256); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | v = visitor_input_test_init(data, buf); |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 226 | visit_type_int(v, NULL, &ires, NULL); |
Hu Tao | cac124d | 2014-06-10 19:15:26 +0800 | [diff] [blame] | 227 | visitor_input_teardown(data, NULL); |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 228 | |
| 229 | v = visitor_input_test_init(data, buf); |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 230 | visit_type_intList(v, NULL, &ilres, NULL); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 231 | visitor_input_teardown(data, NULL); |
| 232 | |
| 233 | v = visitor_input_test_init(data, buf); |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 234 | visit_type_bool(v, NULL, &bres, NULL); |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 235 | visitor_input_teardown(data, NULL); |
| 236 | |
| 237 | v = visitor_input_test_init(data, buf); |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 238 | visit_type_number(v, NULL, &nres, NULL); |
Hu Tao | cac124d | 2014-06-10 19:15:26 +0800 | [diff] [blame] | 239 | visitor_input_teardown(data, NULL); |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 240 | |
| 241 | v = visitor_input_test_init(data, buf); |
Peter Maydell | 0184543 | 2013-02-05 20:44:23 +0000 | [diff] [blame] | 242 | sres = NULL; |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 243 | visit_type_str(v, NULL, &sres, NULL); |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 244 | g_free(sres); |
Hu Tao | cac124d | 2014-06-10 19:15:26 +0800 | [diff] [blame] | 245 | visitor_input_teardown(data, NULL); |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 246 | |
| 247 | v = visitor_input_test_init(data, buf); |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 248 | visit_type_EnumOne(v, NULL, &eres, NULL); |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 249 | visitor_input_teardown(data, NULL); |
| 250 | } |
| 251 | } |
| 252 | |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 253 | static void input_visitor_test_add(const char *testpath, |
| 254 | TestInputVisitorData *data, |
| 255 | void (*test_func)(TestInputVisitorData *data, const void *user_data)) |
| 256 | { |
| 257 | g_test_add(testpath, TestInputVisitorData, data, NULL, test_func, |
| 258 | visitor_input_teardown); |
| 259 | } |
| 260 | |
| 261 | int main(int argc, char **argv) |
| 262 | { |
| 263 | TestInputVisitorData in_visitor_data; |
| 264 | |
| 265 | g_test_init(&argc, &argv, NULL); |
| 266 | |
| 267 | input_visitor_test_add("/string-visitor/input/int", |
| 268 | &in_visitor_data, test_visitor_in_int); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 269 | input_visitor_test_add("/string-visitor/input/intList", |
| 270 | &in_visitor_data, test_visitor_in_intList); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 271 | input_visitor_test_add("/string-visitor/input/bool", |
| 272 | &in_visitor_data, test_visitor_in_bool); |
| 273 | input_visitor_test_add("/string-visitor/input/number", |
| 274 | &in_visitor_data, test_visitor_in_number); |
| 275 | input_visitor_test_add("/string-visitor/input/string", |
| 276 | &in_visitor_data, test_visitor_in_string); |
| 277 | input_visitor_test_add("/string-visitor/input/enum", |
| 278 | &in_visitor_data, test_visitor_in_enum); |
Blue Swirl | 3f0f31a | 2013-01-19 15:43:53 +0000 | [diff] [blame] | 279 | input_visitor_test_add("/string-visitor/input/fuzz", |
| 280 | &in_visitor_data, test_visitor_in_fuzz); |
Paolo Bonzini | 2d7799f | 2012-02-09 11:21:03 +0100 | [diff] [blame] | 281 | |
| 282 | g_test_run(); |
| 283 | |
| 284 | return 0; |
| 285 | } |