OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/app.h" | 11 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/app.h" |
12 | 12 |
13 #include <limits> | 13 #include "testing/gmock/include/gmock/gmock.h" |
14 | |
15 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h" | 15 #include "webrtc/test/rtcp_packet_parser.h" |
17 | |
18 using webrtc::rtcp::App; | |
19 using webrtc::RTCPUtility::RtcpCommonHeader; | |
20 using webrtc::RTCPUtility::RtcpParseCommonHeader; | |
21 | 16 |
22 namespace webrtc { | 17 namespace webrtc { |
23 namespace { | 18 namespace { |
24 | 19 |
25 const uint32_t kName = ((uint32_t)'n' << 24) | ((uint32_t)'a' << 16) | | 20 using ::testing::ElementsAreArray; |
26 ((uint32_t)'m' << 8) | (uint32_t)'e'; | 21 using ::testing::make_tuple; |
27 const uint32_t kSenderSsrc = 0x12345678; | 22 using ::webrtc::rtcp::App; |
28 | 23 |
29 class RtcpPacketAppTest : public ::testing::Test { | 24 constexpr uint32_t kName = ((uint32_t)'n' << 24) | ((uint32_t)'a' << 16) | |
30 protected: | 25 ((uint32_t)'m' << 8) | (uint32_t)'e'; |
31 void BuildPacket() { packet = app.Build(); } | 26 constexpr uint8_t kSubtype = 0x1e; |
32 void ParsePacket() { | 27 constexpr uint32_t kSenderSsrc = 0x12345678; |
33 RtcpCommonHeader header; | 28 constexpr uint8_t kData[] = {'t', 'e', 's', 't', 'd', 'a', 't', 'a'}; |
34 EXPECT_TRUE(RtcpParseCommonHeader(packet.data(), packet.size(), &header)); | 29 constexpr uint8_t kVersionBits = 2 << 6; |
35 // Check there is exactly one RTCP packet in the buffer. | 30 constexpr uint8_t kPaddingBit = 1 << 5; |
36 EXPECT_EQ(header.BlockSize(), packet.size()); | 31 // clang-format off |
37 EXPECT_TRUE(parsed_.Parse( | 32 constexpr uint8_t kPacketWithoutData[] = { |
38 header, packet.data() + RtcpCommonHeader::kHeaderSizeBytes)); | 33 kVersionBits | kSubtype, App::kPacketType, 0x00, 0x02, |
39 } | 34 0x12, 0x34, 0x56, 0x78, |
| 35 'n', 'a', 'm', 'e'}; |
| 36 constexpr uint8_t kPacketWithData[] = { |
| 37 kVersionBits | kSubtype, App::kPacketType, 0x00, 0x04, |
| 38 0x12, 0x34, 0x56, 0x78, |
| 39 'n', 'a', 'm', 'e', |
| 40 't', 'e', 's', 't', |
| 41 'd', 'a', 't', 'a'}; |
| 42 constexpr uint8_t kTooSmallPacket[] = { |
| 43 kVersionBits | kSubtype, App::kPacketType, 0x00, 0x01, |
| 44 0x12, 0x34, 0x56, 0x78}; |
| 45 constexpr uint8_t kPaddingSize = 1; |
| 46 constexpr uint8_t kPacketWithUnalignedPayload[] = { |
| 47 kVersionBits | kPaddingBit | kSubtype, App::kPacketType, 0x00, 0x03, |
| 48 0x12, 0x34, 0x56, 0x78, |
| 49 'n', 'a', 'm', 'e', |
| 50 'd', 'a', 't', kPaddingSize}; |
| 51 // clang-format on |
| 52 } // namespace |
40 | 53 |
| 54 TEST(RtcpPacketAppTest, CreateWithoutData) { |
41 App app; | 55 App app; |
42 rtc::Buffer packet; | 56 app.From(kSenderSsrc); |
43 const App& parsed() { return parsed_; } | 57 app.WithSubType(kSubtype); |
44 | |
45 private: | |
46 App parsed_; | |
47 }; | |
48 | |
49 TEST_F(RtcpPacketAppTest, WithNoData) { | |
50 app.WithSubType(30); | |
51 app.WithName(kName); | 58 app.WithName(kName); |
52 | 59 |
53 BuildPacket(); | 60 rtc::Buffer raw = app.Build(); |
54 ParsePacket(); | |
55 | 61 |
56 EXPECT_EQ(30U, parsed().sub_type()); | 62 EXPECT_THAT(make_tuple(raw.data(), raw.size()), |
57 EXPECT_EQ(kName, parsed().name()); | 63 ElementsAreArray(kPacketWithoutData)); |
58 EXPECT_EQ(0u, parsed().data_size()); | |
59 } | 64 } |
60 | 65 |
61 TEST_F(RtcpPacketAppTest, WithData) { | 66 TEST(RtcpPacketAppTest, ParseWithoutData) { |
62 app.From(kSenderSsrc); | 67 App parsed; |
63 app.WithSubType(30); | 68 EXPECT_TRUE(test::ParseSinglePacket(kPacketWithoutData, &parsed)); |
64 app.WithName(kName); | |
65 const uint8_t kData[] = {'t', 'e', 's', 't', 'd', 'a', 't', 'a'}; | |
66 const size_t kDataLength = sizeof(kData) / sizeof(kData[0]); | |
67 app.WithData(kData, kDataLength); | |
68 | 69 |
69 BuildPacket(); | 70 EXPECT_EQ(kSenderSsrc, parsed.ssrc()); |
70 ParsePacket(); | 71 EXPECT_EQ(kSubtype, parsed.sub_type()); |
71 | 72 EXPECT_EQ(kName, parsed.name()); |
72 EXPECT_EQ(30U, parsed().sub_type()); | 73 EXPECT_EQ(0u, parsed.data_size()); |
73 EXPECT_EQ(kName, parsed().name()); | |
74 EXPECT_EQ(kDataLength, parsed().data_size()); | |
75 EXPECT_EQ(0, memcmp(kData, parsed().data(), kDataLength)); | |
76 } | 74 } |
77 | 75 |
78 } // namespace | 76 TEST(RtcpPacketAppTest, CreateWithData) { |
| 77 App app; |
| 78 app.From(kSenderSsrc); |
| 79 app.WithSubType(kSubtype); |
| 80 app.WithName(kName); |
| 81 app.WithData(kData, sizeof(kData)); |
| 82 |
| 83 rtc::Buffer raw = app.Build(); |
| 84 |
| 85 EXPECT_THAT(make_tuple(raw.data(), raw.size()), |
| 86 ElementsAreArray(kPacketWithData)); |
| 87 } |
| 88 |
| 89 TEST(RtcpPacketAppTest, ParseWithData) { |
| 90 App parsed; |
| 91 EXPECT_TRUE(test::ParseSinglePacket(kPacketWithData, &parsed)); |
| 92 |
| 93 EXPECT_EQ(kSenderSsrc, parsed.ssrc()); |
| 94 EXPECT_EQ(kSubtype, parsed.sub_type()); |
| 95 EXPECT_EQ(kName, parsed.name()); |
| 96 EXPECT_THAT(make_tuple(parsed.data(), parsed.data_size()), |
| 97 ElementsAreArray(kData)); |
| 98 } |
| 99 |
| 100 TEST(RtcpPacketAppTest, ParseFailsOnTooSmallPacket) { |
| 101 App parsed; |
| 102 EXPECT_FALSE(test::ParseSinglePacket(kTooSmallPacket, &parsed)); |
| 103 } |
| 104 |
| 105 TEST(RtcpPacketAppTest, ParseFailsOnUnalignedPayload) { |
| 106 App parsed; |
| 107 EXPECT_FALSE(test::ParseSinglePacket(kPacketWithUnalignedPayload, &parsed)); |
| 108 } |
| 109 |
79 } // namespace webrtc | 110 } // namespace webrtc |
OLD | NEW |