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