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