| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 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/tools/quic/chlo_extractor.h" |
| 6 |
| 7 #include "net/quic/quic_framer.h" |
| 8 #include "net/quic/test_tools/crypto_test_utils.h" |
| 9 #include "net/quic/test_tools/quic_test_utils.h" |
| 10 |
| 11 using base::StringPiece; |
| 12 using testing::Return; |
| 13 using testing::_; |
| 14 |
| 15 namespace net { |
| 16 namespace test { |
| 17 namespace { |
| 18 |
| 19 class TestDelegate : public ChloExtractor::Delegate { |
| 20 public: |
| 21 TestDelegate() {} |
| 22 ~TestDelegate() override {} |
| 23 |
| 24 // ChloExtractor::Delegate implementation |
| 25 void OnChlo(QuicVersion version, |
| 26 QuicConnectionId connection_id, |
| 27 const CryptoHandshakeMessage& chlo) override { |
| 28 version_ = version; |
| 29 connection_id_ = connection_id; |
| 30 chlo_ = chlo.DebugString(); |
| 31 } |
| 32 |
| 33 QuicConnectionId connection_id() const { return connection_id_; } |
| 34 QuicVersion version() const { return version_; } |
| 35 const std::string& chlo() const { return chlo_; } |
| 36 |
| 37 private: |
| 38 QuicConnectionId connection_id_; |
| 39 QuicVersion version_; |
| 40 std::string chlo_; |
| 41 }; |
| 42 |
| 43 class ChloExtractorTest : public ::testing::Test { |
| 44 public: |
| 45 ChloExtractorTest() { |
| 46 header_.public_header.connection_id = 42; |
| 47 header_.public_header.connection_id_length = PACKET_8BYTE_CONNECTION_ID; |
| 48 header_.public_header.version_flag = true; |
| 49 header_.public_header.versions = |
| 50 SupportedVersions(QuicSupportedVersions().front()); |
| 51 header_.public_header.reset_flag = false; |
| 52 header_.public_header.packet_number_length = PACKET_6BYTE_PACKET_NUMBER; |
| 53 header_.packet_number = 1; |
| 54 header_.entropy_flag = false; |
| 55 header_.entropy_hash = 0; |
| 56 header_.fec_flag = false; |
| 57 header_.is_in_fec_group = NOT_IN_FEC_GROUP; |
| 58 header_.fec_group = 0; |
| 59 } |
| 60 |
| 61 void MakePacket(QuicStreamFrame* stream_frame) { |
| 62 QuicFrame frame(stream_frame); |
| 63 QuicFrames frames; |
| 64 frames.push_back(frame); |
| 65 QuicFramer framer(SupportedVersions(header_.public_header.versions.front()), |
| 66 QuicTime::Zero(), Perspective::IS_CLIENT); |
| 67 std::unique_ptr<QuicPacket> packet( |
| 68 BuildUnsizedDataPacket(&framer, header_, frames)); |
| 69 EXPECT_TRUE(packet != nullptr); |
| 70 size_t encrypted_length = framer.EncryptPayload( |
| 71 ENCRYPTION_NONE, header_.path_id, header_.packet_number, *packet, |
| 72 buffer_, arraysize(buffer_)); |
| 73 ASSERT_NE(0u, encrypted_length); |
| 74 packet_.reset(new QuicEncryptedPacket(buffer_, encrypted_length)); |
| 75 EXPECT_TRUE(packet_ != nullptr); |
| 76 delete stream_frame; |
| 77 } |
| 78 |
| 79 protected: |
| 80 TestDelegate delegate_; |
| 81 QuicPacketHeader header_; |
| 82 std::unique_ptr<QuicEncryptedPacket> packet_; |
| 83 char buffer_[kMaxPacketSize]; |
| 84 }; |
| 85 |
| 86 TEST_F(ChloExtractorTest, FindsValidChlo) { |
| 87 CryptoHandshakeMessage client_hello; |
| 88 client_hello.set_tag(kCHLO); |
| 89 |
| 90 std::string client_hello_str( |
| 91 client_hello.GetSerialized().AsStringPiece().as_string()); |
| 92 // Construct a CHLO with each supported version |
| 93 for (QuicVersion version : QuicSupportedVersions()) { |
| 94 QuicVersionVector versions(SupportedVersions(version)); |
| 95 header_.public_header.versions = versions; |
| 96 MakePacket( |
| 97 new QuicStreamFrame(kCryptoStreamId, false, 0, client_hello_str)); |
| 98 EXPECT_TRUE(ChloExtractor::Extract(*packet_, versions, &delegate_)) |
| 99 << QuicVersionToString(version); |
| 100 EXPECT_EQ(version, delegate_.version()); |
| 101 EXPECT_EQ(header_.public_header.connection_id, delegate_.connection_id()); |
| 102 EXPECT_EQ(client_hello.DebugString(), delegate_.chlo()) |
| 103 << QuicVersionToString(version); |
| 104 } |
| 105 } |
| 106 |
| 107 TEST_F(ChloExtractorTest, DoesNotFindValidChloOnWrongStream) { |
| 108 CryptoHandshakeMessage client_hello; |
| 109 client_hello.set_tag(kCHLO); |
| 110 |
| 111 std::string client_hello_str( |
| 112 client_hello.GetSerialized().AsStringPiece().as_string()); |
| 113 MakePacket( |
| 114 new QuicStreamFrame(kCryptoStreamId + 1, false, 0, client_hello_str)); |
| 115 EXPECT_FALSE( |
| 116 ChloExtractor::Extract(*packet_, QuicSupportedVersions(), &delegate_)); |
| 117 } |
| 118 |
| 119 TEST_F(ChloExtractorTest, DoesNotFindValidChloOnWrongOffset) { |
| 120 CryptoHandshakeMessage client_hello; |
| 121 client_hello.set_tag(kCHLO); |
| 122 |
| 123 std::string client_hello_str( |
| 124 client_hello.GetSerialized().AsStringPiece().as_string()); |
| 125 MakePacket(new QuicStreamFrame(kCryptoStreamId, false, 1, client_hello_str)); |
| 126 EXPECT_FALSE( |
| 127 ChloExtractor::Extract(*packet_, QuicSupportedVersions(), &delegate_)); |
| 128 } |
| 129 |
| 130 TEST_F(ChloExtractorTest, DoesNotFindInvalidChlo) { |
| 131 MakePacket(new QuicStreamFrame(kCryptoStreamId, false, 0, "foo")); |
| 132 EXPECT_FALSE( |
| 133 ChloExtractor::Extract(*packet_, QuicSupportedVersions(), &delegate_)); |
| 134 } |
| 135 |
| 136 } // namespace |
| 137 } // namespace test |
| 138 } // namespace net |
| OLD | NEW |