Merge remote-tracking branch 'remotes/otubo/tags/pull-seccomp-20150123' into staging
[qemu/ar7.git] / tests / test-vmstate.c
blob39b7b01734bbfcbc2367e5c3b55ed7198dd04490
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 <glib.h>
27 #include "qemu-common.h"
28 #include "migration/migration.h"
29 #include "migration/vmstate.h"
30 #include "block/coroutine.h"
32 static char temp_file[] = "/tmp/vmst.test.XXXXXX";
33 static int temp_fd;
35 /* Fake yield_until_fd_readable() implementation so we don't have to pull the
36 * coroutine code as dependency.
38 void yield_until_fd_readable(int fd)
40 fd_set fds;
41 FD_ZERO(&fds);
42 FD_SET(fd, &fds);
43 select(fd + 1, &fds, NULL, NULL, NULL);
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.
52 /* Duplicate temp_fd and seek to the beginning of the file */
53 static QEMUFile *open_test_file(bool write)
55 int fd = dup(temp_fd);
56 lseek(fd, 0, SEEK_SET);
57 if (write) {
58 g_assert_cmpint(ftruncate(fd, 0), ==, 0);
60 return qemu_fdopen(fd, write ? "wb" : "rb");
64 * Check that the contents of the memory-buffered file f match
65 * the given size/data.
67 static void check_mem_file(QEMUFile *f, void *data, size_t size)
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);
77 #define SUCCESS(val) \
78 g_assert_cmpint((val), ==, 0)
80 #define FAILURE(val) \
81 g_assert_cmpint((val), !=, 0)
83 static void save_vmstate(const VMStateDescription *desc, void *obj)
85 QEMUFile *f = open_test_file(true);
87 /* Save file with vmstate */
88 vmstate_save_state(f, desc, obj);
89 qemu_put_byte(f, QEMU_VM_EOF);
90 g_assert(!qemu_file_get_error(f));
91 qemu_fclose(f);
94 static void compare_vmstate(uint8_t *wire, size_t size)
96 QEMUFile *f = open_test_file(false);
97 uint8_t result[size];
99 /* read back as binary */
101 g_assert_cmpint(qemu_get_buffer(f, result, sizeof(result)), ==,
102 sizeof(result));
103 g_assert(!qemu_file_get_error(f));
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)));
109 /* Must reach EOF */
110 qemu_get_byte(f);
111 g_assert_cmpint(qemu_file_get_error(f), ==, -EIO);
113 qemu_fclose(f);
116 static int load_vmstate_one(const VMStateDescription *desc, void *obj,
117 int version, uint8_t *wire, size_t size)
119 QEMUFile *f;
120 int ret;
122 f = open_test_file(true);
123 qemu_put_buffer(f, wire, size);
124 qemu_fclose(f);
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));
133 qemu_fclose(f);
134 return ret;
138 static 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)
143 /* We test with zero size */
144 obj_copy(obj_clone, obj);
145 FAILURE(load_vmstate_one(desc, obj, version, wire, 0));
147 /* Stream ends with QEMU_EOF, so we need at least 3 bytes to be
148 * able to test in the middle */
150 if (size > 3) {
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));
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));
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));
165 obj_copy(obj, obj_clone);
166 return load_vmstate_one(desc, obj, version, wire, size);
169 /* Test struct that we are going to use for our tests */
171 typedef 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;
183 /* Object instantiation, we are going to use it in more than one test */
185 TestSimple 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,
202 /* Description of the values. If you add a primitive type
203 you are expected to add a test here */
205 static 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()
228 /* It describes what goes through the wire. Our tests are basically:
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
240 uint8_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 */
258 static void obj_simple_copy(void *target, void *source)
260 memcpy(target, source, sizeof(TestSimple));
263 static void test_simple_primitive(void)
265 TestSimple obj, obj_clone;
267 memset(&obj, 0, sizeof(obj));
268 save_vmstate(&vmstate_simple_primitive, &obj_simple);
270 compare_vmstate(wire_simple_primitive, sizeof(wire_simple_primitive));
272 SUCCESS(load_vmstate(&vmstate_simple_primitive, &obj, &obj_clone,
273 obj_simple_copy, 1, wire_simple_primitive,
274 sizeof(wire_simple_primitive)));
276 #define FIELD_EQUAL(name) g_assert_cmpint(obj.name, ==, obj_simple.name)
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);
293 #undef FIELD_EQUAL
295 typedef struct TestStruct {
296 uint32_t a, b, c, e;
297 uint64_t d, f;
298 bool skip_c_e;
299 } TestStruct;
301 static const VMStateDescription vmstate_versioned = {
302 .name = "test/versioned",
303 .version_id = 2,
304 .minimum_version_id = 1,
305 .fields = (VMStateField[]) {
306 VMSTATE_UINT32(a, TestStruct),
307 VMSTATE_UINT32_V(b, TestStruct, 2), /* Versioned field in the middle, so
308 * we catch bugs more easily.
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()
318 static void test_load_v1(void)
320 QEMUFile *fsave = open_test_file(true);
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 */
327 qemu_put_buffer(fsave, buf, sizeof(buf));
328 qemu_fclose(fsave);
330 QEMUFile *loading = open_test_file(false);
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);
343 static void test_load_v2(void)
345 QEMUFile *fsave = open_test_file(true);
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 */
355 qemu_put_buffer(fsave, buf, sizeof(buf));
356 qemu_fclose(fsave);
358 QEMUFile *loading = open_test_file(false);
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);
370 static bool test_skip(void *opaque, int version_id)
372 TestStruct *t = (TestStruct *)opaque;
373 return !t->skip_c_e;
376 static const VMStateDescription vmstate_skipping = {
377 .name = "test/skip",
378 .version_id = 2,
379 .minimum_version_id = 1,
380 .fields = (VMStateField[]) {
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()
392 static void test_save_noskip(void)
394 QEMUFile *fsave = qemu_bufopen("w", NULL);
395 TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
396 .skip_c_e = false };
397 vmstate_save_state(fsave, &vmstate_skipping, &obj);
398 g_assert(!qemu_file_get_error(fsave));
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 */
408 check_mem_file(fsave, expected, sizeof(expected));
409 qemu_fclose(fsave);
412 static void test_save_skip(void)
414 QEMUFile *fsave = qemu_bufopen("w", NULL);
415 TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
416 .skip_c_e = true };
417 vmstate_save_state(fsave, &vmstate_skipping, &obj);
418 g_assert(!qemu_file_get_error(fsave));
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 */
426 check_mem_file(fsave, expected, sizeof(expected));
428 qemu_fclose(fsave);
431 static void test_load_noskip(void)
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 */
443 QEMUSizedBuffer *qsb = qsb_create(buf, sizeof(buf));
444 g_assert(qsb);
445 QEMUFile *loading = qemu_bufopen("r", qsb);
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);
456 qsb_free(qsb);
459 static void test_load_skip(void)
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 */
469 QEMUSizedBuffer *qsb = qsb_create(buf, sizeof(buf));
470 g_assert(qsb);
471 QEMUFile *loading = qemu_bufopen("r", qsb);
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);
482 qsb_free(qsb);
485 int main(int argc, char **argv)
487 temp_fd = mkstemp(temp_file);
489 g_test_init(&argc, &argv, NULL);
490 g_test_add_func("/vmstate/simple/primitive", test_simple_primitive);
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();
499 close(temp_fd);
500 unlink(temp_file);
502 return 0;