OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/quic_utils.h" | 5 #include "net/quic/quic_utils.h" |
6 | 6 |
7 #include "net/quic/crypto/crypto_protocol.h" | 7 #include "net/quic/crypto/crypto_protocol.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 using base::StringPiece; | 10 using base::StringPiece; |
11 using std::string; | 11 using std::string; |
12 | 12 |
13 namespace net { | 13 namespace net { |
14 namespace test { | 14 namespace test { |
15 namespace { | 15 namespace { |
16 | 16 |
17 // A test string and a hex+ASCII dump of the same string. | 17 // A test string and a hex+ASCII dump of the same string. |
18 const unsigned char kString[] = { | 18 const unsigned char kString[] = { |
19 0x00, 0x90, 0x69, 0xbd, 0x54, 0x00, 0x00, 0x0d, | 19 0x00, 0x90, 0x69, 0xbd, 0x54, 0x00, 0x00, 0x0d, 0x61, 0x0f, 0x01, |
20 0x61, 0x0f, 0x01, 0x89, 0x08, 0x00, 0x45, 0x00, | 20 0x89, 0x08, 0x00, 0x45, 0x00, 0x00, 0x1c, 0xfb, 0x98, 0x40, 0x00, |
21 0x00, 0x1c, 0xfb, 0x98, 0x40, 0x00, 0x40, 0x01, | 21 0x40, 0x01, 0x7e, 0x18, 0xd8, 0xef, 0x23, 0x01, 0x45, 0x5d, 0x7f, |
22 0x7e, 0x18, 0xd8, 0xef, 0x23, 0x01, 0x45, 0x5d, | 22 0xe2, 0x08, 0x00, 0x6b, 0xcb, 0x0b, 0xc6, 0x80, 0x6e}; |
23 0x7f, 0xe2, 0x08, 0x00, 0x6b, 0xcb, 0x0b, 0xc6, | |
24 0x80, 0x6e | |
25 }; | |
26 | 23 |
27 const unsigned char kHexDump[] = | 24 const unsigned char kHexDump[] = |
28 "0x0000: 0090 69bd 5400 000d 610f 0189 0800 4500 ..i.T...a.....E.\n" | 25 "0x0000: 0090 69bd 5400 000d 610f 0189 0800 4500 ..i.T...a.....E.\n" |
29 "0x0010: 001c fb98 4000 4001 7e18 d8ef 2301 455d ....@.@.~...#.E]\n" | 26 "0x0010: 001c fb98 4000 4001 7e18 d8ef 2301 455d ....@.@.~...#.E]\n" |
30 "0x0020: 7fe2 0800 6bcb 0bc6 806e ....k....n\n"; | 27 "0x0020: 7fe2 0800 6bcb 0bc6 806e ....k....n\n"; |
31 | 28 |
32 TEST(QuicUtilsTest, StreamErrorToString) { | 29 TEST(QuicUtilsTest, StreamErrorToString) { |
33 EXPECT_STREQ("QUIC_BAD_APPLICATION_PAYLOAD", | 30 EXPECT_STREQ("QUIC_BAD_APPLICATION_PAYLOAD", |
34 QuicUtils::StreamErrorToString(QUIC_BAD_APPLICATION_PAYLOAD)); | 31 QuicUtils::StreamErrorToString(QUIC_BAD_APPLICATION_PAYLOAD)); |
35 } | 32 } |
36 | 33 |
37 TEST(QuicUtilsTest, ErrorToString) { | 34 TEST(QuicUtilsTest, ErrorToString) { |
38 EXPECT_STREQ("QUIC_NO_ERROR", | 35 EXPECT_STREQ("QUIC_NO_ERROR", QuicUtils::ErrorToString(QUIC_NO_ERROR)); |
39 QuicUtils::ErrorToString(QUIC_NO_ERROR)); | |
40 } | 36 } |
41 | 37 |
42 TEST(QuicUtilsTest, StringToHexASCIIDumpArgTypes) { | 38 TEST(QuicUtilsTest, StringToHexASCIIDumpArgTypes) { |
43 // Verify that char*, string and StringPiece are all valid argument types. | 39 // Verify that char*, string and StringPiece are all valid argument types. |
44 struct { | 40 struct { |
45 const string input; | 41 const string input; |
46 const string expected; | 42 const string expected; |
47 } tests[] = { | 43 } tests[] = { |
48 { "", "", }, | 44 { |
49 { "A", "0x0000: 41 A\n", }, | 45 "", "", |
50 { "AB", "0x0000: 4142 AB\n", }, | 46 }, |
51 { "ABC", "0x0000: 4142 43 ABC\n", }, | 47 { |
52 { "original", | 48 "A", "0x0000: 41 A\n", |
53 "0x0000: 6f72 6967 696e 616c original\n", }, | 49 }, |
54 }; | 50 { |
| 51 "AB", "0x0000: 4142 AB\n", |
| 52 }, |
| 53 { |
| 54 "ABC", "0x0000: 4142 43 ABC\n", |
| 55 }, |
| 56 { |
| 57 "original", |
| 58 "0x0000: 6f72 6967 696e 616c original\n", |
| 59 }, |
| 60 }; |
55 | 61 |
56 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 62 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
57 EXPECT_EQ(tests[i].expected, | 63 EXPECT_EQ(tests[i].expected, |
58 QuicUtils::StringToHexASCIIDump(tests[i].input.c_str())); | 64 QuicUtils::StringToHexASCIIDump(tests[i].input.c_str())); |
59 EXPECT_EQ(tests[i].expected, | 65 EXPECT_EQ(tests[i].expected, |
60 QuicUtils::StringToHexASCIIDump(tests[i].input)); | 66 QuicUtils::StringToHexASCIIDump(tests[i].input)); |
61 EXPECT_EQ(tests[i].expected, | 67 EXPECT_EQ(tests[i].expected, |
62 QuicUtils::StringToHexASCIIDump(StringPiece(tests[i].input))); | 68 QuicUtils::StringToHexASCIIDump(StringPiece(tests[i].input))); |
63 } | 69 } |
64 } | 70 } |
65 | 71 |
66 TEST(QuicUtilsTest, StringToHexASCIIDumpSuccess) { | 72 TEST(QuicUtilsTest, StringToHexASCIIDumpSuccess) { |
67 EXPECT_EQ(string(reinterpret_cast<const char*>(kHexDump)), | 73 EXPECT_EQ(string(reinterpret_cast<const char*>(kHexDump)), |
68 QuicUtils::StringToHexASCIIDump( | 74 QuicUtils::StringToHexASCIIDump(string( |
69 string(reinterpret_cast<const char*>(kString), sizeof(kString)))); | 75 reinterpret_cast<const char*>(kString), sizeof(kString)))); |
70 } | 76 } |
71 | 77 |
72 TEST(QuicUtilsTest, TagToString) { | 78 TEST(QuicUtilsTest, TagToString) { |
73 EXPECT_EQ("SCFG", | 79 EXPECT_EQ("SCFG", QuicUtils::TagToString(kSCFG)); |
74 QuicUtils::TagToString(kSCFG)); | 80 EXPECT_EQ("SNO ", QuicUtils::TagToString(kServerNonceTag)); |
75 EXPECT_EQ("SNO ", | 81 EXPECT_EQ("CRT ", QuicUtils::TagToString(kCertificateTag)); |
76 QuicUtils::TagToString(kServerNonceTag)); | 82 EXPECT_EQ("CHLO", QuicUtils::TagToString(MakeQuicTag('C', 'H', 'L', 'O'))); |
77 EXPECT_EQ("CRT ", | |
78 QuicUtils::TagToString(kCertificateTag)); | |
79 EXPECT_EQ("CHLO", | |
80 QuicUtils::TagToString(MakeQuicTag('C', 'H', 'L', 'O'))); | |
81 // A tag that contains a non-printing character will be printed as a decimal | 83 // A tag that contains a non-printing character will be printed as a decimal |
82 // number. | 84 // number. |
83 EXPECT_EQ("525092931", | 85 EXPECT_EQ("525092931", |
84 QuicUtils::TagToString(MakeQuicTag('C', 'H', 'L', '\x1f'))); | 86 QuicUtils::TagToString(MakeQuicTag('C', 'H', 'L', '\x1f'))); |
85 } | 87 } |
86 | 88 |
87 } // namespace | 89 } // namespace |
88 } // namespace test | 90 } // namespace test |
89 } // namespace net | 91 } // namespace net |
OLD | NEW |