From 941c302715cc277c37c732762939c3e6fea7372d Mon Sep 17 00:00:00 2001 From: Aleksander Morgado Date: Tue, 28 May 2013 14:43:28 +0200 Subject: [PATCH 1/2] test-utils: fix LE and BE tests Added independent BE tests with input buffers given in BE. Once the values are read by libqmi from the buffer, they will be always in host-endian, so we can safely compare them without further modifications. https://bugs.freedesktop.org/show_bug.cgi?id=64987 --- libqmi-glib/test/test-utils.c | 703 +++++++++++++++++++++++++++++++++++------- 1 file changed, 590 insertions(+), 113 deletions(-) diff --git a/libqmi-glib/test/test-utils.c b/libqmi-glib/test/test-utils.c index bba1c0e..b468317 100644 --- a/libqmi-glib/test/test-utils.c +++ b/libqmi-glib/test/test-utils.c @@ -17,14 +17,6 @@ #include #include "qmi-utils.h" -#define VAL_EQUAL(t, v, c, e) \ -{ \ - if (e == QMI_ENDIAN_LITTLE) \ - g_assert_cmpint (c##_FROM_LE (t), ==, v); \ - else \ - g_assert_cmpint (c##_FROM_BE (t), ==, v); \ -} - static void test_utils_uint8 (void) { @@ -84,7 +76,7 @@ test_utils_int8 (void) } static void -test_utils_uint16 (gconstpointer user_data) +test_utils_uint16_le (void) { static const guint8 in_buffer[8] = { 0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00 @@ -93,7 +85,6 @@ test_utils_uint16 (gconstpointer user_data) 0x500F, 0xE2EB, 0x00B6, 0x0000 }; guint8 out_buffer[8] = { 0 }; - QmiEndian endian = (QmiEndian) user_data; guint i; guint16 in_buffer_size; @@ -110,11 +101,44 @@ test_utils_uint16 (gconstpointer user_data) while (in_buffer_size) { guint16 tmp; - qmi_utils_read_guint16_from_buffer (&in_buffer_walker, &in_buffer_size, endian, &tmp); + qmi_utils_read_guint16_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + g_assert_cmpuint (tmp, ==, values[i++]); + qmi_utils_write_guint16_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + } + + g_assert_cmpuint (out_buffer_size, ==, 0); + g_assert (memcmp (in_buffer, out_buffer, sizeof (in_buffer)) == 0); +} + +static void +test_utils_uint16_be (void) +{ + static const guint8 in_buffer[8] = { + 0x50, 0x0F, 0xE2, 0xEB, 0x00, 0xB6, 0x00, 0x00 + }; + static const guint16 values[4] = { + 0x500F, 0xE2EB, 0x00B6, 0x0000 + }; + guint8 out_buffer[8] = { 0 }; + + guint i; + guint16 in_buffer_size; + const guint8 *in_buffer_walker; + guint16 out_buffer_size; + guint8 *out_buffer_walker; + + in_buffer_size = sizeof (in_buffer); + in_buffer_walker = &in_buffer[0]; + out_buffer_size = sizeof (out_buffer); + out_buffer_walker = &out_buffer[0]; + i = 0; - VAL_EQUAL (tmp, values[i++], GUINT16, endian); + while (in_buffer_size) { + guint16 tmp; - qmi_utils_write_guint16_to_buffer (&out_buffer_walker, &out_buffer_size, endian, &tmp); + qmi_utils_read_guint16_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_BIG, &tmp); + g_assert_cmpuint (tmp, ==, values[i++]); + qmi_utils_write_guint16_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_BIG, &tmp); } g_assert_cmpuint (out_buffer_size, ==, 0); @@ -122,7 +146,7 @@ test_utils_uint16 (gconstpointer user_data) } static void -test_utils_int16 (gconstpointer user_data) +test_utils_int16_le (void) { static const guint8 in_buffer[8] = { 0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00 @@ -131,7 +155,6 @@ test_utils_int16 (gconstpointer user_data) 0x500F, 0xE2EB, 0x00B6, 0x0000 }; guint8 out_buffer[8] = { 0 }; - QmiEndian endian = (QmiEndian) user_data; guint i; guint16 in_buffer_size; @@ -148,11 +171,44 @@ test_utils_int16 (gconstpointer user_data) while (in_buffer_size) { gint16 tmp; - qmi_utils_read_gint16_from_buffer (&in_buffer_walker, &in_buffer_size, endian, &tmp); + qmi_utils_read_gint16_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + g_assert_cmpint (tmp, ==, values[i++]); + qmi_utils_write_gint16_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + } - VAL_EQUAL (tmp, values[i++], GINT16, endian); + g_assert_cmpuint (out_buffer_size, ==, 0); + g_assert (memcmp (in_buffer, out_buffer, sizeof (in_buffer)) == 0); +} - qmi_utils_write_gint16_to_buffer (&out_buffer_walker, &out_buffer_size, endian, &tmp); +static void +test_utils_int16_be (void) +{ + static const guint8 in_buffer[8] = { + 0x50, 0x0F, 0xE2, 0xEB, 0x00, 0xB6, 0x00, 0x00 + }; + static const gint16 values[4] = { + 0x500F, 0xE2EB, 0x00B6, 0x0000 + }; + guint8 out_buffer[8] = { 0 }; + + guint i; + guint16 in_buffer_size; + const guint8 *in_buffer_walker; + guint16 out_buffer_size; + guint8 *out_buffer_walker; + + in_buffer_size = sizeof (in_buffer); + in_buffer_walker = &in_buffer[0]; + out_buffer_size = sizeof (out_buffer); + out_buffer_walker = &out_buffer[0]; + i = 0; + + while (in_buffer_size) { + gint16 tmp; + + qmi_utils_read_gint16_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_BIG, &tmp); + g_assert_cmpint (tmp, ==, values[i++]); + qmi_utils_write_gint16_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_BIG, &tmp); } g_assert_cmpuint (out_buffer_size, ==, 0); @@ -160,7 +216,7 @@ test_utils_int16 (gconstpointer user_data) } static void -test_utils_uint16_unaligned (gconstpointer user_data) +test_utils_uint16_unaligned_le (void) { static const guint8 in_buffer[9] = { 0x00, 0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00 @@ -169,7 +225,6 @@ test_utils_uint16_unaligned (gconstpointer user_data) 0x500F, 0xE2EB, 0x00B6, 0x0000 }; guint8 out_buffer[8] = { 0 }; - QmiEndian endian = (QmiEndian) user_data; guint i; guint16 in_buffer_size; @@ -186,11 +241,44 @@ test_utils_uint16_unaligned (gconstpointer user_data) while (in_buffer_size) { guint16 tmp; - qmi_utils_read_guint16_from_buffer (&in_buffer_walker, &in_buffer_size, endian, &tmp); + qmi_utils_read_guint16_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + g_assert_cmpuint (tmp, ==, values[i++]); + qmi_utils_write_guint16_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + } - VAL_EQUAL (tmp, values[i++], GUINT16, endian); + g_assert_cmpuint (out_buffer_size, ==, 0); + g_assert (memcmp (&in_buffer[1], out_buffer, sizeof (in_buffer) - 1) == 0); +} - qmi_utils_write_guint16_to_buffer (&out_buffer_walker, &out_buffer_size, endian, &tmp); +static void +test_utils_uint16_unaligned_be (void) +{ + static const guint8 in_buffer[9] = { + 0x00, 0x50, 0x0F, 0xE2, 0xEB, 0x00, 0xB6, 0x00, 0x00 + }; + static guint16 values[4] = { + 0x500F, 0xE2EB, 0x00B6, 0x0000 + }; + guint8 out_buffer[8] = { 0 }; + + guint i; + guint16 in_buffer_size; + const guint8 *in_buffer_walker; + guint16 out_buffer_size; + guint8 *out_buffer_walker; + + in_buffer_size = sizeof (in_buffer) - 1; + in_buffer_walker = &in_buffer[1]; + out_buffer_size = sizeof (out_buffer); + out_buffer_walker = &out_buffer[0]; + i = 0; + + while (in_buffer_size) { + guint16 tmp; + + qmi_utils_read_guint16_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_BIG, &tmp); + g_assert_cmpuint (tmp, ==, values[i++]); + qmi_utils_write_guint16_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_BIG, &tmp); } g_assert_cmpuint (out_buffer_size, ==, 0); @@ -198,7 +286,7 @@ test_utils_uint16_unaligned (gconstpointer user_data) } static void -test_utils_int16_unaligned (gconstpointer user_data) +test_utils_int16_unaligned_le (void) { static const guint8 in_buffer[9] = { 0x00, 0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00 @@ -207,7 +295,6 @@ test_utils_int16_unaligned (gconstpointer user_data) 0x500F, 0xE2EB, 0x00B6, 0x0000 }; guint8 out_buffer[8] = { 0 }; - QmiEndian endian = (QmiEndian) user_data; guint i; guint16 in_buffer_size; @@ -224,11 +311,44 @@ test_utils_int16_unaligned (gconstpointer user_data) while (in_buffer_size) { gint16 tmp; - qmi_utils_read_gint16_from_buffer (&in_buffer_walker, &in_buffer_size, endian, &tmp); + qmi_utils_read_gint16_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + g_assert_cmpint (tmp, ==, values[i++]); + qmi_utils_write_gint16_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + } + + g_assert_cmpuint (out_buffer_size, ==, 0); + g_assert (memcmp (&in_buffer[1], out_buffer, sizeof (in_buffer) - 1) == 0); +} + +static void +test_utils_int16_unaligned_be (void) +{ + static const guint8 in_buffer[9] = { + 0x00, 0x50, 0x0F, 0xE2, 0xEB, 0x00, 0xB6, 0x00, 0x00 + }; + static gint16 values[4] = { + 0x500F, 0xE2EB, 0x00B6, 0x0000 + }; + guint8 out_buffer[8] = { 0 }; + + guint i; + guint16 in_buffer_size; + const guint8 *in_buffer_walker; + guint16 out_buffer_size; + guint8 *out_buffer_walker; + + in_buffer_size = sizeof (in_buffer) - 1; + in_buffer_walker = &in_buffer[1]; + out_buffer_size = sizeof (out_buffer); + out_buffer_walker = &out_buffer[0]; + i = 0; - VAL_EQUAL (tmp, values[i++], GINT16, endian); + while (in_buffer_size) { + gint16 tmp; - qmi_utils_write_gint16_to_buffer (&out_buffer_walker, &out_buffer_size, endian, &tmp); + qmi_utils_read_gint16_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_BIG, &tmp); + g_assert_cmpint (tmp, ==, values[i++]); + qmi_utils_write_gint16_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_BIG, &tmp); } g_assert_cmpuint (out_buffer_size, ==, 0); @@ -236,7 +356,7 @@ test_utils_int16_unaligned (gconstpointer user_data) } static void -test_utils_uint32 (gconstpointer user_data) +test_utils_uint32_le (void) { static const guint8 in_buffer[8] = { 0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00 @@ -245,7 +365,6 @@ test_utils_uint32 (gconstpointer user_data) 0xE2EB500F, 0x000000B6 }; guint8 out_buffer[8] = { 0 }; - QmiEndian endian = (QmiEndian) user_data; guint i; guint16 in_buffer_size; @@ -262,11 +381,44 @@ test_utils_uint32 (gconstpointer user_data) while (in_buffer_size) { guint32 tmp; - qmi_utils_read_guint32_from_buffer (&in_buffer_walker, &in_buffer_size, endian, &tmp); + qmi_utils_read_guint32_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + g_assert_cmpuint (tmp, ==, values[i++]); + qmi_utils_write_guint32_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + } + + g_assert_cmpuint (out_buffer_size, ==, 0); + g_assert (memcmp (in_buffer, out_buffer, sizeof (in_buffer)) == 0); +} + +static void +test_utils_uint32_be (void) +{ + static const guint8 in_buffer[8] = { + 0xE2, 0xEB, 0x50, 0x0F, 0x00, 0x00, 0x00, 0xB6 + }; + static const guint32 values[2] = { + 0xE2EB500F, 0x000000B6 + }; + guint8 out_buffer[8] = { 0 }; + + guint i; + guint16 in_buffer_size; + const guint8 *in_buffer_walker; + guint16 out_buffer_size; + guint8 *out_buffer_walker; + + in_buffer_size = sizeof (in_buffer); + in_buffer_walker = &in_buffer[0]; + out_buffer_size = sizeof (out_buffer); + out_buffer_walker = &out_buffer[0]; + i = 0; - VAL_EQUAL (tmp, values[i++], GUINT32, endian); + while (in_buffer_size) { + guint32 tmp; - qmi_utils_write_guint32_to_buffer (&out_buffer_walker, &out_buffer_size, endian, &tmp); + qmi_utils_read_guint32_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_BIG, &tmp); + g_assert_cmpuint (tmp, ==, values[i++]); + qmi_utils_write_guint32_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_BIG, &tmp); } g_assert_cmpuint (out_buffer_size, ==, 0); @@ -274,7 +426,7 @@ test_utils_uint32 (gconstpointer user_data) } static void -test_utils_int32 (gconstpointer user_data) +test_utils_int32_le (void) { static const guint8 in_buffer[8] = { 0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00 @@ -283,7 +435,6 @@ test_utils_int32 (gconstpointer user_data) 0xE2EB500F, 0x000000B6 }; guint8 out_buffer[8] = { 0 }; - QmiEndian endian = (QmiEndian) user_data; guint i; guint16 in_buffer_size; @@ -300,11 +451,44 @@ test_utils_int32 (gconstpointer user_data) while (in_buffer_size) { gint32 tmp; - qmi_utils_read_gint32_from_buffer (&in_buffer_walker, &in_buffer_size, endian, &tmp); + qmi_utils_read_gint32_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + g_assert_cmpint (tmp, ==, values[i++]); + qmi_utils_write_gint32_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + } - VAL_EQUAL (tmp, values[i++], GINT32, endian); + g_assert_cmpuint (out_buffer_size, ==, 0); + g_assert (memcmp (in_buffer, out_buffer, sizeof (in_buffer)) == 0); +} + +static void +test_utils_int32_be (void) +{ + static const guint8 in_buffer[8] = { + 0xE2, 0xEB, 0x50, 0x0F, 0x00, 0x00, 0x00, 0xB6 + }; + static const gint32 values[2] = { + 0xE2EB500F, 0x000000B6 + }; + guint8 out_buffer[8] = { 0 }; + + guint i; + guint16 in_buffer_size; + const guint8 *in_buffer_walker; + guint16 out_buffer_size; + guint8 *out_buffer_walker; - qmi_utils_write_gint32_to_buffer (&out_buffer_walker, &out_buffer_size, endian, &tmp); + in_buffer_size = sizeof (in_buffer); + in_buffer_walker = &in_buffer[0]; + out_buffer_size = sizeof (out_buffer); + out_buffer_walker = &out_buffer[0]; + i = 0; + + while (in_buffer_size) { + gint32 tmp; + + qmi_utils_read_gint32_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_BIG, &tmp); + g_assert_cmpint (tmp, ==, values[i++]); + qmi_utils_write_gint32_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_BIG, &tmp); } g_assert_cmpuint (out_buffer_size, ==, 0); @@ -312,7 +496,7 @@ test_utils_int32 (gconstpointer user_data) } static void -test_utils_uint32_unaligned (gconstpointer user_data) +test_utils_uint32_unaligned_le (void) { static const guint8 in_buffer[9] = { 0x00, 0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00 @@ -321,7 +505,6 @@ test_utils_uint32_unaligned (gconstpointer user_data) 0xE2EB500F, 0x000000B6 }; guint8 out_buffer[8] = { 0 }; - QmiEndian endian = (QmiEndian) user_data; guint i; guint16 in_buffer_size; @@ -338,11 +521,44 @@ test_utils_uint32_unaligned (gconstpointer user_data) while (in_buffer_size) { guint32 tmp; - qmi_utils_read_guint32_from_buffer (&in_buffer_walker, &in_buffer_size, endian, &tmp); + qmi_utils_read_guint32_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + g_assert_cmpuint (tmp, ==, values[i++]); + qmi_utils_write_guint32_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + } - VAL_EQUAL (tmp, values[i++], GUINT32, endian); + g_assert_cmpuint (out_buffer_size, ==, 0); + g_assert (memcmp (&in_buffer[1], out_buffer, sizeof (in_buffer) - 1) == 0); +} - qmi_utils_write_guint32_to_buffer (&out_buffer_walker, &out_buffer_size, endian, &tmp); +static void +test_utils_uint32_unaligned_be (void) +{ + static const guint8 in_buffer[9] = { + 0x00, 0xE2, 0xEB, 0x50, 0x0F, 0x00, 0x00, 0x00, 0xB6 + }; + static guint32 values[2] = { + 0xE2EB500F, 0x000000B6 + }; + guint8 out_buffer[8] = { 0 }; + + guint i; + guint16 in_buffer_size; + const guint8 *in_buffer_walker; + guint16 out_buffer_size; + guint8 *out_buffer_walker; + + in_buffer_size = sizeof (in_buffer) - 1; + in_buffer_walker = &in_buffer[1]; + out_buffer_size = sizeof (out_buffer); + out_buffer_walker = &out_buffer[0]; + i = 0; + + while (in_buffer_size) { + guint32 tmp; + + qmi_utils_read_guint32_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_BIG, &tmp); + g_assert_cmpuint (tmp, ==, values[i++]); + qmi_utils_write_guint32_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_BIG, &tmp); } g_assert_cmpuint (out_buffer_size, ==, 0); @@ -350,7 +566,7 @@ test_utils_uint32_unaligned (gconstpointer user_data) } static void -test_utils_int32_unaligned (gconstpointer user_data) +test_utils_int32_unaligned_le (void) { static const guint8 in_buffer[9] = { 0x00, 0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00 @@ -365,7 +581,6 @@ test_utils_int32_unaligned (gconstpointer user_data) const guint8 *in_buffer_walker; guint16 out_buffer_size; guint8 *out_buffer_walker; - QmiEndian endian = (QmiEndian) user_data; in_buffer_size = sizeof (in_buffer) - 1; in_buffer_walker = &in_buffer[1]; @@ -376,11 +591,44 @@ test_utils_int32_unaligned (gconstpointer user_data) while (in_buffer_size) { gint32 tmp; - qmi_utils_read_gint32_from_buffer (&in_buffer_walker, &in_buffer_size, endian, &tmp); + qmi_utils_read_gint32_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + g_assert_cmpint (tmp, ==, values[i++]); + qmi_utils_write_gint32_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + } + + g_assert_cmpuint (out_buffer_size, ==, 0); + g_assert (memcmp (&in_buffer[1], out_buffer, sizeof (in_buffer) - 1) == 0); +} - VAL_EQUAL (tmp, values[i++], GINT32, endian); +static void +test_utils_int32_unaligned_be (void) +{ + static const guint8 in_buffer[9] = { + 0x00, 0xE2, 0xEB, 0x50, 0x0F, 0x00, 0x00, 0x00, 0xB6 + }; + static gint32 values[2] = { + 0xE2EB500F, 0x000000B6 + }; + guint8 out_buffer[8] = { 0 }; - qmi_utils_write_gint32_to_buffer (&out_buffer_walker, &out_buffer_size, endian, &tmp); + guint i; + guint16 in_buffer_size; + const guint8 *in_buffer_walker; + guint16 out_buffer_size; + guint8 *out_buffer_walker; + + in_buffer_size = sizeof (in_buffer) - 1; + in_buffer_walker = &in_buffer[1]; + out_buffer_size = sizeof (out_buffer); + out_buffer_walker = &out_buffer[0]; + i = 0; + + while (in_buffer_size) { + gint32 tmp; + + qmi_utils_read_gint32_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_BIG, &tmp); + g_assert_cmpint (tmp, ==, values[i++]); + qmi_utils_write_gint32_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_BIG, &tmp); } g_assert_cmpuint (out_buffer_size, ==, 0); @@ -388,7 +636,7 @@ test_utils_int32_unaligned (gconstpointer user_data) } static void -test_utils_uint64 (gconstpointer user_data) +test_utils_uint64_le (void) { static const guint8 in_buffer[8] = { 0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00 @@ -403,7 +651,6 @@ test_utils_uint64 (gconstpointer user_data) const guint8 *in_buffer_walker; guint16 out_buffer_size; guint8 *out_buffer_walker; - QmiEndian endian = (QmiEndian) user_data; in_buffer_size = sizeof (in_buffer); in_buffer_walker = &in_buffer[0]; @@ -414,11 +661,44 @@ test_utils_uint64 (gconstpointer user_data) while (in_buffer_size) { guint64 tmp; - qmi_utils_read_guint64_from_buffer (&in_buffer_walker, &in_buffer_size, endian, &tmp); + qmi_utils_read_guint64_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + g_assert_cmpuint (tmp, ==, values[i++]); + qmi_utils_write_guint64_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + } + + g_assert_cmpuint (out_buffer_size, ==, 0); + g_assert (memcmp (in_buffer, out_buffer, sizeof (in_buffer)) == 0); +} + +static void +test_utils_uint64_be (void) +{ + static const guint8 in_buffer[8] = { + 0x00, 0x00, 0x00, 0xB6, 0xE2, 0xEB, 0x50, 0x0F + }; + static const guint64 values[1] = { + 0x000000B6E2EB500FULL + }; + guint8 out_buffer[8] = { 0 }; + + guint i; + guint16 in_buffer_size; + const guint8 *in_buffer_walker; + guint16 out_buffer_size; + guint8 *out_buffer_walker; + + in_buffer_size = sizeof (in_buffer); + in_buffer_walker = &in_buffer[0]; + out_buffer_size = sizeof (out_buffer); + out_buffer_walker = &out_buffer[0]; + i = 0; - VAL_EQUAL (tmp, values[i++], GUINT64, endian); + while (in_buffer_size) { + guint64 tmp; - qmi_utils_write_guint64_to_buffer (&out_buffer_walker, &out_buffer_size, endian, &tmp); + qmi_utils_read_guint64_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_BIG, &tmp); + g_assert_cmpuint (tmp, ==, values[i++]); + qmi_utils_write_guint64_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_BIG, &tmp); } g_assert_cmpuint (out_buffer_size, ==, 0); @@ -426,7 +706,7 @@ test_utils_uint64 (gconstpointer user_data) } static void -test_utils_int64 (gconstpointer user_data) +test_utils_int64_le (void) { static const guint8 in_buffer[8] = { 0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00 @@ -441,7 +721,6 @@ test_utils_int64 (gconstpointer user_data) const guint8 *in_buffer_walker; guint16 out_buffer_size; guint8 *out_buffer_walker; - QmiEndian endian = (QmiEndian) user_data; in_buffer_size = sizeof (in_buffer); in_buffer_walker = &in_buffer[0]; @@ -452,11 +731,44 @@ test_utils_int64 (gconstpointer user_data) while (in_buffer_size) { gint64 tmp; - qmi_utils_read_gint64_from_buffer (&in_buffer_walker, &in_buffer_size, endian, &tmp); + qmi_utils_read_gint64_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + g_assert_cmpint (tmp, ==, values[i++]); + qmi_utils_write_gint64_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + } - VAL_EQUAL (tmp, values[i++], GINT64, endian); + g_assert_cmpuint (out_buffer_size, ==, 0); + g_assert (memcmp (in_buffer, out_buffer, sizeof (in_buffer)) == 0); +} - qmi_utils_write_gint64_to_buffer (&out_buffer_walker, &out_buffer_size, endian, &tmp); +static void +test_utils_int64_be (void) +{ + static const guint8 in_buffer[8] = { + 0x00, 0x00, 0x00, 0xB6, 0xE2, 0xEB, 0x50, 0x0F + }; + static const gint64 values[1] = { + 0x000000B6E2EB500FLL + }; + guint8 out_buffer[8] = { 0 }; + + guint i; + guint16 in_buffer_size; + const guint8 *in_buffer_walker; + guint16 out_buffer_size; + guint8 *out_buffer_walker; + + in_buffer_size = sizeof (in_buffer); + in_buffer_walker = &in_buffer[0]; + out_buffer_size = sizeof (out_buffer); + out_buffer_walker = &out_buffer[0]; + i = 0; + + while (in_buffer_size) { + gint64 tmp; + + qmi_utils_read_gint64_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_BIG, &tmp); + g_assert_cmpint (tmp, ==, values[i++]); + qmi_utils_write_gint64_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_BIG, &tmp); } g_assert_cmpuint (out_buffer_size, ==, 0); @@ -464,7 +776,7 @@ test_utils_int64 (gconstpointer user_data) } static void -test_utils_uint64_unaligned (gconstpointer user_data) +test_utils_uint64_unaligned_le (void) { static const guint8 in_buffer[9] = { 0x00, 0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00 @@ -479,7 +791,6 @@ test_utils_uint64_unaligned (gconstpointer user_data) const guint8 *in_buffer_walker; guint16 out_buffer_size; guint8 *out_buffer_walker; - QmiEndian endian = (QmiEndian) user_data; in_buffer_size = sizeof (in_buffer) - 1; in_buffer_walker = &in_buffer[1]; @@ -490,11 +801,44 @@ test_utils_uint64_unaligned (gconstpointer user_data) while (in_buffer_size) { guint64 tmp; - qmi_utils_read_guint64_from_buffer (&in_buffer_walker, &in_buffer_size, endian, &tmp); + qmi_utils_read_guint64_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + g_assert_cmpuint (tmp, ==, values[i++]); + qmi_utils_write_guint64_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + } - VAL_EQUAL (tmp, values[i++], GUINT64, endian); + g_assert_cmpuint (out_buffer_size, ==, 0); + g_assert (memcmp (&in_buffer[1], out_buffer, sizeof (in_buffer) - 1) == 0); +} + +static void +test_utils_uint64_unaligned_be (void) +{ + static const guint8 in_buffer[9] = { + 0x00, 0x00, 0x00, 0x00, 0xB6, 0xE2, 0xEB, 0x50, 0x0F + }; + static guint64 values[1] = { + 0x000000B6E2EB500FULL + }; + guint8 out_buffer[8] = { 0 }; - qmi_utils_write_guint64_to_buffer (&out_buffer_walker, &out_buffer_size, endian, &tmp); + guint i; + guint16 in_buffer_size; + const guint8 *in_buffer_walker; + guint16 out_buffer_size; + guint8 *out_buffer_walker; + + in_buffer_size = sizeof (in_buffer) - 1; + in_buffer_walker = &in_buffer[1]; + out_buffer_size = sizeof (out_buffer); + out_buffer_walker = &out_buffer[0]; + i = 0; + + while (in_buffer_size) { + guint64 tmp; + + qmi_utils_read_guint64_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_BIG, &tmp); + g_assert_cmpuint (tmp, ==, values[i++]); + qmi_utils_write_guint64_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_BIG, &tmp); } g_assert_cmpuint (out_buffer_size, ==, 0); @@ -502,7 +846,7 @@ test_utils_uint64_unaligned (gconstpointer user_data) } static void -test_utils_int64_unaligned (gconstpointer user_data) +test_utils_int64_unaligned_le (void) { static const guint8 in_buffer[9] = { 0x00, 0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00 @@ -517,7 +861,6 @@ test_utils_int64_unaligned (gconstpointer user_data) const guint8 *in_buffer_walker; guint16 out_buffer_size; guint8 *out_buffer_walker; - QmiEndian endian = (QmiEndian) user_data; in_buffer_size = sizeof (in_buffer) - 1; in_buffer_walker = &in_buffer[1]; @@ -528,11 +871,44 @@ test_utils_int64_unaligned (gconstpointer user_data) while (in_buffer_size) { gint64 tmp; - qmi_utils_read_gint64_from_buffer (&in_buffer_walker, &in_buffer_size, endian, &tmp); + qmi_utils_read_gint64_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + g_assert_cmpint (tmp, ==, values[i++]); + qmi_utils_write_gint64_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_LITTLE, &tmp); + } - VAL_EQUAL (tmp, values[i++], GINT64, endian); + g_assert_cmpuint (out_buffer_size, ==, 0); + g_assert (memcmp (&in_buffer[1], out_buffer, sizeof (in_buffer) - 1) == 0); +} - qmi_utils_write_gint64_to_buffer (&out_buffer_walker, &out_buffer_size, endian, &tmp); +static void +test_utils_int64_unaligned_be (void) +{ + static const guint8 in_buffer[9] = { + 0x00, 0x00, 0x00, 0x00, 0xB6, 0xE2, 0xEB, 0x50, 0x0F + }; + static gint64 values[1] = { + 0x000000B6E2EB500FLL + }; + guint8 out_buffer[8] = { 0 }; + + guint i; + guint16 in_buffer_size; + const guint8 *in_buffer_walker; + guint16 out_buffer_size; + guint8 *out_buffer_walker; + + in_buffer_size = sizeof (in_buffer) - 1; + in_buffer_walker = &in_buffer[1]; + out_buffer_size = sizeof (out_buffer); + out_buffer_walker = &out_buffer[0]; + i = 0; + + while (in_buffer_size) { + gint64 tmp; + + qmi_utils_read_gint64_from_buffer (&in_buffer_walker, &in_buffer_size, QMI_ENDIAN_BIG, &tmp); + g_assert_cmpint (tmp, ==, values[i++]); + qmi_utils_write_gint64_to_buffer (&out_buffer_walker, &out_buffer_size, QMI_ENDIAN_BIG, &tmp); } g_assert_cmpuint (out_buffer_size, ==, 0); @@ -540,7 +916,7 @@ test_utils_int64_unaligned (gconstpointer user_data) } static void -common_test_utils_uint_sized (guint n_bytes, QmiEndian endian) +common_test_utils_uint_sized_le (guint n_bytes) { static const guint8 in_buffer[8] = { 0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00 @@ -573,26 +949,78 @@ common_test_utils_uint_sized (guint n_bytes, QmiEndian endian) out_buffer_walker = &out_buffer[0]; i = 0; - qmi_utils_read_sized_guint_from_buffer (&in_buffer_walker, &in_buffer_size, n_bytes, endian, &tmp); - VAL_EQUAL (tmp, value, GUINT64, endian); - qmi_utils_write_sized_guint_to_buffer (&out_buffer_walker, &out_buffer_size, n_bytes, endian, &tmp); + qmi_utils_read_sized_guint_from_buffer (&in_buffer_walker, &in_buffer_size, n_bytes, QMI_ENDIAN_LITTLE, &tmp); + g_assert_cmpuint (tmp, ==, value); + qmi_utils_write_sized_guint_to_buffer (&out_buffer_walker, &out_buffer_size, n_bytes, QMI_ENDIAN_LITTLE, &tmp); + + g_assert_cmpuint (out_buffer_size, ==, 8 - n_bytes); + g_assert (memcmp (expected_out_buffer, out_buffer, sizeof (expected_out_buffer)) == 0); +} + +static void +test_utils_uint_sized_le (void) +{ + guint i; + + for (i = 1; i <= 8; i++) { + common_test_utils_uint_sized_le (i); + } +} + +static void +common_test_utils_uint_sized_be (guint n_bytes) +{ + static const guint8 in_buffer[8] = { + 0x00, 0x00, 0x00, 0xB6, 0xE2, 0xEB, 0x50, 0x0F + }; + guint64 value = 0x000000B6E2EB500FULL; + guint8 expected_out_buffer[8] = { 0 }; + guint8 out_buffer[8] = { 0 }; + + guint64 tmp; + guint i; + guint16 in_buffer_size; + const guint8 *in_buffer_walker; + guint16 out_buffer_size; + guint8 *out_buffer_walker; + + /* Build expected intermediate value */ + tmp = 0xFF; + for (i = 1; i < n_bytes; i++) { + tmp <<= 8; + tmp |= 0xFF; + } + value &= tmp; + + /* Build expected output buffer */ + memcpy (expected_out_buffer, in_buffer, n_bytes); + + in_buffer_size = sizeof (in_buffer); + in_buffer_walker = &in_buffer[0]; + out_buffer_size = sizeof (out_buffer); + out_buffer_walker = &out_buffer[0]; + i = 0; + + qmi_utils_read_sized_guint_from_buffer (&in_buffer_walker, &in_buffer_size, n_bytes, QMI_ENDIAN_BIG, &tmp); + g_assert_cmpuint (tmp, ==, value); + qmi_utils_write_sized_guint_to_buffer (&out_buffer_walker, &out_buffer_size, n_bytes, QMI_ENDIAN_BIG, &tmp); g_assert_cmpuint (out_buffer_size, ==, 8 - n_bytes); g_assert (memcmp (expected_out_buffer, out_buffer, sizeof (expected_out_buffer)) == 0); } static void -test_utils_uint_sized (gconstpointer user_data) +test_utils_uint_sized_be (void) { guint i; for (i = 1; i <= 8; i++) { - common_test_utils_uint_sized (i, (QmiEndian) user_data); + common_test_utils_uint_sized_be (i); } } static void -common_test_utils_uint_sized_unaligned (guint n_bytes, QmiEndian endian) +common_test_utils_uint_sized_unaligned_le (guint n_bytes) { static const guint8 in_buffer[9] = { 0x00, 0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00 @@ -625,26 +1053,75 @@ common_test_utils_uint_sized_unaligned (guint n_bytes, QmiEndian endian) out_buffer_walker = &out_buffer[0]; i = 0; - qmi_utils_read_sized_guint_from_buffer (&in_buffer_walker, &in_buffer_size, n_bytes, endian, &tmp); - VAL_EQUAL (tmp, value, GUINT64, endian); - qmi_utils_write_sized_guint_to_buffer (&out_buffer_walker, &out_buffer_size, n_bytes, endian, &tmp); + qmi_utils_read_sized_guint_from_buffer (&in_buffer_walker, &in_buffer_size, n_bytes, QMI_ENDIAN_LITTLE, &tmp); + g_assert_cmpuint (tmp, ==, value); + qmi_utils_write_sized_guint_to_buffer (&out_buffer_walker, &out_buffer_size, n_bytes, QMI_ENDIAN_LITTLE, &tmp); g_assert_cmpuint (out_buffer_size, ==, 8 - n_bytes); g_assert (memcmp (expected_out_buffer, out_buffer, sizeof (expected_out_buffer)) == 0); } static void -test_utils_uint_sized_unaligned (gconstpointer user_data) +test_utils_uint_sized_unaligned_le (void) { guint i; for (i = 1; i <= 8; i++) { - common_test_utils_uint_sized_unaligned (i, (QmiEndian) user_data); + common_test_utils_uint_sized_unaligned_le (i); } } -#define LE ((gconstpointer) QMI_ENDIAN_LITTLE) -#define BE ((gconstpointer) QMI_ENDIAN_BIG) +static void +common_test_utils_uint_sized_unaligned_be (guint n_bytes) +{ + static const guint8 in_buffer[9] = { + 0x00, 0x00, 0x00, 0xB6, 0xE2, 0xEB, 0x50, 0x0F, 0x00 + }; + guint64 value = 0x000000B6E2EB500FULL; + guint8 expected_out_buffer[8] = { 0 }; + guint8 out_buffer[8] = { 0 }; + + guint64 tmp; + guint i; + guint16 in_buffer_size; + const guint8 *in_buffer_walker; + guint16 out_buffer_size; + guint8 *out_buffer_walker; + + /* Build expected intermediate value */ + tmp = 0xFF; + for (i = 1; i < n_bytes; i++) { + tmp <<= 8; + tmp |= 0xFF; + } + value &= tmp; + + /* Build expected output buffer */ + memcpy (expected_out_buffer, &in_buffer[1], n_bytes); + + in_buffer_size = sizeof (in_buffer) - 1; + in_buffer_walker = &in_buffer[1]; + out_buffer_size = sizeof (out_buffer); + out_buffer_walker = &out_buffer[0]; + i = 0; + + qmi_utils_read_sized_guint_from_buffer (&in_buffer_walker, &in_buffer_size, n_bytes, QMI_ENDIAN_BIG, &tmp); + g_assert_cmpuint (tmp, ==, value); + qmi_utils_write_sized_guint_to_buffer (&out_buffer_walker, &out_buffer_size, n_bytes, QMI_ENDIAN_BIG, &tmp); + + g_assert_cmpuint (out_buffer_size, ==, 8 - n_bytes); + g_assert (memcmp (expected_out_buffer, out_buffer, sizeof (expected_out_buffer)) == 0); +} + +static void +test_utils_uint_sized_unaligned_be (void) +{ + guint i; + + for (i = 1; i <= 8; i++) { + common_test_utils_uint_sized_unaligned_be (i); + } +} int main (int argc, char **argv) { @@ -654,37 +1131,37 @@ int main (int argc, char **argv) g_test_add_func ("/libqmi-glib/utils/uint8", test_utils_uint8); g_test_add_func ("/libqmi-glib/utils/int8", test_utils_int8); - g_test_add_data_func ("/libqmi-glib/utils/uint16-LE", LE, test_utils_uint16); - g_test_add_data_func ("/libqmi-glib/utils/uint16-BE", BE, test_utils_uint16); - g_test_add_data_func ("/libqmi-glib/utils/int16-LE", LE, test_utils_int16); - g_test_add_data_func ("/libqmi-glib/utils/int16-BE", BE, test_utils_int16); - g_test_add_data_func ("/libqmi-glib/utils/uint16-unaligned-LE", LE, test_utils_uint16_unaligned); - g_test_add_data_func ("/libqmi-glib/utils/uint16-unaligned-Be", BE, test_utils_uint16_unaligned); - g_test_add_data_func ("/libqmi-glib/utils/int16-unaligned-LE", LE, test_utils_int16_unaligned); - g_test_add_data_func ("/libqmi-glib/utils/int16-unaligned-BE", BE, test_utils_int16_unaligned); - - g_test_add_data_func ("/libqmi-glib/utils/uint32-LE", LE, test_utils_uint32); - g_test_add_data_func ("/libqmi-glib/utils/uint32-BE", BE, test_utils_uint32); - g_test_add_data_func ("/libqmi-glib/utils/int32-LE", LE, test_utils_int32); - g_test_add_data_func ("/libqmi-glib/utils/int32-BE", BE, test_utils_int32); - g_test_add_data_func ("/libqmi-glib/utils/uint32/unaligned-LE", LE, test_utils_uint32_unaligned); - g_test_add_data_func ("/libqmi-glib/utils/uint32/unaligned-BE", BE, test_utils_uint32_unaligned); - g_test_add_data_func ("/libqmi-glib/utils/int32/unaligned-LE", LE, test_utils_int32_unaligned); - g_test_add_data_func ("/libqmi-glib/utils/int32/unaligned-BE", BE, test_utils_int32_unaligned); - - g_test_add_data_func ("/libqmi-glib/utils/uint64-LE", LE, test_utils_uint64); - g_test_add_data_func ("/libqmi-glib/utils/uint64-BE", BE, test_utils_uint64); - g_test_add_data_func ("/libqmi-glib/utils/int64-LE", LE, test_utils_int64); - g_test_add_data_func ("/libqmi-glib/utils/int64-BE", BE, test_utils_int64); - g_test_add_data_func ("/libqmi-glib/utils/uint64/unaligned-LE", LE, test_utils_uint64_unaligned); - g_test_add_data_func ("/libqmi-glib/utils/uint64/unaligned-BE", BE, test_utils_uint64_unaligned); - g_test_add_data_func ("/libqmi-glib/utils/int64/unaligned-LE", LE, test_utils_int64_unaligned); - g_test_add_data_func ("/libqmi-glib/utils/int64/unaligned-BE", BE, test_utils_int64_unaligned); - - g_test_add_data_func ("/libqmi-glib/utils/uint-sized-LE", LE, test_utils_uint_sized); - g_test_add_data_func ("/libqmi-glib/utils/uint-sized-BE", BE, test_utils_uint_sized); - g_test_add_data_func ("/libqmi-glib/utils/uint-sized/unaligned-LE", LE, test_utils_uint_sized_unaligned); - g_test_add_data_func ("/libqmi-glib/utils/uint-sized/unaligned-BE", BE, test_utils_uint_sized_unaligned); + g_test_add_func ("/libqmi-glib/utils/uint16-LE", test_utils_uint16_le); + g_test_add_func ("/libqmi-glib/utils/uint16-BE", test_utils_uint16_be); + g_test_add_func ("/libqmi-glib/utils/int16-LE", test_utils_int16_le); + g_test_add_func ("/libqmi-glib/utils/int16-BE", test_utils_int16_be); + g_test_add_func ("/libqmi-glib/utils/uint16-unaligned-LE", test_utils_uint16_unaligned_le); + g_test_add_func ("/libqmi-glib/utils/uint16-unaligned-Be", test_utils_uint16_unaligned_be); + g_test_add_func ("/libqmi-glib/utils/int16-unaligned-LE", test_utils_int16_unaligned_le); + g_test_add_func ("/libqmi-glib/utils/int16-unaligned-BE", test_utils_int16_unaligned_be); + + g_test_add_func ("/libqmi-glib/utils/uint32-LE", test_utils_uint32_le); + g_test_add_func ("/libqmi-glib/utils/uint32-BE", test_utils_uint32_be); + g_test_add_func ("/libqmi-glib/utils/int32-LE", test_utils_int32_le); + g_test_add_func ("/libqmi-glib/utils/int32-BE", test_utils_int32_be); + g_test_add_func ("/libqmi-glib/utils/uint32/unaligned-LE", test_utils_uint32_unaligned_le); + g_test_add_func ("/libqmi-glib/utils/uint32/unaligned-BE", test_utils_uint32_unaligned_be); + g_test_add_func ("/libqmi-glib/utils/int32/unaligned-LE", test_utils_int32_unaligned_le); + g_test_add_func ("/libqmi-glib/utils/int32/unaligned-BE", test_utils_int32_unaligned_be); + + g_test_add_func ("/libqmi-glib/utils/uint64-LE", test_utils_uint64_le); + g_test_add_func ("/libqmi-glib/utils/uint64-BE", test_utils_uint64_be); + g_test_add_func ("/libqmi-glib/utils/int64-LE", test_utils_int64_le); + g_test_add_func ("/libqmi-glib/utils/int64-BE", test_utils_int64_be); + g_test_add_func ("/libqmi-glib/utils/uint64/unaligned-LE", test_utils_uint64_unaligned_le); + g_test_add_func ("/libqmi-glib/utils/uint64/unaligned-BE", test_utils_uint64_unaligned_be); + g_test_add_func ("/libqmi-glib/utils/int64/unaligned-LE", test_utils_int64_unaligned_le); + g_test_add_func ("/libqmi-glib/utils/int64/unaligned-BE", test_utils_int64_unaligned_be); + + g_test_add_func ("/libqmi-glib/utils/uint-sized-LE", test_utils_uint_sized_le); + g_test_add_func ("/libqmi-glib/utils/uint-sized-BE", test_utils_uint_sized_be); + g_test_add_func ("/libqmi-glib/utils/uint-sized/unaligned-LE", test_utils_uint_sized_unaligned_le); + g_test_add_func ("/libqmi-glib/utils/uint-sized/unaligned-BE", test_utils_uint_sized_unaligned_be); return g_test_run (); } -- 1.8.1.4