aboutsummaryrefslogtreecommitdiff
path: root/cast/streaming/packet_util_unittest.cc
blob: 82e2a40871bd933b58ed130a15bbed28a7c07a06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cast/streaming/packet_util.h"

#include "absl/types/span.h"
#include "gtest/gtest.h"

namespace openscreen {
namespace cast {
namespace {

// Tests that a simple RTCP packet containing only a Sender Report can be
// identified.
TEST(PacketUtilTest, InspectsRtcpPacketFromSender) {
  // clang-format off
  const uint8_t kSenderReportPacket[] = {
    0b10000000,  // Version=2, Padding=no, ItemCount=0.
    200,  // RTCP Packet type.
    0x00, 0x06,  // Length of remainder of packet, in 32-bit words.
    1, 2, 3, 4,  // SSRC of sender.
    0xe0, 0x73, 0x2e, 0x54,  // NTP Timestamp (late evening on 2019-04-30).
        0x80, 0x00, 0x00, 0x00,
    0x00, 0x14, 0x99, 0x70,  // RTP Timestamp (15 seconds, 90kHz timebase).
    0x00, 0x00, 0x01, 0xff,  // Sender's Packet Count.
    0x00, 0x07, 0x11, 0x0d,  // Sender's Octet Count.
  };
  // clang-format on
  const Ssrc kSenderSsrc = 0x01020304;

  const auto result = InspectPacketForRouting(kSenderReportPacket);
  EXPECT_EQ(ApparentPacketType::RTCP, result.first);
  EXPECT_EQ(kSenderSsrc, result.second);
}

// Tests that compound RTCP packets containing a Receiver Report and/or a Cast
// Feedback message can be identified.
TEST(PacketUtilTest, InspectsRtcpPacketFromReceiver) {
  // clang-format off
  const uint8_t kReceiverReportPacket[] = {
    0b10000001,  // Version=2, Padding=no, ItemCount=1.
    201,  // RTCP Packet type.
    0x00, 0x01,  // Length of remainder of packet, in 32-bit words.
    9, 8, 7, 6,  // SSRC of receiver.
  };
  const uint8_t kCastFeedbackPacket[] = {
    // Cast Feedback
    0b10000000 | 15,  // Version=2, Padding=no, Subtype=15.
    206,  // RTCP Packet type byte.
    0x00, 0x04,  // Length of remainder of packet, in 32-bit words.
    9, 8, 7, 6,  // SSRC of receiver.
    1, 2, 3, 4,  // SSRC of sender.
    'C', 'A', 'S', 'T',
    0x0a,  // Checkpoint Frame ID (lower 8 bits).
    0x00,  // Number of "Loss Fields"
    0x00, 0x28,  // Current Playout Delay in milliseconds.
  };
  // clang-format on
  const Ssrc kReceiverSsrc = 0x09080706;

  {
    const auto result = InspectPacketForRouting(kReceiverReportPacket);
    EXPECT_EQ(ApparentPacketType::RTCP, result.first);
    EXPECT_EQ(kReceiverSsrc, result.second);
  }

  {
    const auto result = InspectPacketForRouting(kCastFeedbackPacket);
    EXPECT_EQ(ApparentPacketType::RTCP, result.first);
    EXPECT_EQ(kReceiverSsrc, result.second);
  }

  const absl::Span<const uint8_t> kCompoundCombinations[2][2] = {
      {kReceiverReportPacket, kCastFeedbackPacket},
      {kCastFeedbackPacket, kReceiverReportPacket},
  };
  for (const auto& combo : kCompoundCombinations) {
    uint8_t compound_packet[sizeof(kReceiverReportPacket) +
                            sizeof(kCastFeedbackPacket)];
    memcpy(compound_packet, combo[0].data(), combo[0].size());
    memcpy(compound_packet + combo[0].size(), combo[1].data(), combo[1].size());

    const auto result = InspectPacketForRouting(compound_packet);
    EXPECT_EQ(ApparentPacketType::RTCP, result.first);
    EXPECT_EQ(kReceiverSsrc, result.second);
  }
}

// Tests that a RTP packet can be identified.
TEST(PacketUtilTest, InspectsRtpPacket) {
  // clang-format off
  const uint8_t kInput[] = {
    0b10000000,  // Version/Padding byte.
    96,  // Payload type byte.
    0xbe, 0xef,  // Sequence number.
    9, 8, 7, 6,  // RTP timestamp.
    1, 2, 3, 4,  // SSRC.
    0b10000000,  // Is key frame, no extensions.
    5,  // Frame ID.
    0xa, 0xb,  // Packet ID.
    0xa, 0xc,  // Max packet ID.
    0xf, 0xe, 0xd, 0xc, 0xb, 0xa, 0x9, 0x8,  // Payload.
  };
  // clang-format on
  const Ssrc kSenderSsrc = 0x01020304;

  const auto result = InspectPacketForRouting(kInput);
  EXPECT_EQ(ApparentPacketType::RTP, result.first);
  EXPECT_EQ(kSenderSsrc, result.second);
}

// Tests that a RTP packet with the "127 payload type" hack can be identified as
// valid. See comments in rtp_defines.h for the RtpPayloadType enum definition,
// for further details.
TEST(PacketUtilTest, InspectsAndroidAudioRtpPacket) {
  // clang-format off
  const uint8_t kInput[] = {
    0b10000000,  // Version/Padding byte.
    127,  // Payload type byte.
    0xbe, 0xef,  // Sequence number.
    9, 8, 7, 6,  // RTP timestamp.
    1, 2, 3, 4,  // SSRC.
    0b10000000,  // Is key frame, no extensions.
    5,  // Frame ID.
    0xa, 0xb,  // Packet ID.
    0xa, 0xc,  // Max packet ID.
    0xf, 0xe, 0xd, 0xc, 0xb, 0xa, 0x9, 0x8,  // Payload.
  };
  // clang-format on
  const Ssrc kSenderSsrc = 0x01020304;

  const auto result = InspectPacketForRouting(kInput);
  EXPECT_EQ(ApparentPacketType::RTP, result.first);
  EXPECT_EQ(kSenderSsrc, result.second);
}

// Tests that a malformed RTP packet can be identified.
TEST(PacketUtilTest, InspectsMalformedRtpPacket) {
  // clang-format off
  const uint8_t kInput[] = {
    0b11000000,  // BAD: Version/Padding byte.
    96,  // Payload type byte.
    0xbe, 0xef,  // Sequence number.
    9, 8, 7, 6,  // RTP timestamp.
    1, 2, 3, 4,  // SSRC.
    0b10000000,  // Is key frame, no extensions.
    5,  // Frame ID.
    0xa, 0xb,  // Packet ID.
    0xa, 0xc,  // Max packet ID.
    0xf, 0xe, 0xd, 0xc, 0xb, 0xa, 0x9, 0x8,  // Payload.
  };
  // clang-format on

  const auto result = InspectPacketForRouting(kInput);
  EXPECT_EQ(ApparentPacketType::UNKNOWN, result.first);
}

// Tests that an empty packet is classified as unknown.
TEST(PacketUtilTest, InspectsEmptyPacket) {
  const uint8_t kInput[] = {};

  const auto result =
      InspectPacketForRouting(absl::Span<const uint8_t>(kInput, 0));
  EXPECT_EQ(ApparentPacketType::UNKNOWN, result.first);
}

// Tests that a packet with garbage is classified as unknown.
TEST(PacketUtilTest, InspectsGarbagePacket) {
  // clang-format off
  const uint8_t kInput[] = {
    0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
    0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
    0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
    0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
  };
  // clang-format on

  const auto result = InspectPacketForRouting(kInput);
  EXPECT_EQ(ApparentPacketType::UNKNOWN, result.first);
}

}  // namespace
}  // namespace cast
}  // namespace openscreen