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 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
|
/*
* Copyright (c) 2008-2012 Zmanda, Inc. All Rights Reserved.
* Copyright (c) 2013-2016 Carbonite, Inc. All Rights Reserved.
*
* 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 2
* of the License, 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, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact information: Carbonite Inc., 756 N Pastoria Ave
* Sunnyvale, CA 94085, or: http://www.zmanda.com
*
* Author: Dustin J. Mitchell <dustin@zmanda.com>
*/
#include "amanda.h"
#include "testutils.h"
#include "amutil.h"
/* Utilities */
static char *
safestr(const char *str) {
static char hex[] = "0123456789abcdef";
const char *p;
char *result = malloc(3 + strlen(str) * 3);
char *r = result;
*(r++) = '|';
for (p = str; *p; p++) {
if (isprint((int)*p)) {
*(r++) = *p;
} else {
*(r++) = '#';
*(r++) = hex[((*p)&0xf0) >> 4];
*(r++) = hex[(*p)&0xf];
}
}
*(r++) = '|';
*(r++) = '\0';
return result;
}
char * quotable_strings[] = {
"",
"simple",
"sp a ces",
"\"foo bar\"",
"back\\slash",
"escaped\\ space",
"escaped\\\"quote",
"balanced \"internal\" quotes",
"\"already quoted\" string",
"string that's \"already quoted\"",
"internal\"quote",
"bs-end\\",
"backslash\\nletter",
"backslash\\tletter",
"\t", "\r", "\n", "\f", "\004",
"new\nline",
"newline-end\n",
"ta\tb",
"tab-end\t",
"\\\\\\\\",
"\"",
NULL
};
/****
* Round-trip testing of quoting functions
*/
static gboolean
test_round_trip(void)
{
char **strp;
gboolean success = TRUE;
for (strp = quotable_strings; *strp; strp++) {
char *quoted, *unquoted;
quoted = quote_string(*strp);
unquoted = unquote_string(quoted);
/* if they're not the same, complain */
if (!g_str_equal(*strp, unquoted)) {
char *safe_orig = safestr(*strp);
char *safe_quoted = safestr(quoted);
char *safe_unquoted = safestr(unquoted);
printf(" bad round-trip: %s -quote_string-> %s -unquote_string-> %s\n",
safe_orig, safe_quoted, safe_unquoted);
amfree(safe_orig);
amfree(safe_quoted);
amfree(safe_unquoted);
success = FALSE;
}
amfree(quoted);
amfree(unquoted);
}
return success;
}
/***
* Test that the new split_quoted_strings acts identically to
* the old split(), albeit with a different set of arguments and
* return value. Note that we only test with a delimiter of " ",
* as split() is not used with any other delimiter.
*/
static gboolean
compare_strv(
const char **exp,
char **got,
const char *source,
const char *original)
{
const char **a = exp;
char **b = got;
while (*a && *b) {
if (!g_str_equal(*a, *b))
break;
a++; b++;
}
/* did we exit the loop early, or were they different lengths? */
if (*a || *b) {
char *safe;
safe = safestr(original);
g_printf(" %s: expected [", safe);
amfree(safe);
for (a = exp; *a; a++) {
safe = safestr(*a);
g_printf("%s%s", safe, *(a+1)? ", " : "");
amfree(safe);
}
g_printf("] but got [");
for (b = got; *b; b++) {
safe = safestr(*b);
g_printf("%s%s", safe, *(b+1)? ", " : "");
amfree(safe);
}
g_printf("] using %s.\n", source);
return FALSE;
}
return TRUE;
}
static gboolean
test_split_quoted_strings(void)
{
char **iter1, **iter2, **iter3;
gboolean success = TRUE;
char *middle_strings[] = {
"",
"foo",
"\"foo\"",
"sp aces",
NULL,
};
/* the idea here is to loop over all triples of strings, forming a
* string by quoting them with quote_string and inserting a space, then
* re-splitting with split_quoted_string. This should get us back to our
* starting point. */
for (iter1 = quotable_strings; *iter1; iter1++) {
for (iter2 = middle_strings; *iter2; iter2++) {
for (iter3 = quotable_strings; *iter3; iter3++) {
char *q1 = quote_string(*iter1);
char *q2 = quote_string(*iter2);
char *q3 = quote_string(*iter3);
const char *expected[4] = { *iter1, *iter2, *iter3, NULL };
char *combined = g_strjoin(NULL, q1, " ", q2, " ", q3, NULL);
char **tokens;
tokens = split_quoted_strings(combined);
success = compare_strv(expected, tokens, "split_quoted_strings", combined)
&& success;
amfree(q1);
amfree(q2);
amfree(q3);
amfree(combined);
g_strfreev(tokens);
}
}
}
return success;
}
/****
* Test splitting some edge cases and invalid strings
*/
struct trial {
const char *combined;
const char *expected[5];
};
static gboolean
test_split_quoted_strings_edge(void)
{
gboolean success = TRUE;
struct trial trials[] = {
{ "", { "", NULL, } },
{ " ", { "", "", NULL } },
{ " x", { "", "x", NULL } },
{ "x ", { "x", "", NULL } },
{ "x\\ y", { "x y", NULL } },
{ "\\", { "", NULL } }, /* inv */
{ "z\\", { "z", NULL } }, /* inv */
{ "z\"", { "z", NULL } }, /* inv */
{ "\" \" \"", { " ", "", NULL } }, /* inv */
{ NULL, { NULL, } },
};
struct trial *trial = trials;
while (trial->combined) {
char **tokens = split_quoted_strings(trial->combined);
success = compare_strv(trial->expected, tokens,
"split_quoted_strings", trial->combined)
&& success;
g_strfreev(tokens);
trial++;
}
return success;
}
/****
* Test unquoting of some pathological strings
*/
static gboolean
test_unquote_string(void)
{
gboolean success = TRUE;
char *tests[] = {
"simple", "simple",
"\"quoted\"", "quoted",
"s p a c e", "s p a c e",
/* special escape characters */
"esc \\\" quote", "esc \" quote",
"esc \\t tab", "esc \t tab",
"esc \\\\ esc", "esc \\ esc",
"esc \\02 oct", "esc \02 oct",
"esc \\7 oct", "esc \7 oct",
"esc \\17 oct", "esc \17 oct",
"esc \\117 oct", "esc \117 oct",
"esc \\1117 oct", "esc \1117 oct", /* '7' is distinct char */
/* same, but pre-quoted */
"\"esc \\\" quote\"", "esc \" quote",
"\"esc \\t tab\"", "esc \t tab",
"\"esc \\\\ esc\"", "esc \\ esc",
"\"esc \\02 oct\"", "esc \02 oct",
"\"esc \\7 oct\"", "esc \7 oct",
"\"esc \\17 oct\"", "esc \17 oct",
"\"esc \\117 oct\"", "esc \117 oct",
"\"esc \\1117 oct\"", "esc \1117 oct", /* '7' is distinct char */
/* strips balanced quotes, even inside the string */
">>\"x\"<<", ">>x<<",
">>\"x\"-\"y\"<<", ">>x-y<<",
/* pathological, but valid */
"\\\\", "\\",
"\"\\\"\"", "\"",
"\"\\\\\"", "\\",
"--\\\"", "--\"",
"\\\"--", "\"--",
/* invalid strings (handling here is arbitrary, but these tests
* will alert us if the handling changes) */
"\\", "", /* trailing backslash is ignored */
"xx\\", "xx", /* ditto */
"\\\\\\\\\\\\\\", "\\\\\\", /* ditto */
"\\777", "\377", /* 0777 & 0xff = 0xff */
"\"--", "--", /* leading quote is dropped */
"--\"", "--", /* trailing quote is dropped */
NULL, NULL,
};
char **strp;
for (strp = tests; *strp;) {
char *quoted = *(strp++);
char *expected = *(strp++);
char *unquoted = unquote_string(quoted);
/* if they're not the same, complain */
if (!g_str_equal(expected, unquoted)) {
char *safe_quoted = safestr(quoted);
char *safe_unquoted = safestr(unquoted);
char *safe_expected = safestr(expected);
printf(" %s unquoted to %s; expected %s.\n",
safe_quoted, safe_unquoted, safe_expected);
amfree(safe_quoted);
amfree(safe_unquoted);
amfree(safe_expected);
success = FALSE;
}
amfree(unquoted);
}
return success;
}
/****
* Test the strquotedstr function
*/
static gboolean
test_strquotedstr_skipping(void)
{
char **iter1, **iter2;
gboolean success = TRUE;
/* the idea here is to loop over all pairs of strings, forming a
* string by quoting them with quote_string and inserting a space, then
* re-splitting with strquotedstr. This should get us back to our
* starting point. Note that we have to begin with a non-quoted identifier,
* becuse strquotedstr requires that strtok_r has already been called. */
for (iter1 = quotable_strings; *iter1; iter1++) {
for (iter2 = quotable_strings; *iter2; iter2++) {
char *q1 = quote_string(*iter1);
char *q2 = quote_string(*iter2);
char *combined = g_strjoin(NULL, "START ", q1, " ", q2, NULL);
char *copy = g_strdup(combined);
char *saveptr = NULL;
char *tok;
int i;
tok = strtok_r(copy, " ", &saveptr);
for (i = 1; i <= 2; i++) {
char *expected = (i == 1)? q1:q2;
tok = strquotedstr(&saveptr);
if (!tok) {
g_fprintf(stderr, "while parsing '%s', call %d to strquotedstr returned NULL\n",
combined, i);
success = FALSE;
goto next;
}
if (!g_str_equal(tok, expected)) {
char *safe = safestr(tok);
g_fprintf(stderr, "while parsing '%s', call %d to strquotedstr returned '%s' "
"but '%s' was expected.\n",
combined, i, safe, expected);
amfree(safe);
success = FALSE;
goto next;
}
}
if (strquotedstr(&saveptr) != NULL) {
g_fprintf(stderr, "while parsing '%s', call 3 to strquotedstr did not return NULL\n",
combined);
success = FALSE;
goto next;
}
next:
amfree(q1);
amfree(q2);
amfree(copy);
amfree(combined);
}
}
return success;
}
static gboolean
test_strquotedstr_edge_invalid(void)
{
gboolean success = TRUE;
char *invalid[] = {
"X \"abc", /* unterminated */
"X \"ab cd", /* unterminated second token */
"X a\"b cd", /* unterminated second token with internal quote */
"X b\\", /* trailing backslash */
"X \"b\\", /* trailing backslash in quote */
"X \"b\\\"", /* backslash'd ending quote */
NULL
};
char **iter;
/* run strquotedstr on a bunch of invalid tokens. It should return NULL */
for (iter = invalid; *iter; iter++) {
char *copy = g_strdup(*iter);
char *tok;
char *saveptr = NULL;
tok = strtok_r(copy, " ", &saveptr);
tok = strquotedstr(&saveptr);
if (tok != NULL) {
g_fprintf(stderr, "while parsing invalid '%s', strquotedstr did not return NULL\n",
*iter);
success = FALSE;
}
amfree(copy);
}
return success;
}
static gboolean
test_strquotedstr_edge_valid(void)
{
gboolean success = TRUE;
char *valid[] = {
/* input */ /* expected (omitting "X") */
"X abc\\ def", "abc\\ def", /* backslashed space */
"X \"abc\\ def\"", "\"abc\\ def\"", /* quoted, backslashed space */
"X a\" \"b", "a\" \"b", /* quoted spaces */
NULL, NULL
};
char **iter;
/* run strquotedstr on a bunch of valid, but tricky, tokens. It should return NULL */
for (iter = valid; *iter; iter += 2) {
char *copy = g_strdup(*iter);
char *expected = *(iter+1);
char *tok;
char *saveptr = NULL;
tok = strtok_r(copy, " ", &saveptr);
tok = strquotedstr(&saveptr);
if (tok == NULL) {
g_fprintf(stderr, "while parsing valid '%s', strquotedstr returned NULL\n",
*iter);
success = FALSE;
} else if (!g_str_equal(tok, expected)) {
g_fprintf(stderr, "while parsing valid '%s', strquotedstr returned '%s' while "
"'%s' was expected\n",
*iter, tok, expected);
success = FALSE;
}
amfree(copy);
}
return success;
}
/*
* Main driver
*/
int
main(int argc, char **argv)
{
static TestUtilsTest tests[] = {
TU_TEST(test_round_trip, 90),
TU_TEST(test_unquote_string, 90),
TU_TEST(test_split_quoted_strings, 90),
TU_TEST(test_split_quoted_strings_edge, 90),
TU_TEST(test_strquotedstr_skipping, 90),
TU_TEST(test_strquotedstr_edge_invalid, 90),
TU_TEST(test_strquotedstr_edge_valid, 90),
TU_END()
};
glib_init();
return testutils_run_tests(argc, argv, tests);
}
|