1 /* Test wide output conversion for warn.
2    Copyright (C) 2018-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 <err.h>
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <support/check.h>
25 #include <support/xmemstream.h>
26 #include <wchar.h>
27 
28 /* Used to trigger the large-string path in __fxprintf.  */
29 #define PADDING \
30   "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
31   "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
32   "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
33 
34 #define LPADDING \
35   L"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"  \
36   "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
37   "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
38 
39 
40 static void
one_test(const char * message,int error_code,const wchar_t * expected)41 one_test (const char *message, int error_code, const wchar_t *expected)
42 {
43   wchar_t *buffer = NULL;
44   size_t length = 0;
45   FILE *fp = open_wmemstream (&buffer, &length);
46   TEST_VERIFY_EXIT (fp != NULL);
47   FILE *old_stderr = stderr;
48   stderr = fp;
49   errno = error_code;
50   switch (error_code)
51     {
52     case E2BIG:
53       warn ("%s with padding " PADDING, message);
54       break;
55     case EAGAIN:
56       warn ("%s", message);
57       break;
58     case -1:
59       warnx ("%s", message);
60       break;
61     case -2:
62       warnx ("%s with padding " PADDING, message);
63       break;
64     }
65   stderr = old_stderr;
66   TEST_VERIFY_EXIT (!ferror (fp));
67   TEST_COMPARE (fclose (fp), 0);
68   if (wcscmp (buffer, expected) != 0)
69     FAIL_EXIT1 ("unexpected output: %ls", buffer);
70   free (buffer);
71 }
72 
73 static int
do_test(void)74 do_test (void)
75 {
76   one_test ("no errno", -1,
77             L"tst-warn-wide: no errno\n");
78   one_test ("no errno", -2,
79             L"tst-warn-wide: no errno with padding " PADDING "\n");
80   one_test ("with errno", EAGAIN,
81             L"tst-warn-wide: with errno: Resource temporarily unavailable\n");
82   one_test ("with errno", E2BIG,
83             L"tst-warn-wide: with errno with padding " PADDING
84             ": Argument list too long\n");
85   return 0;
86 }
87 
88 #include <support/test-driver.c>
89