Blame resolv/tst-aton.c

Packit Service 50534c
/* Test legacy IPv4 text-to-address function inet_aton.
Packit Service 50534c
   Copyright (C) 1998-2019 Free Software Foundation, Inc.
Packit Service 50534c
   This file is part of the GNU C Library.
Packit Service 50534c
Packit Service 50534c
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 50534c
   modify it under the terms of the GNU Lesser General Public
Packit Service 50534c
   License as published by the Free Software Foundation; either
Packit Service 50534c
   version 2.1 of the License, or (at your option) any later version.
Packit Service 50534c
Packit Service 50534c
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 50534c
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 50534c
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 50534c
   Lesser General Public License for more details.
Packit Service 50534c
Packit Service 50534c
   You should have received a copy of the GNU Lesser General Public
Packit Service 50534c
   License along with the GNU C Library; if not, see
Packit Service 50534c
   <http://www.gnu.org/licenses/>.  */
Packit Service 50534c
Packit Service 50534c
#include <array_length.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <sys/socket.h>
Packit 6c4009
#include <netinet/in.h>
Packit 6c4009
#include <arpa/inet.h>
Packit 6c4009
Packit Service 50534c
static const struct tests
Packit 6c4009
{
Packit 6c4009
  const char *input;
Packit 6c4009
  int valid;
Packit 6c4009
  uint32_t result;
Packit 6c4009
} tests[] =
Packit 6c4009
{
Packit 6c4009
  { "", 0, 0 },
Packit 6c4009
  { "-1", 0, 0 },
Packit 6c4009
  { "256", 1, 0x00000100 },
Packit 6c4009
  { "256.", 0, 0 },
Packit Service 50534c
  { "255a", 0, 0 },
Packit 6c4009
  { "256a", 0, 0 },
Packit 6c4009
  { "0x100", 1, 0x00000100 },
Packit 6c4009
  { "0200.0x123456", 1, 0x80123456 },
Packit 6c4009
  { "0300.0x89123456.", 0 ,0 },
Packit 6c4009
  { "0100.-0xffff0000", 0, 0 },
Packit 6c4009
  { "0.0xffffff", 1, 0x00ffffff },
Packit 6c4009
  { "0.0x1000000", 0, 0 },
Packit 6c4009
  { "0377.16777215", 1, 0xffffffff },
Packit 6c4009
  { "0377.16777216", 0, 0 },
Packit 6c4009
  { "0x87.077777777", 1, 0x87ffffff },
Packit 6c4009
  { "0x87.0100000000", 0, 0 },
Packit 6c4009
  { "0.1.3", 1, 0x00010003 },
Packit 6c4009
  { "0.256.3", 0, 0 },
Packit 6c4009
  { "256.1.3", 0, 0 },
Packit 6c4009
  { "0.1.0x10000", 0, 0 },
Packit 6c4009
  { "0.1.0xffff", 1, 0x0001ffff },
Packit 6c4009
  { "0.1a.3", 0, 0 },
Packit 6c4009
  { "0.1.a3", 0, 0 },
Packit 6c4009
  { "1.2.3.4", 1, 0x01020304 },
Packit 6c4009
  { "0400.2.3.4", 0, 0 },
Packit 6c4009
  { "1.0x100.3.4", 0, 0 },
Packit 6c4009
  { "1.2.256.4", 0, 0 },
Packit 6c4009
  { "1.2.3.0x100", 0, 0 },
Packit 6c4009
  { "323543357756889", 0, 0 },
Packit Service 50534c
  { "10.1.2.3.4", 0, 0 },
Packit Service 50534c
  { "192.0.2.1", 1, 0xc0000201 },
Packit Service 50534c
  { "192.0.2.2\nX", 1, 0xc0000202 },
Packit Service 50534c
  { "192.0.2.3 Y", 1, 0xc0000203 },
Packit Service 50534c
  { "192.0.2.3Z", 0, 0 },
Packit Service 50534c
  { "192.000.002.010", 1, 0xc0000208 },
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  int result = 0;
Packit 6c4009
  size_t cnt;
Packit 6c4009
Packit Service 50534c
  for (cnt = 0; cnt < array_length (tests); ++cnt)
Packit 6c4009
    {
Packit 6c4009
      struct in_addr addr;
Packit 6c4009
Packit 6c4009
      if ((int) inet_aton (tests[cnt].input, &addr) != tests[cnt].valid)
Packit 6c4009
	{
Packit 6c4009
	  if (tests[cnt].valid)
Packit 6c4009
	    printf ("\"%s\" not seen as valid IP address\n", tests[cnt].input);
Packit 6c4009
	  else
Packit 6c4009
	    printf ("\"%s\" seen as valid IP address\n", tests[cnt].input);
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
      else if (tests[cnt].valid && addr.s_addr != ntohl (tests[cnt].result))
Packit 6c4009
	{
Packit 6c4009
	  printf ("\"%s\" not converted correctly: is %08x, should be %08x\n",
Packit 6c4009
		  tests[cnt].input, addr.s_addr, tests[cnt].result);
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit Service 50534c
#include <support/test-driver.c>