libqmi-glib,tests: new tests to check parsing valid/invalid QMI messages

This commit is contained in:
Aleksander Morgado 2012-09-21 09:47:07 +02:00
parent f9f91b7fe7
commit a336eca159
3 changed files with 135 additions and 4 deletions

1
.gitignore vendored
View File

@ -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

View File

@ -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

View File

@ -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 ();
}