OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/bind.h" |
| 6 #include "base/file_util.h" |
| 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/macros.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "chrome/browser/media/webrtc_rtp_dump_handler.h" |
| 12 #include "chrome/browser/media/webrtc_rtp_dump_writer.h" |
| 13 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 class FakeDumpWriter : public WebRtcRtpDumpWriter { |
| 18 public: |
| 19 explicit FakeDumpWriter(size_t max_dump_size, |
| 20 const base::Closure& max_size_reached_callback) |
| 21 : WebRtcRtpDumpWriter(base::FilePath(), |
| 22 base::FilePath(), |
| 23 max_dump_size, |
| 24 base::Closure()), |
| 25 max_dump_size_(max_dump_size), |
| 26 current_dump_size_(0), |
| 27 max_size_reached_callback_(max_size_reached_callback) {} |
| 28 |
| 29 virtual void WriteRtpPacket(const uint8* packet_header, |
| 30 size_t header_length, |
| 31 size_t packet_length, |
| 32 bool incoming) OVERRIDE { |
| 33 current_dump_size_ += header_length; |
| 34 if (current_dump_size_ > max_dump_size_) |
| 35 max_size_reached_callback_.Run(); |
| 36 } |
| 37 |
| 38 virtual void EndDump(bool incoming, |
| 39 const EndDumpCallback& finished_callback) OVERRIDE { |
| 40 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 41 base::Bind(finished_callback, true)); |
| 42 } |
| 43 |
| 44 private: |
| 45 size_t max_dump_size_; |
| 46 size_t current_dump_size_; |
| 47 base::Closure max_size_reached_callback_; |
| 48 }; |
| 49 |
| 50 class WebRtcRtpDumpHandlerTest : public testing::Test { |
| 51 public: |
| 52 WebRtcRtpDumpHandlerTest() |
| 53 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
| 54 ResetDumpHandler(base::FilePath()); |
| 55 } |
| 56 |
| 57 void ResetDumpHandler(const base::FilePath& dir) { |
| 58 handler_.reset( |
| 59 new WebRtcRtpDumpHandler(dir.empty() ? base::FilePath("dummy") : dir)); |
| 60 |
| 61 scoped_ptr<WebRtcRtpDumpWriter> writer(new FakeDumpWriter( |
| 62 10, |
| 63 base::Bind(&WebRtcRtpDumpHandler::OnMaxDumpSizeReached, |
| 64 base::Unretained(handler_.get())))); |
| 65 |
| 66 handler_->SetDumpWriterForTesting(writer.Pass()); |
| 67 } |
| 68 |
| 69 MOCK_METHOD2(OnStopDumpFinished, |
| 70 void(bool success, const std::string& error)); |
| 71 |
| 72 protected: |
| 73 content::TestBrowserThreadBundle thread_bundle_; |
| 74 scoped_ptr<WebRtcRtpDumpHandler> handler_; |
| 75 }; |
| 76 |
| 77 TEST_F(WebRtcRtpDumpHandlerTest, StateTransition) { |
| 78 std::string error; |
| 79 |
| 80 WebRtcRtpDumpHandler::PacketType types[3]; |
| 81 types[0] = WebRtcRtpDumpHandler::PACKET_TYPE_INCOMING_ONLY; |
| 82 types[1] = WebRtcRtpDumpHandler::PACKET_TYPE_OUTGOING_ONLY; |
| 83 types[2] = WebRtcRtpDumpHandler::PACKET_TYPE_BOTH; |
| 84 |
| 85 WebRtcRtpDumpHandler::ReleasedDumps dumps; |
| 86 for (size_t i = 0; i < arraysize(types); ++i) { |
| 87 DVLOG(2) << "Verifying state transition: type = " << types[i]; |
| 88 |
| 89 // Only StartDump is allowed in STATE_NONE. |
| 90 EXPECT_CALL(*this, OnStopDumpFinished(false, testing::_)); |
| 91 handler_->StopDump(types[i], |
| 92 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 93 base::Unretained(this))); |
| 94 |
| 95 dumps = handler_->ReleaseDumps(); |
| 96 EXPECT_TRUE(dumps.incoming_dump_path.empty()); |
| 97 EXPECT_TRUE(dumps.outgoing_dump_path.empty()); |
| 98 |
| 99 EXPECT_TRUE(handler_->StartDump(types[i], &error)); |
| 100 base::RunLoop().RunUntilIdle(); |
| 101 |
| 102 // Only StopDump is allowed in STATE_STARTED. |
| 103 EXPECT_FALSE(handler_->StartDump(types[i], &error)); |
| 104 |
| 105 dumps = handler_->ReleaseDumps(); |
| 106 EXPECT_TRUE(dumps.incoming_dump_path.empty()); |
| 107 EXPECT_TRUE(dumps.outgoing_dump_path.empty()); |
| 108 |
| 109 EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)); |
| 110 handler_->StopDump(types[i], |
| 111 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 112 base::Unretained(this))); |
| 113 base::RunLoop().RunUntilIdle(); |
| 114 |
| 115 // Only ReleaseDump is allowed in STATE_STOPPED. |
| 116 EXPECT_FALSE(handler_->StartDump(types[i], &error)); |
| 117 |
| 118 EXPECT_CALL(*this, OnStopDumpFinished(false, testing::_)); |
| 119 handler_->StopDump(types[i], |
| 120 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 121 base::Unretained(this))); |
| 122 |
| 123 dumps = handler_->ReleaseDumps(); |
| 124 if (WebRtcRtpDumpHandler::TypeContainsIncoming(types[i])) |
| 125 EXPECT_FALSE(dumps.incoming_dump_path.empty()); |
| 126 |
| 127 if (WebRtcRtpDumpHandler::TypeContainsOutgoing(types[i])) |
| 128 EXPECT_FALSE(dumps.outgoing_dump_path.empty()); |
| 129 |
| 130 base::RunLoop().RunUntilIdle(); |
| 131 ResetDumpHandler(base::FilePath()); |
| 132 } |
| 133 } |
| 134 |
| 135 TEST_F(WebRtcRtpDumpHandlerTest, StoppedWhenMaxSizeReached) { |
| 136 std::string error; |
| 137 |
| 138 EXPECT_TRUE(handler_->StartDump( |
| 139 WebRtcRtpDumpHandler::PACKET_TYPE_INCOMING_ONLY, &error)); |
| 140 |
| 141 std::vector<uint8> buffer(100, 0); |
| 142 handler_->OnRtpPacket(buffer.data(), buffer.size(), buffer.size(), true); |
| 143 base::RunLoop().RunUntilIdle(); |
| 144 |
| 145 // Dumping should have been stopped, so ready to release. |
| 146 WebRtcRtpDumpHandler::ReleasedDumps dumps = handler_->ReleaseDumps(); |
| 147 EXPECT_FALSE(dumps.incoming_dump_path.empty()); |
| 148 } |
| 149 |
| 150 TEST_F(WebRtcRtpDumpHandlerTest, PacketIgnoredIfDumpingNotStarted) { |
| 151 std::vector<uint8> buffer(100, 0); |
| 152 handler_->OnRtpPacket(buffer.data(), buffer.size(), buffer.size(), true); |
| 153 handler_->OnRtpPacket(buffer.data(), buffer.size(), buffer.size(), false); |
| 154 base::RunLoop().RunUntilIdle(); |
| 155 } |
| 156 |
| 157 TEST_F(WebRtcRtpDumpHandlerTest, PacketIgnoredIfDumpingStopped) { |
| 158 std::string error; |
| 159 |
| 160 EXPECT_TRUE(handler_->StartDump( |
| 161 WebRtcRtpDumpHandler::PACKET_TYPE_INCOMING_ONLY, &error)); |
| 162 |
| 163 EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)); |
| 164 handler_->StopDump(WebRtcRtpDumpHandler::PACKET_TYPE_INCOMING_ONLY, |
| 165 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 166 base::Unretained(this))); |
| 167 |
| 168 std::vector<uint8> buffer(100, 0); |
| 169 handler_->OnRtpPacket(buffer.data(), buffer.size(), buffer.size(), true); |
| 170 base::RunLoop().RunUntilIdle(); |
| 171 } |
| 172 |
| 173 TEST_F(WebRtcRtpDumpHandlerTest, CannotStartMoreThanFiveDumps) { |
| 174 std::string error; |
| 175 |
| 176 handler_.reset(); |
| 177 |
| 178 scoped_ptr<WebRtcRtpDumpHandler> handlers[6]; |
| 179 |
| 180 for (size_t i = 0; i < arraysize(handlers); ++i) { |
| 181 handlers[i].reset(new WebRtcRtpDumpHandler(base::FilePath())); |
| 182 |
| 183 if (i < arraysize(handlers) - 1) { |
| 184 EXPECT_TRUE(handlers[i]->StartDump( |
| 185 WebRtcRtpDumpHandler::PACKET_TYPE_INCOMING_ONLY, &error)); |
| 186 } else { |
| 187 EXPECT_FALSE(handlers[i]->StartDump( |
| 188 WebRtcRtpDumpHandler::PACKET_TYPE_INCOMING_ONLY, &error)); |
| 189 } |
| 190 } |
| 191 } |
| 192 |
| 193 TEST_F(WebRtcRtpDumpHandlerTest, StartStopIncomingThenStartStopOutgoing) { |
| 194 std::string error; |
| 195 |
| 196 EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)).Times(2); |
| 197 |
| 198 EXPECT_TRUE(handler_->StartDump( |
| 199 WebRtcRtpDumpHandler::PACKET_TYPE_INCOMING_ONLY, &error)); |
| 200 handler_->StopDump(WebRtcRtpDumpHandler::PACKET_TYPE_INCOMING_ONLY, |
| 201 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 202 base::Unretained(this))); |
| 203 |
| 204 EXPECT_TRUE(handler_->StartDump( |
| 205 WebRtcRtpDumpHandler::PACKET_TYPE_OUTGOING_ONLY, &error)); |
| 206 handler_->StopDump(WebRtcRtpDumpHandler::PACKET_TYPE_OUTGOING_ONLY, |
| 207 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 208 base::Unretained(this))); |
| 209 |
| 210 base::RunLoop().RunUntilIdle(); |
| 211 } |
| 212 |
| 213 TEST_F(WebRtcRtpDumpHandlerTest, StartIncomingStartOutgoingThenStopBoth) { |
| 214 std::string error; |
| 215 |
| 216 EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)); |
| 217 |
| 218 EXPECT_TRUE(handler_->StartDump( |
| 219 WebRtcRtpDumpHandler::PACKET_TYPE_INCOMING_ONLY, &error)); |
| 220 EXPECT_TRUE(handler_->StartDump( |
| 221 WebRtcRtpDumpHandler::PACKET_TYPE_OUTGOING_ONLY, &error)); |
| 222 |
| 223 handler_->StopDump(WebRtcRtpDumpHandler::PACKET_TYPE_INCOMING_ONLY, |
| 224 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 225 base::Unretained(this))); |
| 226 |
| 227 base::RunLoop().RunUntilIdle(); |
| 228 } |
| 229 |
| 230 TEST_F(WebRtcRtpDumpHandlerTest, StartBothThenStopIncomingStopOutgoing) { |
| 231 std::string error; |
| 232 |
| 233 EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)).Times(2); |
| 234 |
| 235 EXPECT_TRUE( |
| 236 handler_->StartDump(WebRtcRtpDumpHandler::PACKET_TYPE_BOTH, &error)); |
| 237 |
| 238 handler_->StopDump(WebRtcRtpDumpHandler::PACKET_TYPE_INCOMING_ONLY, |
| 239 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 240 base::Unretained(this))); |
| 241 handler_->StopDump(WebRtcRtpDumpHandler::PACKET_TYPE_OUTGOING_ONLY, |
| 242 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 243 base::Unretained(this))); |
| 244 |
| 245 base::RunLoop().RunUntilIdle(); |
| 246 } |
| 247 |
| 248 TEST_F(WebRtcRtpDumpHandlerTest, DumpsCleanedUpIfNotReleased) { |
| 249 base::ScopedTempDir temp_dir; |
| 250 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 251 |
| 252 ResetDumpHandler(temp_dir.path()); |
| 253 |
| 254 base::FilePath incoming_dump = temp_dir.path().AppendASCII("recv"); |
| 255 base::FilePath outgoing_dump = temp_dir.path().AppendASCII("send"); |
| 256 const char dummy[] = "dummy"; |
| 257 EXPECT_GT(base::WriteFile(incoming_dump, dummy, arraysize(dummy)), 0); |
| 258 EXPECT_GT(base::WriteFile(outgoing_dump, dummy, arraysize(dummy)), 0); |
| 259 |
| 260 std::string error; |
| 261 EXPECT_TRUE( |
| 262 handler_->StartDump(WebRtcRtpDumpHandler::PACKET_TYPE_BOTH, &error)); |
| 263 |
| 264 EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)); |
| 265 handler_->StopDump(WebRtcRtpDumpHandler::PACKET_TYPE_BOTH, |
| 266 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 267 base::Unretained(this))); |
| 268 base::RunLoop().RunUntilIdle(); |
| 269 |
| 270 handler_.reset(); |
| 271 base::RunLoop().RunUntilIdle(); |
| 272 |
| 273 EXPECT_FALSE(base::PathExists(incoming_dump)); |
| 274 EXPECT_FALSE(base::PathExists(outgoing_dump)); |
| 275 } |
OLD | NEW |