diff options
author | Aleksander Morgado <aleksander@lanedo.com> | 2012-09-21 09:47:07 +0200 |
---|---|---|
committer | Aleksander Morgado <aleksander@lanedo.com> | 2012-09-26 09:25:06 +0200 |
commit | a336eca159f3c8c9b7f2f65c05c9adefb2617322 (patch) | |
tree | 1004342a07ca6b13837c90506d5cb17a31ab5b4c | |
parent | f9f91b7fe7b22a263ac5f16fab062242ac17f757 (diff) |
libqmi-glib,tests: new tests to check parsing valid/invalid QMI messages
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | libqmi-glib/test/Makefile.am | 19 | ||||
-rw-r--r-- | libqmi-glib/test/test-message.c | 119 |
3 files changed, 135 insertions, 4 deletions
@@ -63,6 +63,7 @@ libqmi-glib/test/.deps libqmi-glib/test/Makefile libqmi-glib/test/Makefile.in libqmi-glib/test/test-utils +libqmi-glib/test/test-message cli/.deps cli/.libs diff --git a/libqmi-glib/test/Makefile.am b/libqmi-glib/test/Makefile.am index fa95310..9a5c967 100644 --- a/libqmi-glib/test/Makefile.am +++ b/libqmi-glib/test/Makefile.am @@ -1,23 +1,34 @@ noinst_PROGRAMS = \ - test-utils + test-utils \ + test-message test_utils_SOURCES = \ test-utils.c - test_utils_CPPFLAGS = \ $(LIBQMI_GLIB_CFLAGS) \ -I$(top_srcdir) \ -I$(top_srcdir)/libqmi-glib \ -I$(top_builddir)/libqmi-glib - test_utils_LDADD = \ $(top_builddir)/libqmi-glib/libqmi-glib.la \ $(LIBQMI_GLIB_LIBS) +test_message_SOURCES = \ + test-message.c +test_message_CPPFLAGS = \ + $(LIBQMI_GLIB_CFLAGS) \ + -I$(top_srcdir) \ + -I$(top_srcdir)/libqmi-glib \ + -I$(top_builddir)/libqmi-glib +test_message_LDADD = \ + $(top_builddir)/libqmi-glib/libqmi-glib.la \ + $(LIBQMI_GLIB_LIBS) + if WITH_TESTS -check-local: test-utils +check-local: test-utils test-message $(abs_builddir)/test-utils + $(abs_builddir)/test-message endif diff --git a/libqmi-glib/test/test-message.c b/libqmi-glib/test/test-message.c new file mode 100644 index 0000000..089cdf4 --- /dev/null +++ b/libqmi-glib/test/test-message.c @@ -0,0 +1,119 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details: + * + * Copyright (C) 2012 Aleksander Morgado <aleksander@gnu.org> + */ + +#include <config.h> +#include <glib-object.h> +#include <string.h> +#include "qmi-message.h" + +static void +test_message_parse_common (const guint8 *buffer, + guint buffer_len, + guint n_expected_messages) +{ + GError *error = NULL; + GByteArray *array; + guint n_messages = 0; + + array = g_byte_array_sized_new (buffer_len); + g_byte_array_append (array, buffer, buffer_len); + + do { + QmiMessage *message; + + message = qmi_message_new_from_raw (array, &error); + if (!message) { + if (error) { + g_debug ("Error creating message from raw data: '%s'", error->message); + g_error_free (error); + } + break; + } + + n_messages++; + qmi_message_unref (message); + } while (array->len > 0); + + g_assert_cmpuint (n_messages, ==, n_expected_messages); +} + +static void +test_message_parse_short (void) +{ + const guint8 buffer[] = { + 0x01, 0x26, 0x00, 0x80, 0x03, 0x01, 0x02, 0x01, 0x00, 0x20, 0x00, 0x1a, + 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x9b, + 0x05, 0x11, 0x04, 0x00, 0x01, 0x00, 0x66, 0x05 + }; + + test_message_parse_common (buffer, sizeof (buffer), 0); +} + +static void +test_message_parse_complete (void) +{ + const guint8 buffer[] = { + 0x01, 0x26, 0x00, 0x80, 0x03, 0x01, 0x02, 0x01, 0x00, 0x20, 0x00, 0x1a, + 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x9b, + 0x05, 0x11, 0x04, 0x00, 0x01, 0x00, 0x65, 0x05, 0x12, 0x04, 0x00, 0x01, + 0x00, 0x11, 0x05 + }; + + test_message_parse_common (buffer, sizeof (buffer), 1); +} + +static void +test_message_parse_complete_and_short (void) +{ + const guint8 buffer[] = { + 0x01, 0x26, 0x00, 0x80, 0x03, 0x01, 0x02, 0x01, 0x00, 0x20, 0x00, 0x1a, + 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x9b, + 0x05, 0x11, 0x04, 0x00, 0x01, 0x00, 0x65, 0x05, 0x12, 0x04, 0x00, 0x01, + 0x00, 0x11, 0x05, 0x01, 0x26, 0x00, 0x80, 0x03, 0x01, 0x02, 0x01, 0x00, + 0x20, 0x00, 0x1a, 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + 0x02, 0x00, 0x9b, 0x05, 0x11, 0x04, 0x00, 0x01, 0x00, 0x66, 0x05 + }; + + test_message_parse_common (buffer, sizeof (buffer), 1); +} + +static void +test_message_parse_complete_and_complete (void) +{ + const guint8 buffer[] = { + 0x01, 0x26, 0x00, 0x80, 0x03, 0x01, 0x02, 0x01, 0x00, 0x20, 0x00, 0x1a, + 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x9b, + 0x05, 0x11, 0x04, 0x00, 0x01, 0x00, 0x65, 0x05, 0x12, 0x04, 0x00, 0x01, + 0x00, 0x11, 0x05, 0x01, 0x26, 0x00, 0x80, 0x03, 0x01, 0x02, 0x01, 0x00, + 0x20, 0x00, 0x1a, 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + 0x02, 0x00, 0x9b, 0x05, 0x11, 0x04, 0x00, 0x01, 0x00, 0x65, 0x05, 0x12, + 0x04, 0x00, 0x01, 0x00, 0x11, 0x05 + }; + + test_message_parse_common (buffer, sizeof (buffer), 2); +} + +int main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/libqmi-glib/message/parse/short", test_message_parse_short); + g_test_add_func ("/libqmi-glib/message/parse/complete", test_message_parse_complete); + g_test_add_func ("/libqmi-glib/message/parse/complete-and-short", test_message_parse_complete_and_short); + g_test_add_func ("/libqmi-glib/message/parse/complete-and-complete", test_message_parse_complete_and_complete); + + return g_test_run (); +} |