LCOV - code coverage report
Current view: top level - libsystemd/sd-resolve - test-resolve.c (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 35 46 76.1 %
Date: 2019-08-23 13:36:53 Functions: 3 3 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 17 44 38.6 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: LGPL-2.1+ */
       2                 :            : 
       3                 :            : #include <arpa/inet.h>
       4                 :            : #include <errno.h>
       5                 :            : #include <netinet/in.h>
       6                 :            : #include <resolv.h>
       7                 :            : #include <stdio.h>
       8                 :            : #include <string.h>
       9                 :            : #include <sys/socket.h>
      10                 :            : 
      11                 :            : #include "sd-resolve.h"
      12                 :            : 
      13                 :            : #include "alloc-util.h"
      14                 :            : #include "macro.h"
      15                 :            : #include "socket-util.h"
      16                 :            : #include "string-util.h"
      17                 :            : #include "time-util.h"
      18                 :            : 
      19                 :            : #define TEST_TIMEOUT_USEC (20*USEC_PER_SEC)
      20                 :            : 
      21                 :          8 : static int getaddrinfo_handler(sd_resolve_query *q, int ret, const struct addrinfo *ai, void *userdata) {
      22                 :            :         const struct addrinfo *i;
      23                 :            : 
      24         [ -  + ]:          8 :         assert_se(q);
      25                 :            : 
      26         [ -  + ]:          8 :         if (ret != 0) {
      27         [ #  # ]:          0 :                 log_error("getaddrinfo error: %s %i", gai_strerror(ret), ret);
      28                 :          0 :                 return 0;
      29                 :            :         }
      30                 :            : 
      31         [ +  + ]:         32 :         for (i = ai; i; i = i->ai_next) {
      32                 :         24 :                 _cleanup_free_ char *addr = NULL;
      33                 :            : 
      34         [ -  + ]:         24 :                 assert_se(sockaddr_pretty(i->ai_addr, i->ai_addrlen, false, true, &addr) == 0);
      35                 :         24 :                 puts(addr);
      36                 :            :         }
      37                 :            : 
      38                 :          8 :         printf("canonical name: %s\n", strna(ai->ai_canonname));
      39                 :            : 
      40                 :          8 :         return 0;
      41                 :            : }
      42                 :            : 
      43                 :          4 : static int getnameinfo_handler(sd_resolve_query *q, int ret, const char *host, const char *serv, void *userdata) {
      44         [ -  + ]:          4 :         assert_se(q);
      45                 :            : 
      46         [ -  + ]:          4 :         if (ret != 0) {
      47         [ #  # ]:          0 :                 log_error("getnameinfo error: %s %i", gai_strerror(ret), ret);
      48                 :          0 :                 return 0;
      49                 :            :         }
      50                 :            : 
      51                 :          4 :         printf("Host: %s — Serv: %s\n", strna(host), strna(serv));
      52                 :          4 :         return 0;
      53                 :            : }
      54                 :            : 
      55                 :          4 : int main(int argc, char *argv[]) {
      56                 :          4 :         _cleanup_(sd_resolve_query_unrefp) sd_resolve_query *q1 = NULL, *q2 = NULL;
      57                 :          8 :         _cleanup_(sd_resolve_unrefp) sd_resolve *resolve = NULL;
      58                 :          4 :         int r = 0;
      59                 :            : 
      60                 :          4 :         struct addrinfo hints = {
      61                 :            :                 .ai_family = PF_UNSPEC,
      62                 :            :                 .ai_socktype = SOCK_STREAM,
      63                 :            :                 .ai_flags = AI_CANONNAME
      64                 :            :         };
      65                 :            : 
      66                 :          8 :         struct sockaddr_in sa = {
      67                 :            :                 .sin_family = AF_INET,
      68                 :          4 :                 .sin_port = htons(80)
      69                 :            :         };
      70                 :            : 
      71         [ -  + ]:          4 :         assert_se(sd_resolve_default(&resolve) >= 0);
      72                 :            : 
      73                 :            :         /* Test a floating resolver query */
      74                 :          4 :         r = sd_resolve_getaddrinfo(resolve, NULL, "redhat.com", "http", NULL, getaddrinfo_handler, NULL);
      75         [ -  + ]:          4 :         if (r < 0)
      76         [ #  # ]:          0 :                 log_error_errno(r, "sd_resolve_getaddrinfo(): %m");
      77                 :            : 
      78                 :            :         /* Make a name -> address query */
      79         [ -  + ]:          4 :         r = sd_resolve_getaddrinfo(resolve, &q1, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints, getaddrinfo_handler, NULL);
      80         [ -  + ]:          4 :         if (r < 0)
      81         [ #  # ]:          0 :                 log_error_errno(r, "sd_resolve_getaddrinfo(): %m");
      82                 :            : 
      83                 :            :         /* Make an address -> name query */
      84         [ -  + ]:          4 :         sa.sin_addr.s_addr = inet_addr(argc >= 3 ? argv[2] : "193.99.144.71");
      85                 :          4 :         r = sd_resolve_getnameinfo(resolve, &q2, (struct sockaddr*) &sa, sizeof(sa), 0, SD_RESOLVE_GET_BOTH, getnameinfo_handler, NULL);
      86         [ -  + ]:          4 :         if (r < 0)
      87         [ #  # ]:          0 :                 log_error_errno(r, "sd_resolve_getnameinfo(): %m");
      88                 :            : 
      89                 :            :         /* Wait until all queries are completed */
      90                 :            :         for (;;) {
      91                 :         16 :                 r = sd_resolve_wait(resolve, TEST_TIMEOUT_USEC);
      92         [ +  + ]:         16 :                 if (r == 0)
      93                 :          4 :                         break;
      94         [ -  + ]:         12 :                 if (r == -ETIMEDOUT) {
      95                 :            :                         /* Let's catch timeouts here, so that we can run safely in a CI that has no reliable DNS. Note
      96                 :            :                          * that we invoke exit() directly here, as the stuck NSS call will not allow us to exit
      97                 :            :                          * cleanly. */
      98                 :            : 
      99         [ #  # ]:          0 :                         log_notice_errno(r, "sd_resolve_wait() timed out, but that's OK");
     100                 :          0 :                         exit(EXIT_SUCCESS);
     101                 :            :                 }
     102         [ -  + ]:         12 :                 if (r < 0) {
     103         [ #  # ]:          0 :                         log_error_errno(r, "sd_resolve_wait(): %m");
     104                 :          0 :                         assert_not_reached("sd_resolve_wait() failed");
     105                 :            :                 }
     106                 :            :         }
     107                 :            : 
     108                 :          4 :         return 0;
     109                 :            : }

Generated by: LCOV version 1.14