2e2c56b0d42832a969e82c76d3ec13235ccc0e44
[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   char const *str7; /* Translation of LQ RQ.  */
56 };
57
58 struct result_groups {
59   struct result_strings group1; /* Via quotearg_buffer.  */
60   struct result_strings group2; /* Via quotearg{,_mem}.  */
61   struct result_strings group3; /* Via quotearg_colon{,_mem}.  */
62 };
63
64 /* These quotes are borrowed from a pt_PT.utf8 translation.  */
65 # define LQ "\302\253"
66 # define RQ "\302\273"
67 # define LQ_ENC "\\302\\253"
68 # define RQ_ENC "\\302\\273"
69 # define RQ_ESC "\\\302\273"
70
71 static struct result_strings inputs = {
72   "", "\0001\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b",
73   LQ RQ
74 };
75
76 static struct result_groups results_g[] = {
77   /* literal_quoting_style */
78   { { "", "\0""1\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b",
79       LQ RQ },
80     { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b",
81       LQ RQ },
82     { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b",
83       LQ RQ } },
84
85   /* shell_quoting_style */
86   { { "''", "\0""1\0", 3, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b",
87       "'a\\b'", LQ RQ },
88     { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b",
89       "'a\\b'", LQ RQ },
90     { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
91       "'a\\b'", LQ RQ } },
92
93   /* shell_always_quoting_style */
94   { { "''", "'\0""1\0'", 5, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
95       "'a\\b'", "'" LQ RQ "'" },
96     { "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
97       "'a\\b'", "'" LQ RQ "'" },
98     { "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
99       "'a\\b'", "'" LQ RQ "'" } },
100
101   /* c_quoting_style */
102   { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
103       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"",
104       "\"" LQ_ENC RQ_ENC "\"" },
105     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
106       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"",
107       "\"" LQ_ENC RQ_ENC "\"" },
108     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
109       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"",
110       "\"" LQ_ENC RQ_ENC "\"" } },
111
112   /* c_maybe_quoting_style */
113   { { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
114       "a:b", "a\\b", "\"" LQ_ENC RQ_ENC "\"" },
115     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
116       "a:b", "a\\b", "\"" LQ_ENC RQ_ENC "\"" },
117     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
118       "\"a:b\"", "a\\b", "\"" LQ_ENC RQ_ENC "\"" } },
119
120   /* escape_quoting_style */
121   { { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b",
122       "a\\\\b", LQ_ENC RQ_ENC },
123     { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b",
124       "a\\\\b", LQ_ENC RQ_ENC },
125     { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a\\:b",
126       "a\\\\b", LQ_ENC RQ_ENC } },
127
128   /* locale_quoting_style */
129   { { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
130       "`a:b'", "`a\\\\b'", "`" LQ_ENC RQ_ENC "'" },
131     { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
132       "`a:b'", "`a\\\\b'", "`" LQ_ENC RQ_ENC "'" },
133     { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
134       "`a\\:b'", "`a\\\\b'", "`" LQ_ENC RQ_ENC "'" } },
135
136   /* clocale_quoting_style */
137   { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
138       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"",
139       "\"" LQ_ENC RQ_ENC "\"" },
140     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
141       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"",
142       "\"" LQ_ENC RQ_ENC "\"" },
143     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
144       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"",
145       "\"" LQ_ENC RQ_ENC "\"" } }
146 };
147
148 static struct result_groups flag_results[] = {
149   /* literal_quoting_style and QA_ELIDE_NULL_BYTES */
150   { { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b", LQ RQ },
151     { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b", LQ RQ },
152     { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b", LQ RQ } },
153
154   /* c_quoting_style and QA_ELIDE_OUTER_QUOTES */
155   { { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
156       "a:b", "a\\b", "\"" LQ_ENC RQ_ENC "\"" },
157     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
158       "a:b", "a\\b", "\"" LQ_ENC RQ_ENC "\"" },
159     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
160       "\"a:b\"", "a\\b", "\"" LQ_ENC RQ_ENC "\"" } },
161
162   /* c_quoting_style and QA_SPLIT_TRIGRAPHS */
163   { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
164       "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"", "\"a\\\\b\"",
165       "\"" LQ_ENC RQ_ENC "\"" },
166     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
167       "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"", "\"a\\\\b\"",
168       "\"" LQ_ENC RQ_ENC "\"" },
169     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
170       "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"",
171       "\"" LQ_ENC RQ_ENC "\"" } }
172 };
173
174 #if ENABLE_NLS
175
176 static struct result_groups locale_results[] = {
177   /* locale_quoting_style */
178   { { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
179       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ,
180       LQ LQ RQ_ESC RQ },
181     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
182       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ,
183       LQ LQ RQ_ESC RQ },
184     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
185       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a\\:b" RQ, LQ "a\\\\b" RQ,
186       LQ LQ RQ_ESC RQ } },
187
188   /* clocale_quoting_style */
189   { { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
190       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ,
191       LQ LQ RQ_ESC RQ },
192     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
193       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ,
194       LQ LQ RQ_ESC RQ },
195     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
196       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a\\:b" RQ, LQ "a\\\\b" RQ,
197       LQ LQ RQ_ESC RQ } }
198 };
199
200 #endif /* ENABLE_NLS */
201
202 static void
203 compare (char const *a, size_t la, char const *b, size_t lb)
204 {
205   ASSERT (la == lb);
206   ASSERT (memcmp (a, b, la) == 0);
207   ASSERT (b[lb] == '\0');
208 }
209
210 static void
211 compare_strings (char *(func) (char const *, size_t *),
212                  struct result_strings *results)
213 {
214   size_t len;
215   char *p;
216
217   len = 0;
218   p = func (inputs.str1, &len);
219   compare (results->str1, strlen (results->str1), p, len);
220
221   len = inputs.len2;
222   p = func (inputs.str2, &len);
223   compare (results->str2, results->len2, p, len);
224
225   len = SIZE_MAX;
226   p = func (inputs.str3, &len);
227   compare (results->str3, strlen (results->str3), p, len);
228
229   len = strlen (inputs.str4);
230   p = func (inputs.str4, &len);
231   compare (results->str4, strlen (results->str4), p, len);
232
233   len = SIZE_MAX;
234   p = func (inputs.str5, &len);
235   compare (results->str5, strlen (results->str5), p, len);
236
237   len = strlen (inputs.str6);
238   p = func (inputs.str6, &len);
239   compare (results->str6, strlen (results->str6), p, len);
240
241   len = strlen (inputs.str7);
242   p = func (inputs.str7, &len);
243   compare (results->str7, strlen (results->str7), p, len);
244 }
245
246 static char *
247 use_quotearg_buffer (const char *str, size_t *len)
248 {
249   static char buf[100];
250   size_t size;
251   memset (buf, 0xa5, 100);
252   size = quotearg_buffer (buf, 100, str, *len, NULL);
253   *len = size;
254   ASSERT ((unsigned char) buf[size + 1] == 0xa5);
255   return buf;
256 }
257
258 static char *
259 use_quotearg (const char *str, size_t *len)
260 {
261   char *p = *len == SIZE_MAX ? quotearg (str) : quotearg_mem (str, *len);
262   *len = strlen (p);
263   return p;
264 }
265
266 static char *
267 use_quote_double_quotes (const char *str, size_t *len)
268 {
269   char *p = *len == SIZE_MAX ? quotearg_char (str, '"')
270                                : quotearg_char_mem (str, *len, '"');
271   *len = strlen (p);
272   return p;
273 }
274
275 static char *
276 use_quotearg_colon (const char *str, size_t *len)
277 {
278   char *p = (*len == SIZE_MAX ? quotearg_colon (str)
279              : quotearg_colon_mem (str, *len));
280   *len = strlen (p);
281   return p;
282 }
283
284 int
285 main (int argc, char *argv[])
286 {
287   int i;
288
289   set_program_name (argv[0]);
290
291   /* This program part is hard-wired to the C locale since it does not call
292      setlocale.  */
293   ASSERT (!isprint ('\033'));
294   for (i = literal_quoting_style; i <= clocale_quoting_style; i++)
295     {
296       set_quoting_style (NULL, i);
297       compare_strings (use_quotearg_buffer, &results_g[i].group1);
298       compare_strings (use_quotearg, &results_g[i].group2);
299       if (i == c_quoting_style)
300         compare_strings (use_quote_double_quotes, &results_g[i].group2);
301       compare_strings (use_quotearg_colon, &results_g[i].group3);
302     }
303
304   set_quoting_style (NULL, literal_quoting_style);
305   ASSERT (set_quoting_flags (NULL, QA_ELIDE_NULL_BYTES) == 0);
306   compare_strings (use_quotearg_buffer, &flag_results[0].group1);
307   compare_strings (use_quotearg, &flag_results[0].group2);
308   compare_strings (use_quotearg_colon, &flag_results[0].group3);
309
310   set_quoting_style (NULL, c_quoting_style);
311   ASSERT (set_quoting_flags (NULL, QA_ELIDE_OUTER_QUOTES)
312           == QA_ELIDE_NULL_BYTES);
313   compare_strings (use_quotearg_buffer, &flag_results[1].group1);
314   compare_strings (use_quotearg, &flag_results[1].group2);
315   compare_strings (use_quote_double_quotes, &flag_results[1].group2);
316   compare_strings (use_quotearg_colon, &flag_results[1].group3);
317
318   ASSERT (set_quoting_flags (NULL, QA_SPLIT_TRIGRAPHS)
319           == QA_ELIDE_OUTER_QUOTES);
320   compare_strings (use_quotearg_buffer, &flag_results[2].group1);
321   compare_strings (use_quotearg, &flag_results[2].group2);
322   compare_strings (use_quote_double_quotes, &flag_results[2].group2);
323   compare_strings (use_quotearg_colon, &flag_results[2].group3);
324
325   ASSERT (set_quoting_flags (NULL, 0) == QA_SPLIT_TRIGRAPHS);
326
327 #if ENABLE_NLS
328   /* Clean up environment.  */
329   unsetenv ("LANGUAGE");
330   unsetenv ("LC_ALL");
331   unsetenv ("LC_MESSAGES");
332   unsetenv ("LC_CTYPE");
333   unsetenv ("LANG");
334   unsetenv ("OUTPUT_CHARSET");
335
336   /* This program part runs in a French UTF-8 locale.  It uses
337      the test-quotearg.mo message catalog.  */
338   {
339     const char *locale_name = getenv ("LOCALE");
340
341     if (locale_name != NULL && strcmp (locale_name, "none") != 0
342         && setenv ("LC_ALL", locale_name, 1) == 0
343         && setlocale (LC_ALL, "") != NULL)
344       {
345         textdomain ("test-quotearg");
346         bindtextdomain ("test-quotearg", getenv ("LOCALEDIR"));
347
348         set_quoting_style (NULL, locale_quoting_style);
349         compare_strings (use_quotearg_buffer, &locale_results[0].group1);
350         compare_strings (use_quotearg, &locale_results[0].group2);
351         compare_strings (use_quotearg_colon, &locale_results[0].group3);
352
353         set_quoting_style (NULL, clocale_quoting_style);
354         compare_strings (use_quotearg_buffer, &locale_results[1].group1);
355         compare_strings (use_quotearg, &locale_results[1].group2);
356         compare_strings (use_quotearg_colon, &locale_results[1].group3);
357       }
358   }
359 #endif /* ENABLE_NLS */
360
361   quotearg_free ();
362   return 0;
363 }