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.

354 lines
13 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/common.h"
  28. #include "talk/base/gunit.h"
  29. #include "talk/base/stringencode.h"
  30. #include "talk/base/stringutils.h"
  31. namespace talk_base {
  32. TEST(Utf8EncodeTest, EncodeDecode) {
  33. const struct Utf8Test {
  34. const char* encoded;
  35. size_t encsize, enclen;
  36. unsigned long decoded;
  37. } kTests[] = {
  38. { "a ", 5, 1, 'a' },
  39. { "\x7F ", 5, 1, 0x7F },
  40. { "\xC2\x80 ", 5, 2, 0x80 },
  41. { "\xDF\xBF ", 5, 2, 0x7FF },
  42. { "\xE0\xA0\x80 ", 5, 3, 0x800 },
  43. { "\xEF\xBF\xBF ", 5, 3, 0xFFFF },
  44. { "\xF0\x90\x80\x80 ", 5, 4, 0x10000 },
  45. { "\xF0\x90\x80\x80 ", 3, 0, 0x10000 },
  46. { "\xF0\xF0\x80\x80 ", 5, 0, 0 },
  47. { "\xF0\x90\x80 ", 5, 0, 0 },
  48. { "\x90\x80\x80 ", 5, 0, 0 },
  49. { NULL, 0, 0 },
  50. };
  51. for (size_t i = 0; kTests[i].encoded; ++i) {
  52. unsigned long val = 0;
  53. ASSERT_EQ(kTests[i].enclen, utf8_decode(kTests[i].encoded,
  54. kTests[i].encsize,
  55. &val));
  56. unsigned long result = (kTests[i].enclen == 0) ? 0 : kTests[i].decoded;
  57. ASSERT_EQ(result, val);
  58. if (kTests[i].decoded == 0) {
  59. // Not an interesting encoding test case
  60. continue;
  61. }
  62. char buffer[5];
  63. memset(buffer, 0x01, ARRAY_SIZE(buffer));
  64. ASSERT_EQ(kTests[i].enclen, utf8_encode(buffer,
  65. kTests[i].encsize,
  66. kTests[i].decoded));
  67. ASSERT_TRUE(memcmp(buffer, kTests[i].encoded, kTests[i].enclen) == 0);
  68. // Make sure remainder of buffer is unchanged
  69. ASSERT_TRUE(memory_check(buffer + kTests[i].enclen,
  70. 0x1,
  71. ARRAY_SIZE(buffer) - kTests[i].enclen));
  72. }
  73. }
  74. class HexEncodeTest : public testing::Test {
  75. public:
  76. HexEncodeTest() : enc_res_(0), dec_res_(0) {
  77. for (size_t i = 0; i < sizeof(data_); ++i) {
  78. data_[i] = (i + 128) & 0xff;
  79. }
  80. memset(decoded_, 0x7f, sizeof(decoded_));
  81. }
  82. char data_[10];
  83. char encoded_[31];
  84. char decoded_[11];
  85. size_t enc_res_;
  86. size_t dec_res_;
  87. };
  88. // Test that we can convert to/from hex with no delimiter.
  89. TEST_F(HexEncodeTest, TestWithNoDelimiter) {
  90. enc_res_ = hex_encode(encoded_, sizeof(encoded_), data_, sizeof(data_));
  91. ASSERT_EQ(sizeof(data_) * 2, enc_res_);
  92. ASSERT_STREQ("80818283848586878889", encoded_);
  93. dec_res_ = hex_decode(decoded_, sizeof(decoded_), encoded_, enc_res_);
  94. ASSERT_EQ(sizeof(data_), dec_res_);
  95. ASSERT_EQ(0, memcmp(data_, decoded_, dec_res_));
  96. }
  97. // Test that we can convert to/from hex with a colon delimiter.
  98. TEST_F(HexEncodeTest, TestWithDelimiter) {
  99. enc_res_ = hex_encode_with_delimiter(encoded_, sizeof(encoded_),
  100. data_, sizeof(data_), ':');
  101. ASSERT_EQ(sizeof(data_) * 3 - 1, enc_res_);
  102. ASSERT_STREQ("80:81:82:83:84:85:86:87:88:89", encoded_);
  103. dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_),
  104. encoded_, enc_res_, ':');
  105. ASSERT_EQ(sizeof(data_), dec_res_);
  106. ASSERT_EQ(0, memcmp(data_, decoded_, dec_res_));
  107. }
  108. // Test that encoding with one delimiter and decoding with another fails.
  109. TEST_F(HexEncodeTest, TestWithWrongDelimiter) {
  110. enc_res_ = hex_encode_with_delimiter(encoded_, sizeof(encoded_),
  111. data_, sizeof(data_), ':');
  112. ASSERT_EQ(sizeof(data_) * 3 - 1, enc_res_);
  113. dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_),
  114. encoded_, enc_res_, '/');
  115. ASSERT_EQ(0U, dec_res_);
  116. }
  117. // Test that encoding without a delimiter and decoding with one fails.
  118. TEST_F(HexEncodeTest, TestExpectedDelimiter) {
  119. enc_res_ = hex_encode(encoded_, sizeof(encoded_), data_, sizeof(data_));
  120. ASSERT_EQ(sizeof(data_) * 2, enc_res_);
  121. dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_),
  122. encoded_, enc_res_, ':');
  123. ASSERT_EQ(0U, dec_res_);
  124. }
  125. // Test that encoding with a delimiter and decoding without one fails.
  126. TEST_F(HexEncodeTest, TestExpectedNoDelimiter) {
  127. enc_res_ = hex_encode_with_delimiter(encoded_, sizeof(encoded_),
  128. data_, sizeof(data_), ':');
  129. ASSERT_EQ(sizeof(data_) * 3 - 1, enc_res_);
  130. dec_res_ = hex_decode(decoded_, sizeof(decoded_), encoded_, enc_res_);
  131. ASSERT_EQ(0U, dec_res_);
  132. }
  133. // Test that we handle a zero-length buffer with no delimiter.
  134. TEST_F(HexEncodeTest, TestZeroLengthNoDelimiter) {
  135. enc_res_ = hex_encode(encoded_, sizeof(encoded_), "", 0);
  136. ASSERT_EQ(0U, enc_res_);
  137. dec_res_ = hex_decode(decoded_, sizeof(decoded_), encoded_, enc_res_);
  138. ASSERT_EQ(0U, dec_res_);
  139. }
  140. // Test that we handle a zero-length buffer with a delimiter.
  141. TEST_F(HexEncodeTest, TestZeroLengthWithDelimiter) {
  142. enc_res_ = hex_encode_with_delimiter(encoded_, sizeof(encoded_), "", 0, ':');
  143. ASSERT_EQ(0U, enc_res_);
  144. dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_),
  145. encoded_, enc_res_, ':');
  146. ASSERT_EQ(0U, dec_res_);
  147. }
  148. // Test the std::string variants that take no delimiter.
  149. TEST_F(HexEncodeTest, TestHelpersNoDelimiter) {
  150. std::string result = hex_encode(data_, sizeof(data_));
  151. ASSERT_EQ("80818283848586878889", result);
  152. dec_res_ = hex_decode(decoded_, sizeof(decoded_), result);
  153. ASSERT_EQ(sizeof(data_), dec_res_);
  154. ASSERT_EQ(0, memcmp(data_, decoded_, dec_res_));
  155. }
  156. // Test the std::string variants that use a delimiter.
  157. TEST_F(HexEncodeTest, TestHelpersWithDelimiter) {
  158. std::string result = hex_encode_with_delimiter(data_, sizeof(data_), ':');
  159. ASSERT_EQ("80:81:82:83:84:85:86:87:88:89", result);
  160. dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_), result, ':');
  161. ASSERT_EQ(sizeof(data_), dec_res_);
  162. ASSERT_EQ(0, memcmp(data_, decoded_, dec_res_));
  163. }
  164. // Test that encoding into a too-small output buffer (without delimiter) fails.
  165. TEST_F(HexEncodeTest, TestEncodeTooShort) {
  166. enc_res_ = hex_encode_with_delimiter(encoded_, sizeof(data_) * 2,
  167. data_, sizeof(data_), 0);
  168. ASSERT_EQ(0U, enc_res_);
  169. }
  170. // Test that encoding into a too-small output buffer (with delimiter) fails.
  171. TEST_F(HexEncodeTest, TestEncodeWithDelimiterTooShort) {
  172. enc_res_ = hex_encode_with_delimiter(encoded_, sizeof(data_) * 3 - 1,
  173. data_, sizeof(data_), ':');
  174. ASSERT_EQ(0U, enc_res_);
  175. }
  176. // Test that decoding into a too-small output buffer fails.
  177. TEST_F(HexEncodeTest, TestDecodeTooShort) {
  178. dec_res_ = hex_decode_with_delimiter(decoded_, 4, "0123456789", 10, 0);
  179. ASSERT_EQ(0U, dec_res_);
  180. ASSERT_EQ(0x7f, decoded_[4]);
  181. }
  182. // Test that decoding non-hex data fails.
  183. TEST_F(HexEncodeTest, TestDecodeBogusData) {
  184. dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_), "xyz", 3, 0);
  185. ASSERT_EQ(0U, dec_res_);
  186. }
  187. // Test that decoding an odd number of hex characters fails.
  188. TEST_F(HexEncodeTest, TestDecodeOddHexDigits) {
  189. dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_), "012", 3, 0);
  190. ASSERT_EQ(0U, dec_res_);
  191. }
  192. // Test that decoding a string with too many delimiters fails.
  193. TEST_F(HexEncodeTest, TestDecodeWithDelimiterTooManyDelimiters) {
  194. dec_res_ = hex_decode_with_delimiter(decoded_, 4, "01::23::45::67", 14, ':');
  195. ASSERT_EQ(0U, dec_res_);
  196. }
  197. // Test that decoding a string with a leading delimiter fails.
  198. TEST_F(HexEncodeTest, TestDecodeWithDelimiterLeadingDelimiter) {
  199. dec_res_ = hex_decode_with_delimiter(decoded_, 4, ":01:23:45:67", 12, ':');
  200. ASSERT_EQ(0U, dec_res_);
  201. }
  202. // Test that decoding a string with a trailing delimiter fails.
  203. TEST_F(HexEncodeTest, TestDecodeWithDelimiterTrailingDelimiter) {
  204. dec_res_ = hex_decode_with_delimiter(decoded_, 4, "01:23:45:67:", 12, ':');
  205. ASSERT_EQ(0U, dec_res_);
  206. }
  207. // Tests counting substrings.
  208. TEST(TokenizeTest, CountSubstrings) {
  209. std::vector<std::string> fields;
  210. EXPECT_EQ(5ul, tokenize("one two three four five", ' ', &fields));
  211. fields.clear();
  212. EXPECT_EQ(1ul, tokenize("one", ' ', &fields));
  213. // Extra spaces should be ignored.
  214. fields.clear();
  215. EXPECT_EQ(5ul, tokenize(" one two three four five ", ' ', &fields));
  216. fields.clear();
  217. EXPECT_EQ(1ul, tokenize(" one ", ' ', &fields));
  218. fields.clear();
  219. EXPECT_EQ(0ul, tokenize(" ", ' ', &fields));
  220. }
  221. // Tests comparing substrings.
  222. TEST(TokenizeTest, CompareSubstrings) {
  223. std::vector<std::string> fields;
  224. tokenize("find middle one", ' ', &fields);
  225. ASSERT_EQ(3ul, fields.size());
  226. ASSERT_STREQ("middle", fields.at(1).c_str());
  227. fields.clear();
  228. // Extra spaces should be ignored.
  229. tokenize(" find middle one ", ' ', &fields);
  230. ASSERT_EQ(3ul, fields.size());
  231. ASSERT_STREQ("middle", fields.at(1).c_str());
  232. fields.clear();
  233. tokenize(" ", ' ', &fields);
  234. ASSERT_EQ(0ul, fields.size());
  235. }
  236. TEST(TokenizeTest, TokenizeAppend) {
  237. ASSERT_EQ(0ul, tokenize_append("A B C", ' ', NULL));
  238. std::vector<std::string> fields;
  239. tokenize_append("A B C", ' ', &fields);
  240. ASSERT_EQ(3ul, fields.size());
  241. ASSERT_STREQ("B", fields.at(1).c_str());
  242. tokenize_append("D E", ' ', &fields);
  243. ASSERT_EQ(5ul, fields.size());
  244. ASSERT_STREQ("B", fields.at(1).c_str());
  245. ASSERT_STREQ("E", fields.at(4).c_str());
  246. }
  247. TEST(TokenizeTest, TokenizeWithMarks) {
  248. ASSERT_EQ(0ul, tokenize("D \"A B", ' ', '(', ')', NULL));
  249. std::vector<std::string> fields;
  250. tokenize("A B C", ' ', '"', '"', &fields);
  251. ASSERT_EQ(3ul, fields.size());
  252. ASSERT_STREQ("C", fields.at(2).c_str());
  253. tokenize("\"A B\" C", ' ', '"', '"', &fields);
  254. ASSERT_EQ(2ul, fields.size());
  255. ASSERT_STREQ("A B", fields.at(0).c_str());
  256. tokenize("D \"A B\" C", ' ', '"', '"', &fields);
  257. ASSERT_EQ(3ul, fields.size());
  258. ASSERT_STREQ("D", fields.at(0).c_str());
  259. ASSERT_STREQ("A B", fields.at(1).c_str());
  260. tokenize("D \"A B\" C \"E F\"", ' ', '"', '"', &fields);
  261. ASSERT_EQ(4ul, fields.size());
  262. ASSERT_STREQ("D", fields.at(0).c_str());
  263. ASSERT_STREQ("A B", fields.at(1).c_str());
  264. ASSERT_STREQ("E F", fields.at(3).c_str());
  265. // No matching marks.
  266. tokenize("D \"A B", ' ', '"', '"', &fields);
  267. ASSERT_EQ(3ul, fields.size());
  268. ASSERT_STREQ("D", fields.at(0).c_str());
  269. ASSERT_STREQ("\"A", fields.at(1).c_str());
  270. tokenize("D (A B) C (E F) G", ' ', '(', ')', &fields);
  271. ASSERT_EQ(5ul, fields.size());
  272. ASSERT_STREQ("D", fields.at(0).c_str());
  273. ASSERT_STREQ("A B", fields.at(1).c_str());
  274. ASSERT_STREQ("E F", fields.at(3).c_str());
  275. }
  276. // Tests counting substrings.
  277. TEST(SplitTest, CountSubstrings) {
  278. std::vector<std::string> fields;
  279. EXPECT_EQ(5ul, split("one,two,three,four,five", ',', &fields));
  280. fields.clear();
  281. EXPECT_EQ(1ul, split("one", ',', &fields));
  282. // Empty fields between commas count.
  283. fields.clear();
  284. EXPECT_EQ(5ul, split("one,,three,four,five", ',', &fields));
  285. fields.clear();
  286. EXPECT_EQ(3ul, split(",three,", ',', &fields));
  287. fields.clear();
  288. EXPECT_EQ(1ul, split("", ',', &fields));
  289. }
  290. // Tests comparing substrings.
  291. TEST(SplitTest, CompareSubstrings) {
  292. std::vector<std::string> fields;
  293. split("find,middle,one", ',', &fields);
  294. ASSERT_EQ(3ul, fields.size());
  295. ASSERT_STREQ("middle", fields.at(1).c_str());
  296. fields.clear();
  297. // Empty fields between commas count.
  298. split("find,,middle,one", ',', &fields);
  299. ASSERT_EQ(4ul, fields.size());
  300. ASSERT_STREQ("middle", fields.at(2).c_str());
  301. fields.clear();
  302. split("", ',', &fields);
  303. ASSERT_EQ(1ul, fields.size());
  304. ASSERT_STREQ("", fields.at(0).c_str());
  305. }
  306. } // namespace talk_base