Blame inet/tst-inet6_scopeid_pton.c

Packit 6c4009
/* Tests for __inet6_scopeid_pton and IPv6 scopes in getaddrinfo.
Packit 6c4009
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <arpa/inet.h>
Packit 6c4009
#include <inttypes.h>
Packit 6c4009
#include <net-internal.h>
Packit 6c4009
#include <net/if.h>
Packit 6c4009
#include <netdb.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/support.h>
Packit 6c4009
#include <support/test-driver.h>
Packit 6c4009
Packit 6c4009
/* An interface which is known to the system.  */
Packit 6c4009
static const char *interface_name;
Packit 6c4009
static uint32_t interface_index;
Packit 6c4009
Packit 6c4009
/* Initiale the variables above.  */
Packit 6c4009
static void
Packit 6c4009
setup_interface (void)
Packit 6c4009
{
Packit 6c4009
  struct if_nameindex *list = if_nameindex ();
Packit 6c4009
  if (list != NULL && list[0].if_index != 0 && list[0].if_name[0] != '\0')
Packit 6c4009
    {
Packit 6c4009
      interface_name = list[0].if_name;
Packit 6c4009
      interface_index = list[0].if_index;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Convert ADDRESS to struct in6_addr.  */
Packit 6c4009
static struct in6_addr
Packit 6c4009
from_string (const char *address)
Packit 6c4009
{
Packit 6c4009
  struct in6_addr addr;
Packit 6c4009
  if (inet_pton (AF_INET6, address, &addr) != 1)
Packit 6c4009
    FAIL_EXIT1 ("inet_pton (\"%s\")", address);
Packit 6c4009
  return addr;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Invoke getaddrinfo to parse ADDRESS%SCOPE.  Return true if
Packit 6c4009
   getaddrinfo was successful.  */
Packit 6c4009
static bool
Packit 6c4009
call_gai (int family, const char *address, const char *scope,
Packit 6c4009
          struct sockaddr_in6 *result)
Packit 6c4009
{
Packit 6c4009
  struct addrinfo hints =
Packit 6c4009
    {
Packit 6c4009
      .ai_family = family,
Packit 6c4009
      .ai_flags = AI_NUMERICHOST,
Packit 6c4009
      .ai_socktype = SOCK_DGRAM,
Packit 6c4009
      .ai_protocol = IPPROTO_UDP,
Packit 6c4009
    };
Packit 6c4009
  char *fulladdr = xasprintf ("%s%%%s", address, scope);
Packit 6c4009
  struct addrinfo *ai = NULL;
Packit 6c4009
  int ret = getaddrinfo (fulladdr, NULL, &hints, &ai;;
Packit 6c4009
  if (ret == EAI_ADDRFAMILY || ret == EAI_NONAME)
Packit 6c4009
    {
Packit 6c4009
      if (test_verbose > 0)
Packit 6c4009
        printf ("info: getaddrinfo (\"%s\"): %s (%d)\n",
Packit 6c4009
                fulladdr, gai_strerror (ret), ret);
Packit 6c4009
      free (fulladdr);
Packit 6c4009
      return false;
Packit 6c4009
    }
Packit 6c4009
  if (ret != 0)
Packit 6c4009
    FAIL_EXIT1 ("getaddrinfo (\"%s\"): %s (%d)\n",
Packit 6c4009
                fulladdr, gai_strerror (ret), ret);
Packit 6c4009
  TEST_VERIFY_EXIT (ai != NULL);
Packit 6c4009
  TEST_VERIFY_EXIT (ai->ai_addrlen == sizeof (*result));
Packit 6c4009
  TEST_VERIFY (ai->ai_family == AF_INET6);
Packit 6c4009
  TEST_VERIFY (ai->ai_next == NULL);
Packit 6c4009
  memcpy (result, ai->ai_addr, sizeof (*result));
Packit 6c4009
  free (fulladdr);
Packit 6c4009
  freeaddrinfo (ai);
Packit 6c4009
  return true;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Verify that a successful call to getaddrinfo returned the expected
Packit 6c4009
   scope data.  */
Packit 6c4009
static void
Packit 6c4009
check_ai (const char *what, const char *addr_string, const char *scope_string,
Packit 6c4009
          const struct sockaddr_in6 *sa,
Packit 6c4009
          const struct in6_addr *addr, uint32_t scope)
Packit 6c4009
{
Packit 6c4009
  if (memcmp (addr, &sa->sin6_addr, sizeof (*addr)) != 0)
Packit 6c4009
    {
Packit 6c4009
      support_record_failure ();
Packit 6c4009
      printf ("error: getaddrinfo %s address mismatch for %s%%%s\n",
Packit 6c4009
              what, addr_string, scope_string);
Packit 6c4009
    }
Packit 6c4009
  if (sa->sin6_scope_id != scope)
Packit 6c4009
    {
Packit 6c4009
      support_record_failure ();
Packit 6c4009
      printf ("error: getaddrinfo %s scope mismatch for %s%%%s\n"
Packit 6c4009
              "  expected: %" PRIu32 "\n"
Packit 6c4009
              "  actual:   %" PRIu32 "\n",
Packit 6c4009
              what, addr_string, scope_string, scope, sa->sin6_scope_id);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Check a single address were we expected a failure.  */
Packit 6c4009
static void
Packit 6c4009
expect_failure (const char *address, const char *scope)
Packit 6c4009
{
Packit 6c4009
  if (test_verbose > 0)
Packit 6c4009
    printf ("info: expecting failure for %s%%%s\n", address, scope);
Packit 6c4009
  struct in6_addr addr = from_string (address);
Packit 6c4009
  uint32_t result = 1234;
Packit 6c4009
  if (__inet6_scopeid_pton (&addr, scope, &result) == 0)
Packit 6c4009
    {
Packit 6c4009
      support_record_failure ();
Packit 6c4009
      printf ("error: unexpected success for %s%%%s\n",
Packit 6c4009
              address, scope);
Packit 6c4009
    }
Packit 6c4009
  if (result != 1234)
Packit 6c4009
    {
Packit 6c4009
      support_record_failure ();
Packit 6c4009
      printf ("error: unexpected result update for %s%%%s\n",
Packit 6c4009
              address, scope);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  struct sockaddr_in6 sa;
Packit 6c4009
  if (call_gai (AF_UNSPEC, address, scope, &sa))
Packit 6c4009
    {
Packit 6c4009
      support_record_failure ();
Packit 6c4009
      printf ("error: unexpected getaddrinfo success for %s%%%s (AF_UNSPEC)\n",
Packit 6c4009
              address, scope);
Packit 6c4009
    }
Packit 6c4009
  if (call_gai (AF_INET6, address, scope, &sa))
Packit 6c4009
    {
Packit 6c4009
      support_record_failure ();
Packit 6c4009
      printf ("error: unexpected getaddrinfo success for %s%%%s (AF_INET6)\n",
Packit 6c4009
              address, scope);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Check a single address were we expected a success.  */
Packit 6c4009
static void
Packit 6c4009
expect_success (const char *address, const char *scope, uint32_t expected)
Packit 6c4009
{
Packit 6c4009
  if (test_verbose > 0)
Packit 6c4009
    printf ("info: expecting success for %s%%%s\n", address, scope);
Packit 6c4009
  struct in6_addr addr = from_string (address);
Packit 6c4009
  uint32_t actual = expected + 1;
Packit 6c4009
  if (__inet6_scopeid_pton (&addr, scope, &actual) != 0)
Packit 6c4009
    {
Packit 6c4009
      support_record_failure ();
Packit 6c4009
      printf ("error: unexpected failure for %s%%%s\n",
Packit 6c4009
              address, scope);
Packit 6c4009
    }
Packit 6c4009
  if (actual != expected)
Packit 6c4009
    {
Packit 6c4009
      support_record_failure ();
Packit 6c4009
      printf ("error: unexpected result for for %s%%%s\n",
Packit 6c4009
              address, scope);
Packit 6c4009
      printf ("  expected: %" PRIu32 "\n", expected);
Packit 6c4009
      printf ("  actual:   %" PRIu32 "\n", actual);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  struct sockaddr_in6 sa;
Packit 6c4009
  memset (&sa, 0xc0, sizeof (sa));
Packit 6c4009
  if (call_gai (AF_UNSPEC, address, scope, &sa))
Packit 6c4009
    check_ai ("AF_UNSPEC", address, scope, &sa, &addr, expected);
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      support_record_failure ();
Packit 6c4009
      printf ("error: unexpected getaddrinfo failure for %s%%%s (AF_UNSPEC)\n",
Packit 6c4009
              address, scope);
Packit 6c4009
    }
Packit 6c4009
  memset (&sa, 0xc0, sizeof (sa));
Packit 6c4009
  if (call_gai (AF_INET6, address, scope, &sa))
Packit 6c4009
    check_ai ("AF_INET6", address, scope, &sa, &addr, expected);
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      support_record_failure ();
Packit 6c4009
      printf ("error: unexpected getaddrinfo failure for %s%%%s (AF_INET6)\n",
Packit 6c4009
              address, scope);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  setup_interface ();
Packit 6c4009
Packit 6c4009
  static const char *test_addresses[]
Packit 6c4009
    = { "::", "::1", "2001:db8::1", NULL };
Packit 6c4009
  for (int i = 0; test_addresses[i] != NULL; ++i)
Packit 6c4009
    {
Packit 6c4009
      expect_success (test_addresses[i], "0", 0);
Packit 6c4009
      expect_success (test_addresses[i], "5555", 5555);
Packit 6c4009
Packit 6c4009
      expect_failure (test_addresses[i], "");
Packit 6c4009
      expect_failure (test_addresses[i], "-1");
Packit 6c4009
      expect_failure (test_addresses[i], "-99");
Packit 6c4009
      expect_failure (test_addresses[i], "037777777777");
Packit 6c4009
      expect_failure (test_addresses[i], "0x");
Packit 6c4009
      expect_failure (test_addresses[i], "0x1");
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (interface_name != NULL)
Packit 6c4009
    {
Packit 6c4009
      expect_success ("fe80::1", interface_name, interface_index);
Packit 6c4009
      expect_success ("ff02::1", interface_name, interface_index);
Packit 6c4009
      expect_success ("ff01::1", interface_name, interface_index);
Packit 6c4009
      expect_failure ("::", interface_name);
Packit 6c4009
      expect_failure ("::1", interface_name);
Packit 6c4009
      expect_failure ("2001:db8::1", interface_name);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>