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
);
53 lseek(fd
, 0, SEEK_SET
);
55 g_assert_cmpint(ftruncate(fd
, 0), ==, 0);
57 ioc
= QIO_CHANNEL(qio_channel_file_new_fd(fd
));
59 return qemu_fopen_channel_output(ioc
);
61 return qemu_fopen_channel_input(ioc
);
65 #define SUCCESS(val) \
66 g_assert_cmpint((val), ==, 0)
68 #define FAILURE(val) \
69 g_assert_cmpint((val), !=, 0)
71 static void save_vmstate(const VMStateDescription
*desc
, void *obj
)
73 QEMUFile
*f
= open_test_file(true);
75 /* Save file with vmstate */
76 vmstate_save_state(f
, desc
, obj
, NULL
);
77 qemu_put_byte(f
, QEMU_VM_EOF
);
78 g_assert(!qemu_file_get_error(f
));
82 static void compare_vmstate(uint8_t *wire
, size_t size
)
84 QEMUFile
*f
= open_test_file(false);
87 /* read back as binary */
89 g_assert_cmpint(qemu_get_buffer(f
, result
, sizeof(result
)), ==,
91 g_assert(!qemu_file_get_error(f
));
93 /* Compare that what is on the file is the same that what we
94 expected to be there */
95 SUCCESS(memcmp(result
, wire
, sizeof(result
)));
99 g_assert_cmpint(qemu_file_get_error(f
), ==, -EIO
);
104 static int load_vmstate_one(const VMStateDescription
*desc
, void *obj
,
105 int version
, uint8_t *wire
, size_t size
)
110 f
= open_test_file(true);
111 qemu_put_buffer(f
, wire
, size
);
114 f
= open_test_file(false);
115 ret
= vmstate_load_state(f
, desc
, obj
, version
);
117 g_assert(qemu_file_get_error(f
));
119 g_assert(!qemu_file_get_error(f
));
126 static int load_vmstate(const VMStateDescription
*desc
,
127 void *obj
, void *obj_clone
,
128 void (*obj_copy
)(void *, void*),
129 int version
, uint8_t *wire
, size_t size
)
131 /* We test with zero size */
132 obj_copy(obj_clone
, obj
);
133 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
, 0));
135 /* Stream ends with QEMU_EOF, so we need at least 3 bytes to be
136 * able to test in the middle */
140 /* We test with size - 2. We can't test size - 1 due to EOF tricks */
141 obj_copy(obj
, obj_clone
);
142 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
, size
- 2));
144 /* Test with size/2, first half of real state */
145 obj_copy(obj
, obj_clone
);
146 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
, size
/2));
148 /* Test with size/2, second half of real state */
149 obj_copy(obj
, obj_clone
);
150 FAILURE(load_vmstate_one(desc
, obj
, version
, wire
+ (size
/2), size
/2));
153 obj_copy(obj
, obj_clone
);
154 return load_vmstate_one(desc
, obj
, version
, wire
, size
);
157 /* Test struct that we are going to use for our tests */
159 typedef struct TestSimple
{
166 int16_t i16_1
, i16_2
;
167 int32_t i32_1
, i32_2
;
168 int64_t i64_1
, i64_2
;
171 /* Object instantiation, we are going to use it in more than one test */
173 TestSimple obj_simple
= {
190 /* Description of the values. If you add a primitive type
191 you are expected to add a test here */
193 static const VMStateDescription vmstate_simple_primitive
= {
194 .name
= "simple/primitive",
196 .minimum_version_id
= 1,
197 .fields
= (VMStateField
[]) {
198 VMSTATE_BOOL(b_1
, TestSimple
),
199 VMSTATE_BOOL(b_2
, TestSimple
),
200 VMSTATE_UINT8(u8_1
, TestSimple
),
201 VMSTATE_UINT16(u16_1
, TestSimple
),
202 VMSTATE_UINT32(u32_1
, TestSimple
),
203 VMSTATE_UINT64(u64_1
, TestSimple
),
204 VMSTATE_INT8(i8_1
, TestSimple
),
205 VMSTATE_INT8(i8_2
, TestSimple
),
206 VMSTATE_INT16(i16_1
, TestSimple
),
207 VMSTATE_INT16(i16_2
, TestSimple
),
208 VMSTATE_INT32(i32_1
, TestSimple
),
209 VMSTATE_INT32(i32_2
, TestSimple
),
210 VMSTATE_INT64(i64_1
, TestSimple
),
211 VMSTATE_INT64(i64_2
, TestSimple
),
212 VMSTATE_END_OF_LIST()
216 /* It describes what goes through the wire. Our tests are basically:
219 - save a struct a vmstate to a file
220 - read that file back (binary read, no vmstate)
221 - compare it with what we expect to be on the wire
223 - save to the file what we expect to be on the wire
224 - read struct back with vmstate in a different
225 - compare back with the original struct
228 uint8_t wire_simple_primitive
[] = {
232 /* u16_1 */ 0x02, 0x00,
233 /* u32_1 */ 0x00, 0x01, 0x11, 0x70,
234 /* u64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
237 /* i16_1 */ 0x02, 0x00,
238 /* i16_2 */ 0xfe, 0x0,
239 /* i32_1 */ 0x00, 0x01, 0x11, 0x70,
240 /* i32_2 */ 0xff, 0xfe, 0xee, 0x90,
241 /* i64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
242 /* i64_2 */ 0xff, 0xff, 0xff, 0xff, 0xff, 0x47, 0x0b, 0x84,
243 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
246 static void obj_simple_copy(void *target
, void *source
)
248 memcpy(target
, source
, sizeof(TestSimple
));
251 static void test_simple_primitive(void)
253 TestSimple obj
, obj_clone
;
255 memset(&obj
, 0, sizeof(obj
));
256 save_vmstate(&vmstate_simple_primitive
, &obj_simple
);
258 compare_vmstate(wire_simple_primitive
, sizeof(wire_simple_primitive
));
260 SUCCESS(load_vmstate(&vmstate_simple_primitive
, &obj
, &obj_clone
,
261 obj_simple_copy
, 1, wire_simple_primitive
,
262 sizeof(wire_simple_primitive
)));
264 #define FIELD_EQUAL(name) g_assert_cmpint(obj.name, ==, obj_simple.name)
283 typedef struct TestStruct
{
289 static const VMStateDescription vmstate_versioned
= {
290 .name
= "test/versioned",
292 .minimum_version_id
= 1,
293 .fields
= (VMStateField
[]) {
294 VMSTATE_UINT32(a
, TestStruct
),
295 VMSTATE_UINT32_V(b
, TestStruct
, 2), /* Versioned field in the middle, so
296 * we catch bugs more easily.
298 VMSTATE_UINT32(c
, TestStruct
),
299 VMSTATE_UINT64(d
, TestStruct
),
300 VMSTATE_UINT32_V(e
, TestStruct
, 2),
301 VMSTATE_UINT64_V(f
, TestStruct
, 2),
302 VMSTATE_END_OF_LIST()
306 static void test_load_v1(void)
308 QEMUFile
*fsave
= open_test_file(true);
312 0, 0, 0, 0, 0, 0, 0, 40, /* d */
313 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
315 qemu_put_buffer(fsave
, buf
, sizeof(buf
));
318 QEMUFile
*loading
= open_test_file(false);
319 TestStruct obj
= { .b
= 200, .e
= 500, .f
= 600 };
320 vmstate_load_state(loading
, &vmstate_versioned
, &obj
, 1);
321 g_assert(!qemu_file_get_error(loading
));
322 g_assert_cmpint(obj
.a
, ==, 10);
323 g_assert_cmpint(obj
.b
, ==, 200);
324 g_assert_cmpint(obj
.c
, ==, 30);
325 g_assert_cmpint(obj
.d
, ==, 40);
326 g_assert_cmpint(obj
.e
, ==, 500);
327 g_assert_cmpint(obj
.f
, ==, 600);
328 qemu_fclose(loading
);
331 static void test_load_v2(void)
333 QEMUFile
*fsave
= open_test_file(true);
338 0, 0, 0, 0, 0, 0, 0, 40, /* d */
340 0, 0, 0, 0, 0, 0, 0, 60, /* f */
341 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
343 qemu_put_buffer(fsave
, buf
, sizeof(buf
));
346 QEMUFile
*loading
= open_test_file(false);
348 vmstate_load_state(loading
, &vmstate_versioned
, &obj
, 2);
349 g_assert_cmpint(obj
.a
, ==, 10);
350 g_assert_cmpint(obj
.b
, ==, 20);
351 g_assert_cmpint(obj
.c
, ==, 30);
352 g_assert_cmpint(obj
.d
, ==, 40);
353 g_assert_cmpint(obj
.e
, ==, 50);
354 g_assert_cmpint(obj
.f
, ==, 60);
355 qemu_fclose(loading
);
358 static bool test_skip(void *opaque
, int version_id
)
360 TestStruct
*t
= (TestStruct
*)opaque
;
364 static const VMStateDescription vmstate_skipping
= {
367 .minimum_version_id
= 1,
368 .fields
= (VMStateField
[]) {
369 VMSTATE_UINT32(a
, TestStruct
),
370 VMSTATE_UINT32(b
, TestStruct
),
371 VMSTATE_UINT32_TEST(c
, TestStruct
, test_skip
),
372 VMSTATE_UINT64(d
, TestStruct
),
373 VMSTATE_UINT32_TEST(e
, TestStruct
, test_skip
),
374 VMSTATE_UINT64_V(f
, TestStruct
, 2),
375 VMSTATE_END_OF_LIST()
380 static void test_save_noskip(void)
382 QEMUFile
*fsave
= open_test_file(true);
383 TestStruct obj
= { .a
= 1, .b
= 2, .c
= 3, .d
= 4, .e
= 5, .f
= 6,
385 vmstate_save_state(fsave
, &vmstate_skipping
, &obj
, NULL
);
386 g_assert(!qemu_file_get_error(fsave
));
388 uint8_t expected
[] = {
392 0, 0, 0, 0, 0, 0, 0, 4, /* d */
394 0, 0, 0, 0, 0, 0, 0, 6, /* f */
398 compare_vmstate(expected
, sizeof(expected
));
401 static void test_save_skip(void)
403 QEMUFile
*fsave
= open_test_file(true);
404 TestStruct obj
= { .a
= 1, .b
= 2, .c
= 3, .d
= 4, .e
= 5, .f
= 6,
406 vmstate_save_state(fsave
, &vmstate_skipping
, &obj
, NULL
);
407 g_assert(!qemu_file_get_error(fsave
));
409 uint8_t expected
[] = {
412 0, 0, 0, 0, 0, 0, 0, 4, /* d */
413 0, 0, 0, 0, 0, 0, 0, 6, /* f */
417 compare_vmstate(expected
, sizeof(expected
));
420 static void test_load_noskip(void)
422 QEMUFile
*fsave
= open_test_file(true);
427 0, 0, 0, 0, 0, 0, 0, 40, /* d */
429 0, 0, 0, 0, 0, 0, 0, 60, /* f */
430 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
432 qemu_put_buffer(fsave
, buf
, sizeof(buf
));
435 QEMUFile
*loading
= open_test_file(false);
436 TestStruct obj
= { .skip_c_e
= false };
437 vmstate_load_state(loading
, &vmstate_skipping
, &obj
, 2);
438 g_assert(!qemu_file_get_error(loading
));
439 g_assert_cmpint(obj
.a
, ==, 10);
440 g_assert_cmpint(obj
.b
, ==, 20);
441 g_assert_cmpint(obj
.c
, ==, 30);
442 g_assert_cmpint(obj
.d
, ==, 40);
443 g_assert_cmpint(obj
.e
, ==, 50);
444 g_assert_cmpint(obj
.f
, ==, 60);
445 qemu_fclose(loading
);
448 static void test_load_skip(void)
450 QEMUFile
*fsave
= open_test_file(true);
454 0, 0, 0, 0, 0, 0, 0, 40, /* d */
455 0, 0, 0, 0, 0, 0, 0, 60, /* f */
456 QEMU_VM_EOF
, /* just to ensure we won't get EOF reported prematurely */
458 qemu_put_buffer(fsave
, buf
, sizeof(buf
));
461 QEMUFile
*loading
= open_test_file(false);
462 TestStruct obj
= { .skip_c_e
= true, .c
= 300, .e
= 500 };
463 vmstate_load_state(loading
, &vmstate_skipping
, &obj
, 2);
464 g_assert(!qemu_file_get_error(loading
));
465 g_assert_cmpint(obj
.a
, ==, 10);
466 g_assert_cmpint(obj
.b
, ==, 20);
467 g_assert_cmpint(obj
.c
, ==, 300);
468 g_assert_cmpint(obj
.d
, ==, 40);
469 g_assert_cmpint(obj
.e
, ==, 500);
470 g_assert_cmpint(obj
.f
, ==, 60);
471 qemu_fclose(loading
);
474 int main(int argc
, char **argv
)
476 temp_fd
= mkstemp(temp_file
);
478 module_call_init(MODULE_INIT_QOM
);
480 g_test_init(&argc
, &argv
, NULL
);
481 g_test_add_func("/vmstate/simple/primitive", test_simple_primitive
);
482 g_test_add_func("/vmstate/versioned/load/v1", test_load_v1
);
483 g_test_add_func("/vmstate/versioned/load/v2", test_load_v2
);
484 g_test_add_func("/vmstate/field_exists/load/noskip", test_load_noskip
);
485 g_test_add_func("/vmstate/field_exists/load/skip", test_load_skip
);
486 g_test_add_func("/vmstate/field_exists/save/noskip", test_save_noskip
);
487 g_test_add_func("/vmstate/field_exists/save/skip", test_save_skip
);