Laszlo Ersek | 3953e3a | 2013-08-20 00:35:40 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Options Visitor unit-tests. |
| 3 | * |
| 4 | * Copyright (C) 2013 Red Hat, Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Laszlo Ersek <lersek@redhat.com> (based on test-string-output-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 | |
| 15 | #include "qemu/config-file.h" /* qemu_add_opts() */ |
| 16 | #include "qemu/option.h" /* qemu_opts_parse() */ |
| 17 | #include "qapi/opts-visitor.h" /* opts_visitor_new() */ |
| 18 | #include "test-qapi-visit.h" /* visit_type_UserDefOptions() */ |
| 19 | #include "qapi/dealloc-visitor.h" /* qapi_dealloc_visitor_new() */ |
| 20 | |
| 21 | static QemuOptsList userdef_opts = { |
| 22 | .name = "userdef", |
| 23 | .head = QTAILQ_HEAD_INITIALIZER(userdef_opts.head), |
| 24 | .desc = { { 0 } } /* validated with OptsVisitor */ |
| 25 | }; |
| 26 | |
| 27 | /* fixture (= glib test case context) and test case manipulation */ |
| 28 | |
| 29 | typedef struct OptsVisitorFixture { |
| 30 | UserDefOptions *userdef; |
| 31 | Error *err; |
| 32 | } OptsVisitorFixture; |
| 33 | |
| 34 | |
| 35 | static void |
| 36 | setup_fixture(OptsVisitorFixture *f, gconstpointer test_data) |
| 37 | { |
| 38 | const char *opts_string = test_data; |
| 39 | QemuOpts *opts; |
| 40 | OptsVisitor *ov; |
| 41 | |
| 42 | opts = qemu_opts_parse(qemu_find_opts("userdef"), opts_string, 0); |
| 43 | g_assert(opts != NULL); |
| 44 | |
| 45 | ov = opts_visitor_new(opts); |
| 46 | visit_type_UserDefOptions(opts_get_visitor(ov), &f->userdef, NULL, |
| 47 | &f->err); |
| 48 | opts_visitor_cleanup(ov); |
| 49 | qemu_opts_del(opts); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | static void |
| 54 | teardown_fixture(OptsVisitorFixture *f, gconstpointer test_data) |
| 55 | { |
| 56 | if (f->userdef != NULL) { |
| 57 | QapiDeallocVisitor *dv; |
| 58 | |
| 59 | dv = qapi_dealloc_visitor_new(); |
| 60 | visit_type_UserDefOptions(qapi_dealloc_get_visitor(dv), &f->userdef, |
| 61 | NULL, NULL); |
| 62 | qapi_dealloc_visitor_cleanup(dv); |
| 63 | } |
| 64 | error_free(f->err); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | static void |
| 69 | add_test(const char *testpath, |
| 70 | void (*test_func)(OptsVisitorFixture *f, gconstpointer test_data), |
| 71 | gconstpointer test_data) |
| 72 | { |
| 73 | g_test_add(testpath, OptsVisitorFixture, test_data, setup_fixture, |
| 74 | test_func, teardown_fixture); |
| 75 | } |
| 76 | |
| 77 | /* test output evaluation */ |
| 78 | |
| 79 | static void |
| 80 | expect_ok(OptsVisitorFixture *f, gconstpointer test_data) |
| 81 | { |
| 82 | g_assert(f->err == NULL); |
| 83 | g_assert(f->userdef != NULL); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | static void |
| 88 | expect_fail(OptsVisitorFixture *f, gconstpointer test_data) |
| 89 | { |
| 90 | g_assert(f->err != NULL); |
| 91 | |
| 92 | /* The error message is printed when this test utility is invoked directly |
| 93 | * (ie. without gtester) and the --verbose flag is passed: |
| 94 | * |
| 95 | * tests/test-opts-visitor --verbose |
| 96 | */ |
| 97 | g_test_message("'%s': %s", (const char *)test_data, |
| 98 | error_get_pretty(f->err)); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | static void |
| 103 | test_value(OptsVisitorFixture *f, gconstpointer test_data) |
| 104 | { |
| 105 | uint64_t magic, bitval; |
| 106 | intList *i64; |
| 107 | uint64List *u64; |
| 108 | uint16List *u16; |
| 109 | |
| 110 | expect_ok(f, test_data); |
| 111 | |
| 112 | magic = 0; |
| 113 | for (i64 = f->userdef->i64; i64 != NULL; i64 = i64->next) { |
| 114 | g_assert(-16 <= i64->value && i64->value < 64-16); |
| 115 | bitval = 1ull << (i64->value + 16); |
| 116 | g_assert((magic & bitval) == 0); |
| 117 | magic |= bitval; |
| 118 | } |
| 119 | g_assert(magic == 0xDEADBEEF); |
| 120 | |
| 121 | magic = 0; |
| 122 | for (u64 = f->userdef->u64; u64 != NULL; u64 = u64->next) { |
| 123 | g_assert(u64->value < 64); |
| 124 | bitval = 1ull << u64->value; |
| 125 | g_assert((magic & bitval) == 0); |
| 126 | magic |= bitval; |
| 127 | } |
Stefan Hajnoczi | 5cb6be2 | 2013-11-05 17:42:48 +0100 | [diff] [blame] | 128 | g_assert(magic == 0xBADC0FFEE0DDF00DULL); |
Laszlo Ersek | 3953e3a | 2013-08-20 00:35:40 +0200 | [diff] [blame] | 129 | |
| 130 | magic = 0; |
| 131 | for (u16 = f->userdef->u16; u16 != NULL; u16 = u16->next) { |
| 132 | g_assert(u16->value < 64); |
| 133 | bitval = 1ull << u16->value; |
| 134 | g_assert((magic & bitval) == 0); |
| 135 | magic |= bitval; |
| 136 | } |
| 137 | g_assert(magic == 0xD15EA5E); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | static void |
| 142 | expect_i64_min(OptsVisitorFixture *f, gconstpointer test_data) |
| 143 | { |
| 144 | expect_ok(f, test_data); |
| 145 | g_assert(f->userdef->has_i64); |
| 146 | g_assert(f->userdef->i64->next == NULL); |
| 147 | g_assert(f->userdef->i64->value == INT64_MIN); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | static void |
| 152 | expect_i64_max(OptsVisitorFixture *f, gconstpointer test_data) |
| 153 | { |
| 154 | expect_ok(f, test_data); |
| 155 | g_assert(f->userdef->has_i64); |
| 156 | g_assert(f->userdef->i64->next == NULL); |
| 157 | g_assert(f->userdef->i64->value == INT64_MAX); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | static void |
| 162 | expect_zero(OptsVisitorFixture *f, gconstpointer test_data) |
| 163 | { |
| 164 | expect_ok(f, test_data); |
| 165 | g_assert(f->userdef->has_u64); |
| 166 | g_assert(f->userdef->u64->next == NULL); |
| 167 | g_assert(f->userdef->u64->value == 0); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | static void |
| 172 | expect_u64_max(OptsVisitorFixture *f, gconstpointer test_data) |
| 173 | { |
| 174 | expect_ok(f, test_data); |
| 175 | g_assert(f->userdef->has_u64); |
| 176 | g_assert(f->userdef->u64->next == NULL); |
| 177 | g_assert(f->userdef->u64->value == UINT64_MAX); |
| 178 | } |
| 179 | |
| 180 | /* test cases */ |
| 181 | |
| 182 | int |
| 183 | main(int argc, char **argv) |
| 184 | { |
| 185 | g_test_init(&argc, &argv, NULL); |
| 186 | |
| 187 | qemu_add_opts(&userdef_opts); |
| 188 | |
| 189 | /* Three hexadecimal magic numbers, "dead beef", "bad coffee, odd food" and |
| 190 | * "disease", from |
| 191 | * <http://en.wikipedia.org/wiki/Magic_number_%28programming%29>, were |
| 192 | * converted to binary and dissected into bit ranges. Each magic number is |
| 193 | * going to be recomposed using the lists called "i64", "u64" and "u16", |
| 194 | * respectively. |
| 195 | * |
| 196 | * (Note that these types pertain to the individual bit shift counts, not |
| 197 | * the magic numbers themselves; the intent is to exercise opts_type_int() |
| 198 | * and opts_type_uint64().) |
| 199 | * |
| 200 | * The "i64" shift counts have been decreased by 16 (decimal) in order to |
| 201 | * test negative values as well. Finally, the full list of QemuOpt elements |
| 202 | * has been permuted with "shuf". |
| 203 | * |
| 204 | * Both "i64" and "u64" have some (distinct) single-element ranges |
| 205 | * represented as both "a" and "a-a". "u16" is a special case of "i64" (see |
| 206 | * visit_type_uint16()), so it wouldn't add a separate test in this regard. |
| 207 | */ |
| 208 | |
| 209 | add_test("/visitor/opts/flatten/value", &test_value, |
| 210 | "i64=-1-0,u64=12-16,u64=2-3,i64=-11--9,u64=57,u16=9,i64=5-5," |
| 211 | "u16=1-4,u16=20,u64=63-63,i64=-16--13,u64=50-52,i64=14-15,u16=11," |
| 212 | "i64=7,u16=18,i64=2-3,u16=6,u64=54-55,u64=0,u64=18-20,u64=33-43," |
| 213 | "i64=9-12,u16=26-27,u64=59-61,u16=13-16,u64=29-31,u64=22-23," |
| 214 | "u16=24,i64=-7--3"); |
| 215 | |
| 216 | add_test("/visitor/opts/i64/val1/errno", &expect_fail, |
| 217 | "i64=0x8000000000000000"); |
| 218 | add_test("/visitor/opts/i64/val1/empty", &expect_fail, "i64="); |
| 219 | add_test("/visitor/opts/i64/val1/trailing", &expect_fail, "i64=5z"); |
| 220 | add_test("/visitor/opts/i64/nonlist", &expect_fail, "i64x=5-6"); |
| 221 | add_test("/visitor/opts/i64/val2/errno", &expect_fail, |
| 222 | "i64=0x7fffffffffffffff-0x8000000000000000"); |
| 223 | add_test("/visitor/opts/i64/val2/empty", &expect_fail, "i64=5-"); |
| 224 | add_test("/visitor/opts/i64/val2/trailing", &expect_fail, "i64=5-6z"); |
| 225 | add_test("/visitor/opts/i64/range/empty", &expect_fail, "i64=6-5"); |
| 226 | add_test("/visitor/opts/i64/range/minval", &expect_i64_min, |
| 227 | "i64=-0x8000000000000000--0x8000000000000000"); |
| 228 | add_test("/visitor/opts/i64/range/maxval", &expect_i64_max, |
| 229 | "i64=0x7fffffffffffffff-0x7fffffffffffffff"); |
| 230 | |
| 231 | add_test("/visitor/opts/u64/val1/errno", &expect_fail, "u64=-1"); |
| 232 | add_test("/visitor/opts/u64/val1/empty", &expect_fail, "u64="); |
| 233 | add_test("/visitor/opts/u64/val1/trailing", &expect_fail, "u64=5z"); |
| 234 | add_test("/visitor/opts/u64/nonlist", &expect_fail, "u64x=5-6"); |
| 235 | add_test("/visitor/opts/u64/val2/errno", &expect_fail, |
| 236 | "u64=0xffffffffffffffff-0x10000000000000000"); |
| 237 | add_test("/visitor/opts/u64/val2/empty", &expect_fail, "u64=5-"); |
| 238 | add_test("/visitor/opts/u64/val2/trailing", &expect_fail, "u64=5-6z"); |
| 239 | add_test("/visitor/opts/u64/range/empty", &expect_fail, "u64=6-5"); |
| 240 | add_test("/visitor/opts/u64/range/minval", &expect_zero, "u64=0-0"); |
| 241 | add_test("/visitor/opts/u64/range/maxval", &expect_u64_max, |
| 242 | "u64=0xffffffffffffffff-0xffffffffffffffff"); |
| 243 | |
| 244 | /* Test maximum range sizes. The macro value is open-coded here |
| 245 | * *intentionally*; the test case must use concrete values by design. If |
| 246 | * OPTS_VISITOR_RANGE_MAX is changed, the following values need to be |
| 247 | * recalculated as well. The assert and this comment should help with it. |
| 248 | */ |
| 249 | g_assert(OPTS_VISITOR_RANGE_MAX == 65536); |
| 250 | |
| 251 | /* The unsigned case is simple, a u64-u64 difference can always be |
| 252 | * represented as a u64. |
| 253 | */ |
| 254 | add_test("/visitor/opts/u64/range/max", &expect_ok, "u64=0-65535"); |
| 255 | add_test("/visitor/opts/u64/range/2big", &expect_fail, "u64=0-65536"); |
| 256 | |
| 257 | /* The same cannot be said about an i64-i64 difference. */ |
| 258 | add_test("/visitor/opts/i64/range/max/pos/a", &expect_ok, |
| 259 | "i64=0x7fffffffffff0000-0x7fffffffffffffff"); |
| 260 | add_test("/visitor/opts/i64/range/max/pos/b", &expect_ok, |
| 261 | "i64=0x7ffffffffffeffff-0x7ffffffffffffffe"); |
| 262 | add_test("/visitor/opts/i64/range/2big/pos", &expect_fail, |
| 263 | "i64=0x7ffffffffffeffff-0x7fffffffffffffff"); |
| 264 | add_test("/visitor/opts/i64/range/max/neg/a", &expect_ok, |
| 265 | "i64=-0x8000000000000000--0x7fffffffffff0001"); |
| 266 | add_test("/visitor/opts/i64/range/max/neg/b", &expect_ok, |
| 267 | "i64=-0x7fffffffffffffff--0x7fffffffffff0000"); |
| 268 | add_test("/visitor/opts/i64/range/2big/neg", &expect_fail, |
| 269 | "i64=-0x8000000000000000--0x7fffffffffff0000"); |
| 270 | add_test("/visitor/opts/i64/range/2big/full", &expect_fail, |
| 271 | "i64=-0x8000000000000000-0x7fffffffffffffff"); |
| 272 | |
| 273 | g_test_run(); |
| 274 | return 0; |
| 275 | } |