Tests for module 'sh-quote'.
[gnulib.git] / tests / test-sh-quote.c
1 /* Test of sh-quote module.
2    Copyright (C) 2012 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 of the License, or
7    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2012.  */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include "sh-quote.h"
23
24 #include <limits.h>
25 #include <string.h>
26
27 #include "macros.h"
28
29 static void
30 check_one (const char *input, const char *expected)
31 {
32   char buf[1000];
33   size_t output_len;
34   char *output;
35   char *bufend;
36
37   output_len = shell_quote_length (input);
38
39   output = shell_quote (input);
40   ASSERT (strlen (output) == output_len);
41
42   ASSERT (output_len <= sizeof (buf) - 2);
43   memset (buf, '\0', output_len + 1);
44   buf[output_len + 1] = '%';
45   bufend = shell_quote_copy (buf, input);
46   ASSERT (memcmp (buf, output, output_len + 1) == 0);
47   ASSERT (buf[output_len + 1] == '%');
48
49   ASSERT (strcmp (output, expected) == 0);
50
51   free (output);
52 }
53
54 int
55 main (void)
56 {
57   /* Check the shell_quote_length, shell_quote_copy, shell_quote functions.  */
58   {
59     int c;
60
61     /* Empty argument.  */
62     check_one ("", "''");
63
64     /* Identifier or number.  */
65     check_one ("foo", "foo");
66     check_one ("phr0ck", "phr0ck");
67
68     /* Whitespace would be interpreted as argument separator by the shell.  */
69     check_one ("foo\tbar", "'foo\tbar'");
70     check_one ("foo\nbar", "'foo\nbar'");
71     check_one ("foo bar", "'foo bar'");
72
73     /* '!' at the beginning of argv[0] would introduce a negated command.  */
74     check_one ("!foo", "'!foo'");
75
76     /* '"' would be interpreted as the start of a string.  */
77     check_one ("\"foo\"bar", "'\"foo\"bar'");
78
79     /* '#' at the beginning of an argument would be interpreted as the start
80        of a comment.  */
81     check_one ("#foo", "'#foo'");
82
83     /* '$' at the beginning of an argument would be interpreted as a variable
84        reference.  */
85     check_one ("$foo", "'$foo'");
86
87     /* '&' at the beginning of an argument would be interpreted as a background
88        task indicator.  */
89     check_one ("&", "'&'");
90
91     /* "'" would be interpreted as the start of a string.  */
92     check_one ("'foo'bar", "''\\''foo'\\''bar'"); /* or "\"'foo'bar\"" */
93
94     /* '(' at the beginning of argv[0] would introduce a subshell command.  */
95     check_one ("(", "'('");
96
97     /* ')' at the beginning of an argument would be interpreted as the end of
98        the command.  */
99     check_one (")", "')'");
100
101     /* '*' would be interpreted as a wildcard character.  */
102     check_one ("*", "'*'");
103     check_one ("*foo", "'*foo'");
104
105     /* ';' at the beginning of an argument would be interpreted as an empty
106        statement in argv[0] and as the end of the command otherwise.  */
107     check_one (";", "';'");
108     check_one ("foo;", "'foo;'");
109
110     /* '<' would be interpreted as a redirection of stdin.  */
111     check_one ("<", "'<'");
112
113     /* '=' inside argv[0] would be interpreted as an environment variable
114        assignment.  */
115     check_one ("foo=bar", "'foo=bar'");
116
117     /* '>' would be interpreted as a redirection of stdout.  */
118     check_one (">", "'>'");
119
120     /* '?' would be interpreted as a wildcard character.  */
121     check_one ("?", "'?'");
122     check_one ("foo?bar", "'foo?bar'");
123
124     /* '^' would be interpreted in old /bin/sh, e.g. SunOS 4.1.4.  */
125     check_one ("^", "'^'");
126
127     /* "[...]" would be interpreted as a wildcard pattern.  */
128     check_one ("[", "'['");
129     check_one ("]", "]"); /* or "']'" */
130
131     /* '\' would be interpreted as an escape character.  */
132     check_one ("\\foo", "'\\foo'");
133
134     /* '`' would be interpreted as the start of a command substitution.  */
135     check_one ("`foo", "'`foo'");
136
137     /* '{' at the beginning of argv[0] would introduce a complex command.  */
138     check_one ("{", "'{'");
139
140     /* '|' at the beginning of an argument would be interpreted as a pipe
141        between commands.  */
142     check_one ("|", "'|'");
143
144     /* '}' at the beginning of an argument would be interpreted as the end of
145        the command.  */
146     check_one ("}", "'}'");
147
148     /* '~' at the beginning of an argument would be interpreted as a reference
149        to a user's home directory.  */
150     check_one ("~", "'~'");
151     check_one ("~foo", "'~foo'");
152
153     /* A string that contains both ' and ".  */
154     check_one ("foo'bar\"baz", "'foo'\\''bar\"baz'"); /* or "\"foo'bar\\\"baz\"" */
155
156     /* All other characters don't need quoting.  */
157     for (c = 1; c < UCHAR_MAX; c++)
158       if (strchr ("\t\n\r !\"#$&'()*;<=>?^[\\]`{|}~", c) == NULL)
159         {
160           char s[5];
161           s[0] = 'a';
162           s[1] = (char) c;
163           s[2] = 'z';
164           s[3] = (char) c;
165           s[4] = '\0';
166
167           check_one (s, s);
168         }
169   }
170
171   /* Check the shell_quote_argv function.  */
172   {
173     char *argv[1];
174     char *result;
175     argv[0] = NULL;
176     result = shell_quote_argv (argv);
177     ASSERT (strcmp (result, "") == 0);
178     free (result);
179   }
180   {
181     char *argv[2];
182     char *result;
183     argv[0] = (char *) "foo bar/baz";
184     argv[1] = NULL;
185     result = shell_quote_argv (argv);
186     ASSERT (strcmp (result, "'foo bar/baz'") == 0); /* or "\"foo bar/baz\"" */
187     free (result);
188   }
189   {
190     char *argv[3];
191     char *result;
192     argv[0] = (char *) "foo bar/baz";
193     argv[1] = (char *) "$";
194     argv[2] = NULL;
195     result = shell_quote_argv (argv);
196     ASSERT (strcmp (result, "'foo bar/baz' '$'") == 0); /* or "\"foo bar/baz\" \"\\$\"" */
197     free (result);
198   }
199
200   return 0;
201 }