migration: remove use of qemu_bufopen from vmstate tests
[qemu/rayw.git] / tests / test-vmstate.c
blobf337cf6ecd719b112cce541625557f50211deac2
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 /* Duplicate temp_fd and seek to the beginning of the file */
49 static QEMUFile *open_test_file(bool write)
51 int fd = dup(temp_fd);
52 lseek(fd, 0, SEEK_SET);
53 if (write) {
54 g_assert_cmpint(ftruncate(fd, 0), ==, 0);
56 return qemu_fdopen(fd, write ? "wb" : "rb");
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 compare_vmstate(uint8_t *wire, size_t size)
78 QEMUFile *f = open_test_file(false);
79 uint8_t result[size];
81 /* read back as binary */
83 g_assert_cmpint(qemu_get_buffer(f, result, sizeof(result)), ==,
84 sizeof(result));
85 g_assert(!qemu_file_get_error(f));
87 /* Compare that what is on the file is the same that what we
88 expected to be there */
89 SUCCESS(memcmp(result, wire, sizeof(result)));
91 /* Must reach EOF */
92 qemu_get_byte(f);
93 g_assert_cmpint(qemu_file_get_error(f), ==, -EIO);
95 qemu_fclose(f);
98 static int load_vmstate_one(const VMStateDescription *desc, void *obj,
99 int version, uint8_t *wire, size_t size)
101 QEMUFile *f;
102 int ret;
104 f = open_test_file(true);
105 qemu_put_buffer(f, wire, size);
106 qemu_fclose(f);
108 f = open_test_file(false);
109 ret = vmstate_load_state(f, desc, obj, version);
110 if (ret) {
111 g_assert(qemu_file_get_error(f));
112 } else{
113 g_assert(!qemu_file_get_error(f));
115 qemu_fclose(f);
116 return ret;
120 static int load_vmstate(const VMStateDescription *desc,
121 void *obj, void *obj_clone,
122 void (*obj_copy)(void *, void*),
123 int version, uint8_t *wire, size_t size)
125 /* We test with zero size */
126 obj_copy(obj_clone, obj);
127 FAILURE(load_vmstate_one(desc, obj, version, wire, 0));
129 /* Stream ends with QEMU_EOF, so we need at least 3 bytes to be
130 * able to test in the middle */
132 if (size > 3) {
134 /* We test with size - 2. We can't test size - 1 due to EOF tricks */
135 obj_copy(obj, obj_clone);
136 FAILURE(load_vmstate_one(desc, obj, version, wire, size - 2));
138 /* Test with size/2, first half of real state */
139 obj_copy(obj, obj_clone);
140 FAILURE(load_vmstate_one(desc, obj, version, wire, size/2));
142 /* Test with size/2, second half of real state */
143 obj_copy(obj, obj_clone);
144 FAILURE(load_vmstate_one(desc, obj, version, wire + (size/2), size/2));
147 obj_copy(obj, obj_clone);
148 return load_vmstate_one(desc, obj, version, wire, size);
151 /* Test struct that we are going to use for our tests */
153 typedef struct TestSimple {
154 bool b_1, b_2;
155 uint8_t u8_1;
156 uint16_t u16_1;
157 uint32_t u32_1;
158 uint64_t u64_1;
159 int8_t i8_1, i8_2;
160 int16_t i16_1, i16_2;
161 int32_t i32_1, i32_2;
162 int64_t i64_1, i64_2;
163 } TestSimple;
165 /* Object instantiation, we are going to use it in more than one test */
167 TestSimple obj_simple = {
168 .b_1 = true,
169 .b_2 = false,
170 .u8_1 = 130,
171 .u16_1 = 512,
172 .u32_1 = 70000,
173 .u64_1 = 12121212,
174 .i8_1 = 65,
175 .i8_2 = -65,
176 .i16_1 = 512,
177 .i16_2 = -512,
178 .i32_1 = 70000,
179 .i32_2 = -70000,
180 .i64_1 = 12121212,
181 .i64_2 = -12121212,
184 /* Description of the values. If you add a primitive type
185 you are expected to add a test here */
187 static const VMStateDescription vmstate_simple_primitive = {
188 .name = "simple/primitive",
189 .version_id = 1,
190 .minimum_version_id = 1,
191 .fields = (VMStateField[]) {
192 VMSTATE_BOOL(b_1, TestSimple),
193 VMSTATE_BOOL(b_2, TestSimple),
194 VMSTATE_UINT8(u8_1, TestSimple),
195 VMSTATE_UINT16(u16_1, TestSimple),
196 VMSTATE_UINT32(u32_1, TestSimple),
197 VMSTATE_UINT64(u64_1, TestSimple),
198 VMSTATE_INT8(i8_1, TestSimple),
199 VMSTATE_INT8(i8_2, TestSimple),
200 VMSTATE_INT16(i16_1, TestSimple),
201 VMSTATE_INT16(i16_2, TestSimple),
202 VMSTATE_INT32(i32_1, TestSimple),
203 VMSTATE_INT32(i32_2, TestSimple),
204 VMSTATE_INT64(i64_1, TestSimple),
205 VMSTATE_INT64(i64_2, TestSimple),
206 VMSTATE_END_OF_LIST()
210 /* It describes what goes through the wire. Our tests are basically:
212 * save test
213 - save a struct a vmstate to a file
214 - read that file back (binary read, no vmstate)
215 - compare it with what we expect to be on the wire
216 * load test
217 - save to the file what we expect to be on the wire
218 - read struct back with vmstate in a different
219 - compare back with the original struct
222 uint8_t wire_simple_primitive[] = {
223 /* b_1 */ 0x01,
224 /* b_2 */ 0x00,
225 /* u8_1 */ 0x82,
226 /* u16_1 */ 0x02, 0x00,
227 /* u32_1 */ 0x00, 0x01, 0x11, 0x70,
228 /* u64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
229 /* i8_1 */ 0x41,
230 /* i8_2 */ 0xbf,
231 /* i16_1 */ 0x02, 0x00,
232 /* i16_2 */ 0xfe, 0x0,
233 /* i32_1 */ 0x00, 0x01, 0x11, 0x70,
234 /* i32_2 */ 0xff, 0xfe, 0xee, 0x90,
235 /* i64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
236 /* i64_2 */ 0xff, 0xff, 0xff, 0xff, 0xff, 0x47, 0x0b, 0x84,
237 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
240 static void obj_simple_copy(void *target, void *source)
242 memcpy(target, source, sizeof(TestSimple));
245 static void test_simple_primitive(void)
247 TestSimple obj, obj_clone;
249 memset(&obj, 0, sizeof(obj));
250 save_vmstate(&vmstate_simple_primitive, &obj_simple);
252 compare_vmstate(wire_simple_primitive, sizeof(wire_simple_primitive));
254 SUCCESS(load_vmstate(&vmstate_simple_primitive, &obj, &obj_clone,
255 obj_simple_copy, 1, wire_simple_primitive,
256 sizeof(wire_simple_primitive)));
258 #define FIELD_EQUAL(name) g_assert_cmpint(obj.name, ==, obj_simple.name)
260 FIELD_EQUAL(b_1);
261 FIELD_EQUAL(b_2);
262 FIELD_EQUAL(u8_1);
263 FIELD_EQUAL(u16_1);
264 FIELD_EQUAL(u32_1);
265 FIELD_EQUAL(u64_1);
266 FIELD_EQUAL(i8_1);
267 FIELD_EQUAL(i8_2);
268 FIELD_EQUAL(i16_1);
269 FIELD_EQUAL(i16_2);
270 FIELD_EQUAL(i32_1);
271 FIELD_EQUAL(i32_2);
272 FIELD_EQUAL(i64_1);
273 FIELD_EQUAL(i64_2);
275 #undef FIELD_EQUAL
277 typedef struct TestStruct {
278 uint32_t a, b, c, e;
279 uint64_t d, f;
280 bool skip_c_e;
281 } TestStruct;
283 static const VMStateDescription vmstate_versioned = {
284 .name = "test/versioned",
285 .version_id = 2,
286 .minimum_version_id = 1,
287 .fields = (VMStateField[]) {
288 VMSTATE_UINT32(a, TestStruct),
289 VMSTATE_UINT32_V(b, TestStruct, 2), /* Versioned field in the middle, so
290 * we catch bugs more easily.
292 VMSTATE_UINT32(c, TestStruct),
293 VMSTATE_UINT64(d, TestStruct),
294 VMSTATE_UINT32_V(e, TestStruct, 2),
295 VMSTATE_UINT64_V(f, TestStruct, 2),
296 VMSTATE_END_OF_LIST()
300 static void test_load_v1(void)
302 QEMUFile *fsave = open_test_file(true);
303 uint8_t buf[] = {
304 0, 0, 0, 10, /* a */
305 0, 0, 0, 30, /* c */
306 0, 0, 0, 0, 0, 0, 0, 40, /* d */
307 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
309 qemu_put_buffer(fsave, buf, sizeof(buf));
310 qemu_fclose(fsave);
312 QEMUFile *loading = open_test_file(false);
313 TestStruct obj = { .b = 200, .e = 500, .f = 600 };
314 vmstate_load_state(loading, &vmstate_versioned, &obj, 1);
315 g_assert(!qemu_file_get_error(loading));
316 g_assert_cmpint(obj.a, ==, 10);
317 g_assert_cmpint(obj.b, ==, 200);
318 g_assert_cmpint(obj.c, ==, 30);
319 g_assert_cmpint(obj.d, ==, 40);
320 g_assert_cmpint(obj.e, ==, 500);
321 g_assert_cmpint(obj.f, ==, 600);
322 qemu_fclose(loading);
325 static void test_load_v2(void)
327 QEMUFile *fsave = open_test_file(true);
328 uint8_t buf[] = {
329 0, 0, 0, 10, /* a */
330 0, 0, 0, 20, /* b */
331 0, 0, 0, 30, /* c */
332 0, 0, 0, 0, 0, 0, 0, 40, /* d */
333 0, 0, 0, 50, /* e */
334 0, 0, 0, 0, 0, 0, 0, 60, /* f */
335 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
337 qemu_put_buffer(fsave, buf, sizeof(buf));
338 qemu_fclose(fsave);
340 QEMUFile *loading = open_test_file(false);
341 TestStruct obj;
342 vmstate_load_state(loading, &vmstate_versioned, &obj, 2);
343 g_assert_cmpint(obj.a, ==, 10);
344 g_assert_cmpint(obj.b, ==, 20);
345 g_assert_cmpint(obj.c, ==, 30);
346 g_assert_cmpint(obj.d, ==, 40);
347 g_assert_cmpint(obj.e, ==, 50);
348 g_assert_cmpint(obj.f, ==, 60);
349 qemu_fclose(loading);
352 static bool test_skip(void *opaque, int version_id)
354 TestStruct *t = (TestStruct *)opaque;
355 return !t->skip_c_e;
358 static const VMStateDescription vmstate_skipping = {
359 .name = "test/skip",
360 .version_id = 2,
361 .minimum_version_id = 1,
362 .fields = (VMStateField[]) {
363 VMSTATE_UINT32(a, TestStruct),
364 VMSTATE_UINT32(b, TestStruct),
365 VMSTATE_UINT32_TEST(c, TestStruct, test_skip),
366 VMSTATE_UINT64(d, TestStruct),
367 VMSTATE_UINT32_TEST(e, TestStruct, test_skip),
368 VMSTATE_UINT64_V(f, TestStruct, 2),
369 VMSTATE_END_OF_LIST()
374 static void test_save_noskip(void)
376 QEMUFile *fsave = open_test_file(true);
377 TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
378 .skip_c_e = false };
379 vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
380 g_assert(!qemu_file_get_error(fsave));
382 uint8_t expected[] = {
383 0, 0, 0, 1, /* a */
384 0, 0, 0, 2, /* b */
385 0, 0, 0, 3, /* c */
386 0, 0, 0, 0, 0, 0, 0, 4, /* d */
387 0, 0, 0, 5, /* e */
388 0, 0, 0, 0, 0, 0, 0, 6, /* f */
391 qemu_fclose(fsave);
392 compare_vmstate(expected, sizeof(expected));
395 static void test_save_skip(void)
397 QEMUFile *fsave = open_test_file(true);
398 TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
399 .skip_c_e = true };
400 vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
401 g_assert(!qemu_file_get_error(fsave));
403 uint8_t expected[] = {
404 0, 0, 0, 1, /* a */
405 0, 0, 0, 2, /* b */
406 0, 0, 0, 0, 0, 0, 0, 4, /* d */
407 0, 0, 0, 0, 0, 0, 0, 6, /* f */
410 qemu_fclose(fsave);
411 compare_vmstate(expected, sizeof(expected));
414 static void test_load_noskip(void)
416 QEMUFile *fsave = open_test_file(true);
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 */
426 qemu_put_buffer(fsave, buf, sizeof(buf));
427 qemu_fclose(fsave);
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 QEMUFile *fsave = open_test_file(true);
445 uint8_t buf[] = {
446 0, 0, 0, 10, /* a */
447 0, 0, 0, 20, /* b */
448 0, 0, 0, 0, 0, 0, 0, 40, /* d */
449 0, 0, 0, 0, 0, 0, 0, 60, /* f */
450 QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
452 qemu_put_buffer(fsave, buf, sizeof(buf));
453 qemu_fclose(fsave);
455 QEMUFile *loading = open_test_file(false);
456 TestStruct obj = { .skip_c_e = true, .c = 300, .e = 500 };
457 vmstate_load_state(loading, &vmstate_skipping, &obj, 2);
458 g_assert(!qemu_file_get_error(loading));
459 g_assert_cmpint(obj.a, ==, 10);
460 g_assert_cmpint(obj.b, ==, 20);
461 g_assert_cmpint(obj.c, ==, 300);
462 g_assert_cmpint(obj.d, ==, 40);
463 g_assert_cmpint(obj.e, ==, 500);
464 g_assert_cmpint(obj.f, ==, 60);
465 qemu_fclose(loading);
468 int main(int argc, char **argv)
470 temp_fd = mkstemp(temp_file);
472 g_test_init(&argc, &argv, NULL);
473 g_test_add_func("/vmstate/simple/primitive", test_simple_primitive);
474 g_test_add_func("/vmstate/versioned/load/v1", test_load_v1);
475 g_test_add_func("/vmstate/versioned/load/v2", test_load_v2);
476 g_test_add_func("/vmstate/field_exists/load/noskip", test_load_noskip);
477 g_test_add_func("/vmstate/field_exists/load/skip", test_load_skip);
478 g_test_add_func("/vmstate/field_exists/save/noskip", test_save_noskip);
479 g_test_add_func("/vmstate/field_exists/save/skip", test_save_skip);
480 g_test_run();
482 close(temp_fd);
483 unlink(temp_file);
485 return 0;