+
+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);