1 /* Test ns_name_compress corner cases.
2    Copyright (C) 2017-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 <resolv.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <support/check.h>
23 #include <support/support.h>
24 
25 /* Check that we can process names which fit into the destination
26    buffer exactly.  See bug 21359.  */
27 static void
test_exact_fit(const char * name,size_t length)28 test_exact_fit (const char *name, size_t length)
29 {
30   unsigned char *buf = xmalloc (length + 1);
31   memset (buf, '$', length + 1);
32   enum { ptr_count = 5 };
33   const unsigned char *dnptrs[ptr_count] = { buf, };
34   int ret = ns_name_compress (name, buf, length,
35                           dnptrs, dnptrs + ptr_count);
36   if (ret < 0)
37     {
38       support_record_failure ();
39       printf ("error: ns_name_compress for %s/%zu failed\n", name, length);
40       return;
41     }
42   if ((size_t) ret != length)
43     {
44       support_record_failure ();
45       printf ("error: ns_name_compress for %s/%zu result mismatch: %d\n",
46               name, length, ret);
47     }
48   if (buf[length] != '$')
49     {
50       support_record_failure ();
51       printf ("error: ns_name_compress for %s/%zu padding write\n",
52               name, length);
53     }
54   free (buf);
55 }
56 
57 static int
do_test(void)58 do_test (void)
59 {
60   test_exact_fit ("abc", 5);
61   test_exact_fit ("abc.", 5);
62   {
63     char long_name[]
64       = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
65       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
66       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
67       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.";
68     TEST_VERIFY (strlen (long_name) == NS_MAXCDNAME - 1);
69     test_exact_fit (long_name, NS_MAXCDNAME);
70     long_name[sizeof (long_name) - 1] = '\0';
71     test_exact_fit (long_name, NS_MAXCDNAME);
72   }
73   return 0;
74 }
75 
76 #include <support/test-driver.c>
77