app_unittest.cc revision 0219c9b4bfcbb778137756210eb95f40d936cc66
1/*
2 *  Copyright (c) 2015 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 "webrtc/modules/rtp_rtcp/source/rtcp_packet/app.h"
12
13#include <limits>
14
15#include "testing/gtest/include/gtest/gtest.h"
16#include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
17
18using webrtc::rtcp::App;
19using webrtc::rtcp::RawPacket;
20using webrtc::RTCPUtility::RtcpCommonHeader;
21using webrtc::RTCPUtility::RtcpParseCommonHeader;
22
23namespace webrtc {
24namespace {
25
26const uint32_t kName = ((uint32_t)'n' << 24) | ((uint32_t)'a' << 16) |
27                       ((uint32_t)'m' << 8) | (uint32_t)'e';
28const uint32_t kSenderSsrc = 0x12345678;
29
30class RtcpPacketAppTest : public ::testing::Test {
31 protected:
32  void BuildPacket() { packet = app.Build().Pass(); }
33  void ParsePacket() {
34    RtcpCommonHeader header;
35    EXPECT_TRUE(
36        RtcpParseCommonHeader(packet->Buffer(), packet->Length(), &header));
37    // Check there is exactly one RTCP packet in the buffer.
38    EXPECT_EQ(header.BlockSize(), packet->Length());
39    EXPECT_TRUE(parsed_.Parse(
40        header, packet->Buffer() + RtcpCommonHeader::kHeaderSizeBytes));
41  }
42
43  App app;
44  rtc::scoped_ptr<RawPacket> packet;
45  const App& parsed() { return parsed_; }
46
47 private:
48  App parsed_;
49};
50
51TEST_F(RtcpPacketAppTest, WithNoData) {
52  app.WithSubType(30);
53  app.WithName(kName);
54
55  BuildPacket();
56  ParsePacket();
57
58  EXPECT_EQ(30U, parsed().sub_type());
59  EXPECT_EQ(kName, parsed().name());
60  EXPECT_EQ(0u, parsed().data_size());
61}
62
63TEST_F(RtcpPacketAppTest, WithData) {
64  app.From(kSenderSsrc);
65  app.WithSubType(30);
66  app.WithName(kName);
67  const uint8_t kData[] = {'t', 'e', 's', 't', 'd', 'a', 't', 'a'};
68  const size_t kDataLength = sizeof(kData) / sizeof(kData[0]);
69  app.WithData(kData, kDataLength);
70
71  BuildPacket();
72  ParsePacket();
73
74  EXPECT_EQ(30U, parsed().sub_type());
75  EXPECT_EQ(kName, parsed().name());
76  EXPECT_EQ(kDataLength, parsed().data_size());
77  EXPECT_EQ(0, memcmp(kData, parsed().data(), kDataLength));
78}
79
80}  // namespace
81}  // namespace webrtc
82