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"
32 #include "io/channel-file.h"
34 static char temp_file
[] = "/tmp/vmst.test.XXXXXX";
37 /* Fake yield_until_fd_readable() implementation so we don't have to pull the
38 * coroutine code as dependency.
40 void yield_until_fd_readable(int fd
)
45 select(fd
+ 1, &fds
, NULL
, NULL
, NULL
);
49 /* Duplicate temp_fd and seek to the beginning of the file */
50 static QEMUFile
*open_test_file(bool write
)
52 int fd
= dup(temp_fd
);
54 lseek(fd
, 0, SEEK_SET
);
56 g_assert_cmpint(ftruncate(fd
, 0), ==, 0);
58 ioc
= QIO_CHANNEL(qio_channel_file_new_fd(fd
));
60 return qemu_fopen_channel_output(ioc
);
62 return qemu_fopen_channel_input(ioc
);
66 #define SUCCESS(val) \
67 g_assert_cmpint((val), ==, 0)
69 #define FAILURE(val) \
70 g_assert_cmpint((val), !=, 0)
72 static void save_vmstate(const VMStateDescription
*desc
, void *obj
)
74 QEMUFile
*f
= open_test_file(true);
76 /* Save file with vmstate */
77 vmstate_save_state(f
, desc
, obj
, NULL
);
78 qemu_put_byte(f
, QEMU_VM_EOF
);
79 g_assert(!qemu_file_get_error(f
));
83 static void compare_vmstate(uint8_t *wire
, size_t size
)
85 QEMUFile
*f
= open_test_file(false);
88 /* read back as binary */
90 g_assert_cmpint(qemu_get_buffer(f
, result
, sizeof(result
)), ==,
92 g_assert(!qemu_file_get_error(f
));
94 /* Compare that what is on the file is the same that what we
95 expected to be there */
96 SUCCESS(memcmp(result
, wire
, sizeof(result
)));
100 g_assert_cmpint(qemu_file_get_error(f
), ==, -EIO
);
105 static int load_vmstate_one(const VMStateDescription
*desc
, void *obj
,
106 int version
, uint8_t *wire
, size_t size
)
111 f
= open_test_file(true);
112 qemu_put_buffer(f
, wire
, size
);
115 f
= open_test_file(false);
116 ret
= vmstate_load_state(f
, desc
, obj
, version
);
118 g_assert(qemu_file_get_error(f
));
120 g_assert(!qemu_file_get_error(f
));
127 static int load_vmstate(const VMStateDescription
*desc
,
128 void *obj
, void *obj_clone
,
129 void (*obj_copy
)(void *, void*),
130 int version
, uint8_t *wire
, size_t size
)
132 /* We test with zero size */
133 obj_copy(obj_clone
, obj
);
134 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
, 0));
136 /* Stream ends with QEMU_EOF, so we need at least 3 bytes to be
137 * able to test in the middle */
141 /* We test with size - 2. We can't test size - 1 due to EOF tricks */
142 obj_copy(obj
, obj_clone
);
143 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
, size
- 2));
145 /* Test with size/2, first half of real state */
146 obj_copy(obj
, obj_clone
);
147 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
, size
/2));
149 /* Test with size/2, second half of real state */
150 obj_copy(obj
, obj_clone
);
151 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
+ (size
/2), size
/2));
154 obj_copy(obj
, obj_clone
);
155 return load_vmstate_one(desc
, obj
, version
, wire
, size
);
158 /* Test struct that we are going to use for our tests */
160 typedef struct TestSimple
{
167 int16_t i16_1
, i16_2
;
168 int32_t i32_1
, i32_2
;
169 int64_t i64_1
, i64_2
;
172 /* Object instantiation, we are going to use it in more than one test */
174 TestSimple obj_simple
= {
191 /* Description of the values. If you add a primitive type
192 you are expected to add a test here */
194 static const VMStateDescription vmstate_simple_primitive
= {
195 .name
= "simple/primitive",
197 .minimum_version_id
= 1,
198 .fields
= (VMStateField
[]) {
199 VMSTATE_BOOL(b_1
, TestSimple
),
200 VMSTATE_BOOL(b_2
, TestSimple
),
201 VMSTATE_UINT8(u8_1
, TestSimple
),
202 VMSTATE_UINT16(u16_1
, TestSimple
),
203 VMSTATE_UINT32(u32_1
, TestSimple
),
204 VMSTATE_UINT64(u64_1
, TestSimple
),
205 VMSTATE_INT8(i8_1
, TestSimple
),
206 VMSTATE_INT8(i8_2
, TestSimple
),
207 VMSTATE_INT16(i16_1
, TestSimple
),
208 VMSTATE_INT16(i16_2
, TestSimple
),
209 VMSTATE_INT32(i32_1
, TestSimple
),
210 VMSTATE_INT32(i32_2
, TestSimple
),
211 VMSTATE_INT64(i64_1
, TestSimple
),
212 VMSTATE_INT64(i64_2
, TestSimple
),
213 VMSTATE_END_OF_LIST()
217 /* It describes what goes through the wire. Our tests are basically:
220 - save a struct a vmstate to a file
221 - read that file back (binary read, no vmstate)
222 - compare it with what we expect to be on the wire
224 - save to the file what we expect to be on the wire
225 - read struct back with vmstate in a different
226 - compare back with the original struct
229 uint8_t wire_simple_primitive
[] = {
233 /* u16_1 */ 0x02, 0x00,
234 /* u32_1 */ 0x00, 0x01, 0x11, 0x70,
235 /* u64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
238 /* i16_1 */ 0x02, 0x00,
239 /* i16_2 */ 0xfe, 0x0,
240 /* i32_1 */ 0x00, 0x01, 0x11, 0x70,
241 /* i32_2 */ 0xff, 0xfe, 0xee, 0x90,
242 /* i64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
243 /* i64_2 */ 0xff, 0xff, 0xff, 0xff, 0xff, 0x47, 0x0b, 0x84,
244 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
247 static void obj_simple_copy(void *target
, void *source
)
249 memcpy(target
, source
, sizeof(TestSimple
));
252 static void test_simple_primitive(void)
254 TestSimple obj
, obj_clone
;
256 memset(&obj
, 0, sizeof(obj
));
257 save_vmstate(&vmstate_simple_primitive
, &obj_simple
);
259 compare_vmstate(wire_simple_primitive
, sizeof(wire_simple_primitive
));
261 SUCCESS(load_vmstate(&vmstate_simple_primitive
, &obj
, &obj_clone
,
262 obj_simple_copy
, 1, wire_simple_primitive
,
263 sizeof(wire_simple_primitive
)));
265 #define FIELD_EQUAL(name) g_assert_cmpint(obj.name, ==, obj_simple.name)
284 typedef struct TestStruct
{
290 static const VMStateDescription vmstate_versioned
= {
291 .name
= "test/versioned",
293 .minimum_version_id
= 1,
294 .fields
= (VMStateField
[]) {
295 VMSTATE_UINT32(a
, TestStruct
),
296 VMSTATE_UINT32_V(b
, TestStruct
, 2), /* Versioned field in the middle, so
297 * we catch bugs more easily.
299 VMSTATE_UINT32(c
, TestStruct
),
300 VMSTATE_UINT64(d
, TestStruct
),
301 VMSTATE_UINT32_V(e
, TestStruct
, 2),
302 VMSTATE_UINT64_V(f
, TestStruct
, 2),
303 VMSTATE_END_OF_LIST()
307 static void test_load_v1(void)
309 QEMUFile
*fsave
= open_test_file(true);
313 0, 0, 0, 0, 0, 0, 0, 40, /* d */
314 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
316 qemu_put_buffer(fsave
, buf
, sizeof(buf
));
319 QEMUFile
*loading
= open_test_file(false);
320 TestStruct obj
= { .b
= 200, .e
= 500, .f
= 600 };
321 vmstate_load_state(loading
, &vmstate_versioned
, &obj
, 1);
322 g_assert(!qemu_file_get_error(loading
));
323 g_assert_cmpint(obj
.a
, ==, 10);
324 g_assert_cmpint(obj
.b
, ==, 200);
325 g_assert_cmpint(obj
.c
, ==, 30);
326 g_assert_cmpint(obj
.d
, ==, 40);
327 g_assert_cmpint(obj
.e
, ==, 500);
328 g_assert_cmpint(obj
.f
, ==, 600);
329 qemu_fclose(loading
);
332 static void test_load_v2(void)
334 QEMUFile
*fsave
= open_test_file(true);
339 0, 0, 0, 0, 0, 0, 0, 40, /* d */
341 0, 0, 0, 0, 0, 0, 0, 60, /* f */
342 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
344 qemu_put_buffer(fsave
, buf
, sizeof(buf
));
347 QEMUFile
*loading
= open_test_file(false);
349 vmstate_load_state(loading
, &vmstate_versioned
, &obj
, 2);
350 g_assert_cmpint(obj
.a
, ==, 10);
351 g_assert_cmpint(obj
.b
, ==, 20);
352 g_assert_cmpint(obj
.c
, ==, 30);
353 g_assert_cmpint(obj
.d
, ==, 40);
354 g_assert_cmpint(obj
.e
, ==, 50);
355 g_assert_cmpint(obj
.f
, ==, 60);
356 qemu_fclose(loading
);
359 static bool test_skip(void *opaque
, int version_id
)
361 TestStruct
*t
= (TestStruct
*)opaque
;
365 static const VMStateDescription vmstate_skipping
= {
368 .minimum_version_id
= 1,
369 .fields
= (VMStateField
[]) {
370 VMSTATE_UINT32(a
, TestStruct
),
371 VMSTATE_UINT32(b
, TestStruct
),
372 VMSTATE_UINT32_TEST(c
, TestStruct
, test_skip
),
373 VMSTATE_UINT64(d
, TestStruct
),
374 VMSTATE_UINT32_TEST(e
, TestStruct
, test_skip
),
375 VMSTATE_UINT64_V(f
, TestStruct
, 2),
376 VMSTATE_END_OF_LIST()
381 static void test_save_noskip(void)
383 QEMUFile
*fsave
= open_test_file(true);
384 TestStruct obj
= { .a
= 1, .b
= 2, .c
= 3, .d
= 4, .e
= 5, .f
= 6,
386 vmstate_save_state(fsave
, &vmstate_skipping
, &obj
, NULL
);
387 g_assert(!qemu_file_get_error(fsave
));
389 uint8_t expected
[] = {
393 0, 0, 0, 0, 0, 0, 0, 4, /* d */
395 0, 0, 0, 0, 0, 0, 0, 6, /* f */
399 compare_vmstate(expected
, sizeof(expected
));
402 static void test_save_skip(void)
404 QEMUFile
*fsave
= open_test_file(true);
405 TestStruct obj
= { .a
= 1, .b
= 2, .c
= 3, .d
= 4, .e
= 5, .f
= 6,
407 vmstate_save_state(fsave
, &vmstate_skipping
, &obj
, NULL
);
408 g_assert(!qemu_file_get_error(fsave
));
410 uint8_t expected
[] = {
413 0, 0, 0, 0, 0, 0, 0, 4, /* d */
414 0, 0, 0, 0, 0, 0, 0, 6, /* f */
418 compare_vmstate(expected
, sizeof(expected
));
421 static void test_load_noskip(void)
423 QEMUFile
*fsave
= open_test_file(true);
428 0, 0, 0, 0, 0, 0, 0, 40, /* d */
430 0, 0, 0, 0, 0, 0, 0, 60, /* f */
431 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
433 qemu_put_buffer(fsave
, buf
, sizeof(buf
));
436 QEMUFile
*loading
= open_test_file(false);
437 TestStruct obj
= { .skip_c_e
= false };
438 vmstate_load_state(loading
, &vmstate_skipping
, &obj
, 2);
439 g_assert(!qemu_file_get_error(loading
));
440 g_assert_cmpint(obj
.a
, ==, 10);
441 g_assert_cmpint(obj
.b
, ==, 20);
442 g_assert_cmpint(obj
.c
, ==, 30);
443 g_assert_cmpint(obj
.d
, ==, 40);
444 g_assert_cmpint(obj
.e
, ==, 50);
445 g_assert_cmpint(obj
.f
, ==, 60);
446 qemu_fclose(loading
);
449 static void test_load_skip(void)
451 QEMUFile
*fsave
= open_test_file(true);
455 0, 0, 0, 0, 0, 0, 0, 40, /* d */
456 0, 0, 0, 0, 0, 0, 0, 60, /* f */
457 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
459 qemu_put_buffer(fsave
, buf
, sizeof(buf
));
462 QEMUFile
*loading
= open_test_file(false);
463 TestStruct obj
= { .skip_c_e
= true, .c
= 300, .e
= 500 };
464 vmstate_load_state(loading
, &vmstate_skipping
, &obj
, 2);
465 g_assert(!qemu_file_get_error(loading
));
466 g_assert_cmpint(obj
.a
, ==, 10);
467 g_assert_cmpint(obj
.b
, ==, 20);
468 g_assert_cmpint(obj
.c
, ==, 300);
469 g_assert_cmpint(obj
.d
, ==, 40);
470 g_assert_cmpint(obj
.e
, ==, 500);
471 g_assert_cmpint(obj
.f
, ==, 60);
472 qemu_fclose(loading
);
475 int main(int argc
, char **argv
)
477 temp_fd
= mkstemp(temp_file
);
479 module_call_init(MODULE_INIT_QOM
);
481 g_test_init(&argc
, &argv
, NULL
);
482 g_test_add_func("/vmstate/simple/primitive", test_simple_primitive
);
483 g_test_add_func("/vmstate/versioned/load/v1", test_load_v1
);
484 g_test_add_func("/vmstate/versioned/load/v2", test_load_v2
);
485 g_test_add_func("/vmstate/field_exists/load/noskip", test_load_noskip
);
486 g_test_add_func("/vmstate/field_exists/load/skip", test_load_skip
);
487 g_test_add_func("/vmstate/field_exists/save/noskip", test_save_noskip
);
488 g_test_add_func("/vmstate/field_exists/save/skip", test_save_skip
);