libwmc: add more test code and start of com tests

This commit is contained in:
Dan Williams
2011-09-27 13:13:40 -05:00
parent 8250dee131
commit 18fc92ef73
10 changed files with 591 additions and 22 deletions

View File

@@ -1,28 +1,17 @@
noinst_LTLIBRARIES = libwmc.la libwmc-test.la
noinst_LTLIBRARIES = libwmc.la
libwmc_la_CPPFLAGS = \
$(MM_CFLAGS)
libwmc_la_SOURCES = \
errors.c \
errors.h \
utils.c \
utils.h
utils.h \
com.c \
com.h
libwmc_la_LIBADD = \
$(MM_LIBS)
###########################################
# Test library without symbol versioning
###########################################
libwmc_test_la_CPPFLAGS = \
$(MM_CFLAGS)
libwmc_test_la_SOURCES = \
utils.c \
utils.h
libwmc_test_la_LIBADD = \
$(MM_LIBS)

57
libwmc/src/com.c Normal file
View File

@@ -0,0 +1,57 @@
/* -*- 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 <errno.h>
#include <termios.h>
#include <fcntl.h>
#include <string.h>
#include "com.h"
#include "errors.h"
wbool
wmc_port_setup (int fd, WmcError **error)
{
struct termios stbuf;
errno = 0;
memset (&stbuf, 0, sizeof (stbuf));
if (tcgetattr (fd, &stbuf) != 0) {
wmc_error_set (error, WMC_SERIAL_ERROR, WMC_SERIAL_ERROR_CONFIG_FAILED,
"tcgetattr() error: %d", errno);
}
stbuf.c_cflag &= ~(CBAUD | CSIZE | CSTOPB | CLOCAL | PARENB);
stbuf.c_iflag &= ~(HUPCL | IUTF8 | IUCLC | ISTRIP | IXON | IXOFF | IXANY | ICRNL);
stbuf.c_oflag &= ~(OPOST | OCRNL | ONLCR | OLCUC | ONLRET);
stbuf.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO | ECHOE | ECHOK | ECHONL);
stbuf.c_lflag &= ~(NOFLSH | XCASE | TOSTOP | ECHOPRT | ECHOCTL | ECHOKE);
stbuf.c_cc[VMIN] = 1;
stbuf.c_cc[VTIME] = 0;
stbuf.c_cc[VEOF] = 1;
stbuf.c_cflag |= (B115200 | CS8 | CREAD | 0 | 0); /* No parity, 1 stop bit */
errno = 0;
if (tcsetattr (fd, TCSANOW, &stbuf) < 0) {
wmc_error_set (error, WMC_SERIAL_ERROR, WMC_SERIAL_ERROR_CONFIG_FAILED,
"tcsetattr() error: %d", errno);
return FALSE;
}
return TRUE;
}

33
libwmc/src/com.h Normal file
View File

@@ -0,0 +1,33 @@
/* -*- 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 LIBWMC_COM_H
#define LIBWMC_COM_H
#include "errors.h"
enum {
WMC_SERIAL_ERROR = 1000
};
enum {
WMC_SERIAL_ERROR_CONFIG_FAILED = 1
};
wbool wmc_port_setup (int fd, WmcError **error);
#endif /* LIBWMC_COM_H */

109
libwmc/src/errors.c Normal file
View File

@@ -0,0 +1,109 @@
/* -*- 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 "errors.h"
#include <stdlib.h>
#include <string.h>
WmcError *
wmc_error_new (u_int32_t domain,
u_int32_t code,
const char *format,
...)
{
WmcError *error;
va_list args;
int n;
wmc_return_val_if_fail (format != NULL, NULL);
wmc_return_val_if_fail (format[0] != '\0', NULL);
error = malloc (sizeof (WmcError));
wmc_assert (error != NULL);
error->domain = domain;
error->code = code;
va_start (args, format);
n = vasprintf (&error->message, format, args);
va_end (args);
if (n < 0) {
free (error);
return NULL;
}
return error;
}
void
wmc_error_set (WmcError **error,
u_int32_t domain,
u_int32_t code,
const char *format,
...)
{
va_list args;
int n;
if (error == NULL)
return;
wmc_return_if_fail (*error == NULL);
wmc_return_if_fail (format[0] != '\0');
*error = malloc (sizeof (WmcError));
wmc_assert (*error != NULL);
(*error)->domain = domain;
(*error)->code = code;
va_start (args, format);
n = vasprintf (&(*error)->message, format, args);
va_end (args);
if (n < 0) {
free (*error);
*error = NULL;
}
}
static void
free_error (WmcError *error)
{
if (error) {
if (error->message)
free (error->message);
memset (error, 0, sizeof (*error));
free (error);
}
}
void
wmc_clear_error (WmcError **error)
{
if (error) {
free_error (*error);
*error = NULL;
}
}
void
wmc_free_error (WmcError *error)
{
free_error (error);
}

74
libwmc/src/errors.h Normal file
View File

@@ -0,0 +1,74 @@
/* -*- 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 LIBWMC_ERRORS_H
#define LIBWMC_ERRORS_H
#include <config.h>
#include <sys/types.h>
#include <assert.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdio.h>
typedef u_int8_t wbool;
#ifndef TRUE
#define TRUE (u_int8_t) 1
#endif
#ifndef FALSE
#define FALSE (u_int8_t) 0
#endif
typedef struct {
u_int32_t domain;
u_int32_t code;
char *message;
} WmcError;
WmcError *wmc_error_new (u_int32_t domain,
u_int32_t code,
const char *format,
...) __attribute__((__format__ (__printf__, 3, 4)));
void wmc_error_set (WmcError **error,
u_int32_t domain,
u_int32_t code,
const char *format,
...) __attribute__((__format__ (__printf__, 4, 5)));
void wmc_clear_error (WmcError **error);
void wmc_free_error (WmcError *error);
#define wmc_assert assert
#define wmc_return_if_fail(e) \
{ \
if (!(e)) { \
fprintf (stderr, "failed: ##e##\n"); \
return; \
} \
}
#define wmc_return_val_if_fail(e, v) \
{ \
if (!(e)) { \
fprintf (stderr, "failed: ##e##\n"); \
return v; \
} \
}
#endif /* LIBWMC_COM_H */

View File

@@ -274,9 +274,11 @@ uml290_wmc_encapsulate (char *inbuf,
* hdlc_decapsulate_buffer:
* @inbuf: buffer in which to look for an HDLC frame
* @inbuf_len: length of valid data in @inbuf
* @check_known_crc: if %TRUE, validate the CRC using @known_crc
* @check_known_crc: if %TRUE, validate the CRC using @known_crc if the normal
* CRC check fails
* @known_crc: if @check_known_crc is %TRUE, compare the frame's CRC against
* @known_crc. @known_crc must be in Little Endian (LE) byte order.
* @known_crc if the normal CRC check fails. @known_crc must be in Little
* Endian (LE) byte order.
* @outbuf: buffer in which to put decapsulated data from the HDLC frame
* @outbuf_len: max size of @outbuf
* @out_decap_len: on success, size of the decapsulated data
@@ -365,12 +367,14 @@ hdlc_decapsulate_buffer (const char *inbuf,
}
/* Check the CRC of the packet's data */
crc = check_known_crc ? known_crc : crc16 (outbuf, unesc_len - 2, 0);
crc = crc16 (outbuf, unesc_len - 2, 0);
pkt_crc = outbuf[unesc_len - 2] & 0xFF;
pkt_crc |= (outbuf[unesc_len - 1] & 0xFF) << 8;
if (crc != pkt_crc) {
*out_used = pkt_len + 1; /* packet + CRC + 0x7E */
return FALSE;
if (!check_known_crc || (pkt_crc != known_crc)) {
*out_used = pkt_len + 1; /* packet + CRC + 0x7E */
return FALSE;
}
}
*out_used = pkt_len + 1; /* packet + CRC + 0x7E */

View File

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

223
libwmc/tests/test-wmc-com.c Normal file
View File

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

View File

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

View File

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