Blame support/tst-support_format_dns_packet.c

Packit 6c4009
/* Tests for the support_format_dns_packet function.
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 <support/check.h>
Packit 6c4009
#include <support/format_nss.h>
Packit 6c4009
#include <support/run_diff.h>
Packit 6c4009
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
check_packet (const void *buffer, size_t length,
Packit 6c4009
              const char *name, const char *expected)
Packit 6c4009
{
Packit 6c4009
  char *actual = support_format_dns_packet (buffer, length);
Packit 6c4009
  if (strcmp (actual, expected) != 0)
Packit 6c4009
    {
Packit 6c4009
      support_record_failure ();
Packit 6c4009
      printf ("error: formatted packet does not match: %s\n", name);
Packit 6c4009
      support_run_diff ("expected", expected,
Packit 6c4009
                        "actual", actual);
Packit 6c4009
    }
Packit 6c4009
  free (actual);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
test_aaaa_length (void)
Packit 6c4009
{
Packit 6c4009
  static const char packet[] =
Packit 6c4009
    /* Header: Response with two records.  */
Packit 6c4009
    "\x12\x34\x80\x00\x00\x01\x00\x02\x00\x00\x00\x00"
Packit 6c4009
    /* Question section.  www.example/IN/AAAA.  */
Packit 6c4009
    "\x03www\x07""example\x00\x00\x1c\x00\x01"
Packit 6c4009
    /* Answer section.  www.example AAAA [corrupted].  */
Packit 6c4009
    "\xc0\x0c"
Packit 6c4009
    "\x00\x1c\x00\x01\x00\x00\x00\x00\x00\x10"
Packit 6c4009
    "\x20\x01\x0d\xb8\x05\x06\x07\x08"
Packit 6c4009
    "\x11\x12\x13\x14\x15\x16\x17\x18"
Packit 6c4009
    /* www.example AAAA [corrupted].  */
Packit 6c4009
    "\xc0\x0c"
Packit 6c4009
    "\x00\x1c\x00\x01\x00\x00\x00\x00\x00\x11"
Packit 6c4009
    "\x01\x02\x03\x04\x05\x06\x07\x08"
Packit 6c4009
    "\x11\x12\x13\x14\x15\x16\x17\x18" "\xff";
Packit 6c4009
  check_packet (packet, sizeof (packet) - 1, __func__,
Packit 6c4009
                "name: www.example\n"
Packit 6c4009
                "address: 2001:db8:506:708:1112:1314:1516:1718\n"
Packit 6c4009
                "error: AAAA record of size 17: www.example\n");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
test_multiple_cnames (void)
Packit 6c4009
{
Packit 6c4009
  static const char packet[] =
Packit 6c4009
    /* Header: Response with three records.  */
Packit 6c4009
    "\x12\x34\x80\x00\x00\x01\x00\x03\x00\x00\x00\x00"
Packit 6c4009
    /* Question section.  www.example/IN/A.  */
Packit 6c4009
    "\x03www\x07""example\x00\x00\x01\x00\x01"
Packit 6c4009
    /* Answer section.  www.example CNAME www1.example.  */
Packit 6c4009
    "\xc0\x0c"
Packit 6c4009
    "\x00\x05\x00\x01\x00\x00\x00\x00\x00\x07"
Packit 6c4009
    "\x04www1\xc0\x10"
Packit 6c4009
    /* www1 CNAME www2.  */
Packit 6c4009
    "\x04www1\xc0\x10"
Packit 6c4009
    "\x00\x05\x00\x01\x00\x00\x00\x00\x00\x07"
Packit 6c4009
    "\x04www2\xc0\x10"
Packit 6c4009
    /* www2 A 192.0.2.1.  */
Packit 6c4009
    "\x04www2\xc0\x10"
Packit 6c4009
    "\x00\x01\x00\x01\x00\x00\x00\x00\x00\x04"
Packit 6c4009
    "\xc0\x00\x02\x01";
Packit 6c4009
  check_packet (packet, sizeof (packet) - 1, __func__,
Packit 6c4009
                "name: www.example\n"
Packit 6c4009
                "name: www1.example\n"
Packit 6c4009
                "name: www2.example\n"
Packit 6c4009
                "address: 192.0.2.1\n");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  test_aaaa_length ();
Packit 6c4009
  test_multiple_cnames ();
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>