2 * Test code for VMState
4 * Copyright (c) 2013 Red Hat Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
27 #include "qemu-common.h"
28 #include "migration/migration.h"
29 #include "migration/vmstate.h"
30 #include "qemu/coroutine.h"
31 #include "io/channel-file.h"
33 static char temp_file[] = "/tmp/vmst.test.XXXXXX";
36 /* Fake yield_until_fd_readable() implementation so we don't have to pull the
37 * coroutine code as dependency.
39 void yield_until_fd_readable(int fd)
44 select(fd + 1, &fds, NULL, NULL, NULL);
48 /* Duplicate temp_fd and seek to the beginning of the file */
49 static QEMUFile *open_test_file(bool write)
51 int fd = dup(temp_fd);
55 lseek(fd, 0, SEEK_SET);
57 g_assert_cmpint(ftruncate(fd, 0), ==, 0);
59 ioc = QIO_CHANNEL(qio_channel_file_new_fd(fd));
61 f = qemu_fopen_channel_output(ioc);
63 f = qemu_fopen_channel_input(ioc);
65 object_unref(OBJECT(ioc));
69 #define SUCCESS(val) \
70 g_assert_cmpint((val), ==, 0)
72 #define FAILURE(val) \
73 g_assert_cmpint((val), !=, 0)
75 static void save_vmstate(const VMStateDescription *desc, void *obj)
77 QEMUFile *f = open_test_file(true);
79 /* Save file with vmstate */
80 vmstate_save_state(f, desc, obj, NULL);
81 qemu_put_byte(f, QEMU_VM_EOF);
82 g_assert(!qemu_file_get_error(f));
86 static void save_buffer(const uint8_t *buf, size_t buf_size)
88 QEMUFile *fsave = open_test_file(true);
89 qemu_put_buffer(fsave, buf, buf_size);
93 static void compare_vmstate(uint8_t *wire, size_t size)
95 QEMUFile *f = open_test_file(false);
98 /* read back as binary */
100 g_assert_cmpint(qemu_get_buffer(f, result, sizeof(result)), ==,
102 g_assert(!qemu_file_get_error(f));
104 /* Compare that what is on the file is the same that what we
105 expected to be there */
106 SUCCESS(memcmp(result, wire, sizeof(result)));
110 g_assert_cmpint(qemu_file_get_error(f), ==, -EIO);
115 static int load_vmstate_one(const VMStateDescription *desc, void *obj,
116 int version, uint8_t *wire, size_t size)
121 f = open_test_file(true);
122 qemu_put_buffer(f, wire, size);
125 f = open_test_file(false);
126 ret = vmstate_load_state(f, desc, obj, version);
128 g_assert(qemu_file_get_error(f));
130 g_assert(!qemu_file_get_error(f));
137 static int load_vmstate(const VMStateDescription *desc,
138 void *obj, void *obj_clone,
139 void (*obj_copy)(void *, void*),
140 int version, uint8_t *wire, size_t size)
142 /* We test with zero size */
143 obj_copy(obj_clone, obj);
144 FAILURE(load_vmstate_one(desc, obj, version, wire, 0));
146 /* Stream ends with QEMU_EOF, so we need at least 3 bytes to be
147 * able to test in the middle */
151 /* We test with size - 2. We can't test size - 1 due to EOF tricks */
152 obj_copy(obj, obj_clone);
153 FAILURE(load_vmstate_one(desc, obj, version, wire, size - 2));
155 /* Test with size/2, first half of real state */
156 obj_copy(obj, obj_clone);
157 FAILURE(load_vmstate_one(desc, obj, version, wire, size/2));
159 /* Test with size/2, second half of real state */
160 obj_copy(obj, obj_clone);
161 FAILURE(load_vmstate_one(desc, obj, version, wire + (size/2), size/2));
164 obj_copy(obj, obj_clone);
165 return load_vmstate_one(desc, obj, version, wire, size);
168 /* Test struct that we are going to use for our tests */
170 typedef struct TestSimple {
177 int16_t i16_1, i16_2;
178 int32_t i32_1, i32_2;
179 int64_t i64_1, i64_2;
182 /* Object instantiation, we are going to use it in more than one test */
184 TestSimple obj_simple = {
201 /* Description of the values. If you add a primitive type
202 you are expected to add a test here */
204 static const VMStateDescription vmstate_simple_primitive = {
205 .name = "simple/primitive",
207 .minimum_version_id = 1,
208 .fields = (VMStateField[]) {
209 VMSTATE_BOOL(b_1, TestSimple),
210 VMSTATE_BOOL(b_2, TestSimple),
211 VMSTATE_UINT8(u8_1, TestSimple),
212 VMSTATE_UINT16(u16_1, TestSimple),
213 VMSTATE_UINT32(u32_1, TestSimple),
214 VMSTATE_UINT64(u64_1, TestSimple),
215 VMSTATE_INT8(i8_1, TestSimple),
216 VMSTATE_INT8(i8_2, TestSimple),
217 VMSTATE_INT16(i16_1, TestSimple),
218 VMSTATE_INT16(i16_2, TestSimple),
219 VMSTATE_INT32(i32_1, TestSimple),
220 VMSTATE_INT32(i32_2, TestSimple),
221 VMSTATE_INT64(i64_1, TestSimple),
222 VMSTATE_INT64(i64_2, TestSimple),
223 VMSTATE_END_OF_LIST()
227 /* It describes what goes through the wire. Our tests are basically:
230 - save a struct a vmstate to a file
231 - read that file back (binary read, no vmstate)
232 - compare it with what we expect to be on the wire
234 - save to the file what we expect to be on the wire
235 - read struct back with vmstate in a different
236 - compare back with the original struct
239 uint8_t wire_simple_primitive[] = {
243 /* u16_1 */ 0x02, 0x00,
244 /* u32_1 */ 0x00, 0x01, 0x11, 0x70,
245 /* u64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
248 /* i16_1 */ 0x02, 0x00,
249 /* i16_2 */ 0xfe, 0x0,
250 /* i32_1 */ 0x00, 0x01, 0x11, 0x70,
251 /* i32_2 */ 0xff, 0xfe, 0xee, 0x90,
252 /* i64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
253 /* i64_2 */ 0xff, 0xff, 0xff, 0xff, 0xff, 0x47, 0x0b, 0x84,
254 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
257 static void obj_simple_copy(void *target, void *source)
259 memcpy(target, source, sizeof(TestSimple));
262 static void test_simple_primitive(void)
264 TestSimple obj, obj_clone;
266 memset(&obj, 0, sizeof(obj));
267 save_vmstate(&vmstate_simple_primitive, &obj_simple);
269 compare_vmstate(wire_simple_primitive, sizeof(wire_simple_primitive));
271 SUCCESS(load_vmstate(&vmstate_simple_primitive, &obj, &obj_clone,
272 obj_simple_copy, 1, wire_simple_primitive,
273 sizeof(wire_simple_primitive)));
275 #define FIELD_EQUAL(name) g_assert_cmpint(obj.name, ==, obj_simple.name)
294 typedef struct TestStruct {
300 static const VMStateDescription vmstate_versioned = {
301 .name = "test/versioned",
303 .minimum_version_id = 1,
304 .fields = (VMStateField[]) {
305 VMSTATE_UINT32(a, TestStruct),
306 VMSTATE_UINT32_V(b, TestStruct, 2), /* Versioned field in the middle, so
307 * we catch bugs more easily.
309 VMSTATE_UINT32(c, TestStruct),
310 VMSTATE_UINT64(d, TestStruct),
311 VMSTATE_UINT32_V(e, TestStruct, 2),
312 VMSTATE_UINT64_V(f, TestStruct, 2),
313 VMSTATE_END_OF_LIST()
317 static void test_load_v1(void)
322 0, 0, 0, 0, 0, 0, 0, 40, /* d */
323 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
325 save_buffer(buf, sizeof(buf));
327 QEMUFile *loading = open_test_file(false);
328 TestStruct obj = { .b = 200, .e = 500, .f = 600 };
329 vmstate_load_state(loading, &vmstate_versioned, &obj, 1);
330 g_assert(!qemu_file_get_error(loading));
331 g_assert_cmpint(obj.a, ==, 10);
332 g_assert_cmpint(obj.b, ==, 200);
333 g_assert_cmpint(obj.c, ==, 30);
334 g_assert_cmpint(obj.d, ==, 40);
335 g_assert_cmpint(obj.e, ==, 500);
336 g_assert_cmpint(obj.f, ==, 600);
337 qemu_fclose(loading);
340 static void test_load_v2(void)
346 0, 0, 0, 0, 0, 0, 0, 40, /* d */
348 0, 0, 0, 0, 0, 0, 0, 60, /* f */
349 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
351 save_buffer(buf, sizeof(buf));
353 QEMUFile *loading = open_test_file(false);
355 vmstate_load_state(loading, &vmstate_versioned, &obj, 2);
356 g_assert_cmpint(obj.a, ==, 10);
357 g_assert_cmpint(obj.b, ==, 20);
358 g_assert_cmpint(obj.c, ==, 30);
359 g_assert_cmpint(obj.d, ==, 40);
360 g_assert_cmpint(obj.e, ==, 50);
361 g_assert_cmpint(obj.f, ==, 60);
362 qemu_fclose(loading);
365 static bool test_skip(void *opaque, int version_id)
367 TestStruct *t = (TestStruct *)opaque;
371 static const VMStateDescription vmstate_skipping = {
374 .minimum_version_id = 1,
375 .fields = (VMStateField[]) {
376 VMSTATE_UINT32(a, TestStruct),
377 VMSTATE_UINT32(b, TestStruct),
378 VMSTATE_UINT32_TEST(c, TestStruct, test_skip),
379 VMSTATE_UINT64(d, TestStruct),
380 VMSTATE_UINT32_TEST(e, TestStruct, test_skip),
381 VMSTATE_UINT64_V(f, TestStruct, 2),
382 VMSTATE_END_OF_LIST()
387 static void test_save_noskip(void)
389 QEMUFile *fsave = open_test_file(true);
390 TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
392 vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
393 g_assert(!qemu_file_get_error(fsave));
395 uint8_t expected[] = {
399 0, 0, 0, 0, 0, 0, 0, 4, /* d */
401 0, 0, 0, 0, 0, 0, 0, 6, /* f */
405 compare_vmstate(expected, sizeof(expected));
408 static void test_save_skip(void)
410 QEMUFile *fsave = open_test_file(true);
411 TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
413 vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
414 g_assert(!qemu_file_get_error(fsave));
416 uint8_t expected[] = {
419 0, 0, 0, 0, 0, 0, 0, 4, /* d */
420 0, 0, 0, 0, 0, 0, 0, 6, /* f */
424 compare_vmstate(expected, sizeof(expected));
427 static void test_load_noskip(void)
433 0, 0, 0, 0, 0, 0, 0, 40, /* d */
435 0, 0, 0, 0, 0, 0, 0, 60, /* f */
436 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
438 save_buffer(buf, sizeof(buf));
440 QEMUFile *loading = open_test_file(false);
441 TestStruct obj = { .skip_c_e = false };
442 vmstate_load_state(loading, &vmstate_skipping, &obj, 2);
443 g_assert(!qemu_file_get_error(loading));
444 g_assert_cmpint(obj.a, ==, 10);
445 g_assert_cmpint(obj.b, ==, 20);
446 g_assert_cmpint(obj.c, ==, 30);
447 g_assert_cmpint(obj.d, ==, 40);
448 g_assert_cmpint(obj.e, ==, 50);
449 g_assert_cmpint(obj.f, ==, 60);
450 qemu_fclose(loading);
453 static void test_load_skip(void)
458 0, 0, 0, 0, 0, 0, 0, 40, /* d */
459 0, 0, 0, 0, 0, 0, 0, 60, /* f */
460 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
462 save_buffer(buf, sizeof(buf));
464 QEMUFile *loading = open_test_file(false);
465 TestStruct obj = { .skip_c_e = true, .c = 300, .e = 500 };
466 vmstate_load_state(loading, &vmstate_skipping, &obj, 2);
467 g_assert(!qemu_file_get_error(loading));
468 g_assert_cmpint(obj.a, ==, 10);
469 g_assert_cmpint(obj.b, ==, 20);
470 g_assert_cmpint(obj.c, ==, 300);
471 g_assert_cmpint(obj.d, ==, 40);
472 g_assert_cmpint(obj.e, ==, 500);
473 g_assert_cmpint(obj.f, ==, 60);
474 qemu_fclose(loading);
482 const VMStateDescription vmsd_tst = {
485 .minimum_version_id = 1,
486 .fields = (VMStateField[]) {
487 VMSTATE_INT32(i, TestStructTriv),
488 VMSTATE_END_OF_LIST()
495 TestStructTriv *ar[AR_SIZE];
496 } TestArrayOfPtrToStuct;
498 const VMStateDescription vmsd_arps = {
501 .minimum_version_id = 1,
502 .fields = (VMStateField[]) {
503 VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(ar, TestArrayOfPtrToStuct,
504 AR_SIZE, 0, vmsd_tst, TestStructTriv),
505 VMSTATE_END_OF_LIST()
508 static void test_arr_ptr_str_no0_save(void)
510 TestStructTriv ar[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
511 TestArrayOfPtrToStuct sample = {.ar = {&ar[0], &ar[1], &ar[2], &ar[3]} };
512 uint8_t wire_sample[] = {
513 0x00, 0x00, 0x00, 0x00,
514 0x00, 0x00, 0x00, 0x01,
515 0x00, 0x00, 0x00, 0x02,
516 0x00, 0x00, 0x00, 0x03,
520 save_vmstate(&vmsd_arps, &sample);
521 compare_vmstate(wire_sample, sizeof(wire_sample));
524 static void test_arr_ptr_str_no0_load(void)
526 TestStructTriv ar_gt[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
527 TestStructTriv ar[AR_SIZE] = {};
528 TestArrayOfPtrToStuct obj = {.ar = {&ar[0], &ar[1], &ar[2], &ar[3]} };
530 uint8_t wire_sample[] = {
531 0x00, 0x00, 0x00, 0x00,
532 0x00, 0x00, 0x00, 0x01,
533 0x00, 0x00, 0x00, 0x02,
534 0x00, 0x00, 0x00, 0x03,
538 save_buffer(wire_sample, sizeof(wire_sample));
539 SUCCESS(load_vmstate_one(&vmsd_arps, &obj, 1,
540 wire_sample, sizeof(wire_sample)));
541 for (idx = 0; idx < AR_SIZE; ++idx) {
542 /* compare the target array ar with the ground truth array ar_gt */
543 g_assert_cmpint(ar_gt[idx].i, ==, ar[idx].i);
547 int main(int argc, char **argv)
549 temp_fd = mkstemp(temp_file);
551 module_call_init(MODULE_INIT_QOM);
553 g_test_init(&argc, &argv, NULL);
554 g_test_add_func("/vmstate/simple/primitive", test_simple_primitive);
555 g_test_add_func("/vmstate/versioned/load/v1", test_load_v1);
556 g_test_add_func("/vmstate/versioned/load/v2", test_load_v2);
557 g_test_add_func("/vmstate/field_exists/load/noskip", test_load_noskip);
558 g_test_add_func("/vmstate/field_exists/load/skip", test_load_skip);
559 g_test_add_func("/vmstate/field_exists/save/noskip", test_save_noskip);
560 g_test_add_func("/vmstate/field_exists/save/skip", test_save_skip);
561 g_test_add_func("/vmstate/array/ptr/str/no0/save",
562 test_arr_ptr_str_no0_save);
563 g_test_add_func("/vmstate/array/ptr/str/no0/load",
564 test_arr_ptr_str_no0_load);