Home | History | Annotate | Download | only in quic
      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_reliable_client_stream.h"
      6 
      7 #include "net/base/net_errors.h"
      8 #include "net/base/test_completion_callback.h"
      9 #include "net/quic/quic_client_session.h"
     10 #include "net/quic/quic_utils.h"
     11 #include "net/quic/spdy_utils.h"
     12 #include "net/quic/test_tools/quic_test_utils.h"
     13 #include "testing/gmock/include/gmock/gmock.h"
     14 
     15 using testing::Return;
     16 using testing::StrEq;
     17 using testing::_;
     18 
     19 namespace net {
     20 namespace test {
     21 namespace {
     22 
     23 const QuicGuid kStreamId = 3;
     24 
     25 class MockDelegate : public QuicReliableClientStream::Delegate {
     26  public:
     27   MockDelegate() {}
     28 
     29   MOCK_METHOD0(OnSendData, int());
     30   MOCK_METHOD2(OnSendDataComplete, int(int, bool*));
     31   MOCK_METHOD2(OnDataReceived, int(const char*, int));
     32   MOCK_METHOD1(OnClose, void(QuicErrorCode));
     33   MOCK_METHOD1(OnError, void(int));
     34   MOCK_METHOD0(HasSendHeadersComplete, bool());
     35 
     36  private:
     37   DISALLOW_COPY_AND_ASSIGN(MockDelegate);
     38 };
     39 
     40 class QuicReliableClientStreamTest : public ::testing::Test {
     41  public:
     42   QuicReliableClientStreamTest()
     43       : session_(new MockConnection(false)),
     44         stream_(kStreamId, &session_, BoundNetLog()) {
     45     stream_.SetDelegate(&delegate_);
     46   }
     47 
     48   void InitializeHeaders() {
     49     headers_[":host"] = "www.google.com";
     50     headers_[":path"] = "/index.hml";
     51     headers_[":scheme"] = "https";
     52     headers_["cookie"] =
     53         "__utma=208381060.1228362404.1372200928.1372200928.1372200928.1; "
     54         "__utmc=160408618; "
     55         "GX=DQAAAOEAAACWJYdewdE9rIrW6qw3PtVi2-d729qaa-74KqOsM1NVQblK4VhX"
     56         "hoALMsy6HOdDad2Sz0flUByv7etmo3mLMidGrBoljqO9hSVA40SLqpG_iuKKSHX"
     57         "RW3Np4bq0F0SDGDNsW0DSmTS9ufMRrlpARJDS7qAI6M3bghqJp4eABKZiRqebHT"
     58         "pMU-RXvTI5D5oCF1vYxYofH_l1Kviuiy3oQ1kS1enqWgbhJ2t61_SNdv-1XJIS0"
     59         "O3YeHLmVCs62O6zp89QwakfAWK9d3IDQvVSJzCQsvxvNIvaZFa567MawWlXg0Rh"
     60         "1zFMi5vzcns38-8_Sns; "
     61         "GA=v*2%2Fmem*57968640*47239936%2Fmem*57968640*47114716%2Fno-nm-"
     62         "yj*15%2Fno-cc-yj*5%2Fpc-ch*133685%2Fpc-s-cr*133947%2Fpc-s-t*1339"
     63         "47%2Fno-nm-yj*4%2Fno-cc-yj*1%2Fceft-as*1%2Fceft-nqas*0%2Fad-ra-c"
     64         "v_p%2Fad-nr-cv_p-f*1%2Fad-v-cv_p*859%2Fad-ns-cv_p-f*1%2Ffn-v-ad%"
     65         "2Fpc-t*250%2Fpc-cm*461%2Fpc-s-cr*722%2Fpc-s-t*722%2Fau_p*4"
     66         "SICAID=AJKiYcHdKgxum7KMXG0ei2t1-W4OD1uW-ecNsCqC0wDuAXiDGIcT_HA2o1"
     67         "3Rs1UKCuBAF9g8rWNOFbxt8PSNSHFuIhOo2t6bJAVpCsMU5Laa6lewuTMYI8MzdQP"
     68         "ARHKyW-koxuhMZHUnGBJAM1gJODe0cATO_KGoX4pbbFxxJ5IicRxOrWK_5rU3cdy6"
     69         "edlR9FsEdH6iujMcHkbE5l18ehJDwTWmBKBzVD87naobhMMrF6VvnDGxQVGp9Ir_b"
     70         "Rgj3RWUoPumQVCxtSOBdX0GlJOEcDTNCzQIm9BSfetog_eP_TfYubKudt5eMsXmN6"
     71         "QnyXHeGeK2UINUzJ-D30AFcpqYgH9_1BvYSpi7fc7_ydBU8TaD8ZRxvtnzXqj0RfG"
     72         "tuHghmv3aD-uzSYJ75XDdzKdizZ86IG6Fbn1XFhYZM-fbHhm3mVEXnyRW4ZuNOLFk"
     73         "Fas6LMcVC6Q8QLlHYbXBpdNFuGbuZGUnav5C-2I_-46lL0NGg3GewxGKGHvHEfoyn"
     74         "EFFlEYHsBQ98rXImL8ySDycdLEFvBPdtctPmWCfTxwmoSMLHU2SCVDhbqMWU5b0yr"
     75         "JBCScs_ejbKaqBDoB7ZGxTvqlrB__2ZmnHHjCr8RgMRtKNtIeuZAo ";
     76   }
     77 
     78   testing::StrictMock<MockDelegate> delegate_;
     79   MockSession session_;
     80   QuicReliableClientStream stream_;
     81   QuicCryptoClientConfig crypto_config_;
     82   SpdyHeaderBlock headers_;
     83 };
     84 
     85 TEST_F(QuicReliableClientStreamTest, OnFinRead) {
     86   InitializeHeaders();
     87   QuicSpdyCompressor compressor;
     88   string compressed_headers = compressor.CompressHeaders(headers_);
     89   QuicStreamFrame frame1(kStreamId, false, 0, MakeIOVector(compressed_headers));
     90   string uncompressed_headers =
     91       SpdyUtils::SerializeUncompressedHeaders(headers_);
     92   EXPECT_CALL(delegate_, OnDataReceived(StrEq(uncompressed_headers.data()),
     93                                         uncompressed_headers.size()));
     94   stream_.OnStreamFrame(frame1);
     95 
     96   IOVector iov;
     97   QuicStreamFrame frame2(kStreamId, true, compressed_headers.length(), iov);
     98   EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR));
     99   stream_.OnStreamFrame(frame2);
    100 }
    101 
    102 TEST_F(QuicReliableClientStreamTest, ProcessData) {
    103   const char data[] = "hello world!";
    104   EXPECT_CALL(delegate_, OnDataReceived(StrEq(data), arraysize(data)));
    105   EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR));
    106 
    107   EXPECT_EQ(arraysize(data), stream_.ProcessData(data, arraysize(data)));
    108 }
    109 
    110 TEST_F(QuicReliableClientStreamTest, ProcessDataWithError) {
    111   const char data[] = "hello world!";
    112   EXPECT_CALL(delegate_,
    113               OnDataReceived(StrEq(data),
    114                              arraysize(data))).WillOnce(Return(ERR_UNEXPECTED));
    115   EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR));
    116 
    117 
    118   EXPECT_EQ(0u, stream_.ProcessData(data, arraysize(data)));
    119 }
    120 
    121 TEST_F(QuicReliableClientStreamTest, OnError) {
    122   EXPECT_CALL(delegate_, OnError(ERR_INTERNET_DISCONNECTED));
    123 
    124   stream_.OnError(ERR_INTERNET_DISCONNECTED);
    125   EXPECT_FALSE(stream_.GetDelegate());
    126 }
    127 
    128 TEST_F(QuicReliableClientStreamTest, WriteStreamData) {
    129   EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR));
    130 
    131   const char kData1[] = "hello world";
    132   const size_t kDataLen = arraysize(kData1);
    133 
    134   // All data written.
    135   EXPECT_CALL(session_, WritevData(stream_.id(), _, _, _, _, _)).WillOnce(
    136       Return(QuicConsumedData(kDataLen, true)));
    137   TestCompletionCallback callback;
    138   EXPECT_EQ(OK, stream_.WriteStreamData(base::StringPiece(kData1, kDataLen),
    139                                         true, callback.callback()));
    140 }
    141 
    142 TEST_F(QuicReliableClientStreamTest, WriteStreamDataAsync) {
    143   EXPECT_CALL(delegate_, HasSendHeadersComplete());
    144   EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR));
    145 
    146   const char kData1[] = "hello world";
    147   const size_t kDataLen = arraysize(kData1);
    148 
    149   // No data written.
    150   EXPECT_CALL(session_, WritevData(stream_.id(), _, _, _, _, _)).WillOnce(
    151       Return(QuicConsumedData(0, false)));
    152   TestCompletionCallback callback;
    153   EXPECT_EQ(ERR_IO_PENDING,
    154             stream_.WriteStreamData(base::StringPiece(kData1, kDataLen),
    155                                     true, callback.callback()));
    156   ASSERT_FALSE(callback.have_result());
    157 
    158   // All data written.
    159   EXPECT_CALL(session_, WritevData(stream_.id(), _, _, _, _, _)).WillOnce(
    160       Return(QuicConsumedData(kDataLen, true)));
    161   stream_.OnCanWrite();
    162   ASSERT_TRUE(callback.have_result());
    163   EXPECT_EQ(OK, callback.WaitForResult());
    164 }
    165 
    166 }  // namespace
    167 }  // namespace test
    168 }  // namespace net
    169