migration: Export qemu-file-channel.c functions in its own file
[qemu/ar7.git] / tests / test-vmstate.c
blob1c135708f4684ce4d260eb3a024d33ad84044978
1 /*
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
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
27 #include "qemu-common.h"
28 #include "migration/migration.h"
29 #include "migration/vmstate.h"
30 #include "../migration/qemu-file-channel.h"
31 #include "qemu/coroutine.h"
32 #include "io/channel-file.h"
34 static char temp_file[] = "/tmp/vmst.test.XXXXXX";
35 static int temp_fd;
38 /* Duplicate temp_fd and seek to the beginning of the file */
39 static QEMUFile *open_test_file(bool write)
41 int fd = dup(temp_fd);
42 QIOChannel *ioc;
43 QEMUFile *f;
45 lseek(fd, 0, SEEK_SET);
46 if (write) {
47 g_assert_cmpint(ftruncate(fd, 0), ==, 0);
49 ioc = QIO_CHANNEL(qio_channel_file_new_fd(fd));
50 if (write) {
51 f = qemu_fopen_channel_output(ioc);
52 } else {
53 f = qemu_fopen_channel_input(ioc);
55 object_unref(OBJECT(ioc));
56 return f;
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));
73 qemu_fclose(f);
76 static void save_buffer(const uint8_t *buf, size_t buf_size)
78 QEMUFile *fsave = open_test_file(true);
79 qemu_put_buffer(fsave, buf, buf_size);
80 qemu_fclose(fsave);
83 static void compare_vmstate(const uint8_t *wire, size_t size)
85 QEMUFile *f = open_test_file(false);
86 uint8_t result[size];
88 /* read back as binary */
90 g_assert_cmpint(qemu_get_buffer(f, result, sizeof(result)), ==,
91 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)));
98 /* Must reach EOF */
99 qemu_get_byte(f);
100 g_assert_cmpint(qemu_file_get_error(f), ==, -EIO);
102 qemu_fclose(f);
105 static int load_vmstate_one(const VMStateDescription *desc, void *obj,
106 int version, const uint8_t *wire, size_t size)
108 QEMUFile *f;
109 int ret;
111 f = open_test_file(true);
112 qemu_put_buffer(f, wire, size);
113 qemu_fclose(f);
115 f = open_test_file(false);
116 ret = vmstate_load_state(f, desc, obj, version);
117 if (ret) {
118 g_assert(qemu_file_get_error(f));
119 } else{
120 g_assert(!qemu_file_get_error(f));
122 qemu_fclose(f);
123 return ret;
127 static int load_vmstate(const VMStateDescription *desc,
128 void *obj, void *obj_clone,
129 void (*obj_copy)(void *, void*),
130 int version, const 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 */
139 if (size > 3) {
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 {
161 bool b_1, b_2;
162 uint8_t u8_1;
163 uint16_t u16_1;
164 uint32_t u32_1;
165 uint64_t u64_1;
166 int8_t i8_1, i8_2;
167 int16_t i16_1, i16_2;
168 int32_t i32_1, i32_2;
169 int64_t i64_1, i64_2;
170 } TestSimple;
172 /* Object instantiation, we are going to use it in more than one test */
174 TestSimple obj_simple = {
175 .b_1 = true,
176 .b_2 = false,
177 .u8_1 = 130,
178 .u16_1 = 512,
179 .u32_1 = 70000,
180 .u64_1 = 12121212,
181 .i8_1 = 65,
182 .i8_2 = -65,
183 .i16_1 = 512,
184 .i16_2 = -512,
185 .i32_1 = 70000,
186 .i32_2 = -70000,
187 .i64_1 = 12121212,
188 .i64_2 = -12121212,
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",
196 .version_id = 1,
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:
219 * save test
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
223 * load test
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[] = {
230 /* b_1 */ 0x01,
231 /* b_2 */ 0x00,
232 /* u8_1 */ 0x82,
233 /* u16_1 */ 0x02, 0x00,
234 /* u32_1 */ 0x00, 0x01, 0x11, 0x70,
235 /* u64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
236 /* i8_1 */ 0x41,
237 /* i8_2 */ 0xbf,
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)
267 FIELD_EQUAL(b_1);
268 FIELD_EQUAL(b_2);
269 FIELD_EQUAL(u8_1);
270 FIELD_EQUAL(u16_1);
271 FIELD_EQUAL(u32_1);
272 FIELD_EQUAL(u64_1);
273 FIELD_EQUAL(i8_1);
274 FIELD_EQUAL(i8_2);
275 FIELD_EQUAL(i16_1);
276 FIELD_EQUAL(i16_2);
277 FIELD_EQUAL(i32_1);
278 FIELD_EQUAL(i32_2);
279 FIELD_EQUAL(i64_1);
280 FIELD_EQUAL(i64_2);
283 typedef struct TestStruct {
284 uint32_t a, b, c, e;
285 uint64_t d, f;
286 bool skip_c_e;
287 } TestStruct;
289 static const VMStateDescription vmstate_versioned = {
290 .name = "test/versioned",
291 .version_id = 2,
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 uint8_t buf[] = {
309 0, 0, 0, 10, /* a */
310 0, 0, 0, 30, /* c */
311 0, 0, 0, 0, 0, 0, 0, 40, /* d */
312 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
314 save_buffer(buf, sizeof(buf));
316 QEMUFile *loading = open_test_file(false);
317 TestStruct obj = { .b = 200, .e = 500, .f = 600 };
318 vmstate_load_state(loading, &vmstate_versioned, &obj, 1);
319 g_assert(!qemu_file_get_error(loading));
320 g_assert_cmpint(obj.a, ==, 10);
321 g_assert_cmpint(obj.b, ==, 200);
322 g_assert_cmpint(obj.c, ==, 30);
323 g_assert_cmpint(obj.d, ==, 40);
324 g_assert_cmpint(obj.e, ==, 500);
325 g_assert_cmpint(obj.f, ==, 600);
326 qemu_fclose(loading);
329 static void test_load_v2(void)
331 uint8_t buf[] = {
332 0, 0, 0, 10, /* a */
333 0, 0, 0, 20, /* b */
334 0, 0, 0, 30, /* c */
335 0, 0, 0, 0, 0, 0, 0, 40, /* d */
336 0, 0, 0, 50, /* e */
337 0, 0, 0, 0, 0, 0, 0, 60, /* f */
338 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
340 save_buffer(buf, sizeof(buf));
342 QEMUFile *loading = open_test_file(false);
343 TestStruct obj;
344 vmstate_load_state(loading, &vmstate_versioned, &obj, 2);
345 g_assert_cmpint(obj.a, ==, 10);
346 g_assert_cmpint(obj.b, ==, 20);
347 g_assert_cmpint(obj.c, ==, 30);
348 g_assert_cmpint(obj.d, ==, 40);
349 g_assert_cmpint(obj.e, ==, 50);
350 g_assert_cmpint(obj.f, ==, 60);
351 qemu_fclose(loading);
354 static bool test_skip(void *opaque, int version_id)
356 TestStruct *t = (TestStruct *)opaque;
357 return !t->skip_c_e;
360 static const VMStateDescription vmstate_skipping = {
361 .name = "test/skip",
362 .version_id = 2,
363 .minimum_version_id = 1,
364 .fields = (VMStateField[]) {
365 VMSTATE_UINT32(a, TestStruct),
366 VMSTATE_UINT32(b, TestStruct),
367 VMSTATE_UINT32_TEST(c, TestStruct, test_skip),
368 VMSTATE_UINT64(d, TestStruct),
369 VMSTATE_UINT32_TEST(e, TestStruct, test_skip),
370 VMSTATE_UINT64_V(f, TestStruct, 2),
371 VMSTATE_END_OF_LIST()
376 static void test_save_noskip(void)
378 QEMUFile *fsave = open_test_file(true);
379 TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
380 .skip_c_e = false };
381 vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
382 g_assert(!qemu_file_get_error(fsave));
384 uint8_t expected[] = {
385 0, 0, 0, 1, /* a */
386 0, 0, 0, 2, /* b */
387 0, 0, 0, 3, /* c */
388 0, 0, 0, 0, 0, 0, 0, 4, /* d */
389 0, 0, 0, 5, /* e */
390 0, 0, 0, 0, 0, 0, 0, 6, /* f */
393 qemu_fclose(fsave);
394 compare_vmstate(expected, sizeof(expected));
397 static void test_save_skip(void)
399 QEMUFile *fsave = open_test_file(true);
400 TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
401 .skip_c_e = true };
402 vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
403 g_assert(!qemu_file_get_error(fsave));
405 uint8_t expected[] = {
406 0, 0, 0, 1, /* a */
407 0, 0, 0, 2, /* b */
408 0, 0, 0, 0, 0, 0, 0, 4, /* d */
409 0, 0, 0, 0, 0, 0, 0, 6, /* f */
412 qemu_fclose(fsave);
413 compare_vmstate(expected, sizeof(expected));
416 static void test_load_noskip(void)
418 uint8_t buf[] = {
419 0, 0, 0, 10, /* a */
420 0, 0, 0, 20, /* b */
421 0, 0, 0, 30, /* c */
422 0, 0, 0, 0, 0, 0, 0, 40, /* d */
423 0, 0, 0, 50, /* e */
424 0, 0, 0, 0, 0, 0, 0, 60, /* f */
425 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
427 save_buffer(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 uint8_t buf[] = {
445 0, 0, 0, 10, /* a */
446 0, 0, 0, 20, /* b */
447 0, 0, 0, 0, 0, 0, 0, 40, /* d */
448 0, 0, 0, 0, 0, 0, 0, 60, /* f */
449 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
451 save_buffer(buf, sizeof(buf));
453 QEMUFile *loading = open_test_file(false);
454 TestStruct obj = { .skip_c_e = true, .c = 300, .e = 500 };
455 vmstate_load_state(loading, &vmstate_skipping, &obj, 2);
456 g_assert(!qemu_file_get_error(loading));
457 g_assert_cmpint(obj.a, ==, 10);
458 g_assert_cmpint(obj.b, ==, 20);
459 g_assert_cmpint(obj.c, ==, 300);
460 g_assert_cmpint(obj.d, ==, 40);
461 g_assert_cmpint(obj.e, ==, 500);
462 g_assert_cmpint(obj.f, ==, 60);
463 qemu_fclose(loading);
466 typedef struct {
467 int32_t i;
468 } TestStructTriv;
470 const VMStateDescription vmsd_tst = {
471 .name = "test/tst",
472 .version_id = 1,
473 .minimum_version_id = 1,
474 .fields = (VMStateField[]) {
475 VMSTATE_INT32(i, TestStructTriv),
476 VMSTATE_END_OF_LIST()
480 /* test array migration */
482 #define AR_SIZE 4
484 typedef struct {
485 TestStructTriv *ar[AR_SIZE];
486 } TestArrayOfPtrToStuct;
488 const VMStateDescription vmsd_arps = {
489 .name = "test/arps",
490 .version_id = 1,
491 .minimum_version_id = 1,
492 .fields = (VMStateField[]) {
493 VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(ar, TestArrayOfPtrToStuct,
494 AR_SIZE, 0, vmsd_tst, TestStructTriv),
495 VMSTATE_END_OF_LIST()
499 static uint8_t wire_arr_ptr_no0[] = {
500 0x00, 0x00, 0x00, 0x00,
501 0x00, 0x00, 0x00, 0x01,
502 0x00, 0x00, 0x00, 0x02,
503 0x00, 0x00, 0x00, 0x03,
504 QEMU_VM_EOF
507 static void test_arr_ptr_str_no0_save(void)
509 TestStructTriv ar[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
510 TestArrayOfPtrToStuct sample = {.ar = {&ar[0], &ar[1], &ar[2], &ar[3]} };
512 save_vmstate(&vmsd_arps, &sample);
513 compare_vmstate(wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0));
516 static void test_arr_ptr_str_no0_load(void)
518 TestStructTriv ar_gt[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
519 TestStructTriv ar[AR_SIZE] = {};
520 TestArrayOfPtrToStuct obj = {.ar = {&ar[0], &ar[1], &ar[2], &ar[3]} };
521 int idx;
523 save_buffer(wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0));
524 SUCCESS(load_vmstate_one(&vmsd_arps, &obj, 1,
525 wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0)));
526 for (idx = 0; idx < AR_SIZE; ++idx) {
527 /* compare the target array ar with the ground truth array ar_gt */
528 g_assert_cmpint(ar_gt[idx].i, ==, ar[idx].i);
532 static uint8_t wire_arr_ptr_0[] = {
533 0x00, 0x00, 0x00, 0x00,
534 VMS_NULLPTR_MARKER,
535 0x00, 0x00, 0x00, 0x02,
536 0x00, 0x00, 0x00, 0x03,
537 QEMU_VM_EOF
540 static void test_arr_ptr_str_0_save(void)
542 TestStructTriv ar[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
543 TestArrayOfPtrToStuct sample = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
545 save_vmstate(&vmsd_arps, &sample);
546 compare_vmstate(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
549 static void test_arr_ptr_str_0_load(void)
551 TestStructTriv ar_gt[AR_SIZE] = {{.i = 0}, {.i = 0}, {.i = 2}, {.i = 3} };
552 TestStructTriv ar[AR_SIZE] = {};
553 TestArrayOfPtrToStuct obj = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
554 int idx;
556 save_buffer(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
557 SUCCESS(load_vmstate_one(&vmsd_arps, &obj, 1,
558 wire_arr_ptr_0, sizeof(wire_arr_ptr_0)));
559 for (idx = 0; idx < AR_SIZE; ++idx) {
560 /* compare the target array ar with the ground truth array ar_gt */
561 g_assert_cmpint(ar_gt[idx].i, ==, ar[idx].i);
563 for (idx = 0; idx < AR_SIZE; ++idx) {
564 if (idx == 1) {
565 g_assert_cmpint((uintptr_t)(obj.ar[idx]), ==, 0);
566 } else {
567 g_assert_cmpint((uintptr_t)(obj.ar[idx]), !=, 0);
572 typedef struct TestArrayOfPtrToInt {
573 int32_t *ar[AR_SIZE];
574 } TestArrayOfPtrToInt;
576 const VMStateDescription vmsd_arpp = {
577 .name = "test/arps",
578 .version_id = 1,
579 .minimum_version_id = 1,
580 .fields = (VMStateField[]) {
581 VMSTATE_ARRAY_OF_POINTER(ar, TestArrayOfPtrToInt,
582 AR_SIZE, 0, vmstate_info_int32, int32_t*),
583 VMSTATE_END_OF_LIST()
587 static void test_arr_ptr_prim_0_save(void)
589 int32_t ar[AR_SIZE] = {0 , 1, 2, 3};
590 TestArrayOfPtrToInt sample = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
592 save_vmstate(&vmsd_arpp, &sample);
593 compare_vmstate(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
596 static void test_arr_ptr_prim_0_load(void)
598 int32_t ar_gt[AR_SIZE] = {0, 1, 2, 3};
599 int32_t ar[AR_SIZE] = {3 , 42, 1, 0};
600 TestArrayOfPtrToInt obj = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
601 int idx;
603 save_buffer(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
604 SUCCESS(load_vmstate_one(&vmsd_arpp, &obj, 1,
605 wire_arr_ptr_0, sizeof(wire_arr_ptr_0)));
606 for (idx = 0; idx < AR_SIZE; ++idx) {
607 /* compare the target array ar with the ground truth array ar_gt */
608 if (idx == 1) {
609 g_assert_cmpint(42, ==, ar[idx]);
610 } else {
611 g_assert_cmpint(ar_gt[idx], ==, ar[idx]);
616 /* test QTAILQ migration */
617 typedef struct TestQtailqElement TestQtailqElement;
619 struct TestQtailqElement {
620 bool b;
621 uint8_t u8;
622 QTAILQ_ENTRY(TestQtailqElement) next;
625 typedef struct TestQtailq {
626 int16_t i16;
627 QTAILQ_HEAD(TestQtailqHead, TestQtailqElement) q;
628 int32_t i32;
629 } TestQtailq;
631 static const VMStateDescription vmstate_q_element = {
632 .name = "test/queue-element",
633 .version_id = 1,
634 .minimum_version_id = 1,
635 .fields = (VMStateField[]) {
636 VMSTATE_BOOL(b, TestQtailqElement),
637 VMSTATE_UINT8(u8, TestQtailqElement),
638 VMSTATE_END_OF_LIST()
642 static const VMStateDescription vmstate_q = {
643 .name = "test/queue",
644 .version_id = 1,
645 .minimum_version_id = 1,
646 .fields = (VMStateField[]) {
647 VMSTATE_INT16(i16, TestQtailq),
648 VMSTATE_QTAILQ_V(q, TestQtailq, 1, vmstate_q_element, TestQtailqElement,
649 next),
650 VMSTATE_INT32(i32, TestQtailq),
651 VMSTATE_END_OF_LIST()
655 uint8_t wire_q[] = {
656 /* i16 */ 0xfe, 0x0,
657 /* start of element 0 of q */ 0x01,
658 /* .b */ 0x01,
659 /* .u8 */ 0x82,
660 /* start of element 1 of q */ 0x01,
661 /* b */ 0x00,
662 /* u8 */ 0x41,
663 /* end of q */ 0x00,
664 /* i32 */ 0x00, 0x01, 0x11, 0x70,
665 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
668 static void test_save_q(void)
670 TestQtailq obj_q = {
671 .i16 = -512,
672 .i32 = 70000,
675 TestQtailqElement obj_qe1 = {
676 .b = true,
677 .u8 = 130,
680 TestQtailqElement obj_qe2 = {
681 .b = false,
682 .u8 = 65,
685 QTAILQ_INIT(&obj_q.q);
686 QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe1, next);
687 QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe2, next);
689 save_vmstate(&vmstate_q, &obj_q);
690 compare_vmstate(wire_q, sizeof(wire_q));
693 static void test_load_q(void)
695 TestQtailq obj_q = {
696 .i16 = -512,
697 .i32 = 70000,
700 TestQtailqElement obj_qe1 = {
701 .b = true,
702 .u8 = 130,
705 TestQtailqElement obj_qe2 = {
706 .b = false,
707 .u8 = 65,
710 QTAILQ_INIT(&obj_q.q);
711 QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe1, next);
712 QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe2, next);
714 QEMUFile *fsave = open_test_file(true);
716 qemu_put_buffer(fsave, wire_q, sizeof(wire_q));
717 g_assert(!qemu_file_get_error(fsave));
718 qemu_fclose(fsave);
720 QEMUFile *fload = open_test_file(false);
721 TestQtailq tgt;
723 QTAILQ_INIT(&tgt.q);
724 vmstate_load_state(fload, &vmstate_q, &tgt, 1);
725 char eof = qemu_get_byte(fload);
726 g_assert(!qemu_file_get_error(fload));
727 g_assert_cmpint(tgt.i16, ==, obj_q.i16);
728 g_assert_cmpint(tgt.i32, ==, obj_q.i32);
729 g_assert_cmpint(eof, ==, QEMU_VM_EOF);
731 TestQtailqElement *qele_from = QTAILQ_FIRST(&obj_q.q);
732 TestQtailqElement *qlast_from = QTAILQ_LAST(&obj_q.q, TestQtailqHead);
733 TestQtailqElement *qele_to = QTAILQ_FIRST(&tgt.q);
734 TestQtailqElement *qlast_to = QTAILQ_LAST(&tgt.q, TestQtailqHead);
736 while (1) {
737 g_assert_cmpint(qele_to->b, ==, qele_from->b);
738 g_assert_cmpint(qele_to->u8, ==, qele_from->u8);
739 if ((qele_from == qlast_from) || (qele_to == qlast_to)) {
740 break;
742 qele_from = QTAILQ_NEXT(qele_from, next);
743 qele_to = QTAILQ_NEXT(qele_to, next);
746 g_assert_cmpint((uintptr_t) qele_from, ==, (uintptr_t) qlast_from);
747 g_assert_cmpint((uintptr_t) qele_to, ==, (uintptr_t) qlast_to);
749 /* clean up */
750 TestQtailqElement *qele;
751 while (!QTAILQ_EMPTY(&tgt.q)) {
752 qele = QTAILQ_LAST(&tgt.q, TestQtailqHead);
753 QTAILQ_REMOVE(&tgt.q, qele, next);
754 free(qele);
755 qele = NULL;
757 qemu_fclose(fload);
760 typedef struct TmpTestStruct {
761 TestStruct *parent;
762 int64_t diff;
763 } TmpTestStruct;
765 static void tmp_child_pre_save(void *opaque)
767 struct TmpTestStruct *tts = opaque;
769 tts->diff = tts->parent->b - tts->parent->a;
772 static int tmp_child_post_load(void *opaque, int version_id)
774 struct TmpTestStruct *tts = opaque;
776 tts->parent->b = tts->parent->a + tts->diff;
778 return 0;
781 static const VMStateDescription vmstate_tmp_back_to_parent = {
782 .name = "test/tmp_child_parent",
783 .fields = (VMStateField[]) {
784 VMSTATE_UINT64(f, TestStruct),
785 VMSTATE_END_OF_LIST()
789 static const VMStateDescription vmstate_tmp_child = {
790 .name = "test/tmp_child",
791 .pre_save = tmp_child_pre_save,
792 .post_load = tmp_child_post_load,
793 .fields = (VMStateField[]) {
794 VMSTATE_INT64(diff, TmpTestStruct),
795 VMSTATE_STRUCT_POINTER(parent, TmpTestStruct,
796 vmstate_tmp_back_to_parent, TestStruct),
797 VMSTATE_END_OF_LIST()
801 static const VMStateDescription vmstate_with_tmp = {
802 .name = "test/with_tmp",
803 .version_id = 1,
804 .fields = (VMStateField[]) {
805 VMSTATE_UINT32(a, TestStruct),
806 VMSTATE_UINT64(d, TestStruct),
807 VMSTATE_WITH_TMP(TestStruct, TmpTestStruct, vmstate_tmp_child),
808 VMSTATE_END_OF_LIST()
812 static void obj_tmp_copy(void *target, void *source)
814 memcpy(target, source, sizeof(TestStruct));
817 static void test_tmp_struct(void)
819 TestStruct obj, obj_clone;
821 uint8_t const wire_with_tmp[] = {
822 /* u32 a */ 0x00, 0x00, 0x00, 0x02,
823 /* u64 d */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
824 /* diff */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
825 /* u64 f */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
826 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
829 memset(&obj, 0, sizeof(obj));
830 obj.a = 2;
831 obj.b = 4;
832 obj.d = 1;
833 obj.f = 8;
834 save_vmstate(&vmstate_with_tmp, &obj);
836 compare_vmstate(wire_with_tmp, sizeof(wire_with_tmp));
838 memset(&obj, 0, sizeof(obj));
839 SUCCESS(load_vmstate(&vmstate_with_tmp, &obj, &obj_clone,
840 obj_tmp_copy, 1, wire_with_tmp,
841 sizeof(wire_with_tmp)));
842 g_assert_cmpint(obj.a, ==, 2); /* From top level vmsd */
843 g_assert_cmpint(obj.b, ==, 4); /* from the post_load */
844 g_assert_cmpint(obj.d, ==, 1); /* From top level vmsd */
845 g_assert_cmpint(obj.f, ==, 8); /* From the child->parent */
848 int main(int argc, char **argv)
850 temp_fd = mkstemp(temp_file);
852 module_call_init(MODULE_INIT_QOM);
854 g_test_init(&argc, &argv, NULL);
855 g_test_add_func("/vmstate/simple/primitive", test_simple_primitive);
856 g_test_add_func("/vmstate/versioned/load/v1", test_load_v1);
857 g_test_add_func("/vmstate/versioned/load/v2", test_load_v2);
858 g_test_add_func("/vmstate/field_exists/load/noskip", test_load_noskip);
859 g_test_add_func("/vmstate/field_exists/load/skip", test_load_skip);
860 g_test_add_func("/vmstate/field_exists/save/noskip", test_save_noskip);
861 g_test_add_func("/vmstate/field_exists/save/skip", test_save_skip);
862 g_test_add_func("/vmstate/array/ptr/str/no0/save",
863 test_arr_ptr_str_no0_save);
864 g_test_add_func("/vmstate/array/ptr/str/no0/load",
865 test_arr_ptr_str_no0_load);
866 g_test_add_func("/vmstate/array/ptr/str/0/save", test_arr_ptr_str_0_save);
867 g_test_add_func("/vmstate/array/ptr/str/0/load",
868 test_arr_ptr_str_0_load);
869 g_test_add_func("/vmstate/array/ptr/prim/0/save",
870 test_arr_ptr_prim_0_save);
871 g_test_add_func("/vmstate/array/ptr/prim/0/load",
872 test_arr_ptr_prim_0_load);
873 g_test_add_func("/vmstate/qtailq/save/saveq", test_save_q);
874 g_test_add_func("/vmstate/qtailq/load/loadq", test_load_q);
875 g_test_add_func("/vmstate/tmp_struct", test_tmp_struct);
876 g_test_run();
878 close(temp_fd);
879 unlink(temp_file);
881 return 0;