]>
Commit | Line | Data |
---|---|---|
2668b4bf EH |
1 | /* |
2 | * Test code for VMState | |
3 | * | |
4 | * Copyright (c) 2013 Red Hat Inc. | |
5 | * | |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
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. | |
23 | */ | |
24 | ||
681c28a3 | 25 | #include "qemu/osdep.h" |
2668b4bf EH |
26 | |
27 | #include "qemu-common.h" | |
28 | #include "migration/migration.h" | |
29 | #include "migration/vmstate.h" | |
08a0aee1 JQ |
30 | #include "migration/qemu-file-types.h" |
31 | #include "../migration/qemu-file.h" | |
40014d81 | 32 | #include "../migration/qemu-file-channel.h" |
10817bf0 | 33 | #include "qemu/coroutine.h" |
8925839f | 34 | #include "io/channel-file.h" |
2668b4bf | 35 | |
748bfb4e SW |
36 | static char temp_file[] = "/tmp/vmst.test.XXXXXX"; |
37 | static int temp_fd; | |
2668b4bf | 38 | |
9935baca | 39 | |
2668b4bf | 40 | /* Duplicate temp_fd and seek to the beginning of the file */ |
c6f6646c | 41 | static QEMUFile *open_test_file(bool write) |
2668b4bf EH |
42 | { |
43 | int fd = dup(temp_fd); | |
8925839f | 44 | QIOChannel *ioc; |
4ae3c0e2 MAL |
45 | QEMUFile *f; |
46 | ||
2668b4bf | 47 | lseek(fd, 0, SEEK_SET); |
c6f6646c | 48 | if (write) { |
2668b4bf EH |
49 | g_assert_cmpint(ftruncate(fd, 0), ==, 0); |
50 | } | |
8925839f DB |
51 | ioc = QIO_CHANNEL(qio_channel_file_new_fd(fd)); |
52 | if (write) { | |
4ae3c0e2 | 53 | f = qemu_fopen_channel_output(ioc); |
8925839f | 54 | } else { |
4ae3c0e2 | 55 | f = qemu_fopen_channel_input(ioc); |
8925839f | 56 | } |
4ae3c0e2 MAL |
57 | object_unref(OBJECT(ioc)); |
58 | return f; | |
2668b4bf EH |
59 | } |
60 | ||
4ea7df4e JQ |
61 | #define SUCCESS(val) \ |
62 | g_assert_cmpint((val), ==, 0) | |
2668b4bf | 63 | |
4ea7df4e JQ |
64 | #define FAILURE(val) \ |
65 | g_assert_cmpint((val), !=, 0) | |
2668b4bf | 66 | |
4ea7df4e JQ |
67 | static void save_vmstate(const VMStateDescription *desc, void *obj) |
68 | { | |
69 | QEMUFile *f = open_test_file(true); | |
70 | ||
71 | /* Save file with vmstate */ | |
8118f095 | 72 | vmstate_save_state(f, desc, obj, NULL); |
4ea7df4e JQ |
73 | qemu_put_byte(f, QEMU_VM_EOF); |
74 | g_assert(!qemu_file_get_error(f)); | |
75 | qemu_fclose(f); | |
76 | } | |
2668b4bf | 77 | |
6d57b4c0 HP |
78 | static void save_buffer(const uint8_t *buf, size_t buf_size) |
79 | { | |
80 | QEMUFile *fsave = open_test_file(true); | |
81 | qemu_put_buffer(fsave, buf, buf_size); | |
82 | qemu_fclose(fsave); | |
83 | } | |
84 | ||
5c379d90 | 85 | static void compare_vmstate(const uint8_t *wire, size_t size) |
2668b4bf | 86 | { |
4ea7df4e JQ |
87 | QEMUFile *f = open_test_file(false); |
88 | uint8_t result[size]; | |
2668b4bf | 89 | |
4ea7df4e JQ |
90 | /* read back as binary */ |
91 | ||
92 | g_assert_cmpint(qemu_get_buffer(f, result, sizeof(result)), ==, | |
2668b4bf | 93 | sizeof(result)); |
4ea7df4e JQ |
94 | g_assert(!qemu_file_get_error(f)); |
95 | ||
96 | /* Compare that what is on the file is the same that what we | |
97 | expected to be there */ | |
98 | SUCCESS(memcmp(result, wire, sizeof(result))); | |
2668b4bf EH |
99 | |
100 | /* Must reach EOF */ | |
4ea7df4e JQ |
101 | qemu_get_byte(f); |
102 | g_assert_cmpint(qemu_file_get_error(f), ==, -EIO); | |
2668b4bf | 103 | |
4ea7df4e | 104 | qemu_fclose(f); |
2668b4bf EH |
105 | } |
106 | ||
4ea7df4e | 107 | static int load_vmstate_one(const VMStateDescription *desc, void *obj, |
5c379d90 | 108 | int version, const uint8_t *wire, size_t size) |
2668b4bf | 109 | { |
4ea7df4e JQ |
110 | QEMUFile *f; |
111 | int ret; | |
112 | ||
113 | f = open_test_file(true); | |
114 | qemu_put_buffer(f, wire, size); | |
115 | qemu_fclose(f); | |
116 | ||
117 | f = open_test_file(false); | |
118 | ret = vmstate_load_state(f, desc, obj, version); | |
119 | if (ret) { | |
120 | g_assert(qemu_file_get_error(f)); | |
121 | } else{ | |
122 | g_assert(!qemu_file_get_error(f)); | |
123 | } | |
124 | qemu_fclose(f); | |
125 | return ret; | |
126 | } | |
2668b4bf | 127 | |
4ea7df4e JQ |
128 | |
129 | static int load_vmstate(const VMStateDescription *desc, | |
130 | void *obj, void *obj_clone, | |
131 | void (*obj_copy)(void *, void*), | |
5c379d90 | 132 | int version, const uint8_t *wire, size_t size) |
4ea7df4e JQ |
133 | { |
134 | /* We test with zero size */ | |
135 | obj_copy(obj_clone, obj); | |
136 | FAILURE(load_vmstate_one(desc, obj, version, wire, 0)); | |
137 | ||
138 | /* Stream ends with QEMU_EOF, so we need at least 3 bytes to be | |
139 | * able to test in the middle */ | |
140 | ||
141 | if (size > 3) { | |
142 | ||
143 | /* We test with size - 2. We can't test size - 1 due to EOF tricks */ | |
144 | obj_copy(obj, obj_clone); | |
145 | FAILURE(load_vmstate_one(desc, obj, version, wire, size - 2)); | |
146 | ||
147 | /* Test with size/2, first half of real state */ | |
148 | obj_copy(obj, obj_clone); | |
149 | FAILURE(load_vmstate_one(desc, obj, version, wire, size/2)); | |
150 | ||
151 | /* Test with size/2, second half of real state */ | |
152 | obj_copy(obj, obj_clone); | |
153 | FAILURE(load_vmstate_one(desc, obj, version, wire + (size/2), size/2)); | |
154 | ||
155 | } | |
156 | obj_copy(obj, obj_clone); | |
157 | return load_vmstate_one(desc, obj, version, wire, size); | |
158 | } | |
159 | ||
160 | /* Test struct that we are going to use for our tests */ | |
161 | ||
162 | typedef struct TestSimple { | |
163 | bool b_1, b_2; | |
164 | uint8_t u8_1; | |
165 | uint16_t u16_1; | |
166 | uint32_t u32_1; | |
167 | uint64_t u64_1; | |
168 | int8_t i8_1, i8_2; | |
169 | int16_t i16_1, i16_2; | |
170 | int32_t i32_1, i32_2; | |
171 | int64_t i64_1, i64_2; | |
172 | } TestSimple; | |
173 | ||
174 | /* Object instantiation, we are going to use it in more than one test */ | |
175 | ||
176 | TestSimple obj_simple = { | |
177 | .b_1 = true, | |
178 | .b_2 = false, | |
179 | .u8_1 = 130, | |
180 | .u16_1 = 512, | |
181 | .u32_1 = 70000, | |
182 | .u64_1 = 12121212, | |
183 | .i8_1 = 65, | |
184 | .i8_2 = -65, | |
185 | .i16_1 = 512, | |
186 | .i16_2 = -512, | |
187 | .i32_1 = 70000, | |
188 | .i32_2 = -70000, | |
189 | .i64_1 = 12121212, | |
190 | .i64_2 = -12121212, | |
191 | }; | |
192 | ||
193 | /* Description of the values. If you add a primitive type | |
194 | you are expected to add a test here */ | |
195 | ||
196 | static const VMStateDescription vmstate_simple_primitive = { | |
197 | .name = "simple/primitive", | |
198 | .version_id = 1, | |
199 | .minimum_version_id = 1, | |
200 | .fields = (VMStateField[]) { | |
201 | VMSTATE_BOOL(b_1, TestSimple), | |
202 | VMSTATE_BOOL(b_2, TestSimple), | |
203 | VMSTATE_UINT8(u8_1, TestSimple), | |
204 | VMSTATE_UINT16(u16_1, TestSimple), | |
205 | VMSTATE_UINT32(u32_1, TestSimple), | |
206 | VMSTATE_UINT64(u64_1, TestSimple), | |
207 | VMSTATE_INT8(i8_1, TestSimple), | |
208 | VMSTATE_INT8(i8_2, TestSimple), | |
209 | VMSTATE_INT16(i16_1, TestSimple), | |
210 | VMSTATE_INT16(i16_2, TestSimple), | |
211 | VMSTATE_INT32(i32_1, TestSimple), | |
212 | VMSTATE_INT32(i32_2, TestSimple), | |
213 | VMSTATE_INT64(i64_1, TestSimple), | |
214 | VMSTATE_INT64(i64_2, TestSimple), | |
215 | VMSTATE_END_OF_LIST() | |
216 | } | |
217 | }; | |
218 | ||
219 | /* It describes what goes through the wire. Our tests are basically: | |
220 | ||
221 | * save test | |
222 | - save a struct a vmstate to a file | |
223 | - read that file back (binary read, no vmstate) | |
224 | - compare it with what we expect to be on the wire | |
225 | * load test | |
226 | - save to the file what we expect to be on the wire | |
227 | - read struct back with vmstate in a different | |
228 | - compare back with the original struct | |
229 | */ | |
230 | ||
231 | uint8_t wire_simple_primitive[] = { | |
232 | /* b_1 */ 0x01, | |
233 | /* b_2 */ 0x00, | |
234 | /* u8_1 */ 0x82, | |
235 | /* u16_1 */ 0x02, 0x00, | |
236 | /* u32_1 */ 0x00, 0x01, 0x11, 0x70, | |
237 | /* u64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c, | |
238 | /* i8_1 */ 0x41, | |
239 | /* i8_2 */ 0xbf, | |
240 | /* i16_1 */ 0x02, 0x00, | |
241 | /* i16_2 */ 0xfe, 0x0, | |
242 | /* i32_1 */ 0x00, 0x01, 0x11, 0x70, | |
243 | /* i32_2 */ 0xff, 0xfe, 0xee, 0x90, | |
244 | /* i64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c, | |
245 | /* i64_2 */ 0xff, 0xff, 0xff, 0xff, 0xff, 0x47, 0x0b, 0x84, | |
246 | QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */ | |
247 | }; | |
248 | ||
249 | static void obj_simple_copy(void *target, void *source) | |
250 | { | |
251 | memcpy(target, source, sizeof(TestSimple)); | |
252 | } | |
253 | ||
254 | static void test_simple_primitive(void) | |
255 | { | |
256 | TestSimple obj, obj_clone; | |
257 | ||
258 | memset(&obj, 0, sizeof(obj)); | |
259 | save_vmstate(&vmstate_simple_primitive, &obj_simple); | |
260 | ||
261 | compare_vmstate(wire_simple_primitive, sizeof(wire_simple_primitive)); | |
262 | ||
263 | SUCCESS(load_vmstate(&vmstate_simple_primitive, &obj, &obj_clone, | |
264 | obj_simple_copy, 1, wire_simple_primitive, | |
265 | sizeof(wire_simple_primitive))); | |
266 | ||
267 | #define FIELD_EQUAL(name) g_assert_cmpint(obj.name, ==, obj_simple.name) | |
268 | ||
269 | FIELD_EQUAL(b_1); | |
270 | FIELD_EQUAL(b_2); | |
271 | FIELD_EQUAL(u8_1); | |
272 | FIELD_EQUAL(u16_1); | |
273 | FIELD_EQUAL(u32_1); | |
274 | FIELD_EQUAL(u64_1); | |
275 | FIELD_EQUAL(i8_1); | |
276 | FIELD_EQUAL(i8_2); | |
277 | FIELD_EQUAL(i16_1); | |
278 | FIELD_EQUAL(i16_2); | |
279 | FIELD_EQUAL(i32_1); | |
280 | FIELD_EQUAL(i32_2); | |
281 | FIELD_EQUAL(i64_1); | |
282 | FIELD_EQUAL(i64_2); | |
2668b4bf | 283 | } |
4ea7df4e JQ |
284 | |
285 | typedef struct TestStruct { | |
286 | uint32_t a, b, c, e; | |
287 | uint64_t d, f; | |
288 | bool skip_c_e; | |
289 | } TestStruct; | |
2668b4bf EH |
290 | |
291 | static const VMStateDescription vmstate_versioned = { | |
4ea7df4e | 292 | .name = "test/versioned", |
2668b4bf EH |
293 | .version_id = 2, |
294 | .minimum_version_id = 1, | |
35d08458 | 295 | .fields = (VMStateField[]) { |
2668b4bf EH |
296 | VMSTATE_UINT32(a, TestStruct), |
297 | VMSTATE_UINT32_V(b, TestStruct, 2), /* Versioned field in the middle, so | |
298 | * we catch bugs more easily. | |
299 | */ | |
300 | VMSTATE_UINT32(c, TestStruct), | |
301 | VMSTATE_UINT64(d, TestStruct), | |
302 | VMSTATE_UINT32_V(e, TestStruct, 2), | |
303 | VMSTATE_UINT64_V(f, TestStruct, 2), | |
304 | VMSTATE_END_OF_LIST() | |
305 | } | |
306 | }; | |
307 | ||
308 | static void test_load_v1(void) | |
309 | { | |
2668b4bf EH |
310 | uint8_t buf[] = { |
311 | 0, 0, 0, 10, /* a */ | |
312 | 0, 0, 0, 30, /* c */ | |
313 | 0, 0, 0, 0, 0, 0, 0, 40, /* d */ | |
314 | QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */ | |
315 | }; | |
6d57b4c0 | 316 | save_buffer(buf, sizeof(buf)); |
2668b4bf | 317 | |
c6f6646c | 318 | QEMUFile *loading = open_test_file(false); |
2668b4bf EH |
319 | TestStruct obj = { .b = 200, .e = 500, .f = 600 }; |
320 | vmstate_load_state(loading, &vmstate_versioned, &obj, 1); | |
321 | g_assert(!qemu_file_get_error(loading)); | |
322 | g_assert_cmpint(obj.a, ==, 10); | |
323 | g_assert_cmpint(obj.b, ==, 200); | |
324 | g_assert_cmpint(obj.c, ==, 30); | |
325 | g_assert_cmpint(obj.d, ==, 40); | |
326 | g_assert_cmpint(obj.e, ==, 500); | |
327 | g_assert_cmpint(obj.f, ==, 600); | |
328 | qemu_fclose(loading); | |
329 | } | |
330 | ||
331 | static void test_load_v2(void) | |
332 | { | |
2668b4bf EH |
333 | uint8_t buf[] = { |
334 | 0, 0, 0, 10, /* a */ | |
335 | 0, 0, 0, 20, /* b */ | |
336 | 0, 0, 0, 30, /* c */ | |
337 | 0, 0, 0, 0, 0, 0, 0, 40, /* d */ | |
338 | 0, 0, 0, 50, /* e */ | |
339 | 0, 0, 0, 0, 0, 0, 0, 60, /* f */ | |
340 | QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */ | |
341 | }; | |
6d57b4c0 | 342 | save_buffer(buf, sizeof(buf)); |
2668b4bf | 343 | |
c6f6646c | 344 | QEMUFile *loading = open_test_file(false); |
2668b4bf EH |
345 | TestStruct obj; |
346 | vmstate_load_state(loading, &vmstate_versioned, &obj, 2); | |
347 | g_assert_cmpint(obj.a, ==, 10); | |
348 | g_assert_cmpint(obj.b, ==, 20); | |
349 | g_assert_cmpint(obj.c, ==, 30); | |
350 | g_assert_cmpint(obj.d, ==, 40); | |
351 | g_assert_cmpint(obj.e, ==, 50); | |
352 | g_assert_cmpint(obj.f, ==, 60); | |
353 | qemu_fclose(loading); | |
354 | } | |
355 | ||
356 | static bool test_skip(void *opaque, int version_id) | |
357 | { | |
358 | TestStruct *t = (TestStruct *)opaque; | |
359 | return !t->skip_c_e; | |
360 | } | |
361 | ||
362 | static const VMStateDescription vmstate_skipping = { | |
4ea7df4e | 363 | .name = "test/skip", |
2668b4bf EH |
364 | .version_id = 2, |
365 | .minimum_version_id = 1, | |
35d08458 | 366 | .fields = (VMStateField[]) { |
2668b4bf EH |
367 | VMSTATE_UINT32(a, TestStruct), |
368 | VMSTATE_UINT32(b, TestStruct), | |
369 | VMSTATE_UINT32_TEST(c, TestStruct, test_skip), | |
370 | VMSTATE_UINT64(d, TestStruct), | |
371 | VMSTATE_UINT32_TEST(e, TestStruct, test_skip), | |
372 | VMSTATE_UINT64_V(f, TestStruct, 2), | |
373 | VMSTATE_END_OF_LIST() | |
374 | } | |
375 | }; | |
376 | ||
377 | ||
378 | static void test_save_noskip(void) | |
379 | { | |
a8ec4437 | 380 | QEMUFile *fsave = open_test_file(true); |
2668b4bf EH |
381 | TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6, |
382 | .skip_c_e = false }; | |
8118f095 | 383 | vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL); |
2668b4bf | 384 | g_assert(!qemu_file_get_error(fsave)); |
2668b4bf | 385 | |
2668b4bf EH |
386 | uint8_t expected[] = { |
387 | 0, 0, 0, 1, /* a */ | |
388 | 0, 0, 0, 2, /* b */ | |
389 | 0, 0, 0, 3, /* c */ | |
390 | 0, 0, 0, 0, 0, 0, 0, 4, /* d */ | |
391 | 0, 0, 0, 5, /* e */ | |
392 | 0, 0, 0, 0, 0, 0, 0, 6, /* f */ | |
393 | }; | |
a8ec4437 | 394 | |
9935baca | 395 | qemu_fclose(fsave); |
a8ec4437 | 396 | compare_vmstate(expected, sizeof(expected)); |
2668b4bf EH |
397 | } |
398 | ||
399 | static void test_save_skip(void) | |
400 | { | |
a8ec4437 | 401 | QEMUFile *fsave = open_test_file(true); |
2668b4bf EH |
402 | TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6, |
403 | .skip_c_e = true }; | |
8118f095 | 404 | vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL); |
2668b4bf | 405 | g_assert(!qemu_file_get_error(fsave)); |
2668b4bf | 406 | |
2668b4bf EH |
407 | uint8_t expected[] = { |
408 | 0, 0, 0, 1, /* a */ | |
409 | 0, 0, 0, 2, /* b */ | |
410 | 0, 0, 0, 0, 0, 0, 0, 4, /* d */ | |
411 | 0, 0, 0, 0, 0, 0, 0, 6, /* f */ | |
412 | }; | |
2668b4bf | 413 | |
9935baca | 414 | qemu_fclose(fsave); |
a8ec4437 | 415 | compare_vmstate(expected, sizeof(expected)); |
2668b4bf EH |
416 | } |
417 | ||
418 | static void test_load_noskip(void) | |
419 | { | |
2668b4bf EH |
420 | uint8_t buf[] = { |
421 | 0, 0, 0, 10, /* a */ | |
422 | 0, 0, 0, 20, /* b */ | |
423 | 0, 0, 0, 30, /* c */ | |
424 | 0, 0, 0, 0, 0, 0, 0, 40, /* d */ | |
425 | 0, 0, 0, 50, /* e */ | |
426 | 0, 0, 0, 0, 0, 0, 0, 60, /* f */ | |
427 | QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */ | |
428 | }; | |
6d57b4c0 | 429 | save_buffer(buf, sizeof(buf)); |
2668b4bf | 430 | |
a8ec4437 | 431 | QEMUFile *loading = open_test_file(false); |
2668b4bf EH |
432 | TestStruct obj = { .skip_c_e = false }; |
433 | vmstate_load_state(loading, &vmstate_skipping, &obj, 2); | |
434 | g_assert(!qemu_file_get_error(loading)); | |
435 | g_assert_cmpint(obj.a, ==, 10); | |
436 | g_assert_cmpint(obj.b, ==, 20); | |
437 | g_assert_cmpint(obj.c, ==, 30); | |
438 | g_assert_cmpint(obj.d, ==, 40); | |
439 | g_assert_cmpint(obj.e, ==, 50); | |
440 | g_assert_cmpint(obj.f, ==, 60); | |
441 | qemu_fclose(loading); | |
442 | } | |
443 | ||
444 | static void test_load_skip(void) | |
445 | { | |
2668b4bf EH |
446 | uint8_t buf[] = { |
447 | 0, 0, 0, 10, /* a */ | |
448 | 0, 0, 0, 20, /* b */ | |
449 | 0, 0, 0, 0, 0, 0, 0, 40, /* d */ | |
450 | 0, 0, 0, 0, 0, 0, 0, 60, /* f */ | |
451 | QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */ | |
452 | }; | |
6d57b4c0 | 453 | save_buffer(buf, sizeof(buf)); |
2668b4bf | 454 | |
a8ec4437 | 455 | QEMUFile *loading = open_test_file(false); |
2668b4bf EH |
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); | |
466 | } | |
467 | ||
8cc49f03 HP |
468 | typedef struct { |
469 | int32_t i; | |
470 | } TestStructTriv; | |
471 | ||
472 | const VMStateDescription vmsd_tst = { | |
473 | .name = "test/tst", | |
474 | .version_id = 1, | |
475 | .minimum_version_id = 1, | |
476 | .fields = (VMStateField[]) { | |
477 | VMSTATE_INT32(i, TestStructTriv), | |
478 | VMSTATE_END_OF_LIST() | |
479 | } | |
480 | }; | |
481 | ||
cc958831 HP |
482 | /* test array migration */ |
483 | ||
8cc49f03 HP |
484 | #define AR_SIZE 4 |
485 | ||
486 | typedef struct { | |
487 | TestStructTriv *ar[AR_SIZE]; | |
488 | } TestArrayOfPtrToStuct; | |
489 | ||
490 | const VMStateDescription vmsd_arps = { | |
491 | .name = "test/arps", | |
492 | .version_id = 1, | |
493 | .minimum_version_id = 1, | |
494 | .fields = (VMStateField[]) { | |
495 | VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(ar, TestArrayOfPtrToStuct, | |
496 | AR_SIZE, 0, vmsd_tst, TestStructTriv), | |
497 | VMSTATE_END_OF_LIST() | |
498 | } | |
499 | }; | |
cc958831 HP |
500 | |
501 | static uint8_t wire_arr_ptr_no0[] = { | |
502 | 0x00, 0x00, 0x00, 0x00, | |
503 | 0x00, 0x00, 0x00, 0x01, | |
504 | 0x00, 0x00, 0x00, 0x02, | |
505 | 0x00, 0x00, 0x00, 0x03, | |
506 | QEMU_VM_EOF | |
507 | }; | |
508 | ||
8cc49f03 HP |
509 | static void test_arr_ptr_str_no0_save(void) |
510 | { | |
511 | TestStructTriv ar[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} }; | |
512 | TestArrayOfPtrToStuct sample = {.ar = {&ar[0], &ar[1], &ar[2], &ar[3]} }; | |
8cc49f03 HP |
513 | |
514 | save_vmstate(&vmsd_arps, &sample); | |
cc958831 | 515 | compare_vmstate(wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0)); |
8cc49f03 HP |
516 | } |
517 | ||
518 | static void test_arr_ptr_str_no0_load(void) | |
519 | { | |
520 | TestStructTriv ar_gt[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} }; | |
521 | TestStructTriv ar[AR_SIZE] = {}; | |
522 | TestArrayOfPtrToStuct obj = {.ar = {&ar[0], &ar[1], &ar[2], &ar[3]} }; | |
523 | int idx; | |
8cc49f03 | 524 | |
cc958831 HP |
525 | save_buffer(wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0)); |
526 | SUCCESS(load_vmstate_one(&vmsd_arps, &obj, 1, | |
527 | wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0))); | |
528 | for (idx = 0; idx < AR_SIZE; ++idx) { | |
529 | /* compare the target array ar with the ground truth array ar_gt */ | |
530 | g_assert_cmpint(ar_gt[idx].i, ==, ar[idx].i); | |
531 | } | |
532 | } | |
533 | ||
534 | static uint8_t wire_arr_ptr_0[] = { | |
535 | 0x00, 0x00, 0x00, 0x00, | |
536 | VMS_NULLPTR_MARKER, | |
537 | 0x00, 0x00, 0x00, 0x02, | |
538 | 0x00, 0x00, 0x00, 0x03, | |
539 | QEMU_VM_EOF | |
540 | }; | |
541 | ||
542 | static void test_arr_ptr_str_0_save(void) | |
543 | { | |
544 | TestStructTriv ar[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} }; | |
545 | TestArrayOfPtrToStuct sample = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} }; | |
546 | ||
547 | save_vmstate(&vmsd_arps, &sample); | |
548 | compare_vmstate(wire_arr_ptr_0, sizeof(wire_arr_ptr_0)); | |
549 | } | |
550 | ||
551 | static void test_arr_ptr_str_0_load(void) | |
552 | { | |
553 | TestStructTriv ar_gt[AR_SIZE] = {{.i = 0}, {.i = 0}, {.i = 2}, {.i = 3} }; | |
554 | TestStructTriv ar[AR_SIZE] = {}; | |
555 | TestArrayOfPtrToStuct obj = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} }; | |
556 | int idx; | |
557 | ||
558 | save_buffer(wire_arr_ptr_0, sizeof(wire_arr_ptr_0)); | |
8cc49f03 | 559 | SUCCESS(load_vmstate_one(&vmsd_arps, &obj, 1, |
cc958831 | 560 | wire_arr_ptr_0, sizeof(wire_arr_ptr_0))); |
8cc49f03 HP |
561 | for (idx = 0; idx < AR_SIZE; ++idx) { |
562 | /* compare the target array ar with the ground truth array ar_gt */ | |
563 | g_assert_cmpint(ar_gt[idx].i, ==, ar[idx].i); | |
564 | } | |
cc958831 HP |
565 | for (idx = 0; idx < AR_SIZE; ++idx) { |
566 | if (idx == 1) { | |
567 | g_assert_cmpint((uintptr_t)(obj.ar[idx]), ==, 0); | |
568 | } else { | |
569 | g_assert_cmpint((uintptr_t)(obj.ar[idx]), !=, 0); | |
570 | } | |
571 | } | |
8cc49f03 HP |
572 | } |
573 | ||
43333099 HP |
574 | typedef struct TestArrayOfPtrToInt { |
575 | int32_t *ar[AR_SIZE]; | |
576 | } TestArrayOfPtrToInt; | |
577 | ||
578 | const VMStateDescription vmsd_arpp = { | |
579 | .name = "test/arps", | |
580 | .version_id = 1, | |
581 | .minimum_version_id = 1, | |
582 | .fields = (VMStateField[]) { | |
583 | VMSTATE_ARRAY_OF_POINTER(ar, TestArrayOfPtrToInt, | |
584 | AR_SIZE, 0, vmstate_info_int32, int32_t*), | |
585 | VMSTATE_END_OF_LIST() | |
586 | } | |
587 | }; | |
588 | ||
589 | static void test_arr_ptr_prim_0_save(void) | |
590 | { | |
591 | int32_t ar[AR_SIZE] = {0 , 1, 2, 3}; | |
592 | TestArrayOfPtrToInt sample = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} }; | |
593 | ||
594 | save_vmstate(&vmsd_arpp, &sample); | |
595 | compare_vmstate(wire_arr_ptr_0, sizeof(wire_arr_ptr_0)); | |
596 | } | |
597 | ||
598 | static void test_arr_ptr_prim_0_load(void) | |
599 | { | |
600 | int32_t ar_gt[AR_SIZE] = {0, 1, 2, 3}; | |
601 | int32_t ar[AR_SIZE] = {3 , 42, 1, 0}; | |
602 | TestArrayOfPtrToInt obj = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} }; | |
603 | int idx; | |
604 | ||
605 | save_buffer(wire_arr_ptr_0, sizeof(wire_arr_ptr_0)); | |
606 | SUCCESS(load_vmstate_one(&vmsd_arpp, &obj, 1, | |
607 | wire_arr_ptr_0, sizeof(wire_arr_ptr_0))); | |
608 | for (idx = 0; idx < AR_SIZE; ++idx) { | |
609 | /* compare the target array ar with the ground truth array ar_gt */ | |
610 | if (idx == 1) { | |
611 | g_assert_cmpint(42, ==, ar[idx]); | |
612 | } else { | |
613 | g_assert_cmpint(ar_gt[idx], ==, ar[idx]); | |
614 | } | |
615 | } | |
616 | } | |
617 | ||
7e99f22c JD |
618 | /* test QTAILQ migration */ |
619 | typedef struct TestQtailqElement TestQtailqElement; | |
620 | ||
621 | struct TestQtailqElement { | |
622 | bool b; | |
623 | uint8_t u8; | |
624 | QTAILQ_ENTRY(TestQtailqElement) next; | |
625 | }; | |
626 | ||
627 | typedef struct TestQtailq { | |
628 | int16_t i16; | |
629 | QTAILQ_HEAD(TestQtailqHead, TestQtailqElement) q; | |
630 | int32_t i32; | |
631 | } TestQtailq; | |
632 | ||
633 | static const VMStateDescription vmstate_q_element = { | |
634 | .name = "test/queue-element", | |
635 | .version_id = 1, | |
636 | .minimum_version_id = 1, | |
637 | .fields = (VMStateField[]) { | |
638 | VMSTATE_BOOL(b, TestQtailqElement), | |
639 | VMSTATE_UINT8(u8, TestQtailqElement), | |
640 | VMSTATE_END_OF_LIST() | |
641 | }, | |
642 | }; | |
643 | ||
644 | static const VMStateDescription vmstate_q = { | |
645 | .name = "test/queue", | |
646 | .version_id = 1, | |
647 | .minimum_version_id = 1, | |
648 | .fields = (VMStateField[]) { | |
649 | VMSTATE_INT16(i16, TestQtailq), | |
650 | VMSTATE_QTAILQ_V(q, TestQtailq, 1, vmstate_q_element, TestQtailqElement, | |
651 | next), | |
652 | VMSTATE_INT32(i32, TestQtailq), | |
653 | VMSTATE_END_OF_LIST() | |
654 | } | |
655 | }; | |
656 | ||
657 | uint8_t wire_q[] = { | |
658 | /* i16 */ 0xfe, 0x0, | |
659 | /* start of element 0 of q */ 0x01, | |
660 | /* .b */ 0x01, | |
661 | /* .u8 */ 0x82, | |
662 | /* start of element 1 of q */ 0x01, | |
663 | /* b */ 0x00, | |
664 | /* u8 */ 0x41, | |
665 | /* end of q */ 0x00, | |
666 | /* i32 */ 0x00, 0x01, 0x11, 0x70, | |
667 | QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */ | |
668 | }; | |
669 | ||
670 | static void test_save_q(void) | |
671 | { | |
672 | TestQtailq obj_q = { | |
673 | .i16 = -512, | |
674 | .i32 = 70000, | |
675 | }; | |
676 | ||
677 | TestQtailqElement obj_qe1 = { | |
678 | .b = true, | |
679 | .u8 = 130, | |
680 | }; | |
681 | ||
682 | TestQtailqElement obj_qe2 = { | |
683 | .b = false, | |
684 | .u8 = 65, | |
685 | }; | |
686 | ||
687 | QTAILQ_INIT(&obj_q.q); | |
688 | QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe1, next); | |
689 | QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe2, next); | |
690 | ||
691 | save_vmstate(&vmstate_q, &obj_q); | |
692 | compare_vmstate(wire_q, sizeof(wire_q)); | |
693 | } | |
694 | ||
695 | static void test_load_q(void) | |
696 | { | |
697 | TestQtailq obj_q = { | |
698 | .i16 = -512, | |
699 | .i32 = 70000, | |
700 | }; | |
701 | ||
702 | TestQtailqElement obj_qe1 = { | |
703 | .b = true, | |
704 | .u8 = 130, | |
705 | }; | |
706 | ||
707 | TestQtailqElement obj_qe2 = { | |
708 | .b = false, | |
709 | .u8 = 65, | |
710 | }; | |
711 | ||
712 | QTAILQ_INIT(&obj_q.q); | |
713 | QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe1, next); | |
714 | QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe2, next); | |
715 | ||
716 | QEMUFile *fsave = open_test_file(true); | |
717 | ||
718 | qemu_put_buffer(fsave, wire_q, sizeof(wire_q)); | |
719 | g_assert(!qemu_file_get_error(fsave)); | |
720 | qemu_fclose(fsave); | |
721 | ||
722 | QEMUFile *fload = open_test_file(false); | |
723 | TestQtailq tgt; | |
724 | ||
725 | QTAILQ_INIT(&tgt.q); | |
726 | vmstate_load_state(fload, &vmstate_q, &tgt, 1); | |
727 | char eof = qemu_get_byte(fload); | |
728 | g_assert(!qemu_file_get_error(fload)); | |
729 | g_assert_cmpint(tgt.i16, ==, obj_q.i16); | |
730 | g_assert_cmpint(tgt.i32, ==, obj_q.i32); | |
731 | g_assert_cmpint(eof, ==, QEMU_VM_EOF); | |
732 | ||
733 | TestQtailqElement *qele_from = QTAILQ_FIRST(&obj_q.q); | |
734 | TestQtailqElement *qlast_from = QTAILQ_LAST(&obj_q.q, TestQtailqHead); | |
735 | TestQtailqElement *qele_to = QTAILQ_FIRST(&tgt.q); | |
736 | TestQtailqElement *qlast_to = QTAILQ_LAST(&tgt.q, TestQtailqHead); | |
737 | ||
738 | while (1) { | |
739 | g_assert_cmpint(qele_to->b, ==, qele_from->b); | |
740 | g_assert_cmpint(qele_to->u8, ==, qele_from->u8); | |
741 | if ((qele_from == qlast_from) || (qele_to == qlast_to)) { | |
742 | break; | |
743 | } | |
744 | qele_from = QTAILQ_NEXT(qele_from, next); | |
745 | qele_to = QTAILQ_NEXT(qele_to, next); | |
746 | } | |
747 | ||
748 | g_assert_cmpint((uintptr_t) qele_from, ==, (uintptr_t) qlast_from); | |
749 | g_assert_cmpint((uintptr_t) qele_to, ==, (uintptr_t) qlast_to); | |
750 | ||
751 | /* clean up */ | |
752 | TestQtailqElement *qele; | |
753 | while (!QTAILQ_EMPTY(&tgt.q)) { | |
754 | qele = QTAILQ_LAST(&tgt.q, TestQtailqHead); | |
755 | QTAILQ_REMOVE(&tgt.q, qele, next); | |
756 | free(qele); | |
757 | qele = NULL; | |
758 | } | |
759 | qemu_fclose(fload); | |
760 | } | |
761 | ||
5c379d90 DDAG |
762 | typedef struct TmpTestStruct { |
763 | TestStruct *parent; | |
764 | int64_t diff; | |
765 | } TmpTestStruct; | |
766 | ||
767 | static void tmp_child_pre_save(void *opaque) | |
768 | { | |
769 | struct TmpTestStruct *tts = opaque; | |
770 | ||
771 | tts->diff = tts->parent->b - tts->parent->a; | |
772 | } | |
773 | ||
774 | static int tmp_child_post_load(void *opaque, int version_id) | |
775 | { | |
776 | struct TmpTestStruct *tts = opaque; | |
777 | ||
778 | tts->parent->b = tts->parent->a + tts->diff; | |
779 | ||
780 | return 0; | |
781 | } | |
782 | ||
783 | static const VMStateDescription vmstate_tmp_back_to_parent = { | |
784 | .name = "test/tmp_child_parent", | |
785 | .fields = (VMStateField[]) { | |
786 | VMSTATE_UINT64(f, TestStruct), | |
787 | VMSTATE_END_OF_LIST() | |
788 | } | |
789 | }; | |
790 | ||
791 | static const VMStateDescription vmstate_tmp_child = { | |
792 | .name = "test/tmp_child", | |
793 | .pre_save = tmp_child_pre_save, | |
794 | .post_load = tmp_child_post_load, | |
795 | .fields = (VMStateField[]) { | |
796 | VMSTATE_INT64(diff, TmpTestStruct), | |
797 | VMSTATE_STRUCT_POINTER(parent, TmpTestStruct, | |
798 | vmstate_tmp_back_to_parent, TestStruct), | |
799 | VMSTATE_END_OF_LIST() | |
800 | } | |
801 | }; | |
802 | ||
803 | static const VMStateDescription vmstate_with_tmp = { | |
804 | .name = "test/with_tmp", | |
805 | .version_id = 1, | |
806 | .fields = (VMStateField[]) { | |
807 | VMSTATE_UINT32(a, TestStruct), | |
808 | VMSTATE_UINT64(d, TestStruct), | |
809 | VMSTATE_WITH_TMP(TestStruct, TmpTestStruct, vmstate_tmp_child), | |
810 | VMSTATE_END_OF_LIST() | |
811 | } | |
812 | }; | |
813 | ||
814 | static void obj_tmp_copy(void *target, void *source) | |
815 | { | |
816 | memcpy(target, source, sizeof(TestStruct)); | |
817 | } | |
818 | ||
819 | static void test_tmp_struct(void) | |
820 | { | |
821 | TestStruct obj, obj_clone; | |
822 | ||
823 | uint8_t const wire_with_tmp[] = { | |
824 | /* u32 a */ 0x00, 0x00, 0x00, 0x02, | |
825 | /* u64 d */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, | |
826 | /* diff */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, | |
827 | /* u64 f */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, | |
828 | QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */ | |
829 | }; | |
830 | ||
831 | memset(&obj, 0, sizeof(obj)); | |
832 | obj.a = 2; | |
833 | obj.b = 4; | |
834 | obj.d = 1; | |
835 | obj.f = 8; | |
836 | save_vmstate(&vmstate_with_tmp, &obj); | |
837 | ||
838 | compare_vmstate(wire_with_tmp, sizeof(wire_with_tmp)); | |
839 | ||
840 | memset(&obj, 0, sizeof(obj)); | |
841 | SUCCESS(load_vmstate(&vmstate_with_tmp, &obj, &obj_clone, | |
842 | obj_tmp_copy, 1, wire_with_tmp, | |
843 | sizeof(wire_with_tmp))); | |
844 | g_assert_cmpint(obj.a, ==, 2); /* From top level vmsd */ | |
845 | g_assert_cmpint(obj.b, ==, 4); /* from the post_load */ | |
846 | g_assert_cmpint(obj.d, ==, 1); /* From top level vmsd */ | |
847 | g_assert_cmpint(obj.f, ==, 8); /* From the child->parent */ | |
848 | } | |
849 | ||
2668b4bf EH |
850 | int main(int argc, char **argv) |
851 | { | |
852 | temp_fd = mkstemp(temp_file); | |
853 | ||
8925839f DB |
854 | module_call_init(MODULE_INIT_QOM); |
855 | ||
2668b4bf | 856 | g_test_init(&argc, &argv, NULL); |
4ea7df4e | 857 | g_test_add_func("/vmstate/simple/primitive", test_simple_primitive); |
2668b4bf EH |
858 | g_test_add_func("/vmstate/versioned/load/v1", test_load_v1); |
859 | g_test_add_func("/vmstate/versioned/load/v2", test_load_v2); | |
860 | g_test_add_func("/vmstate/field_exists/load/noskip", test_load_noskip); | |
861 | g_test_add_func("/vmstate/field_exists/load/skip", test_load_skip); | |
862 | g_test_add_func("/vmstate/field_exists/save/noskip", test_save_noskip); | |
863 | g_test_add_func("/vmstate/field_exists/save/skip", test_save_skip); | |
8cc49f03 HP |
864 | g_test_add_func("/vmstate/array/ptr/str/no0/save", |
865 | test_arr_ptr_str_no0_save); | |
866 | g_test_add_func("/vmstate/array/ptr/str/no0/load", | |
867 | test_arr_ptr_str_no0_load); | |
cc958831 HP |
868 | g_test_add_func("/vmstate/array/ptr/str/0/save", test_arr_ptr_str_0_save); |
869 | g_test_add_func("/vmstate/array/ptr/str/0/load", | |
870 | test_arr_ptr_str_0_load); | |
43333099 HP |
871 | g_test_add_func("/vmstate/array/ptr/prim/0/save", |
872 | test_arr_ptr_prim_0_save); | |
873 | g_test_add_func("/vmstate/array/ptr/prim/0/load", | |
874 | test_arr_ptr_prim_0_load); | |
7e99f22c JD |
875 | g_test_add_func("/vmstate/qtailq/save/saveq", test_save_q); |
876 | g_test_add_func("/vmstate/qtailq/load/loadq", test_load_q); | |
5c379d90 | 877 | g_test_add_func("/vmstate/tmp_struct", test_tmp_struct); |
2668b4bf EH |
878 | g_test_run(); |
879 | ||
880 | close(temp_fd); | |
881 | unlink(temp_file); | |
882 | ||
883 | return 0; | |
884 | } |