escaping_test.cc
Go to the documentation of this file.
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 // https://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 
15 #include "absl/strings/escaping.h"
16 
17 #include <array>
18 #include <cstdio>
19 #include <cstring>
20 #include <memory>
21 #include <vector>
22 
23 #include "gmock/gmock.h"
24 #include "gtest/gtest.h"
26 #include "absl/strings/str_cat.h"
27 
29 
30 namespace {
31 
32 struct epair {
33  std::string escaped;
34  std::string unescaped;
35 };
36 
37 TEST(CEscape, EscapeAndUnescape) {
38  const std::string inputs[] = {
39  std::string("foo\nxx\r\b\0023"),
40  std::string(""),
41  std::string("abc"),
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"),
50  std::string(
51  "\\\"\xe8\xb0\xb7\xe6\xad\x8c\\\" is Google\\\'s Chinese name"),
52  };
53  // Do this twice, once for octal escapes and once for hex escapes.
54  for (int kind = 0; kind < 4; kind++) {
55  for (const std::string& original : inputs) {
56  std::string escaped;
57  switch (kind) {
58  case 0:
59  escaped = absl::CEscape(original);
60  break;
61  case 1:
62  escaped = absl::CHexEscape(original);
63  break;
64  case 2:
65  escaped = absl::Utf8SafeCEscape(original);
66  break;
67  case 3:
68  escaped = absl::Utf8SafeCHexEscape(original);
69  break;
70  }
71  std::string unescaped_str;
72  EXPECT_TRUE(absl::CUnescape(escaped, &unescaped_str));
73  EXPECT_EQ(unescaped_str, original);
74 
75  // Check in-place unescaping
76  std::string s = escaped;
77  EXPECT_TRUE(absl::CUnescape(s, &s));
78  ASSERT_EQ(s, original);
79  }
80  }
81  // Check that all possible two character strings can be escaped then
82  // unescaped successfully.
83  for (int char0 = 0; char0 < 256; char0++) {
84  for (int char1 = 0; char1 < 256; char1++) {
85  char chars[2];
86  chars[0] = char0;
87  chars[1] = char1;
88  std::string s(chars, 2);
89  std::string escaped = absl::CHexEscape(s);
90  std::string unescaped;
91  EXPECT_TRUE(absl::CUnescape(escaped, &unescaped));
92  EXPECT_EQ(s, unescaped);
93  }
94  }
95 }
96 
97 TEST(CEscape, BasicEscaping) {
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"}
104  };
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"}
111  };
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"}
121  };
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"}
131  };
132 
133  for (const epair& val : oct_values) {
134  std::string escaped = absl::CEscape(val.unescaped);
135  EXPECT_EQ(escaped, val.escaped);
136  }
137  for (const epair& val : hex_values) {
138  std::string escaped = absl::CHexEscape(val.unescaped);
139  EXPECT_EQ(escaped, val.escaped);
140  }
141  for (const epair& val : utf8_oct_values) {
142  std::string escaped = absl::Utf8SafeCEscape(val.unescaped);
143  EXPECT_EQ(escaped, val.escaped);
144  }
145  for (const epair& val : utf8_hex_values) {
146  std::string escaped = absl::Utf8SafeCHexEscape(val.unescaped);
147  EXPECT_EQ(escaped, val.escaped);
148  }
149 }
150 
151 TEST(Unescape, BasicFunction) {
152  epair tests[] =
153  {{"\\u0030", "0"},
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) {
159  std::string out;
160  EXPECT_TRUE(absl::CUnescape(val.escaped, &out));
161  EXPECT_EQ(out, val.unescaped);
162  }
163  std::string bad[] = {"\\u1", // too short
164  "\\U1", // too short
165  "\\Uffffff", // exceeds 0x10ffff (largest Unicode)
166  "\\U00110000", // exceeds 0x10ffff (largest Unicode)
167  "\\uD835", // surrogate character (D800-DFFF)
168  "\\U0000DD04", // surrogate character (D800-DFFF)
169  "\\777", // exceeds 0xff
170  "\\xABCD"}; // exceeds 0xff
171  for (const std::string& e : bad) {
172  std::string error;
173  std::string out;
174  EXPECT_FALSE(absl::CUnescape(e, &out, &error));
175  EXPECT_FALSE(error.empty());
176  }
177 }
178 
179 class CUnescapeTest : public testing::Test {
180  protected:
181  static const char kStringWithMultipleOctalNulls[];
182  static const char kStringWithMultipleHexNulls[];
183  static const char kStringWithMultipleUnicodeNulls[];
184 
185  std::string result_string_;
186 };
187 
188 const char CUnescapeTest::kStringWithMultipleOctalNulls[] =
189  "\\0\\n" // null escape \0 plus newline
190  "0\\n" // just a number 0 (not a null escape) plus newline
191  "\\00\\12" // null escape \00 plus octal newline code
192  "\\000"; // null escape \000
193 
194 // This has the same ingredients as kStringWithMultipleOctalNulls
195 // but with \x hex escapes instead of octal escapes.
196 const char CUnescapeTest::kStringWithMultipleHexNulls[] =
197  "\\x0\\n"
198  "0\\n"
199  "\\x00\\xa"
200  "\\x000";
201 
202 const char CUnescapeTest::kStringWithMultipleUnicodeNulls[] =
203  "\\u0000\\n" // short-form (4-digit) null escape plus newline
204  "0\\n" // just a number 0 (not a null escape) plus newline
205  "\\U00000000"; // long-form (8-digit) null escape
206 
207 TEST_F(CUnescapeTest, Unescapes1CharOctalNull) {
208  std::string original_string = "\\0";
209  EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
210  EXPECT_EQ(std::string("\0", 1), result_string_);
211 }
212 
213 TEST_F(CUnescapeTest, Unescapes2CharOctalNull) {
214  std::string original_string = "\\00";
215  EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
216  EXPECT_EQ(std::string("\0", 1), result_string_);
217 }
218 
219 TEST_F(CUnescapeTest, Unescapes3CharOctalNull) {
220  std::string original_string = "\\000";
221  EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
222  EXPECT_EQ(std::string("\0", 1), result_string_);
223 }
224 
225 TEST_F(CUnescapeTest, Unescapes1CharHexNull) {
226  std::string original_string = "\\x0";
227  EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
228  EXPECT_EQ(std::string("\0", 1), result_string_);
229 }
230 
231 TEST_F(CUnescapeTest, Unescapes2CharHexNull) {
232  std::string original_string = "\\x00";
233  EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
234  EXPECT_EQ(std::string("\0", 1), result_string_);
235 }
236 
237 TEST_F(CUnescapeTest, Unescapes3CharHexNull) {
238  std::string original_string = "\\x000";
239  EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
240  EXPECT_EQ(std::string("\0", 1), result_string_);
241 }
242 
243 TEST_F(CUnescapeTest, Unescapes4CharUnicodeNull) {
244  std::string original_string = "\\u0000";
245  EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
246  EXPECT_EQ(std::string("\0", 1), result_string_);
247 }
248 
249 TEST_F(CUnescapeTest, Unescapes8CharUnicodeNull) {
250  std::string original_string = "\\U00000000";
251  EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
252  EXPECT_EQ(std::string("\0", 1), result_string_);
253 }
254 
255 TEST_F(CUnescapeTest, UnescapesMultipleOctalNulls) {
256  std::string original_string(kStringWithMultipleOctalNulls);
257  EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
258  // All escapes, including newlines and null escapes, should have been
259  // converted to the equivalent characters.
260  EXPECT_EQ(std::string("\0\n"
261  "0\n"
262  "\0\n"
263  "\0",
264  7),
265  result_string_);
266 }
267 
268 
269 TEST_F(CUnescapeTest, UnescapesMultipleHexNulls) {
270  std::string original_string(kStringWithMultipleHexNulls);
271  EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
272  EXPECT_EQ(std::string("\0\n"
273  "0\n"
274  "\0\n"
275  "\0",
276  7),
277  result_string_);
278 }
279 
280 TEST_F(CUnescapeTest, UnescapesMultipleUnicodeNulls) {
281  std::string original_string(kStringWithMultipleUnicodeNulls);
282  EXPECT_TRUE(absl::CUnescape(original_string, &result_string_));
283  EXPECT_EQ(std::string("\0\n"
284  "0\n"
285  "\0",
286  5),
287  result_string_);
288 }
289 
290 static struct {
291  absl::string_view plaintext;
292  absl::string_view cyphertext;
293 } const base64_tests[] = {
294  // Empty std::string.
295  {{"", 0}, {"", 0}},
296  {{nullptr, 0},
297  {"", 0}}, // if length is zero, plaintext ptr must be ignored!
298 
299  // Basic bit patterns;
300  // values obtained with "echo -n '...' | uuencode -m test"
301 
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=="},
311 
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="},
338 
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="},
356 
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"},
382 
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///"},
408 
409  // Random numbers: values obtained with
410  //
411  // #! /bin/bash
412  // dd bs=$1 count=1 if=/dev/random of=/tmp/bar.random
413  // od -N $1 -t o1 /tmp/bar.random
414  // uuencode -m test < /tmp/bar.random
415  //
416  // where $1 is the number of bytes (2, 3)
417 
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="},
450 
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"},
515 
516  // various lengths, generated by this python script:
517  //
518  // from std::string import lowercase as lc
519  // for i in range(27):
520  // print '{ %2d, "%s",%s "%s" },' % (i, lc[:i], ' ' * (26-i),
521  // lc[:i].encode('base64').strip())
522 
523  {{"", 0}, {"", 0}},
524  {"a", "YQ=="},
525  {"ab", "YWI="},
526  {"abc", "YWJj"},
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="},
550 };
551 
552 template <typename StringType>
553 void TestEscapeAndUnescape() {
554  // Check the short strings; this tests the math (and boundaries)
555  for (const auto& tc : base64_tests) {
556  StringType encoded("this junk should be ignored");
557  absl::Base64Escape(tc.plaintext, &encoded);
558  EXPECT_EQ(encoded, tc.cyphertext);
559  EXPECT_EQ(absl::Base64Escape(tc.plaintext), tc.cyphertext);
560 
561  StringType decoded("this junk should be ignored");
562  EXPECT_TRUE(absl::Base64Unescape(encoded, &decoded));
563  EXPECT_EQ(decoded, tc.plaintext);
564 
565  StringType websafe(tc.cyphertext);
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]) {
570  websafe.resize(c);
571  break;
572  }
573  }
574 
575  encoded = "this junk should be ignored";
576  absl::WebSafeBase64Escape(tc.plaintext, &encoded);
577  EXPECT_EQ(encoded, websafe);
578  EXPECT_EQ(absl::WebSafeBase64Escape(tc.plaintext), websafe);
579 
580  // Let's try the std::string version of the decoder
581  decoded = "this junk should be ignored";
582  EXPECT_TRUE(absl::WebSafeBase64Unescape(websafe, &decoded));
583  EXPECT_EQ(decoded, tc.plaintext);
584  }
585 
586  // Now try the long strings, this tests the streaming
587  for (const auto& tc : absl::strings_internal::base64_strings()) {
588  StringType buffer;
589  absl::WebSafeBase64Escape(tc.plaintext, &buffer);
590  EXPECT_EQ(tc.cyphertext, buffer);
591  EXPECT_EQ(absl::WebSafeBase64Escape(tc.plaintext), tc.cyphertext);
592  }
593 
594  // Verify the behavior when decoding bad data
595  {
596  absl::string_view data_set[] = {"ab-/", absl::string_view("\0bcd", 4),
597  absl::string_view("abc.\0", 5)};
598  for (absl::string_view bad_data : data_set) {
599  StringType buf;
600  EXPECT_FALSE(absl::Base64Unescape(bad_data, &buf));
601  EXPECT_FALSE(absl::WebSafeBase64Unescape(bad_data, &buf));
602  EXPECT_TRUE(buf.empty());
603  }
604  }
605 }
606 
607 TEST(Base64, EscapeAndUnescape) {
608  TestEscapeAndUnescape<std::string>();
609 }
610 
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');
615 
616  std::string escaped;
617  absl::Base64Escape(huge, &escaped);
618 
619  // Generates the std::string that should match a base64 encoded "xxx..." std::string.
620  // "xxx" in base64 is "eHh4".
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");
625  }
626  EXPECT_EQ(expected_encoding, escaped);
627 
628  std::string unescaped;
629  EXPECT_TRUE(absl::Base64Unescape(escaped, &unescaped));
630  EXPECT_EQ(huge, unescaped);
631 }
632 
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";
637 
638  std::string bytes_result = absl::HexStringToBytes(hex_mixed);
639  EXPECT_EQ(bytes_expected, bytes_result);
640 
641  std::string prefix_valid = hex_mixed + "?";
642  std::string prefix_valid_result = absl::HexStringToBytes(
643  absl::string_view(prefix_valid.data(), prefix_valid.size() - 1));
644  EXPECT_EQ(bytes_expected, prefix_valid_result);
645 
646  std::string infix_valid = "?" + hex_mixed + "???";
647  std::string infix_valid_result = absl::HexStringToBytes(
648  absl::string_view(infix_valid.data() + 1, hex_mixed.size()));
649  EXPECT_EQ(bytes_expected, infix_valid_result);
650 
651  std::string hex_result = absl::BytesToHexString(bytes_expected);
652  EXPECT_EQ(hex_only_lower, hex_result);
653 }
654 
655 } // namespace
std::string Utf8SafeCEscape(absl::string_view src)
Definition: escaping.cc:1049
std::string * out
Definition: parser_test.cc:293
const std::array< base64_testcase, 5 > & base64_strings()
std::string CEscape(absl::string_view src)
Definition: escaping.cc:1039
std::string Utf8SafeCHexEscape(absl::string_view src)
Definition: escaping.cc:1053
std::string BytesToHexString(absl::string_view from)
Definition: escaping.cc:1121
char buf[N]
bool WebSafeBase64Unescape(absl::string_view src, std::string *dest)
Definition: escaping.cc:1085
std::string CHexEscape(absl::string_view src)
Definition: escaping.cc:1045
void WebSafeBase64Escape(absl::string_view src, std::string *dest)
Definition: escaping.cc:1094
TEST_F(GraphCyclesTest, NoCycle)
void Base64Escape(absl::string_view src, std::string *dest)
Definition: escaping.cc:1089
std::string HexStringToBytes(absl::string_view from)
Definition: escaping.cc:1113
bool Base64Unescape(absl::string_view src, std::string *dest)
Definition: escaping.cc:1081
bool CUnescape(absl::string_view source, std::string *dest, std::string *error)
Definition: escaping.cc:1034
TEST(Symbolize, Unimplemented)


abseil_cpp
Author(s):
autogenerated on Mon Feb 28 2022 21:31:18