summaryrefslogtreecommitdiff
path: root/tests/test-system-quote-main.c
blob: 811fc7ed09d6d520541f871ff58410025e0096d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* Test of system-quote module.
   Copyright (C) 2012-2023 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <https://www.gnu.org/licenses/>.  */

/* Written by Bruno Haible <bruno@clisp.org>, 2012.  */

#include <config.h>

/* Specification.  */
#include "system-quote.h"

#if defined _WIN32 && !defined __CYGWIN__
# define WINDOWS_NATIVE
#endif

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef WINDOWS_NATIVE
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif

#include "macros.h"

#define EXPECTED_DATA_FILE "t-sq-data.tmp"

static int failed;

static void
check_one (enum system_command_interpreter interpreter, const char *prog,
           const char *input)
{
  char buf[1000];
  size_t output_len;
  char *output;
  char *bufend;

  output_len = system_quote_length (interpreter, input);

  output = system_quote (interpreter, input);
  ASSERT (strlen (output) == output_len);

  ASSERT (output_len <= sizeof (buf) - 2);
  memset (buf, '\0', output_len + 1);
  buf[output_len + 1] = '%';
  bufend = system_quote_copy (buf, interpreter, input);
  ASSERT (bufend == buf + output_len);
  ASSERT (memcmp (buf, output, output_len + 1) == 0);
  ASSERT (buf[output_len + 1] == '%');

  /* Store INPUT in EXPECTED_DATA_FILE, for verification by the child
     process.  */
  {
    FILE *fp = fopen (EXPECTED_DATA_FILE, "wb");
    if (fp == NULL)
      exit (3);
    if (fwrite (input, 1, strlen (input), fp) != strlen (input))
      exit (4);
    if (fclose (fp))
      exit (5);
  }

  /* Invoke the child process through system() and popen().  */
  {
    char command[1000];

    sprintf (command, "%s %s", prog, output);

    switch (interpreter)
      {
      case SCI_SYSTEM:
#ifdef WINDOWS_NATIVE
      case SCI_WINDOWS_CMD:
#endif
        {
          int exitcode = system (command);
          if (exitcode != 0)
            {
              fprintf (stderr, "for input = |%s|: system() command failed with status %d: %s\n",
                       input, exitcode, command);
              failed = 1;
            }
        }
        {
          FILE *fp = popen (command, "r");
          int exitcode = pclose (fp);
          if (exitcode != 0)
            {
              fprintf (stderr, "for input = |%s|: popen() command failed with status %d: %s\n",
                       input, exitcode, command);
              failed = 1;
            }
        }
        break;
#ifdef WINDOWS_NATIVE
      case SCI_WINDOWS_CREATEPROCESS:
        {
          PROCESS_INFORMATION pinfo;
          STARTUPINFO sinfo;
          sinfo.cb = sizeof (STARTUPINFO);
          sinfo.lpReserved = NULL;
          sinfo.lpDesktop = NULL;
          sinfo.lpTitle = NULL;
          sinfo.cbReserved2 = 0;
          sinfo.lpReserved2 = NULL;
          sinfo.dwFlags = STARTF_USESTDHANDLES;
          sinfo.hStdInput = GetStdHandle (STD_INPUT_HANDLE);
          sinfo.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE);
          sinfo.hStdError = GetStdHandle (STD_ERROR_HANDLE);

          if (CreateProcess (NULL, command, NULL, NULL, TRUE, 0, NULL, NULL,
                             &sinfo, &pinfo))
            {
              DWORD exitcode;
              CloseHandle (pinfo.hThread);
              if (WaitForSingleObject (pinfo.hProcess, INFINITE) == WAIT_OBJECT_0)
                {
                  if (GetExitCodeProcess (pinfo.hProcess, &exitcode))
                    {
                      if (exitcode != 0)
                        {
                          fprintf (stderr, "for input = |%s|: CreateProcess() command failed with status %d: %s\n",
                                   input, exitcode, command);
                          failed = 1;
                        }
                    }
                  else
                    {
                      fprintf (stderr, "for input = |%s|: GetExitCodeProcess failed, GetLastError() = %u\n",
                               input, GetLastError ());
                      failed = 1;
                    }
                }
              else
                {
                  fprintf (stderr, "for input = |%s|: WaitForSingleObject failed\n",
                           input);
                  failed = 1;
                }
              CloseHandle (pinfo.hProcess);
            }
          else
            {
              fprintf (stderr, "for input = |%s|: CreateProcess failed, GetLastError() = %u\n",
                       input, GetLastError ());
              failed = 1;
            }
        }
        break;
#endif
      default:
        break;
      }
  }

  free (output);
}

static void
check_all (enum system_command_interpreter interpreter,
           bool windows_cmd_limitations,
           const char *prog)
{
  /* Check the system_quote_length, system_quote_copy, system_quote
     functions.  */
  {
    int c;

    /* Empty argument.  */
    check_one (interpreter, prog, "");

    /* Identifier or number.  */
    check_one (interpreter, prog, "foo");
    check_one (interpreter, prog, "phr0ck");

    /* Whitespace would be interpreted as argument separator by the shell.  */
    check_one (interpreter, prog, "foo\tbar");
    if (!windows_cmd_limitations)
      {
        check_one (interpreter, prog, "foo\nbar");
        check_one (interpreter, prog, "foo\rbar");
      }
    check_one (interpreter, prog, "foo bar");

    /* '!' at the beginning of argv[0] would introduce a negated command.  */
    check_one (interpreter, prog, "!foo");

    /* '"' would be interpreted as the start of a string.  */
    check_one (interpreter, prog, "\"foo\"bar");

    /* '#' at the beginning of an argument would be interpreted as the start
       of a comment.  */
    check_one (interpreter, prog, "#foo");

    /* '$' at the beginning of an argument would be interpreted as a variable
       reference.  */
    check_one (interpreter, prog, "$foo");

    /* '&' at the beginning of an argument would be interpreted as a background
       task indicator.  */
    check_one (interpreter, prog, "&");

    /* "'" would be interpreted as the start of a string.  */
    check_one (interpreter, prog, "'foo'bar");

    /* '(' at the beginning of argv[0] would introduce a subshell command.  */
    check_one (interpreter, prog, "(");

    /* ')' at the beginning of an argument would be interpreted as the end of
       the command.  */
    check_one (interpreter, prog, ")");

    /* '*' would be interpreted as a wildcard character.  */
    check_one (interpreter, prog, "*");
    check_one (interpreter, prog, "*foo");

    /* ';' at the beginning of an argument would be interpreted as an empty
       statement in argv[0] and as the end of the command otherwise.  */
    check_one (interpreter, prog, ";");
    check_one (interpreter, prog, "foo;");

    /* '<' would be interpreted as a redirection of stdin.  */
    check_one (interpreter, prog, "<");

    /* '=' inside argv[0] would be interpreted as an environment variable
       assignment.  */
    check_one (interpreter, prog, "foo=bar");

    /* '>' would be interpreted as a redirection of stdout.  */
    check_one (interpreter, prog, ">");

    /* '?' would be interpreted as a wildcard character.  */
    check_one (interpreter, prog, "?");
    check_one (interpreter, prog, "??");
    check_one (interpreter, prog, "???");
    check_one (interpreter, prog, "????");
    check_one (interpreter, prog, "?????");
    check_one (interpreter, prog, "??????");
    check_one (interpreter, prog, "???????");
    check_one (interpreter, prog, "????????");
    check_one (interpreter, prog, "?????????");
    check_one (interpreter, prog, "??????????");
    check_one (interpreter, prog, "foo?bar");

    /* '^' would be interpreted in old /bin/sh, e.g. SunOS 4.1.4.  */
    check_one (interpreter, prog, "^");

    /* "[...]" would be interpreted as a wildcard pattern.  */
    check_one (interpreter, prog, "[");
    check_one (interpreter, prog, "]");

    /* '\' would be interpreted as an escape character.  */
    check_one (interpreter, prog, "\\foo");

    /* '`' would be interpreted as the start of a command substitution.  */
    check_one (interpreter, prog, "`foo");

    /* '{' at the beginning of argv[0] would introduce a complex command.  */
    check_one (interpreter, prog, "{");

    /* '|' at the beginning of an argument would be interpreted as a pipe
       between commands.  */
    check_one (interpreter, prog, "|");

    /* '}' at the beginning of an argument would be interpreted as the end of
       the command.  */
    check_one (interpreter, prog, "}");

    /* '~' at the beginning of an argument would be interpreted as a reference
       to a user's home directory.  */
    check_one (interpreter, prog, "~");
    check_one (interpreter, prog, "~foo");

    /* A string that contains both ' and ".  */
    check_one (interpreter, prog, "foo'bar\"baz");

    /* '%' is used for environment variable references in Windows cmd.exe.  */
    check_one (interpreter, prog, "%");
    check_one (interpreter, prog, "%%");
    check_one (interpreter, prog, "%foo%");
    check_one (interpreter, prog, "%PATH%");

    /* All other characters don't need quoting.  */
    for (c = 1; c <= UCHAR_MAX; c++)
      if (strchr ("\t\n\r !\"#$&'()*;<=>?^[\\]`{|}~", c) == NULL)
        {
          char s[5];
          s[0] = 'a';
          s[1] = (char) c;
          s[2] = 'z';
          s[3] = (char) c;
          s[4] = '\0';

          check_one (interpreter, prog, s);
        }
  }
}

int
main (int argc, char *argv[])
{
  char *prog;

  if (argc != 2)
    {
      fprintf (stderr, "%s: need 1 argument\n", argv[0]);
      return 2;
    }
  prog = argv[1];

#ifdef WINDOWS_NATIVE
  /* Make PROG suitable for native Windows system calls and cmd.exe:
     Replace '/' with '\\'.  */
  {
    char *p;
    for (p = prog; *p != '\0'; p++)
      if (*p == '/')
        *p = '\\';
  }
#endif

#ifdef WINDOWS_NATIVE
  check_all (SCI_SYSTEM, true, prog); /* equivalent to SCI_WINDOWS_CMD */
  check_all (SCI_WINDOWS_CREATEPROCESS, false, prog);
  check_all (SCI_WINDOWS_CMD, true, prog);
#else
  check_all (SCI_SYSTEM, false, prog); /* equivalent to SCI_POSIX_SH */
#endif

  /* Clean up.  */
  unlink (EXPECTED_DATA_FILE);

  return failed;
}