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/big_endian.h" | |
6 #include "base/bind.h" | |
7 #include "base/file_util.h" | |
8 #include "base/files/scoped_temp_dir.h" | |
9 #include "base/macros.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/run_loop.h" | |
12 #include "chrome/browser/media/webrtc_rtp_dump_writer.h" | |
13 #include "content/public/browser/browser_thread.h" | |
14 #include "content/public/test/test_browser_thread_bundle.h" | |
15 #include "content/public/test/test_utils.h" | |
16 #include "testing/gmock/include/gmock/gmock.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 #include "third_party/zlib/zlib.h" | |
19 | |
20 static const size_t kMinimumRtpHeaderLength = 12; | |
21 | |
22 static void CreateFakeRtpPacketHeader(size_t csrc_count, | |
23 size_t extension_header_count, | |
24 std::vector<uint8>* packet_header) { | |
25 packet_header->resize(kMinimumRtpHeaderLength + csrc_count * sizeof(uint32) + | |
26 (extension_header_count + 1) * sizeof(uint32)); | |
27 | |
28 memset(&(*packet_header)[0], 0, packet_header->size()); | |
29 | |
30 // First byte format: vvpxcccc, where 'vv' is the version, 'p' is padding, 'x' | |
31 // is the extension bit, 'cccc' is the CSRC count. | |
32 (*packet_header)[0] = 0; | |
33 (*packet_header)[0] |= (0x2 << 6); // version. | |
34 // The extension bit. | |
35 (*packet_header)[0] |= (extension_header_count > 0 ? (0x1 << 4) : 0); | |
36 (*packet_header)[0] |= (csrc_count & 0xf); | |
37 | |
38 // Set extension length. | |
39 size_t offset = kMinimumRtpHeaderLength + | |
40 (csrc_count & 0xf) * sizeof(uint32) + sizeof(uint16); | |
41 base::WriteBigEndian(reinterpret_cast<char*>(&(*packet_header)[offset]), | |
42 static_cast<uint16>(extension_header_count)); | |
43 } | |
44 | |
45 class WebRtcRtpDumpWriterTest : public testing::Test { | |
46 public: | |
47 WebRtcRtpDumpWriterTest() | |
48 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP | | |
49 content::TestBrowserThreadBundle::REAL_FILE_THREAD), | |
50 temp_dir_(new base::ScopedTempDir()) {} | |
51 | |
52 virtual void SetUp() { | |
53 ASSERT_TRUE(temp_dir_->CreateUniqueTempDir()); | |
54 | |
55 incoming_dump_path_ = temp_dir_->path().AppendASCII("rtpdump_recv"); | |
56 outgoing_dump_path_ = temp_dir_->path().AppendASCII("rtpdump_send"); | |
57 writer_.reset(new WebRtcRtpDumpWriter( | |
58 incoming_dump_path_, | |
59 outgoing_dump_path_, | |
60 4 * 1024 * 1024, | |
61 base::Bind(&WebRtcRtpDumpWriterTest::OnMaxSizeReached, | |
62 base::Unretained(this)))); | |
63 } | |
64 | |
65 // Verifies that the dump contains records of |rtp_packet| repeated | |
66 // |packet_count| times. | |
67 void VerifyDumps(size_t incoming_packet_count, size_t outgoing_packet_count) { | |
68 std::string incoming_dump; | |
69 std::string outgoing_dump; | |
70 | |
71 if (incoming_packet_count) { | |
72 EXPECT_TRUE(base::ReadFileToString(incoming_dump_path_, &incoming_dump)); | |
73 EXPECT_TRUE(VerifyCompressedDump(&incoming_dump, incoming_packet_count)); | |
74 } else { | |
75 EXPECT_FALSE(base::PathExists(incoming_dump_path_)); | |
76 } | |
77 | |
78 if (outgoing_packet_count) { | |
79 EXPECT_TRUE(base::ReadFileToString(outgoing_dump_path_, &outgoing_dump)); | |
80 EXPECT_TRUE(VerifyCompressedDump(&outgoing_dump, outgoing_packet_count)); | |
81 } else { | |
82 EXPECT_FALSE(base::PathExists(outgoing_dump_path_)); | |
83 } | |
84 } | |
85 | |
86 MOCK_METHOD2(OnEndDumpDone, void(bool, bool)); | |
87 MOCK_METHOD0(OnMaxSizeReached, void(void)); | |
88 | |
89 protected: | |
90 // Verifies the compressed dump file contains the expected number of packets. | |
91 bool VerifyCompressedDump(std::string* dump, size_t expected_packet_count) { | |
92 EXPECT_GT(dump->size(), 0U); | |
93 | |
94 std::vector<uint8> decompressed_dump; | |
95 EXPECT_TRUE(Decompress(dump, &decompressed_dump)); | |
96 | |
97 size_t actual_packet_count = 0; | |
98 EXPECT_TRUE(ReadDecompressedDump(decompressed_dump, &actual_packet_count)); | |
99 EXPECT_EQ(expected_packet_count, actual_packet_count); | |
100 | |
101 return true; | |
102 } | |
103 | |
104 // Decompresses the |input| into |output|. | |
105 bool Decompress(std::string* input, std::vector<uint8>* output) { | |
106 z_stream stream = {0}; | |
107 | |
108 int result = inflateInit2(&stream, 15 + 16); | |
109 EXPECT_EQ(Z_OK, result); | |
110 | |
111 output->resize(input->size() * 100); | |
112 | |
113 stream.next_in = | |
114 reinterpret_cast<unsigned char*>(const_cast<char*>(&(*input)[0])); | |
115 stream.avail_in = input->size(); | |
116 stream.next_out = &(*output)[0]; | |
117 stream.avail_out = output->size(); | |
118 | |
119 result = inflate(&stream, Z_FINISH); | |
120 DCHECK_EQ(Z_STREAM_END, result); | |
121 result = inflateEnd(&stream); | |
122 DCHECK_EQ(Z_OK, result); | |
123 | |
124 output->resize(output->size() - stream.avail_out); | |
125 return true; | |
126 } | |
127 | |
128 // Tries to read |dump| as a rtpplay dump file and returns the number of | |
129 // packets found in the dump. | |
130 bool ReadDecompressedDump(const std::vector<uint8>& dump, | |
131 size_t* packet_count) { | |
132 static const char kFirstLine[] = "#!rtpplay1.0 0.0.0.0/0\n"; | |
133 static const size_t kDumpFileHeaderSize = 4 * sizeof(uint32); | |
134 | |
135 *packet_count = 0; | |
136 size_t dump_pos = 0; | |
137 | |
138 // Verifies the first line. | |
139 EXPECT_EQ(memcmp(&dump[0], kFirstLine, arraysize(kFirstLine) - 1), 0); | |
140 | |
141 dump_pos += arraysize(kFirstLine) - 1; | |
142 EXPECT_GT(dump.size(), dump_pos); | |
143 | |
144 // Skips the file header. | |
145 dump_pos += kDumpFileHeaderSize; | |
146 EXPECT_GT(dump.size(), dump_pos); | |
147 | |
148 // Reads each packet dump. | |
149 while (dump_pos < dump.size()) { | |
150 size_t packet_dump_length = 0; | |
151 if (!VerifyPacketDump(&dump[dump_pos], | |
152 dump.size() - dump_pos, | |
153 &packet_dump_length)) { | |
154 DVLOG(0) << "Failed to read the packet dump for packet " | |
155 << *packet_count << ", dump_pos = " << dump_pos | |
156 << ", dump_length = " << dump.size(); | |
157 return false; | |
158 } | |
159 | |
160 EXPECT_GE(dump.size(), dump_pos + packet_dump_length); | |
161 dump_pos += packet_dump_length; | |
162 | |
163 (*packet_count)++; | |
164 } | |
165 return true; | |
166 } | |
167 | |
168 // Tries to read one packet dump starting at |dump| and returns the size of | |
169 // the packet dump. | |
170 bool VerifyPacketDump(const uint8* dump, | |
171 size_t dump_length, | |
172 size_t* packet_dump_length) { | |
173 static const size_t kDumpHeaderLength = 8; | |
174 | |
175 size_t dump_pos = 0; | |
176 base::ReadBigEndian(reinterpret_cast<const char*>(dump + dump_pos), | |
177 reinterpret_cast<uint16*>(packet_dump_length)); | |
178 if (*packet_dump_length < kDumpHeaderLength + kMinimumRtpHeaderLength) | |
179 return false; | |
180 | |
181 EXPECT_GE(dump_length, *packet_dump_length); | |
182 dump_pos += sizeof(uint16); | |
183 | |
184 uint16 rtp_packet_length = 0; | |
185 base::ReadBigEndian(reinterpret_cast<const char*>(dump + dump_pos), | |
186 &rtp_packet_length); | |
187 if (rtp_packet_length < kMinimumRtpHeaderLength) | |
188 return false; | |
189 | |
190 dump_pos += sizeof(uint16); | |
191 | |
192 // Skips the elapsed time field. | |
193 dump_pos += sizeof(uint32); | |
194 | |
195 return IsValidRtpHeader(dump + dump_pos, | |
196 *packet_dump_length - kDumpHeaderLength); | |
197 } | |
198 | |
199 // Returns true if |header| is a valid RTP header. | |
200 bool IsValidRtpHeader(const uint8* header, size_t length) { | |
201 if ((header[0] & 0xC0) != 0x80) | |
202 return false; | |
203 | |
204 size_t cc_count = header[0] & 0x0F; | |
205 size_t header_length_without_extn = kMinimumRtpHeaderLength + 4 * cc_count; | |
206 | |
207 if (length < header_length_without_extn) | |
208 return false; | |
209 | |
210 uint16 extension_count = 0; | |
211 base::ReadBigEndian( | |
212 reinterpret_cast<const char*>(header + header_length_without_extn + 2), | |
213 &extension_count); | |
214 | |
215 if (length < (extension_count + 1) * 4 + header_length_without_extn) | |
216 return false; | |
217 | |
218 return true; | |
219 } | |
220 | |
221 content::TestBrowserThreadBundle thread_bundle_; | |
222 scoped_ptr<base::ScopedTempDir> temp_dir_; | |
223 base::FilePath incoming_dump_path_; | |
224 base::FilePath outgoing_dump_path_; | |
225 scoped_ptr<WebRtcRtpDumpWriter> writer_; | |
226 }; | |
227 | |
228 TEST_F(WebRtcRtpDumpWriterTest, NoDumpFileIfNoPacketDumped) { | |
229 // The scope is used to make sure the EXPECT_CALL is checked before exiting | |
230 // the scope. | |
231 { | |
232 EXPECT_CALL(*this, OnEndDumpDone(false, false)); | |
233 | |
234 writer_->EndDump(RTP_DUMP_BOTH, | |
235 base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone, | |
236 base::Unretained(this))); | |
237 | |
238 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | |
239 base::RunLoop().RunUntilIdle(); | |
240 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | |
241 base::RunLoop().RunUntilIdle(); | |
242 } | |
243 EXPECT_FALSE(base::PathExists(incoming_dump_path_)); | |
244 EXPECT_FALSE(base::PathExists(outgoing_dump_path_)); | |
245 } | |
246 | |
247 TEST_F(WebRtcRtpDumpWriterTest, WriteAndFlushSmallSizeDump) { | |
248 std::vector<uint8> packet_header; | |
249 CreateFakeRtpPacketHeader(1, 2, &packet_header); | |
250 | |
251 writer_->WriteRtpPacket( | |
252 &packet_header[0], packet_header.size(), 100, true); | |
253 writer_->WriteRtpPacket( | |
254 &packet_header[0], packet_header.size(), 100, false); | |
255 | |
256 // The scope is used to make sure the EXPECT_CALL is checked before exiting | |
257 // the scope. | |
258 { | |
259 EXPECT_CALL(*this, OnEndDumpDone(true, true)); | |
260 | |
261 writer_->EndDump(RTP_DUMP_BOTH, | |
262 base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone, | |
263 base::Unretained(this))); | |
264 | |
265 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | |
266 base::RunLoop().RunUntilIdle(); | |
267 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | |
268 base::RunLoop().RunUntilIdle(); | |
269 } | |
270 | |
271 VerifyDumps(1, 1); | |
272 } | |
273 | |
274 TEST_F(WebRtcRtpDumpWriterTest, WriteOverMaxLimit) { | |
275 // Reset the writer with a small max size limit. | |
276 writer_.reset(new WebRtcRtpDumpWriter( | |
277 incoming_dump_path_, | |
278 outgoing_dump_path_, | |
279 100, | |
280 base::Bind(&WebRtcRtpDumpWriterTest::OnMaxSizeReached, | |
281 base::Unretained(this)))); | |
282 | |
283 std::vector<uint8> packet_header; | |
284 CreateFakeRtpPacketHeader(3, 4, &packet_header); | |
285 | |
286 const size_t kPacketCount = 200; | |
287 // The scope is used to make sure the EXPECT_CALL is checked before exiting | |
288 // the scope. | |
289 { | |
290 EXPECT_CALL(*this, OnMaxSizeReached()).Times(testing::AtLeast(1)); | |
291 | |
292 // Write enough packets to overflow the in-memory buffer and max limit. | |
293 for (size_t i = 0; i < kPacketCount; ++i) { | |
294 writer_->WriteRtpPacket( | |
295 &packet_header[0], packet_header.size(), 100, true); | |
296 | |
297 writer_->WriteRtpPacket( | |
298 &packet_header[0], packet_header.size(), 100, false); | |
299 } | |
300 | |
301 EXPECT_CALL(*this, OnEndDumpDone(true, true)); | |
302 | |
303 writer_->EndDump(RTP_DUMP_BOTH, | |
304 base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone, | |
305 base::Unretained(this))); | |
306 | |
307 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | |
308 base::RunLoop().RunUntilIdle(); | |
309 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | |
310 base::RunLoop().RunUntilIdle(); | |
311 } | |
312 VerifyDumps(kPacketCount, kPacketCount); | |
313 } | |
314 | |
315 TEST_F(WebRtcRtpDumpWriterTest, DestroyWriterBeforeEndDumpCallback) { | |
316 EXPECT_CALL(*this, OnEndDumpDone(testing::_, testing::_)).Times(0); | |
317 | |
318 writer_->EndDump(RTP_DUMP_BOTH, | |
319 base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone, | |
320 base::Unretained(this))); | |
321 | |
322 writer_.reset(); | |
323 | |
324 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | |
325 base::RunLoop().RunUntilIdle(); | |
326 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | |
327 base::RunLoop().RunUntilIdle(); | |
328 } | |
329 | |
330 TEST_F(WebRtcRtpDumpWriterTest, EndDumpsSeparately) { | |
331 std::vector<uint8> packet_header; | |
332 CreateFakeRtpPacketHeader(1, 2, &packet_header); | |
333 | |
334 writer_->WriteRtpPacket( | |
335 &packet_header[0], packet_header.size(), 100, true); | |
336 writer_->WriteRtpPacket( | |
337 &packet_header[0], packet_header.size(), 100, true); | |
338 writer_->WriteRtpPacket( | |
339 &packet_header[0], packet_header.size(), 100, false); | |
340 | |
341 // The scope is used to make sure the EXPECT_CALL is checked before exiting | |
342 // the scope. | |
343 { | |
344 EXPECT_CALL(*this, OnEndDumpDone(true, false)); | |
345 EXPECT_CALL(*this, OnEndDumpDone(false, true)); | |
346 | |
347 writer_->EndDump(RTP_DUMP_INCOMING, | |
348 base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone, | |
349 base::Unretained(this))); | |
350 | |
351 writer_->EndDump(RTP_DUMP_OUTGOING, | |
352 base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone, | |
353 base::Unretained(this))); | |
354 | |
355 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | |
356 base::RunLoop().RunUntilIdle(); | |
357 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | |
358 base::RunLoop().RunUntilIdle(); | |
359 } | |
360 | |
361 VerifyDumps(2, 1); | |
362 } | |
OLD | NEW |