诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

886 lines
35 KiB

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. #include <google/protobuf/stubs/strutil.h>
  32. #include <locale.h>
  33. #include <google/protobuf/stubs/stl_util.h>
  34. #include <google/protobuf/testing/googletest.h>
  35. #include <gtest/gtest.h>
  36. #ifdef _WIN32
  37. #define snprintf _snprintf
  38. #endif
  39. namespace google {
  40. namespace protobuf {
  41. namespace {
  42. // TODO(kenton): Copy strutil tests from google3?
  43. TEST(StringUtilityTest, ImmuneToLocales) {
  44. // Remember the old locale.
  45. char* old_locale_cstr = setlocale(LC_NUMERIC, nullptr);
  46. ASSERT_TRUE(old_locale_cstr != nullptr);
  47. string old_locale = old_locale_cstr;
  48. // Set the locale to "C".
  49. ASSERT_TRUE(setlocale(LC_NUMERIC, "C") != nullptr);
  50. EXPECT_EQ("1.5", SimpleDtoa(1.5));
  51. EXPECT_EQ("1.5", SimpleFtoa(1.5));
  52. if (setlocale(LC_NUMERIC, "es_ES") == nullptr &&
  53. setlocale(LC_NUMERIC, "es_ES.utf8") == nullptr) {
  54. // Some systems may not have the desired locale available.
  55. GOOGLE_LOG(WARNING)
  56. << "Couldn't set locale to es_ES. Skipping this test.";
  57. } else {
  58. EXPECT_EQ("1.5", SimpleDtoa(1.5));
  59. EXPECT_EQ("1.5", SimpleFtoa(1.5));
  60. }
  61. // Return to original locale.
  62. setlocale(LC_NUMERIC, old_locale.c_str());
  63. }
  64. #define EXPECT_EQ_ARRAY(len, x, y, msg) \
  65. for (int j = 0; j < len; ++j) { \
  66. EXPECT_EQ(x[j], y[j]) << "" # x << " != " # y \
  67. << " byte " << j << ": " << msg; \
  68. }
  69. static struct {
  70. int plain_length;
  71. const char* plaintext;
  72. const char* cyphertext;
  73. } base64_tests[] = {
  74. // Empty string.
  75. { 0, "", ""},
  76. // Basic bit patterns;
  77. // values obtained with "echo -n '...' | uuencode -m test"
  78. { 1, "\000", "AA==" },
  79. { 1, "\001", "AQ==" },
  80. { 1, "\002", "Ag==" },
  81. { 1, "\004", "BA==" },
  82. { 1, "\010", "CA==" },
  83. { 1, "\020", "EA==" },
  84. { 1, "\040", "IA==" },
  85. { 1, "\100", "QA==" },
  86. { 1, "\200", "gA==" },
  87. { 1, "\377", "/w==" },
  88. { 1, "\376", "/g==" },
  89. { 1, "\375", "/Q==" },
  90. { 1, "\373", "+w==" },
  91. { 1, "\367", "9w==" },
  92. { 1, "\357", "7w==" },
  93. { 1, "\337", "3w==" },
  94. { 1, "\277", "vw==" },
  95. { 1, "\177", "fw==" },
  96. { 2, "\000\000", "AAA=" },
  97. { 2, "\000\001", "AAE=" },
  98. { 2, "\000\002", "AAI=" },
  99. { 2, "\000\004", "AAQ=" },
  100. { 2, "\000\010", "AAg=" },
  101. { 2, "\000\020", "ABA=" },
  102. { 2, "\000\040", "ACA=" },
  103. { 2, "\000\100", "AEA=" },
  104. { 2, "\000\200", "AIA=" },
  105. { 2, "\001\000", "AQA=" },
  106. { 2, "\002\000", "AgA=" },
  107. { 2, "\004\000", "BAA=" },
  108. { 2, "\010\000", "CAA=" },
  109. { 2, "\020\000", "EAA=" },
  110. { 2, "\040\000", "IAA=" },
  111. { 2, "\100\000", "QAA=" },
  112. { 2, "\200\000", "gAA=" },
  113. { 2, "\377\377", "//8=" },
  114. { 2, "\377\376", "//4=" },
  115. { 2, "\377\375", "//0=" },
  116. { 2, "\377\373", "//s=" },
  117. { 2, "\377\367", "//c=" },
  118. { 2, "\377\357", "/+8=" },
  119. { 2, "\377\337", "/98=" },
  120. { 2, "\377\277", "/78=" },
  121. { 2, "\377\177", "/38=" },
  122. { 2, "\376\377", "/v8=" },
  123. { 2, "\375\377", "/f8=" },
  124. { 2, "\373\377", "+/8=" },
  125. { 2, "\367\377", "9/8=" },
  126. { 2, "\357\377", "7/8=" },
  127. { 2, "\337\377", "3/8=" },
  128. { 2, "\277\377", "v/8=" },
  129. { 2, "\177\377", "f/8=" },
  130. { 3, "\000\000\000", "AAAA" },
  131. { 3, "\000\000\001", "AAAB" },
  132. { 3, "\000\000\002", "AAAC" },
  133. { 3, "\000\000\004", "AAAE" },
  134. { 3, "\000\000\010", "AAAI" },
  135. { 3, "\000\000\020", "AAAQ" },
  136. { 3, "\000\000\040", "AAAg" },
  137. { 3, "\000\000\100", "AABA" },
  138. { 3, "\000\000\200", "AACA" },
  139. { 3, "\000\001\000", "AAEA" },
  140. { 3, "\000\002\000", "AAIA" },
  141. { 3, "\000\004\000", "AAQA" },
  142. { 3, "\000\010\000", "AAgA" },
  143. { 3, "\000\020\000", "ABAA" },
  144. { 3, "\000\040\000", "ACAA" },
  145. { 3, "\000\100\000", "AEAA" },
  146. { 3, "\000\200\000", "AIAA" },
  147. { 3, "\001\000\000", "AQAA" },
  148. { 3, "\002\000\000", "AgAA" },
  149. { 3, "\004\000\000", "BAAA" },
  150. { 3, "\010\000\000", "CAAA" },
  151. { 3, "\020\000\000", "EAAA" },
  152. { 3, "\040\000\000", "IAAA" },
  153. { 3, "\100\000\000", "QAAA" },
  154. { 3, "\200\000\000", "gAAA" },
  155. { 3, "\377\377\377", "////" },
  156. { 3, "\377\377\376", "///+" },
  157. { 3, "\377\377\375", "///9" },
  158. { 3, "\377\377\373", "///7" },
  159. { 3, "\377\377\367", "///3" },
  160. { 3, "\377\377\357", "///v" },
  161. { 3, "\377\377\337", "///f" },
  162. { 3, "\377\377\277", "//+/" },
  163. { 3, "\377\377\177", "//9/" },
  164. { 3, "\377\376\377", "//7/" },
  165. { 3, "\377\375\377", "//3/" },
  166. { 3, "\377\373\377", "//v/" },
  167. { 3, "\377\367\377", "//f/" },
  168. { 3, "\377\357\377", "/+//" },
  169. { 3, "\377\337\377", "/9//" },
  170. { 3, "\377\277\377", "/7//" },
  171. { 3, "\377\177\377", "/3//" },
  172. { 3, "\376\377\377", "/v//" },
  173. { 3, "\375\377\377", "/f//" },
  174. { 3, "\373\377\377", "+///" },
  175. { 3, "\367\377\377", "9///" },
  176. { 3, "\357\377\377", "7///" },
  177. { 3, "\337\377\377", "3///" },
  178. { 3, "\277\377\377", "v///" },
  179. { 3, "\177\377\377", "f///" },
  180. // Random numbers: values obtained with
  181. //
  182. // #! /bin/bash
  183. // dd bs=$1 count=1 if=/dev/random of=/tmp/bar.random
  184. // od -N $1 -t o1 /tmp/bar.random
  185. // uuencode -m test < /tmp/bar.random
  186. //
  187. // where $1 is the number of bytes (2, 3)
  188. { 2, "\243\361", "o/E=" },
  189. { 2, "\024\167", "FHc=" },
  190. { 2, "\313\252", "y6o=" },
  191. { 2, "\046\041", "JiE=" },
  192. { 2, "\145\236", "ZZ4=" },
  193. { 2, "\254\325", "rNU=" },
  194. { 2, "\061\330", "Mdg=" },
  195. { 2, "\245\032", "pRo=" },
  196. { 2, "\006\000", "BgA=" },
  197. { 2, "\375\131", "/Vk=" },
  198. { 2, "\303\210", "w4g=" },
  199. { 2, "\040\037", "IB8=" },
  200. { 2, "\261\372", "sfo=" },
  201. { 2, "\335\014", "3Qw=" },
  202. { 2, "\233\217", "m48=" },
  203. { 2, "\373\056", "+y4=" },
  204. { 2, "\247\232", "p5o=" },
  205. { 2, "\107\053", "Rys=" },
  206. { 2, "\204\077", "hD8=" },
  207. { 2, "\276\211", "vok=" },
  208. { 2, "\313\110", "y0g=" },
  209. { 2, "\363\376", "8/4=" },
  210. { 2, "\251\234", "qZw=" },
  211. { 2, "\103\262", "Q7I=" },
  212. { 2, "\142\312", "Yso=" },
  213. { 2, "\067\211", "N4k=" },
  214. { 2, "\220\001", "kAE=" },
  215. { 2, "\152\240", "aqA=" },
  216. { 2, "\367\061", "9zE=" },
  217. { 2, "\133\255", "W60=" },
  218. { 2, "\176\035", "fh0=" },
  219. { 2, "\032\231", "Gpk=" },
  220. { 3, "\013\007\144", "Cwdk" },
  221. { 3, "\030\112\106", "GEpG" },
  222. { 3, "\047\325\046", "J9Um" },
  223. { 3, "\310\160\022", "yHAS" },
  224. { 3, "\131\100\237", "WUCf" },
  225. { 3, "\064\342\134", "NOJc" },
  226. { 3, "\010\177\004", "CH8E" },
  227. { 3, "\345\147\205", "5WeF" },
  228. { 3, "\300\343\360", "wOPw" },
  229. { 3, "\061\240\201", "MaCB" },
  230. { 3, "\225\333\044", "ldsk" },
  231. { 3, "\215\137\352", "jV/q" },
  232. { 3, "\371\147\160", "+Wdw" },
  233. { 3, "\030\320\051", "GNAp" },
  234. { 3, "\044\174\241", "JHyh" },
  235. { 3, "\260\127\037", "sFcf" },
  236. { 3, "\111\045\033", "SSUb" },
  237. { 3, "\202\114\107", "gkxH" },
  238. { 3, "\057\371\042", "L/ki" },
  239. { 3, "\223\247\244", "k6ek" },
  240. { 3, "\047\216\144", "J45k" },
  241. { 3, "\203\070\327", "gzjX" },
  242. { 3, "\247\140\072", "p2A6" },
  243. { 3, "\124\115\116", "VE1O" },
  244. { 3, "\157\162\050", "b3Io" },
  245. { 3, "\357\223\004", "75ME" },
  246. { 3, "\052\117\156", "Kk9u" },
  247. { 3, "\347\154\000", "52wA" },
  248. { 3, "\303\012\142", "wwpi" },
  249. { 3, "\060\035\362", "MB3y" },
  250. { 3, "\130\226\361", "WJbx" },
  251. { 3, "\173\013\071", "ews5" },
  252. { 3, "\336\004\027", "3gQX" },
  253. { 3, "\357\366\234", "7/ac" },
  254. { 3, "\353\304\111", "68RJ" },
  255. { 3, "\024\264\131", "FLRZ" },
  256. { 3, "\075\114\251", "PUyp" },
  257. { 3, "\315\031\225", "zRmV" },
  258. { 3, "\154\201\276", "bIG+" },
  259. { 3, "\200\066\072", "gDY6" },
  260. { 3, "\142\350\267", "Yui3" },
  261. { 3, "\033\000\166", "GwB2" },
  262. { 3, "\210\055\077", "iC0/" },
  263. { 3, "\341\037\124", "4R9U" },
  264. { 3, "\161\103\152", "cUNq" },
  265. { 3, "\270\142\131", "uGJZ" },
  266. { 3, "\337\076\074", "3z48" },
  267. { 3, "\375\106\362", "/Uby" },
  268. { 3, "\227\301\127", "l8FX" },
  269. { 3, "\340\002\234", "4AKc" },
  270. { 3, "\121\064\033", "UTQb" },
  271. { 3, "\157\134\143", "b1xj" },
  272. { 3, "\247\055\327", "py3X" },
  273. { 3, "\340\142\005", "4GIF" },
  274. { 3, "\060\260\143", "MLBj" },
  275. { 3, "\075\203\170", "PYN4" },
  276. { 3, "\143\160\016", "Y3AO" },
  277. { 3, "\313\013\063", "ywsz" },
  278. { 3, "\174\236\135", "fJ5d" },
  279. { 3, "\103\047\026", "QycW" },
  280. { 3, "\365\005\343", "9QXj" },
  281. { 3, "\271\160\223", "uXCT" },
  282. { 3, "\362\255\172", "8q16" },
  283. { 3, "\113\012\015", "SwoN" },
  284. // various lengths, generated by this python script:
  285. //
  286. // from string import lowercase as lc
  287. // for i in range(27):
  288. // print '{ %2d, "%s",%s "%s" },' % (i, lc[:i], ' ' * (26-i),
  289. // lc[:i].encode('base64').strip())
  290. { 0, "", "" },
  291. { 1, "a", "YQ==" },
  292. { 2, "ab", "YWI=" },
  293. { 3, "abc", "YWJj" },
  294. { 4, "abcd", "YWJjZA==" },
  295. { 5, "abcde", "YWJjZGU=" },
  296. { 6, "abcdef", "YWJjZGVm" },
  297. { 7, "abcdefg", "YWJjZGVmZw==" },
  298. { 8, "abcdefgh", "YWJjZGVmZ2g=" },
  299. { 9, "abcdefghi", "YWJjZGVmZ2hp" },
  300. { 10, "abcdefghij", "YWJjZGVmZ2hpag==" },
  301. { 11, "abcdefghijk", "YWJjZGVmZ2hpams=" },
  302. { 12, "abcdefghijkl", "YWJjZGVmZ2hpamts" },
  303. { 13, "abcdefghijklm", "YWJjZGVmZ2hpamtsbQ==" },
  304. { 14, "abcdefghijklmn", "YWJjZGVmZ2hpamtsbW4=" },
  305. { 15, "abcdefghijklmno", "YWJjZGVmZ2hpamtsbW5v" },
  306. { 16, "abcdefghijklmnop", "YWJjZGVmZ2hpamtsbW5vcA==" },
  307. { 17, "abcdefghijklmnopq", "YWJjZGVmZ2hpamtsbW5vcHE=" },
  308. { 18, "abcdefghijklmnopqr", "YWJjZGVmZ2hpamtsbW5vcHFy" },
  309. { 19, "abcdefghijklmnopqrs", "YWJjZGVmZ2hpamtsbW5vcHFycw==" },
  310. { 20, "abcdefghijklmnopqrst", "YWJjZGVmZ2hpamtsbW5vcHFyc3Q=" },
  311. { 21, "abcdefghijklmnopqrstu", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1" },
  312. { 22, "abcdefghijklmnopqrstuv", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dg==" },
  313. { 23, "abcdefghijklmnopqrstuvw", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnc=" },
  314. { 24, "abcdefghijklmnopqrstuvwx", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4" },
  315. { 25, "abcdefghijklmnopqrstuvwxy", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eQ==" },
  316. { 26, "abcdefghijklmnopqrstuvwxyz", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo=" },
  317. };
  318. static struct {
  319. const char* plaintext;
  320. const char* cyphertext;
  321. } base64_strings[] = {
  322. // Some google quotes
  323. // Cyphertext created with "uuencode (GNU sharutils) 4.6.3"
  324. // (Note that we're testing the websafe encoding, though, so if
  325. // you add messages, be sure to run "tr -- '+/' '-_'" on the output)
  326. { "I was always good at math and science, and I never realized "
  327. "that was unusual or somehow undesirable. So one of the things "
  328. "I care a lot about is helping to remove that stigma, "
  329. "to show girls that you can be feminine, you can like the things "
  330. "that girls like, but you can also be really good at technology. "
  331. "You can be really good at building things."
  332. " - Marissa Meyer, Newsweek, 2010-12-22" "\n",
  333. "SSB3YXMgYWx3YXlzIGdvb2QgYXQgbWF0aCBhbmQgc2NpZW5jZSwgYW5kIEkg"
  334. "bmV2ZXIgcmVhbGl6ZWQgdGhhdCB3YXMgdW51c3VhbCBvciBzb21laG93IHVu"
  335. "ZGVzaXJhYmxlLiBTbyBvbmUgb2YgdGhlIHRoaW5ncyBJIGNhcmUgYSBsb3Qg"
  336. "YWJvdXQgaXMgaGVscGluZyB0byByZW1vdmUgdGhhdCBzdGlnbWEsIHRvIHNo"
  337. "b3cgZ2lybHMgdGhhdCB5b3UgY2FuIGJlIGZlbWluaW5lLCB5b3UgY2FuIGxp"
  338. "a2UgdGhlIHRoaW5ncyB0aGF0IGdpcmxzIGxpa2UsIGJ1dCB5b3UgY2FuIGFs"
  339. "c28gYmUgcmVhbGx5IGdvb2QgYXQgdGVjaG5vbG9neS4gWW91IGNhbiBiZSBy"
  340. "ZWFsbHkgZ29vZCBhdCBidWlsZGluZyB0aGluZ3MuIC0gTWFyaXNzYSBNZXll"
  341. "ciwgTmV3c3dlZWssIDIwMTAtMTItMjIK" },
  342. { "Typical first year for a new cluster: "
  343. "~0.5 overheating "
  344. "~1 PDU failure "
  345. "~1 rack-move "
  346. "~1 network rewiring "
  347. "~20 rack failures "
  348. "~5 racks go wonky "
  349. "~8 network maintenances "
  350. "~12 router reloads "
  351. "~3 router failures "
  352. "~dozens of minor 30-second blips for dns "
  353. "~1000 individual machine failures "
  354. "~thousands of hard drive failures "
  355. "slow disks, bad memory, misconfigured machines, flaky machines, etc."
  356. " - Jeff Dean, The Joys of Real Hardware" "\n",
  357. "VHlwaWNhbCBmaXJzdCB5ZWFyIGZvciBhIG5ldyBjbHVzdGVyOiB-MC41IG92"
  358. "ZXJoZWF0aW5nIH4xIFBEVSBmYWlsdXJlIH4xIHJhY2stbW92ZSB-MSBuZXR3"
  359. "b3JrIHJld2lyaW5nIH4yMCByYWNrIGZhaWx1cmVzIH41IHJhY2tzIGdvIHdv"
  360. "bmt5IH44IG5ldHdvcmsgbWFpbnRlbmFuY2VzIH4xMiByb3V0ZXIgcmVsb2Fk"
  361. "cyB-MyByb3V0ZXIgZmFpbHVyZXMgfmRvemVucyBvZiBtaW5vciAzMC1zZWNv"
  362. "bmQgYmxpcHMgZm9yIGRucyB-MTAwMCBpbmRpdmlkdWFsIG1hY2hpbmUgZmFp"
  363. "bHVyZXMgfnRob3VzYW5kcyBvZiBoYXJkIGRyaXZlIGZhaWx1cmVzIHNsb3cg"
  364. "ZGlza3MsIGJhZCBtZW1vcnksIG1pc2NvbmZpZ3VyZWQgbWFjaGluZXMsIGZs"
  365. "YWt5IG1hY2hpbmVzLCBldGMuIC0gSmVmZiBEZWFuLCBUaGUgSm95cyBvZiBS"
  366. "ZWFsIEhhcmR3YXJlCg" },
  367. { "I'm the head of the webspam team at Google. "
  368. "That means that if you type your name into Google and get porn back, "
  369. "it's my fault. Unless you're a porn star, in which case porn is a "
  370. "completely reasonable response."
  371. " - Matt Cutts, Google Plus" "\n",
  372. "SSdtIHRoZSBoZWFkIG9mIHRoZSB3ZWJzcGFtIHRlYW0gYXQgR29vZ2xlLiAg"
  373. "VGhhdCBtZWFucyB0aGF0IGlmIHlvdSB0eXBlIHlvdXIgbmFtZSBpbnRvIEdv"
  374. "b2dsZSBhbmQgZ2V0IHBvcm4gYmFjaywgaXQncyBteSBmYXVsdC4gVW5sZXNz"
  375. "IHlvdSdyZSBhIHBvcm4gc3RhciwgaW4gd2hpY2ggY2FzZSBwb3JuIGlzIGEg"
  376. "Y29tcGxldGVseSByZWFzb25hYmxlIHJlc3BvbnNlLiAtIE1hdHQgQ3V0dHMs"
  377. "IEdvb2dsZSBQbHVzCg" },
  378. { "It will still be a long time before machines approach human intelligence. "
  379. "But luckily, machines don't actually have to be intelligent; "
  380. "they just have to fake it. Access to a wealth of information, "
  381. "combined with a rudimentary decision-making capacity, "
  382. "can often be almost as useful. Of course, the results are better yet "
  383. "when coupled with intelligence. A reference librarian with access to "
  384. "a good search engine is a formidable tool."
  385. " - Craig Silverstein, Siemens Pictures of the Future, Spring 2004" "\n",
  386. "SXQgd2lsbCBzdGlsbCBiZSBhIGxvbmcgdGltZSBiZWZvcmUgbWFjaGluZXMg"
  387. "YXBwcm9hY2ggaHVtYW4gaW50ZWxsaWdlbmNlLiBCdXQgbHVja2lseSwgbWFj"
  388. "aGluZXMgZG9uJ3QgYWN0dWFsbHkgaGF2ZSB0byBiZSBpbnRlbGxpZ2VudDsg"
  389. "dGhleSBqdXN0IGhhdmUgdG8gZmFrZSBpdC4gQWNjZXNzIHRvIGEgd2VhbHRo"
  390. "IG9mIGluZm9ybWF0aW9uLCBjb21iaW5lZCB3aXRoIGEgcnVkaW1lbnRhcnkg"
  391. "ZGVjaXNpb24tbWFraW5nIGNhcGFjaXR5LCBjYW4gb2Z0ZW4gYmUgYWxtb3N0"
  392. "IGFzIHVzZWZ1bC4gT2YgY291cnNlLCB0aGUgcmVzdWx0cyBhcmUgYmV0dGVy"
  393. "IHlldCB3aGVuIGNvdXBsZWQgd2l0aCBpbnRlbGxpZ2VuY2UuIEEgcmVmZXJl"
  394. "bmNlIGxpYnJhcmlhbiB3aXRoIGFjY2VzcyB0byBhIGdvb2Qgc2VhcmNoIGVu"
  395. "Z2luZSBpcyBhIGZvcm1pZGFibGUgdG9vbC4gLSBDcmFpZyBTaWx2ZXJzdGVp"
  396. "biwgU2llbWVucyBQaWN0dXJlcyBvZiB0aGUgRnV0dXJlLCBTcHJpbmcgMjAw"
  397. "NAo" },
  398. // Degenerate edge case
  399. { "",
  400. "" },
  401. };
  402. TEST(Base64, EscapeAndUnescape) {
  403. // Check the short strings; this tests the math (and boundaries)
  404. for (int i = 0; i < sizeof(base64_tests) / sizeof(base64_tests[0]); ++i) {
  405. char encode_buffer[100];
  406. int encode_length;
  407. char decode_buffer[100];
  408. int decode_length;
  409. int cypher_length;
  410. string decode_str;
  411. const unsigned char* unsigned_plaintext =
  412. reinterpret_cast<const unsigned char*>(base64_tests[i].plaintext);
  413. StringPiece plaintext(base64_tests[i].plaintext,
  414. base64_tests[i].plain_length);
  415. cypher_length = strlen(base64_tests[i].cyphertext);
  416. // The basic escape function:
  417. memset(encode_buffer, 0, sizeof(encode_buffer));
  418. encode_length = Base64Escape(unsigned_plaintext,
  419. base64_tests[i].plain_length,
  420. encode_buffer,
  421. sizeof(encode_buffer));
  422. // Is it of the expected length?
  423. EXPECT_EQ(encode_length, cypher_length);
  424. // Would it have been okay to allocate only CalculateBase64EscapeLen()?
  425. EXPECT_EQ(CalculateBase64EscapedLen(base64_tests[i].plain_length),
  426. encode_length);
  427. // Is it the expected encoded value?
  428. ASSERT_STREQ(encode_buffer, base64_tests[i].cyphertext);
  429. // If we encode it into a buffer of exactly the right length...
  430. memset(encode_buffer, 0, sizeof(encode_buffer));
  431. encode_length = Base64Escape(unsigned_plaintext,
  432. base64_tests[i].plain_length,
  433. encode_buffer,
  434. cypher_length);
  435. // Is it still of the expected length?
  436. EXPECT_EQ(encode_length, cypher_length);
  437. // And is the value still correct? (i.e., not losing the last byte)
  438. EXPECT_STREQ(encode_buffer, base64_tests[i].cyphertext);
  439. // If we decode it back:
  440. decode_str.clear();
  441. EXPECT_TRUE(Base64Unescape(
  442. StringPiece(encode_buffer, cypher_length), &decode_str));
  443. // Is it of the expected length?
  444. EXPECT_EQ(base64_tests[i].plain_length, decode_str.length());
  445. // Is it the expected decoded value?
  446. EXPECT_EQ(plaintext, decode_str);
  447. // Let's try with a pre-populated string.
  448. string encoded("this junk should be ignored");
  449. Base64Escape(string(base64_tests[i].plaintext,
  450. base64_tests[i].plain_length),
  451. &encoded);
  452. EXPECT_EQ(encoded, string(encode_buffer, cypher_length));
  453. string decoded("this junk should be ignored");
  454. EXPECT_TRUE(Base64Unescape(
  455. StringPiece(encode_buffer, cypher_length), &decoded));
  456. EXPECT_EQ(decoded.size(), base64_tests[i].plain_length);
  457. EXPECT_EQ_ARRAY(decoded.size(), decoded, base64_tests[i].plaintext, i);
  458. // Our decoder treats the padding '=' characters at the end as
  459. // optional (but if there are any, there must be the correct
  460. // number of them.) If encode_buffer has any, run some additional
  461. // tests that fiddle with them.
  462. char* first_equals = strchr(encode_buffer, '=');
  463. if (first_equals) {
  464. // How many equals signs does the string start with?
  465. int equals = (*(first_equals+1) == '=') ? 2 : 1;
  466. // Try chopping off the equals sign(s) entirely. The decoder
  467. // should still be okay with this.
  468. string decoded2("this junk should also be ignored");
  469. *first_equals = '\0';
  470. EXPECT_TRUE(Base64Unescape(
  471. StringPiece(encode_buffer, first_equals - encode_buffer), &decoded2));
  472. EXPECT_EQ(decoded.size(), base64_tests[i].plain_length);
  473. EXPECT_EQ_ARRAY(decoded.size(), decoded, base64_tests[i].plaintext, i);
  474. // Now test chopping off the equals sign(s) and adding
  475. // whitespace. Our decoder should still accept this.
  476. decoded2.assign("this junk should be ignored");
  477. *first_equals = ' ';
  478. *(first_equals+1) = '\0';
  479. EXPECT_TRUE(Base64Unescape(
  480. StringPiece(encode_buffer, first_equals - encode_buffer + 1),
  481. &decoded2));
  482. EXPECT_EQ(decoded.size(), base64_tests[i].plain_length);
  483. EXPECT_EQ_ARRAY(decoded.size(), decoded, base64_tests[i].plaintext, i);
  484. // Now stick a bad character at the end of the string. The decoder
  485. // should refuse this string.
  486. decoded2.assign("this junk should be ignored");
  487. *first_equals = '?';
  488. *(first_equals+1) = '\0';
  489. EXPECT_TRUE(
  490. !Base64Unescape(
  491. StringPiece(encode_buffer, first_equals - encode_buffer + 1),
  492. &decoded2));
  493. int len;
  494. // Test whitespace mixed with the padding. (eg "AA = = ") The
  495. // decoder should accept this.
  496. if (equals == 2) {
  497. snprintf(first_equals, 6, " = = ");
  498. len = first_equals - encode_buffer + 5;
  499. } else {
  500. snprintf(first_equals, 6, " = ");
  501. len = first_equals - encode_buffer + 3;
  502. }
  503. decoded2.assign("this junk should be ignored");
  504. EXPECT_TRUE(
  505. Base64Unescape(StringPiece(encode_buffer, len), &decoded2));
  506. EXPECT_EQ(decoded.size(), base64_tests[i].plain_length);
  507. EXPECT_EQ_ARRAY(decoded.size(), decoded, base64_tests[i].plaintext, i);
  508. // Test whitespace mixed with the padding, but with the wrong
  509. // number of equals signs (eg "AA = "). The decoder should
  510. // refuse these strings.
  511. if (equals == 1) {
  512. snprintf(first_equals, 6, " = = ");
  513. len = first_equals - encode_buffer + 5;
  514. } else {
  515. snprintf(first_equals, 6, " = ");
  516. len = first_equals - encode_buffer + 3;
  517. }
  518. EXPECT_TRUE(
  519. !Base64Unescape(StringPiece(encode_buffer, len), &decoded2));
  520. }
  521. // Cool! the basic Base64 encoder/decoder works.
  522. // Let's try the alternate alphabet: tr -- '+/' '-_'
  523. char websafe[100];
  524. memset(websafe, 0, sizeof(websafe));
  525. strncpy(websafe, base64_tests[i].cyphertext, cypher_length);
  526. for (int c = 0; c < sizeof(websafe); ++c) {
  527. if ('+' == websafe[c]) { websafe[c] = '-'; }
  528. if ('/' == websafe[c]) { websafe[c] = '_'; }
  529. }
  530. // The websafe escape function:
  531. memset(encode_buffer, 0, sizeof(encode_buffer));
  532. encode_length = WebSafeBase64Escape(unsigned_plaintext,
  533. base64_tests[i].plain_length,
  534. encode_buffer,
  535. sizeof(encode_buffer),
  536. true);
  537. // Is it of the expected length?
  538. EXPECT_EQ(encode_length, cypher_length);
  539. EXPECT_EQ(
  540. CalculateBase64EscapedLen(base64_tests[i].plain_length, true),
  541. encode_length);
  542. // Is it the expected encoded value?
  543. EXPECT_STREQ(encode_buffer, websafe);
  544. // If we encode it into a buffer of exactly the right length...
  545. memset(encode_buffer, 0, sizeof(encode_buffer));
  546. encode_length = WebSafeBase64Escape(unsigned_plaintext,
  547. base64_tests[i].plain_length,
  548. encode_buffer,
  549. cypher_length,
  550. true);
  551. // Is it still of the expected length?
  552. EXPECT_EQ(encode_length, cypher_length);
  553. // And is the value still correct? (i.e., not losing the last byte)
  554. EXPECT_STREQ(encode_buffer, websafe);
  555. // Let's try the string version of the encoder
  556. encoded = "this junk should be ignored";
  557. WebSafeBase64Escape(
  558. unsigned_plaintext, base64_tests[i].plain_length,
  559. &encoded, true);
  560. EXPECT_EQ(encoded.size(), cypher_length);
  561. EXPECT_STREQ(encoded.c_str(), websafe);
  562. // If we decode it back:
  563. memset(decode_buffer, 0, sizeof(decode_buffer));
  564. decode_length = WebSafeBase64Unescape(encode_buffer,
  565. cypher_length,
  566. decode_buffer,
  567. sizeof(decode_buffer));
  568. // Is it of the expected length?
  569. EXPECT_EQ(decode_length, base64_tests[i].plain_length);
  570. // Is it the expected decoded value?
  571. EXPECT_EQ(0,
  572. memcmp(decode_buffer, base64_tests[i].plaintext, decode_length));
  573. // If we decode it into a buffer of exactly the right length...
  574. memset(decode_buffer, 0, sizeof(decode_buffer));
  575. decode_length = WebSafeBase64Unescape(encode_buffer,
  576. cypher_length,
  577. decode_buffer,
  578. decode_length);
  579. // Is it still of the expected length?
  580. EXPECT_EQ(decode_length, base64_tests[i].plain_length);
  581. // And is it the expected decoded value?
  582. EXPECT_EQ(0,
  583. memcmp(decode_buffer, base64_tests[i].plaintext, decode_length));
  584. // Try using '.' for the pad character.
  585. for (int c = cypher_length - 1; c >= 0 && '=' == encode_buffer[c]; --c) {
  586. encode_buffer[c] = '.';
  587. }
  588. // If we decode it back:
  589. memset(decode_buffer, 0, sizeof(decode_buffer));
  590. decode_length = WebSafeBase64Unescape(encode_buffer,
  591. cypher_length,
  592. decode_buffer,
  593. sizeof(decode_buffer));
  594. // Is it of the expected length?
  595. EXPECT_EQ(decode_length, base64_tests[i].plain_length);
  596. // Is it the expected decoded value?
  597. EXPECT_EQ(0,
  598. memcmp(decode_buffer, base64_tests[i].plaintext, decode_length));
  599. // If we decode it into a buffer of exactly the right length...
  600. memset(decode_buffer, 0, sizeof(decode_buffer));
  601. decode_length = WebSafeBase64Unescape(encode_buffer,
  602. cypher_length,
  603. decode_buffer,
  604. decode_length);
  605. // Is it still of the expected length?
  606. EXPECT_EQ(decode_length, base64_tests[i].plain_length);
  607. // And is it the expected decoded value?
  608. EXPECT_EQ(0,
  609. memcmp(decode_buffer, base64_tests[i].plaintext, decode_length));
  610. // Let's try the string version of the decoder
  611. decoded = "this junk should be ignored";
  612. EXPECT_TRUE(WebSafeBase64Unescape(
  613. StringPiece(encode_buffer, cypher_length), &decoded));
  614. EXPECT_EQ(decoded.size(), base64_tests[i].plain_length);
  615. EXPECT_EQ_ARRAY(decoded.size(), decoded, base64_tests[i].plaintext, i);
  616. // Okay! the websafe Base64 encoder/decoder works.
  617. // Let's try the unpadded version
  618. for (int c = 0; c < sizeof(websafe); ++c) {
  619. if ('=' == websafe[c]) {
  620. websafe[c] = '\0';
  621. cypher_length = c;
  622. break;
  623. }
  624. }
  625. // The websafe escape function:
  626. memset(encode_buffer, 0, sizeof(encode_buffer));
  627. encode_length = WebSafeBase64Escape(unsigned_plaintext,
  628. base64_tests[i].plain_length,
  629. encode_buffer,
  630. sizeof(encode_buffer),
  631. false);
  632. // Is it of the expected length?
  633. EXPECT_EQ(encode_length, cypher_length);
  634. EXPECT_EQ(
  635. CalculateBase64EscapedLen(base64_tests[i].plain_length, false),
  636. encode_length);
  637. // Is it the expected encoded value?
  638. EXPECT_STREQ(encode_buffer, websafe);
  639. // If we encode it into a buffer of exactly the right length...
  640. memset(encode_buffer, 0, sizeof(encode_buffer));
  641. encode_length = WebSafeBase64Escape(unsigned_plaintext,
  642. base64_tests[i].plain_length,
  643. encode_buffer,
  644. cypher_length,
  645. false);
  646. // Is it still of the expected length?
  647. EXPECT_EQ(encode_length, cypher_length);
  648. // And is the value still correct? (i.e., not losing the last byte)
  649. EXPECT_STREQ(encode_buffer, websafe);
  650. // Let's try the (other) string version of the encoder
  651. string plain(base64_tests[i].plaintext, base64_tests[i].plain_length);
  652. encoded = "this junk should be ignored";
  653. WebSafeBase64Escape(plain, &encoded);
  654. EXPECT_EQ(encoded.size(), cypher_length);
  655. EXPECT_STREQ(encoded.c_str(), websafe);
  656. // If we decode it back:
  657. memset(decode_buffer, 0, sizeof(decode_buffer));
  658. decode_length = WebSafeBase64Unescape(encode_buffer,
  659. cypher_length,
  660. decode_buffer,
  661. sizeof(decode_buffer));
  662. // Is it of the expected length?
  663. EXPECT_EQ(decode_length, base64_tests[i].plain_length);
  664. // Is it the expected decoded value?
  665. EXPECT_EQ(0,
  666. memcmp(decode_buffer, base64_tests[i].plaintext, decode_length));
  667. // If we decode it into a buffer of exactly the right length...
  668. memset(decode_buffer, 0, sizeof(decode_buffer));
  669. decode_length = WebSafeBase64Unescape(encode_buffer,
  670. cypher_length,
  671. decode_buffer,
  672. decode_length);
  673. // Is it still of the expected length?
  674. EXPECT_EQ(decode_length, base64_tests[i].plain_length);
  675. // And is it the expected decoded value?
  676. EXPECT_EQ(0,
  677. memcmp(decode_buffer, base64_tests[i].plaintext, decode_length));
  678. // Let's try the string version of the decoder
  679. decoded = "this junk should be ignored";
  680. EXPECT_TRUE(WebSafeBase64Unescape(
  681. StringPiece(encode_buffer, cypher_length), &decoded));
  682. EXPECT_EQ(decoded.size(), base64_tests[i].plain_length);
  683. EXPECT_EQ_ARRAY(decoded.size(), decoded, base64_tests[i].plaintext, i);
  684. // This value works. Try the next.
  685. }
  686. // Now try the long strings, this tests the streaming
  687. for (int i = 0; i < sizeof(base64_strings) / sizeof(base64_strings[0]);
  688. ++i) {
  689. const unsigned char* unsigned_plaintext =
  690. reinterpret_cast<const unsigned char*>(base64_strings[i].plaintext);
  691. int plain_length = strlen(base64_strings[i].plaintext);
  692. int cypher_length = strlen(base64_strings[i].cyphertext);
  693. std::vector<char> buffer(cypher_length+1);
  694. int encode_length = WebSafeBase64Escape(unsigned_plaintext,
  695. plain_length,
  696. &buffer[0],
  697. buffer.size(),
  698. false);
  699. EXPECT_EQ(cypher_length, encode_length);
  700. EXPECT_EQ(
  701. CalculateBase64EscapedLen(plain_length, false), encode_length);
  702. buffer[ encode_length ] = '\0';
  703. EXPECT_STREQ(base64_strings[i].cyphertext, &buffer[0]);
  704. }
  705. // Verify the behavior when decoding bad data
  706. {
  707. const char* bad_data = "ab-/";
  708. string buf;
  709. EXPECT_FALSE(Base64Unescape(StringPiece(bad_data), &buf));
  710. EXPECT_TRUE(!WebSafeBase64Unescape(bad_data, &buf));
  711. EXPECT_TRUE(buf.empty());
  712. }
  713. }
  714. // Test StrCat of ints and longs of various sizes and signdedness.
  715. TEST(StrCat, Ints) {
  716. const short s = -1; // NOLINT(runtime/int)
  717. const uint16_t us = 2;
  718. const int i = -3;
  719. const unsigned int ui = 4;
  720. const long l = -5; // NOLINT(runtime/int)
  721. const unsigned long ul = 6; // NOLINT(runtime/int)
  722. const long long ll = -7; // NOLINT(runtime/int)
  723. const unsigned long long ull = 8; // NOLINT(runtime/int)
  724. const ptrdiff_t ptrdiff = -9;
  725. const size_t size = 10;
  726. const intptr_t intptr = -12;
  727. const uintptr_t uintptr = 13;
  728. string answer;
  729. answer = StrCat(s, us);
  730. EXPECT_EQ(answer, "-12");
  731. answer = StrCat(i, ui);
  732. EXPECT_EQ(answer, "-34");
  733. answer = StrCat(l, ul);
  734. EXPECT_EQ(answer, "-56");
  735. answer = StrCat(ll, ull);
  736. EXPECT_EQ(answer, "-78");
  737. answer = StrCat(ptrdiff, size);
  738. EXPECT_EQ(answer, "-910");
  739. answer = StrCat(ptrdiff, intptr);
  740. EXPECT_EQ(answer, "-9-12");
  741. answer = StrCat(uintptr, 0);
  742. EXPECT_EQ(answer, "130");
  743. }
  744. class ReplaceChars : public ::testing::TestWithParam<std::tuple<string, string, const char*, char>> {
  745. };
  746. TEST_P(ReplaceChars, ReplacesAllOccurencesOfAnyCharInReplaceWithAReplaceChar) {
  747. string expected = std::get<0>(GetParam());
  748. string string_to_replace_in = std::get<1>(GetParam());
  749. const char* what_to_replace = std::get<2>(GetParam());
  750. char replacement = std::get<3>(GetParam());
  751. ReplaceCharacters(&string_to_replace_in, what_to_replace, replacement);
  752. ASSERT_EQ(expected, string_to_replace_in);
  753. }
  754. INSTANTIATE_TEST_CASE_P(Replace,
  755. ReplaceChars,
  756. ::testing::Values(
  757. std::make_tuple("", "", "", '_'), // empty string should remain empty
  758. std::make_tuple(" ", " ", "", '_'), // no replacement string
  759. std::make_tuple(" ", " ", "_-abcedf", '*'), // replacement character not in string
  760. std::make_tuple("replace", "Replace", "R", 'r'), // replace one character
  761. std::make_tuple("not_spaces__", "not\nspaces\t ", " \t\r\n", '_'), // replace some special characters
  762. std::make_tuple("c++", "cxx", "x", '+'), // same character multiple times
  763. std::make_tuple("qvvvvvng v T", "queueing a T", "aeiou", 'v'))); // replace all voewls
  764. class StripWs: public ::testing::TestWithParam<std::tuple<string, string>> {
  765. };
  766. TEST_P(StripWs, AlwaysStripsLeadingAndTrailingWhitespace) {
  767. string expected = std::get<0>(GetParam());
  768. string string_to_strip = std::get<1>(GetParam());
  769. StripWhitespace(&string_to_strip);
  770. ASSERT_EQ(expected, string_to_strip);
  771. }
  772. INSTANTIATE_TEST_CASE_P(Strip,
  773. StripWs,
  774. ::testing::Values(
  775. std::make_tuple("", ""), // empty string should remain empty
  776. std::make_tuple("", " "), // only ws should become empty
  777. std::make_tuple("no whitespace", " no whitespace"), // leading ws removed
  778. std::make_tuple("no whitespace", "no whitespace "), // trailing ws removed
  779. std::make_tuple("no whitespace", " no whitespace "), // same nb. of leading and trailing
  780. std::make_tuple("no whitespace", " no whitespace "), // different nb. of leading/trailing
  781. std::make_tuple("no whitespace", " no whitespace "))); // more trailing than leading
  782. } // anonymous namespace
  783. } // namespace protobuf
  784. } // namespace google