selftest: add a test for async_connect_send()
authorRalph Boehme <slow@samba.org>
Sun, 18 Oct 2015 20:23:20 +0000 (22:23 +0200)
committerJeremy Allison <jra@samba.org>
Tue, 20 Oct 2015 18:22:22 +0000 (20:22 +0200)
Bug: https://bugzilla.samba.org/show_bug.cgi?id=11564

Signed-off-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/async_req/async_connect_send_test.c [new file with mode: 0644]
lib/async_req/wscript_build
source3/script/tests/test_async_req.sh [new file with mode: 0755]
source3/selftest/tests.py

diff --git a/lib/async_req/async_connect_send_test.c b/lib/async_req/async_connect_send_test.c
new file mode 100644 (file)
index 0000000..e612056
--- /dev/null
@@ -0,0 +1,130 @@
+/*
+ * Test async connect
+ * Copyright (C) Ralph Boehme 2015
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "replace.h"
+#include "lib/tevent/tevent.h"
+#include "lib/async_req/async_sock.h"
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+int main(int argc, const char *argv[])
+{
+       int result, listen_sock, status, exit_status;
+       uint16_t port;
+       struct sockaddr_in addr = { 0 };
+       pid_t pid;
+
+       listen_sock = socket(PF_INET, SOCK_STREAM, 0);
+       if (listen_sock == -1) {
+               perror("socket() failed");
+               exit(1);
+       }
+
+       addr.sin_family = AF_INET;
+       addr.sin_addr.s_addr = inet_addr("127.0.0.1");
+
+       for (port = 1024; port < UINT16_MAX; port++) {
+               addr.sin_port = htons(port);
+               result = bind(listen_sock, (struct sockaddr *)&addr, sizeof(addr));
+               if (result == 0) {
+                       break;
+               }
+       }
+
+       if (port == UINT16_MAX) {
+               printf("Huh, no free port?\n");
+               return 1;
+       }
+
+       result = listen(listen_sock, 1);
+       if (result == -1) {
+               perror("listen() failed");
+               close(listen_sock);
+               return 1;
+       }
+
+       pid = fork();
+       if (pid == -1) {
+               perror("fork");
+               return 1;
+       }
+
+       if (pid == 0) {
+               struct tevent_context *ev;
+               struct tevent_req *req;
+               int fd;
+
+               ev = tevent_context_init(NULL);
+               if (ev == NULL) {
+                       fprintf(stderr, "tevent_context_init failed\n");
+                       return 1;
+               }
+
+               fd = socket(PF_INET, SOCK_STREAM, 0);
+               if (fd == -1) {
+                       perror("socket");
+                       return 1;
+               }
+
+               memset(&addr, sizeof(addr), 0);
+               addr.sin_family = AF_INET;
+               addr.sin_port = htons(port);
+               addr.sin_addr.s_addr = inet_addr("127.0.0.1");
+
+               req = async_connect_send(ev, ev, fd,
+                                        (struct sockaddr *)&addr,
+                                        sizeof(struct sockaddr_in),
+                                        NULL, NULL, NULL);
+
+               if (!tevent_req_poll(req, ev)) {
+                       perror("tevent_req_poll() failed");
+                       return 1;
+               }
+
+               status = 0;
+               result = async_connect_recv(req, &status);
+               if (result != 0) {
+                       return status;
+               }
+               return 0;
+       }
+
+       result = waitpid(pid, &status, 0);
+       if (result == -1) {
+               perror("waitpid");
+               return 1;
+       }
+
+       if (!WIFEXITED(status)) {
+               printf("child status: %d\n", status);
+               return 2;
+       }
+
+       exit_status = WEXITSTATUS(status);
+       printf("test done: status=%d\n", exit_status);
+
+       if (exit_status != 0) {
+               return exit_status;
+       }
+
+       return 0;
+}
index e8af569b4f9d619e3ff00faa3d6f881a67801382..9c2522376db3bb03df014c391655289292870e37 100644 (file)
@@ -7,3 +7,7 @@ bld.SAMBA_SUBSYSTEM('LIBASYNC_REQ',
        deps='tevent-util socket-blocking'
        )
 
+bld.SAMBA_BINARY('async_connect_send_test',
+                 source='async_connect_send_test.c',
+                 deps='LIBASYNC_REQ'
+)
diff --git a/source3/script/tests/test_async_req.sh b/source3/script/tests/test_async_req.sh
new file mode 100755 (executable)
index 0000000..a92f990
--- /dev/null
@@ -0,0 +1,11 @@
+#!/bin/sh
+
+incdir=`dirname $0`/../../../testprogs/blackbox
+. $incdir/subunit.sh
+
+failed=0
+
+testit "async_connect_send" $VALGRIND $BINDIR/async_connect_send_test ||
+       failed=`expr $failed + 1`
+
+testok $0 $failed
index ecbb368bfcef1c39f25bfac44007e0935e8f043d..4f6c12366e69abda058f005a1e2debdf9ed59a5f 100755 (executable)
@@ -249,6 +249,9 @@ plantestsuite(
     "samba3.pthreadpool", "nt4_dc",
     [os.path.join(samba3srcdir, "script/tests/test_pthreadpool.sh")])
 
+plantestsuite("samba3.async_req", "nt4_dc",
+              [os.path.join(samba3srcdir, "script/tests/test_async_req.sh")])
+
 #smbtorture4 tests
 
 base = ["base.attr", "base.charset", "base.chkpath", "base.defer_open", "base.delaywrite", "base.delete",