(root)/
glibc-2.38/
support/
tst-support_format_dns_packet.c
       1  /* Tests for the support_format_dns_packet function.
       2     Copyright (C) 2016-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 <support/check.h>
      20  #include <support/format_nss.h>
      21  #include <support/run_diff.h>
      22  
      23  #include <stdio.h>
      24  #include <stdlib.h>
      25  #include <string.h>
      26  
      27  static void
      28  check_packet (const void *buffer, size_t length,
      29                const char *name, const char *expected)
      30  {
      31    char *actual = support_format_dns_packet (buffer, length);
      32    if (strcmp (actual, expected) != 0)
      33      {
      34        support_record_failure ();
      35        printf ("error: formatted packet does not match: %s\n", name);
      36        support_run_diff ("expected", expected,
      37                          "actual", actual);
      38      }
      39    free (actual);
      40  }
      41  
      42  static void
      43  test_aaaa_length (void)
      44  {
      45    static const char packet[] =
      46      /* Header: Response with two records.  */
      47      "\x12\x34\x80\x00\x00\x01\x00\x02\x00\x00\x00\x00"
      48      /* Question section.  www.example/IN/AAAA.  */
      49      "\x03www\x07""example\x00\x00\x1c\x00\x01"
      50      /* Answer section.  www.example AAAA [corrupted].  */
      51      "\xc0\x0c"
      52      "\x00\x1c\x00\x01\x00\x00\x00\x00\x00\x10"
      53      "\x20\x01\x0d\xb8\x05\x06\x07\x08"
      54      "\x11\x12\x13\x14\x15\x16\x17\x18"
      55      /* www.example AAAA [corrupted].  */
      56      "\xc0\x0c"
      57      "\x00\x1c\x00\x01\x00\x00\x00\x00\x00\x11"
      58      "\x01\x02\x03\x04\x05\x06\x07\x08"
      59      "\x11\x12\x13\x14\x15\x16\x17\x18" "\xff";
      60    check_packet (packet, sizeof (packet) - 1, __func__,
      61                  "name: www.example\n"
      62                  "address: 2001:db8:506:708:1112:1314:1516:1718\n"
      63                  "error: AAAA record of size 17: www.example\n");
      64  }
      65  
      66  static void
      67  test_multiple_cnames (void)
      68  {
      69    static const char packet[] =
      70      /* Header: Response with three records.  */
      71      "\x12\x34\x80\x00\x00\x01\x00\x03\x00\x00\x00\x00"
      72      /* Question section.  www.example/IN/A.  */
      73      "\x03www\x07""example\x00\x00\x01\x00\x01"
      74      /* Answer section.  www.example CNAME www1.example.  */
      75      "\xc0\x0c"
      76      "\x00\x05\x00\x01\x00\x00\x00\x00\x00\x07"
      77      "\x04www1\xc0\x10"
      78      /* www1 CNAME www2.  */
      79      "\x04www1\xc0\x10"
      80      "\x00\x05\x00\x01\x00\x00\x00\x00\x00\x07"
      81      "\x04www2\xc0\x10"
      82      /* www2 A 192.0.2.1.  */
      83      "\x04www2\xc0\x10"
      84      "\x00\x01\x00\x01\x00\x00\x00\x00\x00\x04"
      85      "\xc0\x00\x02\x01";
      86    check_packet (packet, sizeof (packet) - 1, __func__,
      87                  "name: www.example\n"
      88                  "data: www.example CNAME www1.example\n"
      89                  "data: www1.example CNAME www2.example\n"
      90                  "address: 192.0.2.1\n");
      91  }
      92  
      93  static int
      94  do_test (void)
      95  {
      96    test_aaaa_length ();
      97    test_multiple_cnames ();
      98    return 0;
      99  }
     100  
     101  #include <support/test-driver.c>