summaryrefslogtreecommitdiffstats
path: root/net/quic/quic_crypto_stream_test.cc
blob: 10f1657140ece415f9eec57520da9d292ebc9fbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/quic/quic_crypto_stream.h"

#include <string>
#include <vector>

#include "base/memory/scoped_ptr.h"
#include "net/quic/test_tools/quic_test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using std::string;
using std::vector;

namespace net {
namespace test {
namespace {

class MockQuicCryptoStream : public QuicCryptoStream {
 public:
  explicit MockQuicCryptoStream(QuicSession* session)
      : QuicCryptoStream(session) {
  }

  virtual void OnHandshakeMessage(
      const CryptoHandshakeMessage& message) OVERRIDE {
    messages_.push_back(message);
  }

  vector<CryptoHandshakeMessage>* messages() {
    return &messages_;
  }

 private:
  vector<CryptoHandshakeMessage> messages_;

  DISALLOW_COPY_AND_ASSIGN(MockQuicCryptoStream);
};

class QuicCryptoStreamTest : public ::testing::Test {
 public:
  QuicCryptoStreamTest()
      : addr_(IPAddressNumber(), 1),
        connection_(new MockConnection(1, addr_)),
        session_(connection_, true),
        stream_(&session_) {
    message_.tag = kSHLO;
    message_.tag_value_map[1] = "abc";
    message_.tag_value_map[2] = "def";
    ConstructHandshakeMessage();
  }

  void ConstructHandshakeMessage() {
    CryptoFramer framer;
    message_data_.reset(framer.ConstructHandshakeMessage(message_));
  }

 protected:
  IPEndPoint addr_;
  MockConnection* connection_;
  MockSession session_;
  MockQuicCryptoStream stream_;
  CryptoHandshakeMessage message_;
  scoped_ptr<QuicData> message_data_;

 private:
  DISALLOW_COPY_AND_ASSIGN(QuicCryptoStreamTest);
};

TEST_F(QuicCryptoStreamTest, NotInitiallyConected) {
  EXPECT_FALSE(stream_.handshake_complete());
}

TEST_F(QuicCryptoStreamTest, OnErrorClosesConnection) {
  CryptoFramer framer;
  EXPECT_CALL(session_, ConnectionClose(QUIC_NO_ERROR, false));
  stream_.OnError(&framer);
}

TEST_F(QuicCryptoStreamTest, ProcessData) {
  EXPECT_EQ(message_data_->length(),
            stream_.ProcessData(message_data_->data(),
                                message_data_->length()));
  ASSERT_EQ(1u, stream_.messages()->size());
  EXPECT_EQ(kSHLO, (*stream_.messages())[0].tag);
  EXPECT_EQ(2u, (*stream_.messages())[0].tag_value_map.size());
  EXPECT_EQ("abc", (*stream_.messages())[0].tag_value_map[1]);
  EXPECT_EQ("def", (*stream_.messages())[0].tag_value_map[2]);
}

TEST_F(QuicCryptoStreamTest, ProcessBadData) {
  string bad(message_data_->data(), message_data_->length());
  bad[6] = 0x7F;  // out of order tag

  EXPECT_CALL(*connection_,
              SendConnectionClose(QUIC_CRYPTO_TAGS_OUT_OF_ORDER));
  EXPECT_EQ(0u, stream_.ProcessData(bad.data(), bad.length()));
}

}  // namespace
}  // namespace test
}  // namespace net