[PATCH] libosmocore[master]: Add minimal testing of socket.c helper functions

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Harald Welte gerrit-no-reply at lists.osmocom.org
Fri Jan 27 22:14:10 UTC 2017


Hello Jenkins Builder,

I'd like you to reexamine a change.  Please visit

    https://gerrit.osmocom.org/1698

to look at the new patch set (#3).

Add minimal testing of socket.c helper functions

Change-Id: I2773b3859a206f96fb8fa095d50a653d9eeb8d79
---
M .gitignore
M tests/Makefile.am
A tests/socket/socket_test.c
A tests/socket/socket_test.err
A tests/socket/socket_test.ok
M tests/testsuite.at
6 files changed, 94 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/98/1698/3

diff --git a/.gitignore b/.gitignore
index fb159c3..ad8354e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -98,6 +98,7 @@
 tests/fsm/fsm_test
 tests/write_queue/wqueue_test
 tests/oap/oap_test
+tests/socket/socket_test
 
 utils/osmo-arfcn
 utils/osmo-auc-gen
diff --git a/tests/Makefile.am b/tests/Makefile.am
index b9eb8f2..5731bf8 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -14,7 +14,7 @@
 		 smscb/gsm0341_test stats/stats_test			\
 		 bitvec/bitvec_test msgb/msgb_test bits/bitcomp_test	\
 		 tlv/tlv_test gsup/gsup_test oap/oap_test fsm/fsm_test	\
-		 write_queue/wqueue_test
+		 write_queue/wqueue_test socket/socket_test
 
 if ENABLE_MSGFILE
 check_PROGRAMS += msgfile/msgfile_test
@@ -140,6 +140,9 @@
 write_queue_wqueue_test_SOURCES = write_queue/wqueue_test.c
 write_queue_wqueue_test_LDADD = $(top_builddir)/src/libosmocore.la
 
+socket_socket_test_SOURCES = socket/socket_test.c
+socket_socket_test_LDADD = $(top_builddir)/src/libosmocore.la
+
 # The `:;' works around a Bash 3.2 bug when the output is not writeable.
 $(srcdir)/package.m4: $(top_srcdir)/configure.ac
 	:;{ \
@@ -176,7 +179,8 @@
 	     bitvec/bitvec_test.ok msgb/msgb_test.ok bits/bitcomp_test.ok \
 	     sim/sim_test.ok tlv/tlv_test.ok gsup/gsup_test.ok		\
 	     oap/oap_test.ok fsm/fsm_test.ok fsm/fsm_test.err		\
-	     write_queue/wqueue_test.ok
+	     write_queue/wqueue_test.ok socket/socket_test.ok		\
+	     socket/socket_test.err
 
 DISTCLEANFILES = atconfig atlocal
 
diff --git a/tests/socket/socket_test.c b/tests/socket/socket_test.c
new file mode 100644
index 0000000..0f5f9d1
--- /dev/null
+++ b/tests/socket/socket_test.c
@@ -0,0 +1,75 @@
+/*
+ * (C) 2017 by Harald Welte <laforge at gnumonks.org>
+ * All Rights Reserved
+ *
+ * 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.
+ *
+ * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <fcntl.h>
+
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+
+#include <osmocom/core/utils.h>
+#include <osmocom/core/socket.h>
+
+#include "../config.h"
+
+static int test_sockinit(void)
+{
+	int fd, rc;
+	char *name;
+
+	printf("Checking osmo_sock_init() with bind to a random local UDP port\n");
+	fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
+			    "0.0.0.0", 0, OSMO_SOCK_F_BIND);
+	OSMO_ASSERT(fd >= 0);
+	name = osmo_sock_get_name(NULL, fd);
+	/* expect it to be not connected, and bound to INADDR_ANY */
+	OSMO_ASSERT(!strncmp(name, "(NULL<->0.0.0.0:", 16));
+	talloc_free(name);
+	/* expect it to be blocking */
+	rc = fcntl(fd, F_GETFL);
+	OSMO_ASSERT(!(rc & O_NONBLOCK));
+	close(fd);
+
+	printf("Checking for OSMO_SOCK_F_NONBLOCK\n");
+	fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
+			    "0.0.0.0", 0, OSMO_SOCK_F_BIND|OSMO_SOCK_F_NONBLOCK);
+	OSMO_ASSERT(fd >= 0);
+	/* expect it to be blocking */
+	rc = fcntl(fd, F_GETFL);
+	OSMO_ASSERT(rc & O_NONBLOCK);
+	close(fd);
+
+	printf("Checking for invalid flags\n");
+	fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
+			    "0.0.0.0", 0, OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT);
+	OSMO_ASSERT(fd < 0);
+
+	return 0;
+}
+
+int main(int argc, char *argv[])
+{
+	test_sockinit();
+	return 0;
+}
diff --git a/tests/socket/socket_test.err b/tests/socket/socket_test.err
new file mode 100644
index 0000000..5367239
--- /dev/null
+++ b/tests/socket/socket_test.err
@@ -0,0 +1 @@
+invalid: both bind and connect flags set: 0.0.0.0:0
diff --git a/tests/socket/socket_test.ok b/tests/socket/socket_test.ok
new file mode 100644
index 0000000..d6ec40e
--- /dev/null
+++ b/tests/socket/socket_test.ok
@@ -0,0 +1,3 @@
+Checking osmo_sock_init() with bind to a random local UDP port
+Checking for OSMO_SOCK_F_NONBLOCK
+Checking for invalid flags
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 426c74c..d6181c9 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -228,3 +228,11 @@
 touch experr
 AT_CHECK([$abs_top_builddir/tests/oap/oap_test], [0], [expout], [experr])
 AT_CLEANUP
+
+AT_SETUP([socket])
+AT_KEYWORDS([socket])
+cat $abs_srcdir/socket/socket_test.ok > expout
+cat $abs_srcdir/socket/socket_test.err > experr
+touch experr
+AT_CHECK([$abs_top_builddir/tests/socket/socket_test], [0], [expout], [experr])
+AT_CLEANUP

-- 
To view, visit https://gerrit.osmocom.org/1698
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newpatchset
Gerrit-Change-Id: I2773b3859a206f96fb8fa095d50a653d9eeb8d79
Gerrit-PatchSet: 3
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder



More information about the gerrit-log mailing list