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
|
#include "wvtest.h"
#include "wvtclstring.h"
#include "wvstring.h"
#include "wvstringlist.h"
#include "wvstringmask.h"
#include "wvstream.h"
#include <signal.h>
WVTEST_MAIN("escaping and unescaping")
{
WvString a, b(""), c("jabba"), d("crab poody-doo"), e("\"quote this!\""),
f("pooky{doo"), g("big}monkey {potatoes"), h("hammer\\}time"),
i("shameless{frog}parts"), j("wagloo-mas\nuffle"),
k("nasty{bad{}}}$break"), result, desired;
//null
result = wvtcl_escape(a);
WVFAIL(result);
result = wvtcl_unescape(result);
WVPASSEQ(result, a);
result = wvtcl_escape(b);
desired = WvString("{}");
WVPASSEQ(result, desired);
result = wvtcl_unescape(result);
WVPASSEQ(result, b);
//no escape required
result = wvtcl_escape(c);
WVPASSEQ(result, c);
result = wvtcl_unescape(result);
WVPASSEQ(result, c);
//bracket escape required
WVPASSEQ(wvtcl_escape(" foo"), "{ foo}");
WVPASSEQ(wvtcl_escape("foo "), "{foo }");
result = wvtcl_escape(d);
desired = WvString("{%s}", d.cstr());
if (!WVPASS(result == desired))
printf(" because [%s] != [%s]\n", result.cstr(), desired.cstr());
result = wvtcl_unescape(result);
WVPASS(result == d);
result = wvtcl_escape(e);
desired = WvString("{\"quote this!\"}");
if (!WVPASS(result == desired))
printf(" because [%s] != [%s]\n", result.cstr(), desired.cstr());
result = wvtcl_unescape(WvString("\"quote this!\""));
desired = WvString("quote this!");
if (!WVPASS(result == desired))
printf(" because [%s] != [%s]\n", result.cstr(), desired.cstr());
result = wvtcl_escape(i);
desired = WvString("{shameless{frog}parts}");
if (!WVPASS(result == desired))
printf(" because [%s] != [%s]\n", result.cstr(), desired.cstr());
result = wvtcl_unescape(result);
if (!WVPASS(result == i))
printf(" because [%s] != [%s]\n", result.cstr(), i.cstr());
result = wvtcl_escape(h);
desired = WvString("{hammer\\}time}");
printf("%s\n", result.cstr());
if (!WVPASS(result == desired))
printf(" because [%s] != [%s]\n", result.cstr(), desired.cstr());
result = wvtcl_unescape(result);
if (!WVPASS(result == h))
printf(" because [%s] != [%s]\n", result.cstr(), h.cstr());
//\ escaping required
result = wvtcl_escape(f);
desired = WvString("pooky\\{doo");
if (!WVPASS(result == desired))
printf(" because [%s] != [%s]\n", result.cstr(), desired.cstr());
result = wvtcl_unescape(result);
if (!WVPASS(result == f))
printf(" because [%s] != [%s]\n", result.cstr(), f.cstr());
result = wvtcl_escape(g);
desired = WvString("big\\}monkey\\ \\{potatoes");
if (!WVPASS(result == desired))
printf(" because [%s] != [%s]\n", result.cstr(), desired.cstr());
result = wvtcl_unescape(result);
if (!WVPASS(result == g))
printf(" because [%s] != [%s]\n", result.cstr(), g.cstr());
//escaping with our own nasties
result = wvtcl_escape(j, WvStringMask("o-\nu"));
desired = WvString("{wagloo-mas\nuffle}");
if (!WVPASS(result == desired))
printf(" because [%s] != [%s]\n", result.cstr(), desired.cstr());
result = wvtcl_unescape(result);
if (!WVPASS(result == j))
printf(" because [%s] != [%s]\n", result.cstr(), j.cstr());
result = wvtcl_escape(k, WvStringMask("$ky"));
desired = WvString("nast\\y\\{bad\\{\\}\\}\\}\\$brea\\k");
if (!WVPASS(result == desired))
printf(" because [%s] != [%s]\n", result.cstr(), desired.cstr());
result = wvtcl_unescape(result);
printf("%s\n", result.cstr());
if (!WVPASS(result == k))
printf(" because [%s] != [%s]\n", result.cstr(), k.cstr());
}
WVTEST_MAIN("encoding and decoding")
{
signal(SIGALRM, SIG_IGN);
WvString a("jabba"), b("crab poody-doo"), c("pooky{doo"), result, desired;
WvStringList list;
list.append(&a, false);
list.append(&b, false);
list.append(&c, false);
list.append(new WvString(), true);
result = wvtcl_encode(list, WvStringMask(' '), WvStringMask(' '));
desired = WvString("%s {%s} pooky\\{doo ", a, b);
if (!WVPASS(result == desired))
printf(" because [%s] != [%s]\n", result.cstr(), desired.cstr());
wvtcl_decode(list, result);
result = *list.first();
list.unlink_first();
if (!WVPASS(result == a))
printf(" because [%s] != [%s]\n", result.cstr(), a.cstr());
result = *list.first();
list.unlink_first();
if (!WVPASS(result == b))
printf(" because [%s] != [%s]\n", result.cstr(), b.cstr());
result = *list.first();
list.unlink_first();
if (!WVPASS(result == c))
printf(" because [%s] != [%s]\n", result.cstr(), c.cstr());
result = *list.first();
WVFAIL(result);
list.unlink_first();
}
WVTEST_MAIN("getword")
{
WvDynBuf buf;
WvString result, desired, test("jabba {crab poody-doo} pooky\\{doo");
// set buffer
buf.putstr(test);
// don't unescape
result = wvtcl_getword(buf, WvStringMask(" "), false);
desired = WvString("jabba");
if (!WVPASS(result == desired))
printf(" because [%s] != [%s]\n", result.cstr(), desired.cstr());
// buffer should be updated properly
result = buf.getstr();
desired = WvString(" {crab poody-doo} pooky\\{doo");
if (!WVPASS(result == desired))
printf(" because [%s] != [%s]\n", result.cstr(), desired.cstr());
// reset buffer
buf.putstr(test);
// unescape
result = wvtcl_getword(buf, WvStringMask(" "));
desired = WvString("jabba");
if (!WVPASS(result == desired))
printf(" because [%s] != [%s]\n", result.cstr(), desired.cstr());
// buffer should be updated properly
result = wvtcl_getword(buf, WvStringMask(" "));
desired = WvString("crab poody-doo");
if (!WVPASS(result == desired))
printf(" because [%s] != [%s]\n", result.cstr(), desired.cstr());
result = buf.getstr();
desired = WvString(" pooky\\{doo");
if (!WVPASS(result == desired))
printf(" because [%s] != [%s]\n", result.cstr(), desired.cstr());
// test no word possible
test = WvString("---{incomplete-");
buf.putstr(test);
result = wvtcl_getword(buf, WvStringMask("-"));
// should return null
WVFAIL(result);
result = buf.getstr();
if (!WVPASS(result == test))
printf(" because [%s] != [%s]\n", result.cstr(), test.cstr());
// test no word possible and whitespace eating
test = WvString(" ");
buf.putstr(test);
result = wvtcl_getword(buf, WvStringMask(" "));
// should return null
WVFAIL(result);
result = buf.getstr();
// buffer should be reset to original state, ie unchanged
if (!WVPASS(result == test))
printf(" because [%s] != [%s]\n", result.cstr(), test.cstr());
}
static void _do_word(WvBuf &buf, WvStringParm word, size_t expect)
{
WvString new_word = wvtcl_getword(buf, WVTCL_NASTY_NEWLINES);
WVPASSEQ(buf.used(), expect);
WVPASSEQ(word, new_word);
}
static void do_word(WvBuf &buf, WvStringParm word, size_t expect)
{
buf.putstr("%s\n", word);
_do_word(buf, word, expect);
}
WVTEST_MAIN("getword with dynamic buffer")
{
WvDynBuf buf;
WvString word;
buf.putstr("\n\n\n");
do_word(buf, "foo", 1);
buf.zap();
do_word(buf, "n = 6", 1);
do_word(buf, "", 2); // left old newline *and* new newline
do_word(buf, "[S1]", 1);
do_word(buf, "a = b", 1);
do_word(buf, "", 2);
do_word(buf, "[S2]", 1);
do_word(buf, "Enable = 0", 1);
}
WVTEST_MAIN("wvtcl_encode specific failures")
{
WvStringList words;
wvout->print(wvtcl_encode(words));
words.append("{{2304 5816322} var} 1102878961 DIR");
words.append("{{2304 5816323} lib} 1098721377 DIR");
words.append("{{2304 5852807} defoma} 1098721283 DIR");
words.append("{{2304 5852904} gs.d} 1098721283 DIR");
words.append("{{2304 5852905} dirs} 1098721269 DIR");
words.append("{{2304 5852906} fonts} 1098721283 DIR");
words.append("{{2304 5853099} Fontmap} 1098721283 REG");
wvout->print(wvtcl_encode(words));
}
WVTEST_MAIN("mismatched braces")
{
WvDynBuf buf;
buf.putstr("close}\n{brace}\n{me\n");
_do_word(buf, "close}", 13); // first closebrace isn't "special"
_do_word(buf, "brace", 5); // second word is in braces
_do_word(buf, WvString(), 5); // last word is incomplete
}
WVTEST_MAIN("backslashed braces")
{
WvString line("VAL a b\\}\\\n\\{c\\}\\ =\\ d\\\ne\\ =\\ f\\{");
WvString encoded("%s\n", line);
WvString word1("VAL"), word2("a"), word3("b}\n{c} = d\ne = f{");
WvDynBuf buf;
buf.putstr(encoded);
WVPASSEQ(buf.used(), 34);
WvString oline = wvtcl_getword(buf, WVTCL_NASTY_NEWLINES, false);
WVPASSEQ(line, oline);
WVPASSEQ(buf.used(), 1); // trailing newline
buf.zap();
buf.putstr(line);
WvString w1 = wvtcl_getword(buf, WvStringMask(" "));
WvString w2 = wvtcl_getword(buf, WvStringMask(" "));
WvString w3 = wvtcl_getword(buf, WvStringMask(" "));
WVPASSEQ(w1, word1);
WVPASSEQ(w2, word2);
WVPASSEQ(w3, word3);
}
WVTEST_MAIN("BUGZID:17077")
{
const char *dirname = "directory name ending in backslash \\";
WvString encoded_dirname = wvtcl_escape(dirname);
WvDynBuf buf;
buf.putstr(encoded_dirname);
WVPASSEQ(wvtcl_getword(buf, WVTCL_NASTY_NEWLINES, false), encoded_dirname);
}
static void tcltest(const char *a, const char *b)
{
if (strcmp(a, b) != 0)
{
fprintf(stderr, "\n");
WVPASSEQ(a, b); // always fails
}
else
fprintf(stderr, "."); // otherwise just print a progress dot
}
WVTEST_MAIN("wvtcl_getword comprehensive nounescape")
{
WvDynBuf buf;
const int slen = 4;
char str[slen+1], last[slen+1] = "goo";
struct
{
const char *chars;
const WvStringMask *mask;
} const *test_set, test_sets[] = {
{ "a{}\\\"", &WVTCL_NASTY_SPACES },
{ "a {}\\\"", &WVTCL_NASTY_NEWLINES },
{ NULL, NULL }
};
for (test_set = &test_sets[0]; test_set->chars; ++test_set)
{
const WvStringMask &mask = *test_set->mask;
const char *chars = test_set->chars;
const int len = strlen(chars) + 1;
for (int i=0; i<len; ++i)
{
str[0] = chars[i];
if (mask[str[0]]) continue;
for (int j=0; j<len; ++j)
{
fprintf(stderr, "\n%d,%d: ", i, j);
str[1] = chars[j];
if (mask[str[1]]) continue;
for (int k=0; k<len; ++k)
{
str[2] = chars[k];
if (mask[str[2]]) continue;
for (int l=0; l<len; ++l)
{
str[3] = chars[l];
if (mask[str[3]]) continue;
str[slen] = '\0';
if (strcmp(last, str) == 0)
continue;
WvString estr = wvtcl_escape(str, mask);
tcltest(wvtcl_unescape(estr), str);
buf.putstr(estr);
tcltest(wvtcl_getword(buf, mask, false), estr);
buf.zap();
WvString eestr = wvtcl_escape(estr, mask);
tcltest(wvtcl_unescape(eestr), estr);
buf.putstr(eestr);
tcltest(wvtcl_getword(buf, mask, false), eestr);
buf.zap();
strcpy(last, str);
}
}
}
}
}
fprintf(stderr, "\n");
}
|