23 #include "gmock/gmock.h" 24 #include "gtest/gtest.h" 34 std::string unescaped;
38 const std::string inputs[] = {
39 std::string(
"foo\nxx\r\b\0023"),
42 std::string(
"\1chad_rules"),
43 std::string(
"\1arnar_drools"),
44 std::string(
"xxxx\r\t'\"\\"),
45 std::string(
"\0xx\0", 4),
46 std::string(
"\x01\x31"),
47 std::string(
"abc\xb\x42\141bc"),
48 std::string(
"123\1\x31\x32\x33"),
49 std::string(
"\xc1\xca\x1b\x62\x19o\xcc\x04"),
51 "\\\"\xe8\xb0\xb7\xe6\xad\x8c\\\" is Google\\\'s Chinese name"),
54 for (
int kind = 0; kind < 4; kind++) {
55 for (
const std::string& original : inputs) {
71 std::string unescaped_str;
73 EXPECT_EQ(unescaped_str, original);
76 std::string s = escaped;
78 ASSERT_EQ(s, original);
83 for (
int char0 = 0; char0 < 256; char0++) {
84 for (
int char1 = 0; char1 < 256; char1++) {
88 std::string s(chars, 2);
90 std::string unescaped;
92 EXPECT_EQ(s, unescaped);
98 epair oct_values[] = {
99 {
"foo\\rbar\\nbaz\\t",
"foo\rbar\nbaz\t"},
100 {
"\\'full of \\\"sound\\\" and \\\"fury\\\"\\'",
101 "'full of \"sound\" and \"fury\"'"},
102 {
"signi\\\\fying\\\\ nothing\\\\",
"signi\\fying\\ nothing\\"},
103 {
"\\010\\t\\n\\013\\014\\r",
"\010\011\012\013\014\015"}
105 epair hex_values[] = {
106 {
"ubik\\rubik\\nubik\\t",
"ubik\rubik\nubik\t"},
107 {
"I\\\'ve just seen a \\\"face\\\"",
108 "I've just seen a \"face\""},
109 {
"hel\\\\ter\\\\skel\\\\ter\\\\",
"hel\\ter\\skel\\ter\\"},
110 {
"\\x08\\t\\n\\x0b\\x0c\\r",
"\010\011\012\013\014\015"}
112 epair utf8_oct_values[] = {
113 {
"\xe8\xb0\xb7\xe6\xad\x8c\\r\xe8\xb0\xb7\xe6\xad\x8c\\nbaz\\t",
114 "\xe8\xb0\xb7\xe6\xad\x8c\r\xe8\xb0\xb7\xe6\xad\x8c\nbaz\t"},
115 {
"\\\"\xe8\xb0\xb7\xe6\xad\x8c\\\" is Google\\\'s Chinese name",
116 "\"\xe8\xb0\xb7\xe6\xad\x8c\" is Google\'s Chinese name"},
117 {
"\xe3\x83\xa1\xe3\x83\xbc\xe3\x83\xab\\\\are\\\\Japanese\\\\chars\\\\",
118 "\xe3\x83\xa1\xe3\x83\xbc\xe3\x83\xab\\are\\Japanese\\chars\\"},
119 {
"\xed\x81\xac\xeb\xa1\xac\\010\\t\\n\\013\\014\\r",
120 "\xed\x81\xac\xeb\xa1\xac\010\011\012\013\014\015"}
122 epair utf8_hex_values[] = {
123 {
"\x20\xe4\xbd\xa0\\t\xe5\xa5\xbd,\\r!\\n",
124 "\x20\xe4\xbd\xa0\t\xe5\xa5\xbd,\r!\n"},
125 {
"\xe8\xa9\xa6\xe9\xa8\x93\\\' means \\\"test\\\"",
126 "\xe8\xa9\xa6\xe9\xa8\x93\' means \"test\""},
127 {
"\\\\\xe6\x88\x91\\\\:\\\\\xe6\x9d\xa8\xe6\xac\xa2\\\\",
128 "\\\xe6\x88\x91\\:\\\xe6\x9d\xa8\xe6\xac\xa2\\"},
129 {
"\xed\x81\xac\xeb\xa1\xac\\x08\\t\\n\\x0b\\x0c\\r",
130 "\xed\x81\xac\xeb\xa1\xac\010\011\012\013\014\015"}
133 for (
const epair& val : oct_values) {
135 EXPECT_EQ(escaped, val.escaped);
137 for (
const epair& val : hex_values) {
139 EXPECT_EQ(escaped, val.escaped);
141 for (
const epair& val : utf8_oct_values) {
143 EXPECT_EQ(escaped, val.escaped);
145 for (
const epair& val : utf8_hex_values) {
147 EXPECT_EQ(escaped, val.escaped);
151 TEST(Unescape, BasicFunction) {
154 {
"\\u00A3",
"\xC2\xA3"},
155 {
"\\u22FD",
"\xE2\x8B\xBD"},
156 {
"\\U00010000",
"\xF0\x90\x80\x80"},
157 {
"\\U0010FFFD",
"\xF4\x8F\xBF\xBD"}};
158 for (
const epair& val : tests) {
161 EXPECT_EQ(out, val.unescaped);
163 std::string bad[] = {
"\\u1",
171 for (
const std::string& e : bad) {
175 EXPECT_FALSE(error.empty());
179 class CUnescapeTest :
public testing::Test {
181 static const char kStringWithMultipleOctalNulls[];
182 static const char kStringWithMultipleHexNulls[];
183 static const char kStringWithMultipleUnicodeNulls[];
185 std::string result_string_;
188 const char CUnescapeTest::kStringWithMultipleOctalNulls[] =
196 const char CUnescapeTest::kStringWithMultipleHexNulls[] =
202 const char CUnescapeTest::kStringWithMultipleUnicodeNulls[] =
207 TEST_F(CUnescapeTest, Unescapes1CharOctalNull) {
208 std::string original_string =
"\\0";
210 EXPECT_EQ(std::string(
"\0", 1), result_string_);
213 TEST_F(CUnescapeTest, Unescapes2CharOctalNull) {
214 std::string original_string =
"\\00";
216 EXPECT_EQ(std::string(
"\0", 1), result_string_);
219 TEST_F(CUnescapeTest, Unescapes3CharOctalNull) {
220 std::string original_string =
"\\000";
222 EXPECT_EQ(std::string(
"\0", 1), result_string_);
225 TEST_F(CUnescapeTest, Unescapes1CharHexNull) {
226 std::string original_string =
"\\x0";
228 EXPECT_EQ(std::string(
"\0", 1), result_string_);
231 TEST_F(CUnescapeTest, Unescapes2CharHexNull) {
232 std::string original_string =
"\\x00";
234 EXPECT_EQ(std::string(
"\0", 1), result_string_);
237 TEST_F(CUnescapeTest, Unescapes3CharHexNull) {
238 std::string original_string =
"\\x000";
240 EXPECT_EQ(std::string(
"\0", 1), result_string_);
243 TEST_F(CUnescapeTest, Unescapes4CharUnicodeNull) {
244 std::string original_string =
"\\u0000";
246 EXPECT_EQ(std::string(
"\0", 1), result_string_);
249 TEST_F(CUnescapeTest, Unescapes8CharUnicodeNull) {
250 std::string original_string =
"\\U00000000";
252 EXPECT_EQ(std::string(
"\0", 1), result_string_);
255 TEST_F(CUnescapeTest, UnescapesMultipleOctalNulls) {
256 std::string original_string(kStringWithMultipleOctalNulls);
260 EXPECT_EQ(std::string(
"\0\n" 269 TEST_F(CUnescapeTest, UnescapesMultipleHexNulls) {
270 std::string original_string(kStringWithMultipleHexNulls);
272 EXPECT_EQ(std::string(
"\0\n" 280 TEST_F(CUnescapeTest, UnescapesMultipleUnicodeNulls) {
281 std::string original_string(kStringWithMultipleUnicodeNulls);
283 EXPECT_EQ(std::string(
"\0\n" 293 }
const base64_tests[] = {
302 {{
"\000", 1},
"AA=="},
303 {{
"\001", 1},
"AQ=="},
304 {{
"\002", 1},
"Ag=="},
305 {{
"\004", 1},
"BA=="},
306 {{
"\010", 1},
"CA=="},
307 {{
"\020", 1},
"EA=="},
308 {{
"\040", 1},
"IA=="},
309 {{
"\100", 1},
"QA=="},
310 {{
"\200", 1},
"gA=="},
312 {{
"\377", 1},
"/w=="},
313 {{
"\376", 1},
"/g=="},
314 {{
"\375", 1},
"/Q=="},
315 {{
"\373", 1},
"+w=="},
316 {{
"\367", 1},
"9w=="},
317 {{
"\357", 1},
"7w=="},
318 {{
"\337", 1},
"3w=="},
319 {{
"\277", 1},
"vw=="},
320 {{
"\177", 1},
"fw=="},
321 {{
"\000\000", 2},
"AAA="},
322 {{
"\000\001", 2},
"AAE="},
323 {{
"\000\002", 2},
"AAI="},
324 {{
"\000\004", 2},
"AAQ="},
325 {{
"\000\010", 2},
"AAg="},
326 {{
"\000\020", 2},
"ABA="},
327 {{
"\000\040", 2},
"ACA="},
328 {{
"\000\100", 2},
"AEA="},
329 {{
"\000\200", 2},
"AIA="},
330 {{
"\001\000", 2},
"AQA="},
331 {{
"\002\000", 2},
"AgA="},
332 {{
"\004\000", 2},
"BAA="},
333 {{
"\010\000", 2},
"CAA="},
334 {{
"\020\000", 2},
"EAA="},
335 {{
"\040\000", 2},
"IAA="},
336 {{
"\100\000", 2},
"QAA="},
337 {{
"\200\000", 2},
"gAA="},
339 {{
"\377\377", 2},
"//8="},
340 {{
"\377\376", 2},
"//4="},
341 {{
"\377\375", 2},
"//0="},
342 {{
"\377\373", 2},
"//s="},
343 {{
"\377\367", 2},
"//c="},
344 {{
"\377\357", 2},
"/+8="},
345 {{
"\377\337", 2},
"/98="},
346 {{
"\377\277", 2},
"/78="},
347 {{
"\377\177", 2},
"/38="},
348 {{
"\376\377", 2},
"/v8="},
349 {{
"\375\377", 2},
"/f8="},
350 {{
"\373\377", 2},
"+/8="},
351 {{
"\367\377", 2},
"9/8="},
352 {{
"\357\377", 2},
"7/8="},
353 {{
"\337\377", 2},
"3/8="},
354 {{
"\277\377", 2},
"v/8="},
355 {{
"\177\377", 2},
"f/8="},
357 {{
"\000\000\000", 3},
"AAAA"},
358 {{
"\000\000\001", 3},
"AAAB"},
359 {{
"\000\000\002", 3},
"AAAC"},
360 {{
"\000\000\004", 3},
"AAAE"},
361 {{
"\000\000\010", 3},
"AAAI"},
362 {{
"\000\000\020", 3},
"AAAQ"},
363 {{
"\000\000\040", 3},
"AAAg"},
364 {{
"\000\000\100", 3},
"AABA"},
365 {{
"\000\000\200", 3},
"AACA"},
366 {{
"\000\001\000", 3},
"AAEA"},
367 {{
"\000\002\000", 3},
"AAIA"},
368 {{
"\000\004\000", 3},
"AAQA"},
369 {{
"\000\010\000", 3},
"AAgA"},
370 {{
"\000\020\000", 3},
"ABAA"},
371 {{
"\000\040\000", 3},
"ACAA"},
372 {{
"\000\100\000", 3},
"AEAA"},
373 {{
"\000\200\000", 3},
"AIAA"},
374 {{
"\001\000\000", 3},
"AQAA"},
375 {{
"\002\000\000", 3},
"AgAA"},
376 {{
"\004\000\000", 3},
"BAAA"},
377 {{
"\010\000\000", 3},
"CAAA"},
378 {{
"\020\000\000", 3},
"EAAA"},
379 {{
"\040\000\000", 3},
"IAAA"},
380 {{
"\100\000\000", 3},
"QAAA"},
381 {{
"\200\000\000", 3},
"gAAA"},
383 {{
"\377\377\377", 3},
"////"},
384 {{
"\377\377\376", 3},
"///+"},
385 {{
"\377\377\375", 3},
"///9"},
386 {{
"\377\377\373", 3},
"///7"},
387 {{
"\377\377\367", 3},
"///3"},
388 {{
"\377\377\357", 3},
"///v"},
389 {{
"\377\377\337", 3},
"///f"},
390 {{
"\377\377\277", 3},
"//+/"},
391 {{
"\377\377\177", 3},
"//9/"},
392 {{
"\377\376\377", 3},
"//7/"},
393 {{
"\377\375\377", 3},
"//3/"},
394 {{
"\377\373\377", 3},
"//v/"},
395 {{
"\377\367\377", 3},
"//f/"},
396 {{
"\377\357\377", 3},
"/+//"},
397 {{
"\377\337\377", 3},
"/9//"},
398 {{
"\377\277\377", 3},
"/7//"},
399 {{
"\377\177\377", 3},
"/3//"},
400 {{
"\376\377\377", 3},
"/v//"},
401 {{
"\375\377\377", 3},
"/f//"},
402 {{
"\373\377\377", 3},
"+///"},
403 {{
"\367\377\377", 3},
"9///"},
404 {{
"\357\377\377", 3},
"7///"},
405 {{
"\337\377\377", 3},
"3///"},
406 {{
"\277\377\377", 3},
"v///"},
407 {{
"\177\377\377", 3},
"f///"},
418 {{
"\243\361", 2},
"o/E="},
419 {{
"\024\167", 2},
"FHc="},
420 {{
"\313\252", 2},
"y6o="},
421 {{
"\046\041", 2},
"JiE="},
422 {{
"\145\236", 2},
"ZZ4="},
423 {{
"\254\325", 2},
"rNU="},
424 {{
"\061\330", 2},
"Mdg="},
425 {{
"\245\032", 2},
"pRo="},
426 {{
"\006\000", 2},
"BgA="},
427 {{
"\375\131", 2},
"/Vk="},
428 {{
"\303\210", 2},
"w4g="},
429 {{
"\040\037", 2},
"IB8="},
430 {{
"\261\372", 2},
"sfo="},
431 {{
"\335\014", 2},
"3Qw="},
432 {{
"\233\217", 2},
"m48="},
433 {{
"\373\056", 2},
"+y4="},
434 {{
"\247\232", 2},
"p5o="},
435 {{
"\107\053", 2},
"Rys="},
436 {{
"\204\077", 2},
"hD8="},
437 {{
"\276\211", 2},
"vok="},
438 {{
"\313\110", 2},
"y0g="},
439 {{
"\363\376", 2},
"8/4="},
440 {{
"\251\234", 2},
"qZw="},
441 {{
"\103\262", 2},
"Q7I="},
442 {{
"\142\312", 2},
"Yso="},
443 {{
"\067\211", 2},
"N4k="},
444 {{
"\220\001", 2},
"kAE="},
445 {{
"\152\240", 2},
"aqA="},
446 {{
"\367\061", 2},
"9zE="},
447 {{
"\133\255", 2},
"W60="},
448 {{
"\176\035", 2},
"fh0="},
449 {{
"\032\231", 2},
"Gpk="},
451 {{
"\013\007\144", 3},
"Cwdk"},
452 {{
"\030\112\106", 3},
"GEpG"},
453 {{
"\047\325\046", 3},
"J9Um"},
454 {{
"\310\160\022", 3},
"yHAS"},
455 {{
"\131\100\237", 3},
"WUCf"},
456 {{
"\064\342\134", 3},
"NOJc"},
457 {{
"\010\177\004", 3},
"CH8E"},
458 {{
"\345\147\205", 3},
"5WeF"},
459 {{
"\300\343\360", 3},
"wOPw"},
460 {{
"\061\240\201", 3},
"MaCB"},
461 {{
"\225\333\044", 3},
"ldsk"},
462 {{
"\215\137\352", 3},
"jV/q"},
463 {{
"\371\147\160", 3},
"+Wdw"},
464 {{
"\030\320\051", 3},
"GNAp"},
465 {{
"\044\174\241", 3},
"JHyh"},
466 {{
"\260\127\037", 3},
"sFcf"},
467 {{
"\111\045\033", 3},
"SSUb"},
468 {{
"\202\114\107", 3},
"gkxH"},
469 {{
"\057\371\042", 3},
"L/ki"},
470 {{
"\223\247\244", 3},
"k6ek"},
471 {{
"\047\216\144", 3},
"J45k"},
472 {{
"\203\070\327", 3},
"gzjX"},
473 {{
"\247\140\072", 3},
"p2A6"},
474 {{
"\124\115\116", 3},
"VE1O"},
475 {{
"\157\162\050", 3},
"b3Io"},
476 {{
"\357\223\004", 3},
"75ME"},
477 {{
"\052\117\156", 3},
"Kk9u"},
478 {{
"\347\154\000", 3},
"52wA"},
479 {{
"\303\012\142", 3},
"wwpi"},
480 {{
"\060\035\362", 3},
"MB3y"},
481 {{
"\130\226\361", 3},
"WJbx"},
482 {{
"\173\013\071", 3},
"ews5"},
483 {{
"\336\004\027", 3},
"3gQX"},
484 {{
"\357\366\234", 3},
"7/ac"},
485 {{
"\353\304\111", 3},
"68RJ"},
486 {{
"\024\264\131", 3},
"FLRZ"},
487 {{
"\075\114\251", 3},
"PUyp"},
488 {{
"\315\031\225", 3},
"zRmV"},
489 {{
"\154\201\276", 3},
"bIG+"},
490 {{
"\200\066\072", 3},
"gDY6"},
491 {{
"\142\350\267", 3},
"Yui3"},
492 {{
"\033\000\166", 3},
"GwB2"},
493 {{
"\210\055\077", 3},
"iC0/"},
494 {{
"\341\037\124", 3},
"4R9U"},
495 {{
"\161\103\152", 3},
"cUNq"},
496 {{
"\270\142\131", 3},
"uGJZ"},
497 {{
"\337\076\074", 3},
"3z48"},
498 {{
"\375\106\362", 3},
"/Uby"},
499 {{
"\227\301\127", 3},
"l8FX"},
500 {{
"\340\002\234", 3},
"4AKc"},
501 {{
"\121\064\033", 3},
"UTQb"},
502 {{
"\157\134\143", 3},
"b1xj"},
503 {{
"\247\055\327", 3},
"py3X"},
504 {{
"\340\142\005", 3},
"4GIF"},
505 {{
"\060\260\143", 3},
"MLBj"},
506 {{
"\075\203\170", 3},
"PYN4"},
507 {{
"\143\160\016", 3},
"Y3AO"},
508 {{
"\313\013\063", 3},
"ywsz"},
509 {{
"\174\236\135", 3},
"fJ5d"},
510 {{
"\103\047\026", 3},
"QycW"},
511 {{
"\365\005\343", 3},
"9QXj"},
512 {{
"\271\160\223", 3},
"uXCT"},
513 {{
"\362\255\172", 3},
"8q16"},
514 {{
"\113\012\015", 3},
"SwoN"},
527 {
"abcd",
"YWJjZA=="},
528 {
"abcde",
"YWJjZGU="},
529 {
"abcdef",
"YWJjZGVm"},
530 {
"abcdefg",
"YWJjZGVmZw=="},
531 {
"abcdefgh",
"YWJjZGVmZ2g="},
532 {
"abcdefghi",
"YWJjZGVmZ2hp"},
533 {
"abcdefghij",
"YWJjZGVmZ2hpag=="},
534 {
"abcdefghijk",
"YWJjZGVmZ2hpams="},
535 {
"abcdefghijkl",
"YWJjZGVmZ2hpamts"},
536 {
"abcdefghijklm",
"YWJjZGVmZ2hpamtsbQ=="},
537 {
"abcdefghijklmn",
"YWJjZGVmZ2hpamtsbW4="},
538 {
"abcdefghijklmno",
"YWJjZGVmZ2hpamtsbW5v"},
539 {
"abcdefghijklmnop",
"YWJjZGVmZ2hpamtsbW5vcA=="},
540 {
"abcdefghijklmnopq",
"YWJjZGVmZ2hpamtsbW5vcHE="},
541 {
"abcdefghijklmnopqr",
"YWJjZGVmZ2hpamtsbW5vcHFy"},
542 {
"abcdefghijklmnopqrs",
"YWJjZGVmZ2hpamtsbW5vcHFycw=="},
543 {
"abcdefghijklmnopqrst",
"YWJjZGVmZ2hpamtsbW5vcHFyc3Q="},
544 {
"abcdefghijklmnopqrstu",
"YWJjZGVmZ2hpamtsbW5vcHFyc3R1"},
545 {
"abcdefghijklmnopqrstuv",
"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dg=="},
546 {
"abcdefghijklmnopqrstuvw",
"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnc="},
547 {
"abcdefghijklmnopqrstuvwx",
"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4"},
548 {
"abcdefghijklmnopqrstuvwxy",
"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eQ=="},
549 {
"abcdefghijklmnopqrstuvwxyz",
"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo="},
552 template <
typename StringType>
553 void TestEscapeAndUnescape() {
555 for (
const auto& tc : base64_tests) {
556 StringType encoded(
"this junk should be ignored");
558 EXPECT_EQ(encoded, tc.cyphertext);
561 StringType decoded(
"this junk should be ignored");
563 EXPECT_EQ(decoded, tc.plaintext);
566 for (
int c = 0; c < websafe.size(); ++c) {
567 if (
'+' == websafe[c]) websafe[c] =
'-';
568 if (
'/' == websafe[c]) websafe[c] =
'_';
569 if (
'=' == websafe[c]) {
575 encoded =
"this junk should be ignored";
577 EXPECT_EQ(encoded, websafe);
581 decoded =
"this junk should be ignored";
583 EXPECT_EQ(decoded, tc.plaintext);
590 EXPECT_EQ(tc.cyphertext, buffer);
602 EXPECT_TRUE(buf.empty());
607 TEST(Base64, EscapeAndUnescape) {
608 TestEscapeAndUnescape<std::string>();
611 TEST(Base64, DISABLED_HugeData) {
612 const size_t kSize = size_t(3) * 1000 * 1000 * 1000;
613 static_assert(kSize % 3 == 0,
"kSize must be divisible by 3");
614 const std::string huge(kSize,
'x');
621 std::string expected_encoding;
622 expected_encoding.reserve(kSize / 3 * 4);
623 for (
size_t i = 0;
i < kSize / 3; ++
i) {
624 expected_encoding.append(
"eHh4");
626 EXPECT_EQ(expected_encoding, escaped);
628 std::string unescaped;
630 EXPECT_EQ(huge, unescaped);
633 TEST(HexAndBack, HexStringToBytes_and_BytesToHexString) {
634 std::string hex_mixed =
"0123456789abcdefABCDEF";
635 std::string bytes_expected =
"\x01\x23\x45\x67\x89\xab\xcd\xef\xAB\xCD\xEF";
636 std::string hex_only_lower =
"0123456789abcdefabcdef";
639 EXPECT_EQ(bytes_expected, bytes_result);
641 std::string prefix_valid = hex_mixed +
"?";
644 EXPECT_EQ(bytes_expected, prefix_valid_result);
646 std::string infix_valid =
"?" + hex_mixed +
"???";
649 EXPECT_EQ(bytes_expected, infix_valid_result);
652 EXPECT_EQ(hex_only_lower, hex_result);
std::string Utf8SafeCEscape(absl::string_view src)
const std::array< base64_testcase, 5 > & base64_strings()
std::string CEscape(absl::string_view src)
std::string Utf8SafeCHexEscape(absl::string_view src)
std::string BytesToHexString(absl::string_view from)
bool WebSafeBase64Unescape(absl::string_view src, std::string *dest)
std::string CHexEscape(absl::string_view src)
void WebSafeBase64Escape(absl::string_view src, std::string *dest)
TEST_F(GraphCyclesTest, NoCycle)
void Base64Escape(absl::string_view src, std::string *dest)
std::string HexStringToBytes(absl::string_view from)
bool Base64Unescape(absl::string_view src, std::string *dest)
bool CUnescape(absl::string_view source, std::string *dest, std::string *error)
TEST(Symbolize, Unimplemented)