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 /* 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
);
52 lseek(fd
, 0, SEEK_SET
);
54 g_assert_cmpint(ftruncate(fd
, 0), ==, 0);
56 return qemu_fdopen(fd
, write
? "wb" : "rb");
59 #define SUCCESS(val) \
60 g_assert_cmpint((val), ==, 0)
62 #define FAILURE(val) \
63 g_assert_cmpint((val), !=, 0)
65 static void save_vmstate(const VMStateDescription
*desc
, void *obj
)
67 QEMUFile
*f
= open_test_file(true);
69 /* Save file with vmstate */
70 vmstate_save_state(f
, desc
, obj
, NULL
);
71 qemu_put_byte(f
, QEMU_VM_EOF
);
72 g_assert(!qemu_file_get_error(f
));
76 static void compare_vmstate(uint8_t *wire
, size_t size
)
78 QEMUFile
*f
= open_test_file(false);
81 /* read back as binary */
83 g_assert_cmpint(qemu_get_buffer(f
, result
, sizeof(result
)), ==,
85 g_assert(!qemu_file_get_error(f
));
87 /* Compare that what is on the file is the same that what we
88 expected to be there */
89 SUCCESS(memcmp(result
, wire
, sizeof(result
)));
93 g_assert_cmpint(qemu_file_get_error(f
), ==, -EIO
);
98 static int load_vmstate_one(const VMStateDescription
*desc
, void *obj
,
99 int version
, uint8_t *wire
, size_t size
)
104 f
= open_test_file(true);
105 qemu_put_buffer(f
, wire
, size
);
108 f
= open_test_file(false);
109 ret
= vmstate_load_state(f
, desc
, obj
, version
);
111 g_assert(qemu_file_get_error(f
));
113 g_assert(!qemu_file_get_error(f
));
120 static int load_vmstate(const VMStateDescription
*desc
,
121 void *obj
, void *obj_clone
,
122 void (*obj_copy
)(void *, void*),
123 int version
, uint8_t *wire
, size_t size
)
125 /* We test with zero size */
126 obj_copy(obj_clone
, obj
);
127 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
, 0));
129 /* Stream ends with QEMU_EOF, so we need at least 3 bytes to be
130 * able to test in the middle */
134 /* We test with size - 2. We can't test size - 1 due to EOF tricks */
135 obj_copy(obj
, obj_clone
);
136 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
, size
- 2));
138 /* Test with size/2, first half of real state */
139 obj_copy(obj
, obj_clone
);
140 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
, size
/2));
142 /* Test with size/2, second half of real state */
143 obj_copy(obj
, obj_clone
);
144 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
+ (size
/2), size
/2));
147 obj_copy(obj
, obj_clone
);
148 return load_vmstate_one(desc
, obj
, version
, wire
, size
);
151 /* Test struct that we are going to use for our tests */
153 typedef struct TestSimple
{
160 int16_t i16_1
, i16_2
;
161 int32_t i32_1
, i32_2
;
162 int64_t i64_1
, i64_2
;
165 /* Object instantiation, we are going to use it in more than one test */
167 TestSimple obj_simple
= {
184 /* Description of the values. If you add a primitive type
185 you are expected to add a test here */
187 static const VMStateDescription vmstate_simple_primitive
= {
188 .name
= "simple/primitive",
190 .minimum_version_id
= 1,
191 .fields
= (VMStateField
[]) {
192 VMSTATE_BOOL(b_1
, TestSimple
),
193 VMSTATE_BOOL(b_2
, TestSimple
),
194 VMSTATE_UINT8(u8_1
, TestSimple
),
195 VMSTATE_UINT16(u16_1
, TestSimple
),
196 VMSTATE_UINT32(u32_1
, TestSimple
),
197 VMSTATE_UINT64(u64_1
, TestSimple
),
198 VMSTATE_INT8(i8_1
, TestSimple
),
199 VMSTATE_INT8(i8_2
, TestSimple
),
200 VMSTATE_INT16(i16_1
, TestSimple
),
201 VMSTATE_INT16(i16_2
, TestSimple
),
202 VMSTATE_INT32(i32_1
, TestSimple
),
203 VMSTATE_INT32(i32_2
, TestSimple
),
204 VMSTATE_INT64(i64_1
, TestSimple
),
205 VMSTATE_INT64(i64_2
, TestSimple
),
206 VMSTATE_END_OF_LIST()
210 /* It describes what goes through the wire. Our tests are basically:
213 - save a struct a vmstate to a file
214 - read that file back (binary read, no vmstate)
215 - compare it with what we expect to be on the wire
217 - save to the file what we expect to be on the wire
218 - read struct back with vmstate in a different
219 - compare back with the original struct
222 uint8_t wire_simple_primitive
[] = {
226 /* u16_1 */ 0x02, 0x00,
227 /* u32_1 */ 0x00, 0x01, 0x11, 0x70,
228 /* u64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
231 /* i16_1 */ 0x02, 0x00,
232 /* i16_2 */ 0xfe, 0x0,
233 /* i32_1 */ 0x00, 0x01, 0x11, 0x70,
234 /* i32_2 */ 0xff, 0xfe, 0xee, 0x90,
235 /* i64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
236 /* i64_2 */ 0xff, 0xff, 0xff, 0xff, 0xff, 0x47, 0x0b, 0x84,
237 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
240 static void obj_simple_copy(void *target
, void *source
)
242 memcpy(target
, source
, sizeof(TestSimple
));
245 static void test_simple_primitive(void)
247 TestSimple obj
, obj_clone
;
249 memset(&obj
, 0, sizeof(obj
));
250 save_vmstate(&vmstate_simple_primitive
, &obj_simple
);
252 compare_vmstate(wire_simple_primitive
, sizeof(wire_simple_primitive
));
254 SUCCESS(load_vmstate(&vmstate_simple_primitive
, &obj
, &obj_clone
,
255 obj_simple_copy
, 1, wire_simple_primitive
,
256 sizeof(wire_simple_primitive
)));
258 #define FIELD_EQUAL(name) g_assert_cmpint(obj.name, ==, obj_simple.name)
277 typedef struct TestStruct
{
283 static const VMStateDescription vmstate_versioned
= {
284 .name
= "test/versioned",
286 .minimum_version_id
= 1,
287 .fields
= (VMStateField
[]) {
288 VMSTATE_UINT32(a
, TestStruct
),
289 VMSTATE_UINT32_V(b
, TestStruct
, 2), /* Versioned field in the middle, so
290 * we catch bugs more easily.
292 VMSTATE_UINT32(c
, TestStruct
),
293 VMSTATE_UINT64(d
, TestStruct
),
294 VMSTATE_UINT32_V(e
, TestStruct
, 2),
295 VMSTATE_UINT64_V(f
, TestStruct
, 2),
296 VMSTATE_END_OF_LIST()
300 static void test_load_v1(void)
302 QEMUFile
*fsave
= open_test_file(true);
306 0, 0, 0, 0, 0, 0, 0, 40, /* d */
307 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
309 qemu_put_buffer(fsave
, buf
, sizeof(buf
));
312 QEMUFile
*loading
= open_test_file(false);
313 TestStruct obj
= { .b
= 200, .e
= 500, .f
= 600 };
314 vmstate_load_state(loading
, &vmstate_versioned
, &obj
, 1);
315 g_assert(!qemu_file_get_error(loading
));
316 g_assert_cmpint(obj
.a
, ==, 10);
317 g_assert_cmpint(obj
.b
, ==, 200);
318 g_assert_cmpint(obj
.c
, ==, 30);
319 g_assert_cmpint(obj
.d
, ==, 40);
320 g_assert_cmpint(obj
.e
, ==, 500);
321 g_assert_cmpint(obj
.f
, ==, 600);
322 qemu_fclose(loading
);
325 static void test_load_v2(void)
327 QEMUFile
*fsave
= open_test_file(true);
332 0, 0, 0, 0, 0, 0, 0, 40, /* d */
334 0, 0, 0, 0, 0, 0, 0, 60, /* f */
335 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
337 qemu_put_buffer(fsave
, buf
, sizeof(buf
));
340 QEMUFile
*loading
= open_test_file(false);
342 vmstate_load_state(loading
, &vmstate_versioned
, &obj
, 2);
343 g_assert_cmpint(obj
.a
, ==, 10);
344 g_assert_cmpint(obj
.b
, ==, 20);
345 g_assert_cmpint(obj
.c
, ==, 30);
346 g_assert_cmpint(obj
.d
, ==, 40);
347 g_assert_cmpint(obj
.e
, ==, 50);
348 g_assert_cmpint(obj
.f
, ==, 60);
349 qemu_fclose(loading
);
352 static bool test_skip(void *opaque
, int version_id
)
354 TestStruct
*t
= (TestStruct
*)opaque
;
358 static const VMStateDescription vmstate_skipping
= {
361 .minimum_version_id
= 1,
362 .fields
= (VMStateField
[]) {
363 VMSTATE_UINT32(a
, TestStruct
),
364 VMSTATE_UINT32(b
, TestStruct
),
365 VMSTATE_UINT32_TEST(c
, TestStruct
, test_skip
),
366 VMSTATE_UINT64(d
, TestStruct
),
367 VMSTATE_UINT32_TEST(e
, TestStruct
, test_skip
),
368 VMSTATE_UINT64_V(f
, TestStruct
, 2),
369 VMSTATE_END_OF_LIST()
374 static void test_save_noskip(void)
376 QEMUFile
*fsave
= open_test_file(true);
377 TestStruct obj
= { .a
= 1, .b
= 2, .c
= 3, .d
= 4, .e
= 5, .f
= 6,
379 vmstate_save_state(fsave
, &vmstate_skipping
, &obj
, NULL
);
380 g_assert(!qemu_file_get_error(fsave
));
382 uint8_t expected
[] = {
386 0, 0, 0, 0, 0, 0, 0, 4, /* d */
388 0, 0, 0, 0, 0, 0, 0, 6, /* f */
392 compare_vmstate(expected
, sizeof(expected
));
395 static void test_save_skip(void)
397 QEMUFile
*fsave
= open_test_file(true);
398 TestStruct obj
= { .a
= 1, .b
= 2, .c
= 3, .d
= 4, .e
= 5, .f
= 6,
400 vmstate_save_state(fsave
, &vmstate_skipping
, &obj
, NULL
);
401 g_assert(!qemu_file_get_error(fsave
));
403 uint8_t expected
[] = {
406 0, 0, 0, 0, 0, 0, 0, 4, /* d */
407 0, 0, 0, 0, 0, 0, 0, 6, /* f */
411 compare_vmstate(expected
, sizeof(expected
));
414 static void test_load_noskip(void)
416 QEMUFile
*fsave
= open_test_file(true);
421 0, 0, 0, 0, 0, 0, 0, 40, /* d */
423 0, 0, 0, 0, 0, 0, 0, 60, /* f */
424 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
426 qemu_put_buffer(fsave
, buf
, sizeof(buf
));
429 QEMUFile
*loading
= open_test_file(false);
430 TestStruct obj
= { .skip_c_e
= false };
431 vmstate_load_state(loading
, &vmstate_skipping
, &obj
, 2);
432 g_assert(!qemu_file_get_error(loading
));
433 g_assert_cmpint(obj
.a
, ==, 10);
434 g_assert_cmpint(obj
.b
, ==, 20);
435 g_assert_cmpint(obj
.c
, ==, 30);
436 g_assert_cmpint(obj
.d
, ==, 40);
437 g_assert_cmpint(obj
.e
, ==, 50);
438 g_assert_cmpint(obj
.f
, ==, 60);
439 qemu_fclose(loading
);
442 static void test_load_skip(void)
444 QEMUFile
*fsave
= open_test_file(true);
448 0, 0, 0, 0, 0, 0, 0, 40, /* d */
449 0, 0, 0, 0, 0, 0, 0, 60, /* f */
450 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
452 qemu_put_buffer(fsave
, buf
, sizeof(buf
));
455 QEMUFile
*loading
= open_test_file(false);
456 TestStruct obj
= { .skip_c_e
= true, .c
= 300, .e
= 500 };
457 vmstate_load_state(loading
, &vmstate_skipping
, &obj
, 2);
458 g_assert(!qemu_file_get_error(loading
));
459 g_assert_cmpint(obj
.a
, ==, 10);
460 g_assert_cmpint(obj
.b
, ==, 20);
461 g_assert_cmpint(obj
.c
, ==, 300);
462 g_assert_cmpint(obj
.d
, ==, 40);
463 g_assert_cmpint(obj
.e
, ==, 500);
464 g_assert_cmpint(obj
.f
, ==, 60);
465 qemu_fclose(loading
);
468 int main(int argc
, char **argv
)
470 temp_fd
= mkstemp(temp_file
);
472 g_test_init(&argc
, &argv
, NULL
);
473 g_test_add_func("/vmstate/simple/primitive", test_simple_primitive
);
474 g_test_add_func("/vmstate/versioned/load/v1", test_load_v1
);
475 g_test_add_func("/vmstate/versioned/load/v2", test_load_v2
);
476 g_test_add_func("/vmstate/field_exists/load/noskip", test_load_noskip
);
477 g_test_add_func("/vmstate/field_exists/load/skip", test_load_skip
);
478 g_test_add_func("/vmstate/field_exists/save/noskip", test_save_noskip
);
479 g_test_add_func("/vmstate/field_exists/save/skip", test_save_skip
);