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 type_none(false, false); |
| 81 EXPECT_FALSE(handler_->StartDump(type_none, &error)); |
| 82 |
| 83 WebRtcRtpDumpHandler::PacketType types[3]; |
| 84 types[0] = WebRtcRtpDumpHandler::PacketType(true, false); |
| 85 types[1] = WebRtcRtpDumpHandler::PacketType(false, true); |
| 86 types[2] = WebRtcRtpDumpHandler::PacketType(true, true); |
| 87 |
| 88 WebRtcRtpDumpHandler::ReleasedDumps dumps; |
| 89 for (size_t i = 0; i < arraysize(types); ++i) { |
| 90 DVLOG(2) << "Verifying state transition: incoming = " << types[i].incoming |
| 91 << ", outgoing = " << types[i].outgoing; |
| 92 |
| 93 // Only StartDump is allowed in STATE_NONE. |
| 94 EXPECT_CALL(*this, OnStopDumpFinished(false, testing::_)); |
| 95 handler_->StopDump(types[i], |
| 96 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 97 base::Unretained(this))); |
| 98 |
| 99 dumps = handler_->ReleaseDumps(); |
| 100 EXPECT_TRUE(dumps.incoming_dump_path.empty()); |
| 101 EXPECT_TRUE(dumps.outgoing_dump_path.empty()); |
| 102 |
| 103 EXPECT_TRUE(handler_->StartDump(types[i], &error)); |
| 104 base::RunLoop().RunUntilIdle(); |
| 105 |
| 106 // Only StopDump is allowed in STATE_STARTED. |
| 107 EXPECT_FALSE(handler_->StartDump(types[i], &error)); |
| 108 |
| 109 dumps = handler_->ReleaseDumps(); |
| 110 EXPECT_TRUE(dumps.incoming_dump_path.empty()); |
| 111 EXPECT_TRUE(dumps.outgoing_dump_path.empty()); |
| 112 |
| 113 EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)); |
| 114 handler_->StopDump(types[i], |
| 115 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 116 base::Unretained(this))); |
| 117 base::RunLoop().RunUntilIdle(); |
| 118 |
| 119 // Only ReleaseDump is allowed in STATE_STOPPED. |
| 120 EXPECT_FALSE(handler_->StartDump(types[i], &error)); |
| 121 |
| 122 EXPECT_CALL(*this, OnStopDumpFinished(false, testing::_)); |
| 123 handler_->StopDump(types[i], |
| 124 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 125 base::Unretained(this))); |
| 126 |
| 127 dumps = handler_->ReleaseDumps(); |
| 128 if (types[i].incoming) |
| 129 EXPECT_FALSE(dumps.incoming_dump_path.empty()); |
| 130 |
| 131 if (types[i].outgoing) |
| 132 EXPECT_FALSE(dumps.outgoing_dump_path.empty()); |
| 133 |
| 134 base::RunLoop().RunUntilIdle(); |
| 135 ResetDumpHandler(base::FilePath()); |
| 136 } |
| 137 } |
| 138 |
| 139 TEST_F(WebRtcRtpDumpHandlerTest, StoppedWhenMaxSizeReached) { |
| 140 std::string error; |
| 141 |
| 142 WebRtcRtpDumpHandler::PacketType type(true, false); |
| 143 EXPECT_TRUE(handler_->StartDump(type, &error)); |
| 144 |
| 145 std::vector<uint8> buffer(100, 0); |
| 146 handler_->OnRtpPacket(buffer.data(), buffer.size(), buffer.size(), true); |
| 147 base::RunLoop().RunUntilIdle(); |
| 148 |
| 149 // Dumping should have been stopped, so ready to release. |
| 150 WebRtcRtpDumpHandler::ReleasedDumps dumps = handler_->ReleaseDumps(); |
| 151 EXPECT_FALSE(dumps.incoming_dump_path.empty()); |
| 152 } |
| 153 |
| 154 TEST_F(WebRtcRtpDumpHandlerTest, PacketIgnoredIfDumpingNotStarted) { |
| 155 std::vector<uint8> buffer(100, 0); |
| 156 handler_->OnRtpPacket(buffer.data(), buffer.size(), buffer.size(), true); |
| 157 handler_->OnRtpPacket(buffer.data(), buffer.size(), buffer.size(), false); |
| 158 base::RunLoop().RunUntilIdle(); |
| 159 } |
| 160 |
| 161 TEST_F(WebRtcRtpDumpHandlerTest, PacketIgnoredIfDumpingStopped) { |
| 162 std::string error; |
| 163 |
| 164 WebRtcRtpDumpHandler::PacketType type(true, false); |
| 165 EXPECT_TRUE(handler_->StartDump(type, &error)); |
| 166 |
| 167 EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)); |
| 168 handler_->StopDump(type, |
| 169 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 170 base::Unretained(this))); |
| 171 |
| 172 std::vector<uint8> buffer(100, 0); |
| 173 handler_->OnRtpPacket(buffer.data(), buffer.size(), buffer.size(), true); |
| 174 base::RunLoop().RunUntilIdle(); |
| 175 } |
| 176 |
| 177 TEST_F(WebRtcRtpDumpHandlerTest, CannotStartMoreThanFiveDumps) { |
| 178 std::string error; |
| 179 |
| 180 handler_.reset(); |
| 181 |
| 182 scoped_ptr<WebRtcRtpDumpHandler> handlers[6]; |
| 183 |
| 184 for (size_t i = 0; i < arraysize(handlers); ++i) { |
| 185 handlers[i].reset(new WebRtcRtpDumpHandler(base::FilePath())); |
| 186 |
| 187 WebRtcRtpDumpHandler::PacketType type(true, false); |
| 188 if (i < arraysize(handlers) - 1) |
| 189 EXPECT_TRUE(handlers[i]->StartDump(type, &error)); |
| 190 else |
| 191 EXPECT_FALSE(handlers[i]->StartDump(type, &error)); |
| 192 } |
| 193 } |
| 194 |
| 195 TEST_F(WebRtcRtpDumpHandlerTest, StartStopIncomingThenStartStopOutgoing) { |
| 196 std::string error; |
| 197 |
| 198 EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)).Times(2); |
| 199 |
| 200 WebRtcRtpDumpHandler::PacketType type(true, false); |
| 201 EXPECT_TRUE(handler_->StartDump(type, &error)); |
| 202 handler_->StopDump(type, |
| 203 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 204 base::Unretained(this))); |
| 205 |
| 206 type = WebRtcRtpDumpHandler::PacketType(false, true); |
| 207 EXPECT_TRUE(handler_->StartDump(type, &error)); |
| 208 handler_->StopDump(type, |
| 209 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 210 base::Unretained(this))); |
| 211 |
| 212 base::RunLoop().RunUntilIdle(); |
| 213 } |
| 214 |
| 215 TEST_F(WebRtcRtpDumpHandlerTest, StartIncomingStartOutgoingThenStopBoth) { |
| 216 std::string error; |
| 217 |
| 218 EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)); |
| 219 |
| 220 EXPECT_TRUE(handler_->StartDump(WebRtcRtpDumpHandler::PacketType(true, false), |
| 221 &error)); |
| 222 EXPECT_TRUE(handler_->StartDump(WebRtcRtpDumpHandler::PacketType(false, true), |
| 223 &error)); |
| 224 |
| 225 handler_->StopDump(WebRtcRtpDumpHandler::PacketType(true, true), |
| 226 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 227 base::Unretained(this))); |
| 228 |
| 229 base::RunLoop().RunUntilIdle(); |
| 230 } |
| 231 |
| 232 TEST_F(WebRtcRtpDumpHandlerTest, StartBothThenStopIncomingStopOutgoing) { |
| 233 std::string error; |
| 234 |
| 235 EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)).Times(2); |
| 236 |
| 237 EXPECT_TRUE(handler_->StartDump(WebRtcRtpDumpHandler::PacketType(true, true), |
| 238 &error)); |
| 239 |
| 240 handler_->StopDump(WebRtcRtpDumpHandler::PacketType(true, false), |
| 241 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 242 base::Unretained(this))); |
| 243 handler_->StopDump(WebRtcRtpDumpHandler::PacketType(false, true), |
| 244 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 245 base::Unretained(this))); |
| 246 |
| 247 base::RunLoop().RunUntilIdle(); |
| 248 } |
| 249 |
| 250 TEST_F(WebRtcRtpDumpHandlerTest, DumpsCleanedUpIfNotReleased) { |
| 251 base::ScopedTempDir temp_dir; |
| 252 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 253 |
| 254 ResetDumpHandler(temp_dir.path()); |
| 255 |
| 256 base::FilePath incoming_dump = temp_dir.path().AppendASCII("recv"); |
| 257 base::FilePath outgoing_dump = temp_dir.path().AppendASCII("send"); |
| 258 const char dummy[] = "dummy"; |
| 259 EXPECT_GT(base::WriteFile(incoming_dump, dummy, arraysize(dummy)), 0); |
| 260 EXPECT_GT(base::WriteFile(outgoing_dump, dummy, arraysize(dummy)), 0); |
| 261 |
| 262 WebRtcRtpDumpHandler::PacketType type(true, true); |
| 263 std::string error; |
| 264 EXPECT_TRUE(handler_->StartDump(type, &error)); |
| 265 |
| 266 EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)); |
| 267 handler_->StopDump(type, |
| 268 base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished, |
| 269 base::Unretained(this))); |
| 270 base::RunLoop().RunUntilIdle(); |
| 271 |
| 272 handler_.reset(); |
| 273 base::RunLoop().RunUntilIdle(); |
| 274 |
| 275 EXPECT_FALSE(base::PathExists(incoming_dump)); |
| 276 EXPECT_FALSE(base::PathExists(outgoing_dump)); |
| 277 } |
OLD | NEW |