summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2014-06-29 19:24:56 -0400
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2014-07-15 22:31:40 -0400
commitb31f535c9a7af9b5f5c4f5d4e52be9159af72127 (patch)
tree6b5dfd1c64fe8c04123ecd5688d82d1486a6581b /src/test
parentad95fd1d2b9c6344864857c2ba7634fd87753f8e (diff)
shared/socket-util: add function to query remote address
Diffstat (limited to 'src/test')
-rw-r--r--src/test/test-socket-util.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/test/test-socket-util.c b/src/test/test-socket-util.c
index ed183e8036..96812e5058 100644
--- a/src/test/test-socket-util.c
+++ b/src/test/test-socket-util.c
@@ -22,6 +22,7 @@
#include "util.h"
#include "macro.h"
#include "log.h"
+#include "async.h"
static void test_socket_address_parse(void) {
SocketAddress a;
@@ -212,6 +213,58 @@ static void test_in_addr_prefix_next(void) {
}
+static void *connect_thread(void *arg) {
+ union sockaddr_union *sa = arg;
+ _cleanup_close_ int fd = -1;
+
+ fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
+ assert(fd >= 0);
+
+ assert_se(connect(fd, &sa->sa, sizeof(sa->in)) == 0);
+
+ return NULL;
+}
+
+static void test_nameinfo_pretty(void) {
+ _cleanup_free_ char *stdin = NULL, *localhost = NULL;
+
+ union sockaddr_union s = {
+ .in.sin_family = AF_INET,
+ .in.sin_port = 0,
+ .in.sin_addr.s_addr = htonl(INADDR_ANY),
+ };
+ int r;
+
+ union sockaddr_union c = {};
+ socklen_t slen = sizeof(c.in), clen = sizeof(c.in);
+
+ _cleanup_close_ int sfd = -1, cfd = -1, afd = -1;
+ r = getnameinfo_pretty(STDIN_FILENO, &stdin);
+ log_info("No connection remote: %s", strerror(-r));
+
+ assert_se(r < 0);
+
+ sfd = socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
+ assert(sfd >= 0);
+
+ assert_se(bind(sfd, &s.sa, sizeof(s.in)) == 0);
+
+ /* find out the port number */
+ assert_se(getsockname(sfd, &s.sa, &slen) == 0);
+
+ assert_se(listen(sfd, 1) == 0);
+
+ assert_se(asynchronous_job(connect_thread, &s) == 0);
+
+ log_debug("Accepting new connection on fd:%d", sfd);
+ cfd = accept4(sfd, &c.sa, &clen, SOCK_CLOEXEC);
+ assert(cfd >= 0);
+
+ r = getnameinfo_pretty(cfd, &localhost);
+ log_info("Connection from %s", localhost);
+ assert(r == 0);
+}
+
int main(int argc, char *argv[]) {
log_set_max_level(LOG_DEBUG);
@@ -224,5 +277,7 @@ int main(int argc, char *argv[]) {
test_in_addr_prefix_intersect();
test_in_addr_prefix_next();
+ test_nameinfo_pretty();
+
return 0;
}