aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2017-04-08 20:52:33 +0200
committerHarald Welte <laforge@gnumonks.org>2017-04-08 21:26:12 +0200
commitff60be3726fc766e27ad7d5890b11405b8c70041 (patch)
treeaa9160f84ffaac2d7f9a1b93c50bfcda9a5d9ac6 /tests
parent18a62b04887d8c6f6d338404330391a66c337b69 (diff)
Add osmo_sock_init2() function, allowing both BIND *and* CONNECTlaforge/sock2
The old osmo_sock_init() function allows only either a bind (for a server socket), or a connect (for a client socket), but not both together. So there's no way to have a client socket that is bound to a specific local IP and/or port, which is needed for some use cases. Change-Id: Idab124bcca47872f55311a82d6818aed590965e6
Diffstat (limited to 'tests')
-rw-r--r--tests/socket/socket_test.c47
-rw-r--r--tests/socket/socket_test.err1
-rw-r--r--tests/socket/socket_test.ok4
3 files changed, 52 insertions, 0 deletions
diff --git a/tests/socket/socket_test.c b/tests/socket/socket_test.c
index 5b6abc42..57425ef5 100644
--- a/tests/socket/socket_test.c
+++ b/tests/socket/socket_test.c
@@ -73,6 +73,52 @@ static int test_sockinit(void)
return 0;
}
+static int test_sockinit2(void)
+{
+ int fd, rc;
+ char *name;
+
+ printf("Checking osmo_sock_init2() with bind to a random local UDP port\n");
+ fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
+ "0.0.0.0", 0, NULL, 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 osmo_sock_init2() for OSMO_SOCK_F_NONBLOCK\n");
+ fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
+ "0.0.0.0", 0, NULL, 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 osmo_sock_init2() for invalid flags\n");
+ fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, "0.0.0.0", 0, NULL, 0, 0);
+ OSMO_ASSERT(fd < 0);
+
+ printf("Checking osmo_sock_init2() for combined BIND + CONNECT\n");
+ fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, "127.0.0.1", 0, "127.0.0.1", 53,
+ OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT);
+ OSMO_ASSERT(fd >= 0);
+ name = osmo_sock_get_name(NULL, fd);
+ OSMO_ASSERT(!strncmp(name, "(127.0.0.1:53<->127.0.0.1", 25));
+ talloc_free(name);
+
+ return 0;
+}
+
+
const struct log_info_cat default_categories[] = {
};
@@ -88,6 +134,7 @@ int main(int argc, char *argv[])
log_set_print_filename(osmo_stderr_target, 0);
test_sockinit();
+ test_sockinit2();
return EXIT_SUCCESS;
}
diff --git a/tests/socket/socket_test.err b/tests/socket/socket_test.err
index 5367239c..ed6e1865 100644
--- a/tests/socket/socket_test.err
+++ b/tests/socket/socket_test.err
@@ -1 +1,2 @@
invalid: both bind and connect flags set: 0.0.0.0:0
+invalid: you have to specify either BIND or CONNECT flags
diff --git a/tests/socket/socket_test.ok b/tests/socket/socket_test.ok
index d6ec40ed..4b24fbce 100644
--- a/tests/socket/socket_test.ok
+++ b/tests/socket/socket_test.ok
@@ -1,3 +1,7 @@
Checking osmo_sock_init() with bind to a random local UDP port
Checking for OSMO_SOCK_F_NONBLOCK
Checking for invalid flags
+Checking osmo_sock_init2() with bind to a random local UDP port
+Checking osmo_sock_init2() for OSMO_SOCK_F_NONBLOCK
+Checking osmo_sock_init2() for invalid flags
+Checking osmo_sock_init2() for combined BIND + CONNECT