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 // First byte format: vvpxcccc, where 'vv' is the version, 'p' is padding, 'x' |
| 29 // is the extension bit, 'cccc' is the CSRC count. |
| 30 (*packet_header)[0] = 0; |
| 31 (*packet_header)[0] |= (0x2 << 6); // version. |
| 32 // The extension bit. |
| 33 (*packet_header)[0] |= (extension_header_count > 0 ? (0x1 << 4) : 0); |
| 34 (*packet_header)[0] |= (csrc_count & 0xf); |
| 35 |
| 36 // Set extension length. |
| 37 size_t offset = |
| 38 kMinimumRtpHeaderLength + (csrc_count & 0xf) * sizeof(uint32) + 2; |
| 39 base::WriteBigEndian(reinterpret_cast<char*>(packet_header + offset), |
| 40 static_cast<uint16>(extension_header_count)); |
| 41 } |
| 42 |
| 43 class WebRtcRtpDumpWriterTest : public testing::Test { |
| 44 public: |
| 45 WebRtcRtpDumpWriterTest() |
| 46 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP | |
| 47 content::TestBrowserThreadBundle::REAL_FILE_THREAD), |
| 48 temp_dir_(new base::ScopedTempDir()) {} |
| 49 |
| 50 virtual void SetUp() { |
| 51 ASSERT_TRUE(temp_dir_->CreateUniqueTempDir()); |
| 52 |
| 53 incoming_dump_path_ = temp_dir_->path().AppendASCII("rtpdump_recv"); |
| 54 outgoing_dump_path_ = temp_dir_->path().AppendASCII("rtpdump_send"); |
| 55 writer_.reset(new WebRtcRtpDumpWriter( |
| 56 incoming_dump_path_, |
| 57 outgoing_dump_path_, |
| 58 4 * 1024 * 1024, |
| 59 base::Bind(&WebRtcRtpDumpWriterTest::OnMaxSizeReached, |
| 60 base::Unretained(this)))); |
| 61 } |
| 62 |
| 63 // Verifies that the dump contains records of |rtp_packet| repeated |
| 64 // |packet_count| times. |
| 65 bool VerifyDumps(size_t incoming_packet_count, size_t outgoing_packet_count) { |
| 66 std::string incoming_dump; |
| 67 std::string outgoing_dump; |
| 68 |
| 69 if (incoming_packet_count) { |
| 70 EXPECT_TRUE(base::ReadFileToString(incoming_dump_path_, &incoming_dump)); |
| 71 EXPECT_TRUE(VerifyCompressedDump(&incoming_dump, incoming_packet_count)); |
| 72 } else { |
| 73 EXPECT_FALSE(base::PathExists(incoming_dump_path_)); |
| 74 } |
| 75 |
| 76 if (outgoing_packet_count) { |
| 77 EXPECT_TRUE(base::ReadFileToString(outgoing_dump_path_, &outgoing_dump)); |
| 78 EXPECT_TRUE(VerifyCompressedDump(&outgoing_dump, outgoing_packet_count)); |
| 79 } else { |
| 80 EXPECT_FALSE(base::PathExists(outgoing_dump_path_)); |
| 81 } |
| 82 |
| 83 return true; |
| 84 } |
| 85 |
| 86 MOCK_METHOD1(OnEndDumpDone, void(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->data())); |
| 115 stream.avail_in = input->size(); |
| 116 stream.next_out = output->data(); |
| 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.data(), 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.data() + 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 += 2; |
| 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 += 2; |
| 191 |
| 192 // Skips the elapsed time field. |
| 193 dump_pos += 4; |
| 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 EXPECT_CALL(*this, OnEndDumpDone(false)).Times(2); |
| 230 |
| 231 writer_->EndDump(true, |
| 232 base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone, |
| 233 base::Unretained(this))); |
| 234 |
| 235 writer_->EndDump(false, |
| 236 base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone, |
| 237 base::Unretained(this))); |
| 238 |
| 239 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); |
| 240 base::RunLoop().RunUntilIdle(); |
| 241 |
| 242 EXPECT_FALSE(base::PathExists(incoming_dump_path_)); |
| 243 EXPECT_FALSE(base::PathExists(outgoing_dump_path_)); |
| 244 } |
| 245 |
| 246 TEST_F(WebRtcRtpDumpWriterTest, WriteAndFlushSmallSizeDump) { |
| 247 std::vector<uint8> packet_header; |
| 248 CreateFakeRtpPacketHeader(1, 2, &packet_header); |
| 249 |
| 250 writer_->WriteRtpPacket( |
| 251 packet_header.data(), packet_header.size(), 100, true); |
| 252 |
| 253 writer_->WriteRtpPacket( |
| 254 packet_header.data(), packet_header.size(), 100, false); |
| 255 |
| 256 EXPECT_CALL(*this, OnEndDumpDone(true)).Times(2); |
| 257 |
| 258 writer_->EndDump(true, |
| 259 base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone, |
| 260 base::Unretained(this))); |
| 261 |
| 262 writer_->EndDump(false, |
| 263 base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone, |
| 264 base::Unretained(this))); |
| 265 |
| 266 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); |
| 267 base::RunLoop().RunUntilIdle(); |
| 268 |
| 269 EXPECT_TRUE(VerifyDumps(1, 1)); |
| 270 } |
| 271 |
| 272 TEST_F(WebRtcRtpDumpWriterTest, WriteOverMaxLimit) { |
| 273 // Reset the writer with a small max size limit. |
| 274 writer_.reset(new WebRtcRtpDumpWriter( |
| 275 incoming_dump_path_, |
| 276 outgoing_dump_path_, |
| 277 100, |
| 278 base::Bind(&WebRtcRtpDumpWriterTest::OnMaxSizeReached, |
| 279 base::Unretained(this)))); |
| 280 |
| 281 std::vector<uint8> packet_header; |
| 282 CreateFakeRtpPacketHeader(3, 4, &packet_header); |
| 283 |
| 284 EXPECT_CALL(*this, OnMaxSizeReached()).Times(testing::AtLeast(1)); |
| 285 |
| 286 // Write enough packets to overflow the in-memory buffer and max limit. |
| 287 const size_t kPacketCount = 200; |
| 288 for (size_t i = 0; i < kPacketCount; ++i) { |
| 289 writer_->WriteRtpPacket( |
| 290 packet_header.data(), packet_header.size(), 100, true); |
| 291 |
| 292 writer_->WriteRtpPacket( |
| 293 packet_header.data(), packet_header.size(), 100, false); |
| 294 } |
| 295 |
| 296 EXPECT_CALL(*this, OnEndDumpDone(true)).Times(2); |
| 297 |
| 298 writer_->EndDump(true, |
| 299 base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone, |
| 300 base::Unretained(this))); |
| 301 |
| 302 writer_->EndDump(false, |
| 303 base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone, |
| 304 base::Unretained(this))); |
| 305 |
| 306 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); |
| 307 base::RunLoop().RunUntilIdle(); |
| 308 |
| 309 EXPECT_TRUE(VerifyDumps(kPacketCount, kPacketCount)); |
| 310 } |
| 311 |
| 312 TEST_F(WebRtcRtpDumpWriterTest, DestroyWriterBeforeEndDumpCallback) { |
| 313 EXPECT_CALL(*this, OnEndDumpDone(true)).Times(0); |
| 314 |
| 315 writer_->EndDump(true, |
| 316 base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone, |
| 317 base::Unretained(this))); |
| 318 |
| 319 writer_->EndDump(false, |
| 320 base::Bind(&WebRtcRtpDumpWriterTest::OnEndDumpDone, |
| 321 base::Unretained(this))); |
| 322 |
| 323 writer_.reset(); |
| 324 |
| 325 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); |
| 326 base::RunLoop().RunUntilIdle(); |
| 327 } |
OLD | NEW |