escaping_test.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/strings/escaping.h"
00016 
00017 #include <array>
00018 #include <cstdio>
00019 #include <cstring>
00020 #include <memory>
00021 #include <vector>
00022 
00023 #include "gmock/gmock.h"
00024 #include "gtest/gtest.h"
00025 #include "absl/container/fixed_array.h"
00026 #include "absl/strings/str_cat.h"
00027 
00028 #include "absl/strings/internal/escaping_test_common.h"
00029 
00030 namespace {
00031 
00032 struct epair {
00033   std::string escaped;
00034   std::string unescaped;
00035 };
00036 
00037 TEST(CEscape, EscapeAndUnescape) {
00038   const std::string inputs[] = {
00039       std::string("foo\nxx\r\b\0023"),
00040       std::string(""),
00041       std::string("abc"),
00042       std::string("\1chad_rules"),
00043       std::string("\1arnar_drools"),
00044       std::string("xxxx\r\t'\"\\"),
00045       std::string("\0xx\0", 4),
00046       std::string("\x01\x31"),
00047       std::string("abc\xb\x42\141bc"),
00048       std::string("123\1\x31\x32\x33"),
00049       std::string("\xc1\xca\x1b\x62\x19o\xcc\x04"),
00050       std::string(
00051           "\\\"\xe8\xb0\xb7\xe6\xad\x8c\\\" is Google\\\'s Chinese name"),
00052   };
00053   // Do this twice, once for octal escapes and once for hex escapes.
00054   for (int kind = 0; kind < 4; kind++) {
00055     for (const std::string& original : inputs) {
00056       std::string escaped;
00057       switch (kind) {
00058         case 0:
00059           escaped = absl::CEscape(original);
00060           break;
00061         case 1:
00062           escaped = absl::CHexEscape(original);
00063           break;
00064         case 2:
00065           escaped = absl::Utf8SafeCEscape(original);
00066           break;
00067         case 3:
00068           escaped = absl::Utf8SafeCHexEscape(original);
00069           break;
00070       }
00071       std::string unescaped_str;
00072       EXPECT_TRUE(absl::CUnescape(escaped, &unescaped_str));
00073       EXPECT_EQ(unescaped_str, original);
00074 
00075       // Check in-place unescaping
00076       std::string s = escaped;
00077       EXPECT_TRUE(absl::CUnescape(s, &s));
00078       ASSERT_EQ(s, original);
00079     }
00080   }
00081   // Check that all possible two character strings can be escaped then
00082   // unescaped successfully.
00083   for (int char0 = 0; char0 < 256; char0++) {
00084     for (int char1 = 0; char1 < 256; char1++) {
00085       char chars[2];
00086       chars[0] = char0;
00087       chars[1] = char1;
00088       std::string s(chars, 2);
00089       std::string escaped = absl::CHexEscape(s);
00090       std::string unescaped;
00091       EXPECT_TRUE(absl::CUnescape(escaped, &unescaped));
00092       EXPECT_EQ(s, unescaped);
00093     }
00094   }
00095 }
00096 
00097 TEST(CEscape, BasicEscaping) {
00098   epair oct_values[] = {
00099       {"foo\\rbar\\nbaz\\t", "foo\rbar\nbaz\t"},
00100       {"\\'full of \\\"sound\\\" and \\\"fury\\\"\\'",
00101        "'full of \"sound\" and \"fury\"'"},
00102       {"signi\\\\fying\\\\ nothing\\\\", "signi\\fying\\ nothing\\"},
00103       {"\\010\\t\\n\\013\\014\\r", "\010\011\012\013\014\015"}
00104   };
00105   epair hex_values[] = {
00106       {"ubik\\rubik\\nubik\\t", "ubik\rubik\nubik\t"},
00107       {"I\\\'ve just seen a \\\"face\\\"",
00108        "I've just seen a \"face\""},
00109       {"hel\\\\ter\\\\skel\\\\ter\\\\", "hel\\ter\\skel\\ter\\"},
00110       {"\\x08\\t\\n\\x0b\\x0c\\r", "\010\011\012\013\014\015"}
00111   };
00112   epair utf8_oct_values[] = {
00113       {"\xe8\xb0\xb7\xe6\xad\x8c\\r\xe8\xb0\xb7\xe6\xad\x8c\\nbaz\\t",
00114        "\xe8\xb0\xb7\xe6\xad\x8c\r\xe8\xb0\xb7\xe6\xad\x8c\nbaz\t"},
00115       {"\\\"\xe8\xb0\xb7\xe6\xad\x8c\\\" is Google\\\'s Chinese name",
00116        "\"\xe8\xb0\xb7\xe6\xad\x8c\" is Google\'s Chinese name"},
00117       {"\xe3\x83\xa1\xe3\x83\xbc\xe3\x83\xab\\\\are\\\\Japanese\\\\chars\\\\",
00118        "\xe3\x83\xa1\xe3\x83\xbc\xe3\x83\xab\\are\\Japanese\\chars\\"},
00119       {"\xed\x81\xac\xeb\xa1\xac\\010\\t\\n\\013\\014\\r",
00120        "\xed\x81\xac\xeb\xa1\xac\010\011\012\013\014\015"}
00121   };
00122   epair utf8_hex_values[] = {
00123       {"\x20\xe4\xbd\xa0\\t\xe5\xa5\xbd,\\r!\\n",
00124        "\x20\xe4\xbd\xa0\t\xe5\xa5\xbd,\r!\n"},
00125       {"\xe8\xa9\xa6\xe9\xa8\x93\\\' means \\\"test\\\"",
00126        "\xe8\xa9\xa6\xe9\xa8\x93\' means \"test\""},
00127       {"\\\\\xe6\x88\x91\\\\:\\\\\xe6\x9d\xa8\xe6\xac\xa2\\\\",
00128        "\\\xe6\x88\x91\\:\\\xe6\x9d\xa8\xe6\xac\xa2\\"},
00129       {"\xed\x81\xac\xeb\xa1\xac\\x08\\t\\n\\x0b\\x0c\\r",
00130        "\xed\x81\xac\xeb\xa1\xac\010\011\012\013\014\015"}
00131   };
00132 
00133   for (const epair& val : oct_values) {
00134     std::string escaped = absl::CEscape(val.unescaped);
00135     EXPECT_EQ(escaped, val.escaped);
00136   }
00137   for (const epair& val : hex_values) {
00138     std::string escaped = absl::CHexEscape(val.unescaped);
00139     EXPECT_EQ(escaped, val.escaped);
00140   }
00141   for (const epair& val : utf8_oct_values) {
00142     std::string escaped = absl::Utf8SafeCEscape(val.unescaped);
00143     EXPECT_EQ(escaped, val.escaped);
00144   }
00145   for (const epair& val : utf8_hex_values) {
00146     std::string escaped = absl::Utf8SafeCHexEscape(val.unescaped);
00147     EXPECT_EQ(escaped, val.escaped);
00148   }
00149 }
00150 
00151 TEST(Unescape, BasicFunction) {
00152   epair tests[] =
00153     {{"\\u0030", "0"},
00154      {"\\u00A3", "\xC2\xA3"},
00155      {"\\u22FD", "\xE2\x8B\xBD"},
00156      {"\\U00010000", "\xF0\x90\x80\x80"},
00157      {"\\U0010FFFD", "\xF4\x8F\xBF\xBD"}};
00158   for (const epair& val : tests) {
00159     std::string out;
00160     EXPECT_TRUE(absl::CUnescape(val.escaped, &out));
00161     EXPECT_EQ(out, val.unescaped);
00162   }
00163   std::string bad[] = {"\\u1",         // too short
00164                        "\\U1",         // too short
00165                        "\\Uffffff",    // exceeds 0x10ffff (largest Unicode)
00166                        "\\U00110000",  // exceeds 0x10ffff (largest Unicode)
00167                        "\\uD835",      // surrogate character (D800-DFFF)
00168                        "\\U0000DD04",  // surrogate character (D800-DFFF)
00169                        "\\777",        // exceeds 0xff
00170                        "\\xABCD"};     // exceeds 0xff
00171   for (const std::string& e : bad) {
00172     std::string error;
00173     std::string out;
00174     EXPECT_FALSE(absl::CUnescape(e, &out, &error));
00175     EXPECT_FALSE(error.empty());
00176   }
00177 }
00178 
00179 class CUnescapeTest : public testing::Test {
00180  protected:
00181   static const char kStringWithMultipleOctalNulls[];
00182   static const char kStringWithMultipleHexNulls[];
00183   static const char kStringWithMultipleUnicodeNulls[];
00184 
00185   std::string result_string_;
00186 };
00187 
00188 const char CUnescapeTest::kStringWithMultipleOctalNulls[] =
00189     "\\0\\n"    // null escape \0 plus newline
00190     "0\\n"      // just a number 0 (not a null escape) plus newline
00191     "\\00\\12"  // null escape \00 plus octal newline code
00192     "\\000";    // null escape \000
00193 
00194 // This has the same ingredients as kStringWithMultipleOctalNulls
00195 // but with \x hex escapes instead of octal escapes.
00196 const char CUnescapeTest::kStringWithMultipleHexNulls[] =
00197     "\\x0\\n"
00198     "0\\n"
00199     "\\x00\\xa"
00200     "\\x000";
00201 
00202 const char CUnescapeTest::kStringWithMultipleUnicodeNulls[] =
00203     "\\u0000\\n"    // short-form (4-digit) null escape plus newline
00204     "0\\n"          // just a number 0 (not a null escape) plus newline
00205     "\\U00000000";  // long-form (8-digit) null escape
00206 
00207 TEST_F(CUnescapeTest, Unescapes1CharOctalNull) {
00208   std::string original_string = "\\0";
00209   EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
00210   EXPECT_EQ(std::string("\0", 1), result_string_);
00211 }
00212 
00213 TEST_F(CUnescapeTest, Unescapes2CharOctalNull) {
00214   std::string original_string = "\\00";
00215   EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
00216   EXPECT_EQ(std::string("\0", 1), result_string_);
00217 }
00218 
00219 TEST_F(CUnescapeTest, Unescapes3CharOctalNull) {
00220   std::string original_string = "\\000";
00221   EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
00222   EXPECT_EQ(std::string("\0", 1), result_string_);
00223 }
00224 
00225 TEST_F(CUnescapeTest, Unescapes1CharHexNull) {
00226   std::string original_string = "\\x0";
00227   EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
00228   EXPECT_EQ(std::string("\0", 1), result_string_);
00229 }
00230 
00231 TEST_F(CUnescapeTest, Unescapes2CharHexNull) {
00232   std::string original_string = "\\x00";
00233   EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
00234   EXPECT_EQ(std::string("\0", 1), result_string_);
00235 }
00236 
00237 TEST_F(CUnescapeTest, Unescapes3CharHexNull) {
00238   std::string original_string = "\\x000";
00239   EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
00240   EXPECT_EQ(std::string("\0", 1), result_string_);
00241 }
00242 
00243 TEST_F(CUnescapeTest, Unescapes4CharUnicodeNull) {
00244   std::string original_string = "\\u0000";
00245   EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
00246   EXPECT_EQ(std::string("\0", 1), result_string_);
00247 }
00248 
00249 TEST_F(CUnescapeTest, Unescapes8CharUnicodeNull) {
00250   std::string original_string = "\\U00000000";
00251   EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
00252   EXPECT_EQ(std::string("\0", 1), result_string_);
00253 }
00254 
00255 TEST_F(CUnescapeTest, UnescapesMultipleOctalNulls) {
00256   std::string original_string(kStringWithMultipleOctalNulls);
00257   EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
00258   // All escapes, including newlines and null escapes, should have been
00259   // converted to the equivalent characters.
00260   EXPECT_EQ(std::string("\0\n"
00261                         "0\n"
00262                         "\0\n"
00263                         "\0",
00264                         7),
00265             result_string_);
00266 }
00267 
00268 
00269 TEST_F(CUnescapeTest, UnescapesMultipleHexNulls) {
00270   std::string original_string(kStringWithMultipleHexNulls);
00271   EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
00272   EXPECT_EQ(std::string("\0\n"
00273                         "0\n"
00274                         "\0\n"
00275                         "\0",
00276                         7),
00277             result_string_);
00278 }
00279 
00280 TEST_F(CUnescapeTest, UnescapesMultipleUnicodeNulls) {
00281   std::string original_string(kStringWithMultipleUnicodeNulls);
00282   EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
00283   EXPECT_EQ(std::string("\0\n"
00284                         "0\n"
00285                         "\0",
00286                         5),
00287             result_string_);
00288 }
00289 
00290 static struct {
00291   absl::string_view plaintext;
00292   absl::string_view cyphertext;
00293 } const base64_tests[] = {
00294     // Empty std::string.
00295     {{"", 0}, {"", 0}},
00296     {{nullptr, 0},
00297      {"", 0}},  // if length is zero, plaintext ptr must be ignored!
00298 
00299     // Basic bit patterns;
00300     // values obtained with "echo -n '...' | uuencode -m test"
00301 
00302     {{"\000", 1}, "AA=="},
00303     {{"\001", 1}, "AQ=="},
00304     {{"\002", 1}, "Ag=="},
00305     {{"\004", 1}, "BA=="},
00306     {{"\010", 1}, "CA=="},
00307     {{"\020", 1}, "EA=="},
00308     {{"\040", 1}, "IA=="},
00309     {{"\100", 1}, "QA=="},
00310     {{"\200", 1}, "gA=="},
00311 
00312     {{"\377", 1}, "/w=="},
00313     {{"\376", 1}, "/g=="},
00314     {{"\375", 1}, "/Q=="},
00315     {{"\373", 1}, "+w=="},
00316     {{"\367", 1}, "9w=="},
00317     {{"\357", 1}, "7w=="},
00318     {{"\337", 1}, "3w=="},
00319     {{"\277", 1}, "vw=="},
00320     {{"\177", 1}, "fw=="},
00321     {{"\000\000", 2}, "AAA="},
00322     {{"\000\001", 2}, "AAE="},
00323     {{"\000\002", 2}, "AAI="},
00324     {{"\000\004", 2}, "AAQ="},
00325     {{"\000\010", 2}, "AAg="},
00326     {{"\000\020", 2}, "ABA="},
00327     {{"\000\040", 2}, "ACA="},
00328     {{"\000\100", 2}, "AEA="},
00329     {{"\000\200", 2}, "AIA="},
00330     {{"\001\000", 2}, "AQA="},
00331     {{"\002\000", 2}, "AgA="},
00332     {{"\004\000", 2}, "BAA="},
00333     {{"\010\000", 2}, "CAA="},
00334     {{"\020\000", 2}, "EAA="},
00335     {{"\040\000", 2}, "IAA="},
00336     {{"\100\000", 2}, "QAA="},
00337     {{"\200\000", 2}, "gAA="},
00338 
00339     {{"\377\377", 2}, "//8="},
00340     {{"\377\376", 2}, "//4="},
00341     {{"\377\375", 2}, "//0="},
00342     {{"\377\373", 2}, "//s="},
00343     {{"\377\367", 2}, "//c="},
00344     {{"\377\357", 2}, "/+8="},
00345     {{"\377\337", 2}, "/98="},
00346     {{"\377\277", 2}, "/78="},
00347     {{"\377\177", 2}, "/38="},
00348     {{"\376\377", 2}, "/v8="},
00349     {{"\375\377", 2}, "/f8="},
00350     {{"\373\377", 2}, "+/8="},
00351     {{"\367\377", 2}, "9/8="},
00352     {{"\357\377", 2}, "7/8="},
00353     {{"\337\377", 2}, "3/8="},
00354     {{"\277\377", 2}, "v/8="},
00355     {{"\177\377", 2}, "f/8="},
00356 
00357     {{"\000\000\000", 3}, "AAAA"},
00358     {{"\000\000\001", 3}, "AAAB"},
00359     {{"\000\000\002", 3}, "AAAC"},
00360     {{"\000\000\004", 3}, "AAAE"},
00361     {{"\000\000\010", 3}, "AAAI"},
00362     {{"\000\000\020", 3}, "AAAQ"},
00363     {{"\000\000\040", 3}, "AAAg"},
00364     {{"\000\000\100", 3}, "AABA"},
00365     {{"\000\000\200", 3}, "AACA"},
00366     {{"\000\001\000", 3}, "AAEA"},
00367     {{"\000\002\000", 3}, "AAIA"},
00368     {{"\000\004\000", 3}, "AAQA"},
00369     {{"\000\010\000", 3}, "AAgA"},
00370     {{"\000\020\000", 3}, "ABAA"},
00371     {{"\000\040\000", 3}, "ACAA"},
00372     {{"\000\100\000", 3}, "AEAA"},
00373     {{"\000\200\000", 3}, "AIAA"},
00374     {{"\001\000\000", 3}, "AQAA"},
00375     {{"\002\000\000", 3}, "AgAA"},
00376     {{"\004\000\000", 3}, "BAAA"},
00377     {{"\010\000\000", 3}, "CAAA"},
00378     {{"\020\000\000", 3}, "EAAA"},
00379     {{"\040\000\000", 3}, "IAAA"},
00380     {{"\100\000\000", 3}, "QAAA"},
00381     {{"\200\000\000", 3}, "gAAA"},
00382 
00383     {{"\377\377\377", 3}, "////"},
00384     {{"\377\377\376", 3}, "///+"},
00385     {{"\377\377\375", 3}, "///9"},
00386     {{"\377\377\373", 3}, "///7"},
00387     {{"\377\377\367", 3}, "///3"},
00388     {{"\377\377\357", 3}, "///v"},
00389     {{"\377\377\337", 3}, "///f"},
00390     {{"\377\377\277", 3}, "//+/"},
00391     {{"\377\377\177", 3}, "//9/"},
00392     {{"\377\376\377", 3}, "//7/"},
00393     {{"\377\375\377", 3}, "//3/"},
00394     {{"\377\373\377", 3}, "//v/"},
00395     {{"\377\367\377", 3}, "//f/"},
00396     {{"\377\357\377", 3}, "/+//"},
00397     {{"\377\337\377", 3}, "/9//"},
00398     {{"\377\277\377", 3}, "/7//"},
00399     {{"\377\177\377", 3}, "/3//"},
00400     {{"\376\377\377", 3}, "/v//"},
00401     {{"\375\377\377", 3}, "/f//"},
00402     {{"\373\377\377", 3}, "+///"},
00403     {{"\367\377\377", 3}, "9///"},
00404     {{"\357\377\377", 3}, "7///"},
00405     {{"\337\377\377", 3}, "3///"},
00406     {{"\277\377\377", 3}, "v///"},
00407     {{"\177\377\377", 3}, "f///"},
00408 
00409     // Random numbers: values obtained with
00410     //
00411     //  #! /bin/bash
00412     //  dd bs=$1 count=1 if=/dev/random of=/tmp/bar.random
00413     //  od -N $1 -t o1 /tmp/bar.random
00414     //  uuencode -m test < /tmp/bar.random
00415     //
00416     // where $1 is the number of bytes (2, 3)
00417 
00418     {{"\243\361", 2}, "o/E="},
00419     {{"\024\167", 2}, "FHc="},
00420     {{"\313\252", 2}, "y6o="},
00421     {{"\046\041", 2}, "JiE="},
00422     {{"\145\236", 2}, "ZZ4="},
00423     {{"\254\325", 2}, "rNU="},
00424     {{"\061\330", 2}, "Mdg="},
00425     {{"\245\032", 2}, "pRo="},
00426     {{"\006\000", 2}, "BgA="},
00427     {{"\375\131", 2}, "/Vk="},
00428     {{"\303\210", 2}, "w4g="},
00429     {{"\040\037", 2}, "IB8="},
00430     {{"\261\372", 2}, "sfo="},
00431     {{"\335\014", 2}, "3Qw="},
00432     {{"\233\217", 2}, "m48="},
00433     {{"\373\056", 2}, "+y4="},
00434     {{"\247\232", 2}, "p5o="},
00435     {{"\107\053", 2}, "Rys="},
00436     {{"\204\077", 2}, "hD8="},
00437     {{"\276\211", 2}, "vok="},
00438     {{"\313\110", 2}, "y0g="},
00439     {{"\363\376", 2}, "8/4="},
00440     {{"\251\234", 2}, "qZw="},
00441     {{"\103\262", 2}, "Q7I="},
00442     {{"\142\312", 2}, "Yso="},
00443     {{"\067\211", 2}, "N4k="},
00444     {{"\220\001", 2}, "kAE="},
00445     {{"\152\240", 2}, "aqA="},
00446     {{"\367\061", 2}, "9zE="},
00447     {{"\133\255", 2}, "W60="},
00448     {{"\176\035", 2}, "fh0="},
00449     {{"\032\231", 2}, "Gpk="},
00450 
00451     {{"\013\007\144", 3}, "Cwdk"},
00452     {{"\030\112\106", 3}, "GEpG"},
00453     {{"\047\325\046", 3}, "J9Um"},
00454     {{"\310\160\022", 3}, "yHAS"},
00455     {{"\131\100\237", 3}, "WUCf"},
00456     {{"\064\342\134", 3}, "NOJc"},
00457     {{"\010\177\004", 3}, "CH8E"},
00458     {{"\345\147\205", 3}, "5WeF"},
00459     {{"\300\343\360", 3}, "wOPw"},
00460     {{"\061\240\201", 3}, "MaCB"},
00461     {{"\225\333\044", 3}, "ldsk"},
00462     {{"\215\137\352", 3}, "jV/q"},
00463     {{"\371\147\160", 3}, "+Wdw"},
00464     {{"\030\320\051", 3}, "GNAp"},
00465     {{"\044\174\241", 3}, "JHyh"},
00466     {{"\260\127\037", 3}, "sFcf"},
00467     {{"\111\045\033", 3}, "SSUb"},
00468     {{"\202\114\107", 3}, "gkxH"},
00469     {{"\057\371\042", 3}, "L/ki"},
00470     {{"\223\247\244", 3}, "k6ek"},
00471     {{"\047\216\144", 3}, "J45k"},
00472     {{"\203\070\327", 3}, "gzjX"},
00473     {{"\247\140\072", 3}, "p2A6"},
00474     {{"\124\115\116", 3}, "VE1O"},
00475     {{"\157\162\050", 3}, "b3Io"},
00476     {{"\357\223\004", 3}, "75ME"},
00477     {{"\052\117\156", 3}, "Kk9u"},
00478     {{"\347\154\000", 3}, "52wA"},
00479     {{"\303\012\142", 3}, "wwpi"},
00480     {{"\060\035\362", 3}, "MB3y"},
00481     {{"\130\226\361", 3}, "WJbx"},
00482     {{"\173\013\071", 3}, "ews5"},
00483     {{"\336\004\027", 3}, "3gQX"},
00484     {{"\357\366\234", 3}, "7/ac"},
00485     {{"\353\304\111", 3}, "68RJ"},
00486     {{"\024\264\131", 3}, "FLRZ"},
00487     {{"\075\114\251", 3}, "PUyp"},
00488     {{"\315\031\225", 3}, "zRmV"},
00489     {{"\154\201\276", 3}, "bIG+"},
00490     {{"\200\066\072", 3}, "gDY6"},
00491     {{"\142\350\267", 3}, "Yui3"},
00492     {{"\033\000\166", 3}, "GwB2"},
00493     {{"\210\055\077", 3}, "iC0/"},
00494     {{"\341\037\124", 3}, "4R9U"},
00495     {{"\161\103\152", 3}, "cUNq"},
00496     {{"\270\142\131", 3}, "uGJZ"},
00497     {{"\337\076\074", 3}, "3z48"},
00498     {{"\375\106\362", 3}, "/Uby"},
00499     {{"\227\301\127", 3}, "l8FX"},
00500     {{"\340\002\234", 3}, "4AKc"},
00501     {{"\121\064\033", 3}, "UTQb"},
00502     {{"\157\134\143", 3}, "b1xj"},
00503     {{"\247\055\327", 3}, "py3X"},
00504     {{"\340\142\005", 3}, "4GIF"},
00505     {{"\060\260\143", 3}, "MLBj"},
00506     {{"\075\203\170", 3}, "PYN4"},
00507     {{"\143\160\016", 3}, "Y3AO"},
00508     {{"\313\013\063", 3}, "ywsz"},
00509     {{"\174\236\135", 3}, "fJ5d"},
00510     {{"\103\047\026", 3}, "QycW"},
00511     {{"\365\005\343", 3}, "9QXj"},
00512     {{"\271\160\223", 3}, "uXCT"},
00513     {{"\362\255\172", 3}, "8q16"},
00514     {{"\113\012\015", 3}, "SwoN"},
00515 
00516     // various lengths, generated by this python script:
00517     //
00518     // from std::string import lowercase as lc
00519     // for i in range(27):
00520     //   print '{ %2d, "%s",%s "%s" },' % (i, lc[:i], ' ' * (26-i),
00521     //                                     lc[:i].encode('base64').strip())
00522 
00523     {{"", 0}, {"", 0}},
00524     {"a", "YQ=="},
00525     {"ab", "YWI="},
00526     {"abc", "YWJj"},
00527     {"abcd", "YWJjZA=="},
00528     {"abcde", "YWJjZGU="},
00529     {"abcdef", "YWJjZGVm"},
00530     {"abcdefg", "YWJjZGVmZw=="},
00531     {"abcdefgh", "YWJjZGVmZ2g="},
00532     {"abcdefghi", "YWJjZGVmZ2hp"},
00533     {"abcdefghij", "YWJjZGVmZ2hpag=="},
00534     {"abcdefghijk", "YWJjZGVmZ2hpams="},
00535     {"abcdefghijkl", "YWJjZGVmZ2hpamts"},
00536     {"abcdefghijklm", "YWJjZGVmZ2hpamtsbQ=="},
00537     {"abcdefghijklmn", "YWJjZGVmZ2hpamtsbW4="},
00538     {"abcdefghijklmno", "YWJjZGVmZ2hpamtsbW5v"},
00539     {"abcdefghijklmnop", "YWJjZGVmZ2hpamtsbW5vcA=="},
00540     {"abcdefghijklmnopq", "YWJjZGVmZ2hpamtsbW5vcHE="},
00541     {"abcdefghijklmnopqr", "YWJjZGVmZ2hpamtsbW5vcHFy"},
00542     {"abcdefghijklmnopqrs", "YWJjZGVmZ2hpamtsbW5vcHFycw=="},
00543     {"abcdefghijklmnopqrst", "YWJjZGVmZ2hpamtsbW5vcHFyc3Q="},
00544     {"abcdefghijklmnopqrstu", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1"},
00545     {"abcdefghijklmnopqrstuv", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dg=="},
00546     {"abcdefghijklmnopqrstuvw", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnc="},
00547     {"abcdefghijklmnopqrstuvwx", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4"},
00548     {"abcdefghijklmnopqrstuvwxy", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eQ=="},
00549     {"abcdefghijklmnopqrstuvwxyz", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo="},
00550 };
00551 
00552 template <typename StringType>
00553 void TestEscapeAndUnescape() {
00554   // Check the short strings; this tests the math (and boundaries)
00555   for (const auto& tc : base64_tests) {
00556     StringType encoded("this junk should be ignored");
00557     absl::Base64Escape(tc.plaintext, &encoded);
00558     EXPECT_EQ(encoded, tc.cyphertext);
00559     EXPECT_EQ(absl::Base64Escape(tc.plaintext), tc.cyphertext);
00560 
00561     StringType decoded("this junk should be ignored");
00562     EXPECT_TRUE(absl::Base64Unescape(encoded, &decoded));
00563     EXPECT_EQ(decoded, tc.plaintext);
00564 
00565     StringType websafe(tc.cyphertext);
00566     for (int c = 0; c < websafe.size(); ++c) {
00567       if ('+' == websafe[c]) websafe[c] = '-';
00568       if ('/' == websafe[c]) websafe[c] = '_';
00569       if ('=' == websafe[c]) {
00570         websafe.resize(c);
00571         break;
00572       }
00573     }
00574 
00575     encoded = "this junk should be ignored";
00576     absl::WebSafeBase64Escape(tc.plaintext, &encoded);
00577     EXPECT_EQ(encoded, websafe);
00578     EXPECT_EQ(absl::WebSafeBase64Escape(tc.plaintext), websafe);
00579 
00580     // Let's try the std::string version of the decoder
00581     decoded = "this junk should be ignored";
00582     EXPECT_TRUE(absl::WebSafeBase64Unescape(websafe, &decoded));
00583     EXPECT_EQ(decoded, tc.plaintext);
00584   }
00585 
00586   // Now try the long strings, this tests the streaming
00587   for (const auto& tc : absl::strings_internal::base64_strings()) {
00588     StringType buffer;
00589     absl::WebSafeBase64Escape(tc.plaintext, &buffer);
00590     EXPECT_EQ(tc.cyphertext, buffer);
00591     EXPECT_EQ(absl::WebSafeBase64Escape(tc.plaintext), tc.cyphertext);
00592   }
00593 
00594   // Verify the behavior when decoding bad data
00595   {
00596     absl::string_view data_set[] = {"ab-/", absl::string_view("\0bcd", 4),
00597                                     absl::string_view("abc.\0", 5)};
00598     for (absl::string_view bad_data : data_set) {
00599       StringType buf;
00600       EXPECT_FALSE(absl::Base64Unescape(bad_data, &buf));
00601       EXPECT_FALSE(absl::WebSafeBase64Unescape(bad_data, &buf));
00602       EXPECT_TRUE(buf.empty());
00603     }
00604   }
00605 }
00606 
00607 TEST(Base64, EscapeAndUnescape) {
00608   TestEscapeAndUnescape<std::string>();
00609 }
00610 
00611 TEST(Base64, DISABLED_HugeData) {
00612   const size_t kSize = size_t(3) * 1000 * 1000 * 1000;
00613   static_assert(kSize % 3 == 0, "kSize must be divisible by 3");
00614   const std::string huge(kSize, 'x');
00615 
00616   std::string escaped;
00617   absl::Base64Escape(huge, &escaped);
00618 
00619   // Generates the std::string that should match a base64 encoded "xxx..." std::string.
00620   // "xxx" in base64 is "eHh4".
00621   std::string expected_encoding;
00622   expected_encoding.reserve(kSize / 3 * 4);
00623   for (size_t i = 0; i < kSize / 3; ++i) {
00624     expected_encoding.append("eHh4");
00625   }
00626   EXPECT_EQ(expected_encoding, escaped);
00627 
00628   std::string unescaped;
00629   EXPECT_TRUE(absl::Base64Unescape(escaped, &unescaped));
00630   EXPECT_EQ(huge, unescaped);
00631 }
00632 
00633 TEST(HexAndBack, HexStringToBytes_and_BytesToHexString) {
00634   std::string hex_mixed = "0123456789abcdefABCDEF";
00635   std::string bytes_expected = "\x01\x23\x45\x67\x89\xab\xcd\xef\xAB\xCD\xEF";
00636   std::string hex_only_lower = "0123456789abcdefabcdef";
00637 
00638   std::string bytes_result = absl::HexStringToBytes(hex_mixed);
00639   EXPECT_EQ(bytes_expected, bytes_result);
00640 
00641   std::string prefix_valid = hex_mixed + "?";
00642   std::string prefix_valid_result = absl::HexStringToBytes(
00643       absl::string_view(prefix_valid.data(), prefix_valid.size() - 1));
00644   EXPECT_EQ(bytes_expected, prefix_valid_result);
00645 
00646   std::string infix_valid = "?" + hex_mixed + "???";
00647   std::string infix_valid_result = absl::HexStringToBytes(
00648       absl::string_view(infix_valid.data() + 1, hex_mixed.size()));
00649   EXPECT_EQ(bytes_expected, infix_valid_result);
00650 
00651   std::string hex_result = absl::BytesToHexString(bytes_expected);
00652   EXPECT_EQ(hex_only_lower, hex_result);
00653 }
00654 
00655 }  // namespace


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14