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"
28 #include "qemu-common.h"
29 #include "migration/migration.h"
30 #include "migration/vmstate.h"
31 #include "qemu/coroutine.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 * Some tests use 'open_test_file' to work on a real fd, some use
49 * an in memory file (QEMUSizedBuffer+qemu_bufopen); we could pick one
50 * but this way we test both.
53 /* Duplicate temp_fd and seek to the beginning of the file */
54 static QEMUFile
*open_test_file(bool write
)
56 int fd
= dup(temp_fd
);
57 lseek(fd
, 0, SEEK_SET
);
59 g_assert_cmpint(ftruncate(fd
, 0), ==, 0);
61 return qemu_fdopen(fd
, write
? "wb" : "rb");
65 * Check that the contents of the memory-buffered file f match
66 * the given size/data.
68 static void check_mem_file(QEMUFile
*f
, void *data
, size_t size
)
70 uint8_t *result
= g_malloc(size
);
71 const QEMUSizedBuffer
*qsb
= qemu_buf_get(f
);
72 g_assert_cmpint(qsb_get_length(qsb
), ==, size
);
73 g_assert_cmpint(qsb_get_buffer(qsb
, 0, size
, result
), ==, size
);
74 g_assert_cmpint(memcmp(result
, data
, size
), ==, 0);
78 #define SUCCESS(val) \
79 g_assert_cmpint((val), ==, 0)
81 #define FAILURE(val) \
82 g_assert_cmpint((val), !=, 0)
84 static void save_vmstate(const VMStateDescription
*desc
, void *obj
)
86 QEMUFile
*f
= open_test_file(true);
88 /* Save file with vmstate */
89 vmstate_save_state(f
, desc
, obj
, NULL
);
90 qemu_put_byte(f
, QEMU_VM_EOF
);
91 g_assert(!qemu_file_get_error(f
));
95 static void compare_vmstate(uint8_t *wire
, size_t size
)
97 QEMUFile
*f
= open_test_file(false);
100 /* read back as binary */
102 g_assert_cmpint(qemu_get_buffer(f
, result
, sizeof(result
)), ==,
104 g_assert(!qemu_file_get_error(f
));
106 /* Compare that what is on the file is the same that what we
107 expected to be there */
108 SUCCESS(memcmp(result
, wire
, sizeof(result
)));
112 g_assert_cmpint(qemu_file_get_error(f
), ==, -EIO
);
117 static int load_vmstate_one(const VMStateDescription
*desc
, void *obj
,
118 int version
, uint8_t *wire
, size_t size
)
123 f
= open_test_file(true);
124 qemu_put_buffer(f
, wire
, size
);
127 f
= open_test_file(false);
128 ret
= vmstate_load_state(f
, desc
, obj
, version
);
130 g_assert(qemu_file_get_error(f
));
132 g_assert(!qemu_file_get_error(f
));
139 static int load_vmstate(const VMStateDescription
*desc
,
140 void *obj
, void *obj_clone
,
141 void (*obj_copy
)(void *, void*),
142 int version
, uint8_t *wire
, size_t size
)
144 /* We test with zero size */
145 obj_copy(obj_clone
, obj
);
146 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
, 0));
148 /* Stream ends with QEMU_EOF, so we need at least 3 bytes to be
149 * able to test in the middle */
153 /* We test with size - 2. We can't test size - 1 due to EOF tricks */
154 obj_copy(obj
, obj_clone
);
155 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
, size
- 2));
157 /* Test with size/2, first half of real state */
158 obj_copy(obj
, obj_clone
);
159 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
, size
/2));
161 /* Test with size/2, second half of real state */
162 obj_copy(obj
, obj_clone
);
163 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
+ (size
/2), size
/2));
166 obj_copy(obj
, obj_clone
);
167 return load_vmstate_one(desc
, obj
, version
, wire
, size
);
170 /* Test struct that we are going to use for our tests */
172 typedef struct TestSimple
{
179 int16_t i16_1
, i16_2
;
180 int32_t i32_1
, i32_2
;
181 int64_t i64_1
, i64_2
;
184 /* Object instantiation, we are going to use it in more than one test */
186 TestSimple obj_simple
= {
203 /* Description of the values. If you add a primitive type
204 you are expected to add a test here */
206 static const VMStateDescription vmstate_simple_primitive
= {
207 .name
= "simple/primitive",
209 .minimum_version_id
= 1,
210 .fields
= (VMStateField
[]) {
211 VMSTATE_BOOL(b_1
, TestSimple
),
212 VMSTATE_BOOL(b_2
, TestSimple
),
213 VMSTATE_UINT8(u8_1
, TestSimple
),
214 VMSTATE_UINT16(u16_1
, TestSimple
),
215 VMSTATE_UINT32(u32_1
, TestSimple
),
216 VMSTATE_UINT64(u64_1
, TestSimple
),
217 VMSTATE_INT8(i8_1
, TestSimple
),
218 VMSTATE_INT8(i8_2
, TestSimple
),
219 VMSTATE_INT16(i16_1
, TestSimple
),
220 VMSTATE_INT16(i16_2
, TestSimple
),
221 VMSTATE_INT32(i32_1
, TestSimple
),
222 VMSTATE_INT32(i32_2
, TestSimple
),
223 VMSTATE_INT64(i64_1
, TestSimple
),
224 VMSTATE_INT64(i64_2
, TestSimple
),
225 VMSTATE_END_OF_LIST()
229 /* It describes what goes through the wire. Our tests are basically:
232 - save a struct a vmstate to a file
233 - read that file back (binary read, no vmstate)
234 - compare it with what we expect to be on the wire
236 - save to the file what we expect to be on the wire
237 - read struct back with vmstate in a different
238 - compare back with the original struct
241 uint8_t wire_simple_primitive
[] = {
245 /* u16_1 */ 0x02, 0x00,
246 /* u32_1 */ 0x00, 0x01, 0x11, 0x70,
247 /* u64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
250 /* i16_1 */ 0x02, 0x00,
251 /* i16_2 */ 0xfe, 0x0,
252 /* i32_1 */ 0x00, 0x01, 0x11, 0x70,
253 /* i32_2 */ 0xff, 0xfe, 0xee, 0x90,
254 /* i64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
255 /* i64_2 */ 0xff, 0xff, 0xff, 0xff, 0xff, 0x47, 0x0b, 0x84,
256 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
259 static void obj_simple_copy(void *target
, void *source
)
261 memcpy(target
, source
, sizeof(TestSimple
));
264 static void test_simple_primitive(void)
266 TestSimple obj
, obj_clone
;
268 memset(&obj
, 0, sizeof(obj
));
269 save_vmstate(&vmstate_simple_primitive
, &obj_simple
);
271 compare_vmstate(wire_simple_primitive
, sizeof(wire_simple_primitive
));
273 SUCCESS(load_vmstate(&vmstate_simple_primitive
, &obj
, &obj_clone
,
274 obj_simple_copy
, 1, wire_simple_primitive
,
275 sizeof(wire_simple_primitive
)));
277 #define FIELD_EQUAL(name) g_assert_cmpint(obj.name, ==, obj_simple.name)
296 typedef struct TestStruct
{
302 static const VMStateDescription vmstate_versioned
= {
303 .name
= "test/versioned",
305 .minimum_version_id
= 1,
306 .fields
= (VMStateField
[]) {
307 VMSTATE_UINT32(a
, TestStruct
),
308 VMSTATE_UINT32_V(b
, TestStruct
, 2), /* Versioned field in the middle, so
309 * we catch bugs more easily.
311 VMSTATE_UINT32(c
, TestStruct
),
312 VMSTATE_UINT64(d
, TestStruct
),
313 VMSTATE_UINT32_V(e
, TestStruct
, 2),
314 VMSTATE_UINT64_V(f
, TestStruct
, 2),
315 VMSTATE_END_OF_LIST()
319 static void test_load_v1(void)
321 QEMUFile
*fsave
= open_test_file(true);
325 0, 0, 0, 0, 0, 0, 0, 40, /* d */
326 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
328 qemu_put_buffer(fsave
, buf
, sizeof(buf
));
331 QEMUFile
*loading
= open_test_file(false);
332 TestStruct obj
= { .b
= 200, .e
= 500, .f
= 600 };
333 vmstate_load_state(loading
, &vmstate_versioned
, &obj
, 1);
334 g_assert(!qemu_file_get_error(loading
));
335 g_assert_cmpint(obj
.a
, ==, 10);
336 g_assert_cmpint(obj
.b
, ==, 200);
337 g_assert_cmpint(obj
.c
, ==, 30);
338 g_assert_cmpint(obj
.d
, ==, 40);
339 g_assert_cmpint(obj
.e
, ==, 500);
340 g_assert_cmpint(obj
.f
, ==, 600);
341 qemu_fclose(loading
);
344 static void test_load_v2(void)
346 QEMUFile
*fsave
= open_test_file(true);
351 0, 0, 0, 0, 0, 0, 0, 40, /* d */
353 0, 0, 0, 0, 0, 0, 0, 60, /* f */
354 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
356 qemu_put_buffer(fsave
, buf
, sizeof(buf
));
359 QEMUFile
*loading
= open_test_file(false);
361 vmstate_load_state(loading
, &vmstate_versioned
, &obj
, 2);
362 g_assert_cmpint(obj
.a
, ==, 10);
363 g_assert_cmpint(obj
.b
, ==, 20);
364 g_assert_cmpint(obj
.c
, ==, 30);
365 g_assert_cmpint(obj
.d
, ==, 40);
366 g_assert_cmpint(obj
.e
, ==, 50);
367 g_assert_cmpint(obj
.f
, ==, 60);
368 qemu_fclose(loading
);
371 static bool test_skip(void *opaque
, int version_id
)
373 TestStruct
*t
= (TestStruct
*)opaque
;
377 static const VMStateDescription vmstate_skipping
= {
380 .minimum_version_id
= 1,
381 .fields
= (VMStateField
[]) {
382 VMSTATE_UINT32(a
, TestStruct
),
383 VMSTATE_UINT32(b
, TestStruct
),
384 VMSTATE_UINT32_TEST(c
, TestStruct
, test_skip
),
385 VMSTATE_UINT64(d
, TestStruct
),
386 VMSTATE_UINT32_TEST(e
, TestStruct
, test_skip
),
387 VMSTATE_UINT64_V(f
, TestStruct
, 2),
388 VMSTATE_END_OF_LIST()
393 static void test_save_noskip(void)
395 QEMUFile
*fsave
= qemu_bufopen("w", NULL
);
396 TestStruct obj
= { .a
= 1, .b
= 2, .c
= 3, .d
= 4, .e
= 5, .f
= 6,
398 vmstate_save_state(fsave
, &vmstate_skipping
, &obj
, NULL
);
399 g_assert(!qemu_file_get_error(fsave
));
401 uint8_t expected
[] = {
405 0, 0, 0, 0, 0, 0, 0, 4, /* d */
407 0, 0, 0, 0, 0, 0, 0, 6, /* f */
409 check_mem_file(fsave
, expected
, sizeof(expected
));
413 static void test_save_skip(void)
415 QEMUFile
*fsave
= qemu_bufopen("w", NULL
);
416 TestStruct obj
= { .a
= 1, .b
= 2, .c
= 3, .d
= 4, .e
= 5, .f
= 6,
418 vmstate_save_state(fsave
, &vmstate_skipping
, &obj
, NULL
);
419 g_assert(!qemu_file_get_error(fsave
));
421 uint8_t expected
[] = {
424 0, 0, 0, 0, 0, 0, 0, 4, /* d */
425 0, 0, 0, 0, 0, 0, 0, 6, /* f */
427 check_mem_file(fsave
, expected
, sizeof(expected
));
432 static void test_load_noskip(void)
438 0, 0, 0, 0, 0, 0, 0, 40, /* d */
440 0, 0, 0, 0, 0, 0, 0, 60, /* f */
441 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
444 QEMUSizedBuffer
*qsb
= qsb_create(buf
, sizeof(buf
));
446 QEMUFile
*loading
= qemu_bufopen("r", qsb
);
447 TestStruct obj
= { .skip_c_e
= false };
448 vmstate_load_state(loading
, &vmstate_skipping
, &obj
, 2);
449 g_assert(!qemu_file_get_error(loading
));
450 g_assert_cmpint(obj
.a
, ==, 10);
451 g_assert_cmpint(obj
.b
, ==, 20);
452 g_assert_cmpint(obj
.c
, ==, 30);
453 g_assert_cmpint(obj
.d
, ==, 40);
454 g_assert_cmpint(obj
.e
, ==, 50);
455 g_assert_cmpint(obj
.f
, ==, 60);
456 qemu_fclose(loading
);
460 static void test_load_skip(void)
465 0, 0, 0, 0, 0, 0, 0, 40, /* d */
466 0, 0, 0, 0, 0, 0, 0, 60, /* f */
467 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
470 QEMUSizedBuffer
*qsb
= qsb_create(buf
, sizeof(buf
));
472 QEMUFile
*loading
= qemu_bufopen("r", qsb
);
473 TestStruct obj
= { .skip_c_e
= true, .c
= 300, .e
= 500 };
474 vmstate_load_state(loading
, &vmstate_skipping
, &obj
, 2);
475 g_assert(!qemu_file_get_error(loading
));
476 g_assert_cmpint(obj
.a
, ==, 10);
477 g_assert_cmpint(obj
.b
, ==, 20);
478 g_assert_cmpint(obj
.c
, ==, 300);
479 g_assert_cmpint(obj
.d
, ==, 40);
480 g_assert_cmpint(obj
.e
, ==, 500);
481 g_assert_cmpint(obj
.f
, ==, 60);
482 qemu_fclose(loading
);
486 int main(int argc
, char **argv
)
488 temp_fd
= mkstemp(temp_file
);
490 g_test_init(&argc
, &argv
, NULL
);
491 g_test_add_func("/vmstate/simple/primitive", test_simple_primitive
);
492 g_test_add_func("/vmstate/versioned/load/v1", test_load_v1
);
493 g_test_add_func("/vmstate/versioned/load/v2", test_load_v2
);
494 g_test_add_func("/vmstate/field_exists/load/noskip", test_load_noskip
);
495 g_test_add_func("/vmstate/field_exists/load/skip", test_load_skip
);
496 g_test_add_func("/vmstate/field_exists/save/noskip", test_save_noskip
);
497 g_test_add_func("/vmstate/field_exists/save/skip", test_save_skip
);