Tests for module 'unicase/u32-casefold'.
[gnulib.git] / tests / test-quotearg.c
1 /* Test of quotearg family of functions.
2    Copyright (C) 2008-2009 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Eric Blake <ebb9@byu.net>, 2008.  */
19
20 #include <config.h>
21
22 #include "quotearg.h"
23
24 #include <ctype.h>
25 #include <locale.h>
26 #include <stdbool.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "progname.h"
33 #include "gettext.h"
34
35 #define ASSERT(expr) \
36   do                                                                         \
37     {                                                                        \
38       if (!(expr))                                                           \
39         {                                                                    \
40           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
41           fflush (stderr);                                                   \
42           abort ();                                                          \
43         }                                                                    \
44     }                                                                        \
45   while (0)
46
47 struct result_strings {
48   char const *str1; /* Translation of "".  */
49   char const *str2; /* Translation of "\0""1\0".  */
50   size_t len2; /* Length of str2.  */
51   char const *str3; /* Translation of "simple".  */
52   char const *str4; /* Translation of " \t\n'\"\033?""?/\\".  */
53   char const *str5; /* Translation of "a:b".  */
54   char const *str6; /* Translation of "a\\b".  */
55 };
56
57 struct result_groups {
58   struct result_strings group1; /* Via quotearg_buffer.  */
59   struct result_strings group2; /* Via quotearg{,_mem}.  */
60   struct result_strings group3; /* Via quotearg_colon{,_mem}.  */
61 };
62
63 static struct result_strings inputs = {
64   "", "\0001\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b"
65 };
66
67 static struct result_groups results_g[] = {
68   /* literal_quoting_style */
69   { { "", "\0""1\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" },
70     { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" },
71     { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" } },
72
73   /* shell_quoting_style */
74   { { "''", "\0""1\0", 3, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b",
75       "'a\\b'" },
76     { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b",
77       "'a\\b'" },
78     { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
79       "'a\\b'" } },
80
81   /* shell_always_quoting_style */
82   { { "''", "'\0""1\0'", 5, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
83       "'a\\b'" },
84     { "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
85       "'a\\b'" },
86     { "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
87       "'a\\b'" } },
88
89   /* c_quoting_style */
90   { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
91       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
92     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
93       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
94     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
95       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"" } },
96
97   /* c_maybe_quoting_style */
98   { { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
99       "a:b", "a\\b" },
100     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
101       "a:b", "a\\b" },
102     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
103       "\"a:b\"", "a\\b" } },
104
105   /* escape_quoting_style */
106   { { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b",
107       "a\\\\b" },
108     { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b",
109       "a\\\\b" },
110     { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a\\:b",
111       "a\\\\b" } },
112
113   /* locale_quoting_style */
114   { { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
115       "`a:b'", "`a\\\\b'" },
116     { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
117       "`a:b'", "`a\\\\b'" },
118     { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
119       "`a\\:b'", "`a\\\\b'" } },
120
121   /* clocale_quoting_style */
122   { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
123       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
124     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
125       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
126     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
127       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"" } }
128 };
129
130 static struct result_groups flag_results[] = {
131   /* literal_quoting_style and QA_ELIDE_NULL_BYTES */
132   { { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" },
133     { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" },
134     { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" } },
135
136   /* c_quoting_style and QA_ELIDE_OUTER_QUOTES */
137   { { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
138       "a:b", "a\\b" },
139     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
140       "a:b", "a\\b" },
141     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
142       "\"a:b\"", "a\\b" } },
143
144   /* c_quoting_style and QA_SPLIT_TRIGRAPHS */
145   { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
146       "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
147     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
148       "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
149     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
150       "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"" } }
151 };
152
153 #if ENABLE_NLS
154
155 /* These quotes are borrowed from a pt_PT.utf8 translation.  */
156 # define LQ "\302\253"
157 # define RQ "\302\273"
158
159 static struct result_groups locale_results[] = {
160   /* locale_quoting_style */
161   { { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
162       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ },
163     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
164       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ },
165     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
166       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a\\:b" RQ, LQ "a\\\\b" RQ } },
167
168   /* clocale_quoting_style */
169   { { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
170       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ },
171     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
172       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ },
173     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
174       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a\\:b" RQ, LQ "a\\\\b" RQ } }
175 };
176
177 #endif /* ENABLE_NLS */
178
179 static void
180 compare (char const *a, size_t la, char const *b, size_t lb)
181 {
182   ASSERT (la == lb);
183   ASSERT (memcmp (a, b, la) == 0);
184   ASSERT (b[lb] == '\0');
185 }
186
187 static void
188 compare_strings (char *(func) (char const *, size_t *),
189                  struct result_strings *results)
190 {
191   size_t len;
192   char *p;
193
194   len = 0;
195   p = func (inputs.str1, &len);
196   compare (results->str1, strlen (results->str1), p, len);
197
198   len = inputs.len2;
199   p = func (inputs.str2, &len);
200   compare (results->str2, results->len2, p, len);
201
202   len = SIZE_MAX;
203   p = func (inputs.str3, &len);
204   compare (results->str3, strlen (results->str3), p, len);
205
206   len = strlen (inputs.str4);
207   p = func (inputs.str4, &len);
208   compare (results->str4, strlen (results->str4), p, len);
209
210   len = SIZE_MAX;
211   p = func (inputs.str5, &len);
212   compare (results->str5, strlen (results->str5), p, len);
213
214   len = strlen (inputs.str6);
215   p = func (inputs.str6, &len);
216   compare (results->str6, strlen (results->str6), p, len);
217 }
218
219 static char *
220 use_quotearg_buffer (const char *str, size_t *len)
221 {
222   static char buf[100];
223   size_t size;
224   memset (buf, 0xa5, 100);
225   size = quotearg_buffer (buf, 100, str, *len, NULL);
226   *len = size;
227   ASSERT ((unsigned char) buf[size + 1] == 0xa5);
228   return buf;
229 }
230
231 static char *
232 use_quotearg (const char *str, size_t *len)
233 {
234   char *p = *len == SIZE_MAX ? quotearg (str) : quotearg_mem (str, *len);
235   *len = strlen (p);
236   return p;
237 }
238
239 static char *
240 use_quotearg_colon (const char *str, size_t *len)
241 {
242   char *p = (*len == SIZE_MAX ? quotearg_colon (str)
243              : quotearg_colon_mem (str, *len));
244   *len = strlen (p);
245   return p;
246 }
247
248 int
249 main (int argc, char *argv[])
250 {
251   int i;
252
253   set_program_name (argv[0]);
254
255   /* This program part is hard-wired to the C locale since it does not call
256      setlocale.  */
257   ASSERT (!isprint ('\033'));
258   for (i = literal_quoting_style; i <= clocale_quoting_style; i++)
259     {
260       set_quoting_style (NULL, i);
261       compare_strings (use_quotearg_buffer, &results_g[i].group1);
262       compare_strings (use_quotearg, &results_g[i].group2);
263       compare_strings (use_quotearg_colon, &results_g[i].group3);
264     }
265
266   set_quoting_style (NULL, literal_quoting_style);
267   ASSERT (set_quoting_flags (NULL, QA_ELIDE_NULL_BYTES) == 0);
268   compare_strings (use_quotearg_buffer, &flag_results[0].group1);
269   compare_strings (use_quotearg, &flag_results[0].group2);
270   compare_strings (use_quotearg_colon, &flag_results[0].group3);
271
272   set_quoting_style (NULL, c_quoting_style);
273   ASSERT (set_quoting_flags (NULL, QA_ELIDE_OUTER_QUOTES)
274           == QA_ELIDE_NULL_BYTES);
275   compare_strings (use_quotearg_buffer, &flag_results[1].group1);
276   compare_strings (use_quotearg, &flag_results[1].group2);
277   compare_strings (use_quotearg_colon, &flag_results[1].group3);
278
279   ASSERT (set_quoting_flags (NULL, QA_SPLIT_TRIGRAPHS)
280           == QA_ELIDE_OUTER_QUOTES);
281   compare_strings (use_quotearg_buffer, &flag_results[2].group1);
282   compare_strings (use_quotearg, &flag_results[2].group2);
283   compare_strings (use_quotearg_colon, &flag_results[2].group3);
284
285   ASSERT (set_quoting_flags (NULL, 0) == QA_SPLIT_TRIGRAPHS);
286
287 #if ENABLE_NLS
288   /* Clean up environment.  */
289   unsetenv ("LANGUAGE");
290   unsetenv ("LC_ALL");
291   unsetenv ("LC_MESSAGES");
292   unsetenv ("LC_CTYPE");
293   unsetenv ("LANG");
294   unsetenv ("OUTPUT_CHARSET");
295
296   /* This program part runs in a French UTF-8 locale.  It uses
297      the test-quotearg.mo message catalog.  */
298   {
299     const char *locale_name = getenv ("LOCALE");
300
301     if (locale_name != NULL && strcmp (locale_name, "none") != 0
302         && setenv ("LC_ALL", locale_name, 1) == 0
303         && setlocale (LC_ALL, "") != NULL)
304       {
305         textdomain ("test-quotearg");
306         bindtextdomain ("test-quotearg", getenv ("LOCALEDIR"));
307
308         set_quoting_style (NULL, locale_quoting_style);
309         compare_strings (use_quotearg_buffer, &locale_results[0].group1);
310         compare_strings (use_quotearg, &locale_results[0].group2);
311         compare_strings (use_quotearg_colon, &locale_results[0].group3);
312
313         set_quoting_style (NULL, clocale_quoting_style);
314         compare_strings (use_quotearg_buffer, &locale_results[1].group1);
315         compare_strings (use_quotearg, &locale_results[1].group2);
316         compare_strings (use_quotearg_colon, &locale_results[1].group3);
317       }
318   }
319 #endif /* ENABLE_NLS */
320
321   quotearg_free ();
322   return 0;
323 }