summaryrefslogtreecommitdiffstats
path: root/tests/socket
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2017-01-27 10:29:49 +0100
committerHarald Welte <laforge@gnumonks.org>2017-02-08 18:23:06 +0100
commit4ffb43f654d12663af0c072eb6191716d377f4b5 (patch)
tree48c98a8efeb3a11f60fd5d3704a8c5e3873edad6 /tests/socket
parentf12d40f4df1d42e4e1ee81607db53cb01dced80d (diff)
Add minimal testing of socket.c helper functions
Change-Id: I2773b3859a206f96fb8fa095d50a653d9eeb8d79
Diffstat (limited to 'tests/socket')
-rw-r--r--tests/socket/socket_test.c78
-rw-r--r--tests/socket/socket_test.err1
-rw-r--r--tests/socket/socket_test.ok3
3 files changed, 82 insertions, 0 deletions
diff --git a/tests/socket/socket_test.c b/tests/socket/socket_test.c
new file mode 100644
index 00000000..75088e5f
--- /dev/null
+++ b/tests/socket/socket_test.c
@@ -0,0 +1,78 @@
+/*
+ * (C) 2017 by Harald Welte <laforge@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. We cannot match on INADDR_ANY,
+ * as apparently that won't work on FreeBSD if there's only one
+ * address (e.g. 127.0.0.1) assigned to the entire system, like
+ * the Osmocom FreeBSD build slaves */
+ OSMO_ASSERT(!strncmp(name, "(NULL<->", 7));
+ 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 00000000..5367239c
--- /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 00000000..d6ec40ed
--- /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