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 // Rtp message with invalid length. | |
16 static unsigned char kRtpMsgWithInvalidLength[] = { | |
17 0x94, 0x00, 0x00, 0x00, | |
18 0x00, 0x00, 0x00, 0x00, | |
19 0xAA, 0xBB, 0xCC, 0XDD, // SSRC | |
20 0xDD, 0xCC, 0xBB, 0xAA // Only 1 CSRC, but CC count is 4. | |
21 }; | |
22 | |
23 // Rtp message with single byte header extension, invalid extension length. | |
24 static unsigned char kRtpMsgWithInvalidExtnLength[] = { | |
25 0x90, 0x00, 0x00, 0x00, | |
26 0x00, 0x00, 0x00, 0x00, | |
27 0x00, 0x00, 0x00, 0x00, | |
28 0xBE, 0xDE, 0x0A, 0x00 // Extn length - 0x0A00 | |
29 }; | |
30 | |
31 // Valid rtp Message with 2 byte header extension. | |
32 static unsigned char kRtpMsgWith2ByteExtnHeader[] = { | |
33 0x90, 0x00, 0x00, 0x00, | |
34 0x00, 0x00, 0x00, 0x00, | |
35 0xAA, 0xBB, 0xCC, 0XDD, // SSRC | |
36 0x10, 0x00, 0x00, 0x01, // 2 Byte header extension | |
37 0x01, 0x00, 0x00, 0x00 | |
38 }; | |
39 | |
40 // Stun Indication message with Zero length | |
41 static unsigned char kTurnSendIndicationMsgWithNoAttributes[] = { | |
42 0x00, 0x16, 0x00, 0x00, // Zero length | |
43 0x21, 0x12, 0xA4, 0x42, // magic cookie | |
44 '0', '1', '2', '3', // transaction id | |
45 '4', '5', '6', '7', | |
46 '8', '9', 'a', 'b', | |
47 }; | |
48 | |
49 // Stun Send Indication message with invalid length in stun header. | |
50 static unsigned char kTurnSendIndicationMsgWithInvalidLength[] = { | |
51 0x00, 0x16, 0xFF, 0x00, // length of 0xFF00 | |
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 no DATA attribute in message. | |
59 static unsigned char kTurnSendIndicatinMsgWithNoDataAttribute[] = { | |
60 0x00, 0x16, 0x00, 0x08, // length of | |
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 0x00, 0x20, 0x00, 0x04, // Mapped address. | |
66 0x00, 0x00, 0x00, 0x00 | |
67 }; | |
68 | |
69 // A valid STUN indication message with a valid RTP header in data attribute | |
70 // payload field and no extension bit set. | |
71 static unsigned char kTurnSendIndicationMsgWithoutRtpExtension[] = { | |
72 0x00, 0x16, 0x00, 0x18, // length of | |
73 0x21, 0x12, 0xA4, 0x42, // magic cookie | |
74 '0', '1', '2', '3', // transaction id | |
75 '4', '5', '6', '7', | |
76 '8', '9', 'a', 'b', | |
77 0x00, 0x20, 0x00, 0x04, // Mapped address. | |
78 0x00, 0x00, 0x00, 0x00, | |
79 0x00, 0x13, 0x00, 0x0C, // Data attribute. | |
80 0x80, 0x00, 0x00, 0x00, // RTP packet. | |
81 0x00, 0x00, 0x00, 0x00, | |
82 0x00, 0x00, 0x00, 0x00, | |
83 }; | |
84 | |
85 // A valid STUN indication message with a valid RTP header and a extension | |
86 // header. | |
87 static unsigned char kTurnSendIndicationMsgWithAbsSendTimeExtension[] = { | |
88 0x00, 0x16, 0x00, 0x20, // length of | |
89 0x21, 0x12, 0xA4, 0x42, // magic cookie | |
90 '0', '1', '2', '3', // transaction id | |
91 '4', '5', '6', '7', | |
92 '8', '9', 'a', 'b', | |
93 0x00, 0x20, 0x00, 0x04, // Mapped address. | |
94 0x00, 0x00, 0x00, 0x00, | |
95 0x00, 0x13, 0x00, 0x14, // Data attribute. | |
96 0x90, 0x00, 0x00, 0x00, // RTP packet. | |
97 0x00, 0x00, 0x00, 0x00, | |
98 0x00, 0x00, 0x00, 0x00, | |
99 0xBE, 0xDE, 0x00, 0x01, | |
100 0x32, 0xaa, 0xbb, 0xcc, | |
101 }; | |
102 | |
103 // A valid TURN channel header, but not RTP packet. | |
104 static unsigned char kTurnChannelMsgNoRtpPacket[] = { | |
105 0x40, 0x00, 0x00, 0x04, | |
106 0xaa, 0xbb, 0xcc, 0xdd, | |
107 }; | |
108 | |
109 // Turn ChannelMessage with zero length of payload. | |
110 static unsigned char kTurnChannelMsgWithZeroLength[] = { | |
111 0x40, 0x00, 0x00, 0x00 | |
112 }; | |
113 | |
114 // Turn ChannelMessage, wrapping a RTP packet without extension. | |
115 static unsigned char kTurnChannelMsgWithRtpPacket[] = { | |
116 0x40, 0x00, 0x00, 0x0C, | |
117 0x80, 0x00, 0x00, 0x00, // RTP packet. | |
118 0x00, 0x00, 0x00, 0x00, | |
119 0x00, 0x00, 0x00, 0x00, | |
120 }; | |
121 | |
122 // Turn ChannelMessage, wrapping a RTP packet with AbsSendTime Extension. | |
123 static unsigned char kTurnChannelMsgWithAbsSendTimeExtension[] = { | |
124 0x40, 0x00, 0x00, 0x14, | |
125 0x90, 0x00, 0x00, 0x00, // RTP packet. | |
126 0x00, 0x00, 0x00, 0x00, | |
127 0x00, 0x00, 0x00, 0x00, | |
128 0xBE, 0xDE, 0x00, 0x01, | |
129 0x32, 0xaa, 0xbb, 0xcc, | |
130 }; | |
131 | |
132 // RTP packet with single byte extension header of length 4 bytes. | |
133 // Extension id = 3 and length = 3 | |
134 static unsigned char kRtpMsgWithAbsSendTimeExtension[] = { | |
135 0x90, 0x00, 0x00, 0x00, | |
136 0x00, 0x00, 0x00, 0x00, | |
137 0x00, 0x00, 0x00, 0x00, | |
138 0xBE, 0xDE, 0x00, 0x01, | |
139 0x32, 0xaa, 0xbb, 0xcc, | |
140 }; | |
141 | |
142 namespace content { | |
143 | |
144 // This test verifies parsing of all invalid raw packets. | |
145 TEST(P2PSocketHostTest, TestInvalidRawRtpMessages) { | |
146 int start_pos = INT_MAX, rtp_length = INT_MAX; | |
147 EXPECT_FALSE(packet_processing_helpers::GetRtpPacketStartPositionAndLength( | |
148 reinterpret_cast<char*>(kRtpMsgWithInvalidLength), | |
149 sizeof(kRtpMsgWithInvalidLength), | |
150 &start_pos, &rtp_length)); | |
151 EXPECT_EQ(INT_MAX, start_pos); | |
152 EXPECT_EQ(INT_MAX, rtp_length); | |
153 | |
154 EXPECT_FALSE(packet_processing_helpers::GetRtpPacketStartPositionAndLength( | |
155 reinterpret_cast<char*>(kRtpMsgWithInvalidExtnLength), | |
156 sizeof(kRtpMsgWithInvalidExtnLength), | |
157 &start_pos, &rtp_length)); | |
158 EXPECT_EQ(INT_MAX, start_pos); | |
159 EXPECT_EQ(INT_MAX, rtp_length); | |
160 } | |
161 | |
162 // Verify invalid TURN send indication messages. Messages are proper STUN | |
163 // messages with incorrect values in attributes. | |
164 TEST(P2PSocketHostTest, TestInvalidTurnSendIndicationMessages) { | |
165 // Initializing out params to very large value. | |
166 int start_pos = INT_MAX, rtp_length = INT_MAX; | |
167 EXPECT_FALSE(packet_processing_helpers::GetRtpPacketStartPositionAndLength( | |
168 reinterpret_cast<char*>(kTurnSendIndicationMsgWithNoAttributes), | |
169 sizeof(kTurnSendIndicationMsgWithNoAttributes), | |
170 &start_pos, &rtp_length)); | |
171 EXPECT_EQ(INT_MAX, start_pos); | |
172 EXPECT_EQ(INT_MAX, rtp_length); | |
173 | |
174 EXPECT_FALSE(packet_processing_helpers::GetRtpPacketStartPositionAndLength( | |
175 reinterpret_cast<char*>(kTurnSendIndicationMsgWithInvalidLength), | |
176 sizeof(kTurnSendIndicationMsgWithInvalidLength), | |
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*>(kTurnSendIndicatinMsgWithNoDataAttribute), | |
183 sizeof(kTurnSendIndicatinMsgWithNoDataAttribute), | |
184 &start_pos, &rtp_length)); | |
185 EXPECT_EQ(INT_MAX, start_pos); | |
186 EXPECT_EQ(INT_MAX, rtp_length); | |
187 } | |
188 | |
189 // This test verifies incorrectly formed TURN channel messages. | |
190 TEST(P2PSocketHostTest, TestInvalidTurnChannelMessages) { | |
191 int start_pos = INT_MAX, rtp_length = INT_MAX; | |
192 EXPECT_FALSE(packet_processing_helpers::GetRtpPacketStartPositionAndLength( | |
193 reinterpret_cast<char*>(kTurnChannelMsgNoRtpPacket), | |
194 sizeof(kTurnChannelMsgNoRtpPacket), | |
195 &start_pos, &rtp_length)); | |
196 EXPECT_EQ(INT_MAX, start_pos); | |
197 EXPECT_EQ(INT_MAX, rtp_length); | |
198 | |
199 EXPECT_FALSE(packet_processing_helpers::GetRtpPacketStartPositionAndLength( | |
200 reinterpret_cast<char*>(kTurnChannelMsgWithZeroLength), | |
201 sizeof(kTurnChannelMsgWithZeroLength), | |
202 &start_pos, &rtp_length)); | |
203 EXPECT_EQ(INT_MAX, start_pos); | |
204 EXPECT_EQ(INT_MAX, rtp_length); | |
205 } | |
206 | |
207 // This test verifies parsing of a valid RTP packet which has 2byte header | |
208 // extension instead of a 1 byte header extension. | |
209 TEST(P2PSocketHostTest, TestValid2ByteExtnHdrRtpMessage) { | |
210 int start_pos = INT_MAX, rtp_length = INT_MAX; | |
211 EXPECT_TRUE(packet_processing_helpers::GetRtpPacketStartPositionAndLength( | |
212 reinterpret_cast<char*>(kRtpMsgWith2ByteExtnHeader), | |
213 sizeof(kRtpMsgWith2ByteExtnHeader), | |
214 &start_pos, &rtp_length)); | |
215 EXPECT_EQ(20, rtp_length); | |
216 EXPECT_EQ(0, start_pos); | |
217 } | |
218 | |
219 // This test verifies parsing of a valid RTP packet which has 1 byte header | |
220 // AbsSendTime extension in it. | |
221 TEST(P2PSocketHostTest, TestValidRtpPacketWithAbsSendTimeExtension) { | |
222 int start_pos = INT_MAX, rtp_length = INT_MAX; | |
223 EXPECT_TRUE(packet_processing_helpers::GetRtpPacketStartPositionAndLength( | |
224 reinterpret_cast<char*>(kRtpMsgWithAbsSendTimeExtension), | |
225 sizeof(kRtpMsgWithAbsSendTimeExtension), | |
226 &start_pos, &rtp_length)); | |
227 EXPECT_EQ(20, rtp_length); | |
228 EXPECT_EQ(0, start_pos); | |
229 } | |
230 | |
231 // This test verifies parsing of a valid TURN Send Indication messages. | |
232 TEST(P2PSocketHostTest, TestValidTurnSendIndicationMessages) { | |
233 int start_pos = INT_MAX, rtp_length = INT_MAX; | |
234 EXPECT_TRUE(packet_processing_helpers::GetRtpPacketStartPositionAndLength( | |
235 reinterpret_cast<char*>(kTurnSendIndicationMsgWithoutRtpExtension), | |
236 sizeof(kTurnSendIndicationMsgWithoutRtpExtension), | |
237 &start_pos, &rtp_length)); | |
238 EXPECT_EQ(12, rtp_length); | |
239 EXPECT_EQ(32, start_pos); | |
240 | |
241 start_pos = INT_MAX, rtp_length = INT_MAX; | |
242 EXPECT_TRUE(packet_processing_helpers::GetRtpPacketStartPositionAndLength( | |
243 reinterpret_cast<char*>(kTurnSendIndicationMsgWithAbsSendTimeExtension), | |
244 sizeof(kTurnSendIndicationMsgWithAbsSendTimeExtension), | |
245 &start_pos, &rtp_length)); | |
246 EXPECT_EQ(20, rtp_length); | |
247 EXPECT_EQ(32, start_pos); | |
248 } | |
249 | |
250 // This test verifies parsing of valid TURN Channel Messages. | |
251 TEST(P2PSocketHostTest, TestValidTurnChannelMessages) { | |
252 int start_pos = -1, rtp_length = -1; | |
253 EXPECT_TRUE(packet_processing_helpers::GetRtpPacketStartPositionAndLength( | |
254 reinterpret_cast<char*>(kTurnChannelMsgWithRtpPacket), | |
255 sizeof(kTurnChannelMsgWithRtpPacket), &start_pos, &rtp_length)); | |
256 EXPECT_EQ(12, rtp_length); | |
257 EXPECT_EQ(4, start_pos); | |
258 | |
259 start_pos = -1, rtp_length = -1; | |
260 EXPECT_TRUE(packet_processing_helpers::GetRtpPacketStartPositionAndLength( | |
261 reinterpret_cast<char*>(kTurnChannelMsgWithAbsSendTimeExtension), | |
262 sizeof(kTurnChannelMsgWithAbsSendTimeExtension), | |
263 &start_pos, &rtp_length)); | |
264 EXPECT_EQ(20, rtp_length); | |
265 EXPECT_EQ(4, start_pos); | |
266 } | |
267 | |
268 // Verify handling of a 2 byte extension header RTP messsage. Currently we don't | |
269 // handle this kind of message. | |
270 TEST(P2PSocketHostTest, TestUpdateAbsSendTimeExtensionIn2ByteHeaderExtn) { | |
271 EXPECT_FALSE(packet_processing_helpers::UpdateRtpAbsSendTimeExtn( | |
272 reinterpret_cast<char*>(kRtpMsgWith2ByteExtnHeader), | |
273 sizeof(kRtpMsgWith2ByteExtnHeader), 3)); | |
274 } | |
275 | |
276 // Verify finding an extension ID in the TURN send indication message. | |
277 TEST(P2PSocketHostTest, TestUpdateAbsSendTimeExtensionInTurnSendIndication) { | |
278 EXPECT_TRUE(packet_processing_helpers::UpdateRtpAbsSendTimeExtn( | |
279 reinterpret_cast<char*>(kTurnSendIndicationMsgWithoutRtpExtension), | |
280 sizeof(kTurnSendIndicationMsgWithoutRtpExtension), 3)); | |
281 | |
282 EXPECT_TRUE(packet_processing_helpers::UpdateRtpAbsSendTimeExtn( | |
283 reinterpret_cast<char*>(kTurnSendIndicationMsgWithAbsSendTimeExtension), | |
284 sizeof(kTurnSendIndicationMsgWithAbsSendTimeExtension), 3)); | |
285 } | |
286 | |
287 // Verify finding an extension ID in a raw rtp message. | |
288 TEST(P2PSocketHostTest, TestUpdateAbsSendTimeExtensionInRtpPacket) { | |
289 EXPECT_TRUE(packet_processing_helpers::UpdateRtpAbsSendTimeExtn( | |
290 reinterpret_cast<char*>(kRtpMsgWithAbsSendTimeExtension), | |
291 sizeof(kRtpMsgWithAbsSendTimeExtension), 3)); | |
292 } | |
293 | |
294 // Test without any packet options variables set. This method should return | |
295 // without HMAC value in the packet. | |
296 TEST(P2PSocketHostTest, TestMaybeUpdatePacketAbsSendTimeExtnAndRtpAuthTag) { | |
297 unsigned char fake_tag[4] = { 0xba, 0xdd, 0xba, 0xdd }; | |
298 talk_base::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 packet_processing_helpers::MaybeUpdatePacketAbsSendTimeExtnAndRtpAuthTag( | |
306 &rtp_packet[0], rtp_packet.size(), options)); | |
307 // Making sure we have't updated the HMAC. | |
Solis
2014/03/06 09:41:53
Should check that we haven't touched AST field as
Mallinath (Gone from Chromium)
2014/03/06 16:18:13
Done.
| |
308 EXPECT_EQ(0, memcmp(&rtp_packet[sizeof(kRtpMsgWithAbsSendTimeExtension)], | |
309 fake_tag, 4)); | |
310 } | |
311 | |
312 // Veirfy HMAC is updated when packet option parameters are set. | |
313 TEST(P2PSocketHostTest, | |
314 TestMaybeUpdatePacketAbsSendTimeExtnAndRtpAuthTagWithOptions) { | |
Solis
2014/03/06 09:41:53
You should add a test that this method sets AST wh
Mallinath (Gone from Chromium)
2014/03/06 16:18:13
Done.
| |
315 uint8 fake_tag[4] = { 0xba, 0xdd, 0xba, 0xdd }; | |
316 uint8 test_key[20] = "12345678901234567890"; | |
317 | |
318 talk_base::PacketOptions options; | |
319 options.packet_time_params.srtp_auth_key.resize(20); | |
320 options.packet_time_params.srtp_auth_key.assign( | |
321 test_key, test_key + sizeof(test_key)); | |
322 options.packet_time_params.srtp_auth_tag_len = 4; | |
323 | |
324 std::vector<char> rtp_packet; | |
325 rtp_packet.resize(sizeof(kRtpMsgWithAbsSendTimeExtension) + 4); // tag length | |
326 memcpy(&rtp_packet[0], kRtpMsgWithAbsSendTimeExtension, | |
327 sizeof(kRtpMsgWithAbsSendTimeExtension)); | |
328 memcpy(&rtp_packet[sizeof(kRtpMsgWithAbsSendTimeExtension)], fake_tag, 4); | |
329 EXPECT_TRUE( | |
330 packet_processing_helpers::MaybeUpdatePacketAbsSendTimeExtnAndRtpAuthTag( | |
331 &rtp_packet[0], rtp_packet.size(), options)); | |
332 // HMAC should be different from fake_tag. | |
333 EXPECT_NE(0, memcmp(&rtp_packet[sizeof(kRtpMsgWithAbsSendTimeExtension)], | |
334 fake_tag, 4)); | |
335 } | |
336 | |
337 } // namespace content | |
OLD | NEW |