Commit 332a7ca9 authored by Seth Hampson's avatar Seth Hampson Committed by Commit Bot

Updates to P2PQuicTransport tests.

As part of the effort to add read/write to the P2PQuicTransportStream,
some of the stream level tests need to be moved into their own more
isolated testing environment. This includes using a MockQuicSession.
This change moves the stream unit tests to this new environment, and
also updates the p2p_quic_transport_test.cc to use Mock objects instead
of the fake objects for the delegates.

Bug: 874296
Change-Id: Ifdfd30f753a2b6090c8f73330978c5feed2b9cf6
Reviewed-on: https://chromium-review.googlesource.com/c/1312971Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarHenrik Boström <hbos@chromium.org>
Commit-Queue: Seth Hampson <shampson@chromium.org>
Cr-Commit-Position: refs/heads/master@{#605753}
parent 44323658
...@@ -196,7 +196,9 @@ jumbo_source_set("modules_testing") { ...@@ -196,7 +196,9 @@ jumbo_source_set("modules_testing") {
"peerconnection/adapters/test/mock_ice_transport_adapter_cross_thread_factory.h", "peerconnection/adapters/test/mock_ice_transport_adapter_cross_thread_factory.h",
"peerconnection/adapters/test/mock_p2p_quic_packet_transport.h", "peerconnection/adapters/test/mock_p2p_quic_packet_transport.h",
"peerconnection/adapters/test/mock_p2p_quic_stream.h", "peerconnection/adapters/test/mock_p2p_quic_stream.h",
"peerconnection/adapters/test/mock_p2p_quic_stream_delegate.h",
"peerconnection/adapters/test/mock_p2p_quic_transport.h", "peerconnection/adapters/test/mock_p2p_quic_transport.h",
"peerconnection/adapters/test/mock_p2p_quic_transport_delegate.h",
"peerconnection/adapters/test/mock_p2p_quic_transport_factory.h", "peerconnection/adapters/test/mock_p2p_quic_transport_factory.h",
"peerconnection/testing/internals_rtc_certificate.cc", "peerconnection/testing/internals_rtc_certificate.cc",
"peerconnection/testing/internals_rtc_certificate.h", "peerconnection/testing/internals_rtc_certificate.h",
...@@ -309,6 +311,7 @@ jumbo_source_set("unit_tests") { ...@@ -309,6 +311,7 @@ jumbo_source_set("unit_tests") {
"payments/payment_test_helper.cc", "payments/payment_test_helper.cc",
"payments/payment_test_helper.h", "payments/payment_test_helper.h",
"payments/payments_validators_test.cc", "payments/payments_validators_test.cc",
"peerconnection/adapters/p2p_quic_stream_unittest.cc",
"peerconnection/adapters/p2p_quic_transport_test.cc", "peerconnection/adapters/p2p_quic_transport_test.cc",
"peerconnection/rtc_data_channel_test.cc", "peerconnection/rtc_data_channel_test.cc",
"peerconnection/rtc_ice_transport_test.cc", "peerconnection/rtc_ice_transport_test.cc",
......
...@@ -63,4 +63,13 @@ void P2PQuicStreamImpl::OnFinRead() { ...@@ -63,4 +63,13 @@ void P2PQuicStreamImpl::OnFinRead() {
delegate_->OnRemoteFinish(); delegate_->OnRemoteFinish();
} }
void P2PQuicStreamImpl::OnClose() {
closed_ = true;
quic::QuicStream::OnClose();
}
bool P2PQuicStreamImpl::IsClosedForTesting() {
return closed_;
}
} // namespace blink } // namespace blink
...@@ -47,9 +47,20 @@ class MODULES_EXPORT P2PQuicStreamImpl final : public P2PQuicStream, ...@@ -47,9 +47,20 @@ class MODULES_EXPORT P2PQuicStreamImpl final : public P2PQuicStream,
// receive a stream frame with the FIN bit. // receive a stream frame with the FIN bit.
void OnFinRead() override; void OnFinRead() override;
// Called by the quic::QuicSession. This means the stream is closed for
// reading and writing, and can now be deleted by the quic::QuicSession.
void OnClose() override;
// For testing purposes. This is returns true after quic::QuicStream::OnClose
bool IsClosedForTesting();
private: private:
using quic::QuicStream::Reset; using quic::QuicStream::Reset;
Delegate* delegate_; Delegate* delegate_;
// Set after OnClose gets called.
bool closed_ = false;
}; };
} // namespace blink } // namespace blink
......
// Copyright 2018 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 "third_party/blink/renderer/modules/peerconnection/adapters/p2p_quic_stream.h"
#include "net/test/gtest_util.h"
#include "net/third_party/quic/test_tools/quic_test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/blink/renderer/modules/peerconnection/adapters/p2p_quic_stream_impl.h"
#include "third_party/blink/renderer/modules/peerconnection/adapters/test/mock_p2p_quic_stream_delegate.h"
namespace blink {
namespace {
using testing::_;
using testing::Invoke;
const quic::QuicStreamId kStreamId = 5;
} // namespace
// Unit tests for the P2PQuicStream, using a mock QuicSession, which allows
// us to isolate testing the behaviors of reading and writing.
class P2PQuicStreamTest : public testing::Test {
public:
P2PQuicStreamTest()
: connection_(
new quic::test::MockQuicConnection(&connection_helper_,
&alarm_factory_,
quic::Perspective::IS_CLIENT)),
session_(connection_) {
session_.Initialize();
stream_ = new P2PQuicStreamImpl(kStreamId, &session_);
stream_->SetDelegate(&delegate_);
// The session takes the ownership of the stream.
session_.ActivateStream(std::unique_ptr<P2PQuicStreamImpl>(stream_));
// DCHECKS get hit when the clock is at 0.
connection_helper_.AdvanceTime(quic::QuicTime::Delta::FromSeconds(1));
}
~P2PQuicStreamTest() override {}
quic::test::MockQuicConnectionHelper connection_helper_;
quic::test::MockAlarmFactory alarm_factory_;
// Owned by the |session_|.
quic::test::MockQuicConnection* connection_;
// The MockQuicSession allows us to see data that is being written and control
// whether the data is being "sent across" or blocked.
quic::test::MockQuicSession session_;
MockP2PQuicStreamDelegate delegate_;
// Owned by |session_|.
P2PQuicStreamImpl* stream_;
};
TEST_F(P2PQuicStreamTest, StreamFinishSendsFinAndCanNoLongerWrite) {
EXPECT_CALL(session_, WritevData(stream_, kStreamId, _, _, _))
.WillOnce(Invoke(quic::test::MockQuicSession::ConsumeData));
stream_->Finish();
EXPECT_TRUE(stream_->fin_sent());
EXPECT_TRUE(stream_->write_side_closed());
EXPECT_FALSE(stream_->reading_stopped());
}
TEST_F(P2PQuicStreamTest, StreamResetSendsRst) {
EXPECT_CALL(session_, SendRstStream(kStreamId, _, _));
stream_->Reset();
EXPECT_TRUE(stream_->rst_sent());
}
// Tests that when a stream receives a stream frame with the FIN bit set it
// will fire the appropriate callback and close the stream for reading.
TEST_F(P2PQuicStreamTest, StreamOnStreamFrameWithFin) {
EXPECT_CALL(delegate_, OnRemoteFinish());
quic::QuicStreamFrame fin_frame(kStreamId, /*fin=*/true, 0, 0);
stream_->OnStreamFrame(fin_frame);
EXPECT_TRUE(stream_->reading_stopped());
EXPECT_FALSE(stream_->write_side_closed());
}
// Tests that when a stream receives a stream frame with the FIN bit set after
// it has called Finish(), then the stream will close.
TEST_F(P2PQuicStreamTest, StreamClosedAfterReceivesFin) {
EXPECT_CALL(session_, WritevData(stream_, kStreamId, _, _, _))
.WillOnce(Invoke(quic::test::MockQuicSession::ConsumeData));
stream_->Finish();
EXPECT_FALSE(stream_->IsClosedForTesting());
quic::QuicStreamFrame fin_frame(stream_->id(), /*fin=*/true, 0, 0);
stream_->OnStreamFrame(fin_frame);
EXPECT_TRUE(stream_->reading_stopped());
EXPECT_TRUE(stream_->write_side_closed());
EXPECT_TRUE(stream_->IsClosedForTesting());
}
// Tests that when a stream calls Finish() after receiving a stream frame with
// the FIN bit then the stream will close.
TEST_F(P2PQuicStreamTest, StreamClosedAfterFinish) {
quic::QuicStreamFrame fin_frame(stream_->id(), /*fin=*/true, 0, 0);
stream_->OnStreamFrame(fin_frame);
EXPECT_FALSE(stream_->IsClosedForTesting());
EXPECT_CALL(session_, WritevData(stream_, kStreamId, _, _, _))
.WillOnce(Invoke(quic::test::MockQuicSession::ConsumeData));
stream_->Finish();
EXPECT_TRUE(stream_->IsClosedForTesting());
}
// Tests that when a stream receives a RST_STREAM frame it will fire the
// appropriate callback and the stream will become closed.
TEST_F(P2PQuicStreamTest, StreamClosedAfterReceivingReset) {
EXPECT_CALL(delegate_, OnRemoteReset());
quic::QuicRstStreamFrame rst_frame(quic::kInvalidControlFrameId, kStreamId,
quic::QUIC_STREAM_CANCELLED, 0);
stream_->OnStreamReset(rst_frame);
EXPECT_TRUE(stream_->IsClosedForTesting());
}
} // namespace blink
// Copyright 2018 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.
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_PEERCONNECTION_ADAPTERS_TEST_MOCK_P2P_QUIC_STREAM_DELEGATE_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_PEERCONNECTION_ADAPTERS_TEST_MOCK_P2P_QUIC_STREAM_DELEGATE_H_
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/blink/renderer/modules/peerconnection/adapters/p2p_quic_stream.h"
namespace blink {
class MockP2PQuicStreamDelegate
: public testing::NiceMock<P2PQuicStream::Delegate> {
public:
// P2PQuicStream::Delegate overrides.
MOCK_METHOD0(OnRemoteReset, void());
MOCK_METHOD0(OnRemoteFinish, void());
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_PEERCONNECTION_ADAPTERS_TEST_MOCK_P2P_QUIC_STREAM_DELEGATE_H_
// Copyright 2018 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.
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_PEERCONNECTION_ADAPTERS_TEST_MOCK_P2P_QUIC_TRANSPORT_DELEGATE_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_PEERCONNECTION_ADAPTERS_TEST_MOCK_P2P_QUIC_TRANSPORT_DELEGATE_H_
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/blink/renderer/modules/peerconnection/adapters/p2p_quic_transport.h"
namespace blink {
class MockP2PQuicTransportDelegate
: public testing::NiceMock<P2PQuicTransport::Delegate> {
public:
// P2PQuicTransport::Delegate overrides.
MOCK_METHOD0(OnRemoteStopped, void());
MOCK_METHOD2(OnConnectionFailed, void(const std::string&, bool));
MOCK_METHOD0(OnConnected, void());
MOCK_METHOD1(OnStream, void(P2PQuicStream*));
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_PEERCONNECTION_ADAPTERS_TEST_MOCK_P2P_QUIC_TRANSPORT_DELEGATE_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment