aboutsummaryrefslogtreecommitdiff
path: root/libqcdm/tests
diff options
context:
space:
mode:
authorDan Williams <dcbw@redhat.com>2010-02-16 09:58:47 -0800
committerDan Williams <dcbw@redhat.com>2010-02-16 09:58:47 -0800
commit6239d2e3510bc136a14fdcf7d87e6d85c344bf5a (patch)
treef12dddb5281427693d20e77e3b58bf7e5594840a /libqcdm/tests
parent626f2953bf394eff4361a6d06a608349605fb5aa (diff)
qcdm: implement command handling and minimal infrastructure
Diffstat (limited to 'libqcdm/tests')
-rw-r--r--libqcdm/tests/Makefile.am2
-rw-r--r--libqcdm/tests/test-qcdm-com.c241
-rw-r--r--libqcdm/tests/test-qcdm-com.h27
-rw-r--r--libqcdm/tests/test-qcdm.c53
4 files changed, 322 insertions, 1 deletions
diff --git a/libqcdm/tests/Makefile.am b/libqcdm/tests/Makefile.am
index 810a86b1..8f5e9ae1 100644
--- a/libqcdm/tests/Makefile.am
+++ b/libqcdm/tests/Makefile.am
@@ -8,6 +8,8 @@ test_qcdm_SOURCES = \
test-qcdm-crc.h \
test-qcdm-escaping.c \
test-qcdm-escaping.h \
+ test-qcdm-com.c \
+ test-qcdm-com.h \
test-qcdm.c
test_qcdm_CPPFLAGS = \
diff --git a/libqcdm/tests/test-qcdm-com.c b/libqcdm/tests/test-qcdm-com.c
new file mode 100644
index 00000000..5ea59f67
--- /dev/null
+++ b/libqcdm/tests/test-qcdm-com.c
@@ -0,0 +1,241 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * Copyright (C) 2010 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-qcdm-com.h"
+#include "com.h"
+#include "utils.h"
+#include "result.h"
+#include "commands.h"
+
+typedef struct {
+ char *port;
+ int fd;
+ struct termios old_t;
+ gboolean debug;
+} TestComData;
+
+gpointer
+test_com_setup (const char *port)
+{
+ TestComData *d;
+ int ret;
+
+ d = g_malloc0 (sizeof (TestComData));
+ g_assert (d);
+
+ 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);
+}
+
+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;
+ char *p = &readbuf[0];
+ int total = 0, retries = 0;
+ gboolean escaping = FALSE;
+
+ 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, p, 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) {
+ /* Check for the async control char */
+ if (*p++ == DIAG_CONTROL_CHAR)
+ break;
+ total++;
+ } else {
+ /* Some error occurred */
+ return 0;
+ }
+ } while (total <= sizeof (readbuf));
+
+ if (d->debug)
+ print_buf ("<<<", readbuf, total);
+
+ return dm_unescape (readbuf, total, buf, len, &escaping);
+}
+
+void
+test_com (void *f, void *data)
+{
+ TestComData *d = data;
+ gboolean success;
+ GError *error = NULL;
+ char buf[100];
+ const char *str;
+ gint len;
+ QCDMResult *result;
+ gsize reply_len;
+
+ success = qcdm_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);
+
+ len = qcdm_cmd_version_info_new (buf, sizeof (buf), NULL);
+ g_assert (len == 4);
+
+ /* Send the command */
+ success = send_command (d, buf, len);
+ g_assert (success);
+
+ /* Get a response */
+ reply_len = wait_reply (d, buf, sizeof (buf));
+
+ /* Parse the response into a result structure */
+ result = qcdm_cmd_version_info_result (buf, reply_len, &error);
+ g_assert (result);
+
+ str = NULL;
+ qcdm_result_get_string (result, QCDM_CMD_VERSION_INFO_ITEM_COMP_DATE, &str);
+ g_message ("%s: Compiled Date: %s", __func__, str);
+
+ str = NULL;
+ qcdm_result_get_string (result, QCDM_CMD_VERSION_INFO_ITEM_COMP_TIME, &str);
+ g_message ("%s: Compiled Time: %s", __func__, str);
+
+ str = NULL;
+ qcdm_result_get_string (result, QCDM_CMD_VERSION_INFO_ITEM_RELEASE_DATE, &str);
+ g_message ("%s: Release Date: %s", __func__, str);
+
+ str = NULL;
+ qcdm_result_get_string (result, QCDM_CMD_VERSION_INFO_ITEM_RELEASE_TIME, &str);
+ g_message ("%s: Release Time: %s", __func__, str);
+
+ str = NULL;
+ qcdm_result_get_string (result, QCDM_CMD_VERSION_INFO_ITEM_MODEL, &str);
+ g_message ("%s: Model: %s", __func__, str);
+}
+
diff --git a/libqcdm/tests/test-qcdm-com.h b/libqcdm/tests/test-qcdm-com.h
new file mode 100644
index 00000000..26067386
--- /dev/null
+++ b/libqcdm/tests/test-qcdm-com.h
@@ -0,0 +1,27 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * Copyright (C) 2010 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_QCDM_COM_H
+#define TEST_QCDM_COM_H
+
+gpointer test_com_setup (const char *port);
+void test_com_teardown (gpointer d);
+
+void test_com (void *f, void *data);
+
+#endif /* TEST_QCDM_COM_H */
+
diff --git a/libqcdm/tests/test-qcdm.c b/libqcdm/tests/test-qcdm.c
index b7da97e6..c8581957 100644
--- a/libqcdm/tests/test-qcdm.c
+++ b/libqcdm/tests/test-qcdm.c
@@ -20,17 +20,60 @@
#include "test-qcdm-crc.h"
#include "test-qcdm-escaping.h"
+#include "test-qcdm-com.h"
+
+typedef struct {
+ gpointer com_data;
+} TestData;
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)
+{
+ TestData *d;
+
+ d = g_malloc0 (sizeof (TestData));
+ g_assert (d);
+
+ if (port)
+ d->com_data = test_com_setup (port);
+
+ 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;
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];
+ }
+ }
+
+ data = test_data_new (port);
+
suite = g_test_get_root ();
g_test_suite_add (suite, TESTCASE (test_crc16_1, NULL));
@@ -39,6 +82,14 @@ int main (int argc, char **argv)
g_test_suite_add (suite, TESTCASE (test_escape2, NULL));
g_test_suite_add (suite, TESTCASE (test_escape_unescape, NULL));
- return g_test_run ();
+ /* Live tests */
+ if (port)
+ g_test_suite_add (suite, TESTCASE (test_com, data->com_data));
+
+ result = g_test_run ();
+
+ test_data_free (data);
+
+ return result;
}