LCOV - code coverage report
Current view: top level - lib/async_req - async_connect_send_test.c (source / functions) Hit Total Coverage
Test: coverage report for support-claim-type-attributes 6b5c566e Lines: 38 59 64.4 %
Date: 2023-11-21 12:31:41 Functions: 1 1 100.0 %

          Line data    Source code
       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 <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           2 : int main(int argc, const char *argv[])
      30             : {
      31             :         int result, listen_sock, status, exit_status;
      32             :         uint16_t port;
      33           2 :         struct sockaddr_in addr = { 0 };
      34             :         pid_t pid;
      35             : 
      36           2 :         listen_sock = socket(PF_INET, SOCK_STREAM, 0);
      37           2 :         if (listen_sock == -1) {
      38           0 :                 perror("socket() failed");
      39           0 :                 exit(1);
      40             :         }
      41             : 
      42           2 :         addr.sin_family = AF_INET;
      43           2 :         addr.sin_addr.s_addr = inet_addr("127.0.0.1");
      44             : 
      45           2 :         for (port = 1024; port < UINT16_MAX; port++) {
      46           2 :                 addr.sin_port = htons(port);
      47           2 :                 result = bind(listen_sock, (struct sockaddr *)&addr, sizeof(addr));
      48           2 :                 if (result == 0) {
      49           2 :                         break;
      50             :                 }
      51             :         }
      52             : 
      53           2 :         if (port == UINT16_MAX) {
      54           0 :                 printf("Huh, no free port?\n");
      55           0 :                 return 1;
      56             :         }
      57             : 
      58           2 :         result = listen(listen_sock, 1);
      59           2 :         if (result == -1) {
      60           0 :                 perror("listen() failed");
      61           0 :                 close(listen_sock);
      62           0 :                 return 1;
      63             :         }
      64             : 
      65           2 :         pid = fork();
      66           4 :         if (pid == -1) {
      67           0 :                 perror("fork");
      68           0 :                 return 1;
      69             :         }
      70             : 
      71           4 :         if (pid == 0) {
      72             :                 struct tevent_context *ev;
      73             :                 struct tevent_req *req;
      74             :                 int fd;
      75             : 
      76           2 :                 ev = tevent_context_init(NULL);
      77           2 :                 if (ev == NULL) {
      78           0 :                         fprintf(stderr, "tevent_context_init failed\n");
      79           0 :                         return 1;
      80             :                 }
      81             : 
      82           2 :                 fd = socket(PF_INET, SOCK_STREAM, 0);
      83           2 :                 if (fd == -1) {
      84           0 :                         perror("socket");
      85           0 :                         return 1;
      86             :                 }
      87             : 
      88           2 :                 memset(&addr, 0, sizeof(addr));
      89           2 :                 addr.sin_family = AF_INET;
      90           2 :                 addr.sin_port = htons(port);
      91           2 :                 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
      92             : 
      93           2 :                 req = async_connect_send(ev, ev, fd,
      94             :                                          (struct sockaddr *)&addr,
      95             :                                          sizeof(struct sockaddr_in),
      96             :                                          NULL, NULL, NULL);
      97             : 
      98           2 :                 if (!tevent_req_poll(req, ev)) {
      99           0 :                         perror("tevent_req_poll() failed");
     100           0 :                         return 1;
     101             :                 }
     102             : 
     103           2 :                 status = 0;
     104           2 :                 result = async_connect_recv(req, &status);
     105           2 :                 if (result != 0) {
     106           0 :                         return status;
     107             :                 }
     108           2 :                 return 0;
     109             :         }
     110             : 
     111           2 :         result = waitpid(pid, &status, 0);
     112           2 :         if (result == -1) {
     113           0 :                 perror("waitpid");
     114           0 :                 return 1;
     115             :         }
     116             : 
     117           2 :         if (!WIFEXITED(status)) {
     118           0 :                 printf("child status: %d\n", status);
     119           0 :                 return 2;
     120             :         }
     121             : 
     122           2 :         exit_status = WEXITSTATUS(status);
     123           2 :         printf("test done: status=%d\n", exit_status);
     124             : 
     125           2 :         if (exit_status != 0) {
     126           0 :                 return exit_status;
     127             :         }
     128             : 
     129           2 :         return 0;
     130             : }

Generated by: LCOV version 1.14