OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/quic/platform/api/quic_text_utils.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/strings/string_piece.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 using base::StringPiece; |
| 13 using std::string; |
| 14 |
| 15 namespace net { |
| 16 namespace test { |
| 17 |
| 18 TEST(QuicTextUtilsText, StartsWith) { |
| 19 EXPECT_TRUE(QuicTextUtils::StartsWith("hello world", "hello")); |
| 20 EXPECT_TRUE(QuicTextUtils::StartsWith("hello world", "hello world")); |
| 21 EXPECT_TRUE(QuicTextUtils::StartsWith("hello world", "")); |
| 22 EXPECT_FALSE(QuicTextUtils::StartsWith("hello world", "Hello")); |
| 23 EXPECT_FALSE(QuicTextUtils::StartsWith("hello world", "world")); |
| 24 EXPECT_FALSE(QuicTextUtils::StartsWith("hello world", "bar")); |
| 25 } |
| 26 |
| 27 TEST(QuicTextUtilsText, EndsWithIgnoreCase) { |
| 28 EXPECT_TRUE(QuicTextUtils::EndsWithIgnoreCase("hello world", "world")); |
| 29 EXPECT_TRUE(QuicTextUtils::EndsWithIgnoreCase("hello world", "hello world")); |
| 30 EXPECT_TRUE(QuicTextUtils::EndsWithIgnoreCase("hello world", "")); |
| 31 EXPECT_TRUE(QuicTextUtils::EndsWithIgnoreCase("hello world", "WORLD")); |
| 32 EXPECT_FALSE(QuicTextUtils::EndsWithIgnoreCase("hello world", "hello")); |
| 33 } |
| 34 |
| 35 TEST(QuicTextUtilsText, ToLower) { |
| 36 EXPECT_EQ("lower", QuicTextUtils::ToLower("LOWER")); |
| 37 EXPECT_EQ("lower", QuicTextUtils::ToLower("lower")); |
| 38 EXPECT_EQ("lower", QuicTextUtils::ToLower("lOwEr")); |
| 39 EXPECT_EQ("123", QuicTextUtils::ToLower("123")); |
| 40 EXPECT_EQ("", QuicTextUtils::ToLower("")); |
| 41 } |
| 42 |
| 43 TEST(QuicTextUtilsText, RemoveLeadingAndTrailingWhitespace) { |
| 44 string input; |
| 45 |
| 46 for (auto input : {"text", " text", " text", "text ", "text ", " text ", |
| 47 " text ", "\r\n\ttext", "text\n\r\t"}) { |
| 48 StringPiece piece(input); |
| 49 QuicTextUtils::RemoveLeadingAndTrailingWhitespace(&piece); |
| 50 EXPECT_EQ("text", piece); |
| 51 } |
| 52 } |
| 53 |
| 54 TEST(QuicTextUtilsText, StringToUint64) { |
| 55 uint64_t val = 0; |
| 56 EXPECT_TRUE(QuicTextUtils::StringToUint64("123", &val)); |
| 57 EXPECT_EQ(123u, val); |
| 58 EXPECT_TRUE(QuicTextUtils::StringToUint64("1234", &val)); |
| 59 EXPECT_EQ(1234u, val); |
| 60 EXPECT_FALSE(QuicTextUtils::StringToUint64("", &val)); |
| 61 EXPECT_FALSE(QuicTextUtils::StringToUint64("-123", &val)); |
| 62 EXPECT_FALSE(QuicTextUtils::StringToUint64("-123.0", &val)); |
| 63 } |
| 64 |
| 65 TEST(QuicTextUtilsText, Uint64ToString) { |
| 66 EXPECT_EQ("123", QuicTextUtils::Uint64ToString(123)); |
| 67 EXPECT_EQ("1234", QuicTextUtils::Uint64ToString(1234)); |
| 68 } |
| 69 |
| 70 TEST(QuicTextUtilsText, HexEncode) { |
| 71 EXPECT_EQ("48656c6c6f", QuicTextUtils::HexEncode("Hello", 5)); |
| 72 EXPECT_EQ("48656c6c6f", QuicTextUtils::HexEncode("Hello World", 5)); |
| 73 EXPECT_EQ("48656c6c6f", QuicTextUtils::HexEncode("Hello")); |
| 74 } |
| 75 |
| 76 TEST(QuicTextUtilsText, HexDecode) { |
| 77 EXPECT_EQ("Hello", QuicTextUtils::HexDecode("48656c6c6f")); |
| 78 EXPECT_EQ("", QuicTextUtils::HexDecode("")); |
| 79 } |
| 80 |
| 81 TEST(QuicTextUtilsText, HexDump) { |
| 82 // Verify output of the HexDump method is as expected. |
| 83 char packet[] = { |
| 84 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x51, 0x55, 0x49, 0x43, 0x21, |
| 85 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, |
| 86 0x20, 0x73, 0x68, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x6c, |
| 87 0x6f, 0x6e, 0x67, 0x20, 0x65, 0x6e, 0x6f, 0x75, 0x67, 0x68, 0x20, 0x74, |
| 88 0x6f, 0x20, 0x73, 0x70, 0x61, 0x6e, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, |
| 89 0x70, 0x6c, 0x65, 0x20, 0x6c, 0x69, 0x6e, 0x65, 0x73, 0x20, 0x6f, 0x66, |
| 90 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x2e, 0x01, 0x02, 0x03, 0x00, |
| 91 }; |
| 92 EXPECT_EQ( |
| 93 QuicTextUtils::HexDump(packet), |
| 94 "0x0000: 4865 6c6c 6f2c 2051 5549 4321 2054 6869 Hello,.QUIC!.Thi\n" |
| 95 "0x0010: 7320 7374 7269 6e67 2073 686f 756c 6420 s.string.should.\n" |
| 96 "0x0020: 6265 206c 6f6e 6720 656e 6f75 6768 2074 be.long.enough.t\n" |
| 97 "0x0030: 6f20 7370 616e 206d 756c 7469 706c 6520 o.span.multiple.\n" |
| 98 "0x0040: 6c69 6e65 7320 6f66 206f 7574 7075 742e lines.of.output.\n" |
| 99 "0x0050: 0102 03 ...\n"); |
| 100 } |
| 101 |
| 102 TEST(QuicTextUtilsText, Base64Encode) { |
| 103 string output; |
| 104 string input = "Hello"; |
| 105 QuicTextUtils::Base64Encode(reinterpret_cast<const uint8_t*>(input.data()), |
| 106 input.length(), &output); |
| 107 EXPECT_EQ("SGVsbG8", output); |
| 108 |
| 109 input = |
| 110 "Hello, QUIC! This string should be long enough to span" |
| 111 "multiple lines of output\n"; |
| 112 QuicTextUtils::Base64Encode(reinterpret_cast<const uint8_t*>(input.data()), |
| 113 input.length(), &output); |
| 114 EXPECT_EQ( |
| 115 "SGVsbG8sIFFVSUMhIFRoaXMgc3RyaW5nIHNob3VsZCBiZSBsb25n" |
| 116 "IGVub3VnaCB0byBzcGFubXVsdGlwbGUgbGluZXMgb2Ygb3V0cHV0Cg", |
| 117 output); |
| 118 } |
| 119 |
| 120 TEST(QuicTextUtilsText, ContainsUpperCase) { |
| 121 EXPECT_FALSE(QuicTextUtils::ContainsUpperCase("abc")); |
| 122 EXPECT_FALSE(QuicTextUtils::ContainsUpperCase("")); |
| 123 EXPECT_FALSE(QuicTextUtils::ContainsUpperCase("123")); |
| 124 EXPECT_TRUE(QuicTextUtils::ContainsUpperCase("ABC")); |
| 125 EXPECT_TRUE(QuicTextUtils::ContainsUpperCase("aBc")); |
| 126 } |
| 127 |
| 128 TEST(QuicTextUtilsText, Split) { |
| 129 EXPECT_EQ(std::vector<StringPiece>({"a", "b", "c"}), |
| 130 QuicTextUtils::Split("a,b,c", ',')); |
| 131 EXPECT_EQ(std::vector<StringPiece>({"a", "b", "c"}), |
| 132 QuicTextUtils::Split("a:b:c", ':')); |
| 133 EXPECT_EQ(std::vector<StringPiece>({"a:b:c"}), |
| 134 QuicTextUtils::Split("a:b:c", ',')); |
| 135 } |
| 136 |
| 137 } // namespace test |
| 138 } // namespace net |
OLD | NEW |