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
27 #include "qemu-common.h"
28 #include "migration/migration.h"
29 #include "migration/vmstate.h"
30 #include "block/coroutine.h"
32 static char temp_file
[] = "/tmp/vmst.test.XXXXXX";
35 /* Fake yield_until_fd_readable() implementation so we don't have to pull the
36 * coroutine code as dependency.
38 void yield_until_fd_readable(int fd
)
43 select(fd
+ 1, &fds
, NULL
, NULL
, NULL
);
47 * Some tests use 'open_test_file' to work on a real fd, some use
48 * an in memory file (QEMUSizedBuffer+qemu_bufopen); we could pick one
49 * but this way we test both.
52 /* Duplicate temp_fd and seek to the beginning of the file */
53 static QEMUFile
*open_test_file(bool write
)
55 int fd
= dup(temp_fd
);
56 lseek(fd
, 0, SEEK_SET
);
58 g_assert_cmpint(ftruncate(fd
, 0), ==, 0);
60 return qemu_fdopen(fd
, write
? "wb" : "rb");
64 * Check that the contents of the memory-buffered file f match
65 * the given size/data.
67 static void check_mem_file(QEMUFile
*f
, void *data
, size_t size
)
69 uint8_t *result
= g_malloc(size
);
70 const QEMUSizedBuffer
*qsb
= qemu_buf_get(f
);
71 g_assert_cmpint(qsb_get_length(qsb
), ==, size
);
72 g_assert_cmpint(qsb_get_buffer(qsb
, 0, size
, result
), ==, size
);
73 g_assert_cmpint(memcmp(result
, data
, size
), ==, 0);
77 #define SUCCESS(val) \
78 g_assert_cmpint((val), ==, 0)
80 #define FAILURE(val) \
81 g_assert_cmpint((val), !=, 0)
83 static void save_vmstate(const VMStateDescription
*desc
, void *obj
)
85 QEMUFile
*f
= open_test_file(true);
87 /* Save file with vmstate */
88 vmstate_save_state(f
, desc
, obj
);
89 qemu_put_byte(f
, QEMU_VM_EOF
);
90 g_assert(!qemu_file_get_error(f
));
94 static void compare_vmstate(uint8_t *wire
, size_t size
)
96 QEMUFile
*f
= open_test_file(false);
99 /* read back as binary */
101 g_assert_cmpint(qemu_get_buffer(f
, result
, sizeof(result
)), ==,
103 g_assert(!qemu_file_get_error(f
));
105 /* Compare that what is on the file is the same that what we
106 expected to be there */
107 SUCCESS(memcmp(result
, wire
, sizeof(result
)));
111 g_assert_cmpint(qemu_file_get_error(f
), ==, -EIO
);
116 static int load_vmstate_one(const VMStateDescription
*desc
, void *obj
,
117 int version
, uint8_t *wire
, size_t size
)
122 f
= open_test_file(true);
123 qemu_put_buffer(f
, wire
, size
);
126 f
= open_test_file(false);
127 ret
= vmstate_load_state(f
, desc
, obj
, version
);
129 g_assert(qemu_file_get_error(f
));
131 g_assert(!qemu_file_get_error(f
));
138 static int load_vmstate(const VMStateDescription
*desc
,
139 void *obj
, void *obj_clone
,
140 void (*obj_copy
)(void *, void*),
141 int version
, uint8_t *wire
, size_t size
)
143 /* We test with zero size */
144 obj_copy(obj_clone
, obj
);
145 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
, 0));
147 /* Stream ends with QEMU_EOF, so we need at least 3 bytes to be
148 * able to test in the middle */
152 /* We test with size - 2. We can't test size - 1 due to EOF tricks */
153 obj_copy(obj
, obj_clone
);
154 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
, size
- 2));
156 /* Test with size/2, first half of real state */
157 obj_copy(obj
, obj_clone
);
158 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
, size
/2));
160 /* Test with size/2, second half of real state */
161 obj_copy(obj
, obj_clone
);
162 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
+ (size
/2), size
/2));
165 obj_copy(obj
, obj_clone
);
166 return load_vmstate_one(desc
, obj
, version
, wire
, size
);
169 /* Test struct that we are going to use for our tests */
171 typedef struct TestSimple
{
178 int16_t i16_1
, i16_2
;
179 int32_t i32_1
, i32_2
;
180 int64_t i64_1
, i64_2
;
183 /* Object instantiation, we are going to use it in more than one test */
185 TestSimple obj_simple
= {
202 /* Description of the values. If you add a primitive type
203 you are expected to add a test here */
205 static const VMStateDescription vmstate_simple_primitive
= {
206 .name
= "simple/primitive",
208 .minimum_version_id
= 1,
209 .fields
= (VMStateField
[]) {
210 VMSTATE_BOOL(b_1
, TestSimple
),
211 VMSTATE_BOOL(b_2
, TestSimple
),
212 VMSTATE_UINT8(u8_1
, TestSimple
),
213 VMSTATE_UINT16(u16_1
, TestSimple
),
214 VMSTATE_UINT32(u32_1
, TestSimple
),
215 VMSTATE_UINT64(u64_1
, TestSimple
),
216 VMSTATE_INT8(i8_1
, TestSimple
),
217 VMSTATE_INT8(i8_2
, TestSimple
),
218 VMSTATE_INT16(i16_1
, TestSimple
),
219 VMSTATE_INT16(i16_2
, TestSimple
),
220 VMSTATE_INT32(i32_1
, TestSimple
),
221 VMSTATE_INT32(i32_2
, TestSimple
),
222 VMSTATE_INT64(i64_1
, TestSimple
),
223 VMSTATE_INT64(i64_2
, TestSimple
),
224 VMSTATE_END_OF_LIST()
228 /* It describes what goes through the wire. Our tests are basically:
231 - save a struct a vmstate to a file
232 - read that file back (binary read, no vmstate)
233 - compare it with what we expect to be on the wire
235 - save to the file what we expect to be on the wire
236 - read struct back with vmstate in a different
237 - compare back with the original struct
240 uint8_t wire_simple_primitive
[] = {
244 /* u16_1 */ 0x02, 0x00,
245 /* u32_1 */ 0x00, 0x01, 0x11, 0x70,
246 /* u64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
249 /* i16_1 */ 0x02, 0x00,
250 /* i16_2 */ 0xfe, 0x0,
251 /* i32_1 */ 0x00, 0x01, 0x11, 0x70,
252 /* i32_2 */ 0xff, 0xfe, 0xee, 0x90,
253 /* i64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
254 /* i64_2 */ 0xff, 0xff, 0xff, 0xff, 0xff, 0x47, 0x0b, 0x84,
255 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
258 static void obj_simple_copy(void *target
, void *source
)
260 memcpy(target
, source
, sizeof(TestSimple
));
263 static void test_simple_primitive(void)
265 TestSimple obj
, obj_clone
;
267 memset(&obj
, 0, sizeof(obj
));
268 save_vmstate(&vmstate_simple_primitive
, &obj_simple
);
270 compare_vmstate(wire_simple_primitive
, sizeof(wire_simple_primitive
));
272 SUCCESS(load_vmstate(&vmstate_simple_primitive
, &obj
, &obj_clone
,
273 obj_simple_copy
, 1, wire_simple_primitive
,
274 sizeof(wire_simple_primitive
)));
276 #define FIELD_EQUAL(name) g_assert_cmpint(obj.name, ==, obj_simple.name)
295 typedef struct TestStruct
{
301 static const VMStateDescription vmstate_versioned
= {
302 .name
= "test/versioned",
304 .minimum_version_id
= 1,
305 .fields
= (VMStateField
[]) {
306 VMSTATE_UINT32(a
, TestStruct
),
307 VMSTATE_UINT32_V(b
, TestStruct
, 2), /* Versioned field in the middle, so
308 * we catch bugs more easily.
310 VMSTATE_UINT32(c
, TestStruct
),
311 VMSTATE_UINT64(d
, TestStruct
),
312 VMSTATE_UINT32_V(e
, TestStruct
, 2),
313 VMSTATE_UINT64_V(f
, TestStruct
, 2),
314 VMSTATE_END_OF_LIST()
318 static void test_load_v1(void)
320 QEMUFile
*fsave
= open_test_file(true);
324 0, 0, 0, 0, 0, 0, 0, 40, /* d */
325 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
327 qemu_put_buffer(fsave
, buf
, sizeof(buf
));
330 QEMUFile
*loading
= open_test_file(false);
331 TestStruct obj
= { .b
= 200, .e
= 500, .f
= 600 };
332 vmstate_load_state(loading
, &vmstate_versioned
, &obj
, 1);
333 g_assert(!qemu_file_get_error(loading
));
334 g_assert_cmpint(obj
.a
, ==, 10);
335 g_assert_cmpint(obj
.b
, ==, 200);
336 g_assert_cmpint(obj
.c
, ==, 30);
337 g_assert_cmpint(obj
.d
, ==, 40);
338 g_assert_cmpint(obj
.e
, ==, 500);
339 g_assert_cmpint(obj
.f
, ==, 600);
340 qemu_fclose(loading
);
343 static void test_load_v2(void)
345 QEMUFile
*fsave
= open_test_file(true);
350 0, 0, 0, 0, 0, 0, 0, 40, /* d */
352 0, 0, 0, 0, 0, 0, 0, 60, /* f */
353 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
355 qemu_put_buffer(fsave
, buf
, sizeof(buf
));
358 QEMUFile
*loading
= open_test_file(false);
360 vmstate_load_state(loading
, &vmstate_versioned
, &obj
, 2);
361 g_assert_cmpint(obj
.a
, ==, 10);
362 g_assert_cmpint(obj
.b
, ==, 20);
363 g_assert_cmpint(obj
.c
, ==, 30);
364 g_assert_cmpint(obj
.d
, ==, 40);
365 g_assert_cmpint(obj
.e
, ==, 50);
366 g_assert_cmpint(obj
.f
, ==, 60);
367 qemu_fclose(loading
);
370 static bool test_skip(void *opaque
, int version_id
)
372 TestStruct
*t
= (TestStruct
*)opaque
;
376 static const VMStateDescription vmstate_skipping
= {
379 .minimum_version_id
= 1,
380 .fields
= (VMStateField
[]) {
381 VMSTATE_UINT32(a
, TestStruct
),
382 VMSTATE_UINT32(b
, TestStruct
),
383 VMSTATE_UINT32_TEST(c
, TestStruct
, test_skip
),
384 VMSTATE_UINT64(d
, TestStruct
),
385 VMSTATE_UINT32_TEST(e
, TestStruct
, test_skip
),
386 VMSTATE_UINT64_V(f
, TestStruct
, 2),
387 VMSTATE_END_OF_LIST()
392 static void test_save_noskip(void)
394 QEMUFile
*fsave
= qemu_bufopen("w", NULL
);
395 TestStruct obj
= { .a
= 1, .b
= 2, .c
= 3, .d
= 4, .e
= 5, .f
= 6,
397 vmstate_save_state(fsave
, &vmstate_skipping
, &obj
);
398 g_assert(!qemu_file_get_error(fsave
));
400 uint8_t expected
[] = {
404 0, 0, 0, 0, 0, 0, 0, 4, /* d */
406 0, 0, 0, 0, 0, 0, 0, 6, /* f */
408 check_mem_file(fsave
, expected
, sizeof(expected
));
412 static void test_save_skip(void)
414 QEMUFile
*fsave
= qemu_bufopen("w", NULL
);
415 TestStruct obj
= { .a
= 1, .b
= 2, .c
= 3, .d
= 4, .e
= 5, .f
= 6,
417 vmstate_save_state(fsave
, &vmstate_skipping
, &obj
);
418 g_assert(!qemu_file_get_error(fsave
));
420 uint8_t expected
[] = {
423 0, 0, 0, 0, 0, 0, 0, 4, /* d */
424 0, 0, 0, 0, 0, 0, 0, 6, /* f */
426 check_mem_file(fsave
, expected
, sizeof(expected
));
431 static void test_load_noskip(void)
437 0, 0, 0, 0, 0, 0, 0, 40, /* d */
439 0, 0, 0, 0, 0, 0, 0, 60, /* f */
440 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
443 QEMUSizedBuffer
*qsb
= qsb_create(buf
, sizeof(buf
));
445 QEMUFile
*loading
= qemu_bufopen("r", qsb
);
446 TestStruct obj
= { .skip_c_e
= false };
447 vmstate_load_state(loading
, &vmstate_skipping
, &obj
, 2);
448 g_assert(!qemu_file_get_error(loading
));
449 g_assert_cmpint(obj
.a
, ==, 10);
450 g_assert_cmpint(obj
.b
, ==, 20);
451 g_assert_cmpint(obj
.c
, ==, 30);
452 g_assert_cmpint(obj
.d
, ==, 40);
453 g_assert_cmpint(obj
.e
, ==, 50);
454 g_assert_cmpint(obj
.f
, ==, 60);
455 qemu_fclose(loading
);
459 static void test_load_skip(void)
464 0, 0, 0, 0, 0, 0, 0, 40, /* d */
465 0, 0, 0, 0, 0, 0, 0, 60, /* f */
466 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
469 QEMUSizedBuffer
*qsb
= qsb_create(buf
, sizeof(buf
));
471 QEMUFile
*loading
= qemu_bufopen("r", qsb
);
472 TestStruct obj
= { .skip_c_e
= true, .c
= 300, .e
= 500 };
473 vmstate_load_state(loading
, &vmstate_skipping
, &obj
, 2);
474 g_assert(!qemu_file_get_error(loading
));
475 g_assert_cmpint(obj
.a
, ==, 10);
476 g_assert_cmpint(obj
.b
, ==, 20);
477 g_assert_cmpint(obj
.c
, ==, 300);
478 g_assert_cmpint(obj
.d
, ==, 40);
479 g_assert_cmpint(obj
.e
, ==, 500);
480 g_assert_cmpint(obj
.f
, ==, 60);
481 qemu_fclose(loading
);
485 int main(int argc
, char **argv
)
487 temp_fd
= mkstemp(temp_file
);
489 g_test_init(&argc
, &argv
, NULL
);
490 g_test_add_func("/vmstate/simple/primitive", test_simple_primitive
);
491 g_test_add_func("/vmstate/versioned/load/v1", test_load_v1
);
492 g_test_add_func("/vmstate/versioned/load/v2", test_load_v2
);
493 g_test_add_func("/vmstate/field_exists/load/noskip", test_load_noskip
);
494 g_test_add_func("/vmstate/field_exists/load/skip", test_load_skip
);
495 g_test_add_func("/vmstate/field_exists/save/noskip", test_save_noskip
);
496 g_test_add_func("/vmstate/field_exists/save/skip", test_save_skip
);