escaping_test.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/strings/escaping.h"
  15. #include <array>
  16. #include <cstdio>
  17. #include <cstring>
  18. #include <memory>
  19. #include <vector>
  20. #include "gmock/gmock.h"
  21. #include "gtest/gtest.h"
  22. #include "absl/strings/str_cat.h"
  23. #include "absl/strings/internal/escaping_test_common.inc"
  24. namespace {
  25. struct epair {
  26. std::string escaped;
  27. std::string unescaped;
  28. };
  29. TEST(CEscape, EscapeAndUnescape) {
  30. const std::string inputs[] = {
  31. std::string("foo\nxx\r\b\0023"),
  32. std::string(""),
  33. std::string("abc"),
  34. std::string("\1chad_rules"),
  35. std::string("\1arnar_drools"),
  36. std::string("xxxx\r\t'\"\\"),
  37. std::string("\0xx\0", 4),
  38. std::string("\x01\x31"),
  39. std::string("abc\xb\x42\141bc"),
  40. std::string("123\1\x31\x32\x33"),
  41. std::string("\xc1\xca\x1b\x62\x19o\xcc\x04"),
  42. std::string("\\\"\xe8\xb0\xb7\xe6\xad\x8c\\\" is Google\\\'s Chinese name"),
  43. };
  44. // Do this twice, once for octal escapes and once for hex escapes.
  45. for (int kind = 0; kind < 4; kind++) {
  46. for (const std::string& original : inputs) {
  47. std::string escaped;
  48. switch (kind) {
  49. case 0:
  50. escaped = absl::CEscape(original);
  51. break;
  52. case 1:
  53. escaped = absl::CHexEscape(original);
  54. break;
  55. case 2:
  56. escaped = absl::Utf8SafeCEscape(original);
  57. break;
  58. case 3:
  59. escaped = absl::Utf8SafeCHexEscape(original);
  60. break;
  61. }
  62. std::string unescaped_str;
  63. EXPECT_TRUE(absl::CUnescape(escaped, &unescaped_str));
  64. EXPECT_EQ(unescaped_str, original);
  65. // Check in-place unescaping
  66. std::string s = escaped;
  67. EXPECT_TRUE(absl::CUnescape(s, &s));
  68. ASSERT_EQ(s, original);
  69. }
  70. }
  71. // Check that all possible two character strings can be escaped then
  72. // unescaped successfully.
  73. for (int char0 = 0; char0 < 256; char0++) {
  74. for (int char1 = 0; char1 < 256; char1++) {
  75. char chars[2];
  76. chars[0] = char0;
  77. chars[1] = char1;
  78. std::string s(chars, 2);
  79. std::string escaped = absl::CHexEscape(s);
  80. std::string unescaped;
  81. EXPECT_TRUE(absl::CUnescape(escaped, &unescaped));
  82. EXPECT_EQ(s, unescaped);
  83. }
  84. }
  85. }
  86. TEST(CEscape, BasicEscaping) {
  87. epair oct_values[] = {
  88. {"foo\\rbar\\nbaz\\t", "foo\rbar\nbaz\t"},
  89. {"\\'full of \\\"sound\\\" and \\\"fury\\\"\\'",
  90. "'full of \"sound\" and \"fury\"'"},
  91. {"signi\\\\fying\\\\ nothing\\\\", "signi\\fying\\ nothing\\"},
  92. {"\\010\\t\\n\\013\\014\\r", "\010\011\012\013\014\015"}
  93. };
  94. epair hex_values[] = {
  95. {"ubik\\rubik\\nubik\\t", "ubik\rubik\nubik\t"},
  96. {"I\\\'ve just seen a \\\"face\\\"",
  97. "I've just seen a \"face\""},
  98. {"hel\\\\ter\\\\skel\\\\ter\\\\", "hel\\ter\\skel\\ter\\"},
  99. {"\\x08\\t\\n\\x0b\\x0c\\r", "\010\011\012\013\014\015"}
  100. };
  101. epair utf8_oct_values[] = {
  102. {"\xe8\xb0\xb7\xe6\xad\x8c\\r\xe8\xb0\xb7\xe6\xad\x8c\\nbaz\\t",
  103. "\xe8\xb0\xb7\xe6\xad\x8c\r\xe8\xb0\xb7\xe6\xad\x8c\nbaz\t"},
  104. {"\\\"\xe8\xb0\xb7\xe6\xad\x8c\\\" is Google\\\'s Chinese name",
  105. "\"\xe8\xb0\xb7\xe6\xad\x8c\" is Google\'s Chinese name"},
  106. {"\xe3\x83\xa1\xe3\x83\xbc\xe3\x83\xab\\\\are\\\\Japanese\\\\chars\\\\",
  107. "\xe3\x83\xa1\xe3\x83\xbc\xe3\x83\xab\\are\\Japanese\\chars\\"},
  108. {"\xed\x81\xac\xeb\xa1\xac\\010\\t\\n\\013\\014\\r",
  109. "\xed\x81\xac\xeb\xa1\xac\010\011\012\013\014\015"}
  110. };
  111. epair utf8_hex_values[] = {
  112. {"\x20\xe4\xbd\xa0\\t\xe5\xa5\xbd,\\r!\\n",
  113. "\x20\xe4\xbd\xa0\t\xe5\xa5\xbd,\r!\n"},
  114. {"\xe8\xa9\xa6\xe9\xa8\x93\\\' means \\\"test\\\"",
  115. "\xe8\xa9\xa6\xe9\xa8\x93\' means \"test\""},
  116. {"\\\\\xe6\x88\x91\\\\:\\\\\xe6\x9d\xa8\xe6\xac\xa2\\\\",
  117. "\\\xe6\x88\x91\\:\\\xe6\x9d\xa8\xe6\xac\xa2\\"},
  118. {"\xed\x81\xac\xeb\xa1\xac\\x08\\t\\n\\x0b\\x0c\\r",
  119. "\xed\x81\xac\xeb\xa1\xac\010\011\012\013\014\015"}
  120. };
  121. for (const epair& val : oct_values) {
  122. std::string escaped = absl::CEscape(val.unescaped);
  123. EXPECT_EQ(escaped, val.escaped);
  124. }
  125. for (const epair& val : hex_values) {
  126. std::string escaped = absl::CHexEscape(val.unescaped);
  127. EXPECT_EQ(escaped, val.escaped);
  128. }
  129. for (const epair& val : utf8_oct_values) {
  130. std::string escaped = absl::Utf8SafeCEscape(val.unescaped);
  131. EXPECT_EQ(escaped, val.escaped);
  132. }
  133. for (const epair& val : utf8_hex_values) {
  134. std::string escaped = absl::Utf8SafeCHexEscape(val.unescaped);
  135. EXPECT_EQ(escaped, val.escaped);
  136. }
  137. }
  138. TEST(Unescape, BasicFunction) {
  139. epair tests[] =
  140. {{"\\u0030", "0"},
  141. {"\\u00A3", "\xC2\xA3"},
  142. {"\\u22FD", "\xE2\x8B\xBD"},
  143. {"\\U00010000", "\xF0\x90\x80\x80"},
  144. {"\\U0010FFFD", "\xF4\x8F\xBF\xBD"}};
  145. for (const epair& val : tests) {
  146. std::string out;
  147. EXPECT_TRUE(absl::CUnescape(val.escaped, &out));
  148. EXPECT_EQ(out, val.unescaped);
  149. }
  150. std::string bad[] =
  151. {"\\u1", // too short
  152. "\\U1", // too short
  153. "\\Uffffff", // exceeds 0x10ffff (largest Unicode)
  154. "\\U00110000", // exceeds 0x10ffff (largest Unicode)
  155. "\\uD835", // surrogate character (D800-DFFF)
  156. "\\U0000DD04", // surrogate character (D800-DFFF)
  157. "\\777", // exceeds 0xff
  158. "\\xABCD"}; // exceeds 0xff
  159. for (const std::string& e : bad) {
  160. std::string error;
  161. std::string out;
  162. EXPECT_FALSE(absl::CUnescape(e, &out, &error));
  163. EXPECT_FALSE(error.empty());
  164. }
  165. }
  166. class CUnescapeTest : public testing::Test {
  167. protected:
  168. static const char kStringWithMultipleOctalNulls[];
  169. static const char kStringWithMultipleHexNulls[];
  170. static const char kStringWithMultipleUnicodeNulls[];
  171. std::string result_string_;
  172. };
  173. const char CUnescapeTest::kStringWithMultipleOctalNulls[] =
  174. "\\0\\n" // null escape \0 plus newline
  175. "0\\n" // just a number 0 (not a null escape) plus newline
  176. "\\00\\12" // null escape \00 plus octal newline code
  177. "\\000"; // null escape \000
  178. // This has the same ingredients as kStringWithMultipleOctalNulls
  179. // but with \x hex escapes instead of octal escapes.
  180. const char CUnescapeTest::kStringWithMultipleHexNulls[] =
  181. "\\x0\\n"
  182. "0\\n"
  183. "\\x00\\xa"
  184. "\\x000";
  185. const char CUnescapeTest::kStringWithMultipleUnicodeNulls[] =
  186. "\\u0000\\n" // short-form (4-digit) null escape plus newline
  187. "0\\n" // just a number 0 (not a null escape) plus newline
  188. "\\U00000000"; // long-form (8-digit) null escape
  189. TEST_F(CUnescapeTest, Unescapes1CharOctalNull) {
  190. std::string original_string = "\\0";
  191. EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
  192. EXPECT_EQ(std::string("\0", 1), result_string_);
  193. }
  194. TEST_F(CUnescapeTest, Unescapes2CharOctalNull) {
  195. std::string original_string = "\\00";
  196. EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
  197. EXPECT_EQ(std::string("\0", 1), result_string_);
  198. }
  199. TEST_F(CUnescapeTest, Unescapes3CharOctalNull) {
  200. std::string original_string = "\\000";
  201. EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
  202. EXPECT_EQ(std::string("\0", 1), result_string_);
  203. }
  204. TEST_F(CUnescapeTest, Unescapes1CharHexNull) {
  205. std::string original_string = "\\x0";
  206. EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
  207. EXPECT_EQ(std::string("\0", 1), result_string_);
  208. }
  209. TEST_F(CUnescapeTest, Unescapes2CharHexNull) {
  210. std::string original_string = "\\x00";
  211. EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
  212. EXPECT_EQ(std::string("\0", 1), result_string_);
  213. }
  214. TEST_F(CUnescapeTest, Unescapes3CharHexNull) {
  215. std::string original_string = "\\x000";
  216. EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
  217. EXPECT_EQ(std::string("\0", 1), result_string_);
  218. }
  219. TEST_F(CUnescapeTest, Unescapes4CharUnicodeNull) {
  220. std::string original_string = "\\u0000";
  221. EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
  222. EXPECT_EQ(std::string("\0", 1), result_string_);
  223. }
  224. TEST_F(CUnescapeTest, Unescapes8CharUnicodeNull) {
  225. std::string original_string = "\\U00000000";
  226. EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
  227. EXPECT_EQ(std::string("\0", 1), result_string_);
  228. }
  229. TEST_F(CUnescapeTest, UnescapesMultipleOctalNulls) {
  230. std::string original_string(kStringWithMultipleOctalNulls);
  231. EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
  232. // All escapes, including newlines and null escapes, should have been
  233. // converted to the equivalent characters.
  234. EXPECT_EQ(std::string("\0\n"
  235. "0\n"
  236. "\0\n"
  237. "\0", 7), result_string_);
  238. }
  239. TEST_F(CUnescapeTest, UnescapesMultipleHexNulls) {
  240. std::string original_string(kStringWithMultipleHexNulls);
  241. EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
  242. EXPECT_EQ(std::string("\0\n"
  243. "0\n"
  244. "\0\n"
  245. "\0", 7), result_string_);
  246. }
  247. TEST_F(CUnescapeTest, UnescapesMultipleUnicodeNulls) {
  248. std::string original_string(kStringWithMultipleUnicodeNulls);
  249. EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
  250. EXPECT_EQ(std::string("\0\n"
  251. "0\n"
  252. "\0", 5), result_string_);
  253. }
  254. static struct {
  255. absl::string_view plaintext;
  256. absl::string_view cyphertext;
  257. } const base64_tests[] = {
  258. // Empty std::string.
  259. {{"", 0}, {"", 0}},
  260. {{nullptr, 0},
  261. {"", 0}}, // if length is zero, plaintext ptr must be ignored!
  262. // Basic bit patterns;
  263. // values obtained with "echo -n '...' | uuencode -m test"
  264. {{"\000", 1}, "AA=="},
  265. {{"\001", 1}, "AQ=="},
  266. {{"\002", 1}, "Ag=="},
  267. {{"\004", 1}, "BA=="},
  268. {{"\010", 1}, "CA=="},
  269. {{"\020", 1}, "EA=="},
  270. {{"\040", 1}, "IA=="},
  271. {{"\100", 1}, "QA=="},
  272. {{"\200", 1}, "gA=="},
  273. {{"\377", 1}, "/w=="},
  274. {{"\376", 1}, "/g=="},
  275. {{"\375", 1}, "/Q=="},
  276. {{"\373", 1}, "+w=="},
  277. {{"\367", 1}, "9w=="},
  278. {{"\357", 1}, "7w=="},
  279. {{"\337", 1}, "3w=="},
  280. {{"\277", 1}, "vw=="},
  281. {{"\177", 1}, "fw=="},
  282. {{"\000\000", 2}, "AAA="},
  283. {{"\000\001", 2}, "AAE="},
  284. {{"\000\002", 2}, "AAI="},
  285. {{"\000\004", 2}, "AAQ="},
  286. {{"\000\010", 2}, "AAg="},
  287. {{"\000\020", 2}, "ABA="},
  288. {{"\000\040", 2}, "ACA="},
  289. {{"\000\100", 2}, "AEA="},
  290. {{"\000\200", 2}, "AIA="},
  291. {{"\001\000", 2}, "AQA="},
  292. {{"\002\000", 2}, "AgA="},
  293. {{"\004\000", 2}, "BAA="},
  294. {{"\010\000", 2}, "CAA="},
  295. {{"\020\000", 2}, "EAA="},
  296. {{"\040\000", 2}, "IAA="},
  297. {{"\100\000", 2}, "QAA="},
  298. {{"\200\000", 2}, "gAA="},
  299. {{"\377\377", 2}, "//8="},
  300. {{"\377\376", 2}, "//4="},
  301. {{"\377\375", 2}, "//0="},
  302. {{"\377\373", 2}, "//s="},
  303. {{"\377\367", 2}, "//c="},
  304. {{"\377\357", 2}, "/+8="},
  305. {{"\377\337", 2}, "/98="},
  306. {{"\377\277", 2}, "/78="},
  307. {{"\377\177", 2}, "/38="},
  308. {{"\376\377", 2}, "/v8="},
  309. {{"\375\377", 2}, "/f8="},
  310. {{"\373\377", 2}, "+/8="},
  311. {{"\367\377", 2}, "9/8="},
  312. {{"\357\377", 2}, "7/8="},
  313. {{"\337\377", 2}, "3/8="},
  314. {{"\277\377", 2}, "v/8="},
  315. {{"\177\377", 2}, "f/8="},
  316. {{"\000\000\000", 3}, "AAAA"},
  317. {{"\000\000\001", 3}, "AAAB"},
  318. {{"\000\000\002", 3}, "AAAC"},
  319. {{"\000\000\004", 3}, "AAAE"},
  320. {{"\000\000\010", 3}, "AAAI"},
  321. {{"\000\000\020", 3}, "AAAQ"},
  322. {{"\000\000\040", 3}, "AAAg"},
  323. {{"\000\000\100", 3}, "AABA"},
  324. {{"\000\000\200", 3}, "AACA"},
  325. {{"\000\001\000", 3}, "AAEA"},
  326. {{"\000\002\000", 3}, "AAIA"},
  327. {{"\000\004\000", 3}, "AAQA"},
  328. {{"\000\010\000", 3}, "AAgA"},
  329. {{"\000\020\000", 3}, "ABAA"},
  330. {{"\000\040\000", 3}, "ACAA"},
  331. {{"\000\100\000", 3}, "AEAA"},
  332. {{"\000\200\000", 3}, "AIAA"},
  333. {{"\001\000\000", 3}, "AQAA"},
  334. {{"\002\000\000", 3}, "AgAA"},
  335. {{"\004\000\000", 3}, "BAAA"},
  336. {{"\010\000\000", 3}, "CAAA"},
  337. {{"\020\000\000", 3}, "EAAA"},
  338. {{"\040\000\000", 3}, "IAAA"},
  339. {{"\100\000\000", 3}, "QAAA"},
  340. {{"\200\000\000", 3}, "gAAA"},
  341. {{"\377\377\377", 3}, "////"},
  342. {{"\377\377\376", 3}, "///+"},
  343. {{"\377\377\375", 3}, "///9"},
  344. {{"\377\377\373", 3}, "///7"},
  345. {{"\377\377\367", 3}, "///3"},
  346. {{"\377\377\357", 3}, "///v"},
  347. {{"\377\377\337", 3}, "///f"},
  348. {{"\377\377\277", 3}, "//+/"},
  349. {{"\377\377\177", 3}, "//9/"},
  350. {{"\377\376\377", 3}, "//7/"},
  351. {{"\377\375\377", 3}, "//3/"},
  352. {{"\377\373\377", 3}, "//v/"},
  353. {{"\377\367\377", 3}, "//f/"},
  354. {{"\377\357\377", 3}, "/+//"},
  355. {{"\377\337\377", 3}, "/9//"},
  356. {{"\377\277\377", 3}, "/7//"},
  357. {{"\377\177\377", 3}, "/3//"},
  358. {{"\376\377\377", 3}, "/v//"},
  359. {{"\375\377\377", 3}, "/f//"},
  360. {{"\373\377\377", 3}, "+///"},
  361. {{"\367\377\377", 3}, "9///"},
  362. {{"\357\377\377", 3}, "7///"},
  363. {{"\337\377\377", 3}, "3///"},
  364. {{"\277\377\377", 3}, "v///"},
  365. {{"\177\377\377", 3}, "f///"},
  366. // Random numbers: values obtained with
  367. //
  368. // #! /bin/bash
  369. // dd bs=$1 count=1 if=/dev/random of=/tmp/bar.random
  370. // od -N $1 -t o1 /tmp/bar.random
  371. // uuencode -m test < /tmp/bar.random
  372. //
  373. // where $1 is the number of bytes (2, 3)
  374. {{"\243\361", 2}, "o/E="},
  375. {{"\024\167", 2}, "FHc="},
  376. {{"\313\252", 2}, "y6o="},
  377. {{"\046\041", 2}, "JiE="},
  378. {{"\145\236", 2}, "ZZ4="},
  379. {{"\254\325", 2}, "rNU="},
  380. {{"\061\330", 2}, "Mdg="},
  381. {{"\245\032", 2}, "pRo="},
  382. {{"\006\000", 2}, "BgA="},
  383. {{"\375\131", 2}, "/Vk="},
  384. {{"\303\210", 2}, "w4g="},
  385. {{"\040\037", 2}, "IB8="},
  386. {{"\261\372", 2}, "sfo="},
  387. {{"\335\014", 2}, "3Qw="},
  388. {{"\233\217", 2}, "m48="},
  389. {{"\373\056", 2}, "+y4="},
  390. {{"\247\232", 2}, "p5o="},
  391. {{"\107\053", 2}, "Rys="},
  392. {{"\204\077", 2}, "hD8="},
  393. {{"\276\211", 2}, "vok="},
  394. {{"\313\110", 2}, "y0g="},
  395. {{"\363\376", 2}, "8/4="},
  396. {{"\251\234", 2}, "qZw="},
  397. {{"\103\262", 2}, "Q7I="},
  398. {{"\142\312", 2}, "Yso="},
  399. {{"\067\211", 2}, "N4k="},
  400. {{"\220\001", 2}, "kAE="},
  401. {{"\152\240", 2}, "aqA="},
  402. {{"\367\061", 2}, "9zE="},
  403. {{"\133\255", 2}, "W60="},
  404. {{"\176\035", 2}, "fh0="},
  405. {{"\032\231", 2}, "Gpk="},
  406. {{"\013\007\144", 3}, "Cwdk"},
  407. {{"\030\112\106", 3}, "GEpG"},
  408. {{"\047\325\046", 3}, "J9Um"},
  409. {{"\310\160\022", 3}, "yHAS"},
  410. {{"\131\100\237", 3}, "WUCf"},
  411. {{"\064\342\134", 3}, "NOJc"},
  412. {{"\010\177\004", 3}, "CH8E"},
  413. {{"\345\147\205", 3}, "5WeF"},
  414. {{"\300\343\360", 3}, "wOPw"},
  415. {{"\061\240\201", 3}, "MaCB"},
  416. {{"\225\333\044", 3}, "ldsk"},
  417. {{"\215\137\352", 3}, "jV/q"},
  418. {{"\371\147\160", 3}, "+Wdw"},
  419. {{"\030\320\051", 3}, "GNAp"},
  420. {{"\044\174\241", 3}, "JHyh"},
  421. {{"\260\127\037", 3}, "sFcf"},
  422. {{"\111\045\033", 3}, "SSUb"},
  423. {{"\202\114\107", 3}, "gkxH"},
  424. {{"\057\371\042", 3}, "L/ki"},
  425. {{"\223\247\244", 3}, "k6ek"},
  426. {{"\047\216\144", 3}, "J45k"},
  427. {{"\203\070\327", 3}, "gzjX"},
  428. {{"\247\140\072", 3}, "p2A6"},
  429. {{"\124\115\116", 3}, "VE1O"},
  430. {{"\157\162\050", 3}, "b3Io"},
  431. {{"\357\223\004", 3}, "75ME"},
  432. {{"\052\117\156", 3}, "Kk9u"},
  433. {{"\347\154\000", 3}, "52wA"},
  434. {{"\303\012\142", 3}, "wwpi"},
  435. {{"\060\035\362", 3}, "MB3y"},
  436. {{"\130\226\361", 3}, "WJbx"},
  437. {{"\173\013\071", 3}, "ews5"},
  438. {{"\336\004\027", 3}, "3gQX"},
  439. {{"\357\366\234", 3}, "7/ac"},
  440. {{"\353\304\111", 3}, "68RJ"},
  441. {{"\024\264\131", 3}, "FLRZ"},
  442. {{"\075\114\251", 3}, "PUyp"},
  443. {{"\315\031\225", 3}, "zRmV"},
  444. {{"\154\201\276", 3}, "bIG+"},
  445. {{"\200\066\072", 3}, "gDY6"},
  446. {{"\142\350\267", 3}, "Yui3"},
  447. {{"\033\000\166", 3}, "GwB2"},
  448. {{"\210\055\077", 3}, "iC0/"},
  449. {{"\341\037\124", 3}, "4R9U"},
  450. {{"\161\103\152", 3}, "cUNq"},
  451. {{"\270\142\131", 3}, "uGJZ"},
  452. {{"\337\076\074", 3}, "3z48"},
  453. {{"\375\106\362", 3}, "/Uby"},
  454. {{"\227\301\127", 3}, "l8FX"},
  455. {{"\340\002\234", 3}, "4AKc"},
  456. {{"\121\064\033", 3}, "UTQb"},
  457. {{"\157\134\143", 3}, "b1xj"},
  458. {{"\247\055\327", 3}, "py3X"},
  459. {{"\340\142\005", 3}, "4GIF"},
  460. {{"\060\260\143", 3}, "MLBj"},
  461. {{"\075\203\170", 3}, "PYN4"},
  462. {{"\143\160\016", 3}, "Y3AO"},
  463. {{"\313\013\063", 3}, "ywsz"},
  464. {{"\174\236\135", 3}, "fJ5d"},
  465. {{"\103\047\026", 3}, "QycW"},
  466. {{"\365\005\343", 3}, "9QXj"},
  467. {{"\271\160\223", 3}, "uXCT"},
  468. {{"\362\255\172", 3}, "8q16"},
  469. {{"\113\012\015", 3}, "SwoN"},
  470. // various lengths, generated by this python script:
  471. //
  472. // from std::string import lowercase as lc
  473. // for i in range(27):
  474. // print '{ %2d, "%s",%s "%s" },' % (i, lc[:i], ' ' * (26-i),
  475. // lc[:i].encode('base64').strip())
  476. {{"", 0}, {"", 0}},
  477. {"a", "YQ=="},
  478. {"ab", "YWI="},
  479. {"abc", "YWJj"},
  480. {"abcd", "YWJjZA=="},
  481. {"abcde", "YWJjZGU="},
  482. {"abcdef", "YWJjZGVm"},
  483. {"abcdefg", "YWJjZGVmZw=="},
  484. {"abcdefgh", "YWJjZGVmZ2g="},
  485. {"abcdefghi", "YWJjZGVmZ2hp"},
  486. {"abcdefghij", "YWJjZGVmZ2hpag=="},
  487. {"abcdefghijk", "YWJjZGVmZ2hpams="},
  488. {"abcdefghijkl", "YWJjZGVmZ2hpamts"},
  489. {"abcdefghijklm", "YWJjZGVmZ2hpamtsbQ=="},
  490. {"abcdefghijklmn", "YWJjZGVmZ2hpamtsbW4="},
  491. {"abcdefghijklmno", "YWJjZGVmZ2hpamtsbW5v"},
  492. {"abcdefghijklmnop", "YWJjZGVmZ2hpamtsbW5vcA=="},
  493. {"abcdefghijklmnopq", "YWJjZGVmZ2hpamtsbW5vcHE="},
  494. {"abcdefghijklmnopqr", "YWJjZGVmZ2hpamtsbW5vcHFy"},
  495. {"abcdefghijklmnopqrs", "YWJjZGVmZ2hpamtsbW5vcHFycw=="},
  496. {"abcdefghijklmnopqrst", "YWJjZGVmZ2hpamtsbW5vcHFyc3Q="},
  497. {"abcdefghijklmnopqrstu", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1"},
  498. {"abcdefghijklmnopqrstuv", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dg=="},
  499. {"abcdefghijklmnopqrstuvw", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnc="},
  500. {"abcdefghijklmnopqrstuvwx", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4"},
  501. {"abcdefghijklmnopqrstuvwxy", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eQ=="},
  502. {"abcdefghijklmnopqrstuvwxyz", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo="},
  503. };
  504. TEST(Base64, EscapeAndUnescape) {
  505. // Check the short strings; this tests the math (and boundaries)
  506. for (const auto& tc : base64_tests) {
  507. std::string encoded("this junk should be ignored");
  508. absl::Base64Escape(tc.plaintext, &encoded);
  509. EXPECT_EQ(encoded, tc.cyphertext);
  510. std::string decoded("this junk should be ignored");
  511. EXPECT_TRUE(absl::Base64Unescape(encoded, &decoded));
  512. EXPECT_EQ(decoded, tc.plaintext);
  513. std::string websafe(tc.cyphertext);
  514. for (int c = 0; c < websafe.size(); ++c) {
  515. if ('+' == websafe[c]) websafe[c] = '-';
  516. if ('/' == websafe[c]) websafe[c] = '_';
  517. if ('=' == websafe[c]) {
  518. websafe.resize(c);
  519. break;
  520. }
  521. }
  522. encoded = "this junk should be ignored";
  523. absl::WebSafeBase64Escape(tc.plaintext, &encoded);
  524. EXPECT_EQ(encoded, websafe);
  525. // Let's try the std::string version of the decoder
  526. decoded = "this junk should be ignored";
  527. EXPECT_TRUE(absl::WebSafeBase64Unescape(websafe, &decoded));
  528. EXPECT_EQ(decoded, tc.plaintext);
  529. }
  530. // Now try the long strings, this tests the streaming
  531. for (const auto& tc : base64_strings) {
  532. std::string buffer;
  533. absl::WebSafeBase64Escape(tc.plaintext, &buffer);
  534. EXPECT_EQ(tc.cyphertext, buffer);
  535. }
  536. // Verify the behavior when decoding bad data
  537. {
  538. absl::string_view data_set[] = {"ab-/", absl::string_view("\0bcd", 4),
  539. absl::string_view("abc.\0", 5)};
  540. for (absl::string_view bad_data : data_set) {
  541. std::string buf;
  542. EXPECT_FALSE(absl::Base64Unescape(bad_data, &buf));
  543. EXPECT_FALSE(absl::WebSafeBase64Unescape(bad_data, &buf));
  544. EXPECT_TRUE(buf.empty());
  545. }
  546. }
  547. }
  548. TEST(Base64, DISABLED_HugeData) {
  549. const size_t kSize = size_t(3) * 1000 * 1000 * 1000;
  550. static_assert(kSize % 3 == 0, "kSize must be divisible by 3");
  551. const std::string huge(kSize, 'x');
  552. std::string escaped;
  553. absl::Base64Escape(huge, &escaped);
  554. // Generates the std::string that should match a base64 encoded "xxx..." std::string.
  555. // "xxx" in base64 is "eHh4".
  556. std::string expected_encoding;
  557. expected_encoding.reserve(kSize / 3 * 4);
  558. for (size_t i = 0; i < kSize / 3; ++i) {
  559. expected_encoding.append("eHh4");
  560. }
  561. EXPECT_EQ(expected_encoding, escaped);
  562. std::string unescaped;
  563. EXPECT_TRUE(absl::Base64Unescape(escaped, &unescaped));
  564. EXPECT_EQ(huge, unescaped);
  565. }
  566. TEST(HexAndBack, HexStringToBytes_and_BytesToHexString) {
  567. std::string hex_mixed = "0123456789abcdefABCDEF";
  568. std::string bytes_expected = "\x01\x23\x45\x67\x89\xab\xcd\xef\xAB\xCD\xEF";
  569. std::string hex_only_lower = "0123456789abcdefabcdef";
  570. std::string bytes_result = absl::HexStringToBytes(hex_mixed);
  571. EXPECT_EQ(bytes_expected, bytes_result);
  572. std::string prefix_valid = hex_mixed + "?";
  573. std::string prefix_valid_result = absl::HexStringToBytes(
  574. absl::string_view(prefix_valid.data(), prefix_valid.size() - 1));
  575. EXPECT_EQ(bytes_expected, prefix_valid_result);
  576. std::string infix_valid = "?" + hex_mixed + "???";
  577. std::string infix_valid_result = absl::HexStringToBytes(
  578. absl::string_view(infix_valid.data() + 1, hex_mixed.size()));
  579. EXPECT_EQ(bytes_expected, infix_valid_result);
  580. std::string hex_result = absl::BytesToHexString(bytes_expected);
  581. EXPECT_EQ(hex_only_lower, hex_result);
  582. }
  583. } // namespace