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