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.

396 lines
12 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/string_encode.h"
  11. #include <string.h>
  12. #include <sstream> // no-presubmit-check TODO(webrtc:8982)
  13. #include "test/gtest.h"
  14. namespace rtc {
  15. class HexEncodeTest : public ::testing::Test {
  16. public:
  17. HexEncodeTest() : dec_res_(0) {
  18. for (size_t i = 0; i < sizeof(data_); ++i) {
  19. data_[i] = (i + 128) & 0xff;
  20. }
  21. memset(decoded_, 0x7f, sizeof(decoded_));
  22. }
  23. char data_[10];
  24. char decoded_[11];
  25. size_t dec_res_;
  26. };
  27. // Test that we can convert to/from hex with no delimiter.
  28. TEST_F(HexEncodeTest, TestWithNoDelimiter) {
  29. std::string encoded = hex_encode(data_, sizeof(data_));
  30. EXPECT_EQ("80818283848586878889", encoded);
  31. dec_res_ =
  32. hex_decode(decoded_, sizeof(decoded_), encoded.data(), encoded.size());
  33. ASSERT_EQ(sizeof(data_), dec_res_);
  34. ASSERT_EQ(0, memcmp(data_, decoded_, dec_res_));
  35. }
  36. // Test that we can convert to/from hex with a colon delimiter.
  37. TEST_F(HexEncodeTest, TestWithDelimiter) {
  38. std::string encoded = hex_encode_with_delimiter(data_, sizeof(data_), ':');
  39. EXPECT_EQ("80:81:82:83:84:85:86:87:88:89", encoded);
  40. dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_),
  41. encoded.data(), encoded.size(), ':');
  42. ASSERT_EQ(sizeof(data_), dec_res_);
  43. ASSERT_EQ(0, memcmp(data_, decoded_, dec_res_));
  44. }
  45. // Test that encoding with one delimiter and decoding with another fails.
  46. TEST_F(HexEncodeTest, TestWithWrongDelimiter) {
  47. std::string encoded = hex_encode_with_delimiter(data_, sizeof(data_), ':');
  48. dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_),
  49. encoded.data(), encoded.size(), '/');
  50. ASSERT_EQ(0U, dec_res_);
  51. }
  52. // Test that encoding without a delimiter and decoding with one fails.
  53. TEST_F(HexEncodeTest, TestExpectedDelimiter) {
  54. std::string encoded = hex_encode(data_, sizeof(data_));
  55. EXPECT_EQ(sizeof(data_) * 2, encoded.size());
  56. dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_),
  57. encoded.data(), encoded.size(), ':');
  58. ASSERT_EQ(0U, dec_res_);
  59. }
  60. // Test that encoding with a delimiter and decoding without one fails.
  61. TEST_F(HexEncodeTest, TestExpectedNoDelimiter) {
  62. std::string encoded = hex_encode_with_delimiter(data_, sizeof(data_), ':');
  63. EXPECT_EQ(sizeof(data_) * 3 - 1, encoded.size());
  64. dec_res_ =
  65. hex_decode(decoded_, sizeof(decoded_), encoded.data(), encoded.size());
  66. ASSERT_EQ(0U, dec_res_);
  67. }
  68. // Test that we handle a zero-length buffer with no delimiter.
  69. TEST_F(HexEncodeTest, TestZeroLengthNoDelimiter) {
  70. std::string encoded = hex_encode("", 0);
  71. EXPECT_TRUE(encoded.empty());
  72. dec_res_ =
  73. hex_decode(decoded_, sizeof(decoded_), encoded.data(), encoded.size());
  74. ASSERT_EQ(0U, dec_res_);
  75. }
  76. // Test that we handle a zero-length buffer with a delimiter.
  77. TEST_F(HexEncodeTest, TestZeroLengthWithDelimiter) {
  78. std::string encoded = hex_encode_with_delimiter("", 0, ':');
  79. EXPECT_TRUE(encoded.empty());
  80. dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_),
  81. encoded.data(), encoded.size(), ':');
  82. ASSERT_EQ(0U, dec_res_);
  83. }
  84. // Test that decoding into a too-small output buffer fails.
  85. TEST_F(HexEncodeTest, TestDecodeTooShort) {
  86. dec_res_ = hex_decode_with_delimiter(decoded_, 4, "0123456789", 10, 0);
  87. ASSERT_EQ(0U, dec_res_);
  88. ASSERT_EQ(0x7f, decoded_[4]);
  89. }
  90. // Test that decoding non-hex data fails.
  91. TEST_F(HexEncodeTest, TestDecodeBogusData) {
  92. dec_res_ =
  93. hex_decode_with_delimiter(decoded_, sizeof(decoded_), "axyz", 4, 0);
  94. ASSERT_EQ(0U, dec_res_);
  95. }
  96. // Test that decoding an odd number of hex characters fails.
  97. TEST_F(HexEncodeTest, TestDecodeOddHexDigits) {
  98. dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_), "012", 3, 0);
  99. ASSERT_EQ(0U, dec_res_);
  100. }
  101. // Test that decoding a string with too many delimiters fails.
  102. TEST_F(HexEncodeTest, TestDecodeWithDelimiterTooManyDelimiters) {
  103. dec_res_ = hex_decode_with_delimiter(decoded_, 4, "01::23::45::67", 14, ':');
  104. ASSERT_EQ(0U, dec_res_);
  105. }
  106. // Test that decoding a string with a leading delimiter fails.
  107. TEST_F(HexEncodeTest, TestDecodeWithDelimiterLeadingDelimiter) {
  108. dec_res_ = hex_decode_with_delimiter(decoded_, 4, ":01:23:45:67", 12, ':');
  109. ASSERT_EQ(0U, dec_res_);
  110. }
  111. // Test that decoding a string with a trailing delimiter fails.
  112. TEST_F(HexEncodeTest, TestDecodeWithDelimiterTrailingDelimiter) {
  113. dec_res_ = hex_decode_with_delimiter(decoded_, 4, "01:23:45:67:", 12, ':');
  114. ASSERT_EQ(0U, dec_res_);
  115. }
  116. // Tests counting substrings.
  117. TEST(TokenizeTest, CountSubstrings) {
  118. std::vector<std::string> fields;
  119. EXPECT_EQ(5ul, tokenize("one two three four five", ' ', &fields));
  120. fields.clear();
  121. EXPECT_EQ(1ul, tokenize("one", ' ', &fields));
  122. // Extra spaces should be ignored.
  123. fields.clear();
  124. EXPECT_EQ(5ul, tokenize(" one two three four five ", ' ', &fields));
  125. fields.clear();
  126. EXPECT_EQ(1ul, tokenize(" one ", ' ', &fields));
  127. fields.clear();
  128. EXPECT_EQ(0ul, tokenize(" ", ' ', &fields));
  129. }
  130. // Tests comparing substrings.
  131. TEST(TokenizeTest, CompareSubstrings) {
  132. std::vector<std::string> fields;
  133. tokenize("find middle one", ' ', &fields);
  134. ASSERT_EQ(3ul, fields.size());
  135. ASSERT_STREQ("middle", fields.at(1).c_str());
  136. fields.clear();
  137. // Extra spaces should be ignored.
  138. tokenize(" find middle one ", ' ', &fields);
  139. ASSERT_EQ(3ul, fields.size());
  140. ASSERT_STREQ("middle", fields.at(1).c_str());
  141. fields.clear();
  142. tokenize(" ", ' ', &fields);
  143. ASSERT_EQ(0ul, fields.size());
  144. }
  145. TEST(TokenizeTest, TokenizeAppend) {
  146. ASSERT_EQ(0ul, tokenize_append("A B C", ' ', nullptr));
  147. std::vector<std::string> fields;
  148. tokenize_append("A B C", ' ', &fields);
  149. ASSERT_EQ(3ul, fields.size());
  150. ASSERT_STREQ("B", fields.at(1).c_str());
  151. tokenize_append("D E", ' ', &fields);
  152. ASSERT_EQ(5ul, fields.size());
  153. ASSERT_STREQ("B", fields.at(1).c_str());
  154. ASSERT_STREQ("E", fields.at(4).c_str());
  155. }
  156. TEST(TokenizeTest, TokenizeWithMarks) {
  157. ASSERT_EQ(0ul, tokenize("D \"A B", ' ', '(', ')', nullptr));
  158. std::vector<std::string> fields;
  159. tokenize("A B C", ' ', '"', '"', &fields);
  160. ASSERT_EQ(3ul, fields.size());
  161. ASSERT_STREQ("C", fields.at(2).c_str());
  162. tokenize("\"A B\" C", ' ', '"', '"', &fields);
  163. ASSERT_EQ(2ul, fields.size());
  164. ASSERT_STREQ("A B", fields.at(0).c_str());
  165. tokenize("D \"A B\" C", ' ', '"', '"', &fields);
  166. ASSERT_EQ(3ul, fields.size());
  167. ASSERT_STREQ("D", fields.at(0).c_str());
  168. ASSERT_STREQ("A B", fields.at(1).c_str());
  169. tokenize("D \"A B\" C \"E F\"", ' ', '"', '"', &fields);
  170. ASSERT_EQ(4ul, fields.size());
  171. ASSERT_STREQ("D", fields.at(0).c_str());
  172. ASSERT_STREQ("A B", fields.at(1).c_str());
  173. ASSERT_STREQ("E F", fields.at(3).c_str());
  174. // No matching marks.
  175. tokenize("D \"A B", ' ', '"', '"', &fields);
  176. ASSERT_EQ(3ul, fields.size());
  177. ASSERT_STREQ("D", fields.at(0).c_str());
  178. ASSERT_STREQ("\"A", fields.at(1).c_str());
  179. tokenize("D (A B) C (E F) G", ' ', '(', ')', &fields);
  180. ASSERT_EQ(5ul, fields.size());
  181. ASSERT_STREQ("D", fields.at(0).c_str());
  182. ASSERT_STREQ("A B", fields.at(1).c_str());
  183. ASSERT_STREQ("E F", fields.at(3).c_str());
  184. }
  185. TEST(TokenizeTest, TokenizeWithEmptyTokens) {
  186. std::vector<std::string> fields;
  187. EXPECT_EQ(3ul, tokenize_with_empty_tokens("a.b.c", '.', &fields));
  188. EXPECT_EQ("a", fields[0]);
  189. EXPECT_EQ("b", fields[1]);
  190. EXPECT_EQ("c", fields[2]);
  191. EXPECT_EQ(3ul, tokenize_with_empty_tokens("..c", '.', &fields));
  192. EXPECT_TRUE(fields[0].empty());
  193. EXPECT_TRUE(fields[1].empty());
  194. EXPECT_EQ("c", fields[2]);
  195. EXPECT_EQ(1ul, tokenize_with_empty_tokens("", '.', &fields));
  196. EXPECT_TRUE(fields[0].empty());
  197. }
  198. TEST(TokenizeFirstTest, NoLeadingSpaces) {
  199. std::string token;
  200. std::string rest;
  201. ASSERT_TRUE(tokenize_first("A &*${}", ' ', &token, &rest));
  202. ASSERT_STREQ("A", token.c_str());
  203. ASSERT_STREQ("&*${}", rest.c_str());
  204. ASSERT_TRUE(tokenize_first("A B& *${}", ' ', &token, &rest));
  205. ASSERT_STREQ("A", token.c_str());
  206. ASSERT_STREQ("B& *${}", rest.c_str());
  207. ASSERT_TRUE(tokenize_first("A B& *${} ", ' ', &token, &rest));
  208. ASSERT_STREQ("A", token.c_str());
  209. ASSERT_STREQ("B& *${} ", rest.c_str());
  210. }
  211. TEST(TokenizeFirstTest, LeadingSpaces) {
  212. std::string token;
  213. std::string rest;
  214. ASSERT_TRUE(tokenize_first(" A B C", ' ', &token, &rest));
  215. ASSERT_STREQ("", token.c_str());
  216. ASSERT_STREQ("A B C", rest.c_str());
  217. ASSERT_TRUE(tokenize_first(" A B C ", ' ', &token, &rest));
  218. ASSERT_STREQ("", token.c_str());
  219. ASSERT_STREQ("A B C ", rest.c_str());
  220. }
  221. TEST(TokenizeFirstTest, SingleToken) {
  222. std::string token;
  223. std::string rest;
  224. // In the case where we cannot find delimiter the whole string is a token.
  225. ASSERT_FALSE(tokenize_first("ABC", ' ', &token, &rest));
  226. ASSERT_TRUE(tokenize_first("ABC ", ' ', &token, &rest));
  227. ASSERT_STREQ("ABC", token.c_str());
  228. ASSERT_STREQ("", rest.c_str());
  229. ASSERT_TRUE(tokenize_first(" ABC ", ' ', &token, &rest));
  230. ASSERT_STREQ("", token.c_str());
  231. ASSERT_STREQ("ABC ", rest.c_str());
  232. }
  233. // Tests counting substrings.
  234. TEST(SplitTest, CountSubstrings) {
  235. std::vector<std::string> fields;
  236. EXPECT_EQ(5ul, split("one,two,three,four,five", ',', &fields));
  237. fields.clear();
  238. EXPECT_EQ(1ul, split("one", ',', &fields));
  239. // Empty fields between commas count.
  240. fields.clear();
  241. EXPECT_EQ(5ul, split("one,,three,four,five", ',', &fields));
  242. fields.clear();
  243. EXPECT_EQ(3ul, split(",three,", ',', &fields));
  244. fields.clear();
  245. EXPECT_EQ(1ul, split("", ',', &fields));
  246. }
  247. // Tests comparing substrings.
  248. TEST(SplitTest, CompareSubstrings) {
  249. std::vector<std::string> fields;
  250. split("find,middle,one", ',', &fields);
  251. ASSERT_EQ(3ul, fields.size());
  252. ASSERT_STREQ("middle", fields.at(1).c_str());
  253. fields.clear();
  254. // Empty fields between commas count.
  255. split("find,,middle,one", ',', &fields);
  256. ASSERT_EQ(4ul, fields.size());
  257. ASSERT_STREQ("middle", fields.at(2).c_str());
  258. fields.clear();
  259. split("", ',', &fields);
  260. ASSERT_EQ(1ul, fields.size());
  261. ASSERT_STREQ("", fields.at(0).c_str());
  262. }
  263. TEST(ToString, SanityCheck) {
  264. EXPECT_EQ(ToString(true), "true");
  265. EXPECT_EQ(ToString(false), "false");
  266. const char* c = "message";
  267. EXPECT_EQ(ToString(c), c);
  268. EXPECT_EQ(ToString(std::string(c)), c);
  269. EXPECT_EQ(ToString(short{-123}), "-123");
  270. EXPECT_EQ(ToString((unsigned short)123), "123");
  271. EXPECT_EQ(ToString(int{-123}), "-123");
  272. EXPECT_EQ(ToString((unsigned int)123), "123");
  273. EXPECT_EQ(ToString((long int)-123), "-123");
  274. EXPECT_EQ(ToString((unsigned long int)123), "123");
  275. EXPECT_EQ(ToString((long long int)-123), "-123");
  276. EXPECT_EQ(ToString((unsigned long long int)123), "123");
  277. int i = 10;
  278. int* p = &i;
  279. std::ostringstream s; // no-presubmit-check TODO(webrtc:8982)
  280. s << p;
  281. EXPECT_EQ(s.str(), ToString(p));
  282. EXPECT_EQ(ToString(0.5), "0.5");
  283. }
  284. template <typename T>
  285. void ParsesTo(std::string s, T t) {
  286. T value;
  287. EXPECT_TRUE(FromString(s, &value));
  288. EXPECT_EQ(value, t);
  289. }
  290. TEST(FromString, DecodeValid) {
  291. ParsesTo("true", true);
  292. ParsesTo("false", false);
  293. ParsesTo("105", 105);
  294. ParsesTo("0.25", 0.25);
  295. }
  296. template <typename T>
  297. void FailsToParse(std::string s) {
  298. T value;
  299. EXPECT_FALSE(FromString(s, &value)) << "[" << s << "]";
  300. }
  301. TEST(FromString, DecodeInvalid) {
  302. FailsToParse<bool>("True");
  303. FailsToParse<bool>("0");
  304. FailsToParse<bool>("yes");
  305. FailsToParse<int>("0.5");
  306. FailsToParse<int>("XIV");
  307. FailsToParse<double>("");
  308. FailsToParse<double>(" ");
  309. FailsToParse<int>("1 2");
  310. }
  311. template <typename T>
  312. void RoundTrip(T t) {
  313. std::string s = ToString(t);
  314. T value;
  315. EXPECT_TRUE(FromString(s, &value));
  316. EXPECT_EQ(value, t);
  317. }
  318. TEST(FromString, RoundTrip) {
  319. RoundTrip<int>(123);
  320. RoundTrip(false);
  321. RoundTrip(true);
  322. RoundTrip(0.5);
  323. RoundTrip(-15l);
  324. }
  325. } // namespace rtc