]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-vmstate.c
spapr_iommu: Make H_PUT_TCE_INDIRECT endian-safe
[mirror_qemu.git] / tests / test-vmstate.c
CommitLineData
2668b4bf
EH
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 <glib.h>
26
27#include "qemu-common.h"
28#include "migration/migration.h"
29#include "migration/vmstate.h"
30#include "block/coroutine.h"
31
748bfb4e
SW
32static char temp_file[] = "/tmp/vmst.test.XXXXXX";
33static int temp_fd;
2668b4bf
EH
34
35/* Fake yield_until_fd_readable() implementation so we don't have to pull the
36 * coroutine code as dependency.
37 */
38void yield_until_fd_readable(int fd)
39{
40 fd_set fds;
41 FD_ZERO(&fds);
42 FD_SET(fd, &fds);
43 select(fd + 1, &fds, NULL, NULL, NULL);
44}
45
9935baca
DDAG
46/*
47 * Some tests use 'open_test_file' to work on a real fd, some use
48 * an in memory file (QEMUSizedBuffer+qemu_bufopen); we could pick one
49 * but this way we test both.
50 */
51
2668b4bf 52/* Duplicate temp_fd and seek to the beginning of the file */
c6f6646c 53static QEMUFile *open_test_file(bool write)
2668b4bf
EH
54{
55 int fd = dup(temp_fd);
56 lseek(fd, 0, SEEK_SET);
c6f6646c 57 if (write) {
2668b4bf
EH
58 g_assert_cmpint(ftruncate(fd, 0), ==, 0);
59 }
c6f6646c 60 return qemu_fdopen(fd, write ? "wb" : "rb");
2668b4bf
EH
61}
62
9935baca
DDAG
63/*
64 * Check that the contents of the memory-buffered file f match
65 * the given size/data.
66 */
67static void check_mem_file(QEMUFile *f, void *data, size_t size)
68{
69 uint8_t *result = g_malloc(size);
70 const QEMUSizedBuffer *qsb = qemu_buf_get(f);
71 g_assert_cmpint(qsb_get_length(qsb), ==, size);
72 g_assert_cmpint(qsb_get_buffer(qsb, 0, size, result), ==, size);
73 g_assert_cmpint(memcmp(result, data, size), ==, 0);
74 g_free(result);
75}
76
4ea7df4e
JQ
77#define SUCCESS(val) \
78 g_assert_cmpint((val), ==, 0)
2668b4bf 79
4ea7df4e
JQ
80#define FAILURE(val) \
81 g_assert_cmpint((val), !=, 0)
2668b4bf 82
4ea7df4e
JQ
83static void save_vmstate(const VMStateDescription *desc, void *obj)
84{
85 QEMUFile *f = open_test_file(true);
86
87 /* Save file with vmstate */
8118f095 88 vmstate_save_state(f, desc, obj, NULL);
4ea7df4e
JQ
89 qemu_put_byte(f, QEMU_VM_EOF);
90 g_assert(!qemu_file_get_error(f));
91 qemu_fclose(f);
92}
2668b4bf 93
4ea7df4e 94static void compare_vmstate(uint8_t *wire, size_t size)
2668b4bf 95{
4ea7df4e
JQ
96 QEMUFile *f = open_test_file(false);
97 uint8_t result[size];
2668b4bf 98
4ea7df4e
JQ
99 /* read back as binary */
100
101 g_assert_cmpint(qemu_get_buffer(f, result, sizeof(result)), ==,
2668b4bf 102 sizeof(result));
4ea7df4e
JQ
103 g_assert(!qemu_file_get_error(f));
104
105 /* Compare that what is on the file is the same that what we
106 expected to be there */
107 SUCCESS(memcmp(result, wire, sizeof(result)));
2668b4bf
EH
108
109 /* Must reach EOF */
4ea7df4e
JQ
110 qemu_get_byte(f);
111 g_assert_cmpint(qemu_file_get_error(f), ==, -EIO);
2668b4bf 112
4ea7df4e 113 qemu_fclose(f);
2668b4bf
EH
114}
115
4ea7df4e
JQ
116static int load_vmstate_one(const VMStateDescription *desc, void *obj,
117 int version, uint8_t *wire, size_t size)
2668b4bf 118{
4ea7df4e
JQ
119 QEMUFile *f;
120 int ret;
121
122 f = open_test_file(true);
123 qemu_put_buffer(f, wire, size);
124 qemu_fclose(f);
125
126 f = open_test_file(false);
127 ret = vmstate_load_state(f, desc, obj, version);
128 if (ret) {
129 g_assert(qemu_file_get_error(f));
130 } else{
131 g_assert(!qemu_file_get_error(f));
132 }
133 qemu_fclose(f);
134 return ret;
135}
2668b4bf 136
4ea7df4e
JQ
137
138static int load_vmstate(const VMStateDescription *desc,
139 void *obj, void *obj_clone,
140 void (*obj_copy)(void *, void*),
141 int version, uint8_t *wire, size_t size)
142{
143 /* We test with zero size */
144 obj_copy(obj_clone, obj);
145 FAILURE(load_vmstate_one(desc, obj, version, wire, 0));
146
147 /* Stream ends with QEMU_EOF, so we need at least 3 bytes to be
148 * able to test in the middle */
149
150 if (size > 3) {
151
152 /* We test with size - 2. We can't test size - 1 due to EOF tricks */
153 obj_copy(obj, obj_clone);
154 FAILURE(load_vmstate_one(desc, obj, version, wire, size - 2));
155
156 /* Test with size/2, first half of real state */
157 obj_copy(obj, obj_clone);
158 FAILURE(load_vmstate_one(desc, obj, version, wire, size/2));
159
160 /* Test with size/2, second half of real state */
161 obj_copy(obj, obj_clone);
162 FAILURE(load_vmstate_one(desc, obj, version, wire + (size/2), size/2));
163
164 }
165 obj_copy(obj, obj_clone);
166 return load_vmstate_one(desc, obj, version, wire, size);
167}
168
169/* Test struct that we are going to use for our tests */
170
171typedef struct TestSimple {
172 bool b_1, b_2;
173 uint8_t u8_1;
174 uint16_t u16_1;
175 uint32_t u32_1;
176 uint64_t u64_1;
177 int8_t i8_1, i8_2;
178 int16_t i16_1, i16_2;
179 int32_t i32_1, i32_2;
180 int64_t i64_1, i64_2;
181} TestSimple;
182
183/* Object instantiation, we are going to use it in more than one test */
184
185TestSimple obj_simple = {
186 .b_1 = true,
187 .b_2 = false,
188 .u8_1 = 130,
189 .u16_1 = 512,
190 .u32_1 = 70000,
191 .u64_1 = 12121212,
192 .i8_1 = 65,
193 .i8_2 = -65,
194 .i16_1 = 512,
195 .i16_2 = -512,
196 .i32_1 = 70000,
197 .i32_2 = -70000,
198 .i64_1 = 12121212,
199 .i64_2 = -12121212,
200};
201
202/* Description of the values. If you add a primitive type
203 you are expected to add a test here */
204
205static const VMStateDescription vmstate_simple_primitive = {
206 .name = "simple/primitive",
207 .version_id = 1,
208 .minimum_version_id = 1,
209 .fields = (VMStateField[]) {
210 VMSTATE_BOOL(b_1, TestSimple),
211 VMSTATE_BOOL(b_2, TestSimple),
212 VMSTATE_UINT8(u8_1, TestSimple),
213 VMSTATE_UINT16(u16_1, TestSimple),
214 VMSTATE_UINT32(u32_1, TestSimple),
215 VMSTATE_UINT64(u64_1, TestSimple),
216 VMSTATE_INT8(i8_1, TestSimple),
217 VMSTATE_INT8(i8_2, TestSimple),
218 VMSTATE_INT16(i16_1, TestSimple),
219 VMSTATE_INT16(i16_2, TestSimple),
220 VMSTATE_INT32(i32_1, TestSimple),
221 VMSTATE_INT32(i32_2, TestSimple),
222 VMSTATE_INT64(i64_1, TestSimple),
223 VMSTATE_INT64(i64_2, TestSimple),
224 VMSTATE_END_OF_LIST()
225 }
226};
227
228/* It describes what goes through the wire. Our tests are basically:
229
230 * save test
231 - save a struct a vmstate to a file
232 - read that file back (binary read, no vmstate)
233 - compare it with what we expect to be on the wire
234 * load test
235 - save to the file what we expect to be on the wire
236 - read struct back with vmstate in a different
237 - compare back with the original struct
238*/
239
240uint8_t wire_simple_primitive[] = {
241 /* b_1 */ 0x01,
242 /* b_2 */ 0x00,
243 /* u8_1 */ 0x82,
244 /* u16_1 */ 0x02, 0x00,
245 /* u32_1 */ 0x00, 0x01, 0x11, 0x70,
246 /* u64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
247 /* i8_1 */ 0x41,
248 /* i8_2 */ 0xbf,
249 /* i16_1 */ 0x02, 0x00,
250 /* i16_2 */ 0xfe, 0x0,
251 /* i32_1 */ 0x00, 0x01, 0x11, 0x70,
252 /* i32_2 */ 0xff, 0xfe, 0xee, 0x90,
253 /* i64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
254 /* i64_2 */ 0xff, 0xff, 0xff, 0xff, 0xff, 0x47, 0x0b, 0x84,
255 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
256};
257
258static void obj_simple_copy(void *target, void *source)
259{
260 memcpy(target, source, sizeof(TestSimple));
261}
262
263static void test_simple_primitive(void)
264{
265 TestSimple obj, obj_clone;
266
267 memset(&obj, 0, sizeof(obj));
268 save_vmstate(&vmstate_simple_primitive, &obj_simple);
269
270 compare_vmstate(wire_simple_primitive, sizeof(wire_simple_primitive));
271
272 SUCCESS(load_vmstate(&vmstate_simple_primitive, &obj, &obj_clone,
273 obj_simple_copy, 1, wire_simple_primitive,
274 sizeof(wire_simple_primitive)));
275
276#define FIELD_EQUAL(name) g_assert_cmpint(obj.name, ==, obj_simple.name)
277
278 FIELD_EQUAL(b_1);
279 FIELD_EQUAL(b_2);
280 FIELD_EQUAL(u8_1);
281 FIELD_EQUAL(u16_1);
282 FIELD_EQUAL(u32_1);
283 FIELD_EQUAL(u64_1);
284 FIELD_EQUAL(i8_1);
285 FIELD_EQUAL(i8_2);
286 FIELD_EQUAL(i16_1);
287 FIELD_EQUAL(i16_2);
288 FIELD_EQUAL(i32_1);
289 FIELD_EQUAL(i32_2);
290 FIELD_EQUAL(i64_1);
291 FIELD_EQUAL(i64_2);
2668b4bf 292}
4ea7df4e
JQ
293#undef FIELD_EQUAL
294
295typedef struct TestStruct {
296 uint32_t a, b, c, e;
297 uint64_t d, f;
298 bool skip_c_e;
299} TestStruct;
2668b4bf
EH
300
301static const VMStateDescription vmstate_versioned = {
4ea7df4e 302 .name = "test/versioned",
2668b4bf
EH
303 .version_id = 2,
304 .minimum_version_id = 1,
35d08458 305 .fields = (VMStateField[]) {
2668b4bf
EH
306 VMSTATE_UINT32(a, TestStruct),
307 VMSTATE_UINT32_V(b, TestStruct, 2), /* Versioned field in the middle, so
308 * we catch bugs more easily.
309 */
310 VMSTATE_UINT32(c, TestStruct),
311 VMSTATE_UINT64(d, TestStruct),
312 VMSTATE_UINT32_V(e, TestStruct, 2),
313 VMSTATE_UINT64_V(f, TestStruct, 2),
314 VMSTATE_END_OF_LIST()
315 }
316};
317
318static void test_load_v1(void)
319{
c6f6646c 320 QEMUFile *fsave = open_test_file(true);
2668b4bf
EH
321 uint8_t buf[] = {
322 0, 0, 0, 10, /* a */
323 0, 0, 0, 30, /* c */
324 0, 0, 0, 0, 0, 0, 0, 40, /* d */
325 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
326 };
327 qemu_put_buffer(fsave, buf, sizeof(buf));
328 qemu_fclose(fsave);
329
c6f6646c 330 QEMUFile *loading = open_test_file(false);
2668b4bf
EH
331 TestStruct obj = { .b = 200, .e = 500, .f = 600 };
332 vmstate_load_state(loading, &vmstate_versioned, &obj, 1);
333 g_assert(!qemu_file_get_error(loading));
334 g_assert_cmpint(obj.a, ==, 10);
335 g_assert_cmpint(obj.b, ==, 200);
336 g_assert_cmpint(obj.c, ==, 30);
337 g_assert_cmpint(obj.d, ==, 40);
338 g_assert_cmpint(obj.e, ==, 500);
339 g_assert_cmpint(obj.f, ==, 600);
340 qemu_fclose(loading);
341}
342
343static void test_load_v2(void)
344{
c6f6646c 345 QEMUFile *fsave = open_test_file(true);
2668b4bf
EH
346 uint8_t buf[] = {
347 0, 0, 0, 10, /* a */
348 0, 0, 0, 20, /* b */
349 0, 0, 0, 30, /* c */
350 0, 0, 0, 0, 0, 0, 0, 40, /* d */
351 0, 0, 0, 50, /* e */
352 0, 0, 0, 0, 0, 0, 0, 60, /* f */
353 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
354 };
355 qemu_put_buffer(fsave, buf, sizeof(buf));
356 qemu_fclose(fsave);
357
c6f6646c 358 QEMUFile *loading = open_test_file(false);
2668b4bf
EH
359 TestStruct obj;
360 vmstate_load_state(loading, &vmstate_versioned, &obj, 2);
361 g_assert_cmpint(obj.a, ==, 10);
362 g_assert_cmpint(obj.b, ==, 20);
363 g_assert_cmpint(obj.c, ==, 30);
364 g_assert_cmpint(obj.d, ==, 40);
365 g_assert_cmpint(obj.e, ==, 50);
366 g_assert_cmpint(obj.f, ==, 60);
367 qemu_fclose(loading);
368}
369
370static bool test_skip(void *opaque, int version_id)
371{
372 TestStruct *t = (TestStruct *)opaque;
373 return !t->skip_c_e;
374}
375
376static const VMStateDescription vmstate_skipping = {
4ea7df4e 377 .name = "test/skip",
2668b4bf
EH
378 .version_id = 2,
379 .minimum_version_id = 1,
35d08458 380 .fields = (VMStateField[]) {
2668b4bf
EH
381 VMSTATE_UINT32(a, TestStruct),
382 VMSTATE_UINT32(b, TestStruct),
383 VMSTATE_UINT32_TEST(c, TestStruct, test_skip),
384 VMSTATE_UINT64(d, TestStruct),
385 VMSTATE_UINT32_TEST(e, TestStruct, test_skip),
386 VMSTATE_UINT64_V(f, TestStruct, 2),
387 VMSTATE_END_OF_LIST()
388 }
389};
390
391
392static void test_save_noskip(void)
393{
9935baca 394 QEMUFile *fsave = qemu_bufopen("w", NULL);
2668b4bf
EH
395 TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
396 .skip_c_e = false };
8118f095 397 vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
2668b4bf 398 g_assert(!qemu_file_get_error(fsave));
2668b4bf 399
2668b4bf
EH
400 uint8_t expected[] = {
401 0, 0, 0, 1, /* a */
402 0, 0, 0, 2, /* b */
403 0, 0, 0, 3, /* c */
404 0, 0, 0, 0, 0, 0, 0, 4, /* d */
405 0, 0, 0, 5, /* e */
406 0, 0, 0, 0, 0, 0, 0, 6, /* f */
407 };
9935baca
DDAG
408 check_mem_file(fsave, expected, sizeof(expected));
409 qemu_fclose(fsave);
2668b4bf
EH
410}
411
412static void test_save_skip(void)
413{
9935baca 414 QEMUFile *fsave = qemu_bufopen("w", NULL);
2668b4bf
EH
415 TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
416 .skip_c_e = true };
8118f095 417 vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
2668b4bf 418 g_assert(!qemu_file_get_error(fsave));
2668b4bf 419
2668b4bf
EH
420 uint8_t expected[] = {
421 0, 0, 0, 1, /* a */
422 0, 0, 0, 2, /* b */
423 0, 0, 0, 0, 0, 0, 0, 4, /* d */
424 0, 0, 0, 0, 0, 0, 0, 6, /* f */
425 };
9935baca 426 check_mem_file(fsave, expected, sizeof(expected));
2668b4bf 427
9935baca 428 qemu_fclose(fsave);
2668b4bf
EH
429}
430
431static void test_load_noskip(void)
432{
2668b4bf
EH
433 uint8_t buf[] = {
434 0, 0, 0, 10, /* a */
435 0, 0, 0, 20, /* b */
436 0, 0, 0, 30, /* c */
437 0, 0, 0, 0, 0, 0, 0, 40, /* d */
438 0, 0, 0, 50, /* e */
439 0, 0, 0, 0, 0, 0, 0, 60, /* f */
440 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
441 };
2668b4bf 442
8580b064
YH
443 QEMUSizedBuffer *qsb = qsb_create(buf, sizeof(buf));
444 g_assert(qsb);
445 QEMUFile *loading = qemu_bufopen("r", qsb);
2668b4bf
EH
446 TestStruct obj = { .skip_c_e = false };
447 vmstate_load_state(loading, &vmstate_skipping, &obj, 2);
448 g_assert(!qemu_file_get_error(loading));
449 g_assert_cmpint(obj.a, ==, 10);
450 g_assert_cmpint(obj.b, ==, 20);
451 g_assert_cmpint(obj.c, ==, 30);
452 g_assert_cmpint(obj.d, ==, 40);
453 g_assert_cmpint(obj.e, ==, 50);
454 g_assert_cmpint(obj.f, ==, 60);
455 qemu_fclose(loading);
8580b064 456 qsb_free(qsb);
2668b4bf
EH
457}
458
459static void test_load_skip(void)
460{
2668b4bf
EH
461 uint8_t buf[] = {
462 0, 0, 0, 10, /* a */
463 0, 0, 0, 20, /* b */
464 0, 0, 0, 0, 0, 0, 0, 40, /* d */
465 0, 0, 0, 0, 0, 0, 0, 60, /* f */
466 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
467 };
2668b4bf 468
8580b064
YH
469 QEMUSizedBuffer *qsb = qsb_create(buf, sizeof(buf));
470 g_assert(qsb);
471 QEMUFile *loading = qemu_bufopen("r", qsb);
2668b4bf
EH
472 TestStruct obj = { .skip_c_e = true, .c = 300, .e = 500 };
473 vmstate_load_state(loading, &vmstate_skipping, &obj, 2);
474 g_assert(!qemu_file_get_error(loading));
475 g_assert_cmpint(obj.a, ==, 10);
476 g_assert_cmpint(obj.b, ==, 20);
477 g_assert_cmpint(obj.c, ==, 300);
478 g_assert_cmpint(obj.d, ==, 40);
479 g_assert_cmpint(obj.e, ==, 500);
480 g_assert_cmpint(obj.f, ==, 60);
481 qemu_fclose(loading);
8580b064 482 qsb_free(qsb);
2668b4bf
EH
483}
484
485int main(int argc, char **argv)
486{
487 temp_fd = mkstemp(temp_file);
488
489 g_test_init(&argc, &argv, NULL);
4ea7df4e 490 g_test_add_func("/vmstate/simple/primitive", test_simple_primitive);
2668b4bf
EH
491 g_test_add_func("/vmstate/versioned/load/v1", test_load_v1);
492 g_test_add_func("/vmstate/versioned/load/v2", test_load_v2);
493 g_test_add_func("/vmstate/field_exists/load/noskip", test_load_noskip);
494 g_test_add_func("/vmstate/field_exists/load/skip", test_load_skip);
495 g_test_add_func("/vmstate/field_exists/save/noskip", test_save_noskip);
496 g_test_add_func("/vmstate/field_exists/save/skip", test_save_skip);
497 g_test_run();
498
499 close(temp_fd);
500 unlink(temp_file);
501
502 return 0;
503}