OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 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/p2p/base/rtppacketutil.h" |
| 12 |
| 13 #include <stddef.h> |
| 14 |
| 15 #include <vector> |
| 16 |
| 17 #include "webrtc/base/asyncpacketsocket.h" |
| 18 #include "webrtc/base/gunit.h" |
| 19 |
| 20 static unsigned char kFakeTag[4] = { 0xba, 0xdd, 0xba, 0xdd }; |
| 21 static unsigned char kTestKey[] = "12345678901234567890"; |
| 22 static unsigned char kTestAstValue[3] = { 0xaa, 0xbb, 0xcc }; |
| 23 |
| 24 // Rtp message with invalid length. |
| 25 static unsigned char kRtpMsgWithInvalidLength[] = { |
| 26 0x94, 0x00, 0x00, 0x00, |
| 27 0x00, 0x00, 0x00, 0x00, |
| 28 0xAA, 0xBB, 0xCC, 0XDD, // SSRC |
| 29 0xDD, 0xCC, 0xBB, 0xAA // Only 1 CSRC, but CC count is 4. |
| 30 }; |
| 31 |
| 32 // Rtp message with single byte header extension, invalid extension length. |
| 33 static unsigned char kRtpMsgWithInvalidExtnLength[] = { |
| 34 0x90, 0x00, 0x00, 0x00, |
| 35 0x00, 0x00, 0x00, 0x00, |
| 36 0x00, 0x00, 0x00, 0x00, |
| 37 0xBE, 0xDE, 0x0A, 0x00 // Extn length - 0x0A00 |
| 38 }; |
| 39 |
| 40 // Valid rtp Message with 2 byte header extension. |
| 41 static unsigned char kRtpMsgWith2ByteExtnHeader[] = { |
| 42 0x90, 0x00, 0x00, 0x00, |
| 43 0x00, 0x00, 0x00, 0x00, |
| 44 0xAA, 0xBB, 0xCC, 0XDD, // SSRC |
| 45 0x10, 0x00, 0x00, 0x01, // 2 Byte header extension |
| 46 0x01, 0x00, 0x00, 0x00 |
| 47 }; |
| 48 |
| 49 // Stun Indication message with Zero length |
| 50 static unsigned char kTurnSendIndicationMsgWithNoAttributes[] = { |
| 51 0x00, 0x16, 0x00, 0x00, // Zero length |
| 52 0x21, 0x12, 0xA4, 0x42, // magic cookie |
| 53 '0', '1', '2', '3', // transaction id |
| 54 '4', '5', '6', '7', |
| 55 '8', '9', 'a', 'b', |
| 56 }; |
| 57 |
| 58 // Stun Send Indication message with invalid length in stun header. |
| 59 static unsigned char kTurnSendIndicationMsgWithInvalidLength[] = { |
| 60 0x00, 0x16, 0xFF, 0x00, // length of 0xFF00 |
| 61 0x21, 0x12, 0xA4, 0x42, // magic cookie |
| 62 '0', '1', '2', '3', // transaction id |
| 63 '4', '5', '6', '7', |
| 64 '8', '9', 'a', 'b', |
| 65 }; |
| 66 |
| 67 // Stun Send Indication message with no DATA attribute in message. |
| 68 static unsigned char kTurnSendIndicatinMsgWithNoDataAttribute[] = { |
| 69 0x00, 0x16, 0x00, 0x08, // length of |
| 70 0x21, 0x12, 0xA4, 0x42, // magic cookie |
| 71 '0', '1', '2', '3', // transaction id |
| 72 '4', '5', '6', '7', |
| 73 '8', '9', 'a', 'b', |
| 74 0x00, 0x20, 0x00, 0x04, // Mapped address. |
| 75 0x00, 0x00, 0x00, 0x00 |
| 76 }; |
| 77 |
| 78 // A valid STUN indication message with a valid RTP header in data attribute |
| 79 // payload field and no extension bit set. |
| 80 static unsigned char kTurnSendIndicationMsgWithoutRtpExtension[] = { |
| 81 0x00, 0x16, 0x00, 0x18, // length of |
| 82 0x21, 0x12, 0xA4, 0x42, // magic cookie |
| 83 '0', '1', '2', '3', // transaction id |
| 84 '4', '5', '6', '7', |
| 85 '8', '9', 'a', 'b', |
| 86 0x00, 0x20, 0x00, 0x04, // Mapped address. |
| 87 0x00, 0x00, 0x00, 0x00, |
| 88 0x00, 0x13, 0x00, 0x0C, // Data attribute. |
| 89 0x80, 0x00, 0x00, 0x00, // RTP packet. |
| 90 0x00, 0x00, 0x00, 0x00, |
| 91 0x00, 0x00, 0x00, 0x00, |
| 92 }; |
| 93 |
| 94 // A valid STUN indication message with a valid RTP header and a extension |
| 95 // header. |
| 96 static unsigned char kTurnSendIndicationMsgWithAbsSendTimeExtension[] = { |
| 97 0x00, 0x16, 0x00, 0x24, // length of |
| 98 0x21, 0x12, 0xA4, 0x42, // magic cookie |
| 99 '0', '1', '2', '3', // transaction id |
| 100 '4', '5', '6', '7', |
| 101 '8', '9', 'a', 'b', |
| 102 0x00, 0x20, 0x00, 0x04, // Mapped address. |
| 103 0x00, 0x00, 0x00, 0x00, |
| 104 0x00, 0x13, 0x00, 0x18, // Data attribute. |
| 105 0x90, 0x00, 0x00, 0x00, // RTP packet. |
| 106 0x00, 0x00, 0x00, 0x00, |
| 107 0x00, 0x00, 0x00, 0x00, |
| 108 0xBE, 0xDE, 0x00, 0x02, |
| 109 0x22, 0xaa, 0xbb, 0xcc, |
| 110 0x32, 0xaa, 0xbb, 0xcc, |
| 111 }; |
| 112 |
| 113 // A valid TURN channel header, but not RTP packet. |
| 114 static unsigned char kTurnChannelMsgNoRtpPacket[] = { |
| 115 0x40, 0x00, 0x00, 0x04, |
| 116 0xaa, 0xbb, 0xcc, 0xdd, |
| 117 }; |
| 118 |
| 119 // Turn ChannelMessage with zero length of payload. |
| 120 static unsigned char kTurnChannelMsgWithZeroLength[] = { |
| 121 0x40, 0x00, 0x00, 0x00 |
| 122 }; |
| 123 |
| 124 // Turn ChannelMessage, wrapping a RTP packet without extension. |
| 125 static unsigned char kTurnChannelMsgWithRtpPacket[] = { |
| 126 0x40, 0x00, 0x00, 0x0C, |
| 127 0x80, 0x00, 0x00, 0x00, // RTP packet. |
| 128 0x00, 0x00, 0x00, 0x00, |
| 129 0x00, 0x00, 0x00, 0x00, |
| 130 }; |
| 131 |
| 132 // Turn ChannelMessage, wrapping a RTP packet with AbsSendTime Extension. |
| 133 static unsigned char kTurnChannelMsgWithAbsSendTimeExtension[] = { |
| 134 0x40, 0x00, 0x00, 0x14, |
| 135 0x90, 0x00, 0x00, 0x00, // RTP packet. |
| 136 0x00, 0x00, 0x00, 0x00, |
| 137 0x00, 0x00, 0x00, 0x00, |
| 138 0xBE, 0xDE, 0x00, 0x01, |
| 139 0x32, 0xaa, 0xbb, 0xcc, |
| 140 }; |
| 141 |
| 142 // RTP packet with single byte extension header of length 4 bytes. |
| 143 // Extension id = 3 and length = 3 |
| 144 static unsigned char kRtpMsgWithAbsSendTimeExtension[] = { |
| 145 0x90, 0x00, 0x00, 0x00, |
| 146 0x00, 0x00, 0x00, 0x00, |
| 147 0x00, 0x00, 0x00, 0x00, |
| 148 0xBE, 0xDE, 0x00, 0x02, |
| 149 0x22, 0x00, 0x02, 0x1c, |
| 150 0x32, 0xaa, 0xbb, 0xcc, |
| 151 }; |
| 152 |
| 153 // Index of AbsSendTimeExtn data in message |kRtpMsgWithAbsSendTimeExtension|. |
| 154 static const int kAstIndexInRtpMsg = 21; |
| 155 |
| 156 namespace rtc { |
| 157 |
| 158 // This test verifies parsing of all invalid raw packets. |
| 159 TEST(RtpPacketUtil, TestInvalidRawRtpMessages) { |
| 160 size_t start_pos = SIZE_MAX, rtp_length = SIZE_MAX; |
| 161 EXPECT_FALSE(GetRtpPacketStartPositionAndLength( |
| 162 reinterpret_cast<char*>(kRtpMsgWithInvalidLength), |
| 163 sizeof(kRtpMsgWithInvalidLength), &start_pos, &rtp_length)); |
| 164 EXPECT_EQ(SIZE_MAX, start_pos); |
| 165 EXPECT_EQ(SIZE_MAX, rtp_length); |
| 166 |
| 167 EXPECT_FALSE(GetRtpPacketStartPositionAndLength( |
| 168 reinterpret_cast<char*>(kRtpMsgWithInvalidExtnLength), |
| 169 sizeof(kRtpMsgWithInvalidExtnLength), &start_pos, &rtp_length)); |
| 170 EXPECT_EQ(SIZE_MAX, start_pos); |
| 171 EXPECT_EQ(SIZE_MAX, rtp_length); |
| 172 } |
| 173 |
| 174 // Verify invalid TURN send indication messages. Messages are proper STUN |
| 175 // messages with incorrect values in attributes. |
| 176 TEST(RtpPacketUtil, TestInvalidTurnSendIndicationMessages) { |
| 177 // Initializing out params to very large value. |
| 178 size_t start_pos = SIZE_MAX, rtp_length = SIZE_MAX; |
| 179 EXPECT_FALSE(GetRtpPacketStartPositionAndLength( |
| 180 reinterpret_cast<char*>(kTurnSendIndicationMsgWithNoAttributes), |
| 181 sizeof(kTurnSendIndicationMsgWithNoAttributes), &start_pos, &rtp_length)); |
| 182 EXPECT_EQ(SIZE_MAX, start_pos); |
| 183 EXPECT_EQ(SIZE_MAX, rtp_length); |
| 184 |
| 185 EXPECT_FALSE(GetRtpPacketStartPositionAndLength( |
| 186 reinterpret_cast<char*>(kTurnSendIndicationMsgWithInvalidLength), |
| 187 sizeof(kTurnSendIndicationMsgWithInvalidLength), &start_pos, |
| 188 &rtp_length)); |
| 189 EXPECT_EQ(SIZE_MAX, start_pos); |
| 190 EXPECT_EQ(SIZE_MAX, rtp_length); |
| 191 |
| 192 EXPECT_FALSE(GetRtpPacketStartPositionAndLength( |
| 193 reinterpret_cast<char*>(kTurnSendIndicatinMsgWithNoDataAttribute), |
| 194 sizeof(kTurnSendIndicatinMsgWithNoDataAttribute), &start_pos, |
| 195 &rtp_length)); |
| 196 EXPECT_EQ(SIZE_MAX, start_pos); |
| 197 EXPECT_EQ(SIZE_MAX, rtp_length); |
| 198 } |
| 199 |
| 200 // This test verifies incorrectly formed TURN channel messages. |
| 201 TEST(RtpPacketUtil, TestInvalidTurnChannelMessages) { |
| 202 size_t start_pos = SIZE_MAX, rtp_length = SIZE_MAX; |
| 203 EXPECT_FALSE(GetRtpPacketStartPositionAndLength( |
| 204 reinterpret_cast<char*>(kTurnChannelMsgNoRtpPacket), |
| 205 sizeof(kTurnChannelMsgNoRtpPacket), &start_pos, &rtp_length)); |
| 206 EXPECT_EQ(SIZE_MAX, start_pos); |
| 207 EXPECT_EQ(SIZE_MAX, rtp_length); |
| 208 |
| 209 EXPECT_FALSE(GetRtpPacketStartPositionAndLength( |
| 210 reinterpret_cast<char*>(kTurnChannelMsgWithZeroLength), |
| 211 sizeof(kTurnChannelMsgWithZeroLength), &start_pos, &rtp_length)); |
| 212 EXPECT_EQ(SIZE_MAX, start_pos); |
| 213 EXPECT_EQ(SIZE_MAX, rtp_length); |
| 214 } |
| 215 |
| 216 // This test verifies parsing of a valid RTP packet which has 2byte header |
| 217 // extension instead of a 1 byte header extension. |
| 218 TEST(RtpPacketUtil, TestValid2ByteExtnHdrRtpMessage) { |
| 219 size_t start_pos = SIZE_MAX, rtp_length = SIZE_MAX; |
| 220 EXPECT_TRUE(GetRtpPacketStartPositionAndLength( |
| 221 reinterpret_cast<char*>(kRtpMsgWith2ByteExtnHeader), |
| 222 sizeof(kRtpMsgWith2ByteExtnHeader), &start_pos, &rtp_length)); |
| 223 EXPECT_EQ(20U, rtp_length); |
| 224 EXPECT_EQ(0U, start_pos); |
| 225 } |
| 226 |
| 227 // This test verifies parsing of a valid RTP packet which has 1 byte header |
| 228 // AbsSendTime extension in it. |
| 229 TEST(RtpPacketUtil, TestValidRtpPacketWithAbsSendTimeExtension) { |
| 230 size_t start_pos = SIZE_MAX, rtp_length = SIZE_MAX; |
| 231 EXPECT_TRUE(GetRtpPacketStartPositionAndLength( |
| 232 reinterpret_cast<char*>(kRtpMsgWithAbsSendTimeExtension), |
| 233 sizeof(kRtpMsgWithAbsSendTimeExtension), &start_pos, &rtp_length)); |
| 234 EXPECT_EQ(24U, rtp_length); |
| 235 EXPECT_EQ(0U, start_pos); |
| 236 } |
| 237 |
| 238 // This test verifies parsing of a valid TURN Send Indication messages. |
| 239 TEST(RtpPacketUtil, TestValidTurnSendIndicationMessages) { |
| 240 size_t start_pos = SIZE_MAX, rtp_length = SIZE_MAX; |
| 241 EXPECT_TRUE(GetRtpPacketStartPositionAndLength( |
| 242 reinterpret_cast<char*>(kTurnSendIndicationMsgWithoutRtpExtension), |
| 243 sizeof(kTurnSendIndicationMsgWithoutRtpExtension), &start_pos, |
| 244 &rtp_length)); |
| 245 EXPECT_EQ(12U, rtp_length); |
| 246 EXPECT_EQ(32U, start_pos); |
| 247 |
| 248 start_pos = SIZE_MAX, rtp_length = SIZE_MAX; |
| 249 EXPECT_TRUE(GetRtpPacketStartPositionAndLength( |
| 250 reinterpret_cast<char*>(kTurnSendIndicationMsgWithAbsSendTimeExtension), |
| 251 sizeof(kTurnSendIndicationMsgWithAbsSendTimeExtension), &start_pos, |
| 252 &rtp_length)); |
| 253 EXPECT_EQ(24U, rtp_length); |
| 254 EXPECT_EQ(32U, start_pos); |
| 255 } |
| 256 |
| 257 // This test verifies parsing of valid TURN Channel Messages. |
| 258 TEST(RtpPacketUtil, TestValidTurnChannelMessages) { |
| 259 size_t start_pos = 0, rtp_length = 0; |
| 260 EXPECT_TRUE(GetRtpPacketStartPositionAndLength( |
| 261 reinterpret_cast<char*>(kTurnChannelMsgWithRtpPacket), |
| 262 sizeof(kTurnChannelMsgWithRtpPacket), &start_pos, &rtp_length)); |
| 263 EXPECT_EQ(12U, rtp_length); |
| 264 EXPECT_EQ(4U, start_pos); |
| 265 |
| 266 start_pos = 0, rtp_length = 0; |
| 267 EXPECT_TRUE(GetRtpPacketStartPositionAndLength( |
| 268 reinterpret_cast<char*>(kTurnChannelMsgWithAbsSendTimeExtension), |
| 269 sizeof(kTurnChannelMsgWithAbsSendTimeExtension), &start_pos, |
| 270 &rtp_length)); |
| 271 EXPECT_EQ(20U, rtp_length); |
| 272 EXPECT_EQ(4U, start_pos); |
| 273 } |
| 274 |
| 275 // Verify handling of a 2 byte extension header RTP messsage. Currently we don't |
| 276 // handle this kind of message. |
| 277 TEST(RtpPacketUtil, TestUpdateAbsSendTimeExtensionIn2ByteHeaderExtn) { |
| 278 EXPECT_FALSE(UpdateRtpAbsSendTimeExtension( |
| 279 reinterpret_cast<char*>(kRtpMsgWith2ByteExtnHeader), |
| 280 sizeof(kRtpMsgWith2ByteExtnHeader), 3, 0)); |
| 281 } |
| 282 |
| 283 // Verify finding an extension ID in the TURN send indication message. |
| 284 TEST(RtpPacketUtil, TestUpdateAbsSendTimeExtensionInTurnSendIndication) { |
| 285 EXPECT_TRUE(UpdateRtpAbsSendTimeExtension( |
| 286 reinterpret_cast<char*>(kTurnSendIndicationMsgWithoutRtpExtension), |
| 287 sizeof(kTurnSendIndicationMsgWithoutRtpExtension), 3, 0)); |
| 288 |
| 289 EXPECT_TRUE(UpdateRtpAbsSendTimeExtension( |
| 290 reinterpret_cast<char*>(kTurnSendIndicationMsgWithAbsSendTimeExtension), |
| 291 sizeof(kTurnSendIndicationMsgWithAbsSendTimeExtension), 3, 0)); |
| 292 } |
| 293 |
| 294 // Test without any packet options variables set. This method should return |
| 295 // without HMAC value in the packet. |
| 296 TEST(RtpPacketUtil, TestApplyPacketOptionsWithDefaultValues) { |
| 297 unsigned char fake_tag[4] = {0xba, 0xdd, 0xba, 0xdd}; |
| 298 rtc::PacketOptions options; |
| 299 std::vector<char> rtp_packet; |
| 300 rtp_packet.resize(sizeof(kRtpMsgWithAbsSendTimeExtension) + 4); // tag length |
| 301 memcpy(&rtp_packet[0], kRtpMsgWithAbsSendTimeExtension, |
| 302 sizeof(kRtpMsgWithAbsSendTimeExtension)); |
| 303 memcpy(&rtp_packet[sizeof(kRtpMsgWithAbsSendTimeExtension)], fake_tag, 4); |
| 304 EXPECT_TRUE( |
| 305 ApplyPacketOptions(&rtp_packet[0], rtp_packet.size(), options, 0)); |
| 306 // Making sure we have't updated the HMAC. |
| 307 EXPECT_EQ(0, memcmp(&rtp_packet[sizeof(kRtpMsgWithAbsSendTimeExtension)], |
| 308 fake_tag, 4)); |
| 309 |
| 310 // Verify AbsouluteSendTime extension field is not modified. |
| 311 EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInRtpMsg], kTestAstValue, |
| 312 sizeof(kTestAstValue))); |
| 313 } |
| 314 |
| 315 // Veirfy HMAC is updated when packet option parameters are set. |
| 316 TEST(RtpPacketUtil, TestApplyPacketOptionsWithAuthParams) { |
| 317 rtc::PacketOptions options; |
| 318 options.packet_time_params.srtp_auth_key.resize(20); |
| 319 options.packet_time_params.srtp_auth_key.assign(kTestKey, |
| 320 kTestKey + sizeof(kTestKey)); |
| 321 options.packet_time_params.srtp_auth_tag_len = 4; |
| 322 |
| 323 std::vector<char> rtp_packet; |
| 324 rtp_packet.resize(sizeof(kRtpMsgWithAbsSendTimeExtension) + 4); // tag length |
| 325 memcpy(&rtp_packet[0], kRtpMsgWithAbsSendTimeExtension, |
| 326 sizeof(kRtpMsgWithAbsSendTimeExtension)); |
| 327 memcpy(&rtp_packet[sizeof(kRtpMsgWithAbsSendTimeExtension)], kFakeTag, 4); |
| 328 EXPECT_TRUE( |
| 329 ApplyPacketOptions(&rtp_packet[0], rtp_packet.size(), options, 0)); |
| 330 // HMAC should be different from fake_tag. |
| 331 EXPECT_NE(0, memcmp(&rtp_packet[sizeof(kRtpMsgWithAbsSendTimeExtension)], |
| 332 kFakeTag, sizeof(kFakeTag))); |
| 333 |
| 334 // Verify AbsouluteSendTime extension field is not modified. |
| 335 EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInRtpMsg], kTestAstValue, |
| 336 sizeof(kTestAstValue))); |
| 337 } |
| 338 |
| 339 // Verify finding an extension ID in a raw rtp message. |
| 340 TEST(RtpPacketUtil, TestUpdateAbsSendTimeExtensionInRtpPacket) { |
| 341 EXPECT_TRUE(UpdateRtpAbsSendTimeExtension( |
| 342 reinterpret_cast<char*>(kRtpMsgWithAbsSendTimeExtension), |
| 343 sizeof(kRtpMsgWithAbsSendTimeExtension), 3, 0)); |
| 344 } |
| 345 |
| 346 // Verify we update both AbsSendTime extension header and HMAC. |
| 347 TEST(RtpPacketUtil, TestApplyPacketOptionsWithAuthParamsAndAbsSendTime) { |
| 348 rtc::PacketOptions options; |
| 349 options.packet_time_params.srtp_auth_key.resize(20); |
| 350 options.packet_time_params.srtp_auth_key.assign(kTestKey, |
| 351 kTestKey + sizeof(kTestKey)); |
| 352 options.packet_time_params.srtp_auth_tag_len = 4; |
| 353 options.packet_time_params.rtp_sendtime_extension_id = 3; |
| 354 // 3 is also present in the test message. |
| 355 |
| 356 std::vector<char> rtp_packet; |
| 357 rtp_packet.resize(sizeof(kRtpMsgWithAbsSendTimeExtension) + 4); // tag length |
| 358 memcpy(&rtp_packet[0], kRtpMsgWithAbsSendTimeExtension, |
| 359 sizeof(kRtpMsgWithAbsSendTimeExtension)); |
| 360 memcpy(&rtp_packet[sizeof(kRtpMsgWithAbsSendTimeExtension)], kFakeTag, 4); |
| 361 EXPECT_TRUE( |
| 362 ApplyPacketOptions(&rtp_packet[0], rtp_packet.size(), options, 0xccbbaa)); |
| 363 // HMAC should be different from fake_tag. |
| 364 EXPECT_NE(0, memcmp(&rtp_packet[sizeof(kRtpMsgWithAbsSendTimeExtension)], |
| 365 kFakeTag, sizeof(kFakeTag))); |
| 366 |
| 367 // ApplyPackets should have the new timestamp passed as input. |
| 368 unsigned char timestamp_array[3] = {0xcc, 0xbb, 0xaa}; |
| 369 EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInRtpMsg], timestamp_array, |
| 370 sizeof(timestamp_array))); |
| 371 } |
| 372 |
| 373 } // namespace rtc |
OLD | NEW |