2013-01-31 11:12:16 +04:00
|
|
|
/*
|
|
|
|
* Xor Based Zero Run Length Encoding unit tests.
|
|
|
|
*
|
|
|
|
* Copyright 2013 Red Hat, Inc. and/or its affiliates
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Orit Wasserman <owasserm@redhat.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
2016-02-08 21:08:51 +03:00
|
|
|
#include "qemu/osdep.h"
|
2016-03-20 20:16:19 +03:00
|
|
|
#include "qemu/cutils.h"
|
2017-04-05 22:47:50 +03:00
|
|
|
#include "../migration/xbzrle.h"
|
2013-01-31 11:12:16 +04:00
|
|
|
|
2021-01-18 09:38:05 +03:00
|
|
|
#define XBZRLE_PAGE_SIZE 4096
|
2013-01-31 11:12:16 +04:00
|
|
|
|
|
|
|
static void test_uleb(void)
|
|
|
|
{
|
|
|
|
uint32_t i, val;
|
|
|
|
uint8_t buf[2];
|
|
|
|
int encode_ret, decode_ret;
|
|
|
|
|
|
|
|
for (i = 0; i <= 0x3fff; i++) {
|
|
|
|
encode_ret = uleb128_encode_small(&buf[0], i);
|
|
|
|
decode_ret = uleb128_decode_small(&buf[0], &val);
|
|
|
|
g_assert(encode_ret == decode_ret);
|
|
|
|
g_assert(i == val);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* decode invalid value */
|
|
|
|
buf[0] = 0x80;
|
|
|
|
buf[1] = 0x80;
|
|
|
|
|
|
|
|
decode_ret = uleb128_decode_small(&buf[0], &val);
|
|
|
|
g_assert(decode_ret == -1);
|
|
|
|
g_assert(val == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_encode_decode_zero(void)
|
|
|
|
{
|
2021-01-18 09:38:05 +03:00
|
|
|
uint8_t *buffer = g_malloc0(XBZRLE_PAGE_SIZE);
|
|
|
|
uint8_t *compressed = g_malloc0(XBZRLE_PAGE_SIZE);
|
2013-01-31 11:12:16 +04:00
|
|
|
int i = 0;
|
|
|
|
int dlen = 0;
|
2021-01-18 09:38:05 +03:00
|
|
|
int diff_len = g_test_rand_int_range(0, XBZRLE_PAGE_SIZE - 1006);
|
2013-01-31 11:12:16 +04:00
|
|
|
|
|
|
|
for (i = diff_len; i > 0; i--) {
|
|
|
|
buffer[1000 + i] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[1000 + diff_len + 3] = 103;
|
|
|
|
buffer[1000 + diff_len + 5] = 105;
|
|
|
|
|
|
|
|
/* encode zero page */
|
2023-05-18 06:00:30 +03:00
|
|
|
dlen = xbzrle_encode_buffer(buffer, buffer, XBZRLE_PAGE_SIZE,
|
|
|
|
compressed, XBZRLE_PAGE_SIZE);
|
2013-01-31 11:12:16 +04:00
|
|
|
g_assert(dlen == 0);
|
|
|
|
|
|
|
|
g_free(buffer);
|
|
|
|
g_free(compressed);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_encode_decode_unchanged(void)
|
|
|
|
{
|
2021-01-18 09:38:05 +03:00
|
|
|
uint8_t *compressed = g_malloc0(XBZRLE_PAGE_SIZE);
|
|
|
|
uint8_t *test = g_malloc0(XBZRLE_PAGE_SIZE);
|
2013-01-31 11:12:16 +04:00
|
|
|
int i = 0;
|
|
|
|
int dlen = 0;
|
2021-01-18 09:38:05 +03:00
|
|
|
int diff_len = g_test_rand_int_range(0, XBZRLE_PAGE_SIZE - 1006);
|
2013-01-31 11:12:16 +04:00
|
|
|
|
|
|
|
for (i = diff_len; i > 0; i--) {
|
|
|
|
test[1000 + i] = i + 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
test[1000 + diff_len + 3] = 107;
|
|
|
|
test[1000 + diff_len + 5] = 109;
|
|
|
|
|
|
|
|
/* test unchanged buffer */
|
2023-05-18 06:00:30 +03:00
|
|
|
dlen = xbzrle_encode_buffer(test, test, XBZRLE_PAGE_SIZE,
|
|
|
|
compressed, XBZRLE_PAGE_SIZE);
|
2013-01-31 11:12:16 +04:00
|
|
|
g_assert(dlen == 0);
|
|
|
|
|
|
|
|
g_free(test);
|
|
|
|
g_free(compressed);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_encode_decode_1_byte(void)
|
|
|
|
{
|
2021-01-18 09:38:05 +03:00
|
|
|
uint8_t *buffer = g_malloc0(XBZRLE_PAGE_SIZE);
|
|
|
|
uint8_t *test = g_malloc0(XBZRLE_PAGE_SIZE);
|
|
|
|
uint8_t *compressed = g_malloc(XBZRLE_PAGE_SIZE);
|
2013-01-31 11:12:16 +04:00
|
|
|
int dlen = 0, rc = 0;
|
|
|
|
uint8_t buf[2];
|
|
|
|
|
2021-01-18 09:38:05 +03:00
|
|
|
test[XBZRLE_PAGE_SIZE - 1] = 1;
|
2013-01-31 11:12:16 +04:00
|
|
|
|
2023-05-18 06:00:30 +03:00
|
|
|
dlen = xbzrle_encode_buffer(buffer, test, XBZRLE_PAGE_SIZE,
|
|
|
|
compressed, XBZRLE_PAGE_SIZE);
|
2013-01-31 11:12:16 +04:00
|
|
|
g_assert(dlen == (uleb128_encode_small(&buf[0], 4095) + 2));
|
|
|
|
|
2021-01-18 09:38:05 +03:00
|
|
|
rc = xbzrle_decode_buffer(compressed, dlen, buffer, XBZRLE_PAGE_SIZE);
|
|
|
|
g_assert(rc == XBZRLE_PAGE_SIZE);
|
|
|
|
g_assert(memcmp(test, buffer, XBZRLE_PAGE_SIZE) == 0);
|
2013-01-31 11:12:16 +04:00
|
|
|
|
|
|
|
g_free(buffer);
|
|
|
|
g_free(compressed);
|
|
|
|
g_free(test);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_encode_decode_overflow(void)
|
|
|
|
{
|
2021-01-18 09:38:05 +03:00
|
|
|
uint8_t *compressed = g_malloc0(XBZRLE_PAGE_SIZE);
|
|
|
|
uint8_t *test = g_malloc0(XBZRLE_PAGE_SIZE);
|
|
|
|
uint8_t *buffer = g_malloc0(XBZRLE_PAGE_SIZE);
|
2013-01-31 11:12:16 +04:00
|
|
|
int i = 0, rc = 0;
|
|
|
|
|
2021-01-18 09:38:05 +03:00
|
|
|
for (i = 0; i < XBZRLE_PAGE_SIZE / 2 - 1; i++) {
|
2013-01-31 11:12:16 +04:00
|
|
|
test[i * 2] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* encode overflow */
|
2023-05-18 06:00:30 +03:00
|
|
|
rc = xbzrle_encode_buffer(buffer, test, XBZRLE_PAGE_SIZE,
|
|
|
|
compressed, XBZRLE_PAGE_SIZE);
|
2013-01-31 11:12:16 +04:00
|
|
|
g_assert(rc == -1);
|
|
|
|
|
|
|
|
g_free(buffer);
|
|
|
|
g_free(compressed);
|
|
|
|
g_free(test);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void encode_decode_range(void)
|
|
|
|
{
|
2021-01-18 09:38:05 +03:00
|
|
|
uint8_t *buffer = g_malloc0(XBZRLE_PAGE_SIZE);
|
|
|
|
uint8_t *compressed = g_malloc(XBZRLE_PAGE_SIZE);
|
|
|
|
uint8_t *test = g_malloc0(XBZRLE_PAGE_SIZE);
|
2013-01-31 11:12:16 +04:00
|
|
|
int i = 0, rc = 0;
|
|
|
|
int dlen = 0;
|
|
|
|
|
2021-01-18 09:38:05 +03:00
|
|
|
int diff_len = g_test_rand_int_range(0, XBZRLE_PAGE_SIZE - 1006);
|
2013-01-31 11:12:16 +04:00
|
|
|
|
|
|
|
for (i = diff_len; i > 0; i--) {
|
|
|
|
buffer[1000 + i] = i;
|
|
|
|
test[1000 + i] = i + 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[1000 + diff_len + 3] = 103;
|
|
|
|
test[1000 + diff_len + 3] = 107;
|
|
|
|
|
|
|
|
buffer[1000 + diff_len + 5] = 105;
|
|
|
|
test[1000 + diff_len + 5] = 109;
|
|
|
|
|
|
|
|
/* test encode/decode */
|
2023-05-18 06:00:30 +03:00
|
|
|
dlen = xbzrle_encode_buffer(test, buffer, XBZRLE_PAGE_SIZE,
|
|
|
|
compressed, XBZRLE_PAGE_SIZE);
|
2013-01-31 11:12:16 +04:00
|
|
|
|
2021-01-18 09:38:05 +03:00
|
|
|
rc = xbzrle_decode_buffer(compressed, dlen, test, XBZRLE_PAGE_SIZE);
|
|
|
|
g_assert(rc < XBZRLE_PAGE_SIZE);
|
|
|
|
g_assert(memcmp(test, buffer, XBZRLE_PAGE_SIZE) == 0);
|
2013-01-31 11:12:16 +04:00
|
|
|
|
|
|
|
g_free(buffer);
|
|
|
|
g_free(compressed);
|
|
|
|
g_free(test);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_encode_decode(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 10000; i++) {
|
|
|
|
encode_decode_range();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
g_test_init(&argc, &argv, NULL);
|
|
|
|
g_test_rand_int();
|
|
|
|
g_test_add_func("/xbzrle/uleb", test_uleb);
|
|
|
|
g_test_add_func("/xbzrle/encode_decode_zero", test_encode_decode_zero);
|
|
|
|
g_test_add_func("/xbzrle/encode_decode_unchanged",
|
|
|
|
test_encode_decode_unchanged);
|
|
|
|
g_test_add_func("/xbzrle/encode_decode_1_byte", test_encode_decode_1_byte);
|
|
|
|
g_test_add_func("/xbzrle/encode_decode_overflow",
|
|
|
|
test_encode_decode_overflow);
|
|
|
|
g_test_add_func("/xbzrle/encode_decode", test_encode_decode);
|
|
|
|
|
|
|
|
return g_test_run();
|
|
|
|
}
|