]> Git Repo - qemu.git/blob - tests/test-vmstate.c
tests/test-vmstate.c: test array of ptr with null
[qemu.git] / tests / test-vmstate.c
1 /*
2  *  Test code for VMState
3  *
4  *  Copyright (c) 2013 Red Hat Inc.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  */
24
25 #include "qemu/osdep.h"
26
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"
32
33 static char temp_file[] = "/tmp/vmst.test.XXXXXX";
34 static int temp_fd;
35
36
37 /* Duplicate temp_fd and seek to the beginning of the file */
38 static QEMUFile *open_test_file(bool write)
39 {
40     int fd = dup(temp_fd);
41     QIOChannel *ioc;
42     QEMUFile *f;
43
44     lseek(fd, 0, SEEK_SET);
45     if (write) {
46         g_assert_cmpint(ftruncate(fd, 0), ==, 0);
47     }
48     ioc = QIO_CHANNEL(qio_channel_file_new_fd(fd));
49     if (write) {
50         f = qemu_fopen_channel_output(ioc);
51     } else {
52         f = qemu_fopen_channel_input(ioc);
53     }
54     object_unref(OBJECT(ioc));
55     return f;
56 }
57
58 #define SUCCESS(val) \
59     g_assert_cmpint((val), ==, 0)
60
61 #define FAILURE(val) \
62     g_assert_cmpint((val), !=, 0)
63
64 static void save_vmstate(const VMStateDescription *desc, void *obj)
65 {
66     QEMUFile *f = open_test_file(true);
67
68     /* Save file with vmstate */
69     vmstate_save_state(f, desc, obj, NULL);
70     qemu_put_byte(f, QEMU_VM_EOF);
71     g_assert(!qemu_file_get_error(f));
72     qemu_fclose(f);
73 }
74
75 static void save_buffer(const uint8_t *buf, size_t buf_size)
76 {
77     QEMUFile *fsave = open_test_file(true);
78     qemu_put_buffer(fsave, buf, buf_size);
79     qemu_fclose(fsave);
80 }
81
82 static void compare_vmstate(const uint8_t *wire, size_t size)
83 {
84     QEMUFile *f = open_test_file(false);
85     uint8_t result[size];
86
87     /* read back as binary */
88
89     g_assert_cmpint(qemu_get_buffer(f, result, sizeof(result)), ==,
90                     sizeof(result));
91     g_assert(!qemu_file_get_error(f));
92
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)));
96
97     /* Must reach EOF */
98     qemu_get_byte(f);
99     g_assert_cmpint(qemu_file_get_error(f), ==, -EIO);
100
101     qemu_fclose(f);
102 }
103
104 static int load_vmstate_one(const VMStateDescription *desc, void *obj,
105                             int version, const uint8_t *wire, size_t size)
106 {
107     QEMUFile *f;
108     int ret;
109
110     f = open_test_file(true);
111     qemu_put_buffer(f, wire, size);
112     qemu_fclose(f);
113
114     f = open_test_file(false);
115     ret = vmstate_load_state(f, desc, obj, version);
116     if (ret) {
117         g_assert(qemu_file_get_error(f));
118     } else{
119         g_assert(!qemu_file_get_error(f));
120     }
121     qemu_fclose(f);
122     return ret;
123 }
124
125
126 static int load_vmstate(const VMStateDescription *desc,
127                         void *obj, void *obj_clone,
128                         void (*obj_copy)(void *, void*),
129                         int version, const uint8_t *wire, size_t size)
130 {
131     /* We test with zero size */
132     obj_copy(obj_clone, obj);
133     FAILURE(load_vmstate_one(desc, obj, version, wire, 0));
134
135     /* Stream ends with QEMU_EOF, so we need at least 3 bytes to be
136      * able to test in the middle */
137
138     if (size > 3) {
139
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));
143
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));
147
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));
151
152     }
153     obj_copy(obj, obj_clone);
154     return load_vmstate_one(desc, obj, version, wire, size);
155 }
156
157 /* Test struct that we are going to use for our tests */
158
159 typedef struct TestSimple {
160     bool     b_1,   b_2;
161     uint8_t  u8_1;
162     uint16_t u16_1;
163     uint32_t u32_1;
164     uint64_t u64_1;
165     int8_t   i8_1,  i8_2;
166     int16_t  i16_1, i16_2;
167     int32_t  i32_1, i32_2;
168     int64_t  i64_1, i64_2;
169 } TestSimple;
170
171 /* Object instantiation, we are going to use it in more than one test */
172
173 TestSimple obj_simple = {
174     .b_1 = true,
175     .b_2 = false,
176     .u8_1 = 130,
177     .u16_1 = 512,
178     .u32_1 = 70000,
179     .u64_1 = 12121212,
180     .i8_1 = 65,
181     .i8_2 = -65,
182     .i16_1 = 512,
183     .i16_2 = -512,
184     .i32_1 = 70000,
185     .i32_2 = -70000,
186     .i64_1 = 12121212,
187     .i64_2 = -12121212,
188 };
189
190 /* Description of the values.  If you add a primitive type
191    you are expected to add a test here */
192
193 static const VMStateDescription vmstate_simple_primitive = {
194     .name = "simple/primitive",
195     .version_id = 1,
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()
213     }
214 };
215
216 /* It describes what goes through the wire.  Our tests are basically:
217
218    * save test
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
222    * load test
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
226 */
227
228 uint8_t wire_simple_primitive[] = {
229     /* b_1 */   0x01,
230     /* b_2 */   0x00,
231     /* u8_1 */  0x82,
232     /* u16_1 */ 0x02, 0x00,
233     /* u32_1 */ 0x00, 0x01, 0x11, 0x70,
234     /* u64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
235     /* i8_1 */  0x41,
236     /* i8_2 */  0xbf,
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 */
244 };
245
246 static void obj_simple_copy(void *target, void *source)
247 {
248     memcpy(target, source, sizeof(TestSimple));
249 }
250
251 static void test_simple_primitive(void)
252 {
253     TestSimple obj, obj_clone;
254
255     memset(&obj, 0, sizeof(obj));
256     save_vmstate(&vmstate_simple_primitive, &obj_simple);
257
258     compare_vmstate(wire_simple_primitive, sizeof(wire_simple_primitive));
259
260     SUCCESS(load_vmstate(&vmstate_simple_primitive, &obj, &obj_clone,
261                          obj_simple_copy, 1, wire_simple_primitive,
262                          sizeof(wire_simple_primitive)));
263
264 #define FIELD_EQUAL(name)   g_assert_cmpint(obj.name, ==, obj_simple.name)
265
266     FIELD_EQUAL(b_1);
267     FIELD_EQUAL(b_2);
268     FIELD_EQUAL(u8_1);
269     FIELD_EQUAL(u16_1);
270     FIELD_EQUAL(u32_1);
271     FIELD_EQUAL(u64_1);
272     FIELD_EQUAL(i8_1);
273     FIELD_EQUAL(i8_2);
274     FIELD_EQUAL(i16_1);
275     FIELD_EQUAL(i16_2);
276     FIELD_EQUAL(i32_1);
277     FIELD_EQUAL(i32_2);
278     FIELD_EQUAL(i64_1);
279     FIELD_EQUAL(i64_2);
280 }
281
282 typedef struct TestStruct {
283     uint32_t a, b, c, e;
284     uint64_t d, f;
285     bool skip_c_e;
286 } TestStruct;
287
288 static const VMStateDescription vmstate_versioned = {
289     .name = "test/versioned",
290     .version_id = 2,
291     .minimum_version_id = 1,
292     .fields = (VMStateField[]) {
293         VMSTATE_UINT32(a, TestStruct),
294         VMSTATE_UINT32_V(b, TestStruct, 2), /* Versioned field in the middle, so
295                                              * we catch bugs more easily.
296                                              */
297         VMSTATE_UINT32(c, TestStruct),
298         VMSTATE_UINT64(d, TestStruct),
299         VMSTATE_UINT32_V(e, TestStruct, 2),
300         VMSTATE_UINT64_V(f, TestStruct, 2),
301         VMSTATE_END_OF_LIST()
302     }
303 };
304
305 static void test_load_v1(void)
306 {
307     uint8_t buf[] = {
308         0, 0, 0, 10,             /* a */
309         0, 0, 0, 30,             /* c */
310         0, 0, 0, 0, 0, 0, 0, 40, /* d */
311         QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
312     };
313     save_buffer(buf, sizeof(buf));
314
315     QEMUFile *loading = open_test_file(false);
316     TestStruct obj = { .b = 200, .e = 500, .f = 600 };
317     vmstate_load_state(loading, &vmstate_versioned, &obj, 1);
318     g_assert(!qemu_file_get_error(loading));
319     g_assert_cmpint(obj.a, ==, 10);
320     g_assert_cmpint(obj.b, ==, 200);
321     g_assert_cmpint(obj.c, ==, 30);
322     g_assert_cmpint(obj.d, ==, 40);
323     g_assert_cmpint(obj.e, ==, 500);
324     g_assert_cmpint(obj.f, ==, 600);
325     qemu_fclose(loading);
326 }
327
328 static void test_load_v2(void)
329 {
330     uint8_t buf[] = {
331         0, 0, 0, 10,             /* a */
332         0, 0, 0, 20,             /* b */
333         0, 0, 0, 30,             /* c */
334         0, 0, 0, 0, 0, 0, 0, 40, /* d */
335         0, 0, 0, 50,             /* e */
336         0, 0, 0, 0, 0, 0, 0, 60, /* f */
337         QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
338     };
339     save_buffer(buf, sizeof(buf));
340
341     QEMUFile *loading = open_test_file(false);
342     TestStruct obj;
343     vmstate_load_state(loading, &vmstate_versioned, &obj, 2);
344     g_assert_cmpint(obj.a, ==, 10);
345     g_assert_cmpint(obj.b, ==, 20);
346     g_assert_cmpint(obj.c, ==, 30);
347     g_assert_cmpint(obj.d, ==, 40);
348     g_assert_cmpint(obj.e, ==, 50);
349     g_assert_cmpint(obj.f, ==, 60);
350     qemu_fclose(loading);
351 }
352
353 static bool test_skip(void *opaque, int version_id)
354 {
355     TestStruct *t = (TestStruct *)opaque;
356     return !t->skip_c_e;
357 }
358
359 static const VMStateDescription vmstate_skipping = {
360     .name = "test/skip",
361     .version_id = 2,
362     .minimum_version_id = 1,
363     .fields = (VMStateField[]) {
364         VMSTATE_UINT32(a, TestStruct),
365         VMSTATE_UINT32(b, TestStruct),
366         VMSTATE_UINT32_TEST(c, TestStruct, test_skip),
367         VMSTATE_UINT64(d, TestStruct),
368         VMSTATE_UINT32_TEST(e, TestStruct, test_skip),
369         VMSTATE_UINT64_V(f, TestStruct, 2),
370         VMSTATE_END_OF_LIST()
371     }
372 };
373
374
375 static void test_save_noskip(void)
376 {
377     QEMUFile *fsave = open_test_file(true);
378     TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
379                        .skip_c_e = false };
380     vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
381     g_assert(!qemu_file_get_error(fsave));
382
383     uint8_t expected[] = {
384         0, 0, 0, 1,             /* a */
385         0, 0, 0, 2,             /* b */
386         0, 0, 0, 3,             /* c */
387         0, 0, 0, 0, 0, 0, 0, 4, /* d */
388         0, 0, 0, 5,             /* e */
389         0, 0, 0, 0, 0, 0, 0, 6, /* f */
390     };
391
392     qemu_fclose(fsave);
393     compare_vmstate(expected, sizeof(expected));
394 }
395
396 static void test_save_skip(void)
397 {
398     QEMUFile *fsave = open_test_file(true);
399     TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
400                        .skip_c_e = true };
401     vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
402     g_assert(!qemu_file_get_error(fsave));
403
404     uint8_t expected[] = {
405         0, 0, 0, 1,             /* a */
406         0, 0, 0, 2,             /* b */
407         0, 0, 0, 0, 0, 0, 0, 4, /* d */
408         0, 0, 0, 0, 0, 0, 0, 6, /* f */
409     };
410
411     qemu_fclose(fsave);
412     compare_vmstate(expected, sizeof(expected));
413 }
414
415 static void test_load_noskip(void)
416 {
417     uint8_t buf[] = {
418         0, 0, 0, 10,             /* a */
419         0, 0, 0, 20,             /* b */
420         0, 0, 0, 30,             /* c */
421         0, 0, 0, 0, 0, 0, 0, 40, /* d */
422         0, 0, 0, 50,             /* e */
423         0, 0, 0, 0, 0, 0, 0, 60, /* f */
424         QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
425     };
426     save_buffer(buf, sizeof(buf));
427
428     QEMUFile *loading = open_test_file(false);
429     TestStruct obj = { .skip_c_e = false };
430     vmstate_load_state(loading, &vmstate_skipping, &obj, 2);
431     g_assert(!qemu_file_get_error(loading));
432     g_assert_cmpint(obj.a, ==, 10);
433     g_assert_cmpint(obj.b, ==, 20);
434     g_assert_cmpint(obj.c, ==, 30);
435     g_assert_cmpint(obj.d, ==, 40);
436     g_assert_cmpint(obj.e, ==, 50);
437     g_assert_cmpint(obj.f, ==, 60);
438     qemu_fclose(loading);
439 }
440
441 static void test_load_skip(void)
442 {
443     uint8_t buf[] = {
444         0, 0, 0, 10,             /* a */
445         0, 0, 0, 20,             /* b */
446         0, 0, 0, 0, 0, 0, 0, 40, /* d */
447         0, 0, 0, 0, 0, 0, 0, 60, /* f */
448         QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
449     };
450     save_buffer(buf, sizeof(buf));
451
452     QEMUFile *loading = open_test_file(false);
453     TestStruct obj = { .skip_c_e = true, .c = 300, .e = 500 };
454     vmstate_load_state(loading, &vmstate_skipping, &obj, 2);
455     g_assert(!qemu_file_get_error(loading));
456     g_assert_cmpint(obj.a, ==, 10);
457     g_assert_cmpint(obj.b, ==, 20);
458     g_assert_cmpint(obj.c, ==, 300);
459     g_assert_cmpint(obj.d, ==, 40);
460     g_assert_cmpint(obj.e, ==, 500);
461     g_assert_cmpint(obj.f, ==, 60);
462     qemu_fclose(loading);
463 }
464
465 typedef struct {
466     int32_t i;
467 } TestStructTriv;
468
469 const VMStateDescription vmsd_tst = {
470     .name = "test/tst",
471     .version_id = 1,
472     .minimum_version_id = 1,
473     .fields = (VMStateField[]) {
474         VMSTATE_INT32(i, TestStructTriv),
475         VMSTATE_END_OF_LIST()
476     }
477 };
478
479 /* test array migration */
480
481 #define AR_SIZE 4
482
483 typedef struct {
484     TestStructTriv *ar[AR_SIZE];
485 } TestArrayOfPtrToStuct;
486
487 const VMStateDescription vmsd_arps = {
488     .name = "test/arps",
489     .version_id = 1,
490     .minimum_version_id = 1,
491     .fields = (VMStateField[]) {
492         VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(ar, TestArrayOfPtrToStuct,
493                 AR_SIZE, 0, vmsd_tst, TestStructTriv),
494         VMSTATE_END_OF_LIST()
495     }
496 };
497
498 static uint8_t wire_arr_ptr_no0[] = {
499     0x00, 0x00, 0x00, 0x00,
500     0x00, 0x00, 0x00, 0x01,
501     0x00, 0x00, 0x00, 0x02,
502     0x00, 0x00, 0x00, 0x03,
503     QEMU_VM_EOF
504 };
505
506 static void test_arr_ptr_str_no0_save(void)
507 {
508     TestStructTriv ar[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
509     TestArrayOfPtrToStuct sample = {.ar = {&ar[0], &ar[1], &ar[2], &ar[3]} };
510
511     save_vmstate(&vmsd_arps, &sample);
512     compare_vmstate(wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0));
513 }
514
515 static void test_arr_ptr_str_no0_load(void)
516 {
517     TestStructTriv ar_gt[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
518     TestStructTriv ar[AR_SIZE] = {};
519     TestArrayOfPtrToStuct obj = {.ar = {&ar[0], &ar[1], &ar[2], &ar[3]} };
520     int idx;
521
522     save_buffer(wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0));
523     SUCCESS(load_vmstate_one(&vmsd_arps, &obj, 1,
524                           wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0)));
525     for (idx = 0; idx < AR_SIZE; ++idx) {
526         /* compare the target array ar with the ground truth array ar_gt */
527         g_assert_cmpint(ar_gt[idx].i, ==, ar[idx].i);
528     }
529 }
530
531 static uint8_t wire_arr_ptr_0[] = {
532     0x00, 0x00, 0x00, 0x00,
533     VMS_NULLPTR_MARKER,
534     0x00, 0x00, 0x00, 0x02,
535     0x00, 0x00, 0x00, 0x03,
536     QEMU_VM_EOF
537 };
538
539 static void test_arr_ptr_str_0_save(void)
540 {
541     TestStructTriv ar[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
542     TestArrayOfPtrToStuct sample = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
543
544     save_vmstate(&vmsd_arps, &sample);
545     compare_vmstate(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
546 }
547
548 static void test_arr_ptr_str_0_load(void)
549 {
550     TestStructTriv ar_gt[AR_SIZE] = {{.i = 0}, {.i = 0}, {.i = 2}, {.i = 3} };
551     TestStructTriv ar[AR_SIZE] = {};
552     TestArrayOfPtrToStuct obj = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
553     int idx;
554
555     save_buffer(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
556     SUCCESS(load_vmstate_one(&vmsd_arps, &obj, 1,
557                           wire_arr_ptr_0, sizeof(wire_arr_ptr_0)));
558     for (idx = 0; idx < AR_SIZE; ++idx) {
559         /* compare the target array ar with the ground truth array ar_gt */
560         g_assert_cmpint(ar_gt[idx].i, ==, ar[idx].i);
561     }
562     for (idx = 0; idx < AR_SIZE; ++idx) {
563         if (idx == 1) {
564             g_assert_cmpint((uintptr_t)(obj.ar[idx]), ==, 0);
565         } else {
566             g_assert_cmpint((uintptr_t)(obj.ar[idx]), !=, 0);
567         }
568     }
569 }
570
571 /* test QTAILQ migration */
572 typedef struct TestQtailqElement TestQtailqElement;
573
574 struct TestQtailqElement {
575     bool     b;
576     uint8_t  u8;
577     QTAILQ_ENTRY(TestQtailqElement) next;
578 };
579
580 typedef struct TestQtailq {
581     int16_t  i16;
582     QTAILQ_HEAD(TestQtailqHead, TestQtailqElement) q;
583     int32_t  i32;
584 } TestQtailq;
585
586 static const VMStateDescription vmstate_q_element = {
587     .name = "test/queue-element",
588     .version_id = 1,
589     .minimum_version_id = 1,
590     .fields = (VMStateField[]) {
591         VMSTATE_BOOL(b, TestQtailqElement),
592         VMSTATE_UINT8(u8, TestQtailqElement),
593         VMSTATE_END_OF_LIST()
594     },
595 };
596
597 static const VMStateDescription vmstate_q = {
598     .name = "test/queue",
599     .version_id = 1,
600     .minimum_version_id = 1,
601     .fields = (VMStateField[]) {
602         VMSTATE_INT16(i16, TestQtailq),
603         VMSTATE_QTAILQ_V(q, TestQtailq, 1, vmstate_q_element, TestQtailqElement,
604                          next),
605         VMSTATE_INT32(i32, TestQtailq),
606         VMSTATE_END_OF_LIST()
607     }
608 };
609
610 uint8_t wire_q[] = {
611     /* i16 */                     0xfe, 0x0,
612     /* start of element 0 of q */ 0x01,
613     /* .b  */                     0x01,
614     /* .u8 */                     0x82,
615     /* start of element 1 of q */ 0x01,
616     /* b */                       0x00,
617     /* u8 */                      0x41,
618     /* end of q */                0x00,
619     /* i32 */                     0x00, 0x01, 0x11, 0x70,
620     QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
621 };
622
623 static void test_save_q(void)
624 {
625     TestQtailq obj_q = {
626         .i16 = -512,
627         .i32 = 70000,
628     };
629
630     TestQtailqElement obj_qe1 = {
631         .b = true,
632         .u8 = 130,
633     };
634
635     TestQtailqElement obj_qe2 = {
636         .b = false,
637         .u8 = 65,
638     };
639
640     QTAILQ_INIT(&obj_q.q);
641     QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe1, next);
642     QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe2, next);
643
644     save_vmstate(&vmstate_q, &obj_q);
645     compare_vmstate(wire_q, sizeof(wire_q));
646 }
647
648 static void test_load_q(void)
649 {
650     TestQtailq obj_q = {
651         .i16 = -512,
652         .i32 = 70000,
653     };
654
655     TestQtailqElement obj_qe1 = {
656         .b = true,
657         .u8 = 130,
658     };
659
660     TestQtailqElement obj_qe2 = {
661         .b = false,
662         .u8 = 65,
663     };
664
665     QTAILQ_INIT(&obj_q.q);
666     QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe1, next);
667     QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe2, next);
668
669     QEMUFile *fsave = open_test_file(true);
670
671     qemu_put_buffer(fsave, wire_q, sizeof(wire_q));
672     g_assert(!qemu_file_get_error(fsave));
673     qemu_fclose(fsave);
674
675     QEMUFile *fload = open_test_file(false);
676     TestQtailq tgt;
677
678     QTAILQ_INIT(&tgt.q);
679     vmstate_load_state(fload, &vmstate_q, &tgt, 1);
680     char eof = qemu_get_byte(fload);
681     g_assert(!qemu_file_get_error(fload));
682     g_assert_cmpint(tgt.i16, ==, obj_q.i16);
683     g_assert_cmpint(tgt.i32, ==, obj_q.i32);
684     g_assert_cmpint(eof, ==, QEMU_VM_EOF);
685
686     TestQtailqElement *qele_from = QTAILQ_FIRST(&obj_q.q);
687     TestQtailqElement *qlast_from = QTAILQ_LAST(&obj_q.q, TestQtailqHead);
688     TestQtailqElement *qele_to = QTAILQ_FIRST(&tgt.q);
689     TestQtailqElement *qlast_to = QTAILQ_LAST(&tgt.q, TestQtailqHead);
690
691     while (1) {
692         g_assert_cmpint(qele_to->b, ==, qele_from->b);
693         g_assert_cmpint(qele_to->u8, ==, qele_from->u8);
694         if ((qele_from == qlast_from) || (qele_to == qlast_to)) {
695             break;
696         }
697         qele_from = QTAILQ_NEXT(qele_from, next);
698         qele_to = QTAILQ_NEXT(qele_to, next);
699     }
700
701     g_assert_cmpint((uintptr_t) qele_from, ==, (uintptr_t) qlast_from);
702     g_assert_cmpint((uintptr_t) qele_to, ==, (uintptr_t) qlast_to);
703
704     /* clean up */
705     TestQtailqElement *qele;
706     while (!QTAILQ_EMPTY(&tgt.q)) {
707         qele = QTAILQ_LAST(&tgt.q, TestQtailqHead);
708         QTAILQ_REMOVE(&tgt.q, qele, next);
709         free(qele);
710         qele = NULL;
711     }
712     qemu_fclose(fload);
713 }
714
715 typedef struct TmpTestStruct {
716     TestStruct *parent;
717     int64_t diff;
718 } TmpTestStruct;
719
720 static void tmp_child_pre_save(void *opaque)
721 {
722     struct TmpTestStruct *tts = opaque;
723
724     tts->diff = tts->parent->b - tts->parent->a;
725 }
726
727 static int tmp_child_post_load(void *opaque, int version_id)
728 {
729     struct TmpTestStruct *tts = opaque;
730
731     tts->parent->b = tts->parent->a + tts->diff;
732
733     return 0;
734 }
735
736 static const VMStateDescription vmstate_tmp_back_to_parent = {
737     .name = "test/tmp_child_parent",
738     .fields = (VMStateField[]) {
739         VMSTATE_UINT64(f, TestStruct),
740         VMSTATE_END_OF_LIST()
741     }
742 };
743
744 static const VMStateDescription vmstate_tmp_child = {
745     .name = "test/tmp_child",
746     .pre_save = tmp_child_pre_save,
747     .post_load = tmp_child_post_load,
748     .fields = (VMStateField[]) {
749         VMSTATE_INT64(diff, TmpTestStruct),
750         VMSTATE_STRUCT_POINTER(parent, TmpTestStruct,
751                                vmstate_tmp_back_to_parent, TestStruct),
752         VMSTATE_END_OF_LIST()
753     }
754 };
755
756 static const VMStateDescription vmstate_with_tmp = {
757     .name = "test/with_tmp",
758     .version_id = 1,
759     .fields = (VMStateField[]) {
760         VMSTATE_UINT32(a, TestStruct),
761         VMSTATE_UINT64(d, TestStruct),
762         VMSTATE_WITH_TMP(TestStruct, TmpTestStruct, vmstate_tmp_child),
763         VMSTATE_END_OF_LIST()
764     }
765 };
766
767 static void obj_tmp_copy(void *target, void *source)
768 {
769     memcpy(target, source, sizeof(TestStruct));
770 }
771
772 static void test_tmp_struct(void)
773 {
774     TestStruct obj, obj_clone;
775
776     uint8_t const wire_with_tmp[] = {
777         /* u32 a */ 0x00, 0x00, 0x00, 0x02,
778         /* u64 d */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
779         /* diff  */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
780         /* u64 f */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
781         QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
782     };
783
784     memset(&obj, 0, sizeof(obj));
785     obj.a = 2;
786     obj.b = 4;
787     obj.d = 1;
788     obj.f = 8;
789     save_vmstate(&vmstate_with_tmp, &obj);
790
791     compare_vmstate(wire_with_tmp, sizeof(wire_with_tmp));
792
793     memset(&obj, 0, sizeof(obj));
794     SUCCESS(load_vmstate(&vmstate_with_tmp, &obj, &obj_clone,
795                          obj_tmp_copy, 1, wire_with_tmp,
796                          sizeof(wire_with_tmp)));
797     g_assert_cmpint(obj.a, ==, 2); /* From top level vmsd */
798     g_assert_cmpint(obj.b, ==, 4); /* from the post_load */
799     g_assert_cmpint(obj.d, ==, 1); /* From top level vmsd */
800     g_assert_cmpint(obj.f, ==, 8); /* From the child->parent */
801 }
802
803 int main(int argc, char **argv)
804 {
805     temp_fd = mkstemp(temp_file);
806
807     module_call_init(MODULE_INIT_QOM);
808
809     g_test_init(&argc, &argv, NULL);
810     g_test_add_func("/vmstate/simple/primitive", test_simple_primitive);
811     g_test_add_func("/vmstate/versioned/load/v1", test_load_v1);
812     g_test_add_func("/vmstate/versioned/load/v2", test_load_v2);
813     g_test_add_func("/vmstate/field_exists/load/noskip", test_load_noskip);
814     g_test_add_func("/vmstate/field_exists/load/skip", test_load_skip);
815     g_test_add_func("/vmstate/field_exists/save/noskip", test_save_noskip);
816     g_test_add_func("/vmstate/field_exists/save/skip", test_save_skip);
817     g_test_add_func("/vmstate/array/ptr/str/no0/save",
818                     test_arr_ptr_str_no0_save);
819     g_test_add_func("/vmstate/array/ptr/str/no0/load",
820                     test_arr_ptr_str_no0_load);
821     g_test_add_func("/vmstate/array/ptr/str/0/save", test_arr_ptr_str_0_save);
822     g_test_add_func("/vmstate/array/ptr/str/0/load",
823                     test_arr_ptr_str_0_load);
824     g_test_add_func("/vmstate/qtailq/save/saveq", test_save_q);
825     g_test_add_func("/vmstate/qtailq/load/loadq", test_load_q);
826     g_test_add_func("/vmstate/tmp_struct", test_tmp_struct);
827     g_test_run();
828
829     close(temp_fd);
830     unlink(temp_file);
831
832     return 0;
833 }
This page took 0.067415 seconds and 4 git commands to generate.