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 "quiche/common/quiche_text_utils.h"
6
7 #include <string>
8
9 #include "absl/strings/escaping.h"
10 #include "quiche/common/platform/api/quiche_test.h"
11
12 namespace quiche {
13 namespace test {
14
TEST(QuicheTextUtilsTest,ToLower)15 TEST(QuicheTextUtilsTest, ToLower) {
16 EXPECT_EQ("lower", quiche::QuicheTextUtils::ToLower("LOWER"));
17 EXPECT_EQ("lower", quiche::QuicheTextUtils::ToLower("lower"));
18 EXPECT_EQ("lower", quiche::QuicheTextUtils::ToLower("lOwEr"));
19 EXPECT_EQ("123", quiche::QuicheTextUtils::ToLower("123"));
20 EXPECT_EQ("", quiche::QuicheTextUtils::ToLower(""));
21 }
22
TEST(QuicheTextUtilsTest,RemoveLeadingAndTrailingWhitespace)23 TEST(QuicheTextUtilsTest, RemoveLeadingAndTrailingWhitespace) {
24 for (auto* const input : {"text", " text", " text", "text ", "text ",
25 " text ", " text ", "\r\n\ttext", "text\n\r\t"}) {
26 absl::string_view piece(input);
27 quiche::QuicheTextUtils::RemoveLeadingAndTrailingWhitespace(&piece);
28 EXPECT_EQ("text", piece);
29 }
30 }
31
TEST(QuicheTextUtilsTest,HexDump)32 TEST(QuicheTextUtilsTest, HexDump) {
33 // Verify output for empty input.
34 std::string empty;
35 ASSERT_TRUE(absl::HexStringToBytes("", &empty));
36 EXPECT_EQ("", quiche::QuicheTextUtils::HexDump(empty));
37 // Verify output of the HexDump method is as expected.
38 char packet[] = {
39 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x51, 0x55, 0x49, 0x43, 0x21,
40 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
41 0x20, 0x73, 0x68, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x6c,
42 0x6f, 0x6e, 0x67, 0x20, 0x65, 0x6e, 0x6f, 0x75, 0x67, 0x68, 0x20, 0x74,
43 0x6f, 0x20, 0x73, 0x70, 0x61, 0x6e, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69,
44 0x70, 0x6c, 0x65, 0x20, 0x6c, 0x69, 0x6e, 0x65, 0x73, 0x20, 0x6f, 0x66,
45 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x2e, 0x01, 0x02, 0x03, 0x00,
46 };
47 EXPECT_EQ(
48 quiche::QuicheTextUtils::HexDump(packet),
49 "0x0000: 4865 6c6c 6f2c 2051 5549 4321 2054 6869 Hello,.QUIC!.Thi\n"
50 "0x0010: 7320 7374 7269 6e67 2073 686f 756c 6420 s.string.should.\n"
51 "0x0020: 6265 206c 6f6e 6720 656e 6f75 6768 2074 be.long.enough.t\n"
52 "0x0030: 6f20 7370 616e 206d 756c 7469 706c 6520 o.span.multiple.\n"
53 "0x0040: 6c69 6e65 7320 6f66 206f 7574 7075 742e lines.of.output.\n"
54 "0x0050: 0102 03 ...\n");
55 // Verify that 0x21 and 0x7e are printable, 0x20 and 0x7f are not.
56 std::string printable_and_unprintable_chars;
57 ASSERT_TRUE(
58 absl::HexStringToBytes("20217e7f", &printable_and_unprintable_chars));
59 EXPECT_EQ("0x0000: 2021 7e7f .!~.\n",
60 quiche::QuicheTextUtils::HexDump(printable_and_unprintable_chars));
61 // Verify that values above numeric_limits<unsigned char>::max() are formatted
62 // properly on platforms where char is unsigned.
63 std::string large_chars;
64 ASSERT_TRUE(absl::HexStringToBytes("90aaff", &large_chars));
65 EXPECT_EQ("0x0000: 90aa ff ...\n",
66 quiche::QuicheTextUtils::HexDump(large_chars));
67 }
68
TEST(QuicheTextUtilsTest,Base64Encode)69 TEST(QuicheTextUtilsTest, Base64Encode) {
70 std::string output;
71 std::string input = "Hello";
72 quiche::QuicheTextUtils::Base64Encode(
73 reinterpret_cast<const uint8_t*>(input.data()), input.length(), &output);
74 EXPECT_EQ("SGVsbG8", output);
75
76 input =
77 "Hello, QUIC! This string should be long enough to span"
78 "multiple lines of output\n";
79 quiche::QuicheTextUtils::Base64Encode(
80 reinterpret_cast<const uint8_t*>(input.data()), input.length(), &output);
81 EXPECT_EQ(
82 "SGVsbG8sIFFVSUMhIFRoaXMgc3RyaW5nIHNob3VsZCBiZSBsb25n"
83 "IGVub3VnaCB0byBzcGFubXVsdGlwbGUgbGluZXMgb2Ygb3V0cHV0Cg",
84 output);
85 }
86
TEST(QuicheTextUtilsTest,ContainsUpperCase)87 TEST(QuicheTextUtilsTest, ContainsUpperCase) {
88 EXPECT_FALSE(quiche::QuicheTextUtils::ContainsUpperCase("abc"));
89 EXPECT_FALSE(quiche::QuicheTextUtils::ContainsUpperCase(""));
90 EXPECT_FALSE(quiche::QuicheTextUtils::ContainsUpperCase("123"));
91 EXPECT_TRUE(quiche::QuicheTextUtils::ContainsUpperCase("ABC"));
92 EXPECT_TRUE(quiche::QuicheTextUtils::ContainsUpperCase("aBc"));
93 }
94
95 } // namespace test
96 } // namespace quiche
97