[Spice-devel] [PATCH spice-common v2 3/3] test-marshallers: Test demarshalling

Frediano Ziglio fziglio at redhat.com
Fri Sep 22 10:10:04 UTC 2017


Generate demarshallers code and check it too.

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 tests/Makefile.am        |  4 ++++
 tests/test-marshallers.c | 29 ++++++++++++++++++++++++++---
 tests/test-marshallers.h |  2 +-
 3 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 10033c0..5abf239 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -20,6 +20,7 @@ test_logging_LDADD =					\
 test_marshallers_SOURCES =		\
 	generated_test_marshallers.c	\
 	generated_test_marshallers.h	\
+	generated_test_demarshallers.c	\
 	test-marshallers.c		\
 	test-marshallers.h		\
 	$(NULL)
@@ -37,6 +38,7 @@ test_marshallers_LDADD =				\
 TEST_MARSHALLERS =				\
 	generated_test_marshallers.c		\
 	generated_test_marshallers.h		\
+	generated_test_demarshallers.c		\
 	$(NULL)
 
 BUILT_SOURCES = $(TEST_MARSHALLERS)
@@ -57,6 +59,8 @@ generated_test_marshallers.c: $(srcdir)/test-marshallers.proto $(MARSHALLERS_DEP
 	$(AM_V_GEN)$(PYTHON) $(top_srcdir)/spice_codegen.py --generate-marshallers --server --include test-marshallers.h $< $@ >/dev/null
 generated_test_marshallers.h: $(srcdir)/test-marshallers.proto $(MARSHALLERS_DEPS)
 	$(AM_V_GEN)$(PYTHON) $(top_srcdir)/spice_codegen.py --generate-marshallers --server --include test-marshallers.h -H $< $@ >/dev/null
+generated_test_demarshallers.c: $(srcdir)/test-marshallers.proto $(MARSHALLERS_DEPS)
+	$(AM_V_GEN)$(PYTHON) $(top_srcdir)/spice_codegen.py --generate-demarshallers --client --include test-marshallers.h $< $@ >/dev/null
 
 EXTRA_DIST =				\
 	$(TEST_MARSHALLERS)		\
diff --git a/tests/test-marshallers.c b/tests/test-marshallers.c
index a231b52..ae90770 100644
--- a/tests/test-marshallers.c
+++ b/tests/test-marshallers.c
@@ -15,15 +15,22 @@ static uint8_t expected_data[] = { 123, /* dummy byte */
                                    0x21, 0x43, 0x65, 0x87, 0x09, 0xba, 0xdc, 0xfe, /* data */
 };
 
+typedef void (*message_destructor_t)(uint8_t *message);
+uint8_t * spice_parse_msg(uint8_t *message_start, uint8_t *message_end,
+                          uint32_t channel, uint16_t message_type, int minor,
+                          size_t *size_out, message_destructor_t *free_message);
+
 int main(int argc G_GNUC_UNUSED, char **argv G_GNUC_UNUSED)
 {
     SpiceMarshaller *marshaller;
     SpiceMsgMainShortDataSubMarshall *msg;
-    size_t len;
+    size_t len, msg_len;
     int free_res;
     uint8_t *data;
+    message_destructor_t free_message;
 
-    msg = spice_malloc0(sizeof(SpiceMsgMainShortDataSubMarshall) + 2 * sizeof(uint64_t));
+    msg = g_new0(SpiceMsgMainShortDataSubMarshall, 1);
+    msg->data = g_new(uint64_t, 2);
     msg->dummy_byte = 123;
     msg->data_size = 2;
     msg->data[0] = 0x1234567890abcdef;
@@ -35,11 +42,27 @@ int main(int argc G_GNUC_UNUSED, char **argv G_GNUC_UNUSED)
     data = spice_marshaller_linearize(marshaller, 0, &len, &free_res);
     g_assert_cmpint(len, ==, G_N_ELEMENTS(expected_data));
     g_assert_true(memcmp(data, expected_data, len) == 0);
+
+    g_free(msg->data);
+    g_free(msg);
+
+    // test demarshaller
+    msg = (SpiceMsgMainShortDataSubMarshall *) spice_parse_msg(data, data + len, 1, 1, 0,
+                                                               &msg_len, &free_message);
+
+    g_assert_nonnull(msg);
+    g_assert_cmpint(msg->dummy_byte, ==, 123);
+    g_assert_cmpint(msg->data_size, ==, 2);
+    g_assert_nonnull(msg->data);
+    g_assert_cmpint(msg->data[0], ==, 0x1234567890abcdef);
+    g_assert_cmpint(msg->data[1], ==, 0xfedcba0987654321);
+
+    free_message((uint8_t *) msg);
+
     if (free_res) {
         free(data);
     }
     spice_marshaller_destroy(marshaller);
-    free(msg);
 
     return 0;
 }
diff --git a/tests/test-marshallers.h b/tests/test-marshallers.h
index 371fcdc..7e5a0b2 100644
--- a/tests/test-marshallers.h
+++ b/tests/test-marshallers.h
@@ -5,7 +5,7 @@
 typedef struct {
     uint32_t data_size;
     uint8_t dummy_byte;
-    uint64_t data[];
+    uint64_t *data;
 } SpiceMsgMainShortDataSubMarshall;
 
 #endif /* _H_TEST_MARSHALLERS */
-- 
2.13.5



More information about the Spice-devel mailing list