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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
/* Test of quotearg family of functions.
Copyright (C) 2008-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 Eric Blake <ebb9@byu.net>, 2008. */
#include <config.h>
#include "quotearg.h"
#include <ctype.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "localcharset.h"
#include "macros.h"
#include "zerosize-ptr.h"
#include "test-quotearg.h"
static struct result_groups results_g[] = {
/* literal_quoting_style */
{ { "", "\0""1\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b",
"a' b", LQ RQ, LQ RQ },
{ "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b",
"a' b", LQ RQ, LQ RQ },
{ "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b",
"a' b", LQ RQ, LQ RQ } },
/* shell_quoting_style */
{ { "''", "\0""1\0", 3, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b",
"'a\\b'", "\"a' b\"", LQ RQ, LQ RQ },
{ "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b",
"'a\\b'", "\"a' b\"", LQ RQ, LQ RQ },
{ "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
"'a\\b'", "\"a' b\"", LQ RQ, LQ RQ } },
/* shell_always_quoting_style */
{ { "''", "'\0""1\0'", 5, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
"'a\\b'", "\"a' b\"", "'" LQ RQ "'", "'" LQ RQ "'" },
{ "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
"'a\\b'", "\"a' b\"", "'" LQ RQ "'", "'" LQ RQ "'" },
{ "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
"'a\\b'", "\"a' b\"", "'" LQ RQ "'", "'" LQ RQ "'" } },
/* shell_escape_quoting_style */
{ { "''", "''$'\\0''1'$'\\0'", 15, "simple",
"' '$'\\t\\n'\\''\"'$'\\033''?""?/\\'", "a:b",
"'a\\b'", "\"a' b\"", "''$'" LQ_ENC RQ_ENC "'", LQ RQ },
{ "''", "''$'\\0''1'$'\\0'", 15, "simple",
"' '$'\\t\\n'\\''\"'$'\\033''?""?/\\'", "a:b",
"'a\\b'", "\"a' b\"", "''$'" LQ_ENC RQ_ENC "'", LQ RQ },
{ "''", "''$'\\0''1'$'\\0'", 15, "simple",
"' '$'\\t\\n'\\''\"'$'\\033''?""?/\\'", "'a:b'",
"'a\\b'", "\"a' b\"", "''$'" LQ_ENC RQ_ENC "'", LQ RQ } },
/* shell_escape_always_quoting_style */
{ { "''", "''$'\\0''1'$'\\0'", 15, "'simple'",
"' '$'\\t\\n'\\''\"'$'\\033''?""?/\\'", "'a:b'",
"'a\\b'", "\"a' b\"", "''$'" LQ_ENC RQ_ENC "'", "'" LQ RQ "'" },
{ "''", "''$'\\0''1'$'\\0'", 15, "'simple'",
"' '$'\\t\\n'\\''\"'$'\\033''?""?/\\'", "'a:b'",
"'a\\b'", "\"a' b\"", "''$'" LQ_ENC RQ_ENC "'", "'" LQ RQ "'" },
{ "''", "''$'\\0''1'$'\\0'", 15, "'simple'",
"' '$'\\t\\n'\\''\"'$'\\033''?""?/\\'", "'a:b'",
"'a\\b'", "\"a' b\"", "''$'" LQ_ENC RQ_ENC "'", "'" LQ RQ "'" } },
/* c_quoting_style */
{ { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
"\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"",
"\"a' b\"", "\"" LQ_ENC RQ_ENC "\"", "\"" LQ RQ "\"" },
{ "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
"\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"",
"\"a' b\"", "\"" LQ_ENC RQ_ENC "\"", "\"" LQ RQ "\"" },
{ "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
"\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"",
"\"a' b\"", "\"" LQ_ENC RQ_ENC "\"", "\"" LQ RQ "\"" } },
/* c_maybe_quoting_style */
{ { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
"a:b", "a\\b", "a' b", "\"" LQ_ENC RQ_ENC "\"", LQ RQ },
{ "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
"a:b", "a\\b", "a' b", "\"" LQ_ENC RQ_ENC "\"", LQ RQ },
{ "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
"\"a:b\"", "a\\b", "a' b", "\"" LQ_ENC RQ_ENC "\"", LQ RQ } },
/* escape_quoting_style */
{ { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b",
"a\\\\b", "a' b", LQ_ENC RQ_ENC, LQ RQ },
{ "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b",
"a\\\\b", "a' b", LQ_ENC RQ_ENC, LQ RQ },
{ "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a\\:b",
"a\\\\b", "a' b", LQ_ENC RQ_ENC, LQ RQ } },
/* locale_quoting_style */
{ { "''", "'\\0001\\0'", 9, "'simple'", "' \\t\\n\\'\"\\033?""?/\\\\'",
"'a:b'", "'a\\\\b'", "'a\\' b'", "'" LQ_ENC RQ_ENC "'", "'" LQ RQ "'" },
{ "''", "'\\0001\\0'", 9, "'simple'", "' \\t\\n\\'\"\\033?""?/\\\\'",
"'a:b'", "'a\\\\b'", "'a\\' b'", "'" LQ_ENC RQ_ENC "'", "'" LQ RQ "'" },
{ "''", "'\\0001\\0'", 9, "'simple'", "' \\t\\n\\'\"\\033?""?/\\\\'",
"'a\\:b'", "'a\\\\b'", "'a\\' b'",
"'" LQ_ENC RQ_ENC "'", "'" LQ RQ "'" } },
/* clocale_quoting_style */
{ { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
"\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"",
"\"a' b\"", "\"" LQ_ENC RQ_ENC "\"", "\"" LQ RQ "\"" },
{ "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
"\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"",
"\"a' b\"", "\"" LQ_ENC RQ_ENC "\"", "\"" LQ RQ "\"" },
{ "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
"\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"",
"\"a' b\"", "\"" LQ_ENC RQ_ENC "\"", "\"" LQ RQ "\"" } }
};
static struct result_groups flag_results[] = {
/* literal_quoting_style and QA_ELIDE_NULL_BYTES */
{ { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b", "a' b",
LQ RQ, LQ RQ },
{ "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b", "a' b",
LQ RQ, LQ RQ },
{ "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b", "a' b",
LQ RQ, LQ RQ } },
/* c_quoting_style and QA_ELIDE_OUTER_QUOTES */
{ { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
"a:b", "a\\b", "a' b", "\"" LQ_ENC RQ_ENC "\"", LQ RQ },
{ "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
"a:b", "a\\b", "a' b", "\"" LQ_ENC RQ_ENC "\"", LQ RQ },
{ "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
"\"a:b\"", "a\\b", "a' b", "\"" LQ_ENC RQ_ENC "\"", LQ RQ } },
/* c_quoting_style and QA_SPLIT_TRIGRAPHS */
{ { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
"\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"", "\"a\\\\b\"",
"\"a' b\"", "\"" LQ_ENC RQ_ENC "\"", "\"" LQ RQ "\"" },
{ "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
"\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"", "\"a\\\\b\"",
"\"a' b\"", "\"" LQ_ENC RQ_ENC "\"", "\"" LQ RQ "\"" },
{ "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
"\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"",
"\"a' b\"", "\"" LQ_ENC RQ_ENC "\"", "\"" LQ RQ "\"" } }
};
static char const *custom_quotes[][2] = {
{ "", "" },
{ "'", "'" },
{ "(", ")" },
{ ":", " " },
{ " ", ":" },
{ "# ", "\n" },
{ "\"'", "'\"" }
};
static struct result_groups custom_results[] = {
/* left_quote = right_quote = "" */
{ { "", "\\0001\\0", 7, "simple",
" \\t\\n'\"\\033?""?/\\\\", "a:b", "a\\\\b",
"a' b", LQ_ENC RQ_ENC, LQ RQ },
{ "", "\\0001\\0", 7, "simple",
" \\t\\n'\"\\033?""?/\\\\", "a:b", "a\\\\b",
"a' b", LQ_ENC RQ_ENC, LQ RQ },
{ "", "\\0001\\0", 7, "simple",
" \\t\\n'\"\\033?""?/\\\\", "a\\:b", "a\\\\b",
"a' b", LQ_ENC RQ_ENC, LQ RQ } },
/* left_quote = right_quote = "'" */
{ { "''", "'\\0001\\0'", 9, "'simple'",
"' \\t\\n\\'\"\\033?""?/\\\\'", "'a:b'", "'a\\\\b'",
"'a\\' b'", "'" LQ_ENC RQ_ENC "'", "'" LQ RQ "'" },
{ "''", "'\\0001\\0'", 9, "'simple'",
"' \\t\\n\\'\"\\033?""?/\\\\'", "'a:b'", "'a\\\\b'",
"'a\\' b'", "'" LQ_ENC RQ_ENC "'", "'" LQ RQ "'" },
{ "''", "'\\0001\\0'", 9, "'simple'",
"' \\t\\n\\'\"\\033?""?/\\\\'", "'a\\:b'", "'a\\\\b'",
"'a\\' b'", "'" LQ_ENC RQ_ENC "'", "'" LQ RQ "'" } },
/* left_quote = "(" and right_quote = ")" */
{ { "()", "(\\0001\\0)", 9, "(simple)",
"( \\t\\n'\"\\033?""?/\\\\)", "(a:b)", "(a\\\\b)",
"(a' b)", "(" LQ_ENC RQ_ENC ")", "(" LQ RQ ")" },
{ "()", "(\\0001\\0)", 9, "(simple)",
"( \\t\\n'\"\\033?""?/\\\\)", "(a:b)", "(a\\\\b)",
"(a' b)", "(" LQ_ENC RQ_ENC ")", "(" LQ RQ ")" },
{ "()", "(\\0001\\0)", 9, "(simple)",
"( \\t\\n'\"\\033?""?/\\\\)", "(a\\:b)", "(a\\\\b)",
"(a' b)", "(" LQ_ENC RQ_ENC ")", "(" LQ RQ ")" } },
/* left_quote = ":" and right_quote = " " */
{ { ": ", ":\\0001\\0 ", 9, ":simple ",
":\\ \\t\\n'\"\\033?""?/\\\\ ", ":a:b ", ":a\\\\b ",
":a'\\ b ", ":" LQ_ENC RQ_ENC " ", ":" LQ RQ " " },
{ ": ", ":\\0001\\0 ", 9, ":simple ",
":\\ \\t\\n'\"\\033?""?/\\\\ ", ":a:b ", ":a\\\\b ",
":a'\\ b ", ":" LQ_ENC RQ_ENC " ", ":" LQ RQ " " },
{ ": ", ":\\0001\\0 ", 9, ":simple ",
":\\ \\t\\n'\"\\033?""?/\\\\ ", ":a\\:b ", ":a\\\\b ",
":a'\\ b ", ":" LQ_ENC RQ_ENC " ", ":" LQ RQ " " } },
/* left_quote = " " and right_quote = ":" */
{ { " :", " \\0001\\0:", 9, " simple:",
" \\t\\n'\"\\033?""?/\\\\:", " a\\:b:", " a\\\\b:",
" a' b:", " " LQ_ENC RQ_ENC ":", " " LQ RQ ":" },
{ " :", " \\0001\\0:", 9, " simple:",
" \\t\\n'\"\\033?""?/\\\\:", " a\\:b:", " a\\\\b:",
" a' b:", " " LQ_ENC RQ_ENC ":", " " LQ RQ ":" },
{ " :", " \\0001\\0:", 9, " simple:",
" \\t\\n'\"\\033?""?/\\\\:", " a\\:b:", " a\\\\b:",
" a' b:", " " LQ_ENC RQ_ENC ":", " " LQ RQ ":" } },
/* left_quote = "# " and right_quote = "\n" */
{ { "# \n", "# \\0001\\0\n", 10, "# simple\n",
"# \\t\\n'\"\\033?""?/\\\\\n", "# a:b\n", "# a\\\\b\n",
"# a' b\n", "# " LQ_ENC RQ_ENC "\n", "# " LQ RQ "\n" },
{ "# \n", "# \\0001\\0\n", 10, "# simple\n",
"# \\t\\n'\"\\033?""?/\\\\\n", "# a:b\n", "# a\\\\b\n",
"# a' b\n", "# " LQ_ENC RQ_ENC "\n", "# " LQ RQ "\n" },
{ "# \n", "# \\0001\\0\n", 10, "# simple\n",
"# \\t\\n'\"\\033?""?/\\\\\n", "# a\\:b\n", "# a\\\\b\n",
"# a' b\n", "# " LQ_ENC RQ_ENC "\n", "# " LQ RQ "\n" } },
/* left_quote = "\"'" and right_quote = "'\"" */
{ { "\"''\"", "\"'\\0001\\0'\"", 11, "\"'simple'\"",
"\"' \\t\\n\\'\"\\033?""?/\\\\'\"", "\"'a:b'\"", "\"'a\\\\b'\"",
"\"'a' b'\"", "\"'" LQ_ENC RQ_ENC "'\"", "\"'" LQ RQ "'\"" },
{ "\"''\"", "\"'\\0001\\0'\"", 11, "\"'simple'\"",
"\"' \\t\\n\\'\"\\033?""?/\\\\'\"", "\"'a:b'\"", "\"'a\\\\b'\"",
"\"'a' b'\"", "\"'" LQ_ENC RQ_ENC "'\"", "\"'" LQ RQ "'\"" },
{ "\"''\"", "\"'\\0001\\0'\"", 11, "\"'simple'\"",
"\"' \\t\\n\\'\"\\033?""?/\\\\'\"", "\"'a\\:b'\"", "\"'a\\\\b'\"",
"\"'a' b'\"", "\"'" LQ_ENC RQ_ENC "'\"", "\"'" LQ RQ "'\"" } }
};
static char *
use_quote_double_quotes (const char *str, size_t *len)
{
char *p = *len == SIZE_MAX ? quotearg_char (str, '"')
: quotearg_char_mem (str, *len, '"');
*len = strlen (p);
return p;
}
int
main (_GL_UNUSED int argc, char *argv[])
{
int i;
bool ascii_only = MB_CUR_MAX == 1 && !isprint ((unsigned char) LQ[0]);
/* This part of the program is hard-wired to the C locale since it
does not call setlocale. However, according to POSIX, the use of
8-bit bytes in a character context in the C locale gives
unspecified results (that is, the C locale charset is allowed to
be unibyte with 8-bit bytes rejected [ASCII], unibyte with 8-bit
bytes being characters [often ISO-8859-1], or multibyte [often
UTF-8]). We assume that the latter two cases will be
indistinguishable in this test - that is, the LQ and RQ sequences
will pass through unchanged in either type of charset. So when
testing for quoting of str7, use the ascii_only flag to decide
what to expect for the 8-bit data being quoted. */
ASSERT (!isprint ('\033'));
for (i = literal_quoting_style; i <= clocale_quoting_style; i++)
{
set_quoting_style (NULL, (enum quoting_style) i);
if (!(i == locale_quoting_style || i == clocale_quoting_style)
|| (strcmp (locale_charset (), "ASCII") == 0
|| strcmp (locale_charset (), "ANSI_X3.4-1968") == 0))
{
compare_strings (use_quotearg_buffer, &results_g[i].group1,
ascii_only);
compare_strings (use_quotearg, &results_g[i].group2,
ascii_only);
if (i == c_quoting_style)
compare_strings (use_quote_double_quotes, &results_g[i].group2,
ascii_only);
compare_strings (use_quotearg_colon, &results_g[i].group3,
ascii_only);
}
}
set_quoting_style (NULL, literal_quoting_style);
ASSERT (set_quoting_flags (NULL, QA_ELIDE_NULL_BYTES) == 0);
compare_strings (use_quotearg_buffer, &flag_results[0].group1, ascii_only);
compare_strings (use_quotearg, &flag_results[0].group2, ascii_only);
compare_strings (use_quotearg_colon, &flag_results[0].group3, ascii_only);
set_quoting_style (NULL, c_quoting_style);
ASSERT (set_quoting_flags (NULL, QA_ELIDE_OUTER_QUOTES)
== QA_ELIDE_NULL_BYTES);
compare_strings (use_quotearg_buffer, &flag_results[1].group1, ascii_only);
compare_strings (use_quotearg, &flag_results[1].group2, ascii_only);
compare_strings (use_quote_double_quotes, &flag_results[1].group2,
ascii_only);
compare_strings (use_quotearg_colon, &flag_results[1].group3, ascii_only);
ASSERT (set_quoting_flags (NULL, QA_SPLIT_TRIGRAPHS)
== QA_ELIDE_OUTER_QUOTES);
compare_strings (use_quotearg_buffer, &flag_results[2].group1, ascii_only);
compare_strings (use_quotearg, &flag_results[2].group2, ascii_only);
compare_strings (use_quote_double_quotes, &flag_results[2].group2,
ascii_only);
compare_strings (use_quotearg_colon, &flag_results[2].group3, ascii_only);
ASSERT (set_quoting_flags (NULL, 0) == QA_SPLIT_TRIGRAPHS);
for (i = 0; i < sizeof custom_quotes / sizeof *custom_quotes; ++i)
{
set_custom_quoting (NULL,
custom_quotes[i][0], custom_quotes[i][1]);
compare_strings (use_quotearg_buffer, &custom_results[i].group1,
ascii_only);
compare_strings (use_quotearg, &custom_results[i].group2, ascii_only);
compare_strings (use_quotearg_colon, &custom_results[i].group3,
ascii_only);
}
{
/* Trigger the bug whereby quotearg_buffer would read beyond the NUL
that defines the end of the string being quoted. Use an input
string whose NUL is the last byte before an unreadable page. */
char *z = zerosize_ptr ();
if (z)
{
size_t q_len = 1024;
char *q = malloc (q_len + 1);
char buf[10];
memset (q, 'Q', q_len);
q[q_len] = 0;
/* Z points to the boundary between a readable/writable page
and one that is neither readable nor writable. Position
our string so its NUL is at the end of the writable one. */
char const *str = "____";
size_t s_len = strlen (str);
z -= s_len + 1;
memcpy (z, str, s_len + 1);
set_custom_quoting (NULL, q, q);
/* Whether this actually triggers a SEGV depends on the
implementation of memcmp: whether it compares only byte-at-
a-time, and from left to right (no SEGV) or some other way. */
size_t n = quotearg_buffer (buf, sizeof buf, z, SIZE_MAX, NULL);
ASSERT (n == s_len + 2 * q_len);
ASSERT (memcmp (buf, q, sizeof buf) == 0);
free (q);
}
}
quotearg_free ();
return 0;
}
|