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 bool end_dump_success) |
| 22 : WebRtcRtpDumpWriter(base::FilePath(), |
| 23 base::FilePath(), |
| 24 max_dump_size, |
| 25 base::Closure()), |
| 26 max_dump_size_(max_dump_size), |
| 27 current_dump_size_(0), |
| 28 max_size_reached_callback_(max_size_reached_callback), |
| 29 end_dump_success_(end_dump_success) {} |
| 30 |
| 31 virtual void WriteRtpPacket(const uint8* packet_header, |
| 32 size_t header_length, |
| 33 size_t packet_length, |
| 34 bool incoming) OVERRIDE { |
| 35 current_dump_size_ += header_length; |
| 36 if (current_dump_size_ > max_dump_size_) |
| 37 max_size_reached_callback_.Run(); |
| 38 } |
| 39 |
| 40 virtual void EndDump(RtpDumpType type, |
| 41 const EndDumpCallback& finished_callback) OVERRIDE { |
| 42 bool incoming_sucess = end_dump_success_; |
| 43 bool outgoing_success = end_dump_success_; |
| 44 |
| 45 if (type == RTP_DUMP_INCOMING) |
| 46 outgoing_success = false; |
| 47 else if (type == RTP_DUMP_OUTGOING) |
| 48 incoming_sucess = false; |
| 49 |
| 50 base::MessageLoop::current()->PostTask( |
| 51 FROM_HERE, |
| 52 base::Bind(finished_callback, incoming_sucess, outgoing_success)); |
| 53 } |
| 54 |
| 55 private: |
| 56 size_t max_dump_size_; |
| 57 size_t current_dump_size_; |
| 58 base::Closure max_size_reached_callback_; |
| 59 bool end_dump_success_; |
| 60 }; |
| 61 |
| 62 class WebRtcRtpDumpHandlerTest : public testing::Test { |
| 63 public: |
| 64 WebRtcRtpDumpHandlerTest() |
| 65 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
| 66 ResetDumpHandler(base::FilePath(), true); |
| 67 } |
| 68 |
| 69 void ResetDumpHandler(const base::FilePath& dir, bool end_dump_success) { |
| 70 handler_.reset(new WebRtcRtpDumpHandler( |
| 71 dir.empty() ? base::FilePath(FILE_PATH_LITERAL("dummy")) : dir)); |
| 72 |
| 73 scoped_ptr<WebRtcRtpDumpWriter> writer(new FakeDumpWriter( |
| 74 10, |
| 75 base::Bind(&WebRtcRtpDumpHandler::OnMaxDumpSizeReached, |
| 76 base::Unretained(handler_.get())), |
| 77 end_dump_success)); |
| 78 |
| 79 handler_->SetDumpWriterForTesting(writer.Pass()); |
| 80 } |
| 81 |
| 82 void WriteFakeDumpFiles(const base::FilePath& dir, |
| 83 base::FilePath* incoming_dump, |
| 84 base::FilePath* outgoing_dump) { |
| 85 *incoming_dump = dir.AppendASCII("recv"); |
| 86 *outgoing_dump = dir.AppendASCII("send"); |
| 87 const char dummy[] = "dummy"; |
| 88 EXPECT_GT(base::WriteFile(*incoming_dump, dummy, arraysize(dummy)), 0); |
| 89 EXPECT_GT(base::WriteFile(*outgoing_dump, dummy, arraysize(dummy)), 0); |
| 90 } |
| 91 |
| 92 MOCK_METHOD2(OnStopDumpFinished, |
| 93 void(bool success, const std::string& error)); |
| 94 |
| 95 protected: |
| 96 content::TestBrowserThreadBundle thread_bundle_; |
| 97 scoped_ptr<WebRtcRtpDumpHandler> handler_; |
| 98 }; |
| 99 |
| 100 TEST_F(WebRtcRtpDumpHandlerTest, StateTransition) { |
| 101 std::string error; |
| 102 |
| 103 RtpDumpType types[3]; |
| 104 types[0] = RTP_DUMP_INCOMING; |
| 105 types[1] = RTP_DUMP_OUTGOING; |
| 106 types[2] = RTP_DUMP_BOTH; |
| 107 |
| 108 for (size_t i = 0; i < arraysize(types); ++i) { |
| 109 DVLOG(2) << "Verifying state transition: type = " << types[i]; |
| 110 |
| 111 // Only StartDump is allowed in STATE_NONE. |
| 112 EXPECT_CALL(*this, OnStopDumpFinished(false, testing::_)); |
| 113 handler_->StopDump(types[i], |
| 114 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 115 base::Unretained(this))); |
| 116 |
| 117 WebRtcRtpDumpHandler::ReleasedDumps dumps_0(handler_->ReleaseDumps()); |
| 118 EXPECT_TRUE(dumps_0.incoming_dump_path.empty()); |
| 119 EXPECT_TRUE(dumps_0.outgoing_dump_path.empty()); |
| 120 |
| 121 EXPECT_TRUE(handler_->StartDump(types[i], &error)); |
| 122 base::RunLoop().RunUntilIdle(); |
| 123 |
| 124 // Only StopDump is allowed in STATE_STARTED. |
| 125 EXPECT_FALSE(handler_->StartDump(types[i], &error)); |
| 126 |
| 127 WebRtcRtpDumpHandler::ReleasedDumps dumps_1(handler_->ReleaseDumps()); |
| 128 EXPECT_TRUE(dumps_1.incoming_dump_path.empty()); |
| 129 EXPECT_TRUE(dumps_1.outgoing_dump_path.empty()); |
| 130 |
| 131 EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)); |
| 132 handler_->StopDump(types[i], |
| 133 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 134 base::Unretained(this))); |
| 135 base::RunLoop().RunUntilIdle(); |
| 136 |
| 137 // Only ReleaseDump is allowed in STATE_STOPPED. |
| 138 EXPECT_FALSE(handler_->StartDump(types[i], &error)); |
| 139 |
| 140 EXPECT_CALL(*this, OnStopDumpFinished(false, testing::_)); |
| 141 handler_->StopDump(types[i], |
| 142 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 143 base::Unretained(this))); |
| 144 |
| 145 WebRtcRtpDumpHandler::ReleasedDumps dumps_2(handler_->ReleaseDumps()); |
| 146 if (types[i] == RTP_DUMP_INCOMING || types[i] == RTP_DUMP_BOTH) |
| 147 EXPECT_FALSE(dumps_2.incoming_dump_path.empty()); |
| 148 |
| 149 if (types[i] == RTP_DUMP_OUTGOING || types[i] == RTP_DUMP_BOTH) |
| 150 EXPECT_FALSE(dumps_2.outgoing_dump_path.empty()); |
| 151 |
| 152 base::RunLoop().RunUntilIdle(); |
| 153 ResetDumpHandler(base::FilePath(), true); |
| 154 } |
| 155 } |
| 156 |
| 157 TEST_F(WebRtcRtpDumpHandlerTest, StoppedWhenMaxSizeReached) { |
| 158 std::string error; |
| 159 |
| 160 EXPECT_TRUE(handler_->StartDump(RTP_DUMP_INCOMING, &error)); |
| 161 |
| 162 std::vector<uint8> buffer(100, 0); |
| 163 handler_->OnRtpPacket(buffer.data(), buffer.size(), buffer.size(), true); |
| 164 base::RunLoop().RunUntilIdle(); |
| 165 |
| 166 // Dumping should have been stopped, so ready to release. |
| 167 WebRtcRtpDumpHandler::ReleasedDumps dumps = handler_->ReleaseDumps(); |
| 168 EXPECT_FALSE(dumps.incoming_dump_path.empty()); |
| 169 } |
| 170 |
| 171 TEST_F(WebRtcRtpDumpHandlerTest, PacketIgnoredIfDumpingNotStarted) { |
| 172 std::vector<uint8> buffer(100, 0); |
| 173 handler_->OnRtpPacket(buffer.data(), buffer.size(), buffer.size(), true); |
| 174 handler_->OnRtpPacket(buffer.data(), buffer.size(), buffer.size(), false); |
| 175 base::RunLoop().RunUntilIdle(); |
| 176 } |
| 177 |
| 178 TEST_F(WebRtcRtpDumpHandlerTest, PacketIgnoredIfDumpingStopped) { |
| 179 std::string error; |
| 180 |
| 181 EXPECT_TRUE(handler_->StartDump(RTP_DUMP_INCOMING, &error)); |
| 182 |
| 183 EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)); |
| 184 handler_->StopDump(RTP_DUMP_INCOMING, |
| 185 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 186 base::Unretained(this))); |
| 187 |
| 188 std::vector<uint8> buffer(100, 0); |
| 189 handler_->OnRtpPacket(buffer.data(), buffer.size(), buffer.size(), true); |
| 190 base::RunLoop().RunUntilIdle(); |
| 191 } |
| 192 |
| 193 TEST_F(WebRtcRtpDumpHandlerTest, CannotStartMoreThanFiveDumps) { |
| 194 std::string error; |
| 195 |
| 196 handler_.reset(); |
| 197 |
| 198 scoped_ptr<WebRtcRtpDumpHandler> handlers[6]; |
| 199 |
| 200 for (size_t i = 0; i < arraysize(handlers); ++i) { |
| 201 handlers[i].reset(new WebRtcRtpDumpHandler(base::FilePath())); |
| 202 |
| 203 if (i < arraysize(handlers) - 1) { |
| 204 EXPECT_TRUE(handlers[i]->StartDump(RTP_DUMP_INCOMING, &error)); |
| 205 } else { |
| 206 EXPECT_FALSE(handlers[i]->StartDump(RTP_DUMP_INCOMING, &error)); |
| 207 } |
| 208 } |
| 209 } |
| 210 |
| 211 TEST_F(WebRtcRtpDumpHandlerTest, StartStopIncomingThenStartStopOutgoing) { |
| 212 std::string error; |
| 213 |
| 214 EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)).Times(2); |
| 215 |
| 216 EXPECT_TRUE(handler_->StartDump(RTP_DUMP_INCOMING, &error)); |
| 217 handler_->StopDump(RTP_DUMP_INCOMING, |
| 218 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 219 base::Unretained(this))); |
| 220 |
| 221 EXPECT_TRUE(handler_->StartDump(RTP_DUMP_OUTGOING, &error)); |
| 222 handler_->StopDump(RTP_DUMP_OUTGOING, |
| 223 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 224 base::Unretained(this))); |
| 225 |
| 226 base::RunLoop().RunUntilIdle(); |
| 227 } |
| 228 |
| 229 TEST_F(WebRtcRtpDumpHandlerTest, StartIncomingStartOutgoingThenStopBoth) { |
| 230 std::string error; |
| 231 |
| 232 EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)); |
| 233 |
| 234 EXPECT_TRUE(handler_->StartDump(RTP_DUMP_INCOMING, &error)); |
| 235 EXPECT_TRUE(handler_->StartDump(RTP_DUMP_OUTGOING, &error)); |
| 236 |
| 237 handler_->StopDump(RTP_DUMP_INCOMING, |
| 238 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 239 base::Unretained(this))); |
| 240 |
| 241 base::RunLoop().RunUntilIdle(); |
| 242 } |
| 243 |
| 244 TEST_F(WebRtcRtpDumpHandlerTest, StartBothThenStopIncomingStopOutgoing) { |
| 245 std::string error; |
| 246 |
| 247 EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)).Times(2); |
| 248 |
| 249 EXPECT_TRUE(handler_->StartDump(RTP_DUMP_BOTH, &error)); |
| 250 |
| 251 handler_->StopDump(RTP_DUMP_INCOMING, |
| 252 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 253 base::Unretained(this))); |
| 254 handler_->StopDump(RTP_DUMP_OUTGOING, |
| 255 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 256 base::Unretained(this))); |
| 257 |
| 258 base::RunLoop().RunUntilIdle(); |
| 259 } |
| 260 |
| 261 TEST_F(WebRtcRtpDumpHandlerTest, DumpsCleanedUpIfNotReleased) { |
| 262 base::ScopedTempDir temp_dir; |
| 263 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 264 ResetDumpHandler(temp_dir.path(), true); |
| 265 |
| 266 base::FilePath incoming_dump, outgoing_dump; |
| 267 WriteFakeDumpFiles(temp_dir.path(), &incoming_dump, &outgoing_dump); |
| 268 |
| 269 std::string error; |
| 270 EXPECT_TRUE(handler_->StartDump(RTP_DUMP_BOTH, &error)); |
| 271 |
| 272 EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)); |
| 273 handler_->StopDump(RTP_DUMP_BOTH, |
| 274 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 275 base::Unretained(this))); |
| 276 base::RunLoop().RunUntilIdle(); |
| 277 |
| 278 handler_.reset(); |
| 279 base::RunLoop().RunUntilIdle(); |
| 280 |
| 281 EXPECT_FALSE(base::PathExists(incoming_dump)); |
| 282 EXPECT_FALSE(base::PathExists(outgoing_dump)); |
| 283 } |
| 284 |
| 285 TEST_F(WebRtcRtpDumpHandlerTest, DumpDeletedIfEndDumpFailed) { |
| 286 base::ScopedTempDir temp_dir; |
| 287 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 288 |
| 289 // Make the writer return failure on EndStream. |
| 290 ResetDumpHandler(temp_dir.path(), false); |
| 291 |
| 292 base::FilePath incoming_dump, outgoing_dump; |
| 293 WriteFakeDumpFiles(temp_dir.path(), &incoming_dump, &outgoing_dump); |
| 294 |
| 295 std::string error; |
| 296 EXPECT_TRUE(handler_->StartDump(RTP_DUMP_BOTH, &error)); |
| 297 EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)).Times(2); |
| 298 |
| 299 handler_->StopDump(RTP_DUMP_INCOMING, |
| 300 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 301 base::Unretained(this))); |
| 302 base::RunLoop().RunUntilIdle(); |
| 303 |
| 304 EXPECT_FALSE(base::PathExists(incoming_dump)); |
| 305 EXPECT_TRUE(base::PathExists(outgoing_dump)); |
| 306 |
| 307 handler_->StopDump(RTP_DUMP_OUTGOING, |
| 308 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 309 base::Unretained(this))); |
| 310 base::RunLoop().RunUntilIdle(); |
| 311 EXPECT_FALSE(base::PathExists(outgoing_dump)); |
| 312 } |
OLD | NEW |