Patchwork [v2,2/5] Add XBZRLE testing

login
register
mail settings
Submitter Orit Wasserman
Date Jan. 30, 2013, 11:41 a.m.
Message ID <1359546093-1472-3-git-send-email-owasserm@redhat.com>
Download mbox | patch
Permalink /patch/216868/
State New
Headers show

Comments

Orit Wasserman - Jan. 30, 2013, 11:41 a.m.
Signed-off-by: Orit Wasserman <owasserm@redhat.com>
---
 tests/Makefile      |   3 +
 tests/test-xbzrle.c | 186 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 189 insertions(+)
 create mode 100644 tests/test-xbzrle.c
Eric Blake - Jan. 30, 2013, 6:22 p.m.
On 01/30/2013 04:41 AM, Orit Wasserman wrote:
> Signed-off-by: Orit Wasserman <owasserm@redhat.com>
> ---
>  tests/Makefile      |   3 +
>  tests/test-xbzrle.c | 186 ++++++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 189 insertions(+)
>  create mode 100644 tests/test-xbzrle.c

> +++ b/tests/test-xbzrle.c
> @@ -0,0 +1,186 @@
> +#include <stdint.h>

No copyright notice?  Please state what license this file is under.

> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <strings.h>
> +#include <string.h>
> +#include <sys/time.h>
> +#include <assert.h>
> +#include "qemu-common.h"
> +#include "include/migration/migration.h"
> +
> +#define PAGE_SIZE 4096
> +
> +static void test_uleb(void)
> +{
> +    uint32_t i, val;
> +    uint8_t *buf = g_malloc(sizeof(uint8_t)*2);

Spaces around '*' for multiplication.  Also, why bother mallocing a 2
byte array?  Just make it stack-local:

uint8_t buf[2];

> +    int encode_ret, decode_ret;
> +
> +    for (i = 0; i <= 0x3fff; i++) {
> +        encode_ret = uleb128_encode_small(buf, i);
> +        decode_ret = uleb128_decode_small(buf, &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, &val);
> +    g_assert(decode_ret == -1);

Is it worth testing what happens to the contents of val on error?

> +
> +    g_free(buf);

If you make buf stack-local, you don't need to free it here.

> +static void test_encode_decode_overflow(void)
> +{
> +    uint8_t *compressed = g_malloc0(PAGE_SIZE);
> +    uint8_t *test = g_malloc0(PAGE_SIZE);
> +    uint8_t *buffer = g_malloc0(PAGE_SIZE);
> +    int i = 0, rc = 0;
> +
> +    for (i = 0; i < PAGE_SIZE/2 - 1; i++) {
> +        test[i*2] = 1;

Spaces around binary operators.

Patch

diff --git a/tests/Makefile b/tests/Makefile
index c681ceb..abe9c2a 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -50,6 +50,8 @@  check-unit-y += tests/test-hbitmap$(EXESUF)
 check-unit-y += tests/test-x86-cpuid$(EXESUF)
 # all code tested by test-x86-cpuid is inside topology.h
 gcov-files-test-x86-cpuid-y =
+check-unit-y += tests/test-xbzrle$(EXESUF)
+gcov-files-test-xbzrle-y = xbzrle.c
 
 check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
 
@@ -98,6 +100,7 @@  tests/test-thread-pool$(EXESUF): tests/test-thread-pool.o $(block-obj-y) libqemu
 tests/test-iov$(EXESUF): tests/test-iov.o libqemuutil.a
 tests/test-hbitmap$(EXESUF): tests/test-hbitmap.o libqemuutil.a libqemustub.a
 tests/test-x86-cpuid$(EXESUF): tests/test-x86-cpuid.o
+tests/test-xbzrle$(EXESUF): tests/test-xbzrle.o xbzrle.o page_cache.o libqemuutil.a
 
 tests/test-qapi-types.c tests/test-qapi-types.h :\
 $(SRC_PATH)/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-types.py
diff --git a/tests/test-xbzrle.c b/tests/test-xbzrle.c
new file mode 100644
index 0000000..a8f1ae6
--- /dev/null
+++ b/tests/test-xbzrle.c
@@ -0,0 +1,186 @@ 
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <string.h>
+#include <sys/time.h>
+#include <assert.h>
+#include "qemu-common.h"
+#include "include/migration/migration.h"
+
+#define PAGE_SIZE 4096
+
+static void test_uleb(void)
+{
+    uint32_t i, val;
+    uint8_t *buf = g_malloc(sizeof(uint8_t)*2);
+    int encode_ret, decode_ret;
+
+    for (i = 0; i <= 0x3fff; i++) {
+        encode_ret = uleb128_encode_small(buf, i);
+        decode_ret = uleb128_decode_small(buf, &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, &val);
+    g_assert(decode_ret == -1);
+
+    g_free(buf);
+}
+
+static void test_encode_decode_zero(void)
+{
+    uint8_t *buffer = g_malloc0(PAGE_SIZE);
+    uint8_t *compressed = g_malloc0(PAGE_SIZE);
+    int i = 0;
+    int dlen = 0;
+    int diff_len = g_test_rand_int_range(0, PAGE_SIZE - 1006);
+
+    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 */
+    dlen = xbzrle_encode_buffer(buffer, buffer, PAGE_SIZE, compressed,
+                       PAGE_SIZE);
+    g_assert(dlen == 0);
+
+    g_free(buffer);
+    g_free(compressed);
+}
+
+static void test_encode_decode_unchanged(void)
+{
+    uint8_t *compressed = g_malloc0(PAGE_SIZE);
+    uint8_t *test = g_malloc0(PAGE_SIZE);
+    int i = 0;
+    int dlen = 0;
+    int diff_len = g_test_rand_int_range(0, PAGE_SIZE - 1006);
+
+    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 */
+    dlen = xbzrle_encode_buffer(test, test, PAGE_SIZE, compressed,
+                                PAGE_SIZE);
+    g_assert(dlen == 0);
+
+    g_free(test);
+    g_free(compressed);
+}
+
+static void test_encode_decode_1_byte(void)
+{
+    uint8_t *buffer = g_malloc0(PAGE_SIZE);
+    uint8_t *test = g_malloc0(PAGE_SIZE);
+    uint8_t *compressed = g_malloc(PAGE_SIZE);
+    int dlen = 0, rc = 0;
+    uint8_t *buf = g_malloc(sizeof(uint8_t)*2);
+
+    test[PAGE_SIZE - 1] = 1;
+
+    dlen = xbzrle_encode_buffer(buffer, test, PAGE_SIZE, compressed,
+                       PAGE_SIZE);
+    g_assert(dlen == (uleb128_encode_small(buf, 4095) + 2));
+
+    rc = xbzrle_decode_buffer(compressed, dlen, buffer, PAGE_SIZE);
+    g_assert(rc == PAGE_SIZE);
+    g_assert(memcmp(test, buffer, PAGE_SIZE) == 0);
+
+    g_free(buffer);
+    g_free(compressed);
+    g_free(test);
+    g_free(buf);
+}
+
+static void test_encode_decode_overflow(void)
+{
+    uint8_t *compressed = g_malloc0(PAGE_SIZE);
+    uint8_t *test = g_malloc0(PAGE_SIZE);
+    uint8_t *buffer = g_malloc0(PAGE_SIZE);
+    int i = 0, rc = 0;
+
+    for (i = 0; i < PAGE_SIZE/2 - 1; i++) {
+        test[i*2] = 1;
+    }
+
+    /* encode overflow */
+    rc = xbzrle_encode_buffer(buffer, test, PAGE_SIZE, compressed,
+                              PAGE_SIZE);
+    g_assert(rc == -1);
+
+    g_free(buffer);
+    g_free(compressed);
+    g_free(test);
+}
+
+static void encode_decode_range(void)
+{
+    uint8_t *buffer = g_malloc0(PAGE_SIZE);
+    uint8_t *compressed = g_malloc(PAGE_SIZE);
+    uint8_t *test = g_malloc0(PAGE_SIZE);
+    int i = 0, rc = 0;
+    int dlen = 0;
+
+    int diff_len = g_test_rand_int_range(0, PAGE_SIZE - 1006);
+
+    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 */
+    dlen = xbzrle_encode_buffer(test, buffer, PAGE_SIZE, compressed,
+                                PAGE_SIZE);
+
+    rc = xbzrle_decode_buffer(compressed, dlen, test, PAGE_SIZE);
+    g_assert(rc < PAGE_SIZE);
+    g_assert(memcmp(test, buffer, PAGE_SIZE) == 0);
+
+    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();
+}