LCOV - code coverage report
Current view: top level - src/tds/unittests - addrinfo2str.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 28 30 93.3 %
Date: 2026-11-07 23:58:45 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2026 Frediano Ziglio
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Library General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2 of the License, or (at your option) any later version.
       8             :  *
       9             :  * This library is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Library General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Library General Public
      15             :  * License along with this library; if not, write to the
      16             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      17             :  * Boston, MA 02111-1307, USA.
      18             :  */
      19             : 
      20             : /*
      21             :  * Test tds_addrinfo2str function.
      22             :  */
      23             : #include "common.h"
      24             : 
      25             : static int res = EXIT_SUCCESS;
      26             : 
      27             : static void
      28             : fill_bytes(void *buf, size_t len, uint8_t start)
      29             : {
      30             :         uint8_t *p = (uint8_t *) buf;
      31             : 
      32         200 :         for (; len; --len)
      33         200 :                 *p++ = start++;
      34             : }
      35             : 
      36             : static void
      37          30 : check(struct addrinfo *ai, const char *expected, int line)
      38             : {
      39             :         char buf[128];
      40             :         const char *got;
      41             : 
      42          30 :         memset(buf, 0xab, sizeof(buf));
      43          30 :         got = tds_addrinfo2str(ai, buf, sizeof(buf));
      44          30 :         if (strcmp(got, expected) == 0)
      45          30 :                 return;
      46             : 
      47           0 :         fprintf(stderr, "%d: Wrong result, got '%s' expected '%s'\n", line, got, expected);
      48           0 :         res = EXIT_FAILURE;
      49             : }
      50             : 
      51             : #define check(ai, exp) check(ai, exp, __LINE__)
      52             : 
      53          10 : TEST_MAIN()
      54             : {
      55             :         struct addrinfo ai;
      56             :         struct sockaddr_in sa4;
      57             : 
      58             : #ifdef AF_INET6
      59             :         struct sockaddr_in6 sa6;
      60             : #endif
      61             : 
      62             :         tds_socket_init();
      63             : 
      64          10 :         memset(&ai, 0, sizeof(ai));
      65             : 
      66          10 :         ai.ai_socktype = SOCK_STREAM;
      67             : 
      68             :         /* IPv4 */
      69          10 :         ai.ai_family = AF_INET;
      70          10 :         ai.ai_addr = (struct sockaddr *) &sa4;
      71          10 :         ai.ai_addrlen = sizeof(sa4);
      72          10 :         memset(&sa4, 0xcd, sizeof(sa4));
      73          10 :         sa4.sin_family = AF_INET;
      74          10 :         fill_bytes(&sa4.sin_addr, 4, 1);
      75          10 :         check(&ai, "1.2.3.4");
      76             : 
      77             :         /* IPv6 */
      78             : #ifdef AF_INET6
      79          10 :         ai.ai_family = AF_INET6;
      80          10 :         ai.ai_addr = (struct sockaddr *) &sa6;
      81          10 :         ai.ai_addrlen = sizeof(sa6);
      82          10 :         memset(&sa6, 0xef, sizeof(sa6));
      83          10 :         sa6.sin6_family = AF_INET6;
      84          10 :         fill_bytes(&sa6.sin6_addr, 16, 0x12);
      85          10 :         sa6.sin6_scope_id = 0;
      86          10 :         check(&ai, "1213:1415:1617:1819:1a1b:1c1d:1e1f:2021");
      87             : 
      88          10 :         sa6.sin6_scope_id = 12345678;
      89          10 :         check(&ai, "1213:1415:1617:1819:1a1b:1c1d:1e1f:2021%12345678");
      90             : #endif
      91             : 
      92          10 :         return res;
      93             : }

Generated by: LCOV version 1.13