Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / quic / quic_crypto_stream_test.cc
blob0393e97dc8f398a70906458ff37e3c41779f0eab
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.
5 #include "net/quic/quic_crypto_stream.h"
7 #include <string>
8 #include <vector>
10 #include "base/memory/scoped_ptr.h"
11 #include "net/quic/crypto/crypto_handshake.h"
12 #include "net/quic/crypto/crypto_protocol.h"
13 #include "net/quic/test_tools/crypto_test_utils.h"
14 #include "net/quic/test_tools/quic_test_utils.h"
15 #include "net/quic/test_tools/reliable_quic_stream_peer.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 using std::string;
20 using std::vector;
22 namespace net {
23 namespace test {
24 namespace {
26 class MockQuicCryptoStream : public QuicCryptoStream {
27 public:
28 explicit MockQuicCryptoStream(QuicSession* session)
29 : QuicCryptoStream(session) {
32 void OnHandshakeMessage(const CryptoHandshakeMessage& message) override {
33 messages_.push_back(message);
36 vector<CryptoHandshakeMessage>* messages() {
37 return &messages_;
40 private:
41 vector<CryptoHandshakeMessage> messages_;
43 DISALLOW_COPY_AND_ASSIGN(MockQuicCryptoStream);
46 class QuicCryptoStreamTest : public ::testing::Test {
47 public:
48 QuicCryptoStreamTest()
49 : connection_(new MockConnection(Perspective::IS_CLIENT)),
50 session_(connection_),
51 stream_(&session_) {
52 message_.set_tag(kSHLO);
53 message_.SetStringPiece(1, "abc");
54 message_.SetStringPiece(2, "def");
55 ConstructHandshakeMessage();
58 void ConstructHandshakeMessage() {
59 CryptoFramer framer;
60 message_data_.reset(framer.ConstructHandshakeMessage(message_));
63 protected:
64 MockConnection* connection_;
65 MockQuicSpdySession session_;
66 MockQuicCryptoStream stream_;
67 CryptoHandshakeMessage message_;
68 scoped_ptr<QuicData> message_data_;
70 private:
71 DISALLOW_COPY_AND_ASSIGN(QuicCryptoStreamTest);
74 TEST_F(QuicCryptoStreamTest, NotInitiallyConected) {
75 EXPECT_FALSE(stream_.encryption_established());
76 EXPECT_FALSE(stream_.handshake_confirmed());
79 TEST_F(QuicCryptoStreamTest, ProcessRawData) {
80 stream_.OnStreamFrame(QuicStreamFrame(kCryptoStreamId, /*fin=*/false,
81 /*offset=*/0,
82 message_data_->AsStringPiece()));
83 ASSERT_EQ(1u, stream_.messages()->size());
84 const CryptoHandshakeMessage& message = (*stream_.messages())[0];
85 EXPECT_EQ(kSHLO, message.tag());
86 EXPECT_EQ(2u, message.tag_value_map().size());
87 EXPECT_EQ("abc", CryptoTestUtils::GetValueForTag(message, 1));
88 EXPECT_EQ("def", CryptoTestUtils::GetValueForTag(message, 2));
91 TEST_F(QuicCryptoStreamTest, ProcessBadData) {
92 string bad(message_data_->data(), message_data_->length());
93 const int kFirstTagIndex = sizeof(uint32) + // message tag
94 sizeof(uint16) + // number of tag-value pairs
95 sizeof(uint16); // padding
96 EXPECT_EQ(1, bad[kFirstTagIndex]);
97 bad[kFirstTagIndex] = 0x7F; // out of order tag
99 EXPECT_CALL(*connection_,
100 SendConnectionClose(QUIC_CRYPTO_TAGS_OUT_OF_ORDER));
101 stream_.OnStreamFrame(
102 QuicStreamFrame(kCryptoStreamId, /*fin=*/false, /*offset=*/0, bad));
105 TEST_F(QuicCryptoStreamTest, NoConnectionLevelFlowControl) {
106 EXPECT_FALSE(ReliableQuicStreamPeer::StreamContributesToConnectionFlowControl(
107 &stream_));
110 } // namespace
111 } // namespace test
112 } // namespace net