From 18a2f387e5151efef2d172936bd5d42bdc49a2f4 Mon Sep 17 00:00:00 2001 From: "Byron Campen [:bwc]" Date: Wed, 18 Sep 2013 12:36:13 -0700 Subject: [PATCH] Bug 916187. Part 2. Using the test stun server, write some tests to exercise the checking logic written in 908740. r=abr --- media/mtransport/databuffer.h | 1 + media/mtransport/test/ice_unittest.cpp | 46 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/media/mtransport/databuffer.h b/media/mtransport/databuffer.h index f8f890b7f9a..52fe33be0ee 100644 --- a/media/mtransport/databuffer.h +++ b/media/mtransport/databuffer.h @@ -11,6 +11,7 @@ #include #include #include +#include namespace mozilla { diff --git a/media/mtransport/test/ice_unittest.cpp b/media/mtransport/test/ice_unittest.cpp index 0e18383df18..984ca3088c6 100644 --- a/media/mtransport/test/ice_unittest.cpp +++ b/media/mtransport/test/ice_unittest.cpp @@ -602,6 +602,28 @@ class IceGatherTest : public ::testing::Test { ASSERT_TRUE_WAIT(peer_->gathering_complete(), 10000); } + + void UseFakeStunServerWithResponse(const std::string& fake_addr, + uint16_t fake_port) { + TestStunServer::GetInstance()->SetResponseAddr(fake_addr, fake_port); + // Sets an additional stun server + peer_->SetStunServer(TestStunServer::GetInstance()->addr(), + TestStunServer::GetInstance()->port()); + } + + // NB: Only does substring matching, watch out for stuff like "1.2.3.4" + // matching "21.2.3.47". " 1.2.3.4 " should not have false positives. + bool StreamHasMatchingCandidate(unsigned int stream, + const std::string& match) { + std::vector candidates = peer_->GetCandidates(stream); + for (size_t c = 0; c < candidates.size(); ++c) { + if (std::string::npos != candidates[c].find(match)) { + return true; + } + } + return false; + } + protected: mozilla::ScopedDeletePtr peer_; }; @@ -876,6 +898,30 @@ TEST_F(IceGatherTest, TestBogusCandidate) { peer_->ParseCandidate(0, kBogusIceCandidate); } +TEST_F(IceGatherTest, VerifyTestStunServer) { + UseFakeStunServerWithResponse("192.0.2.133", 3333); + Gather(); + ASSERT_TRUE(StreamHasMatchingCandidate(0, " 192.0.2.133 3333 ")); +} + +TEST_F(IceGatherTest, TestStunServerReturnsWildcardAddr) { + UseFakeStunServerWithResponse("0.0.0.0", 3333); + Gather(); + ASSERT_FALSE(StreamHasMatchingCandidate(0, " 0.0.0.0 ")); +} + +TEST_F(IceGatherTest, TestStunServerReturnsPort0) { + UseFakeStunServerWithResponse("192.0.2.133", 0); + Gather(); + ASSERT_FALSE(StreamHasMatchingCandidate(0, " 192.0.2.133 0 ")); +} + +TEST_F(IceGatherTest, TestStunServerReturnsLoopbackAddr) { + UseFakeStunServerWithResponse("127.0.0.133", 3333); + Gather(); + ASSERT_FALSE(StreamHasMatchingCandidate(0, " 127.0.0.133 ")); +} + TEST_F(IceConnectTest, TestGather) { AddStream("first", 1); ASSERT_TRUE(Gather(true));