| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" | 5 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" |
| 6 | 6 |
| 7 #include <deque> | 7 #include <deque> |
| 8 | 8 |
| 9 #include "base/sys_byteorder.h" | 9 #include "base/sys_byteorder.h" |
| 10 #include "content/browser/renderer_host/p2p/socket_host_test_utils.h" | 10 #include "content/browser/renderer_host/p2p/socket_host_test_utils.h" |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 }; | 85 }; |
| 86 | 86 |
| 87 // Verify that we can send STUN message and that they are formatted | 87 // Verify that we can send STUN message and that they are formatted |
| 88 // properly. | 88 // properly. |
| 89 TEST_F(P2PSocketHostTcpTest, SendStunNoAuth) { | 89 TEST_F(P2PSocketHostTcpTest, SendStunNoAuth) { |
| 90 EXPECT_CALL(sender_, Send( | 90 EXPECT_CALL(sender_, Send( |
| 91 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 91 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
| 92 .Times(3) | 92 .Times(3) |
| 93 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); | 93 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); |
| 94 | 94 |
| 95 talk_base::PacketOptions options; |
| 95 std::vector<char> packet1; | 96 std::vector<char> packet1; |
| 96 CreateStunRequest(&packet1); | 97 CreateStunRequest(&packet1); |
| 97 socket_host_->Send(dest_, packet1, net::DSCP_NO_CHANGE, 0); | 98 socket_host_->Send(dest_, packet1, options, 0); |
| 98 | 99 |
| 99 std::vector<char> packet2; | 100 std::vector<char> packet2; |
| 100 CreateStunResponse(&packet2); | 101 CreateStunResponse(&packet2); |
| 101 socket_host_->Send(dest_, packet2, net::DSCP_NO_CHANGE, 0); | 102 socket_host_->Send(dest_, packet2, options, 0); |
| 102 | 103 |
| 103 std::vector<char> packet3; | 104 std::vector<char> packet3; |
| 104 CreateStunError(&packet3); | 105 CreateStunError(&packet3); |
| 105 socket_host_->Send(dest_, packet3, net::DSCP_NO_CHANGE, 0); | 106 socket_host_->Send(dest_, packet3, options, 0); |
| 106 | 107 |
| 107 std::string expected_data; | 108 std::string expected_data; |
| 108 expected_data.append(IntToSize(packet1.size())); | 109 expected_data.append(IntToSize(packet1.size())); |
| 109 expected_data.append(packet1.begin(), packet1.end()); | 110 expected_data.append(packet1.begin(), packet1.end()); |
| 110 expected_data.append(IntToSize(packet2.size())); | 111 expected_data.append(IntToSize(packet2.size())); |
| 111 expected_data.append(packet2.begin(), packet2.end()); | 112 expected_data.append(packet2.begin(), packet2.end()); |
| 112 expected_data.append(IntToSize(packet3.size())); | 113 expected_data.append(IntToSize(packet3.size())); |
| 113 expected_data.append(packet3.begin(), packet3.end()); | 114 expected_data.append(packet3.begin(), packet3.end()); |
| 114 | 115 |
| 115 EXPECT_EQ(expected_data, sent_data_); | 116 EXPECT_EQ(expected_data, sent_data_); |
| 116 } | 117 } |
| 117 | 118 |
| 118 // Verify that we can receive STUN messages from the socket, and that | 119 // Verify that we can receive STUN messages from the socket, and that |
| 119 // the messages are parsed properly. | 120 // the messages are parsed properly. |
| 120 TEST_F(P2PSocketHostTcpTest, ReceiveStun) { | 121 TEST_F(P2PSocketHostTcpTest, ReceiveStun) { |
| 121 EXPECT_CALL(sender_, Send( | 122 EXPECT_CALL(sender_, Send( |
| 122 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 123 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
| 123 .Times(3) | 124 .Times(3) |
| 124 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); | 125 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); |
| 125 | 126 |
| 127 talk_base::PacketOptions options; |
| 126 std::vector<char> packet1; | 128 std::vector<char> packet1; |
| 127 CreateStunRequest(&packet1); | 129 CreateStunRequest(&packet1); |
| 128 socket_host_->Send(dest_, packet1, net::DSCP_NO_CHANGE, 0); | 130 socket_host_->Send(dest_, packet1, options, 0); |
| 129 | 131 |
| 130 std::vector<char> packet2; | 132 std::vector<char> packet2; |
| 131 CreateStunResponse(&packet2); | 133 CreateStunResponse(&packet2); |
| 132 socket_host_->Send(dest_, packet2, net::DSCP_NO_CHANGE, 0); | 134 socket_host_->Send(dest_, packet2, options, 0); |
| 133 | 135 |
| 134 std::vector<char> packet3; | 136 std::vector<char> packet3; |
| 135 CreateStunError(&packet3); | 137 CreateStunError(&packet3); |
| 136 socket_host_->Send(dest_, packet3, net::DSCP_NO_CHANGE, 0); | 138 socket_host_->Send(dest_, packet3, options, 0); |
| 137 | 139 |
| 138 std::string received_data; | 140 std::string received_data; |
| 139 received_data.append(IntToSize(packet1.size())); | 141 received_data.append(IntToSize(packet1.size())); |
| 140 received_data.append(packet1.begin(), packet1.end()); | 142 received_data.append(packet1.begin(), packet1.end()); |
| 141 received_data.append(IntToSize(packet2.size())); | 143 received_data.append(IntToSize(packet2.size())); |
| 142 received_data.append(packet2.begin(), packet2.end()); | 144 received_data.append(packet2.begin(), packet2.end()); |
| 143 received_data.append(IntToSize(packet3.size())); | 145 received_data.append(IntToSize(packet3.size())); |
| 144 received_data.append(packet3.begin(), packet3.end()); | 146 received_data.append(packet3.begin(), packet3.end()); |
| 145 | 147 |
| 146 EXPECT_CALL(sender_, Send(MatchPacketMessage(packet1))) | 148 EXPECT_CALL(sender_, Send(MatchPacketMessage(packet1))) |
| (...skipping 15 matching lines...) Expand all Loading... |
| 162 } | 164 } |
| 163 } | 165 } |
| 164 | 166 |
| 165 // Verify that we can't send data before we've received STUN response | 167 // Verify that we can't send data before we've received STUN response |
| 166 // from the other side. | 168 // from the other side. |
| 167 TEST_F(P2PSocketHostTcpTest, SendDataNoAuth) { | 169 TEST_F(P2PSocketHostTcpTest, SendDataNoAuth) { |
| 168 EXPECT_CALL(sender_, Send( | 170 EXPECT_CALL(sender_, Send( |
| 169 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID)))) | 171 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID)))) |
| 170 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 172 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 171 | 173 |
| 174 talk_base::PacketOptions options; |
| 172 std::vector<char> packet; | 175 std::vector<char> packet; |
| 173 CreateRandomPacket(&packet); | 176 CreateRandomPacket(&packet); |
| 174 socket_host_->Send(dest_, packet, net::DSCP_NO_CHANGE, 0); | 177 socket_host_->Send(dest_, packet, options, 0); |
| 175 | 178 |
| 176 EXPECT_EQ(0U, sent_data_.size()); | 179 EXPECT_EQ(0U, sent_data_.size()); |
| 177 } | 180 } |
| 178 | 181 |
| 179 // Verify that we can send data after we've received STUN response | 182 // Verify that we can send data after we've received STUN response |
| 180 // from the other side. | 183 // from the other side. |
| 181 TEST_F(P2PSocketHostTcpTest, SendAfterStunRequest) { | 184 TEST_F(P2PSocketHostTcpTest, SendAfterStunRequest) { |
| 182 // Receive packet from |dest_|. | 185 // Receive packet from |dest_|. |
| 183 std::vector<char> request_packet; | 186 std::vector<char> request_packet; |
| 184 CreateStunRequest(&request_packet); | 187 CreateStunRequest(&request_packet); |
| 185 | 188 |
| 186 std::string received_data; | 189 std::string received_data; |
| 187 received_data.append(IntToSize(request_packet.size())); | 190 received_data.append(IntToSize(request_packet.size())); |
| 188 received_data.append(request_packet.begin(), request_packet.end()); | 191 received_data.append(request_packet.begin(), request_packet.end()); |
| 189 | 192 |
| 190 EXPECT_CALL(sender_, Send( | 193 EXPECT_CALL(sender_, Send( |
| 191 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 194 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
| 192 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 195 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 193 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) | 196 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) |
| 194 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 197 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 195 socket_->AppendInputData(&received_data[0], received_data.size()); | 198 socket_->AppendInputData(&received_data[0], received_data.size()); |
| 196 | 199 |
| 200 talk_base::PacketOptions options; |
| 197 // Now we should be able to send any data to |dest_|. | 201 // Now we should be able to send any data to |dest_|. |
| 198 std::vector<char> packet; | 202 std::vector<char> packet; |
| 199 CreateRandomPacket(&packet); | 203 CreateRandomPacket(&packet); |
| 200 socket_host_->Send(dest_, packet, net::DSCP_NO_CHANGE, 0); | 204 socket_host_->Send(dest_, packet, options, 0); |
| 201 | 205 |
| 202 std::string expected_data; | 206 std::string expected_data; |
| 203 expected_data.append(IntToSize(packet.size())); | 207 expected_data.append(IntToSize(packet.size())); |
| 204 expected_data.append(packet.begin(), packet.end()); | 208 expected_data.append(packet.begin(), packet.end()); |
| 205 | 209 |
| 206 EXPECT_EQ(expected_data, sent_data_); | 210 EXPECT_EQ(expected_data, sent_data_); |
| 207 } | 211 } |
| 208 | 212 |
| 209 // Verify that asynchronous writes are handled correctly. | 213 // Verify that asynchronous writes are handled correctly. |
| 210 TEST_F(P2PSocketHostTcpTest, AsyncWrites) { | 214 TEST_F(P2PSocketHostTcpTest, AsyncWrites) { |
| 211 base::MessageLoop message_loop; | 215 base::MessageLoop message_loop; |
| 212 | 216 |
| 213 socket_->set_async_write(true); | 217 socket_->set_async_write(true); |
| 214 | 218 |
| 215 EXPECT_CALL(sender_, Send( | 219 EXPECT_CALL(sender_, Send( |
| 216 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 220 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
| 217 .Times(2) | 221 .Times(2) |
| 218 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); | 222 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); |
| 219 | 223 |
| 224 talk_base::PacketOptions options; |
| 220 std::vector<char> packet1; | 225 std::vector<char> packet1; |
| 221 CreateStunRequest(&packet1); | 226 CreateStunRequest(&packet1); |
| 222 | 227 |
| 223 socket_host_->Send(dest_, packet1, net::DSCP_NO_CHANGE, 0); | 228 socket_host_->Send(dest_, packet1, options, 0); |
| 224 | 229 |
| 225 std::vector<char> packet2; | 230 std::vector<char> packet2; |
| 226 CreateStunResponse(&packet2); | 231 CreateStunResponse(&packet2); |
| 227 socket_host_->Send(dest_, packet2, net::DSCP_NO_CHANGE, 0); | 232 socket_host_->Send(dest_, packet2, options, 0); |
| 228 | 233 |
| 229 message_loop.RunUntilIdle(); | 234 message_loop.RunUntilIdle(); |
| 230 | 235 |
| 231 std::string expected_data; | 236 std::string expected_data; |
| 232 expected_data.append(IntToSize(packet1.size())); | 237 expected_data.append(IntToSize(packet1.size())); |
| 233 expected_data.append(packet1.begin(), packet1.end()); | 238 expected_data.append(packet1.begin(), packet1.end()); |
| 234 expected_data.append(IntToSize(packet2.size())); | 239 expected_data.append(IntToSize(packet2.size())); |
| 235 expected_data.append(packet2.begin(), packet2.end()); | 240 expected_data.append(packet2.begin(), packet2.end()); |
| 236 | 241 |
| 237 EXPECT_EQ(expected_data, sent_data_); | 242 EXPECT_EQ(expected_data, sent_data_); |
| 238 } | 243 } |
| 239 | 244 |
| 240 // Verify that we can send STUN message and that they are formatted | 245 // Verify that we can send STUN message and that they are formatted |
| 241 // properly. | 246 // properly. |
| 242 TEST_F(P2PSocketHostStunTcpTest, SendStunNoAuth) { | 247 TEST_F(P2PSocketHostStunTcpTest, SendStunNoAuth) { |
| 243 EXPECT_CALL(sender_, Send( | 248 EXPECT_CALL(sender_, Send( |
| 244 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 249 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
| 245 .Times(3) | 250 .Times(3) |
| 246 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); | 251 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); |
| 247 | 252 |
| 253 talk_base::PacketOptions options; |
| 248 std::vector<char> packet1; | 254 std::vector<char> packet1; |
| 249 CreateStunRequest(&packet1); | 255 CreateStunRequest(&packet1); |
| 250 socket_host_->Send(dest_, packet1, net::DSCP_NO_CHANGE, 0); | 256 socket_host_->Send(dest_, packet1, options, 0); |
| 251 | 257 |
| 252 std::vector<char> packet2; | 258 std::vector<char> packet2; |
| 253 CreateStunResponse(&packet2); | 259 CreateStunResponse(&packet2); |
| 254 socket_host_->Send(dest_, packet2, net::DSCP_NO_CHANGE, 0); | 260 socket_host_->Send(dest_, packet2, options, 0); |
| 255 | 261 |
| 256 std::vector<char> packet3; | 262 std::vector<char> packet3; |
| 257 CreateStunError(&packet3); | 263 CreateStunError(&packet3); |
| 258 socket_host_->Send(dest_, packet3, net::DSCP_NO_CHANGE, 0); | 264 socket_host_->Send(dest_, packet3, options, 0); |
| 259 | 265 |
| 260 std::string expected_data; | 266 std::string expected_data; |
| 261 expected_data.append(packet1.begin(), packet1.end()); | 267 expected_data.append(packet1.begin(), packet1.end()); |
| 262 expected_data.append(packet2.begin(), packet2.end()); | 268 expected_data.append(packet2.begin(), packet2.end()); |
| 263 expected_data.append(packet3.begin(), packet3.end()); | 269 expected_data.append(packet3.begin(), packet3.end()); |
| 264 | 270 |
| 265 EXPECT_EQ(expected_data, sent_data_); | 271 EXPECT_EQ(expected_data, sent_data_); |
| 266 } | 272 } |
| 267 | 273 |
| 268 // Verify that we can receive STUN messages from the socket, and that | 274 // Verify that we can receive STUN messages from the socket, and that |
| 269 // the messages are parsed properly. | 275 // the messages are parsed properly. |
| 270 TEST_F(P2PSocketHostStunTcpTest, ReceiveStun) { | 276 TEST_F(P2PSocketHostStunTcpTest, ReceiveStun) { |
| 271 EXPECT_CALL(sender_, Send( | 277 EXPECT_CALL(sender_, Send( |
| 272 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 278 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
| 273 .Times(3) | 279 .Times(3) |
| 274 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); | 280 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); |
| 275 | 281 |
| 282 talk_base::PacketOptions options; |
| 276 std::vector<char> packet1; | 283 std::vector<char> packet1; |
| 277 CreateStunRequest(&packet1); | 284 CreateStunRequest(&packet1); |
| 278 socket_host_->Send(dest_, packet1, net::DSCP_NO_CHANGE, 0); | 285 socket_host_->Send(dest_, packet1, options, 0); |
| 279 | 286 |
| 280 std::vector<char> packet2; | 287 std::vector<char> packet2; |
| 281 CreateStunResponse(&packet2); | 288 CreateStunResponse(&packet2); |
| 282 socket_host_->Send(dest_, packet2, net::DSCP_NO_CHANGE, 0); | 289 socket_host_->Send(dest_, packet2, options, 0); |
| 283 | 290 |
| 284 std::vector<char> packet3; | 291 std::vector<char> packet3; |
| 285 CreateStunError(&packet3); | 292 CreateStunError(&packet3); |
| 286 socket_host_->Send(dest_, packet3, net::DSCP_NO_CHANGE, 0); | 293 socket_host_->Send(dest_, packet3, options, 0); |
| 287 | 294 |
| 288 std::string received_data; | 295 std::string received_data; |
| 289 received_data.append(packet1.begin(), packet1.end()); | 296 received_data.append(packet1.begin(), packet1.end()); |
| 290 received_data.append(packet2.begin(), packet2.end()); | 297 received_data.append(packet2.begin(), packet2.end()); |
| 291 received_data.append(packet3.begin(), packet3.end()); | 298 received_data.append(packet3.begin(), packet3.end()); |
| 292 | 299 |
| 293 EXPECT_CALL(sender_, Send(MatchPacketMessage(packet1))) | 300 EXPECT_CALL(sender_, Send(MatchPacketMessage(packet1))) |
| 294 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 301 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 295 EXPECT_CALL(sender_, Send(MatchPacketMessage(packet2))) | 302 EXPECT_CALL(sender_, Send(MatchPacketMessage(packet2))) |
| 296 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 303 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 309 } | 316 } |
| 310 } | 317 } |
| 311 | 318 |
| 312 // Verify that we can't send data before we've received STUN response | 319 // Verify that we can't send data before we've received STUN response |
| 313 // from the other side. | 320 // from the other side. |
| 314 TEST_F(P2PSocketHostStunTcpTest, SendDataNoAuth) { | 321 TEST_F(P2PSocketHostStunTcpTest, SendDataNoAuth) { |
| 315 EXPECT_CALL(sender_, Send( | 322 EXPECT_CALL(sender_, Send( |
| 316 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID)))) | 323 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID)))) |
| 317 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 324 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 318 | 325 |
| 326 talk_base::PacketOptions options; |
| 319 std::vector<char> packet; | 327 std::vector<char> packet; |
| 320 CreateRandomPacket(&packet); | 328 CreateRandomPacket(&packet); |
| 321 socket_host_->Send(dest_, packet, net::DSCP_NO_CHANGE, 0); | 329 socket_host_->Send(dest_, packet, options, 0); |
| 322 | 330 |
| 323 EXPECT_EQ(0U, sent_data_.size()); | 331 EXPECT_EQ(0U, sent_data_.size()); |
| 324 } | 332 } |
| 325 | 333 |
| 326 // Verify that asynchronous writes are handled correctly. | 334 // Verify that asynchronous writes are handled correctly. |
| 327 TEST_F(P2PSocketHostStunTcpTest, AsyncWrites) { | 335 TEST_F(P2PSocketHostStunTcpTest, AsyncWrites) { |
| 328 base::MessageLoop message_loop; | 336 base::MessageLoop message_loop; |
| 329 | 337 |
| 330 socket_->set_async_write(true); | 338 socket_->set_async_write(true); |
| 331 | 339 |
| 332 EXPECT_CALL(sender_, Send( | 340 EXPECT_CALL(sender_, Send( |
| 333 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 341 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
| 334 .Times(2) | 342 .Times(2) |
| 335 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); | 343 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); |
| 336 | 344 |
| 345 talk_base::PacketOptions options; |
| 337 std::vector<char> packet1; | 346 std::vector<char> packet1; |
| 338 CreateStunRequest(&packet1); | 347 CreateStunRequest(&packet1); |
| 339 socket_host_->Send(dest_, packet1, net::DSCP_NO_CHANGE,0); | 348 socket_host_->Send(dest_, packet1, options, 0); |
| 340 | 349 |
| 341 std::vector<char> packet2; | 350 std::vector<char> packet2; |
| 342 CreateStunResponse(&packet2); | 351 CreateStunResponse(&packet2); |
| 343 socket_host_->Send(dest_, packet2, net::DSCP_NO_CHANGE, 0); | 352 socket_host_->Send(dest_, packet2, options, 0); |
| 344 | 353 |
| 345 message_loop.RunUntilIdle(); | 354 message_loop.RunUntilIdle(); |
| 346 | 355 |
| 347 std::string expected_data; | 356 std::string expected_data; |
| 348 expected_data.append(packet1.begin(), packet1.end()); | 357 expected_data.append(packet1.begin(), packet1.end()); |
| 349 expected_data.append(packet2.begin(), packet2.end()); | 358 expected_data.append(packet2.begin(), packet2.end()); |
| 350 | 359 |
| 351 EXPECT_EQ(expected_data, sent_data_); | 360 EXPECT_EQ(expected_data, sent_data_); |
| 352 } | 361 } |
| 353 | 362 |
| 354 } // namespace content | 363 } // namespace content |
| OLD | NEW |