aboutsummaryrefslogtreecommitdiff
path: root/libwmc/tests
diff options
context:
space:
mode:
Diffstat (limited to 'libwmc/tests')
-rw-r--r--libwmc/tests/Makefile.am2
-rw-r--r--libwmc/tests/test-wmc-com.c223
-rw-r--r--libwmc/tests/test-wmc-com.h27
-rw-r--r--libwmc/tests/test-wmc.c51
4 files changed, 303 insertions, 0 deletions
diff --git a/libwmc/tests/Makefile.am b/libwmc/tests/Makefile.am
index 8b5e140b..faef9aa0 100644
--- a/libwmc/tests/Makefile.am
+++ b/libwmc/tests/Makefile.am
@@ -10,6 +10,8 @@ test_wmc_SOURCES = \
test-wmc-escaping.h \
test-wmc-utils.c \
test-wmc-utils.h \
+ test-wmc-com.c \
+ test-wmc-com.h \
test-wmc.c
test_wmc_CPPFLAGS = \
diff --git a/libwmc/tests/test-wmc-com.c b/libwmc/tests/test-wmc-com.c
new file mode 100644
index 00000000..a2e1e448
--- /dev/null
+++ b/libwmc/tests/test-wmc-com.c
@@ -0,0 +1,223 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * Copyright (C) 2011 Red Hat, Inc.
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public
+ * License as published by the Free Software Foundation
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <glib.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <termios.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#include "test-wmc-com.h"
+#include "com.h"
+#include "utils.h"
+#include "errors.h"
+
+/************************************************************/
+
+typedef struct {
+ char *port;
+ int fd;
+ struct termios old_t;
+ gboolean debug;
+ gboolean uml290;
+} TestComData;
+
+gpointer
+test_com_setup (const char *port, gboolean uml290)
+{
+ TestComData *d;
+ int ret;
+
+ d = g_malloc0 (sizeof (TestComData));
+ g_assert (d);
+ d->uml290 = uml290;
+
+ if (getenv ("SERIAL_DEBUG"))
+ d->debug = TRUE;
+
+ errno = 0;
+ d->fd = open (port, O_RDWR | O_EXCL | O_NONBLOCK | O_NOCTTY);
+ if (d->fd < 0)
+ g_warning ("%s: open failed: (%d) %s", port, errno, strerror (errno));
+ g_assert (d->fd >= 0);
+
+ ret = ioctl (d->fd, TIOCEXCL);
+ if (ret) {
+ g_warning ("%s: lock failed: (%d) %s", port, errno, strerror (errno));
+ close (d->fd);
+ d->fd = -1;
+ }
+ g_assert (ret == 0);
+
+ ret = ioctl (d->fd, TCGETA, &d->old_t);
+ if (ret) {
+ g_warning ("%s: old termios failed: (%d) %s", port, errno, strerror (errno));
+ close (d->fd);
+ d->fd = -1;
+ }
+ g_assert (ret == 0);
+
+ d->port = g_strdup (port);
+ return d;
+}
+
+void
+test_com_teardown (gpointer user_data)
+{
+ TestComData *d = user_data;
+
+ g_assert (d);
+
+ g_free (d->port);
+ close (d->fd);
+ g_free (d);
+}
+
+#if 0
+static void
+print_buf (const char *detail, const char *buf, gsize len)
+{
+ int i = 0;
+ gboolean newline = FALSE;
+
+ g_print ("%s (%zu) ", detail, len);
+ for (i = 0; i < len; i++) {
+ g_print ("0x%02x ", buf[i] & 0xFF);
+ if (((i + 1) % 12) == 0) {
+ g_print ("\n");
+ newline = TRUE;
+ } else
+ newline = FALSE;
+ }
+
+ if (!newline)
+ g_print ("\n");
+}
+
+static gboolean
+send_command (TestComData *d, char *buf, gsize len)
+{
+ int status;
+ int eagain_count = 1000;
+ gsize i = 0;
+
+ if (d->debug)
+ print_buf (">>>", buf, len);
+
+ while (i < len) {
+ errno = 0;
+ status = write (d->fd, &buf[i], 1);
+ if (status < 0) {
+ if (errno == EAGAIN) {
+ eagain_count--;
+ if (eagain_count <= 0)
+ return FALSE;
+ } else
+ g_assert (errno == 0);
+ } else
+ i++;
+
+ usleep (1000);
+ }
+
+ return TRUE;
+}
+
+static gsize
+wait_reply (TestComData *d, char *buf, gsize len)
+{
+ fd_set in;
+ int result;
+ struct timeval timeout = { 1, 0 };
+ char readbuf[1024];
+ ssize_t bytes_read;
+ int total = 0, retries = 0;
+ gsize decap_len = 0;
+
+ FD_ZERO (&in);
+ FD_SET (d->fd, &in);
+ result = select (d->fd + 1, &in, NULL, NULL, &timeout);
+ if (result != 1 || !FD_ISSET (d->fd, &in))
+ return 0;
+
+ do {
+ errno = 0;
+ bytes_read = read (d->fd, &readbuf[total], 1);
+ if ((bytes_read == 0) || (errno == EAGAIN)) {
+ /* Haven't gotten the async control char yet */
+ if (retries > 20)
+ return 0; /* 2 seconds, give up */
+
+ /* Otherwise wait a bit and try again */
+ usleep (100000);
+ retries++;
+ continue;
+ } else if (bytes_read == 1) {
+ gboolean more = FALSE, success;
+ gsize used = 0;
+
+ total++;
+ decap_len = 0;
+ success = hdlc_decapsulate_buffer (readbuf, total, d->uml290, 0x3030,
+ buf, len, &decap_len,
+ &used, &more);
+
+ /* Discard used data */
+ if (used > 0) {
+ total -= used;
+ memmove (readbuf, &readbuf[used], total);
+ }
+
+ if (success && !more) {
+ /* Success; we have a packet */
+ break;
+ }
+ } else {
+ /* Some error occurred */
+ return 0;
+ }
+ } while (total < sizeof (readbuf));
+
+ if (d->debug) {
+ print_buf ("<<<", readbuf, total);
+ print_buf ("D<<", buf, decap_len);
+ }
+
+ return decap_len;
+}
+#endif
+
+void
+test_com_port_init (void *f, void *data)
+{
+ TestComData *d = data;
+ WmcError *error = NULL;
+ gboolean success;
+
+ success = wmc_port_setup (d->fd, &error);
+ if (!success) {
+ g_warning ("%s: error setting up port: (%d) %s",
+ d->port,
+ error ? error->code : -1,
+ error && error->message ? error->message : "(unknown)");
+ }
+ g_assert (success);
+}
+
diff --git a/libwmc/tests/test-wmc-com.h b/libwmc/tests/test-wmc-com.h
new file mode 100644
index 00000000..9c6bd149
--- /dev/null
+++ b/libwmc/tests/test-wmc-com.h
@@ -0,0 +1,27 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * Copyright (C) 2011 Red Hat, Inc.
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public
+ * License as published by the Free Software Foundation
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TEST_WMC_COM_H
+#define TEST_WMC_COM_H
+
+gpointer test_com_setup (const char *port, gboolean uml290);
+void test_com_teardown (gpointer d);
+
+void test_com_port_init (void *f, void *data);
+
+#endif /* TEST_WMC_COM_H */
+
diff --git a/libwmc/tests/test-wmc.c b/libwmc/tests/test-wmc.c
index c465d36b..1c1921dd 100644
--- a/libwmc/tests/test-wmc.c
+++ b/libwmc/tests/test-wmc.c
@@ -21,6 +21,11 @@
#include "test-wmc-crc.h"
#include "test-wmc-escaping.h"
#include "test-wmc-utils.h"
+#include "test-wmc-com.h"
+
+typedef struct {
+ gpointer com_data;
+} TestData;
#if GLIB_CHECK_VERSION(2,25,12)
typedef GTestFixtureFunc TCFunc;
@@ -30,13 +35,52 @@ typedef void (*TCFunc)(void);
#define TESTCASE(t, d) g_test_create_case (#t, 0, d, NULL, (TCFunc) t, NULL)
+static TestData *
+test_data_new (const char *port, gboolean uml290)
+{
+ TestData *d;
+
+ d = g_malloc0 (sizeof (TestData));
+ g_assert (d);
+
+ if (port)
+ d->com_data = test_com_setup (port, uml290);
+
+ return d;
+}
+
+static void
+test_data_free (TestData *d)
+{
+ if (d->com_data)
+ test_com_teardown (d->com_data);
+
+ g_free (d);
+}
+
int main (int argc, char **argv)
{
GTestSuite *suite;
+ TestData *data;
+ int i;
+ const char *port = NULL;
gint result;
+ gboolean uml290 = FALSE;
g_test_init (&argc, &argv, NULL);
+ /* See if we got passed a serial port for live testing */
+ for (i = 0; i < argc; i++) {
+ if (!strcmp (argv[i], "--port")) {
+ /* Make sure there's actually a port in the next arg */
+ g_assert (argc > i + 1);
+ port = argv[++i];
+ } else if (!strcmp (argv[i], "--uml290"))
+ uml290 = TRUE;
+ }
+
+ data = test_data_new (port, uml290);
+
suite = g_test_get_root ();
g_test_suite_add (suite, TESTCASE (test_crc16_1, NULL));
g_test_suite_add (suite, TESTCASE (test_crc16_2, NULL));
@@ -51,8 +95,15 @@ int main (int argc, char **argv)
g_test_suite_add (suite, TESTCASE (test_utils_decapsulate_uml290_wmc1, NULL));
g_test_suite_add (suite, TESTCASE (test_utils_decapsulate_pc5740_wmc1, NULL));
+ /* Live tests */
+ if (port) {
+ g_test_suite_add (suite, TESTCASE (test_com_port_init, data->com_data));
+ }
+
result = g_test_run ();
+ test_data_free (data);
+
return result;
}