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