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