From aaa76f3ba8ef4d57c926402279823444e2ab1ff3 Mon Sep 17 00:00:00 2001 From: "phoglund@webrtc.org" Date: Thu, 9 Feb 2012 16:41:30 +0000 Subject: [PATCH] Rewrote network test. BUG= TEST= Review URL: https://webrtc-codereview.appspot.com/383003 git-svn-id: http://webrtc.googlecode.com/svn/trunk@1656 4adac7df-926f-26a2-2b94-8c16560cd09d --- .../mock/mock_voe_connection_observer.h | 26 ++ .../main/interface/mock/mock_voe_observer.h | 29 +++ .../test/auto_test/standard/network_test.cc | 199 +++++++++++++++ .../main/test/auto_test/voe_standard_test.cc | 229 ------------------ .../main/test/voice_engine_tests.gypi | 1 + 5 files changed, 255 insertions(+), 229 deletions(-) create mode 100644 src/voice_engine/main/interface/mock/mock_voe_connection_observer.h create mode 100644 src/voice_engine/main/interface/mock/mock_voe_observer.h create mode 100644 src/voice_engine/main/test/auto_test/standard/network_test.cc diff --git a/src/voice_engine/main/interface/mock/mock_voe_connection_observer.h b/src/voice_engine/main/interface/mock/mock_voe_connection_observer.h new file mode 100644 index 000000000..9eb92719e --- /dev/null +++ b/src/voice_engine/main/interface/mock/mock_voe_connection_observer.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef MOCK_VOE_CONNECTION_OBSERVER_H_ +#define MOCK_VOE_CONNECTION_OBSERVER_H_ + +#include "voe_network.h" + +namespace webrtc { + +class MockVoeConnectionObserver : public VoEConnectionObserver { + public: + MOCK_METHOD2(OnPeriodicDeadOrAlive, void(const int channel, + const bool alive)); +}; + +} + +#endif // MOCK_VOE_CONNECTION_OBSERVER_H_ diff --git a/src/voice_engine/main/interface/mock/mock_voe_observer.h b/src/voice_engine/main/interface/mock/mock_voe_observer.h new file mode 100644 index 000000000..70d1c1383 --- /dev/null +++ b/src/voice_engine/main/interface/mock/mock_voe_observer.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef WEBRTC_VOICE_ENGINE_MOCK_VOE_OBSERVER_H_ +#define WEBRTC_VOICE_ENGINE_MOCK_VOE_OBSERVER_H_ + +#include "voe_base.h" +#include "gmock/gmock.h" + +namespace webrtc { + +class MockVoEObserver: public VoiceEngineObserver { + public: + MockVoEObserver() {} + virtual ~MockVoEObserver() {} + + MOCK_METHOD2(CallbackOnError, void(const int channel, const int error_code)); +}; + +} + +#endif // WEBRTC_VOICE_ENGINE_MOCK_VOE_OBSERVER_H_ diff --git a/src/voice_engine/main/test/auto_test/standard/network_test.cc b/src/voice_engine/main/test/auto_test/standard/network_test.cc new file mode 100644 index 000000000..335fba6b4 --- /dev/null +++ b/src/voice_engine/main/test/auto_test/standard/network_test.cc @@ -0,0 +1,199 @@ +/* + * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "after_streaming_fixture.h" +#include "mock/mock_voe_observer.h" +#include "mock/mock_voe_connection_observer.h" +#include "voe_test_interface.h" + +static const int kDefaultRtpPort = 8000; +static const int kDefaultRtcpPort = 8001; + +class NetworkTest : public AfterStreamingFixture { +}; + +using ::testing::Between; + +TEST_F(NetworkTest, GetSourceInfoReturnsPortsAndIpAfterReceivingPackets) { + // Give some time to send speech packets. + Sleep(200); + + int rtp_port = 0; + int rtcp_port = 0; + char source_ip[32] = "127.0.0.1"; + + EXPECT_EQ(0, voe_network_->GetSourceInfo(channel_, rtp_port, rtcp_port, + source_ip)); + + EXPECT_EQ(kDefaultRtpPort, rtp_port); + EXPECT_EQ(kDefaultRtcpPort, rtcp_port); +} + +TEST_F(NetworkTest, NoFilterIsEnabledByDefault) { + int filter_rtp_port = -1; + int filter_rtcp_port = -1; + char filter_ip[64] = { 0 }; + + EXPECT_EQ(0, voe_network_->GetSourceFilter( + channel_, filter_rtp_port, filter_rtcp_port, filter_ip)); + + EXPECT_EQ(0, filter_rtp_port); + EXPECT_EQ(0, filter_rtcp_port); + EXPECT_STREQ("", filter_ip); +} + +TEST_F(NetworkTest, ManualCanFilterRtpPort) { + TEST_LOG("No filter, should hear audio.\n"); + Sleep(1000); + + int port_to_block = kDefaultRtpPort + 10; + EXPECT_EQ(0, voe_network_->SetSourceFilter(channel_, port_to_block)); + + // Changes should take effect immediately. + int filter_rtp_port = -1; + int filter_rtcp_port = -1; + char filter_ip[64] = { 0 }; + + EXPECT_EQ(0, voe_network_->GetSourceFilter( + channel_, filter_rtp_port, filter_rtcp_port, filter_ip)); + + EXPECT_EQ(port_to_block, filter_rtp_port); + + TEST_LOG("Now filtering port %d, should not hear audio.\n", port_to_block); + Sleep(1000); + + TEST_LOG("Removing filter, should hear audio.\n"); + EXPECT_EQ(0, voe_network_->SetSourceFilter(channel_, 0)); + Sleep(1000); +} + +TEST_F(NetworkTest, ManualCanFilterIp) { + TEST_LOG("You should hear audio.\n"); + Sleep(1000); + + int rtcp_port_to_block = kDefaultRtcpPort + 10; + TEST_LOG("Filtering IP 10.10.10.10, should not hear audio.\n"); + EXPECT_EQ(0, voe_network_->SetSourceFilter( + channel_, 0, rtcp_port_to_block, "10.10.10.10")); + + int filter_rtp_port = -1; + int filter_rtcp_port = -1; + char filter_ip[64] = { 0 }; + EXPECT_EQ(0, voe_network_->GetSourceFilter( + channel_, filter_rtp_port, filter_rtcp_port, filter_ip)); + + EXPECT_EQ(0, filter_rtp_port); + EXPECT_EQ(rtcp_port_to_block, filter_rtcp_port); + EXPECT_STREQ("10.10.10.10", filter_ip); +} + +TEST_F(NetworkTest, + CallsObserverOnTimeoutAndRestartWhenPacketTimeoutNotificationIsEnabled) { + // First, get rid of the default, asserting observer and install our observer. + EXPECT_EQ(0, voe_base_->DeRegisterVoiceEngineObserver()); + webrtc::MockVoEObserver mock_observer; + EXPECT_EQ(0, voe_base_->RegisterVoiceEngineObserver(mock_observer)); + + // Define expectations. + int expected_error = VE_RECEIVE_PACKET_TIMEOUT; + EXPECT_CALL(mock_observer, CallbackOnError(channel_, expected_error)) + .Times(1); + expected_error = VE_PACKET_RECEIPT_RESTARTED; + EXPECT_CALL(mock_observer, CallbackOnError(channel_, expected_error)) + .Times(1); + + // Get some speech going. + Sleep(500); + + // Enable packet timeout. + EXPECT_EQ(0, voe_network_->SetPacketTimeoutNotification(channel_, true, 1)); + + // Trigger a timeout. + EXPECT_EQ(0, voe_base_->StopSend(channel_)); + Sleep(1500); + + // Trigger a restart event. + EXPECT_EQ(0, voe_base_->StartSend(channel_)); + Sleep(500); +} + +TEST_F(NetworkTest, DoesNotCallDeRegisteredObserver) { + // De-register the default observer. This test will fail if the observer gets + // called for any reason, so if this de-register doesn't work the test will + // fail. + EXPECT_EQ(0, voe_base_->DeRegisterVoiceEngineObserver()); + + // Get some speech going. + Sleep(500); + + // Enable packet timeout. + EXPECT_EQ(0, voe_network_->SetPacketTimeoutNotification(channel_, true, 1)); + + // Trigger a timeout. + EXPECT_EQ(0, voe_base_->StopSend(channel_)); + Sleep(1500); +} + +TEST_F(NetworkTest, DeadOrAliveObserverSeesAliveMessagesIfEnabled) { + webrtc::MockVoeConnectionObserver mock_observer; + EXPECT_EQ(0, voe_network_->RegisterDeadOrAliveObserver( + channel_, mock_observer)); + + // We should be called about 4 times in four seconds, but 3 is OK too. + EXPECT_CALL(mock_observer, OnPeriodicDeadOrAlive(channel_, true)) + .Times(Between(3, 4)); + + EXPECT_EQ(0, voe_network_->SetPeriodicDeadOrAliveStatus(channel_, true, 1)); + Sleep(4000); + + EXPECT_EQ(0, voe_network_->DeRegisterDeadOrAliveObserver(channel_)); +} + +TEST_F(NetworkTest, DeadOrAliveObserverSeesDeadMessagesIfEnabled) { + // "When do you see them?" - "All the time!" + webrtc::MockVoeConnectionObserver mock_observer; + EXPECT_EQ(0, voe_network_->RegisterDeadOrAliveObserver( + channel_, mock_observer)); + + Sleep(500); + + // We should be called about 4 times in four seconds, but 3 is OK too. + EXPECT_CALL(mock_observer, OnPeriodicDeadOrAlive(channel_, false)) + .Times(Between(3, 4)); + + EXPECT_EQ(0, voe_network_->SetPeriodicDeadOrAliveStatus(channel_, true, 1)); + EXPECT_EQ(0, voe_rtp_rtcp_->SetRTCPStatus(channel_, false)); + EXPECT_EQ(0, voe_base_->StopSend(channel_)); + Sleep(4000); + + EXPECT_EQ(0, voe_network_->DeRegisterDeadOrAliveObserver(channel_)); +} + +TEST_F(NetworkTest, CanSwitchToExternalTransport) { + EXPECT_EQ(0, voe_base_->StopReceive(channel_)); + EXPECT_EQ(0, voe_base_->DeleteChannel(channel_)); + channel_ = voe_base_->CreateChannel(); + + voetest::FakeExternalTransport external_transport(voe_network_); + EXPECT_EQ(0, voe_network_->RegisterExternalTransport( + channel_, external_transport)); + + EXPECT_EQ(0, voe_base_->StartReceive(channel_)); + EXPECT_EQ(0, voe_base_->StartSend(channel_)); + EXPECT_EQ(0, voe_base_->StartPlayout(channel_)); + + Sleep(1000); + + EXPECT_EQ(0, voe_base_->StopSend(channel_)); + EXPECT_EQ(0, voe_base_->StopPlayout(channel_)); + EXPECT_EQ(0, voe_base_->StopReceive(channel_)); + + EXPECT_EQ(0, voe_network_->DeRegisterExternalTransport(channel_)); +} diff --git a/src/voice_engine/main/test/auto_test/voe_standard_test.cc b/src/voice_engine/main/test/auto_test/voe_standard_test.cc index da682d5ea..e1148eccd 100644 --- a/src/voice_engine/main/test/auto_test/voe_standard_test.cc +++ b/src/voice_engine/main/test/auto_test/voe_standard_test.cc @@ -1109,235 +1109,6 @@ int VoETestManager::DoStandardTest() { //////////// // Network -#ifdef _TEST_NETWORK_ - TEST_LOG("\n\n+++ Network tests +++\n\n"); - -#ifndef WEBRTC_EXTERNAL_TRANSPORT - int sourceRtpPort = 1234; - int sourceRtcpPort = 1235; - - int filterPort = -1; - int filterPortRTCP = -1; - char sourceIp[32] = "127.0.0.1"; - char filterIp[64] = { 0 }; - - SLEEP(200); // Make sure we have received packets - - TEST_MUSTPASS(voe_network_->GetSourceInfo(0, - sourceRtpPort, - sourceRtcpPort, - sourceIp)); - - TEST_LOG("sourceIp = %s, sourceRtpPort = %d, sourceRtcpPort = %d\n", - sourceIp, sourceRtpPort, sourceRtcpPort); - TEST_MUSTPASS(8000 != sourceRtpPort); - TEST_MUSTPASS(8001 != sourceRtcpPort); - - TEST_MUSTPASS(voe_network_->GetSourceFilter(0, - filterPort, - filterPortRTCP, - filterIp)); - TEST_MUSTPASS(0 != filterPort); - TEST_MUSTPASS(0 != filterPortRTCP); - TEST_MUSTPASS(_stricmp(filterIp, "")); - - TEST_LOG("Set filter port to %d => should hear audio\n", sourceRtpPort); - TEST_MUSTPASS(voe_network_->SetSourceFilter(0, - sourceRtpPort, - sourceRtcpPort, - "0.0.0.0")); - TEST_MUSTPASS(voe_network_->GetSourceFilter(0, - filterPort, - filterPortRTCP, - filterIp)); - TEST_MUSTPASS(sourceRtpPort != filterPort); - TEST_MUSTPASS(sourceRtcpPort != filterPortRTCP); - TEST_MUSTPASS(_stricmp(filterIp, "0.0.0.0")); - SLEEP(1000); - TEST_LOG("Set filter port to %d => should *not* hear audio\n", - sourceRtpPort + 10); - TEST_MUSTPASS(voe_network_->SetSourceFilter(0, sourceRtpPort+10)); - TEST_MUSTPASS(voe_network_->GetSourceFilter(0, - filterPort, - filterPortRTCP, - filterIp)); - TEST_MUSTPASS(sourceRtpPort+10 != filterPort); - SLEEP(1000); - TEST_LOG("Disable port filter => should hear audio again\n"); - TEST_MUSTPASS(voe_network_->SetSourceFilter(0, 0)); - SLEEP(1000); - - if (voe_rtp_rtcp_) { - TEST_MUSTPASS(voe_rtp_rtcp_->SetRTCP_CNAME(0, "Tomas")); - } - - TEST_LOG("Set filter IP to %s => should hear audio\n", sourceIp); - TEST_MUSTPASS(voe_network_->SetSourceFilter(0, 0, sourceRtcpPort+10, - sourceIp)); - TEST_MUSTPASS(voe_network_->GetSourceFilter(0, - filterPort, - filterPortRTCP, - filterIp)); - TEST_MUSTPASS(_stricmp(filterIp, sourceIp)); - SLEEP(1000); - TEST_LOG("Set filter IP to 10.10.10.10 => should *not* hear audio\n"); - TEST_MUSTPASS(voe_network_->SetSourceFilter(0, 0, sourceRtcpPort+10, - "10.10.10.10")); - TEST_MUSTPASS(voe_network_->GetSourceFilter(0, filterPort, filterPort, - filterIp)); - TEST_MUSTPASS(_stricmp(filterIp, "10.10.10.10")); - SLEEP(1000); - TEST_LOG("Disable IP filter => should hear audio again\n"); - TEST_MUSTPASS(voe_network_->SetSourceFilter(0, 0, sourceRtcpPort+10, - "0.0.0.0")); - SLEEP(1000); - TEST_LOG("Set filter IP to 10.10.10.10 => should *not* hear audio\n"); - TEST_MUSTPASS(voe_network_->SetSourceFilter(0, 0, sourceRtcpPort+10, - "10.10.10.10")); - SLEEP(1000); - - if (voe_rtp_rtcp_) { - char tmpStr[64]; - SLEEP(2000); - TEST_LOG("Checking RTCP port filter with CNAME...\n"); - TEST_MUSTPASS(voe_rtp_rtcp_->GetRemoteRTCP_CNAME(0, tmpStr)); - TEST_MUSTPASS(!_stricmp("Tomas", tmpStr)); - TEST_MUSTPASS(voe_rtp_rtcp_->SetRTCP_CNAME(0, "Niklas")); - } else { - TEST_LOG("Skipping RTCP port filter test since there is no RTP/RTCP " - "interface!\n"); - } - - TEST_LOG("Disable IP filter => should hear audio again\n"); - TEST_MUSTPASS(voe_network_->SetSourceFilter(0, 0, 0, NULL)); - TEST_MUSTPASS(voe_network_->GetSourceFilter(0, filterPort, filterPortRTCP, - filterIp)); - TEST_MUSTPASS(_stricmp(filterIp, "")); - SLEEP(1000); - - TEST_LOG("Wait 2 seconds for packet timeout...\n"); - TEST_LOG("You should see runtime error %d\n", VE_RECEIVE_PACKET_TIMEOUT); - TEST_MUSTPASS(voe_base_->StopSend(0)); - TEST_MUSTPASS(voe_network_->SetPacketTimeoutNotification(0, true, 2)); - SLEEP(3000); - -#if !defined(_INSTRUMENTATION_TESTING_) - TEST_LOG("error_observer.code is %d\n", error_observer.code); - TEST_MUSTPASS(error_observer.code != VE_RECEIVE_PACKET_TIMEOUT); -#endif - error_observer.code = -1; - TEST_MUSTPASS(voe_base_->StartSend(0)); - if (voe_file_) { - TEST_LOG("Start playing a file as microphone again \n"); - TEST_MUSTPASS(voe_file_->StartPlayingFileAsMicrophone(0, - AudioFilename(), - true, - true)); - } - TEST_LOG("You should see runtime error %d\n", VE_PACKET_RECEIPT_RESTARTED); - SLEEP(1000); -#if !defined(_INSTRUMENTATION_TESTING_) - TEST_MUSTPASS(error_observer.code != VE_PACKET_RECEIPT_RESTARTED); -#endif - -#if !defined(_INSTRUMENTATION_TESTING_) - TEST_LOG("Disabling observer, no runtime error should be seen...\n"); - TEST_MUSTPASS(voe_base_->DeRegisterVoiceEngineObserver()); - error_observer.code = -1; - TEST_MUSTPASS(voe_base_->StopSend(0)); - TEST_MUSTPASS(voe_network_->SetPacketTimeoutNotification(0, true, 2)); - SLEEP(2500); - TEST_MUSTPASS(error_observer.code != -1); - // disable notifications to avoid additional 8082 callbacks - TEST_MUSTPASS(voe_network_->SetPacketTimeoutNotification(0, false, 2)); - TEST_MUSTPASS(voe_base_->StartSend(0)); - if (voe_file_) { - TEST_LOG("Start playing a file as microphone again \n"); - TEST_MUSTPASS(voe_file_->StartPlayingFileAsMicrophone(0, - AudioFilename(), - true, - true)); - } - SLEEP(1000); - /// TEST_MUSTPASS(obs.code != -1); - TEST_LOG("Enabling observer again\n"); - TEST_MUSTPASS(voe_base_->RegisterVoiceEngineObserver(error_observer)); -#endif - - TEST_LOG("Enable dead-or-alive callbacks for 4 seconds (dT=1sec)...\n"); - TEST_LOG("You should see ALIVE messages\n"); - - MyDeadOrAlive dead_or_alive_observer; - TEST_MUSTPASS(voe_network_->RegisterDeadOrAliveObserver( - 0, dead_or_alive_observer)); - TEST_MUSTPASS(voe_network_->SetPeriodicDeadOrAliveStatus(0, true, 1)); - SLEEP(4000); - - // stop sending and flush dead-or-alive states - if (voe_rtp_rtcp_) { - TEST_MUSTPASS(voe_rtp_rtcp_->SetRTCPStatus(0, false)); - } - TEST_MUSTPASS(voe_base_->StopSend(0)); - SLEEP(500); - - TEST_LOG("Disable sending for 4 seconds (dT=1sec)...\n"); - TEST_LOG("You should see DEAD messages (one ALIVE message might" - " sneak in if you are unlucky)\n"); - SLEEP(4000); - TEST_LOG("Disable dead-or-alive callbacks.\n"); - TEST_MUSTPASS(voe_network_->SetPeriodicDeadOrAliveStatus(0, false)); - - TEST_LOG("Enabling external transport\n"); - TEST_MUSTPASS(voe_base_->StopReceive(0)); - - // recreate the channel to ensure that we can switch from transport - // to external transport - TEST_MUSTPASS(voe_base_->DeleteChannel(0)); - TEST_MUSTPASS(voe_base_->CreateChannel()); - - TEST_MUSTPASS(voe_network_->RegisterExternalTransport(0, channel0_transport)); - - TEST_MUSTPASS(voe_base_->StartReceive(0)); - TEST_MUSTPASS(voe_base_->StartSend(0)); - TEST_MUSTPASS(voe_base_->StartPlayout(0)); - if (voe_file_) { - TEST_LOG("Start playing a file as microphone again using" - " external transport\n"); - TEST_MUSTPASS(voe_file_->StartPlayingFileAsMicrophone(0, - AudioFilename(), - true, - true)); - } - SLEEP(4000); - - TEST_LOG("Disabling external transport\n"); - TEST_MUSTPASS(voe_base_->StopSend(0)); - TEST_MUSTPASS(voe_base_->StopPlayout(0)); - TEST_MUSTPASS(voe_base_->StopReceive(0)); - - TEST_MUSTPASS(voe_network_->DeRegisterExternalTransport(0)); - - TEST_MUSTPASS(voe_base_->SetSendDestination(0, 8000, "127.0.0.1")); - TEST_MUSTPASS(voe_base_->SetLocalReceiver(0, 8000)); - - TEST_MUSTPASS(voe_base_->StartReceive(0)); - TEST_MUSTPASS(voe_base_->StartSend(0)); - TEST_MUSTPASS(voe_base_->StartPlayout(0)); - if (voe_file_) { - TEST_LOG("Start playing a file as microphone again using transport\n"); - TEST_MUSTPASS(voe_file_->StartPlayingFileAsMicrophone(0, - AudioFilename(), - true, - true)); - } - SLEEP(2000); -#else - TEST_LOG("Skipping network tests - " - "WEBRTC_EXTERNAL_TRANSPORT is defined \n"); -#endif // #ifndef WEBRTC_EXTERNAL_TRANSPORT -#else - TEST_LOG("\n\n+++ Network tests NOT ENABLED +++\n"); -#endif // #ifdef _TEST_NETWORK_ /////////////// // CallReport diff --git a/src/voice_engine/main/test/voice_engine_tests.gypi b/src/voice_engine/main/test/voice_engine_tests.gypi index 55a0c1c4a..744581f32 100644 --- a/src/voice_engine/main/test/voice_engine_tests.gypi +++ b/src/voice_engine/main/test/voice_engine_tests.gypi @@ -46,6 +46,7 @@ 'auto_test/standard/manual_hold_test.cc', 'auto_test/standard/neteq_test.cc', 'auto_test/standard/network_before_streaming_test.cc', + 'auto_test/standard/network_test.cc', 'auto_test/standard/rtp_rtcp_before_streaming_test.cc', 'auto_test/standard/rtp_rtcp_test.cc', 'auto_test/standard/voe_base_misc_test.cc',