port poll tests to Windows
[gnulib.git] / tests / test-quotearg.c
1 /* Test of quotearg family of functions.
2    Copyright (C) 2008 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 <stdbool.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #if ENABLE_NLS
32 /* On Linux, gettext is optionally defined as a forwarding macro,
33    which would cause syntax errors in our definition below.  But on
34    platforms that require -lintl, we cannot #undef gettext, since we
35    want to provide the entry point libintl_gettext.  So we disable
36    optimizations to avoid the Linux macros.  */
37 # undef __OPTIMIZE__
38 # include <libintl.h>
39
40 /* These quotes are borrowed from a pt_PT.utf8 translation.  */
41 # define LQ "\302\253"
42 # define RQ "\302\273"
43 #endif
44
45 #define ASSERT(expr) \
46   do                                                                         \
47     {                                                                        \
48       if (!(expr))                                                           \
49         {                                                                    \
50           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
51           fflush (stderr);                                                   \
52           abort ();                                                          \
53         }                                                                    \
54     }                                                                        \
55   while (0)
56
57 struct result_strings {
58   char const *str1; /* Translation of "".  */
59   char const *str2; /* Translation of "\0""1\0".  */
60   size_t len2; /* Length of str2.  */
61   char const *str3; /* Translation of "simple".  */
62   char const *str4; /* Translation of " \t\n'\"\033?""?/\\".  */
63   char const *str5; /* Translation of "a:b".  */
64   char const *str6; /* Translation of "a\\b".  */
65 };
66
67 struct result_groups {
68   struct result_strings group1; /* Via quotearg_buffer.  */
69   struct result_strings group2; /* Via quotearg{,_mem}.  */
70   struct result_strings group3; /* Via quotearg_colon{,_mem}.  */
71 };
72
73 static struct result_strings inputs = {
74   "", "\0001\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b"
75 };
76
77 static struct result_groups results_g[] = {
78   /* literal_quoting_style */
79   { { "", "\0""1\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" },
80     { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" },
81     { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" } },
82
83   /* shell_quoting_style */
84   { { "''", "\0""1\0", 3, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b",
85       "'a\\b'" },
86     { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b",
87       "'a\\b'" },
88     { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
89       "'a\\b'" } },
90
91   /* shell_always_quoting_style */
92   { { "''", "'\0""1\0'", 5, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
93       "'a\\b'" },
94     { "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
95       "'a\\b'" },
96     { "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
97       "'a\\b'" } },
98
99   /* c_quoting_style */
100   { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
101       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
102     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
103       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
104     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
105       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"" } },
106
107   /* c_maybe_quoting_style */
108   { { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
109       "a:b", "a\\b" },
110     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
111       "a:b", "a\\b" },
112     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
113       "\"a:b\"", "a\\b" } },
114
115   /* escape_quoting_style */
116   { { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b",
117       "a\\\\b" },
118     { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b",
119       "a\\\\b" },
120     { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a\\:b",
121       "a\\\\b" } },
122
123   /* locale_quoting_style */
124   { { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
125       "`a:b'", "`a\\\\b'" },
126     { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
127       "`a:b'", "`a\\\\b'" },
128     { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
129       "`a\\:b'", "`a\\\\b'" } },
130
131   /* clocale_quoting_style */
132   { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
133       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
134     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
135       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
136     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
137       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"" } }
138 };
139
140 static struct result_groups flag_results[] = {
141   /* literal_quoting_style and QA_ELIDE_NULL_BYTES */
142   { { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" },
143     { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" },
144     { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" } },
145
146   /* c_quoting_style and QA_ELIDE_OUTER_QUOTES */
147   { { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
148       "a:b", "a\\b" },
149     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
150       "a:b", "a\\b" },
151     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
152       "\"a:b\"", "a\\b" } },
153
154   /* c_quoting_style and QA_SPLIT_TRIGRAPHS */
155   { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
156       "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
157     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
158       "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
159     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
160       "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"" } }
161 };
162
163 #if ENABLE_NLS
164 static struct result_groups locale_results[] = {
165   /* locale_quoting_style */
166   { { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
167       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ },
168     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
169       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ },
170     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
171       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a\\:b" RQ, LQ "a\\\\b" RQ } },
172
173   /* clocale_quoting_style */
174   { { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
175       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ },
176     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
177       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ },
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 };
181 #endif /* ENABLE_NLS */
182
183 static void
184 compare (char const *a, size_t la, char const *b, size_t lb)
185 {
186   ASSERT (la == lb);
187   ASSERT (memcmp (a, b, la) == 0);
188   ASSERT (b[lb] == '\0');
189 }
190
191 static void
192 compare_strings (char *(func) (char const *, size_t *),
193                  struct result_strings *results)
194 {
195   size_t len;
196   char *p;
197
198   len = 0;
199   p = func (inputs.str1, &len);
200   compare (results->str1, strlen (results->str1), p, len);
201
202   len = inputs.len2;
203   p = func (inputs.str2, &len);
204   compare (results->str2, results->len2, p, len);
205
206   len = SIZE_MAX;
207   p = func (inputs.str3, &len);
208   compare (results->str3, strlen (results->str3), p, len);
209
210   len = strlen (inputs.str4);
211   p = func (inputs.str4, &len);
212   compare (results->str4, strlen (results->str4), p, len);
213
214   len = SIZE_MAX;
215   p = func (inputs.str5, &len);
216   compare (results->str5, strlen (results->str5), p, len);
217
218   len = strlen (inputs.str6);
219   p = func (inputs.str6, &len);
220   compare (results->str6, strlen (results->str6), p, len);
221 }
222
223 static char *
224 use_quotearg_buffer (const char *str, size_t *len)
225 {
226   static char buf[100];
227   size_t size;
228   memset (buf, 0xa5, 100);
229   size = quotearg_buffer (buf, 100, str, *len, NULL);
230   *len = size;
231   ASSERT ((unsigned char) buf[size + 1] == 0xa5);
232   return buf;
233 }
234
235 static char *
236 use_quotearg (const char *str, size_t *len)
237 {
238   char *p = *len == SIZE_MAX ? quotearg (str) : quotearg_mem (str, *len);
239   *len = strlen (p);
240   return p;
241 }
242
243 static char *
244 use_quotearg_colon (const char *str, size_t *len)
245 {
246   char *p = (*len == SIZE_MAX ? quotearg_colon (str)
247              : quotearg_colon_mem (str, *len));
248   *len = strlen (p);
249   return p;
250 }
251
252 #if ENABLE_NLS
253 /* True if the locale should be faked.  */
254 static bool fake_locale;
255
256 /* A replacement gettext that allows testing of locale quotes without
257    requiring a locale.  */
258 char *
259 gettext (char const *str)
260 {
261   if (fake_locale)
262     {
263       static char lq[] = LQ;
264       static char rq[] = RQ;
265       if (strcmp (str, "`") == 0)
266         return lq;
267       if (strcmp (str, "'") == 0)
268         return rq;
269     }
270   return (char *) str;
271 }
272
273 char *
274 dgettext (char const *d, char const *str)
275 {
276   return gettext (str);
277 }
278
279 char *
280 dcgettext (char const *d, char const *str, int c)
281 {
282   return gettext (str);
283 }
284 #endif /* ENABLE_NLS */
285
286 int
287 main ()
288 {
289   int i;
290
291   /* This program 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       compare_strings (use_quotearg_colon, &results_g[i].group3);
300     }
301
302   set_quoting_style (NULL, literal_quoting_style);
303   ASSERT (set_quoting_flags (NULL, QA_ELIDE_NULL_BYTES) == 0);
304   compare_strings (use_quotearg_buffer, &flag_results[0].group1);
305   compare_strings (use_quotearg, &flag_results[0].group2);
306   compare_strings (use_quotearg_colon, &flag_results[0].group3);
307
308   set_quoting_style (NULL, c_quoting_style);
309   ASSERT (set_quoting_flags (NULL, QA_ELIDE_OUTER_QUOTES)
310           == QA_ELIDE_NULL_BYTES);
311   compare_strings (use_quotearg_buffer, &flag_results[1].group1);
312   compare_strings (use_quotearg, &flag_results[1].group2);
313   compare_strings (use_quotearg_colon, &flag_results[1].group3);
314
315   ASSERT (set_quoting_flags (NULL, QA_SPLIT_TRIGRAPHS)
316           == QA_ELIDE_OUTER_QUOTES);
317   compare_strings (use_quotearg_buffer, &flag_results[2].group1);
318   compare_strings (use_quotearg, &flag_results[2].group2);
319   compare_strings (use_quotearg_colon, &flag_results[2].group3);
320
321   ASSERT (set_quoting_flags (NULL, 0) == QA_SPLIT_TRIGRAPHS);
322
323 #if ENABLE_NLS
324   /* Rather than change locales, and require a .gmo file with
325      translations for "`" and "'" that match our expectations, we
326      merely override the gettext function to satisfy the link
327      dependencies of quotearg.c.  */
328   fake_locale = true;
329
330   set_quoting_style (NULL, locale_quoting_style);
331   compare_strings (use_quotearg_buffer, &locale_results[0].group1);
332   compare_strings (use_quotearg, &locale_results[0].group2);
333   compare_strings (use_quotearg_colon, &locale_results[0].group3);
334
335   set_quoting_style (NULL, clocale_quoting_style);
336   compare_strings (use_quotearg_buffer, &locale_results[1].group1);
337   compare_strings (use_quotearg, &locale_results[1].group2);
338   compare_strings (use_quotearg_colon, &locale_results[1].group3);
339 #endif /* ENABLE_NLS */
340
341   quotearg_free ();
342   return 0;
343 }