Properly setup MockPeerConnectionObserver in tests.

This CL prevents dereferencing potentially null pointer by:
* Setting the pointer in client code.
* Checking the pointer before use.

Bug: webrtc:9855
Change-Id: I90c3d00eedfa4bf97954f4795a83e28894cc40f7
Reviewed-on: https://webrtc-review.googlesource.com/c/107706
Reviewed-by: Henrik Boström <hbos@webrtc.org>
Reviewed-by: Patrik Höglund <phoglund@webrtc.org>
Commit-Queue: Yves Gerey <yvesg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25405}
This commit is contained in:
Yves Gerey
2018-10-25 10:28:12 +02:00
committed by Commit Bot
parent 22a8f98dd5
commit f3ff14c00b
2 changed files with 16 additions and 0 deletions

View File

@ -3956,6 +3956,7 @@ class PeerConnectionMediaConfigTest : public testing::Test {
rtc::scoped_refptr<PeerConnectionInterface> pc(
pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
EXPECT_TRUE(pc.get());
observer_.SetPeerConnectionInterface(pc.get());
return pc->GetConfiguration().media_config;
}
@ -3963,6 +3964,17 @@ class PeerConnectionMediaConfigTest : public testing::Test {
MockPeerConnectionObserver observer_;
};
// This sanity check validates the test infrastructure itself.
TEST_F(PeerConnectionMediaConfigTest, TestCreateAndClose) {
PeerConnectionInterface::RTCConfiguration config;
rtc::scoped_refptr<PeerConnectionInterface> pc(
pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
EXPECT_TRUE(pc.get());
observer_.SetPeerConnectionInterface(pc.get()); // Required.
pc->Close(); // No abort -> ok.
SUCCEED();
}
// This test verifies the default behaviour with no constraints and a
// default RTCConfiguration.
TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {

View File

@ -67,6 +67,7 @@ class MockPeerConnectionObserver : public PeerConnectionObserver {
}
void OnSignalingChange(
PeerConnectionInterface::SignalingState new_state) override {
RTC_DCHECK(pc_);
RTC_DCHECK(pc_->signaling_state() == new_state);
state_ = new_state;
}
@ -92,6 +93,7 @@ class MockPeerConnectionObserver : public PeerConnectionObserver {
void OnIceConnectionChange(
PeerConnectionInterface::IceConnectionState new_state) override {
RTC_DCHECK(pc_);
RTC_DCHECK(pc_->ice_connection_state() == new_state);
// When ICE is finished, the caller will get to a kIceConnectionCompleted
// state, because it has the ICE controlling role, while the callee
@ -104,12 +106,14 @@ class MockPeerConnectionObserver : public PeerConnectionObserver {
}
void OnIceGatheringChange(
PeerConnectionInterface::IceGatheringState new_state) override {
RTC_DCHECK(pc_);
RTC_DCHECK(pc_->ice_gathering_state() == new_state);
ice_gathering_complete_ =
new_state == PeerConnectionInterface::kIceGatheringComplete;
callback_triggered_ = true;
}
void OnIceCandidate(const IceCandidateInterface* candidate) override {
RTC_DCHECK(pc_);
RTC_DCHECK(PeerConnectionInterface::kIceGatheringNew !=
pc_->ice_gathering_state());
candidates_.push_back(absl::make_unique<JsepIceCandidate>(