selftest: Fix memset parameters in test for async_connect_send()
[obnox/samba/samba-obnox.git] / lib / async_req / async_connect_send_test.c
1 /*
2  * Test async connect
3  * Copyright (C) Ralph Boehme 2015
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "replace.h"
20 #include "lib/tevent/tevent.h"
21 #include "lib/async_req/async_sock.h"
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <sys/wait.h>
28
29 int main(int argc, const char *argv[])
30 {
31         int result, listen_sock, status, exit_status;
32         uint16_t port;
33         struct sockaddr_in addr = { 0 };
34         pid_t pid;
35
36         listen_sock = socket(PF_INET, SOCK_STREAM, 0);
37         if (listen_sock == -1) {
38                 perror("socket() failed");
39                 exit(1);
40         }
41
42         addr.sin_family = AF_INET;
43         addr.sin_addr.s_addr = inet_addr("127.0.0.1");
44
45         for (port = 1024; port < UINT16_MAX; port++) {
46                 addr.sin_port = htons(port);
47                 result = bind(listen_sock, (struct sockaddr *)&addr, sizeof(addr));
48                 if (result == 0) {
49                         break;
50                 }
51         }
52
53         if (port == UINT16_MAX) {
54                 printf("Huh, no free port?\n");
55                 return 1;
56         }
57
58         result = listen(listen_sock, 1);
59         if (result == -1) {
60                 perror("listen() failed");
61                 close(listen_sock);
62                 return 1;
63         }
64
65         pid = fork();
66         if (pid == -1) {
67                 perror("fork");
68                 return 1;
69         }
70
71         if (pid == 0) {
72                 struct tevent_context *ev;
73                 struct tevent_req *req;
74                 int fd;
75
76                 ev = tevent_context_init(NULL);
77                 if (ev == NULL) {
78                         fprintf(stderr, "tevent_context_init failed\n");
79                         return 1;
80                 }
81
82                 fd = socket(PF_INET, SOCK_STREAM, 0);
83                 if (fd == -1) {
84                         perror("socket");
85                         return 1;
86                 }
87
88                 memset(&addr, 0, sizeof(addr));
89                 addr.sin_family = AF_INET;
90                 addr.sin_port = htons(port);
91                 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
92
93                 req = async_connect_send(ev, ev, fd,
94                                          (struct sockaddr *)&addr,
95                                          sizeof(struct sockaddr_in),
96                                          NULL, NULL, NULL);
97
98                 if (!tevent_req_poll(req, ev)) {
99                         perror("tevent_req_poll() failed");
100                         return 1;
101                 }
102
103                 status = 0;
104                 result = async_connect_recv(req, &status);
105                 if (result != 0) {
106                         return status;
107                 }
108                 return 0;
109         }
110
111         result = waitpid(pid, &status, 0);
112         if (result == -1) {
113                 perror("waitpid");
114                 return 1;
115         }
116
117         if (!WIFEXITED(status)) {
118                 printf("child status: %d\n", status);
119                 return 2;
120         }
121
122         exit_status = WEXITSTATUS(status);
123         printf("test done: status=%d\n", exit_status);
124
125         if (exit_status != 0) {
126                 return exit_status;
127         }
128
129         return 0;
130 }