Home | History | Annotate | Download | only in test
      1 /*
      2  *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #include <string.h>
     12 
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 #include "webrtc/base/scoped_ptr.h"
     15 #include "webrtc/test/rtp_file_reader.h"
     16 #include "webrtc/test/rtp_file_writer.h"
     17 #include "webrtc/test/testsupport/fileutils.h"
     18 
     19 namespace webrtc {
     20 
     21 class RtpFileWriterTest : public ::testing::Test {
     22  public:
     23   void Init(const std::string& filename) {
     24     filename_ = test::OutputPath() + filename;
     25     rtp_writer_.reset(
     26         test::RtpFileWriter::Create(test::RtpFileWriter::kRtpDump, filename_));
     27   }
     28 
     29   void WriteRtpPackets(int num_packets) {
     30     ASSERT_TRUE(rtp_writer_.get() != NULL);
     31     test::RtpPacket packet;
     32     for (int i = 1; i <= num_packets; ++i) {
     33       packet.length = i;
     34       packet.original_length = i;
     35       packet.time_ms = i;
     36       memset(packet.data, i, packet.length);
     37       EXPECT_TRUE(rtp_writer_->WritePacket(&packet));
     38     }
     39   }
     40 
     41   void CloseOutputFile() { rtp_writer_.reset(); }
     42 
     43   void VerifyFileContents(int expected_packets) {
     44     ASSERT_TRUE(rtp_writer_.get() == NULL)
     45         << "Must call CloseOutputFile before VerifyFileContents";
     46     rtc::scoped_ptr<test::RtpFileReader> rtp_reader(
     47         test::RtpFileReader::Create(test::RtpFileReader::kRtpDump, filename_));
     48     ASSERT_TRUE(rtp_reader.get() != NULL);
     49     test::RtpPacket packet;
     50     int i = 0;
     51     while (rtp_reader->NextPacket(&packet)) {
     52       ++i;
     53       EXPECT_EQ(static_cast<size_t>(i), packet.length);
     54       EXPECT_EQ(static_cast<size_t>(i), packet.original_length);
     55       EXPECT_EQ(static_cast<uint32_t>(i), packet.time_ms);
     56       for (int j = 0; j < i; ++j) {
     57         EXPECT_EQ(i, packet.data[j]);
     58       }
     59     }
     60     EXPECT_EQ(expected_packets, i);
     61   }
     62 
     63  private:
     64   rtc::scoped_ptr<test::RtpFileWriter> rtp_writer_;
     65   std::string filename_;
     66 };
     67 
     68 TEST_F(RtpFileWriterTest, WriteToRtpDump) {
     69   Init("test_rtp_file_writer.rtp");
     70   WriteRtpPackets(10);
     71   CloseOutputFile();
     72   VerifyFileContents(10);
     73 }
     74 
     75 }  // namespace webrtc
     76