| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/quic/platform/api/quic_text_utils.h" | 5 #include "net/quic/platform/api/quic_text_utils.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "net/quic/platform/api/quic_test.h" |
| 10 | 10 |
| 11 using std::string; | 11 using std::string; |
| 12 | 12 |
| 13 namespace net { | 13 namespace net { |
| 14 namespace test { | 14 namespace test { |
| 15 | 15 |
| 16 TEST(QuicTextUtilsText, StartsWith) { | 16 class QuicTextUtilsText : public QuicTest {}; |
| 17 |
| 18 TEST_F(QuicTextUtilsText, StartsWith) { |
| 17 EXPECT_TRUE(QuicTextUtils::StartsWith("hello world", "hello")); | 19 EXPECT_TRUE(QuicTextUtils::StartsWith("hello world", "hello")); |
| 18 EXPECT_TRUE(QuicTextUtils::StartsWith("hello world", "hello world")); | 20 EXPECT_TRUE(QuicTextUtils::StartsWith("hello world", "hello world")); |
| 19 EXPECT_TRUE(QuicTextUtils::StartsWith("hello world", "")); | 21 EXPECT_TRUE(QuicTextUtils::StartsWith("hello world", "")); |
| 20 EXPECT_FALSE(QuicTextUtils::StartsWith("hello world", "Hello")); | 22 EXPECT_FALSE(QuicTextUtils::StartsWith("hello world", "Hello")); |
| 21 EXPECT_FALSE(QuicTextUtils::StartsWith("hello world", "world")); | 23 EXPECT_FALSE(QuicTextUtils::StartsWith("hello world", "world")); |
| 22 EXPECT_FALSE(QuicTextUtils::StartsWith("hello world", "bar")); | 24 EXPECT_FALSE(QuicTextUtils::StartsWith("hello world", "bar")); |
| 23 } | 25 } |
| 24 | 26 |
| 25 TEST(QuicTextUtilsText, EndsWithIgnoreCase) { | 27 TEST_F(QuicTextUtilsText, EndsWithIgnoreCase) { |
| 26 EXPECT_TRUE(QuicTextUtils::EndsWithIgnoreCase("hello world", "world")); | 28 EXPECT_TRUE(QuicTextUtils::EndsWithIgnoreCase("hello world", "world")); |
| 27 EXPECT_TRUE(QuicTextUtils::EndsWithIgnoreCase("hello world", "hello world")); | 29 EXPECT_TRUE(QuicTextUtils::EndsWithIgnoreCase("hello world", "hello world")); |
| 28 EXPECT_TRUE(QuicTextUtils::EndsWithIgnoreCase("hello world", "")); | 30 EXPECT_TRUE(QuicTextUtils::EndsWithIgnoreCase("hello world", "")); |
| 29 EXPECT_TRUE(QuicTextUtils::EndsWithIgnoreCase("hello world", "WORLD")); | 31 EXPECT_TRUE(QuicTextUtils::EndsWithIgnoreCase("hello world", "WORLD")); |
| 30 EXPECT_FALSE(QuicTextUtils::EndsWithIgnoreCase("hello world", "hello")); | 32 EXPECT_FALSE(QuicTextUtils::EndsWithIgnoreCase("hello world", "hello")); |
| 31 } | 33 } |
| 32 | 34 |
| 33 TEST(QuicTextUtilsText, ToLower) { | 35 TEST_F(QuicTextUtilsText, ToLower) { |
| 34 EXPECT_EQ("lower", QuicTextUtils::ToLower("LOWER")); | 36 EXPECT_EQ("lower", QuicTextUtils::ToLower("LOWER")); |
| 35 EXPECT_EQ("lower", QuicTextUtils::ToLower("lower")); | 37 EXPECT_EQ("lower", QuicTextUtils::ToLower("lower")); |
| 36 EXPECT_EQ("lower", QuicTextUtils::ToLower("lOwEr")); | 38 EXPECT_EQ("lower", QuicTextUtils::ToLower("lOwEr")); |
| 37 EXPECT_EQ("123", QuicTextUtils::ToLower("123")); | 39 EXPECT_EQ("123", QuicTextUtils::ToLower("123")); |
| 38 EXPECT_EQ("", QuicTextUtils::ToLower("")); | 40 EXPECT_EQ("", QuicTextUtils::ToLower("")); |
| 39 } | 41 } |
| 40 | 42 |
| 41 TEST(QuicTextUtilsText, RemoveLeadingAndTrailingWhitespace) { | 43 TEST_F(QuicTextUtilsText, RemoveLeadingAndTrailingWhitespace) { |
| 42 string input; | 44 string input; |
| 43 | 45 |
| 44 for (auto* input : {"text", " text", " text", "text ", "text ", " text ", | 46 for (auto* input : {"text", " text", " text", "text ", "text ", " text ", |
| 45 " text ", "\r\n\ttext", "text\n\r\t"}) { | 47 " text ", "\r\n\ttext", "text\n\r\t"}) { |
| 46 QuicStringPiece piece(input); | 48 QuicStringPiece piece(input); |
| 47 QuicTextUtils::RemoveLeadingAndTrailingWhitespace(&piece); | 49 QuicTextUtils::RemoveLeadingAndTrailingWhitespace(&piece); |
| 48 EXPECT_EQ("text", piece); | 50 EXPECT_EQ("text", piece); |
| 49 } | 51 } |
| 50 } | 52 } |
| 51 | 53 |
| 52 TEST(QuicTextUtilsText, StringToNumbers) { | 54 TEST_F(QuicTextUtilsText, StringToNumbers) { |
| 53 const string kMaxInt32Plus1 = "2147483648"; | 55 const string kMaxInt32Plus1 = "2147483648"; |
| 54 const string kMinInt32Minus1 = "-2147483649"; | 56 const string kMinInt32Minus1 = "-2147483649"; |
| 55 const string kMaxUint32Plus1 = "4294967296"; | 57 const string kMaxUint32Plus1 = "4294967296"; |
| 56 | 58 |
| 57 { | 59 { |
| 58 // StringToUint64 | 60 // StringToUint64 |
| 59 uint64_t uint64_val = 0; | 61 uint64_t uint64_val = 0; |
| 60 EXPECT_TRUE(QuicTextUtils::StringToUint64("123", &uint64_val)); | 62 EXPECT_TRUE(QuicTextUtils::StringToUint64("123", &uint64_val)); |
| 61 EXPECT_EQ(123u, uint64_val); | 63 EXPECT_EQ(123u, uint64_val); |
| 62 EXPECT_TRUE(QuicTextUtils::StringToUint64("1234", &uint64_val)); | 64 EXPECT_TRUE(QuicTextUtils::StringToUint64("1234", &uint64_val)); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 EXPECT_FALSE(QuicTextUtils::StringToSizeT("-123.0", &size_t_val)); | 117 EXPECT_FALSE(QuicTextUtils::StringToSizeT("-123.0", &size_t_val)); |
| 116 if (sizeof(size_t) > 4) { | 118 if (sizeof(size_t) > 4) { |
| 117 EXPECT_TRUE(QuicTextUtils::StringToSizeT(kMaxUint32Plus1, &size_t_val)); | 119 EXPECT_TRUE(QuicTextUtils::StringToSizeT(kMaxUint32Plus1, &size_t_val)); |
| 118 EXPECT_EQ(4294967296ull, size_t_val); | 120 EXPECT_EQ(4294967296ull, size_t_val); |
| 119 } else { | 121 } else { |
| 120 EXPECT_FALSE(QuicTextUtils::StringToSizeT(kMaxUint32Plus1, &size_t_val)); | 122 EXPECT_FALSE(QuicTextUtils::StringToSizeT(kMaxUint32Plus1, &size_t_val)); |
| 121 } | 123 } |
| 122 } | 124 } |
| 123 } | 125 } |
| 124 | 126 |
| 125 TEST(QuicTextUtilsText, Uint64ToString) { | 127 TEST_F(QuicTextUtilsText, Uint64ToString) { |
| 126 EXPECT_EQ("123", QuicTextUtils::Uint64ToString(123)); | 128 EXPECT_EQ("123", QuicTextUtils::Uint64ToString(123)); |
| 127 EXPECT_EQ("1234", QuicTextUtils::Uint64ToString(1234)); | 129 EXPECT_EQ("1234", QuicTextUtils::Uint64ToString(1234)); |
| 128 } | 130 } |
| 129 | 131 |
| 130 TEST(QuicTextUtilsText, HexEncode) { | 132 TEST_F(QuicTextUtilsText, HexEncode) { |
| 131 EXPECT_EQ("48656c6c6f", QuicTextUtils::HexEncode("Hello", 5)); | 133 EXPECT_EQ("48656c6c6f", QuicTextUtils::HexEncode("Hello", 5)); |
| 132 EXPECT_EQ("48656c6c6f", QuicTextUtils::HexEncode("Hello World", 5)); | 134 EXPECT_EQ("48656c6c6f", QuicTextUtils::HexEncode("Hello World", 5)); |
| 133 EXPECT_EQ("48656c6c6f", QuicTextUtils::HexEncode("Hello")); | 135 EXPECT_EQ("48656c6c6f", QuicTextUtils::HexEncode("Hello")); |
| 134 EXPECT_EQ("0102779cfa", QuicTextUtils::HexEncode("\x01\x02\x77\x9c\xfa")); | 136 EXPECT_EQ("0102779cfa", QuicTextUtils::HexEncode("\x01\x02\x77\x9c\xfa")); |
| 135 } | 137 } |
| 136 | 138 |
| 137 TEST(QuicTextUtilsText, HexDecode) { | 139 TEST_F(QuicTextUtilsText, HexDecode) { |
| 138 EXPECT_EQ("Hello", QuicTextUtils::HexDecode("48656c6c6f")); | 140 EXPECT_EQ("Hello", QuicTextUtils::HexDecode("48656c6c6f")); |
| 139 EXPECT_EQ("", QuicTextUtils::HexDecode("")); | 141 EXPECT_EQ("", QuicTextUtils::HexDecode("")); |
| 140 EXPECT_EQ("\x01\x02\x77\x9c\xfa", QuicTextUtils::HexDecode("0102779cfa")); | 142 EXPECT_EQ("\x01\x02\x77\x9c\xfa", QuicTextUtils::HexDecode("0102779cfa")); |
| 141 } | 143 } |
| 142 | 144 |
| 143 TEST(QuicTextUtilsText, HexDump) { | 145 TEST_F(QuicTextUtilsText, HexDump) { |
| 144 // Verify output of the HexDump method is as expected. | 146 // Verify output of the HexDump method is as expected. |
| 145 char packet[] = { | 147 char packet[] = { |
| 146 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x51, 0x55, 0x49, 0x43, 0x21, | 148 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x51, 0x55, 0x49, 0x43, 0x21, |
| 147 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, | 149 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 148 0x20, 0x73, 0x68, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x6c, | 150 0x20, 0x73, 0x68, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x6c, |
| 149 0x6f, 0x6e, 0x67, 0x20, 0x65, 0x6e, 0x6f, 0x75, 0x67, 0x68, 0x20, 0x74, | 151 0x6f, 0x6e, 0x67, 0x20, 0x65, 0x6e, 0x6f, 0x75, 0x67, 0x68, 0x20, 0x74, |
| 150 0x6f, 0x20, 0x73, 0x70, 0x61, 0x6e, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, | 152 0x6f, 0x20, 0x73, 0x70, 0x61, 0x6e, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, |
| 151 0x70, 0x6c, 0x65, 0x20, 0x6c, 0x69, 0x6e, 0x65, 0x73, 0x20, 0x6f, 0x66, | 153 0x70, 0x6c, 0x65, 0x20, 0x6c, 0x69, 0x6e, 0x65, 0x73, 0x20, 0x6f, 0x66, |
| 152 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x2e, 0x01, 0x02, 0x03, 0x00, | 154 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x2e, 0x01, 0x02, 0x03, 0x00, |
| 153 }; | 155 }; |
| 154 EXPECT_EQ( | 156 EXPECT_EQ( |
| 155 QuicTextUtils::HexDump(packet), | 157 QuicTextUtils::HexDump(packet), |
| 156 "0x0000: 4865 6c6c 6f2c 2051 5549 4321 2054 6869 Hello,.QUIC!.Thi\n" | 158 "0x0000: 4865 6c6c 6f2c 2051 5549 4321 2054 6869 Hello,.QUIC!.Thi\n" |
| 157 "0x0010: 7320 7374 7269 6e67 2073 686f 756c 6420 s.string.should.\n" | 159 "0x0010: 7320 7374 7269 6e67 2073 686f 756c 6420 s.string.should.\n" |
| 158 "0x0020: 6265 206c 6f6e 6720 656e 6f75 6768 2074 be.long.enough.t\n" | 160 "0x0020: 6265 206c 6f6e 6720 656e 6f75 6768 2074 be.long.enough.t\n" |
| 159 "0x0030: 6f20 7370 616e 206d 756c 7469 706c 6520 o.span.multiple.\n" | 161 "0x0030: 6f20 7370 616e 206d 756c 7469 706c 6520 o.span.multiple.\n" |
| 160 "0x0040: 6c69 6e65 7320 6f66 206f 7574 7075 742e lines.of.output.\n" | 162 "0x0040: 6c69 6e65 7320 6f66 206f 7574 7075 742e lines.of.output.\n" |
| 161 "0x0050: 0102 03 ...\n"); | 163 "0x0050: 0102 03 ...\n"); |
| 162 } | 164 } |
| 163 | 165 |
| 164 TEST(QuicTextUtilsText, Base64Encode) { | 166 TEST_F(QuicTextUtilsText, Base64Encode) { |
| 165 string output; | 167 string output; |
| 166 string input = "Hello"; | 168 string input = "Hello"; |
| 167 QuicTextUtils::Base64Encode(reinterpret_cast<const uint8_t*>(input.data()), | 169 QuicTextUtils::Base64Encode(reinterpret_cast<const uint8_t*>(input.data()), |
| 168 input.length(), &output); | 170 input.length(), &output); |
| 169 EXPECT_EQ("SGVsbG8", output); | 171 EXPECT_EQ("SGVsbG8", output); |
| 170 | 172 |
| 171 input = | 173 input = |
| 172 "Hello, QUIC! This string should be long enough to span" | 174 "Hello, QUIC! This string should be long enough to span" |
| 173 "multiple lines of output\n"; | 175 "multiple lines of output\n"; |
| 174 QuicTextUtils::Base64Encode(reinterpret_cast<const uint8_t*>(input.data()), | 176 QuicTextUtils::Base64Encode(reinterpret_cast<const uint8_t*>(input.data()), |
| 175 input.length(), &output); | 177 input.length(), &output); |
| 176 EXPECT_EQ( | 178 EXPECT_EQ( |
| 177 "SGVsbG8sIFFVSUMhIFRoaXMgc3RyaW5nIHNob3VsZCBiZSBsb25n" | 179 "SGVsbG8sIFFVSUMhIFRoaXMgc3RyaW5nIHNob3VsZCBiZSBsb25n" |
| 178 "IGVub3VnaCB0byBzcGFubXVsdGlwbGUgbGluZXMgb2Ygb3V0cHV0Cg", | 180 "IGVub3VnaCB0byBzcGFubXVsdGlwbGUgbGluZXMgb2Ygb3V0cHV0Cg", |
| 179 output); | 181 output); |
| 180 } | 182 } |
| 181 | 183 |
| 182 TEST(QuicTextUtilsText, ContainsUpperCase) { | 184 TEST_F(QuicTextUtilsText, ContainsUpperCase) { |
| 183 EXPECT_FALSE(QuicTextUtils::ContainsUpperCase("abc")); | 185 EXPECT_FALSE(QuicTextUtils::ContainsUpperCase("abc")); |
| 184 EXPECT_FALSE(QuicTextUtils::ContainsUpperCase("")); | 186 EXPECT_FALSE(QuicTextUtils::ContainsUpperCase("")); |
| 185 EXPECT_FALSE(QuicTextUtils::ContainsUpperCase("123")); | 187 EXPECT_FALSE(QuicTextUtils::ContainsUpperCase("123")); |
| 186 EXPECT_TRUE(QuicTextUtils::ContainsUpperCase("ABC")); | 188 EXPECT_TRUE(QuicTextUtils::ContainsUpperCase("ABC")); |
| 187 EXPECT_TRUE(QuicTextUtils::ContainsUpperCase("aBc")); | 189 EXPECT_TRUE(QuicTextUtils::ContainsUpperCase("aBc")); |
| 188 } | 190 } |
| 189 | 191 |
| 190 TEST(QuicTextUtilsText, Split) { | 192 TEST_F(QuicTextUtilsText, Split) { |
| 191 EXPECT_EQ(std::vector<QuicStringPiece>({"a", "b", "c"}), | 193 EXPECT_EQ(std::vector<QuicStringPiece>({"a", "b", "c"}), |
| 192 QuicTextUtils::Split("a,b,c", ',')); | 194 QuicTextUtils::Split("a,b,c", ',')); |
| 193 EXPECT_EQ(std::vector<QuicStringPiece>({"a", "b", "c"}), | 195 EXPECT_EQ(std::vector<QuicStringPiece>({"a", "b", "c"}), |
| 194 QuicTextUtils::Split("a:b:c", ':')); | 196 QuicTextUtils::Split("a:b:c", ':')); |
| 195 EXPECT_EQ(std::vector<QuicStringPiece>({"a:b:c"}), | 197 EXPECT_EQ(std::vector<QuicStringPiece>({"a:b:c"}), |
| 196 QuicTextUtils::Split("a:b:c", ',')); | 198 QuicTextUtils::Split("a:b:c", ',')); |
| 197 } | 199 } |
| 198 | 200 |
| 199 } // namespace test | 201 } // namespace test |
| 200 } // namespace net | 202 } // namespace net |
| OLD | NEW |