| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <memory> |
| 11 #include <string> | 12 #include <string> |
| 12 | 13 |
| 13 #include "webrtc/p2p/base/relayserver.h" | 14 #include "webrtc/p2p/base/relayserver.h" |
| 14 #include "webrtc/base/gunit.h" | 15 #include "webrtc/base/gunit.h" |
| 15 #include "webrtc/base/helpers.h" | 16 #include "webrtc/base/helpers.h" |
| 16 #include "webrtc/base/logging.h" | 17 #include "webrtc/base/logging.h" |
| 17 #include "webrtc/base/physicalsocketserver.h" | 18 #include "webrtc/base/physicalsocketserver.h" |
| 18 #include "webrtc/base/socketaddress.h" | 19 #include "webrtc/base/socketaddress.h" |
| 19 #include "webrtc/base/ssladapter.h" | 20 #include "webrtc/base/ssladapter.h" |
| 20 #include "webrtc/base/testclient.h" | 21 #include "webrtc/base/testclient.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 server_->AddExternalSocket( | 54 server_->AddExternalSocket( |
| 54 rtc::AsyncUDPSocket::Create(ss_.get(), server_ext_addr)); | 55 rtc::AsyncUDPSocket::Create(ss_.get(), server_ext_addr)); |
| 55 | 56 |
| 56 client1_.reset(new rtc::TestClient( | 57 client1_.reset(new rtc::TestClient( |
| 57 rtc::AsyncUDPSocket::Create(ss_.get(), client1_addr))); | 58 rtc::AsyncUDPSocket::Create(ss_.get(), client1_addr))); |
| 58 client2_.reset(new rtc::TestClient( | 59 client2_.reset(new rtc::TestClient( |
| 59 rtc::AsyncUDPSocket::Create(ss_.get(), client2_addr))); | 60 rtc::AsyncUDPSocket::Create(ss_.get(), client2_addr))); |
| 60 } | 61 } |
| 61 | 62 |
| 62 void Allocate() { | 63 void Allocate() { |
| 63 rtc::scoped_ptr<StunMessage> req( | 64 std::unique_ptr<StunMessage> req(CreateStunMessage(STUN_ALLOCATE_REQUEST)); |
| 64 CreateStunMessage(STUN_ALLOCATE_REQUEST)); | |
| 65 AddUsernameAttr(req.get(), username_); | 65 AddUsernameAttr(req.get(), username_); |
| 66 AddLifetimeAttr(req.get(), LIFETIME); | 66 AddLifetimeAttr(req.get(), LIFETIME); |
| 67 Send1(req.get()); | 67 Send1(req.get()); |
| 68 delete Receive1(); | 68 delete Receive1(); |
| 69 } | 69 } |
| 70 void Bind() { | 70 void Bind() { |
| 71 rtc::scoped_ptr<StunMessage> req( | 71 std::unique_ptr<StunMessage> req(CreateStunMessage(STUN_BINDING_REQUEST)); |
| 72 CreateStunMessage(STUN_BINDING_REQUEST)); | |
| 73 AddUsernameAttr(req.get(), username_); | 72 AddUsernameAttr(req.get(), username_); |
| 74 Send2(req.get()); | 73 Send2(req.get()); |
| 75 delete Receive1(); | 74 delete Receive1(); |
| 76 } | 75 } |
| 77 | 76 |
| 78 void Send1(const StunMessage* msg) { | 77 void Send1(const StunMessage* msg) { |
| 79 rtc::ByteBufferWriter buf; | 78 rtc::ByteBufferWriter buf; |
| 80 msg->Write(&buf); | 79 msg->Write(&buf); |
| 81 SendRaw1(buf.Data(), static_cast<int>(buf.Length())); | 80 SendRaw1(buf.Data(), static_cast<int>(buf.Length())); |
| 82 } | 81 } |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 msg->AddAttribute(attr); | 164 msg->AddAttribute(attr); |
| 166 } | 165 } |
| 167 static void AddDestinationAttr(StunMessage* msg, const SocketAddress& addr) { | 166 static void AddDestinationAttr(StunMessage* msg, const SocketAddress& addr) { |
| 168 StunAddressAttribute* attr = | 167 StunAddressAttribute* attr = |
| 169 StunAttribute::CreateAddress(STUN_ATTR_DESTINATION_ADDRESS); | 168 StunAttribute::CreateAddress(STUN_ATTR_DESTINATION_ADDRESS); |
| 170 attr->SetIP(addr.ipaddr()); | 169 attr->SetIP(addr.ipaddr()); |
| 171 attr->SetPort(addr.port()); | 170 attr->SetPort(addr.port()); |
| 172 msg->AddAttribute(attr); | 171 msg->AddAttribute(attr); |
| 173 } | 172 } |
| 174 | 173 |
| 175 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_; | 174 std::unique_ptr<rtc::PhysicalSocketServer> pss_; |
| 176 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_; | 175 std::unique_ptr<rtc::VirtualSocketServer> ss_; |
| 177 rtc::SocketServerScope ss_scope_; | 176 rtc::SocketServerScope ss_scope_; |
| 178 rtc::scoped_ptr<RelayServer> server_; | 177 std::unique_ptr<RelayServer> server_; |
| 179 rtc::scoped_ptr<rtc::TestClient> client1_; | 178 std::unique_ptr<rtc::TestClient> client1_; |
| 180 rtc::scoped_ptr<rtc::TestClient> client2_; | 179 std::unique_ptr<rtc::TestClient> client2_; |
| 181 std::string username_; | 180 std::string username_; |
| 182 std::string password_; | 181 std::string password_; |
| 183 }; | 182 }; |
| 184 | 183 |
| 185 // Send a complete nonsense message and verify that it is eaten. | 184 // Send a complete nonsense message and verify that it is eaten. |
| 186 TEST_F(RelayServerTest, TestBadRequest) { | 185 TEST_F(RelayServerTest, TestBadRequest) { |
| 187 SendRaw1(bad, static_cast<int>(strlen(bad))); | 186 SendRaw1(bad, static_cast<int>(strlen(bad))); |
| 188 ASSERT_TRUE(Receive1Fails()); | 187 ASSERT_TRUE(Receive1Fails()); |
| 189 } | 188 } |
| 190 | 189 |
| 191 // Send an allocate request without a username and verify it is rejected. | 190 // Send an allocate request without a username and verify it is rejected. |
| 192 TEST_F(RelayServerTest, TestAllocateNoUsername) { | 191 TEST_F(RelayServerTest, TestAllocateNoUsername) { |
| 193 rtc::scoped_ptr<StunMessage> req( | 192 std::unique_ptr<StunMessage> req(CreateStunMessage(STUN_ALLOCATE_REQUEST)), |
| 194 CreateStunMessage(STUN_ALLOCATE_REQUEST)), res; | 193 res; |
| 195 | 194 |
| 196 Send1(req.get()); | 195 Send1(req.get()); |
| 197 res.reset(Receive1()); | 196 res.reset(Receive1()); |
| 198 | 197 |
| 199 ASSERT_TRUE(res); | 198 ASSERT_TRUE(res); |
| 200 EXPECT_EQ(STUN_ALLOCATE_ERROR_RESPONSE, res->type()); | 199 EXPECT_EQ(STUN_ALLOCATE_ERROR_RESPONSE, res->type()); |
| 201 EXPECT_EQ(req->transaction_id(), res->transaction_id()); | 200 EXPECT_EQ(req->transaction_id(), res->transaction_id()); |
| 202 | 201 |
| 203 const StunErrorCodeAttribute* err = res->GetErrorCode(); | 202 const StunErrorCodeAttribute* err = res->GetErrorCode(); |
| 204 ASSERT_TRUE(err != NULL); | 203 ASSERT_TRUE(err != NULL); |
| 205 EXPECT_EQ(4, err->eclass()); | 204 EXPECT_EQ(4, err->eclass()); |
| 206 EXPECT_EQ(32, err->number()); | 205 EXPECT_EQ(32, err->number()); |
| 207 EXPECT_EQ("Missing Username", err->reason()); | 206 EXPECT_EQ("Missing Username", err->reason()); |
| 208 } | 207 } |
| 209 | 208 |
| 210 // Send a binding request and verify that it is rejected. | 209 // Send a binding request and verify that it is rejected. |
| 211 TEST_F(RelayServerTest, TestBindingRequest) { | 210 TEST_F(RelayServerTest, TestBindingRequest) { |
| 212 rtc::scoped_ptr<StunMessage> req( | 211 std::unique_ptr<StunMessage> req(CreateStunMessage(STUN_BINDING_REQUEST)), |
| 213 CreateStunMessage(STUN_BINDING_REQUEST)), res; | 212 res; |
| 214 AddUsernameAttr(req.get(), username_); | 213 AddUsernameAttr(req.get(), username_); |
| 215 | 214 |
| 216 Send1(req.get()); | 215 Send1(req.get()); |
| 217 res.reset(Receive1()); | 216 res.reset(Receive1()); |
| 218 | 217 |
| 219 ASSERT_TRUE(res); | 218 ASSERT_TRUE(res); |
| 220 EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, res->type()); | 219 EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, res->type()); |
| 221 EXPECT_EQ(req->transaction_id(), res->transaction_id()); | 220 EXPECT_EQ(req->transaction_id(), res->transaction_id()); |
| 222 | 221 |
| 223 const StunErrorCodeAttribute* err = res->GetErrorCode(); | 222 const StunErrorCodeAttribute* err = res->GetErrorCode(); |
| 224 ASSERT_TRUE(err != NULL); | 223 ASSERT_TRUE(err != NULL); |
| 225 EXPECT_EQ(6, err->eclass()); | 224 EXPECT_EQ(6, err->eclass()); |
| 226 EXPECT_EQ(0, err->number()); | 225 EXPECT_EQ(0, err->number()); |
| 227 EXPECT_EQ("Operation Not Supported", err->reason()); | 226 EXPECT_EQ("Operation Not Supported", err->reason()); |
| 228 } | 227 } |
| 229 | 228 |
| 230 // Send an allocate request and verify that it is accepted. | 229 // Send an allocate request and verify that it is accepted. |
| 231 TEST_F(RelayServerTest, TestAllocate) { | 230 TEST_F(RelayServerTest, TestAllocate) { |
| 232 rtc::scoped_ptr<StunMessage> req( | 231 std::unique_ptr<StunMessage> req(CreateStunMessage(STUN_ALLOCATE_REQUEST)), |
| 233 CreateStunMessage(STUN_ALLOCATE_REQUEST)), res; | 232 res; |
| 234 AddUsernameAttr(req.get(), username_); | 233 AddUsernameAttr(req.get(), username_); |
| 235 AddLifetimeAttr(req.get(), LIFETIME); | 234 AddLifetimeAttr(req.get(), LIFETIME); |
| 236 | 235 |
| 237 Send1(req.get()); | 236 Send1(req.get()); |
| 238 res.reset(Receive1()); | 237 res.reset(Receive1()); |
| 239 | 238 |
| 240 ASSERT_TRUE(res); | 239 ASSERT_TRUE(res); |
| 241 EXPECT_EQ(STUN_ALLOCATE_RESPONSE, res->type()); | 240 EXPECT_EQ(STUN_ALLOCATE_RESPONSE, res->type()); |
| 242 EXPECT_EQ(req->transaction_id(), res->transaction_id()); | 241 EXPECT_EQ(req->transaction_id(), res->transaction_id()); |
| 243 | 242 |
| 244 const StunAddressAttribute* mapped_addr = | 243 const StunAddressAttribute* mapped_addr = |
| 245 res->GetAddress(STUN_ATTR_MAPPED_ADDRESS); | 244 res->GetAddress(STUN_ATTR_MAPPED_ADDRESS); |
| 246 ASSERT_TRUE(mapped_addr != NULL); | 245 ASSERT_TRUE(mapped_addr != NULL); |
| 247 EXPECT_EQ(1, mapped_addr->family()); | 246 EXPECT_EQ(1, mapped_addr->family()); |
| 248 EXPECT_EQ(server_ext_addr.port(), mapped_addr->port()); | 247 EXPECT_EQ(server_ext_addr.port(), mapped_addr->port()); |
| 249 EXPECT_EQ(server_ext_addr.ipaddr(), mapped_addr->ipaddr()); | 248 EXPECT_EQ(server_ext_addr.ipaddr(), mapped_addr->ipaddr()); |
| 250 | 249 |
| 251 const StunUInt32Attribute* res_lifetime_attr = | 250 const StunUInt32Attribute* res_lifetime_attr = |
| 252 res->GetUInt32(STUN_ATTR_LIFETIME); | 251 res->GetUInt32(STUN_ATTR_LIFETIME); |
| 253 ASSERT_TRUE(res_lifetime_attr != NULL); | 252 ASSERT_TRUE(res_lifetime_attr != NULL); |
| 254 EXPECT_EQ(LIFETIME, res_lifetime_attr->value()); | 253 EXPECT_EQ(LIFETIME, res_lifetime_attr->value()); |
| 255 } | 254 } |
| 256 | 255 |
| 257 // Send a second allocate request and verify that it is also accepted, though | 256 // Send a second allocate request and verify that it is also accepted, though |
| 258 // the lifetime should be ignored. | 257 // the lifetime should be ignored. |
| 259 TEST_F(RelayServerTest, TestReallocate) { | 258 TEST_F(RelayServerTest, TestReallocate) { |
| 260 Allocate(); | 259 Allocate(); |
| 261 | 260 |
| 262 rtc::scoped_ptr<StunMessage> req( | 261 std::unique_ptr<StunMessage> req(CreateStunMessage(STUN_ALLOCATE_REQUEST)), |
| 263 CreateStunMessage(STUN_ALLOCATE_REQUEST)), res; | 262 res; |
| 264 AddMagicCookieAttr(req.get()); | 263 AddMagicCookieAttr(req.get()); |
| 265 AddUsernameAttr(req.get(), username_); | 264 AddUsernameAttr(req.get(), username_); |
| 266 | 265 |
| 267 Send1(req.get()); | 266 Send1(req.get()); |
| 268 res.reset(Receive1()); | 267 res.reset(Receive1()); |
| 269 | 268 |
| 270 ASSERT_TRUE(res); | 269 ASSERT_TRUE(res); |
| 271 EXPECT_EQ(STUN_ALLOCATE_RESPONSE, res->type()); | 270 EXPECT_EQ(STUN_ALLOCATE_RESPONSE, res->type()); |
| 272 EXPECT_EQ(req->transaction_id(), res->transaction_id()); | 271 EXPECT_EQ(req->transaction_id(), res->transaction_id()); |
| 273 | 272 |
| 274 const StunAddressAttribute* mapped_addr = | 273 const StunAddressAttribute* mapped_addr = |
| 275 res->GetAddress(STUN_ATTR_MAPPED_ADDRESS); | 274 res->GetAddress(STUN_ATTR_MAPPED_ADDRESS); |
| 276 ASSERT_TRUE(mapped_addr != NULL); | 275 ASSERT_TRUE(mapped_addr != NULL); |
| 277 EXPECT_EQ(1, mapped_addr->family()); | 276 EXPECT_EQ(1, mapped_addr->family()); |
| 278 EXPECT_EQ(server_ext_addr.port(), mapped_addr->port()); | 277 EXPECT_EQ(server_ext_addr.port(), mapped_addr->port()); |
| 279 EXPECT_EQ(server_ext_addr.ipaddr(), mapped_addr->ipaddr()); | 278 EXPECT_EQ(server_ext_addr.ipaddr(), mapped_addr->ipaddr()); |
| 280 | 279 |
| 281 const StunUInt32Attribute* lifetime_attr = | 280 const StunUInt32Attribute* lifetime_attr = |
| 282 res->GetUInt32(STUN_ATTR_LIFETIME); | 281 res->GetUInt32(STUN_ATTR_LIFETIME); |
| 283 ASSERT_TRUE(lifetime_attr != NULL); | 282 ASSERT_TRUE(lifetime_attr != NULL); |
| 284 EXPECT_EQ(LIFETIME, lifetime_attr->value()); | 283 EXPECT_EQ(LIFETIME, lifetime_attr->value()); |
| 285 } | 284 } |
| 286 | 285 |
| 287 // Send a request from another client and see that it arrives at the first | 286 // Send a request from another client and see that it arrives at the first |
| 288 // client in the binding. | 287 // client in the binding. |
| 289 TEST_F(RelayServerTest, TestRemoteBind) { | 288 TEST_F(RelayServerTest, TestRemoteBind) { |
| 290 Allocate(); | 289 Allocate(); |
| 291 | 290 |
| 292 rtc::scoped_ptr<StunMessage> req( | 291 std::unique_ptr<StunMessage> req(CreateStunMessage(STUN_BINDING_REQUEST)), |
| 293 CreateStunMessage(STUN_BINDING_REQUEST)), res; | 292 res; |
| 294 AddUsernameAttr(req.get(), username_); | 293 AddUsernameAttr(req.get(), username_); |
| 295 | 294 |
| 296 Send2(req.get()); | 295 Send2(req.get()); |
| 297 res.reset(Receive1()); | 296 res.reset(Receive1()); |
| 298 | 297 |
| 299 ASSERT_TRUE(res); | 298 ASSERT_TRUE(res); |
| 300 EXPECT_EQ(STUN_DATA_INDICATION, res->type()); | 299 EXPECT_EQ(STUN_DATA_INDICATION, res->type()); |
| 301 | 300 |
| 302 const StunByteStringAttribute* recv_data = | 301 const StunByteStringAttribute* recv_data = |
| 303 res->GetByteString(STUN_ATTR_DATA); | 302 res->GetByteString(STUN_ATTR_DATA); |
| 304 ASSERT_TRUE(recv_data != NULL); | 303 ASSERT_TRUE(recv_data != NULL); |
| 305 | 304 |
| 306 rtc::ByteBufferReader buf(recv_data->bytes(), recv_data->length()); | 305 rtc::ByteBufferReader buf(recv_data->bytes(), recv_data->length()); |
| 307 rtc::scoped_ptr<StunMessage> res2(new StunMessage()); | 306 std::unique_ptr<StunMessage> res2(new StunMessage()); |
| 308 EXPECT_TRUE(res2->Read(&buf)); | 307 EXPECT_TRUE(res2->Read(&buf)); |
| 309 EXPECT_EQ(STUN_BINDING_REQUEST, res2->type()); | 308 EXPECT_EQ(STUN_BINDING_REQUEST, res2->type()); |
| 310 EXPECT_EQ(req->transaction_id(), res2->transaction_id()); | 309 EXPECT_EQ(req->transaction_id(), res2->transaction_id()); |
| 311 | 310 |
| 312 const StunAddressAttribute* src_addr = | 311 const StunAddressAttribute* src_addr = |
| 313 res->GetAddress(STUN_ATTR_SOURCE_ADDRESS2); | 312 res->GetAddress(STUN_ATTR_SOURCE_ADDRESS2); |
| 314 ASSERT_TRUE(src_addr != NULL); | 313 ASSERT_TRUE(src_addr != NULL); |
| 315 EXPECT_EQ(1, src_addr->family()); | 314 EXPECT_EQ(1, src_addr->family()); |
| 316 EXPECT_EQ(client2_addr.ipaddr(), src_addr->ipaddr()); | 315 EXPECT_EQ(client2_addr.ipaddr(), src_addr->ipaddr()); |
| 317 EXPECT_EQ(client2_addr.port(), src_addr->port()); | 316 EXPECT_EQ(client2_addr.port(), src_addr->port()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 328 SendRaw1(bad, static_cast<int>(strlen(bad))); | 327 SendRaw1(bad, static_cast<int>(strlen(bad))); |
| 329 EXPECT_TRUE(Receive1Fails()); | 328 EXPECT_TRUE(Receive1Fails()); |
| 330 EXPECT_TRUE(Receive2Fails()); | 329 EXPECT_TRUE(Receive2Fails()); |
| 331 } | 330 } |
| 332 | 331 |
| 333 // Send a send request without a username and verify it is rejected. | 332 // Send a send request without a username and verify it is rejected. |
| 334 TEST_F(RelayServerTest, TestSendRequestMissingUsername) { | 333 TEST_F(RelayServerTest, TestSendRequestMissingUsername) { |
| 335 Allocate(); | 334 Allocate(); |
| 336 Bind(); | 335 Bind(); |
| 337 | 336 |
| 338 rtc::scoped_ptr<StunMessage> req( | 337 std::unique_ptr<StunMessage> req(CreateStunMessage(STUN_SEND_REQUEST)), res; |
| 339 CreateStunMessage(STUN_SEND_REQUEST)), res; | |
| 340 AddMagicCookieAttr(req.get()); | 338 AddMagicCookieAttr(req.get()); |
| 341 | 339 |
| 342 Send1(req.get()); | 340 Send1(req.get()); |
| 343 res.reset(Receive1()); | 341 res.reset(Receive1()); |
| 344 | 342 |
| 345 ASSERT_TRUE(res); | 343 ASSERT_TRUE(res); |
| 346 EXPECT_EQ(STUN_SEND_ERROR_RESPONSE, res->type()); | 344 EXPECT_EQ(STUN_SEND_ERROR_RESPONSE, res->type()); |
| 347 EXPECT_EQ(req->transaction_id(), res->transaction_id()); | 345 EXPECT_EQ(req->transaction_id(), res->transaction_id()); |
| 348 | 346 |
| 349 const StunErrorCodeAttribute* err = res->GetErrorCode(); | 347 const StunErrorCodeAttribute* err = res->GetErrorCode(); |
| 350 ASSERT_TRUE(err != NULL); | 348 ASSERT_TRUE(err != NULL); |
| 351 EXPECT_EQ(4, err->eclass()); | 349 EXPECT_EQ(4, err->eclass()); |
| 352 EXPECT_EQ(32, err->number()); | 350 EXPECT_EQ(32, err->number()); |
| 353 EXPECT_EQ("Missing Username", err->reason()); | 351 EXPECT_EQ("Missing Username", err->reason()); |
| 354 } | 352 } |
| 355 | 353 |
| 356 // Send a send request with the wrong username and verify it is rejected. | 354 // Send a send request with the wrong username and verify it is rejected. |
| 357 TEST_F(RelayServerTest, TestSendRequestBadUsername) { | 355 TEST_F(RelayServerTest, TestSendRequestBadUsername) { |
| 358 Allocate(); | 356 Allocate(); |
| 359 Bind(); | 357 Bind(); |
| 360 | 358 |
| 361 rtc::scoped_ptr<StunMessage> req( | 359 std::unique_ptr<StunMessage> req(CreateStunMessage(STUN_SEND_REQUEST)), res; |
| 362 CreateStunMessage(STUN_SEND_REQUEST)), res; | |
| 363 AddMagicCookieAttr(req.get()); | 360 AddMagicCookieAttr(req.get()); |
| 364 AddUsernameAttr(req.get(), "foobarbizbaz"); | 361 AddUsernameAttr(req.get(), "foobarbizbaz"); |
| 365 | 362 |
| 366 Send1(req.get()); | 363 Send1(req.get()); |
| 367 res.reset(Receive1()); | 364 res.reset(Receive1()); |
| 368 | 365 |
| 369 ASSERT_TRUE(res); | 366 ASSERT_TRUE(res); |
| 370 EXPECT_EQ(STUN_SEND_ERROR_RESPONSE, res->type()); | 367 EXPECT_EQ(STUN_SEND_ERROR_RESPONSE, res->type()); |
| 371 EXPECT_EQ(req->transaction_id(), res->transaction_id()); | 368 EXPECT_EQ(req->transaction_id(), res->transaction_id()); |
| 372 | 369 |
| 373 const StunErrorCodeAttribute* err = res->GetErrorCode(); | 370 const StunErrorCodeAttribute* err = res->GetErrorCode(); |
| 374 ASSERT_TRUE(err != NULL); | 371 ASSERT_TRUE(err != NULL); |
| 375 EXPECT_EQ(4, err->eclass()); | 372 EXPECT_EQ(4, err->eclass()); |
| 376 EXPECT_EQ(30, err->number()); | 373 EXPECT_EQ(30, err->number()); |
| 377 EXPECT_EQ("Stale Credentials", err->reason()); | 374 EXPECT_EQ("Stale Credentials", err->reason()); |
| 378 } | 375 } |
| 379 | 376 |
| 380 // Send a send request without a destination address and verify that it is | 377 // Send a send request without a destination address and verify that it is |
| 381 // rejected. | 378 // rejected. |
| 382 TEST_F(RelayServerTest, TestSendRequestNoDestinationAddress) { | 379 TEST_F(RelayServerTest, TestSendRequestNoDestinationAddress) { |
| 383 Allocate(); | 380 Allocate(); |
| 384 Bind(); | 381 Bind(); |
| 385 | 382 |
| 386 rtc::scoped_ptr<StunMessage> req( | 383 std::unique_ptr<StunMessage> req(CreateStunMessage(STUN_SEND_REQUEST)), res; |
| 387 CreateStunMessage(STUN_SEND_REQUEST)), res; | |
| 388 AddMagicCookieAttr(req.get()); | 384 AddMagicCookieAttr(req.get()); |
| 389 AddUsernameAttr(req.get(), username_); | 385 AddUsernameAttr(req.get(), username_); |
| 390 | 386 |
| 391 Send1(req.get()); | 387 Send1(req.get()); |
| 392 res.reset(Receive1()); | 388 res.reset(Receive1()); |
| 393 | 389 |
| 394 ASSERT_TRUE(res); | 390 ASSERT_TRUE(res); |
| 395 EXPECT_EQ(STUN_SEND_ERROR_RESPONSE, res->type()); | 391 EXPECT_EQ(STUN_SEND_ERROR_RESPONSE, res->type()); |
| 396 EXPECT_EQ(req->transaction_id(), res->transaction_id()); | 392 EXPECT_EQ(req->transaction_id(), res->transaction_id()); |
| 397 | 393 |
| 398 const StunErrorCodeAttribute* err = res->GetErrorCode(); | 394 const StunErrorCodeAttribute* err = res->GetErrorCode(); |
| 399 ASSERT_TRUE(err != NULL); | 395 ASSERT_TRUE(err != NULL); |
| 400 EXPECT_EQ(4, err->eclass()); | 396 EXPECT_EQ(4, err->eclass()); |
| 401 EXPECT_EQ(0, err->number()); | 397 EXPECT_EQ(0, err->number()); |
| 402 EXPECT_EQ("Bad Request", err->reason()); | 398 EXPECT_EQ("Bad Request", err->reason()); |
| 403 } | 399 } |
| 404 | 400 |
| 405 // Send a send request without data and verify that it is rejected. | 401 // Send a send request without data and verify that it is rejected. |
| 406 TEST_F(RelayServerTest, TestSendRequestNoData) { | 402 TEST_F(RelayServerTest, TestSendRequestNoData) { |
| 407 Allocate(); | 403 Allocate(); |
| 408 Bind(); | 404 Bind(); |
| 409 | 405 |
| 410 rtc::scoped_ptr<StunMessage> req( | 406 std::unique_ptr<StunMessage> req(CreateStunMessage(STUN_SEND_REQUEST)), res; |
| 411 CreateStunMessage(STUN_SEND_REQUEST)), res; | |
| 412 AddMagicCookieAttr(req.get()); | 407 AddMagicCookieAttr(req.get()); |
| 413 AddUsernameAttr(req.get(), username_); | 408 AddUsernameAttr(req.get(), username_); |
| 414 AddDestinationAttr(req.get(), client2_addr); | 409 AddDestinationAttr(req.get(), client2_addr); |
| 415 | 410 |
| 416 Send1(req.get()); | 411 Send1(req.get()); |
| 417 res.reset(Receive1()); | 412 res.reset(Receive1()); |
| 418 | 413 |
| 419 ASSERT_TRUE(res); | 414 ASSERT_TRUE(res); |
| 420 EXPECT_EQ(STUN_SEND_ERROR_RESPONSE, res->type()); | 415 EXPECT_EQ(STUN_SEND_ERROR_RESPONSE, res->type()); |
| 421 EXPECT_EQ(req->transaction_id(), res->transaction_id()); | 416 EXPECT_EQ(req->transaction_id(), res->transaction_id()); |
| 422 | 417 |
| 423 const StunErrorCodeAttribute* err = res->GetErrorCode(); | 418 const StunErrorCodeAttribute* err = res->GetErrorCode(); |
| 424 ASSERT_TRUE(err != NULL); | 419 ASSERT_TRUE(err != NULL); |
| 425 EXPECT_EQ(4, err->eclass()); | 420 EXPECT_EQ(4, err->eclass()); |
| 426 EXPECT_EQ(00, err->number()); | 421 EXPECT_EQ(00, err->number()); |
| 427 EXPECT_EQ("Bad Request", err->reason()); | 422 EXPECT_EQ("Bad Request", err->reason()); |
| 428 } | 423 } |
| 429 | 424 |
| 430 // Send a binding request after an allocate and verify that it is rejected. | 425 // Send a binding request after an allocate and verify that it is rejected. |
| 431 TEST_F(RelayServerTest, TestSendRequestWrongType) { | 426 TEST_F(RelayServerTest, TestSendRequestWrongType) { |
| 432 Allocate(); | 427 Allocate(); |
| 433 Bind(); | 428 Bind(); |
| 434 | 429 |
| 435 rtc::scoped_ptr<StunMessage> req( | 430 std::unique_ptr<StunMessage> req(CreateStunMessage(STUN_BINDING_REQUEST)), |
| 436 CreateStunMessage(STUN_BINDING_REQUEST)), res; | 431 res; |
| 437 AddMagicCookieAttr(req.get()); | 432 AddMagicCookieAttr(req.get()); |
| 438 AddUsernameAttr(req.get(), username_); | 433 AddUsernameAttr(req.get(), username_); |
| 439 | 434 |
| 440 Send1(req.get()); | 435 Send1(req.get()); |
| 441 res.reset(Receive1()); | 436 res.reset(Receive1()); |
| 442 | 437 |
| 443 ASSERT_TRUE(res); | 438 ASSERT_TRUE(res); |
| 444 EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, res->type()); | 439 EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, res->type()); |
| 445 EXPECT_EQ(req->transaction_id(), res->transaction_id()); | 440 EXPECT_EQ(req->transaction_id(), res->transaction_id()); |
| 446 | 441 |
| 447 const StunErrorCodeAttribute* err = res->GetErrorCode(); | 442 const StunErrorCodeAttribute* err = res->GetErrorCode(); |
| 448 ASSERT_TRUE(err != NULL); | 443 ASSERT_TRUE(err != NULL); |
| 449 EXPECT_EQ(6, err->eclass()); | 444 EXPECT_EQ(6, err->eclass()); |
| 450 EXPECT_EQ(0, err->number()); | 445 EXPECT_EQ(0, err->number()); |
| 451 EXPECT_EQ("Operation Not Supported", err->reason()); | 446 EXPECT_EQ("Operation Not Supported", err->reason()); |
| 452 } | 447 } |
| 453 | 448 |
| 454 // Verify that we can send traffic back and forth between the clients after a | 449 // Verify that we can send traffic back and forth between the clients after a |
| 455 // successful allocate and bind. | 450 // successful allocate and bind. |
| 456 TEST_F(RelayServerTest, TestSendRaw) { | 451 TEST_F(RelayServerTest, TestSendRaw) { |
| 457 Allocate(); | 452 Allocate(); |
| 458 Bind(); | 453 Bind(); |
| 459 | 454 |
| 460 for (int i = 0; i < 10; i++) { | 455 for (int i = 0; i < 10; i++) { |
| 461 rtc::scoped_ptr<StunMessage> req( | 456 std::unique_ptr<StunMessage> req(CreateStunMessage(STUN_SEND_REQUEST)), res; |
| 462 CreateStunMessage(STUN_SEND_REQUEST)), res; | |
| 463 AddMagicCookieAttr(req.get()); | 457 AddMagicCookieAttr(req.get()); |
| 464 AddUsernameAttr(req.get(), username_); | 458 AddUsernameAttr(req.get(), username_); |
| 465 AddDestinationAttr(req.get(), client2_addr); | 459 AddDestinationAttr(req.get(), client2_addr); |
| 466 | 460 |
| 467 StunByteStringAttribute* send_data = | 461 StunByteStringAttribute* send_data = |
| 468 StunAttribute::CreateByteString(STUN_ATTR_DATA); | 462 StunAttribute::CreateByteString(STUN_ATTR_DATA); |
| 469 send_data->CopyBytes(msg1); | 463 send_data->CopyBytes(msg1); |
| 470 req->AddAttribute(send_data); | 464 req->AddAttribute(send_data); |
| 471 | 465 |
| 472 Send1(req.get()); | 466 Send1(req.get()); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 494 | 488 |
| 495 // Verify that a binding expires properly, and rejects send requests. | 489 // Verify that a binding expires properly, and rejects send requests. |
| 496 // Flaky, see https://code.google.com/p/webrtc/issues/detail?id=4134 | 490 // Flaky, see https://code.google.com/p/webrtc/issues/detail?id=4134 |
| 497 TEST_F(RelayServerTest, DISABLED_TestExpiration) { | 491 TEST_F(RelayServerTest, DISABLED_TestExpiration) { |
| 498 Allocate(); | 492 Allocate(); |
| 499 Bind(); | 493 Bind(); |
| 500 | 494 |
| 501 // Wait twice the lifetime to make sure the server has expired the binding. | 495 // Wait twice the lifetime to make sure the server has expired the binding. |
| 502 rtc::Thread::Current()->ProcessMessages((LIFETIME * 2) * 1000); | 496 rtc::Thread::Current()->ProcessMessages((LIFETIME * 2) * 1000); |
| 503 | 497 |
| 504 rtc::scoped_ptr<StunMessage> req( | 498 std::unique_ptr<StunMessage> req(CreateStunMessage(STUN_SEND_REQUEST)), res; |
| 505 CreateStunMessage(STUN_SEND_REQUEST)), res; | |
| 506 AddMagicCookieAttr(req.get()); | 499 AddMagicCookieAttr(req.get()); |
| 507 AddUsernameAttr(req.get(), username_); | 500 AddUsernameAttr(req.get(), username_); |
| 508 AddDestinationAttr(req.get(), client2_addr); | 501 AddDestinationAttr(req.get(), client2_addr); |
| 509 | 502 |
| 510 StunByteStringAttribute* data_attr = | 503 StunByteStringAttribute* data_attr = |
| 511 StunAttribute::CreateByteString(STUN_ATTR_DATA); | 504 StunAttribute::CreateByteString(STUN_ATTR_DATA); |
| 512 data_attr->CopyBytes(msg1); | 505 data_attr->CopyBytes(msg1); |
| 513 req->AddAttribute(data_attr); | 506 req->AddAttribute(data_attr); |
| 514 | 507 |
| 515 Send1(req.get()); | 508 Send1(req.get()); |
| 516 res.reset(Receive1()); | 509 res.reset(Receive1()); |
| 517 | 510 |
| 518 ASSERT_TRUE(res.get() != NULL); | 511 ASSERT_TRUE(res.get() != NULL); |
| 519 EXPECT_EQ(STUN_SEND_ERROR_RESPONSE, res->type()); | 512 EXPECT_EQ(STUN_SEND_ERROR_RESPONSE, res->type()); |
| 520 | 513 |
| 521 const StunErrorCodeAttribute* err = res->GetErrorCode(); | 514 const StunErrorCodeAttribute* err = res->GetErrorCode(); |
| 522 ASSERT_TRUE(err != NULL); | 515 ASSERT_TRUE(err != NULL); |
| 523 EXPECT_EQ(6, err->eclass()); | 516 EXPECT_EQ(6, err->eclass()); |
| 524 EXPECT_EQ(0, err->number()); | 517 EXPECT_EQ(0, err->number()); |
| 525 EXPECT_EQ("Operation Not Supported", err->reason()); | 518 EXPECT_EQ("Operation Not Supported", err->reason()); |
| 526 | 519 |
| 527 // Also verify that traffic from the external client is ignored. | 520 // Also verify that traffic from the external client is ignored. |
| 528 SendRaw2(msg2, static_cast<int>(strlen(msg2))); | 521 SendRaw2(msg2, static_cast<int>(strlen(msg2))); |
| 529 EXPECT_TRUE(ReceiveRaw1().empty()); | 522 EXPECT_TRUE(ReceiveRaw1().empty()); |
| 530 } | 523 } |
| OLD | NEW |