OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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/quic_crypto_stream.h" | |
6 | |
7 #include <cstdint> | |
8 #include <memory> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/macros.h" | |
13 #include "net/quic/crypto/crypto_handshake.h" | |
14 #include "net/quic/crypto/crypto_protocol.h" | |
15 #include "net/quic/test_tools/crypto_test_utils.h" | |
16 #include "net/quic/test_tools/quic_test_utils.h" | |
17 #include "net/quic/test_tools/reliable_quic_stream_peer.h" | |
18 #include "testing/gmock/include/gmock/gmock.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 | |
21 using std::string; | |
22 using std::vector; | |
23 | |
24 namespace net { | |
25 namespace test { | |
26 namespace { | |
27 | |
28 class MockQuicCryptoStream : public QuicCryptoStream { | |
29 public: | |
30 explicit MockQuicCryptoStream(QuicSession* session) | |
31 : QuicCryptoStream(session) {} | |
32 | |
33 void OnHandshakeMessage(const CryptoHandshakeMessage& message) override { | |
34 messages_.push_back(message); | |
35 } | |
36 | |
37 vector<CryptoHandshakeMessage>* messages() { return &messages_; } | |
38 | |
39 private: | |
40 vector<CryptoHandshakeMessage> messages_; | |
41 | |
42 DISALLOW_COPY_AND_ASSIGN(MockQuicCryptoStream); | |
43 }; | |
44 | |
45 class QuicCryptoStreamTest : public ::testing::Test { | |
46 public: | |
47 QuicCryptoStreamTest() | |
48 : connection_(new MockQuicConnection(&helper_, | |
49 &alarm_factory_, | |
50 Perspective::IS_CLIENT)), | |
51 session_(connection_), | |
52 stream_(&session_) { | |
53 message_.set_tag(kSHLO); | |
54 message_.SetStringPiece(1, "abc"); | |
55 message_.SetStringPiece(2, "def"); | |
56 ConstructHandshakeMessage(); | |
57 } | |
58 | |
59 void ConstructHandshakeMessage() { | |
60 CryptoFramer framer; | |
61 message_data_.reset(framer.ConstructHandshakeMessage(message_)); | |
62 } | |
63 | |
64 protected: | |
65 MockQuicConnectionHelper helper_; | |
66 MockAlarmFactory alarm_factory_; | |
67 MockQuicConnection* connection_; | |
68 MockQuicSpdySession session_; | |
69 MockQuicCryptoStream stream_; | |
70 CryptoHandshakeMessage message_; | |
71 std::unique_ptr<QuicData> message_data_; | |
72 | |
73 private: | |
74 DISALLOW_COPY_AND_ASSIGN(QuicCryptoStreamTest); | |
75 }; | |
76 | |
77 TEST_F(QuicCryptoStreamTest, NotInitiallyConected) { | |
78 EXPECT_FALSE(stream_.encryption_established()); | |
79 EXPECT_FALSE(stream_.handshake_confirmed()); | |
80 } | |
81 | |
82 TEST_F(QuicCryptoStreamTest, ProcessRawData) { | |
83 stream_.OnStreamFrame(QuicStreamFrame(kCryptoStreamId, /*fin=*/false, | |
84 /*offset=*/0, | |
85 message_data_->AsStringPiece())); | |
86 ASSERT_EQ(1u, stream_.messages()->size()); | |
87 const CryptoHandshakeMessage& message = (*stream_.messages())[0]; | |
88 EXPECT_EQ(kSHLO, message.tag()); | |
89 EXPECT_EQ(2u, message.tag_value_map().size()); | |
90 EXPECT_EQ("abc", CryptoTestUtils::GetValueForTag(message, 1)); | |
91 EXPECT_EQ("def", CryptoTestUtils::GetValueForTag(message, 2)); | |
92 } | |
93 | |
94 TEST_F(QuicCryptoStreamTest, ProcessBadData) { | |
95 string bad(message_data_->data(), message_data_->length()); | |
96 const int kFirstTagIndex = sizeof(uint32_t) + // message tag | |
97 sizeof(uint16_t) + // number of tag-value pairs | |
98 sizeof(uint16_t); // padding | |
99 EXPECT_EQ(1, bad[kFirstTagIndex]); | |
100 bad[kFirstTagIndex] = 0x7F; // out of order tag | |
101 | |
102 EXPECT_CALL(*connection_, CloseConnection(QUIC_CRYPTO_TAGS_OUT_OF_ORDER, | |
103 testing::_, testing::_)); | |
104 stream_.OnStreamFrame( | |
105 QuicStreamFrame(kCryptoStreamId, /*fin=*/false, /*offset=*/0, bad)); | |
106 } | |
107 | |
108 TEST_F(QuicCryptoStreamTest, NoConnectionLevelFlowControl) { | |
109 EXPECT_FALSE(ReliableQuicStreamPeer::StreamContributesToConnectionFlowControl( | |
110 &stream_)); | |
111 } | |
112 | |
113 } // namespace | |
114 } // namespace test | |
115 } // namespace net | |
OLD | NEW |