(root)/
glibc-2.38/
resolv/
tst-aton.c
       1  /* Test legacy IPv4 text-to-address function inet_aton.
       2     Copyright (C) 1998-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library 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 GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <array_length.h>
      20  #include <stdio.h>
      21  #include <stdint.h>
      22  #include <sys/socket.h>
      23  #include <netinet/in.h>
      24  #include <arpa/inet.h>
      25  
      26  static const struct tests
      27  {
      28    const char *input;
      29    int valid;
      30    uint32_t result;
      31  } tests[] =
      32  {
      33    { "", 0, 0 },
      34    { "-1", 0, 0 },
      35    { "256", 1, 0x00000100 },
      36    { "256.", 0, 0 },
      37    { "255a", 0, 0 },
      38    { "256a", 0, 0 },
      39    { "0x100", 1, 0x00000100 },
      40    { "0200.0x123456", 1, 0x80123456 },
      41    { "0300.0x89123456.", 0 ,0 },
      42    { "0100.-0xffff0000", 0, 0 },
      43    { "0.0xffffff", 1, 0x00ffffff },
      44    { "0.0x1000000", 0, 0 },
      45    { "0377.16777215", 1, 0xffffffff },
      46    { "0377.16777216", 0, 0 },
      47    { "0x87.077777777", 1, 0x87ffffff },
      48    { "0x87.0100000000", 0, 0 },
      49    { "0.1.3", 1, 0x00010003 },
      50    { "0.256.3", 0, 0 },
      51    { "256.1.3", 0, 0 },
      52    { "0.1.0x10000", 0, 0 },
      53    { "0.1.0xffff", 1, 0x0001ffff },
      54    { "0.1a.3", 0, 0 },
      55    { "0.1.a3", 0, 0 },
      56    { "1.2.3.4", 1, 0x01020304 },
      57    { "0400.2.3.4", 0, 0 },
      58    { "1.0x100.3.4", 0, 0 },
      59    { "1.2.256.4", 0, 0 },
      60    { "1.2.3.0x100", 0, 0 },
      61    { "323543357756889", 0, 0 },
      62    { "10.1.2.3.4", 0, 0 },
      63    { "192.0.2.1", 1, 0xc0000201 },
      64    { "192.0.2.2\nX", 1, 0xc0000202 },
      65    { "192.0.2.3 Y", 1, 0xc0000203 },
      66    { "192.0.2.3Z", 0, 0 },
      67    { "192.000.002.010", 1, 0xc0000208 },
      68  };
      69  
      70  
      71  static int
      72  do_test (void)
      73  {
      74    int result = 0;
      75    size_t cnt;
      76  
      77    for (cnt = 0; cnt < array_length (tests); ++cnt)
      78      {
      79        struct in_addr addr;
      80  
      81        if ((int) inet_aton (tests[cnt].input, &addr) != tests[cnt].valid)
      82  	{
      83  	  if (tests[cnt].valid)
      84  	    printf ("\"%s\" not seen as valid IP address\n", tests[cnt].input);
      85  	  else
      86  	    printf ("\"%s\" seen as valid IP address\n", tests[cnt].input);
      87  	  result = 1;
      88  	}
      89        else if (tests[cnt].valid && addr.s_addr != ntohl (tests[cnt].result))
      90  	{
      91  	  printf ("\"%s\" not converted correctly: is %08x, should be %08x\n",
      92  		  tests[cnt].input, addr.s_addr, tests[cnt].result);
      93  	  result = 1;
      94  	}
      95      }
      96  
      97    return result;
      98  }
      99  
     100  #include <support/test-driver.c>