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_crypto_stream.h"
      6 
      7 #include <string>
      8 #include <vector>
      9 
     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 "testing/gmock/include/gmock/gmock.h"
     16 #include "testing/gtest/include/gtest/gtest.h"
     17 
     18 using std::string;
     19 using std::vector;
     20 
     21 namespace net {
     22 namespace test {
     23 namespace {
     24 
     25 class MockQuicCryptoStream : public QuicCryptoStream {
     26  public:
     27   explicit MockQuicCryptoStream(QuicSession* session)
     28       : QuicCryptoStream(session) {
     29   }
     30 
     31   virtual void OnHandshakeMessage(
     32       const CryptoHandshakeMessage& message) OVERRIDE {
     33     messages_.push_back(message);
     34   }
     35 
     36   vector<CryptoHandshakeMessage>* messages() {
     37     return &messages_;
     38   }
     39 
     40  private:
     41   vector<CryptoHandshakeMessage> messages_;
     42 
     43   DISALLOW_COPY_AND_ASSIGN(MockQuicCryptoStream);
     44 };
     45 
     46 class QuicCryptoStreamTest : public ::testing::Test {
     47  public:
     48   QuicCryptoStreamTest()
     49       : addr_(IPAddressNumber(), 1),
     50         connection_(new MockConnection(1, addr_, false)),
     51         session_(connection_, true),
     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   IPEndPoint addr_;
     66   MockConnection* connection_;
     67   MockSession session_;
     68   MockQuicCryptoStream stream_;
     69   CryptoHandshakeMessage message_;
     70   scoped_ptr<QuicData> message_data_;
     71 
     72  private:
     73   DISALLOW_COPY_AND_ASSIGN(QuicCryptoStreamTest);
     74 };
     75 
     76 TEST_F(QuicCryptoStreamTest, NotInitiallyConected) {
     77   EXPECT_FALSE(stream_.encryption_established());
     78   EXPECT_FALSE(stream_.handshake_confirmed());
     79 }
     80 
     81 TEST_F(QuicCryptoStreamTest, OnErrorClosesConnection) {
     82   CryptoFramer framer;
     83   EXPECT_CALL(session_, ConnectionClose(QUIC_NO_ERROR, false));
     84   stream_.OnError(&framer);
     85 }
     86 
     87 TEST_F(QuicCryptoStreamTest, ProcessData) {
     88   EXPECT_EQ(message_data_->length(),
     89             stream_.ProcessData(message_data_->data(),
     90                                 message_data_->length()));
     91   ASSERT_EQ(1u, stream_.messages()->size());
     92   const CryptoHandshakeMessage& message = (*stream_.messages())[0];
     93   EXPECT_EQ(kSHLO, message.tag());
     94   EXPECT_EQ(2u, message.tag_value_map().size());
     95   EXPECT_EQ("abc", CryptoTestUtils::GetValueForTag(message, 1));
     96   EXPECT_EQ("def", CryptoTestUtils::GetValueForTag(message, 2));
     97 }
     98 
     99 TEST_F(QuicCryptoStreamTest, ProcessBadData) {
    100   string bad(message_data_->data(), message_data_->length());
    101   const int kFirstTagIndex = sizeof(uint32) +  // message tag
    102                              sizeof(uint16) +  // number of tag-value pairs
    103                              sizeof(uint16);   // padding
    104   EXPECT_EQ(1, bad[kFirstTagIndex]);
    105   bad[kFirstTagIndex] = 0x7F;  // out of order tag
    106 
    107   EXPECT_CALL(*connection_,
    108               SendConnectionClose(QUIC_CRYPTO_TAGS_OUT_OF_ORDER));
    109   EXPECT_EQ(0u, stream_.ProcessData(bad.data(), bad.length()));
    110 }
    111 
    112 }  // namespace
    113 }  // namespace test
    114 }  // namespace net
    115