2013-11-28 18:01:18 +04:00
|
|
|
/*
|
|
|
|
* Test code for VMState
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013 Red Hat Inc.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-02-08 21:08:51 +03:00
|
|
|
#include "qemu/osdep.h"
|
2013-11-28 18:01:18 +04:00
|
|
|
|
|
|
|
#include "qemu-common.h"
|
|
|
|
#include "migration/migration.h"
|
|
|
|
#include "migration/vmstate.h"
|
2015-09-01 16:48:02 +03:00
|
|
|
#include "qemu/coroutine.h"
|
2016-04-27 13:05:08 +03:00
|
|
|
#include "io/channel-file.h"
|
2013-11-28 18:01:18 +04:00
|
|
|
|
2014-07-07 23:03:38 +04:00
|
|
|
static char temp_file[] = "/tmp/vmst.test.XXXXXX";
|
|
|
|
static int temp_fd;
|
2013-11-28 18:01:18 +04:00
|
|
|
|
|
|
|
/* Fake yield_until_fd_readable() implementation so we don't have to pull the
|
|
|
|
* coroutine code as dependency.
|
|
|
|
*/
|
|
|
|
void yield_until_fd_readable(int fd)
|
|
|
|
{
|
|
|
|
fd_set fds;
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(fd, &fds);
|
|
|
|
select(fd + 1, &fds, NULL, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2014-10-10 15:39:05 +04:00
|
|
|
|
2013-11-28 18:01:18 +04:00
|
|
|
/* Duplicate temp_fd and seek to the beginning of the file */
|
2014-04-05 20:34:36 +04:00
|
|
|
static QEMUFile *open_test_file(bool write)
|
2013-11-28 18:01:18 +04:00
|
|
|
{
|
|
|
|
int fd = dup(temp_fd);
|
2016-04-27 13:05:08 +03:00
|
|
|
QIOChannel *ioc;
|
2016-07-07 21:48:06 +03:00
|
|
|
QEMUFile *f;
|
|
|
|
|
2013-11-28 18:01:18 +04:00
|
|
|
lseek(fd, 0, SEEK_SET);
|
2014-04-05 20:34:36 +04:00
|
|
|
if (write) {
|
2013-11-28 18:01:18 +04:00
|
|
|
g_assert_cmpint(ftruncate(fd, 0), ==, 0);
|
|
|
|
}
|
2016-04-27 13:05:08 +03:00
|
|
|
ioc = QIO_CHANNEL(qio_channel_file_new_fd(fd));
|
|
|
|
if (write) {
|
2016-07-07 21:48:06 +03:00
|
|
|
f = qemu_fopen_channel_output(ioc);
|
2016-04-27 13:05:08 +03:00
|
|
|
} else {
|
2016-07-07 21:48:06 +03:00
|
|
|
f = qemu_fopen_channel_input(ioc);
|
2016-04-27 13:05:08 +03:00
|
|
|
}
|
2016-07-07 21:48:06 +03:00
|
|
|
object_unref(OBJECT(ioc));
|
|
|
|
return f;
|
2013-11-28 18:01:18 +04:00
|
|
|
}
|
|
|
|
|
2014-04-05 22:12:51 +04:00
|
|
|
#define SUCCESS(val) \
|
|
|
|
g_assert_cmpint((val), ==, 0)
|
2013-11-28 18:01:18 +04:00
|
|
|
|
2014-04-05 22:12:51 +04:00
|
|
|
#define FAILURE(val) \
|
|
|
|
g_assert_cmpint((val), !=, 0)
|
2013-11-28 18:01:18 +04:00
|
|
|
|
2014-04-05 22:12:51 +04:00
|
|
|
static void save_vmstate(const VMStateDescription *desc, void *obj)
|
|
|
|
{
|
|
|
|
QEMUFile *f = open_test_file(true);
|
|
|
|
|
|
|
|
/* Save file with vmstate */
|
2015-01-22 17:01:39 +03:00
|
|
|
vmstate_save_state(f, desc, obj, NULL);
|
2014-04-05 22:12:51 +04:00
|
|
|
qemu_put_byte(f, QEMU_VM_EOF);
|
|
|
|
g_assert(!qemu_file_get_error(f));
|
|
|
|
qemu_fclose(f);
|
|
|
|
}
|
2013-11-28 18:01:18 +04:00
|
|
|
|
2016-10-21 17:37:38 +03:00
|
|
|
static void save_buffer(const uint8_t *buf, size_t buf_size)
|
|
|
|
{
|
|
|
|
QEMUFile *fsave = open_test_file(true);
|
|
|
|
qemu_put_buffer(fsave, buf, buf_size);
|
|
|
|
qemu_fclose(fsave);
|
|
|
|
}
|
|
|
|
|
2014-04-05 22:12:51 +04:00
|
|
|
static void compare_vmstate(uint8_t *wire, size_t size)
|
2013-11-28 18:01:18 +04:00
|
|
|
{
|
2014-04-05 22:12:51 +04:00
|
|
|
QEMUFile *f = open_test_file(false);
|
|
|
|
uint8_t result[size];
|
2013-11-28 18:01:18 +04:00
|
|
|
|
2014-04-05 22:12:51 +04:00
|
|
|
/* read back as binary */
|
|
|
|
|
|
|
|
g_assert_cmpint(qemu_get_buffer(f, result, sizeof(result)), ==,
|
2013-11-28 18:01:18 +04:00
|
|
|
sizeof(result));
|
2014-04-05 22:12:51 +04:00
|
|
|
g_assert(!qemu_file_get_error(f));
|
|
|
|
|
|
|
|
/* Compare that what is on the file is the same that what we
|
|
|
|
expected to be there */
|
|
|
|
SUCCESS(memcmp(result, wire, sizeof(result)));
|
2013-11-28 18:01:18 +04:00
|
|
|
|
|
|
|
/* Must reach EOF */
|
2014-04-05 22:12:51 +04:00
|
|
|
qemu_get_byte(f);
|
|
|
|
g_assert_cmpint(qemu_file_get_error(f), ==, -EIO);
|
2013-11-28 18:01:18 +04:00
|
|
|
|
2014-04-05 22:12:51 +04:00
|
|
|
qemu_fclose(f);
|
2013-11-28 18:01:18 +04:00
|
|
|
}
|
|
|
|
|
2014-04-05 22:12:51 +04:00
|
|
|
static int load_vmstate_one(const VMStateDescription *desc, void *obj,
|
|
|
|
int version, uint8_t *wire, size_t size)
|
2013-11-28 18:01:18 +04:00
|
|
|
{
|
2014-04-05 22:12:51 +04:00
|
|
|
QEMUFile *f;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
f = open_test_file(true);
|
|
|
|
qemu_put_buffer(f, wire, size);
|
|
|
|
qemu_fclose(f);
|
|
|
|
|
|
|
|
f = open_test_file(false);
|
|
|
|
ret = vmstate_load_state(f, desc, obj, version);
|
|
|
|
if (ret) {
|
|
|
|
g_assert(qemu_file_get_error(f));
|
|
|
|
} else{
|
|
|
|
g_assert(!qemu_file_get_error(f));
|
|
|
|
}
|
|
|
|
qemu_fclose(f);
|
|
|
|
return ret;
|
|
|
|
}
|
2013-11-28 18:01:18 +04:00
|
|
|
|
2014-04-05 22:12:51 +04:00
|
|
|
|
|
|
|
static int load_vmstate(const VMStateDescription *desc,
|
|
|
|
void *obj, void *obj_clone,
|
|
|
|
void (*obj_copy)(void *, void*),
|
|
|
|
int version, uint8_t *wire, size_t size)
|
|
|
|
{
|
|
|
|
/* We test with zero size */
|
|
|
|
obj_copy(obj_clone, obj);
|
|
|
|
FAILURE(load_vmstate_one(desc, obj, version, wire, 0));
|
|
|
|
|
|
|
|
/* Stream ends with QEMU_EOF, so we need at least 3 bytes to be
|
|
|
|
* able to test in the middle */
|
|
|
|
|
|
|
|
if (size > 3) {
|
|
|
|
|
|
|
|
/* We test with size - 2. We can't test size - 1 due to EOF tricks */
|
|
|
|
obj_copy(obj, obj_clone);
|
|
|
|
FAILURE(load_vmstate_one(desc, obj, version, wire, size - 2));
|
|
|
|
|
|
|
|
/* Test with size/2, first half of real state */
|
|
|
|
obj_copy(obj, obj_clone);
|
|
|
|
FAILURE(load_vmstate_one(desc, obj, version, wire, size/2));
|
|
|
|
|
|
|
|
/* Test with size/2, second half of real state */
|
|
|
|
obj_copy(obj, obj_clone);
|
|
|
|
FAILURE(load_vmstate_one(desc, obj, version, wire + (size/2), size/2));
|
|
|
|
|
|
|
|
}
|
|
|
|
obj_copy(obj, obj_clone);
|
|
|
|
return load_vmstate_one(desc, obj, version, wire, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test struct that we are going to use for our tests */
|
|
|
|
|
|
|
|
typedef struct TestSimple {
|
|
|
|
bool b_1, b_2;
|
|
|
|
uint8_t u8_1;
|
|
|
|
uint16_t u16_1;
|
|
|
|
uint32_t u32_1;
|
|
|
|
uint64_t u64_1;
|
|
|
|
int8_t i8_1, i8_2;
|
|
|
|
int16_t i16_1, i16_2;
|
|
|
|
int32_t i32_1, i32_2;
|
|
|
|
int64_t i64_1, i64_2;
|
|
|
|
} TestSimple;
|
|
|
|
|
|
|
|
/* Object instantiation, we are going to use it in more than one test */
|
|
|
|
|
|
|
|
TestSimple obj_simple = {
|
|
|
|
.b_1 = true,
|
|
|
|
.b_2 = false,
|
|
|
|
.u8_1 = 130,
|
|
|
|
.u16_1 = 512,
|
|
|
|
.u32_1 = 70000,
|
|
|
|
.u64_1 = 12121212,
|
|
|
|
.i8_1 = 65,
|
|
|
|
.i8_2 = -65,
|
|
|
|
.i16_1 = 512,
|
|
|
|
.i16_2 = -512,
|
|
|
|
.i32_1 = 70000,
|
|
|
|
.i32_2 = -70000,
|
|
|
|
.i64_1 = 12121212,
|
|
|
|
.i64_2 = -12121212,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Description of the values. If you add a primitive type
|
|
|
|
you are expected to add a test here */
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_simple_primitive = {
|
|
|
|
.name = "simple/primitive",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.fields = (VMStateField[]) {
|
|
|
|
VMSTATE_BOOL(b_1, TestSimple),
|
|
|
|
VMSTATE_BOOL(b_2, TestSimple),
|
|
|
|
VMSTATE_UINT8(u8_1, TestSimple),
|
|
|
|
VMSTATE_UINT16(u16_1, TestSimple),
|
|
|
|
VMSTATE_UINT32(u32_1, TestSimple),
|
|
|
|
VMSTATE_UINT64(u64_1, TestSimple),
|
|
|
|
VMSTATE_INT8(i8_1, TestSimple),
|
|
|
|
VMSTATE_INT8(i8_2, TestSimple),
|
|
|
|
VMSTATE_INT16(i16_1, TestSimple),
|
|
|
|
VMSTATE_INT16(i16_2, TestSimple),
|
|
|
|
VMSTATE_INT32(i32_1, TestSimple),
|
|
|
|
VMSTATE_INT32(i32_2, TestSimple),
|
|
|
|
VMSTATE_INT64(i64_1, TestSimple),
|
|
|
|
VMSTATE_INT64(i64_2, TestSimple),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* It describes what goes through the wire. Our tests are basically:
|
|
|
|
|
|
|
|
* save test
|
|
|
|
- save a struct a vmstate to a file
|
|
|
|
- read that file back (binary read, no vmstate)
|
|
|
|
- compare it with what we expect to be on the wire
|
|
|
|
* load test
|
|
|
|
- save to the file what we expect to be on the wire
|
|
|
|
- read struct back with vmstate in a different
|
|
|
|
- compare back with the original struct
|
|
|
|
*/
|
|
|
|
|
|
|
|
uint8_t wire_simple_primitive[] = {
|
|
|
|
/* b_1 */ 0x01,
|
|
|
|
/* b_2 */ 0x00,
|
|
|
|
/* u8_1 */ 0x82,
|
|
|
|
/* u16_1 */ 0x02, 0x00,
|
|
|
|
/* u32_1 */ 0x00, 0x01, 0x11, 0x70,
|
|
|
|
/* u64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
|
|
|
|
/* i8_1 */ 0x41,
|
|
|
|
/* i8_2 */ 0xbf,
|
|
|
|
/* i16_1 */ 0x02, 0x00,
|
|
|
|
/* i16_2 */ 0xfe, 0x0,
|
|
|
|
/* i32_1 */ 0x00, 0x01, 0x11, 0x70,
|
|
|
|
/* i32_2 */ 0xff, 0xfe, 0xee, 0x90,
|
|
|
|
/* i64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
|
|
|
|
/* i64_2 */ 0xff, 0xff, 0xff, 0xff, 0xff, 0x47, 0x0b, 0x84,
|
|
|
|
QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void obj_simple_copy(void *target, void *source)
|
|
|
|
{
|
|
|
|
memcpy(target, source, sizeof(TestSimple));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_simple_primitive(void)
|
|
|
|
{
|
|
|
|
TestSimple obj, obj_clone;
|
|
|
|
|
|
|
|
memset(&obj, 0, sizeof(obj));
|
|
|
|
save_vmstate(&vmstate_simple_primitive, &obj_simple);
|
|
|
|
|
|
|
|
compare_vmstate(wire_simple_primitive, sizeof(wire_simple_primitive));
|
|
|
|
|
|
|
|
SUCCESS(load_vmstate(&vmstate_simple_primitive, &obj, &obj_clone,
|
|
|
|
obj_simple_copy, 1, wire_simple_primitive,
|
|
|
|
sizeof(wire_simple_primitive)));
|
|
|
|
|
|
|
|
#define FIELD_EQUAL(name) g_assert_cmpint(obj.name, ==, obj_simple.name)
|
|
|
|
|
|
|
|
FIELD_EQUAL(b_1);
|
|
|
|
FIELD_EQUAL(b_2);
|
|
|
|
FIELD_EQUAL(u8_1);
|
|
|
|
FIELD_EQUAL(u16_1);
|
|
|
|
FIELD_EQUAL(u32_1);
|
|
|
|
FIELD_EQUAL(u64_1);
|
|
|
|
FIELD_EQUAL(i8_1);
|
|
|
|
FIELD_EQUAL(i8_2);
|
|
|
|
FIELD_EQUAL(i16_1);
|
|
|
|
FIELD_EQUAL(i16_2);
|
|
|
|
FIELD_EQUAL(i32_1);
|
|
|
|
FIELD_EQUAL(i32_2);
|
|
|
|
FIELD_EQUAL(i64_1);
|
|
|
|
FIELD_EQUAL(i64_2);
|
2013-11-28 18:01:18 +04:00
|
|
|
}
|
2014-04-05 22:12:51 +04:00
|
|
|
#undef FIELD_EQUAL
|
|
|
|
|
|
|
|
typedef struct TestStruct {
|
|
|
|
uint32_t a, b, c, e;
|
|
|
|
uint64_t d, f;
|
|
|
|
bool skip_c_e;
|
|
|
|
} TestStruct;
|
2013-11-28 18:01:18 +04:00
|
|
|
|
|
|
|
static const VMStateDescription vmstate_versioned = {
|
2014-04-05 22:12:51 +04:00
|
|
|
.name = "test/versioned",
|
2013-11-28 18:01:18 +04:00
|
|
|
.version_id = 2,
|
|
|
|
.minimum_version_id = 1,
|
2014-04-16 18:01:33 +04:00
|
|
|
.fields = (VMStateField[]) {
|
2013-11-28 18:01:18 +04:00
|
|
|
VMSTATE_UINT32(a, TestStruct),
|
|
|
|
VMSTATE_UINT32_V(b, TestStruct, 2), /* Versioned field in the middle, so
|
|
|
|
* we catch bugs more easily.
|
|
|
|
*/
|
|
|
|
VMSTATE_UINT32(c, TestStruct),
|
|
|
|
VMSTATE_UINT64(d, TestStruct),
|
|
|
|
VMSTATE_UINT32_V(e, TestStruct, 2),
|
|
|
|
VMSTATE_UINT64_V(f, TestStruct, 2),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void test_load_v1(void)
|
|
|
|
{
|
|
|
|
uint8_t buf[] = {
|
|
|
|
0, 0, 0, 10, /* a */
|
|
|
|
0, 0, 0, 30, /* c */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 40, /* d */
|
|
|
|
QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
|
|
|
|
};
|
2016-10-21 17:37:38 +03:00
|
|
|
save_buffer(buf, sizeof(buf));
|
2013-11-28 18:01:18 +04:00
|
|
|
|
2014-04-05 20:34:36 +04:00
|
|
|
QEMUFile *loading = open_test_file(false);
|
2013-11-28 18:01:18 +04:00
|
|
|
TestStruct obj = { .b = 200, .e = 500, .f = 600 };
|
|
|
|
vmstate_load_state(loading, &vmstate_versioned, &obj, 1);
|
|
|
|
g_assert(!qemu_file_get_error(loading));
|
|
|
|
g_assert_cmpint(obj.a, ==, 10);
|
|
|
|
g_assert_cmpint(obj.b, ==, 200);
|
|
|
|
g_assert_cmpint(obj.c, ==, 30);
|
|
|
|
g_assert_cmpint(obj.d, ==, 40);
|
|
|
|
g_assert_cmpint(obj.e, ==, 500);
|
|
|
|
g_assert_cmpint(obj.f, ==, 600);
|
|
|
|
qemu_fclose(loading);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_load_v2(void)
|
|
|
|
{
|
|
|
|
uint8_t buf[] = {
|
|
|
|
0, 0, 0, 10, /* a */
|
|
|
|
0, 0, 0, 20, /* b */
|
|
|
|
0, 0, 0, 30, /* c */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 40, /* d */
|
|
|
|
0, 0, 0, 50, /* e */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 60, /* f */
|
|
|
|
QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
|
|
|
|
};
|
2016-10-21 17:37:38 +03:00
|
|
|
save_buffer(buf, sizeof(buf));
|
2013-11-28 18:01:18 +04:00
|
|
|
|
2014-04-05 20:34:36 +04:00
|
|
|
QEMUFile *loading = open_test_file(false);
|
2013-11-28 18:01:18 +04:00
|
|
|
TestStruct obj;
|
|
|
|
vmstate_load_state(loading, &vmstate_versioned, &obj, 2);
|
|
|
|
g_assert_cmpint(obj.a, ==, 10);
|
|
|
|
g_assert_cmpint(obj.b, ==, 20);
|
|
|
|
g_assert_cmpint(obj.c, ==, 30);
|
|
|
|
g_assert_cmpint(obj.d, ==, 40);
|
|
|
|
g_assert_cmpint(obj.e, ==, 50);
|
|
|
|
g_assert_cmpint(obj.f, ==, 60);
|
|
|
|
qemu_fclose(loading);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool test_skip(void *opaque, int version_id)
|
|
|
|
{
|
|
|
|
TestStruct *t = (TestStruct *)opaque;
|
|
|
|
return !t->skip_c_e;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_skipping = {
|
2014-04-05 22:12:51 +04:00
|
|
|
.name = "test/skip",
|
2013-11-28 18:01:18 +04:00
|
|
|
.version_id = 2,
|
|
|
|
.minimum_version_id = 1,
|
2014-04-16 18:01:33 +04:00
|
|
|
.fields = (VMStateField[]) {
|
2013-11-28 18:01:18 +04:00
|
|
|
VMSTATE_UINT32(a, TestStruct),
|
|
|
|
VMSTATE_UINT32(b, TestStruct),
|
|
|
|
VMSTATE_UINT32_TEST(c, TestStruct, test_skip),
|
|
|
|
VMSTATE_UINT64(d, TestStruct),
|
|
|
|
VMSTATE_UINT32_TEST(e, TestStruct, test_skip),
|
|
|
|
VMSTATE_UINT64_V(f, TestStruct, 2),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void test_save_noskip(void)
|
|
|
|
{
|
2016-04-27 13:04:53 +03:00
|
|
|
QEMUFile *fsave = open_test_file(true);
|
2013-11-28 18:01:18 +04:00
|
|
|
TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
|
|
|
|
.skip_c_e = false };
|
2015-01-22 17:01:39 +03:00
|
|
|
vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
|
2013-11-28 18:01:18 +04:00
|
|
|
g_assert(!qemu_file_get_error(fsave));
|
|
|
|
|
|
|
|
uint8_t expected[] = {
|
|
|
|
0, 0, 0, 1, /* a */
|
|
|
|
0, 0, 0, 2, /* b */
|
|
|
|
0, 0, 0, 3, /* c */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 4, /* d */
|
|
|
|
0, 0, 0, 5, /* e */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 6, /* f */
|
|
|
|
};
|
2016-04-27 13:04:53 +03:00
|
|
|
|
2014-10-10 15:39:05 +04:00
|
|
|
qemu_fclose(fsave);
|
2016-04-27 13:04:53 +03:00
|
|
|
compare_vmstate(expected, sizeof(expected));
|
2013-11-28 18:01:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_save_skip(void)
|
|
|
|
{
|
2016-04-27 13:04:53 +03:00
|
|
|
QEMUFile *fsave = open_test_file(true);
|
2013-11-28 18:01:18 +04:00
|
|
|
TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
|
|
|
|
.skip_c_e = true };
|
2015-01-22 17:01:39 +03:00
|
|
|
vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
|
2013-11-28 18:01:18 +04:00
|
|
|
g_assert(!qemu_file_get_error(fsave));
|
|
|
|
|
|
|
|
uint8_t expected[] = {
|
|
|
|
0, 0, 0, 1, /* a */
|
|
|
|
0, 0, 0, 2, /* b */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 4, /* d */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 6, /* f */
|
|
|
|
};
|
|
|
|
|
2014-10-10 15:39:05 +04:00
|
|
|
qemu_fclose(fsave);
|
2016-04-27 13:04:53 +03:00
|
|
|
compare_vmstate(expected, sizeof(expected));
|
2013-11-28 18:01:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_load_noskip(void)
|
|
|
|
{
|
|
|
|
uint8_t buf[] = {
|
|
|
|
0, 0, 0, 10, /* a */
|
|
|
|
0, 0, 0, 20, /* b */
|
|
|
|
0, 0, 0, 30, /* c */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 40, /* d */
|
|
|
|
0, 0, 0, 50, /* e */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 60, /* f */
|
|
|
|
QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
|
|
|
|
};
|
2016-10-21 17:37:38 +03:00
|
|
|
save_buffer(buf, sizeof(buf));
|
2013-11-28 18:01:18 +04:00
|
|
|
|
2016-04-27 13:04:53 +03:00
|
|
|
QEMUFile *loading = open_test_file(false);
|
2013-11-28 18:01:18 +04:00
|
|
|
TestStruct obj = { .skip_c_e = false };
|
|
|
|
vmstate_load_state(loading, &vmstate_skipping, &obj, 2);
|
|
|
|
g_assert(!qemu_file_get_error(loading));
|
|
|
|
g_assert_cmpint(obj.a, ==, 10);
|
|
|
|
g_assert_cmpint(obj.b, ==, 20);
|
|
|
|
g_assert_cmpint(obj.c, ==, 30);
|
|
|
|
g_assert_cmpint(obj.d, ==, 40);
|
|
|
|
g_assert_cmpint(obj.e, ==, 50);
|
|
|
|
g_assert_cmpint(obj.f, ==, 60);
|
|
|
|
qemu_fclose(loading);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_load_skip(void)
|
|
|
|
{
|
|
|
|
uint8_t buf[] = {
|
|
|
|
0, 0, 0, 10, /* a */
|
|
|
|
0, 0, 0, 20, /* b */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 40, /* d */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 60, /* f */
|
|
|
|
QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
|
|
|
|
};
|
2016-10-21 17:37:38 +03:00
|
|
|
save_buffer(buf, sizeof(buf));
|
2013-11-28 18:01:18 +04:00
|
|
|
|
2016-04-27 13:04:53 +03:00
|
|
|
QEMUFile *loading = open_test_file(false);
|
2013-11-28 18:01:18 +04:00
|
|
|
TestStruct obj = { .skip_c_e = true, .c = 300, .e = 500 };
|
|
|
|
vmstate_load_state(loading, &vmstate_skipping, &obj, 2);
|
|
|
|
g_assert(!qemu_file_get_error(loading));
|
|
|
|
g_assert_cmpint(obj.a, ==, 10);
|
|
|
|
g_assert_cmpint(obj.b, ==, 20);
|
|
|
|
g_assert_cmpint(obj.c, ==, 300);
|
|
|
|
g_assert_cmpint(obj.d, ==, 40);
|
|
|
|
g_assert_cmpint(obj.e, ==, 500);
|
|
|
|
g_assert_cmpint(obj.f, ==, 60);
|
|
|
|
qemu_fclose(loading);
|
|
|
|
}
|
|
|
|
|
2016-10-21 17:37:39 +03:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int32_t i;
|
|
|
|
} TestStructTriv;
|
|
|
|
|
|
|
|
const VMStateDescription vmsd_tst = {
|
|
|
|
.name = "test/tst",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.fields = (VMStateField[]) {
|
|
|
|
VMSTATE_INT32(i, TestStructTriv),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#define AR_SIZE 4
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
TestStructTriv *ar[AR_SIZE];
|
|
|
|
} TestArrayOfPtrToStuct;
|
|
|
|
|
|
|
|
const VMStateDescription vmsd_arps = {
|
|
|
|
.name = "test/arps",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.fields = (VMStateField[]) {
|
|
|
|
VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(ar, TestArrayOfPtrToStuct,
|
|
|
|
AR_SIZE, 0, vmsd_tst, TestStructTriv),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
static void test_arr_ptr_str_no0_save(void)
|
|
|
|
{
|
|
|
|
TestStructTriv ar[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
|
|
|
|
TestArrayOfPtrToStuct sample = {.ar = {&ar[0], &ar[1], &ar[2], &ar[3]} };
|
|
|
|
uint8_t wire_sample[] = {
|
|
|
|
0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x01,
|
|
|
|
0x00, 0x00, 0x00, 0x02,
|
|
|
|
0x00, 0x00, 0x00, 0x03,
|
|
|
|
QEMU_VM_EOF
|
|
|
|
};
|
|
|
|
|
|
|
|
save_vmstate(&vmsd_arps, &sample);
|
|
|
|
compare_vmstate(wire_sample, sizeof(wire_sample));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_arr_ptr_str_no0_load(void)
|
|
|
|
{
|
|
|
|
TestStructTriv ar_gt[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
|
|
|
|
TestStructTriv ar[AR_SIZE] = {};
|
|
|
|
TestArrayOfPtrToStuct obj = {.ar = {&ar[0], &ar[1], &ar[2], &ar[3]} };
|
|
|
|
int idx;
|
|
|
|
uint8_t wire_sample[] = {
|
|
|
|
0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x01,
|
|
|
|
0x00, 0x00, 0x00, 0x02,
|
|
|
|
0x00, 0x00, 0x00, 0x03,
|
|
|
|
QEMU_VM_EOF
|
|
|
|
};
|
|
|
|
|
|
|
|
save_buffer(wire_sample, sizeof(wire_sample));
|
|
|
|
SUCCESS(load_vmstate_one(&vmsd_arps, &obj, 1,
|
|
|
|
wire_sample, sizeof(wire_sample)));
|
|
|
|
for (idx = 0; idx < AR_SIZE; ++idx) {
|
|
|
|
/* compare the target array ar with the ground truth array ar_gt */
|
|
|
|
g_assert_cmpint(ar_gt[idx].i, ==, ar[idx].i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-28 18:01:18 +04:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
temp_fd = mkstemp(temp_file);
|
|
|
|
|
2016-04-27 13:05:08 +03:00
|
|
|
module_call_init(MODULE_INIT_QOM);
|
|
|
|
|
2013-11-28 18:01:18 +04:00
|
|
|
g_test_init(&argc, &argv, NULL);
|
2014-04-05 22:12:51 +04:00
|
|
|
g_test_add_func("/vmstate/simple/primitive", test_simple_primitive);
|
2013-11-28 18:01:18 +04:00
|
|
|
g_test_add_func("/vmstate/versioned/load/v1", test_load_v1);
|
|
|
|
g_test_add_func("/vmstate/versioned/load/v2", test_load_v2);
|
|
|
|
g_test_add_func("/vmstate/field_exists/load/noskip", test_load_noskip);
|
|
|
|
g_test_add_func("/vmstate/field_exists/load/skip", test_load_skip);
|
|
|
|
g_test_add_func("/vmstate/field_exists/save/noskip", test_save_noskip);
|
|
|
|
g_test_add_func("/vmstate/field_exists/save/skip", test_save_skip);
|
2016-10-21 17:37:39 +03:00
|
|
|
g_test_add_func("/vmstate/array/ptr/str/no0/save",
|
|
|
|
test_arr_ptr_str_no0_save);
|
|
|
|
g_test_add_func("/vmstate/array/ptr/str/no0/load",
|
|
|
|
test_arr_ptr_str_no0_load);
|
2013-11-28 18:01:18 +04:00
|
|
|
g_test_run();
|
|
|
|
|
|
|
|
close(temp_fd);
|
|
|
|
unlink(temp_file);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|