1// Copyright 2013 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/quic_spdy_client_stream.h"
6
7#include "base/strings/string_number_conversions.h"
8#include "net/quic/quic_utils.h"
9#include "net/quic/test_tools/quic_test_utils.h"
10#include "net/tools/quic/quic_client_session.h"
11#include "net/tools/quic/quic_spdy_client_stream.h"
12#include "net/tools/quic/spdy_utils.h"
13#include "net/tools/quic/test_tools/quic_test_utils.h"
14#include "testing/gmock/include/gmock/gmock.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
17using net::test::DefaultQuicConfig;
18using net::test::SupportedVersions;
19using testing::StrictMock;
20using testing::TestWithParam;
21
22namespace net {
23namespace tools {
24namespace test {
25namespace {
26
27class QuicSpdyClientStreamTest : public TestWithParam<QuicVersion> {
28 public:
29  QuicSpdyClientStreamTest()
30      : connection_(new StrictMock<MockConnection>(
31            false, SupportedVersions(GetParam()))),
32        session_(DefaultQuicConfig(), connection_),
33        body_("hello world") {
34    session_.InitializeSession(
35        QuicServerId("example.com", 80, false, PRIVACY_MODE_DISABLED),
36        &crypto_config_);
37    crypto_config_.SetDefaults();
38
39    headers_.SetResponseFirstlineFromStringPieces("HTTP/1.1", "200", "Ok");
40    headers_.ReplaceOrAppendHeader("content-length", "11");
41
42    headers_string_ = SpdyUtils::SerializeResponseHeaders(headers_);
43
44    // New streams rely on having the peer's flow control receive window
45    // negotiated in the config.
46    session_.config()->SetInitialFlowControlWindowToSend(
47        kInitialSessionFlowControlWindowForTest);
48    session_.config()->SetInitialStreamFlowControlWindowToSend(
49        kInitialStreamFlowControlWindowForTest);
50    session_.config()->SetInitialSessionFlowControlWindowToSend(
51        kInitialSessionFlowControlWindowForTest);
52    stream_.reset(new QuicSpdyClientStream(3, &session_));
53  }
54
55  StrictMock<MockConnection>* connection_;
56  QuicClientSession session_;
57  scoped_ptr<QuicSpdyClientStream> stream_;
58  BalsaHeaders headers_;
59  string headers_string_;
60  string body_;
61  QuicCryptoClientConfig crypto_config_;
62};
63
64INSTANTIATE_TEST_CASE_P(Tests, QuicSpdyClientStreamTest,
65                        ::testing::ValuesIn(QuicSupportedVersions()));
66
67TEST_P(QuicSpdyClientStreamTest, TestFraming) {
68  EXPECT_EQ(headers_string_.size(), stream_->ProcessData(
69      headers_string_.c_str(), headers_string_.size()));
70  EXPECT_EQ(body_.size(),
71            stream_->ProcessData(body_.c_str(), body_.size()));
72  EXPECT_EQ(200u, stream_->headers().parsed_response_code());
73  EXPECT_EQ(body_, stream_->data());
74}
75
76TEST_P(QuicSpdyClientStreamTest, TestFramingOnePacket) {
77  string message = headers_string_ + body_;
78
79  EXPECT_EQ(message.size(), stream_->ProcessData(
80      message.c_str(), message.size()));
81  EXPECT_EQ(200u, stream_->headers().parsed_response_code());
82  EXPECT_EQ(body_, stream_->data());
83}
84
85TEST_P(QuicSpdyClientStreamTest, DISABLED_TestFramingExtraData) {
86  string large_body = "hello world!!!!!!";
87
88  EXPECT_EQ(headers_string_.size(), stream_->ProcessData(
89      headers_string_.c_str(), headers_string_.size()));
90  // The headers should parse successfully.
91  EXPECT_EQ(QUIC_STREAM_NO_ERROR, stream_->stream_error());
92  EXPECT_EQ(200u, stream_->headers().parsed_response_code());
93
94  EXPECT_CALL(*connection_,
95              SendRstStream(stream_->id(), QUIC_BAD_APPLICATION_PAYLOAD, 0));
96  stream_->ProcessData(large_body.c_str(), large_body.size());
97
98  EXPECT_NE(QUIC_STREAM_NO_ERROR, stream_->stream_error());
99}
100
101TEST_P(QuicSpdyClientStreamTest, TestNoBidirectionalStreaming) {
102  QuicStreamFrame frame(3, false, 3, MakeIOVector("asd"));
103
104  EXPECT_FALSE(stream_->write_side_closed());
105  stream_->OnStreamFrame(frame);
106  EXPECT_TRUE(stream_->write_side_closed());
107}
108
109}  // namespace
110}  // namespace test
111}  // namespace tools
112}  // namespace net
113