1/* Tests for the support_format_dns_packet function.
2 Copyright (C) 2016-2022 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
27static void
28check_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 (s1: actual, s2: expected) != 0)
33 {
34 support_record_failure ();
35 printf (format: "error: formatted packet does not match: %s\n", name);
36 support_run_diff (left_label: "expected", left: expected,
37 right_label: "actual", right: actual);
38 }
39 free (ptr: actual);
40}
41
42static void
43test_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 (buffer: packet, length: sizeof (packet) - 1, name: __func__,
61 expected: "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
66static void
67test_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 (buffer: packet, length: sizeof (packet) - 1, name: __func__,
87 expected: "name: www.example\n"
88 "name: www1.example\n"
89 "name: www2.example\n"
90 "address: 192.0.2.1\n");
91}
92
93static int
94do_test (void)
95{
96 test_aaaa_length ();
97 test_multiple_cnames ();
98 return 0;
99}
100
101#include <support/test-driver.c>
102

source code of glibc/support/tst-support_format_dns_packet.c