tests: Add a test for max_sockets
authorMichael Adam <obnox@samba.org>
Thu, 22 Sep 2016 00:13:20 +0000 (02:13 +0200)
committerAndreas Schneider <asn@samba.org>
Thu, 20 Oct 2016 08:51:59 +0000 (10:51 +0200)
Signed-off-by: Michael Adam <obnox@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
tests/CMakeLists.txt
tests/test_max_sockets.c [new file with mode: 0644]

index 9b5c4bfe64c95b01dc8435028832c620315eeec2..aa5b183dbb4dbf203314b95eb2683c16d4c95d2a 100644 (file)
@@ -33,7 +33,8 @@ set(SWRAP_TESTS
     test_echo_udp_sendto_recvfrom
     test_echo_udp_send_recv
     test_echo_udp_sendmsg_recvmsg
-    test_swrap_unit)
+    test_swrap_unit
+    test_max_sockets)
 
 if (HAVE_STRUCT_MSGHDR_MSG_CONTROL)
     set(SWRAP_TESTS ${SWRAP_TESTS} test_sendmsg_recvmsg_fd)
diff --git a/tests/test_max_sockets.c b/tests/test_max_sockets.c
new file mode 100644 (file)
index 0000000..0aac181
--- /dev/null
@@ -0,0 +1,110 @@
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include "config.h"
+#include "torture.h"
+
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#ifdef HAVE_RPC_RPC_H
+#include <rpc/rpc.h>
+#endif
+
+
+#define MAX_SOCKETS 4
+
+static int setup(void **state)
+{
+       int ret;
+       char str[10];
+
+       torture_setup_socket_dir(state);
+
+       ret = snprintf(str, 10, "%d", MAX_SOCKETS);
+       if (ret < 0) {
+               return ret;
+       }
+
+       ret = setenv("SOCKET_WRAPPER_MAX_SOCKETS", str, 1);
+
+       return 0;
+}
+
+static int teardown(void **state)
+{
+       torture_teardown_socket_dir(state);
+
+       return 0;
+}
+
+static int _socket(int *_s)
+{
+       int s;
+
+       s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+
+       if (_s != NULL) {
+               *_s = s;
+       }
+
+       if (s < 0) {
+               return -1;
+       }
+
+       return 0;
+}
+
+static void test_max_sockets(void **state)
+{
+       int rc;
+       int s[MAX_SOCKETS+1] = { 0 };
+       int i;
+
+       (void) state; /* unused */
+
+       for (i = 0; i < MAX_SOCKETS; i++) {
+               rc = _socket(&s[i]);
+               assert_return_code(rc, errno);
+       }
+
+       /* no free space for sockets left */
+       rc = _socket(&s[MAX_SOCKETS]);
+       assert_int_equal(rc, -1);
+       assert_int_equal(errno, ENOMEM);
+
+       /* closing a socket frees up space */
+       close(s[0]);
+       rc = _socket(&s[0]);
+       assert_return_code(rc, errno);
+
+       /* but just one */
+       rc = _socket(&s[MAX_SOCKETS]);
+       assert_int_equal(rc, -1);
+       assert_int_equal(errno, ENOMEM);
+
+       for (i = 0; i < MAX_SOCKETS; i++) {
+               close(s[i]);
+       }
+}
+
+int main(void) {
+       int rc;
+
+       const struct CMUnitTest max_sockets_tests[] = {
+               cmocka_unit_test_setup_teardown(test_max_sockets,
+                                               setup, teardown),
+       };
+
+       rc = cmocka_run_group_tests(max_sockets_tests, NULL, NULL);
+
+       return rc;
+}