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