1 /* Test of quotearg family of functions.
2 Copyright (C) 2008-2009 Free Software Foundation, Inc.
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)
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.
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. */
18 /* Written by Eric Blake <ebb9@byu.net>, 2008. */
35 #define ASSERT(expr) \
40 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
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. */
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}. */
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"
71 static struct result_strings inputs = {
72 "", "\0001\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b",
76 static struct result_groups results_g[] = {
77 /* literal_quoting_style */
78 { { "", "\0""1\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b",
80 { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b",
82 { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b",
85 /* shell_quoting_style */
86 { { "''", "\0""1\0", 3, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b",
88 { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b",
90 { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
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 "'" } },
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 "\"" } },
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 "\"" } },
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 } },
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 "'" } },
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 "\"" } }
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 } },
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 "\"" } },
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 "\"" } }
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,
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,
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,
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,
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,
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,
200 #endif /* ENABLE_NLS */
202 static char const *custom_quotes[][2] = {
212 static struct result_groups custom_results[] = {
213 /* left_quote = right_quote = "" */
214 { { "", "\\0001\\0", 7, "simple",
215 " \\t\\n'\"\\033?""?/\\\\", "a:b", "a\\\\b",
217 { "", "\\0001\\0", 7, "simple",
218 " \\t\\n'\"\\033?""?/\\\\", "a:b", "a\\\\b",
220 { "", "\\0001\\0", 7, "simple",
221 " \\t\\n'\"\\033?""?/\\\\", "a\\:b", "a\\\\b",
224 /* left_quote = right_quote = "'" */
225 { { "''", "'\\0001\\0'", 9, "'simple'",
226 "' \\t\\n\\'\"\\033?""?/\\\\'", "'a:b'", "'a\\\\b'",
227 "'" LQ_ENC RQ_ENC "'" },
228 { "''", "'\\0001\\0'", 9, "'simple'",
229 "' \\t\\n\\'\"\\033?""?/\\\\'", "'a:b'", "'a\\\\b'",
230 "'" LQ_ENC RQ_ENC "'" },
231 { "''", "'\\0001\\0'", 9, "'simple'",
232 "' \\t\\n\\'\"\\033?""?/\\\\'", "'a\\:b'", "'a\\\\b'",
233 "'" LQ_ENC RQ_ENC "'" } },
235 /* left_quote = "(" and right_quote = ")" */
236 { { "()", "(\\0001\\0)", 9, "(simple)",
237 "( \\t\\n'\"\\033?""?/\\\\)", "(a:b)", "(a\\\\b)",
238 "(" LQ_ENC RQ_ENC ")" },
239 { "()", "(\\0001\\0)", 9, "(simple)",
240 "( \\t\\n'\"\\033?""?/\\\\)", "(a:b)", "(a\\\\b)",
241 "(" LQ_ENC RQ_ENC ")" },
242 { "()", "(\\0001\\0)", 9, "(simple)",
243 "( \\t\\n'\"\\033?""?/\\\\)", "(a\\:b)", "(a\\\\b)",
244 "(" LQ_ENC RQ_ENC ")" } },
246 /* left_quote = ":" and right_quote = " " */
247 { { ": ", ":\\0001\\0 ", 9, ":simple ",
248 ":\\ \\t\\n'\"\\033?""?/\\\\ ", ":a:b ", ":a\\\\b ",
249 ":" LQ_ENC RQ_ENC " " },
250 { ": ", ":\\0001\\0 ", 9, ":simple ",
251 ":\\ \\t\\n'\"\\033?""?/\\\\ ", ":a:b ", ":a\\\\b ",
252 ":" LQ_ENC RQ_ENC " " },
253 { ": ", ":\\0001\\0 ", 9, ":simple ",
254 ":\\ \\t\\n'\"\\033?""?/\\\\ ", ":a\\:b ", ":a\\\\b ",
255 ":" LQ_ENC RQ_ENC " " } },
257 /* left_quote = " " and right_quote = ":" */
258 { { " :", " \\0001\\0:", 9, " simple:",
259 " \\t\\n'\"\\033?""?/\\\\:", " a\\:b:", " a\\\\b:",
260 " " LQ_ENC RQ_ENC ":" },
261 { " :", " \\0001\\0:", 9, " simple:",
262 " \\t\\n'\"\\033?""?/\\\\:", " a\\:b:", " a\\\\b:",
263 " " LQ_ENC RQ_ENC ":" },
264 { " :", " \\0001\\0:", 9, " simple:",
265 " \\t\\n'\"\\033?""?/\\\\:", " a\\:b:", " a\\\\b:",
266 " " LQ_ENC RQ_ENC ":" } },
268 /* left_quote = "# " and right_quote = "\n" */
269 { { "# \n", "# \\0001\\0\n", 10, "# simple\n",
270 "# \\t\\n'\"\\033?""?/\\\\\n", "# a:b\n", "# a\\\\b\n",
271 "# " LQ_ENC RQ_ENC "\n" },
272 { "# \n", "# \\0001\\0\n", 10, "# simple\n",
273 "# \\t\\n'\"\\033?""?/\\\\\n", "# a:b\n", "# a\\\\b\n",
274 "# " LQ_ENC RQ_ENC "\n" },
275 { "# \n", "# \\0001\\0\n", 10, "# simple\n",
276 "# \\t\\n'\"\\033?""?/\\\\\n", "# a\\:b\n", "# a\\\\b\n",
277 "# " LQ_ENC RQ_ENC "\n" } },
279 /* left_quote = "\"'" and right_quote = "'\"" */
280 { { "\"''\"", "\"'\\0001\\0'\"", 11, "\"'simple'\"",
281 "\"' \\t\\n\\'\"\\033?""?/\\\\'\"", "\"'a:b'\"", "\"'a\\\\b'\"",
282 "\"'" LQ_ENC RQ_ENC "'\"" },
283 { "\"''\"", "\"'\\0001\\0'\"", 11, "\"'simple'\"",
284 "\"' \\t\\n\\'\"\\033?""?/\\\\'\"", "\"'a:b'\"", "\"'a\\\\b'\"",
285 "\"'" LQ_ENC RQ_ENC "'\"" },
286 { "\"''\"", "\"'\\0001\\0'\"", 11, "\"'simple'\"",
287 "\"' \\t\\n\\'\"\\033?""?/\\\\'\"", "\"'a\\:b'\"", "\"'a\\\\b'\"",
288 "\"'" LQ_ENC RQ_ENC "'\"" } }
292 compare (char const *a, size_t la, char const *b, size_t lb)
295 ASSERT (memcmp (a, b, la) == 0);
296 ASSERT (b[lb] == '\0');
300 compare_strings (char *(func) (char const *, size_t *),
301 struct result_strings *results)
307 p = func (inputs.str1, &len);
308 compare (results->str1, strlen (results->str1), p, len);
311 p = func (inputs.str2, &len);
312 compare (results->str2, results->len2, p, len);
315 p = func (inputs.str3, &len);
316 compare (results->str3, strlen (results->str3), p, len);
318 len = strlen (inputs.str4);
319 p = func (inputs.str4, &len);
320 compare (results->str4, strlen (results->str4), p, len);
323 p = func (inputs.str5, &len);
324 compare (results->str5, strlen (results->str5), p, len);
326 len = strlen (inputs.str6);
327 p = func (inputs.str6, &len);
328 compare (results->str6, strlen (results->str6), p, len);
330 len = strlen (inputs.str7);
331 p = func (inputs.str7, &len);
332 compare (results->str7, strlen (results->str7), p, len);
336 use_quotearg_buffer (const char *str, size_t *len)
338 static char buf[100];
340 memset (buf, 0xa5, 100);
341 size = quotearg_buffer (buf, 100, str, *len, NULL);
343 ASSERT ((unsigned char) buf[size + 1] == 0xa5);
348 use_quotearg (const char *str, size_t *len)
350 char *p = *len == SIZE_MAX ? quotearg (str) : quotearg_mem (str, *len);
356 use_quote_double_quotes (const char *str, size_t *len)
358 char *p = *len == SIZE_MAX ? quotearg_char (str, '"')
359 : quotearg_char_mem (str, *len, '"');
365 use_quotearg_colon (const char *str, size_t *len)
367 char *p = (*len == SIZE_MAX ? quotearg_colon (str)
368 : quotearg_colon_mem (str, *len));
374 main (int argc, char *argv[])
378 set_program_name (argv[0]);
380 /* This program part is hard-wired to the C locale since it does not call
382 ASSERT (!isprint ('\033'));
383 for (i = literal_quoting_style; i <= clocale_quoting_style; i++)
385 set_quoting_style (NULL, i);
386 compare_strings (use_quotearg_buffer, &results_g[i].group1);
387 compare_strings (use_quotearg, &results_g[i].group2);
388 if (i == c_quoting_style)
389 compare_strings (use_quote_double_quotes, &results_g[i].group2);
390 compare_strings (use_quotearg_colon, &results_g[i].group3);
393 set_quoting_style (NULL, literal_quoting_style);
394 ASSERT (set_quoting_flags (NULL, QA_ELIDE_NULL_BYTES) == 0);
395 compare_strings (use_quotearg_buffer, &flag_results[0].group1);
396 compare_strings (use_quotearg, &flag_results[0].group2);
397 compare_strings (use_quotearg_colon, &flag_results[0].group3);
399 set_quoting_style (NULL, c_quoting_style);
400 ASSERT (set_quoting_flags (NULL, QA_ELIDE_OUTER_QUOTES)
401 == QA_ELIDE_NULL_BYTES);
402 compare_strings (use_quotearg_buffer, &flag_results[1].group1);
403 compare_strings (use_quotearg, &flag_results[1].group2);
404 compare_strings (use_quote_double_quotes, &flag_results[1].group2);
405 compare_strings (use_quotearg_colon, &flag_results[1].group3);
407 ASSERT (set_quoting_flags (NULL, QA_SPLIT_TRIGRAPHS)
408 == QA_ELIDE_OUTER_QUOTES);
409 compare_strings (use_quotearg_buffer, &flag_results[2].group1);
410 compare_strings (use_quotearg, &flag_results[2].group2);
411 compare_strings (use_quote_double_quotes, &flag_results[2].group2);
412 compare_strings (use_quotearg_colon, &flag_results[2].group3);
414 ASSERT (set_quoting_flags (NULL, 0) == QA_SPLIT_TRIGRAPHS);
416 for (i = 0; i < sizeof custom_quotes / sizeof *custom_quotes; ++i)
418 set_custom_quoting (NULL,
419 custom_quotes[i][0], custom_quotes[i][1]);
420 compare_strings (use_quotearg_buffer, &custom_results[i].group1);
421 compare_strings (use_quotearg, &custom_results[i].group2);
422 compare_strings (use_quotearg_colon, &custom_results[i].group3);
426 /* Clean up environment. */
427 unsetenv ("LANGUAGE");
429 unsetenv ("LC_MESSAGES");
430 unsetenv ("LC_CTYPE");
432 unsetenv ("OUTPUT_CHARSET");
434 /* This program part runs in a French UTF-8 locale. It uses
435 the test-quotearg.mo message catalog. */
437 const char *locale_name = getenv ("LOCALE");
439 if (locale_name != NULL && strcmp (locale_name, "none") != 0
440 && setenv ("LC_ALL", locale_name, 1) == 0
441 && setlocale (LC_ALL, "") != NULL)
443 textdomain ("test-quotearg");
444 bindtextdomain ("test-quotearg", getenv ("LOCALEDIR"));
446 set_quoting_style (NULL, locale_quoting_style);
447 compare_strings (use_quotearg_buffer, &locale_results[0].group1);
448 compare_strings (use_quotearg, &locale_results[0].group2);
449 compare_strings (use_quotearg_colon, &locale_results[0].group3);
451 set_quoting_style (NULL, clocale_quoting_style);
452 compare_strings (use_quotearg_buffer, &locale_results[1].group1);
453 compare_strings (use_quotearg, &locale_results[1].group2);
454 compare_strings (use_quotearg_colon, &locale_results[1].group3);
457 #endif /* ENABLE_NLS */