Skip to contentSkip to conversation

New rilmodem sms test

PATCH Superseded Tony Espy 2 participants 5 messages Nov 18, 2015 Kbuild

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Tony Espy

author
#0 lore raw

cover New rilmodem sms test

This patch series adds a second rilmodem test for SMS. The common server test functions have been extracted for use by future rilmodem tests.

Tony Espy (2):
unit: add new test-rilmodem-sms
build: add support for test-rilmodem-sms

 Makefile.am                 |  14 ++-
 unit/rilmodem-test-server.c | 198 ++++++++++++++++++++++++++++++++
 unit/rilmodem-test-server.h |  40 +++++++
 unit/test-rilmodem-sms.c    | 267 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 518 insertions(+), 1 deletion(-)
 create mode 100644 unit/rilmodem-test-server.c
 create mode 100644 unit/rilmodem-test-server.h
 create mode 100644 unit/test-rilmodem-sms.c
Signature · 1 line
2.1.4
author
#1 lore raw

PATCH 1/2 unit: add new test-rilmodem-sms

---
 unit/rilmodem-test-server.c | 198 ++++++++++++++++++++++++++++++++
 unit/rilmodem-test-server.h |  40 +++++++
 unit/test-rilmodem-sms.c    | 267 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 505 insertions(+)
 create mode 100644 unit/rilmodem-test-server.c
 create mode 100644 unit/rilmodem-test-server.h
 create mode 100644 unit/test-rilmodem-sms.c
unit/rilmodem-test-server.c new file +198 -0
new file mode 100644
index 0000000..a09e22c
@@ -0,0 +1,198 @@
+/*
+ *
+ *  oFono - Open Source Telephony
+ *
+ *  Copyright (C) 2015 Canonical Ltd.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 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, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#define _GNU_SOURCE
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+
+#include <ofono/types.h>
+
+#include <gril.h>
+
+#include "rilmodem-test-server.h"
+
+#define MAX_REQUEST_SIZE 4096
+
+static int server_sk;
+static ConnectFunc connect_func;
+static GIOChannel *server_io;
+static const struct rilmodem_test_data *rtd;
+
+/* Warning: length is stored in network order */
+struct rsp_hdr {
+	uint32_t length;
+	uint32_t unsolicited;
+	uint32_t serial;
+	uint32_t error;
+};
+
+static gboolean read_server(gpointer data)
+{
+	GIOStatus status;
+	gsize offset, rbytes, wbytes;
+	gchar *buf, *bufp;
+	uint32_t req_serial;
+	struct rsp_hdr rsp;
+
+	buf = g_malloc0(MAX_REQUEST_SIZE);
+
+	status = g_io_channel_read_chars(server_io, buf, MAX_REQUEST_SIZE,
+								&rbytes, NULL);
+	g_assert(status == G_IO_STATUS_NORMAL);
+
+	g_assert(rbytes == rtd->req_size);
+
+	/* validate len, and request_id */
+	g_assert(!memcmp(buf, rtd->req_data, (sizeof(uint32_t) * 2)));
+
+	/*
+	 * header: size (uint32), reqid (uin32), serial (uint32)
+	 * header size == 16 ( excludes sizeof(size) )
+	 */
+
+	/* advance past request_no */
+	bufp = buf + (sizeof(uint32_t) * 2);
+
+	req_serial = (uint32_t) *bufp;
+
+	/* advance past serial_no */
+	bufp += sizeof(uint32_t);
+
+	/* validate the rest of the parcel... */
+	offset = (sizeof(uint32_t) * 3);
+	g_assert(!memcmp(bufp, rtd->req_data + offset,
+						rtd->req_size - offset));
+
+	/* Length does not include the length field. Network order. */
+	rsp.length = htonl(sizeof(rsp) - sizeof(rsp.length) + rtd->rsp_size);
+	rsp.unsolicited = 0;
+	rsp.serial = req_serial;
+	rsp.error = rtd->rsp_error;
+
+	/* copy header */
+	memcpy(buf, &rsp, sizeof(rsp));
+
+	if (rtd->rsp_size) {
+		bufp = buf + sizeof(rsp);
+
+		memcpy(bufp, rtd->rsp_data, rtd->rsp_size);
+	}
+
+
+	status = g_io_channel_write_chars(server_io,
+					buf,
+					sizeof(rsp) + rtd->rsp_size,
+					&wbytes, NULL);
+
+	/* FIXME: assert wbytes is correct */
+
+	g_assert(status == G_IO_STATUS_NORMAL);
+
+	g_free(buf);
+	g_io_channel_unref(server_io);
+
+	return FALSE;
+}
+
+static gboolean on_socket_connected(GIOChannel *chan, GIOCondition cond,
+								gpointer data)
+{
+	struct sockaddr saddr;
+	unsigned int len = sizeof(saddr);
+	int fd;
+	GIOStatus status;
+
+	g_assert(cond == G_IO_IN);
+
+	fd = accept(server_sk, &saddr, &len);
+	g_assert(fd != -1);
+
+	server_io = g_io_channel_unix_new(fd);
+	g_assert(server_io != NULL);
+
+	if (connect_func)
+		connect_func(data);
+
+	status = g_io_channel_set_encoding(server_io, NULL, NULL);
+	g_assert(status == G_IO_STATUS_NORMAL);
+
+	g_io_channel_set_buffered(server_io, FALSE);
+	g_io_channel_set_close_on_unref(server_io, TRUE);
+
+	g_idle_add(read_server, data);
+
+	return FALSE;
+}
+
+void rilmodem_test_server_close(void)
+{
+	g_assert(server_sk);
+	close(server_sk);
+	server_sk = 0;
+}
+
+void rilmodem_test_server_create(ConnectFunc connect,
+				const struct rilmodem_test_data *test_data,
+				void *data)
+{
+	GIOChannel *io;
+	struct sockaddr_un addr;
+	int retval;
+
+	g_assert(server_sk == 0);
+
+	connect_func = connect;
+	rtd = test_data;
+
+	server_sk = socket(AF_UNIX, SOCK_STREAM, 0);
+	g_assert(server_sk);
+
+	memset(&addr, 0, sizeof(addr));
+	addr.sun_family = AF_UNIX;
+	strncpy(addr.sun_path, RIL_SERVER_SOCK_PATH, sizeof(addr.sun_path) - 1);
+
+	/* Unlink any existing socket for this session */
+	unlink(addr.sun_path);
+
+	retval = bind(server_sk, (struct sockaddr *) &addr, sizeof(addr));
+	g_assert(retval >= 0);
+
+	retval = listen(server_sk, 0);
+	g_assert(retval >= 0);
+
+	io = g_io_channel_unix_new(server_sk);
+	g_assert(io != NULL);
+
+	g_io_channel_set_close_on_unref(io, TRUE);
+	g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
+
+	g_io_add_watch_full(io,	G_PRIORITY_DEFAULT,
+				G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+				on_socket_connected, data, NULL);
+
+	g_io_channel_unref(io);
+}
unit/rilmodem-test-server.h new file +40 -0
new file mode 100644
index 0000000..ba8b43c
@@ -0,0 +1,40 @@
+/*
+ *
+ *  oFono - Open Source Telephony
+ *
+ *  Copyright (C) 2015 Canonical Ltd.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 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, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#define RIL_SERVER_SOCK_PATH    "/tmp/unittestril"
+
+struct rilmodem_test_data {
+	const unsigned char *req_data;
+
+	const size_t req_size;
+
+	uint32_t rsp_error;
+	const unsigned char *rsp_data;
+	const size_t rsp_size;
+};
+
+typedef void (*ConnectFunc)(void *data);
+
+void rilmodem_test_server_close(void);
+
+void rilmodem_test_server_create(ConnectFunc connect,
+				const struct rilmodem_test_data *test_data,
+				void *data);
unit/test-rilmodem-sms.c new file +267 -0

267 lines not on this page · raw message

Signature · 1 line
2.1.4
#2 lore raw

Re: [PATCH 1/2] unit: add new test-rilmodem-sms

Hi Tony,

Quoted 9 lines · Tony Espy
---
  unit/rilmodem-test-server.c | 198 ++++++++++++++++++++++++++++++++
  unit/rilmodem-test-server.h |  40 +++++++
  unit/test-rilmodem-sms.c    | 267 ++++++++++++++++++++++++++++++++++++++++++++
  3 files changed, 505 insertions(+)

create mode 100644 unit/rilmodem-test-server.c
create mode 100644 unit/rilmodem-test-server.h
create mode 100644 unit/test-rilmodem-sms.c

<snip>

Quoted 24 lines
unit/rilmodem-test-server.c new file +24 -0
+void rilmodem_test_server_create(ConnectFunc connect,
+				const struct rilmodem_test_data *test_data,
+				void *data)
+{
+	GIOChannel *io;
+	struct sockaddr_un addr;
+	int retval;
+
+	g_assert(server_sk == 0);
+
+	connect_func = connect;
+	rtd = test_data;
+
+	server_sk = socket(AF_UNIX, SOCK_STREAM, 0);
+	g_assert(server_sk);
+
+	memset(&addr, 0, sizeof(addr));
+	addr.sun_family = AF_UNIX;
+	strncpy(addr.sun_path, RIL_SERVER_SOCK_PATH, sizeof(addr.sun_path) - 1);
+
+	/* Unlink any existing socket for this session */
+	unlink(addr.sun_path);
+
+	retval = bind(server_sk, (struct sockaddr *) &addr, sizeof(addr));

unit/rilmodem-test-server.c line 191

Quoted 10 lines
unit/rilmodem-test-server.c new file +10 -0
+	g_assert(retval >= 0);
+
+	retval = listen(server_sk, 0);
+	g_assert(retval >= 0);
+
+	io = g_io_channel_unix_new(server_sk);
+	g_assert(io != NULL);
+
+	g_io_channel_set_close_on_unref(io, TRUE);
+	g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);

Not sure you really need the non-block flag for a socket you will call accept on once. But okay.

Quoted 7 lines
unit/rilmodem-test-server.c new file +7 -0
+
+	g_io_add_watch_full(io,	G_PRIORITY_DEFAULT,
+				G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+				on_socket_connected, data, NULL);
+
+	g_io_channel_unref(io);
+}

<snip>

Quoted 5 lines
unit/test-rilmodem-sms.c new file +0 -0
new file mode 100644
index 0000000..0e3bff3

<snip>

unit/test-rilmodem-sms.c line 130

Quoted 2 lines
unit/test-rilmodem-sms.c new file +2 -0
+/* TODO: Did these get removed upstream? */
+

Whats this about?

Quoted 34 lines
unit/test-rilmodem-sms.c new file +34 -0
+/* Declarations && Re-implementations of core functions. */
+void ril_sms_exit(void);
+void ril_sms_init(void);
+
+struct ofono_sms {
+	void *driver_data;
+};
+
+struct ofono_sms *ofono_sms_create(struct ofono_modem *modem,
+					unsigned int vendor,
+					const char *driver,
+					void *data)
+{
+	struct rilmodem_sms_data *rsd = data;
+	struct ofono_sms *sms = g_new0(struct ofono_sms, 1);
+	int retval;
+
+	retval = smsdriver->probe(sms, OFONO_RIL_VENDOR_AOSP, rsd->ril);
+	g_assert(retval == 0);
+
+	return sms;
+}
+
+int ofono_sms_driver_register(const struct ofono_sms_driver *d)
+{
+	if (smsdriver == NULL)
+		smsdriver = d;
+
+	return 0;
+}
+
+void ofono_sms_set_data(struct ofono_sms *sms, void *data)
+{
+	sms->driver_data = data;

unit/test-rilmodem-sms.c line 174

Quoted 10 lines
unit/test-rilmodem-sms.c new file +10 -0
+}
+
+void *ofono_sms_get_data(struct ofono_sms *sms)
+{
+	return sms->driver_data;
+}
+
+void ofono_sms_register(struct ofono_sms *sms)
+{
+	;

Can we just make these empty functions instead of the ';' thing?

Quoted 24 lines
unit/test-rilmodem-sms.c new file +24 -0
+}
+
+void ofono_sms_driver_unregister(const struct ofono_sms_driver *d)
+{
+	;
+}
+
+void ofono_sms_deliver_notify(struct ofono_sms *sms, const unsigned char *pdu,
+				int len, int tpdu_len)
+{
+	;
+}
+
+void ofono_sms_status_notify(struct ofono_sms *sms, const unsigned char *pdu,
+				int len, int tpdu_len)
+{
+	;
+}
+
+static void server_connect_cb(gpointer data)
+{
+	struct rilmodem_sms_data *rsd = data;
+	const struct sms_data *sd = rsd->test_data;
+

unit/test-rilmodem-sms.c line 208

Quoted 10 lines
unit/test-rilmodem-sms.c new file +10 -0
+	/* This causes local impl of _create() to call driver's probe func. */
+	rsd->sms = ofono_sms_create(NULL, OFONO_RIL_VENDOR_AOSP,
+							"rilmodem", rsd);
+
+	/* add_idle doesn't work, read blocks main loop!!! */
+	g_assert(sd->start_func(rsd) == FALSE);
+}
+
+#if BYTE_ORDER == LITTLE_ENDIAN
+

Can we keep the number of these #if conditions to a minimum? Would it suffice to simply #ifdef this out in main.c? Or better yet, disable from the build completely.

Quoted 50 lines
unit/test-rilmodem-sms.c new file +49 -0
+/*
+ * This unit test:
+ *  - does some test data setup
+ *  - configures a dummy server socket
+ *  - creates a new gril client instance
+ *    - triggers a connect to the dummy
+ *      server socket
+ *  - starts a mainloop
+ */
+static void test_sms_func(gconstpointer data)
+{
+	const struct sms_data *sd = data;
+	struct rilmodem_sms_data *rsd;
+
+	ril_sms_init();
+
+	rsd = g_new0(struct rilmodem_sms_data, 1);
+
+	rsd->test_data = sd;
+
+	rilmodem_test_server_create(&server_connect_cb, &sd->rtd, rsd);
+
+	rsd->ril = g_ril_new(RIL_SERVER_SOCK_PATH, OFONO_RIL_VENDOR_AOSP);
+	g_assert(rsd->ril != NULL);
+
+	mainloop = g_main_loop_new(NULL, FALSE);
+
+	g_main_loop_run(mainloop);
+	g_main_loop_unref(mainloop);
+
+	smsdriver->remove(rsd->sms);
+	g_ril_unref(rsd->ril);
+	g_free(rsd);
+
+	rilmodem_test_server_close();
+
+	ril_sms_exit();
+}
+
+#endif
+
+int main(int argc, char **argv)
+{
+	g_test_init(&argc, &argv, NULL);
+
+/*
+ * As all our architectures are little-endian except for
+ * PowerPC, and the Binder wire-format differs slightly
+ * depending on endian-ness, the following guards against test

unit/test-rilmodem-sms.c line 267

Quoted 10 lines
unit/test-rilmodem-sms.c new file +10 -0
+ * failures when run on PowerPC.
+ */
+#if BYTE_ORDER == LITTLE_ENDIAN
+	g_test_add_data_func("/testrilmodemsms/sca_query/valid/1",
+					&testdata_sca_query_valid_1,
+					test_sms_func);
+
+#endif
+	return g_test_run();
+}

Regards,
-Denis

author
#3 lore raw

PATCH 2/2 build: add support for test-rilmodem-sms

In reply to Tony Espy
---
 Makefile.am | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
Makefile.am +13 -1
index fba25ba..1cdfc1f 100644
@@ -740,7 +740,8 @@ unit_objects =
 unit_tests = unit/test-common unit/test-util unit/test-idmap \
 				unit/test-simutil unit/test-stkutil \
 				unit/test-sms unit/test-cdmasms \
-				unit/test-rilmodem-cs
+				unit/test-rilmodem-cs \
+				unit/test-rilmodem-sms
 
 noinst_PROGRAMS = $(unit_tests) \
 			unit/test-sms-root unit/test-mux unit/test-caif
@@ -801,6 +802,17 @@ unit_test_rilmodem_cs_LDADD = gdbus/libgdbus-internal.la $(builtin_libadd) \
 			@GLIB_LIBS@ @DBUS_LIBS@ -ldl
 unit_objects += $(unit_test_rilmodem_cs_OBJECTS)
 
+unit_test_rilmodem_sms_SOURCES = unit/test-rilmodem-sms.c $(gril_sources) \
+			src/log.c src/common.c src/util.c \
+			gatchat/ringbuffer.h gatchat/ringbuffer.c \
+			drivers/rilmodem/sms.c \
+			unit/rilmodem-test-server.h \
+			unit/rilmodem-test-server.c \
+			src/simutil.c
+unit_test_rilmodem_sms_LDADD = gdbus/libgdbus-internal.la $(builtin_libadd) \
+			@GLIB_LIBS@ @DBUS_LIBS@ -ldl
+unit_objects += $(unit_test_rilmodem_sms_OBJECTS)
+
 TESTS = $(unit_tests)
 
 if TOOLS
Signature · 1 line
2.1.4
#4 lore raw

Re: [PATCH 2/2] build: add support for test-rilmodem-sms

Hi Tony,

Quoted 19 lines · Tony Espy
---
  Makefile.am | 14 +++++++++++++-
  1 file changed, 13 insertions(+), 1 deletion(-)
Makefile.am +2 -1
index fba25ba..1cdfc1f 100644
@@ -740,7 +740,8 @@ unit_objects =
  unit_tests = unit/test-common unit/test-util unit/test-idmap \
  				unit/test-simutil unit/test-stkutil \
  				unit/test-sms unit/test-cdmasms \
-				unit/test-rilmodem-cs
+				unit/test-rilmodem-cs \
+				unit/test-rilmodem-sms
 
  noinst_PROGRAMS = $(unit_tests) \
  			unit/test-sms-root unit/test-mux unit/test-caif

Makefile.am unit_test_rilmodem_cs_LDADD = gdbus/libgdbus-internal.la $(builtin_libadd) \ · line 811

Quoted 10 lines
Makefile.am +7 -0
@@ -801,6 +802,17 @@ unit_test_rilmodem_cs_LDADD = gdbus/libgdbus-internal.la $(builtin_libadd) \
  			@GLIB_LIBS@ @DBUS_LIBS@ -ldl
  unit_objects += $(unit_test_rilmodem_cs_OBJECTS)
 
+unit_test_rilmodem_sms_SOURCES = unit/test-rilmodem-sms.c $(gril_sources) \
+			src/log.c src/common.c src/util.c \
+			gatchat/ringbuffer.h gatchat/ringbuffer.c \
+			drivers/rilmodem/sms.c \
+			unit/rilmodem-test-server.h \
+			unit/rilmodem-test-server.c \
+			src/simutil.c

Can the files common to the ril unit tests be assigned to a variable? I count at least 8 files that are shared between rilmodem_sms & rilmodem_cs. Lets keep duplication to a minimum.

Makefile.am unit_test_rilmodem_cs_LDADD = gdbus/libgdbus-internal.la $(builtin_libadd) \ · line 818

Quoted 8 lines
Makefile.am +4 -0
+unit_test_rilmodem_sms_LDADD = gdbus/libgdbus-internal.la $(builtin_libadd) \
+			@GLIB_LIBS@ @DBUS_LIBS@ -ldl
+unit_objects += $(unit_test_rilmodem_sms_OBJECTS)
+
  TESTS = $(unit_tests)
 
  if TOOLS

Regards,
-Denis