86ffd7c5c1b34361ba399deb62134305f694c9e7
[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 # include <libintl.h>
33
34 /* These quotes are borrowed from a pt_PT.utf8 translation.  */
35 # define LQ "\302\253"
36 # define RQ "\302\273"
37 #endif
38
39 #define ASSERT(expr) \
40   do                                                                         \
41     {                                                                        \
42       if (!(expr))                                                           \
43         {                                                                    \
44           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
45           abort ();                                                          \
46         }                                                                    \
47     }                                                                        \
48   while (0)
49
50 struct result_strings {
51   char const *str1; /* Translation of "".  */
52   char const *str2; /* Translation of "\0""1\0".  */
53   size_t len2; /* Length of str2.  */
54   char const *str3; /* Translation of "simple".  */
55   char const *str4; /* Translation of " \t\n'\"\033?""?/\\".  */
56   char const *str5; /* Translation of "a:b".  */
57 };
58
59 struct result_groups {
60   struct result_strings group1; /* Via quotearg_buffer.  */
61   struct result_strings group2; /* Via quotearg{,_mem}.  */
62   struct result_strings group3; /* Via quotearg_colon{,_mem}.  */
63 };
64
65 static struct result_strings inputs = {
66   "", "\0001\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b"
67 };
68
69 static struct result_groups results[] = {
70   /* literal_quoting_style */
71   { { "", "\0""1\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b" },
72     { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b" },
73     { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b" } },
74
75   /* shell_quoting_style */
76   { { "''", "\0""1\0", 3, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b" },
77     { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b" },
78     { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "'a:b'" } },
79
80   /* shell_always_quoting_style */
81   { { "''", "'\0""1\0'", 5, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'" },
82     { "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'" },
83     { "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'" } },
84
85   /* c_quoting_style */
86   { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
87       "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"" },
88     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
89       "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"" },
90     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
91       "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a\\:b\"" } },
92
93   /* c_maybe_quoting_style */
94   { { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"",
95       "a:b" },
96     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"",
97       "a:b" },
98     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"",
99       "\"a:b\"" } },
100
101   /* escape_quoting_style */
102   { { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b" },
103     { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b" },
104     { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a\\:b" } },
105
106   /* locale_quoting_style */
107   { { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
108       "`a:b'" },
109     { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
110       "`a:b'" },
111     { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
112       "`a\\:b'" } },
113
114   /* clocale_quoting_style */
115   { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
116       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"" },
117     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
118       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"" },
119     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
120       "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a\\:b\"" } }
121 };
122
123 #if ENABLE_NLS
124 static struct result_groups locale_results[] = {
125   /* locale_quoting_style */
126   { { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
127       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ },
128     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
129       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ },
130     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
131       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a\\:b" RQ } },
132
133   /* clocale_quoting_style */
134   { { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
135       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ },
136     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
137       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ },
138     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
139       LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a\\:b" RQ } }
140 };
141 #endif /* ENABLE_NLS */
142
143 static void
144 compare (char const *a, size_t la, char const *b, size_t lb)
145 {
146   ASSERT (la == lb);
147   ASSERT (memcmp (a, b, la) == 0);
148   ASSERT (b[lb] == '\0');
149 }
150
151 static void
152 compare_strings (char *(func) (char const *, size_t *),
153                  struct result_strings *results)
154 {
155   size_t len;
156   char *p;
157
158   len = 0;
159   p = func (inputs.str1, &len);
160   compare (results->str1, strlen (results->str1), p, len);
161
162   len = inputs.len2;
163   p = func (inputs.str2, &len);
164   compare (results->str2, results->len2, p, len);
165
166   len = SIZE_MAX;
167   p = func (inputs.str3, &len);
168   compare (results->str3, strlen (results->str3), p, len);
169
170   len = strlen (inputs.str4);
171   p = func (inputs.str4, &len);
172   compare (results->str4, strlen (results->str4), p, len);
173
174   len = SIZE_MAX;
175   p = func (inputs.str5, &len);
176   compare (results->str5, strlen (results->str5), p, len);
177 }
178
179 static char *
180 use_quotearg_buffer (const char *str, size_t *len)
181 {
182   static char buf[100];
183   size_t size;
184   memset (buf, 0xa5, 100);
185   size = quotearg_buffer (buf, 100, str, *len, NULL);
186   *len = size;
187   ASSERT ((unsigned char) buf[size + 1] == 0xa5);
188   return buf;
189 }
190
191 static char *
192 use_quotearg (const char *str, size_t *len)
193 {
194   char *p = *len == SIZE_MAX ? quotearg (str) : quotearg_mem (str, *len);
195   *len = strlen (p);
196   return p;
197 }
198
199 static char *
200 use_quotearg_colon (const char *str, size_t *len)
201 {
202   char *p = (*len == SIZE_MAX ? quotearg_colon (str)
203              : quotearg_colon_mem (str, *len));
204   *len = strlen (p);
205   return p;
206 }
207
208 #if ENABLE_NLS
209 /* True if the locale should be faked.  */
210 static bool fake_locale;
211
212 /* A replacement gettext that allows testing of locale quotes without
213    requiring a locale.  */
214 char *
215 gettext (char const *str)
216 {
217   if (fake_locale)
218     {
219       static char lq[] = LQ;
220       static char rq[] = RQ;
221       if (strcmp (str, "`") == 0)
222         return lq;
223       if (strcmp (str, "'") == 0)
224         return rq;
225     }
226   return (char *) str;
227 }
228
229 char *
230 dgettext (char const *d, char const *str)
231 {
232   return gettext (str);
233 }
234 #endif /* ENABLE_NLS */
235
236 int
237 main (int argc, char **argv)
238 {
239   int i;
240
241   /* This program is hard-wired to the C locale since it does not call
242      setlocale.  */
243   ASSERT (!isprint ('\033'));
244   for (i = literal_quoting_style; i <= clocale_quoting_style; i++)
245     {
246       set_quoting_style (NULL, i);
247       compare_strings (use_quotearg_buffer, &results[i].group1);
248       compare_strings (use_quotearg, &results[i].group2);
249       compare_strings (use_quotearg_colon, &results[i].group3);
250     }
251
252 #if ENABLE_NLS
253   /* Rather than change locales, and require a .gmo file with
254      translations for "`" and "'" that match our expectations, we
255      merely override the gettext function to satisfy the link
256      dependencies of quotearg.c.  */
257   fake_locale = true;
258
259   set_quoting_style (NULL, locale_quoting_style);
260   compare_strings (use_quotearg_buffer, &locale_results[0].group1);
261   compare_strings (use_quotearg, &locale_results[0].group2);
262   compare_strings (use_quotearg_colon, &locale_results[0].group3);
263
264   set_quoting_style (NULL, clocale_quoting_style);
265   compare_strings (use_quotearg_buffer, &locale_results[1].group1);
266   compare_strings (use_quotearg, &locale_results[1].group2);
267   compare_strings (use_quotearg_colon, &locale_results[1].group3);
268 #endif /* ENABLE_NLS */
269
270   quotearg_free ();
271   return 0;
272 }