]> Savannah Git Hosting - gnulib.git/commitdiff
getsockname tests: More tests.
authorBruno Haible <bruno@clisp.org>
Sat, 27 Jan 2018 07:52:31 +0000 (08:52 +0100)
committerBruno Haible <bruno@clisp.org>
Sat, 27 Jan 2018 07:54:33 +0000 (08:54 +0100)
* tests/test-getsockname.c (open_server_socket): New function, mostly
copied from test-poll.c.
(main): Check that getsockname fills in addr.
* modules/getsockname-tests (Depends-on): Add the necessary
dependencies.
(test_getsockname_LDADD): Link with $(INET_PTON_LIB).

ChangeLog
modules/getsockname-tests
tests/test-getsockname.c

index 4994460a022d5f703900de149b23ea3157c8e7f2..70d421982769a713969fced9d3a1d75ccedcc159 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2018-01-27  Bruno Haible  <bruno@clisp.org>
+
+       getsockname tests: More tests.
+       * tests/test-getsockname.c (open_server_socket): New function, mostly
+       copied from test-poll.c.
+       (main): Check that getsockname fills in addr.
+       * modules/getsockname-tests (Depends-on): Add the necessary
+       dependencies.
+       (test_getsockname_LDADD): Link with $(INET_PTON_LIB).
+
 2018-01-26  Paul Eggert  <eggert@cs.ucla.edu>
 
        manywarnings: fix maintainer comment
index 0134e4c50f18a77018f7b6a8bfab697815ad7c41..9141254054dda41c737592cf1d355c6515e1654f 100644 (file)
@@ -5,11 +5,19 @@ tests/macros.h
 
 Depends-on:
 netinet_in
+arpa_inet
+inet_pton
+errno
+perror
 sockets
+socket
+setsockopt
+bind
+listen
 
 configure.ac:
 
 Makefile.am:
 TESTS += test-getsockname
 check_PROGRAMS += test-getsockname
-test_getsockname_LDADD = $(LDADD) @LIBSOCKET@
+test_getsockname_LDADD = $(LDADD) @LIBSOCKET@ $(INET_PTON_LIB)
index 95f22171ef9b34a501b2337e3e3cca339bec2d4a..2c951fb584805d8fc6fa326dd2d6afa75fbd9b97 100644 (file)
 #include "signature.h"
 SIGNATURE_CHECK (getsockname, int, (int, struct sockaddr *, socklen_t *));
 
-#include <errno.h>
+#include <stdio.h>
+#include <string.h>
 #include <netinet/in.h>
+#include <arpa/inet.h>
 #include <unistd.h>
+#include <errno.h>
 
 #include "sockets.h"
 #include "macros.h"
 
+static int
+open_server_socket (void)
+{
+  int s, x;
+  struct sockaddr_in ia;
+
+  s = socket (AF_INET, SOCK_STREAM, 0);
+
+  x = 1;
+  setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
+
+  memset (&ia, 0, sizeof (ia));
+  ia.sin_family = AF_INET;
+  inet_pton (AF_INET, "0.0.0.0", &ia.sin_addr);
+  /* Port 0 means that the system should assign a port.  */
+  ia.sin_port = htons (0);
+  if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
+    {
+      perror ("bind");
+      exit (77);
+    }
+
+  if (listen (s, 1) < 0)
+    {
+      perror ("listen");
+      exit (77);
+    }
+
+  return s;
+}
+
 int
 main (void)
 {
@@ -52,5 +86,17 @@ main (void)
     ASSERT (errno == EBADF);
   }
 
+  /* Test behaviour for a server socket.  */
+  {
+    int s = open_server_socket ();
+    struct sockaddr_in addr;
+    socklen_t addrlen = sizeof (addr);
+
+    memset (&addr, 0, sizeof (addr));
+    ASSERT (getsockname (s, (struct sockaddr *) &addr, &addrlen) == 0);
+    ASSERT (addr.sin_family == AF_INET);
+    ASSERT (ntohs (addr.sin_port) != 0);
+  }
+
   return 0;
 }