Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(636)

Side by Side Diff: chrome/browser/media/webrtc_rtp_dump_handler_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698