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