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