blob: db93b0a3d2a20192c1f675ff6b9dc64329d16bac [file] [log] [blame]
Orit Wasserman21e3cd22013-01-31 09:12:16 +02001/*
2 * Xor Based Zero Run Length Encoding unit tests.
3 *
4 * Copyright 2013 Red Hat, Inc. and/or its affiliates
5 *
6 * Authors:
7 * Orit Wasserman <owasserm@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
11 *
12 */
13#include <stdint.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <strings.h>
17#include <string.h>
18#include <sys/time.h>
19#include <assert.h>
20#include "qemu-common.h"
21#include "include/migration/migration.h"
22
23#define PAGE_SIZE 4096
24
25static void test_uleb(void)
26{
27 uint32_t i, val;
28 uint8_t buf[2];
29 int encode_ret, decode_ret;
30
31 for (i = 0; i <= 0x3fff; i++) {
32 encode_ret = uleb128_encode_small(&buf[0], i);
33 decode_ret = uleb128_decode_small(&buf[0], &val);
34 g_assert(encode_ret == decode_ret);
35 g_assert(i == val);
36 }
37
38 /* decode invalid value */
39 buf[0] = 0x80;
40 buf[1] = 0x80;
41
42 decode_ret = uleb128_decode_small(&buf[0], &val);
43 g_assert(decode_ret == -1);
44 g_assert(val == 0);
45}
46
47static void test_encode_decode_zero(void)
48{
49 uint8_t *buffer = g_malloc0(PAGE_SIZE);
50 uint8_t *compressed = g_malloc0(PAGE_SIZE);
51 int i = 0;
52 int dlen = 0;
53 int diff_len = g_test_rand_int_range(0, PAGE_SIZE - 1006);
54
55 for (i = diff_len; i > 0; i--) {
56 buffer[1000 + i] = i;
57 }
58
59 buffer[1000 + diff_len + 3] = 103;
60 buffer[1000 + diff_len + 5] = 105;
61
62 /* encode zero page */
63 dlen = xbzrle_encode_buffer(buffer, buffer, PAGE_SIZE, compressed,
64 PAGE_SIZE);
65 g_assert(dlen == 0);
66
67 g_free(buffer);
68 g_free(compressed);
69}
70
71static void test_encode_decode_unchanged(void)
72{
73 uint8_t *compressed = g_malloc0(PAGE_SIZE);
74 uint8_t *test = g_malloc0(PAGE_SIZE);
75 int i = 0;
76 int dlen = 0;
77 int diff_len = g_test_rand_int_range(0, PAGE_SIZE - 1006);
78
79 for (i = diff_len; i > 0; i--) {
80 test[1000 + i] = i + 4;
81 }
82
83 test[1000 + diff_len + 3] = 107;
84 test[1000 + diff_len + 5] = 109;
85
86 /* test unchanged buffer */
87 dlen = xbzrle_encode_buffer(test, test, PAGE_SIZE, compressed,
88 PAGE_SIZE);
89 g_assert(dlen == 0);
90
91 g_free(test);
92 g_free(compressed);
93}
94
95static void test_encode_decode_1_byte(void)
96{
97 uint8_t *buffer = g_malloc0(PAGE_SIZE);
98 uint8_t *test = g_malloc0(PAGE_SIZE);
99 uint8_t *compressed = g_malloc(PAGE_SIZE);
100 int dlen = 0, rc = 0;
101 uint8_t buf[2];
102
103 test[PAGE_SIZE - 1] = 1;
104
105 dlen = xbzrle_encode_buffer(buffer, test, PAGE_SIZE, compressed,
106 PAGE_SIZE);
107 g_assert(dlen == (uleb128_encode_small(&buf[0], 4095) + 2));
108
109 rc = xbzrle_decode_buffer(compressed, dlen, buffer, PAGE_SIZE);
110 g_assert(rc == PAGE_SIZE);
111 g_assert(memcmp(test, buffer, PAGE_SIZE) == 0);
112
113 g_free(buffer);
114 g_free(compressed);
115 g_free(test);
116}
117
118static void test_encode_decode_overflow(void)
119{
120 uint8_t *compressed = g_malloc0(PAGE_SIZE);
121 uint8_t *test = g_malloc0(PAGE_SIZE);
122 uint8_t *buffer = g_malloc0(PAGE_SIZE);
123 int i = 0, rc = 0;
124
125 for (i = 0; i < PAGE_SIZE / 2 - 1; i++) {
126 test[i * 2] = 1;
127 }
128
129 /* encode overflow */
130 rc = xbzrle_encode_buffer(buffer, test, PAGE_SIZE, compressed,
131 PAGE_SIZE);
132 g_assert(rc == -1);
133
134 g_free(buffer);
135 g_free(compressed);
136 g_free(test);
137}
138
139static void encode_decode_range(void)
140{
141 uint8_t *buffer = g_malloc0(PAGE_SIZE);
142 uint8_t *compressed = g_malloc(PAGE_SIZE);
143 uint8_t *test = g_malloc0(PAGE_SIZE);
144 int i = 0, rc = 0;
145 int dlen = 0;
146
147 int diff_len = g_test_rand_int_range(0, PAGE_SIZE - 1006);
148
149 for (i = diff_len; i > 0; i--) {
150 buffer[1000 + i] = i;
151 test[1000 + i] = i + 4;
152 }
153
154 buffer[1000 + diff_len + 3] = 103;
155 test[1000 + diff_len + 3] = 107;
156
157 buffer[1000 + diff_len + 5] = 105;
158 test[1000 + diff_len + 5] = 109;
159
160 /* test encode/decode */
161 dlen = xbzrle_encode_buffer(test, buffer, PAGE_SIZE, compressed,
162 PAGE_SIZE);
163
164 rc = xbzrle_decode_buffer(compressed, dlen, test, PAGE_SIZE);
165 g_assert(rc < PAGE_SIZE);
166 g_assert(memcmp(test, buffer, PAGE_SIZE) == 0);
167
168 g_free(buffer);
169 g_free(compressed);
170 g_free(test);
171}
172
173static void test_encode_decode(void)
174{
175 int i;
176
177 for (i = 0; i < 10000; i++) {
178 encode_decode_range();
179 }
180}
181
182int main(int argc, char **argv)
183{
184 g_test_init(&argc, &argv, NULL);
185 g_test_rand_int();
186 g_test_add_func("/xbzrle/uleb", test_uleb);
187 g_test_add_func("/xbzrle/encode_decode_zero", test_encode_decode_zero);
188 g_test_add_func("/xbzrle/encode_decode_unchanged",
189 test_encode_decode_unchanged);
190 g_test_add_func("/xbzrle/encode_decode_1_byte", test_encode_decode_1_byte);
191 g_test_add_func("/xbzrle/encode_decode_overflow",
192 test_encode_decode_overflow);
193 g_test_add_func("/xbzrle/encode_decode", test_encode_decode);
194
195 return g_test_run();
196}