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 */
203 compare (char const *a, size_t la, char const *b, size_t lb)
206 ASSERT (memcmp (a, b, la) == 0);
207 ASSERT (b[lb] == '\0');
211 compare_strings (char *(func) (char const *, size_t *),
212 struct result_strings *results)
218 p = func (inputs.str1, &len);
219 compare (results->str1, strlen (results->str1), p, len);
222 p = func (inputs.str2, &len);
223 compare (results->str2, results->len2, p, len);
226 p = func (inputs.str3, &len);
227 compare (results->str3, strlen (results->str3), p, len);
229 len = strlen (inputs.str4);
230 p = func (inputs.str4, &len);
231 compare (results->str4, strlen (results->str4), p, len);
234 p = func (inputs.str5, &len);
235 compare (results->str5, strlen (results->str5), p, len);
237 len = strlen (inputs.str6);
238 p = func (inputs.str6, &len);
239 compare (results->str6, strlen (results->str6), p, len);
241 len = strlen (inputs.str7);
242 p = func (inputs.str7, &len);
243 compare (results->str7, strlen (results->str7), p, len);
247 use_quotearg_buffer (const char *str, size_t *len)
249 static char buf[100];
251 memset (buf, 0xa5, 100);
252 size = quotearg_buffer (buf, 100, str, *len, NULL);
254 ASSERT ((unsigned char) buf[size + 1] == 0xa5);
259 use_quotearg (const char *str, size_t *len)
261 char *p = *len == SIZE_MAX ? quotearg (str) : quotearg_mem (str, *len);
267 use_quote_double_quotes (const char *str, size_t *len)
269 char *p = *len == SIZE_MAX ? quotearg_char (str, '"')
270 : quotearg_char_mem (str, *len, '"');
276 use_quotearg_colon (const char *str, size_t *len)
278 char *p = (*len == SIZE_MAX ? quotearg_colon (str)
279 : quotearg_colon_mem (str, *len));
285 main (int argc, char *argv[])
289 set_program_name (argv[0]);
291 /* This program part is hard-wired to the C locale since it does not call
293 ASSERT (!isprint ('\033'));
294 for (i = literal_quoting_style; i <= clocale_quoting_style; i++)
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);
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);
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);
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);
325 ASSERT (set_quoting_flags (NULL, 0) == QA_SPLIT_TRIGRAPHS);
328 /* Clean up environment. */
329 unsetenv ("LANGUAGE");
331 unsetenv ("LC_MESSAGES");
332 unsetenv ("LC_CTYPE");
334 unsetenv ("OUTPUT_CHARSET");
336 /* This program part runs in a French UTF-8 locale. It uses
337 the test-quotearg.mo message catalog. */
339 const char *locale_name = getenv ("LOCALE");
341 if (locale_name != NULL && strcmp (locale_name, "none") != 0
342 && setenv ("LC_ALL", locale_name, 1) == 0
343 && setlocale (LC_ALL, "") != NULL)
345 textdomain ("test-quotearg");
346 bindtextdomain ("test-quotearg", getenv ("LOCALEDIR"));
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);
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);
359 #endif /* ENABLE_NLS */