Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(344)

Side by Side Diff: webrtc/p2p/base/relayserver_unittest.cc

Issue 2859373003: Refactor TestClient to use std::unique_ptr, and fix VirtualSocketServerTest leaks. (Closed)
Patch Set: Comment fix, 0 -> null. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <memory>
12 #include <string> 12 #include <string>
13 13
14 #include "webrtc/p2p/base/relayserver.h"
15 #include "webrtc/base/gunit.h" 14 #include "webrtc/base/gunit.h"
16 #include "webrtc/base/helpers.h" 15 #include "webrtc/base/helpers.h"
17 #include "webrtc/base/logging.h" 16 #include "webrtc/base/logging.h"
18 #include "webrtc/base/physicalsocketserver.h" 17 #include "webrtc/base/physicalsocketserver.h"
18 #include "webrtc/base/ptr_util.h"
19 #include "webrtc/base/socketaddress.h" 19 #include "webrtc/base/socketaddress.h"
20 #include "webrtc/base/ssladapter.h" 20 #include "webrtc/base/ssladapter.h"
21 #include "webrtc/base/testclient.h" 21 #include "webrtc/base/testclient.h"
22 #include "webrtc/base/thread.h" 22 #include "webrtc/base/thread.h"
23 #include "webrtc/base/virtualsocketserver.h" 23 #include "webrtc/base/virtualsocketserver.h"
24 #include "webrtc/p2p/base/relayserver.h"
24 25
25 using rtc::SocketAddress; 26 using rtc::SocketAddress;
26 using namespace cricket; 27 using namespace cricket;
27 28
28 static const uint32_t LIFETIME = 4; // seconds 29 static const uint32_t LIFETIME = 4; // seconds
29 static const SocketAddress server_int_addr("127.0.0.1", 5000); 30 static const SocketAddress server_int_addr("127.0.0.1", 5000);
30 static const SocketAddress server_ext_addr("127.0.0.1", 5001); 31 static const SocketAddress server_ext_addr("127.0.0.1", 5001);
31 static const SocketAddress client1_addr("127.0.0.1", 6000 + (rand() % 1000)); 32 static const SocketAddress client1_addr("127.0.0.1", 6000 + (rand() % 1000));
32 static const SocketAddress client2_addr("127.0.0.1", 7000 + (rand() % 1000)); 33 static const SocketAddress client2_addr("127.0.0.1", 7000 + (rand() % 1000));
33 static const char* bad = "this is a completely nonsensical message whose only " 34 static const char* bad = "this is a completely nonsensical message whose only "
(...skipping 14 matching lines...) Expand all
48 protected: 49 protected:
49 virtual void SetUp() { 50 virtual void SetUp() {
50 server_.reset(new RelayServer(rtc::Thread::Current())); 51 server_.reset(new RelayServer(rtc::Thread::Current()));
51 52
52 server_->AddInternalSocket( 53 server_->AddInternalSocket(
53 rtc::AsyncUDPSocket::Create(ss_.get(), server_int_addr)); 54 rtc::AsyncUDPSocket::Create(ss_.get(), server_int_addr));
54 server_->AddExternalSocket( 55 server_->AddExternalSocket(
55 rtc::AsyncUDPSocket::Create(ss_.get(), server_ext_addr)); 56 rtc::AsyncUDPSocket::Create(ss_.get(), server_ext_addr));
56 57
57 client1_.reset(new rtc::TestClient( 58 client1_.reset(new rtc::TestClient(
58 rtc::AsyncUDPSocket::Create(ss_.get(), client1_addr))); 59 WrapUnique(rtc::AsyncUDPSocket::Create(ss_.get(), client1_addr))));
59 client2_.reset(new rtc::TestClient( 60 client2_.reset(new rtc::TestClient(
60 rtc::AsyncUDPSocket::Create(ss_.get(), client2_addr))); 61 WrapUnique(rtc::AsyncUDPSocket::Create(ss_.get(), client2_addr))));
61 } 62 }
62 63
63 void Allocate() { 64 void Allocate() {
64 std::unique_ptr<StunMessage> req(CreateStunMessage(STUN_ALLOCATE_REQUEST)); 65 std::unique_ptr<StunMessage> req(CreateStunMessage(STUN_ALLOCATE_REQUEST));
65 AddUsernameAttr(req.get(), username_); 66 AddUsernameAttr(req.get(), username_);
66 AddLifetimeAttr(req.get(), LIFETIME); 67 AddLifetimeAttr(req.get(), LIFETIME);
67 Send1(req.get()); 68 Send1(req.get());
68 delete Receive1(); 69 delete Receive1();
69 } 70 }
70 void Bind() { 71 void Bind() {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 return Receive(client2_.get()); 110 return Receive(client2_.get());
110 } 111 }
111 std::string ReceiveRaw1() { 112 std::string ReceiveRaw1() {
112 return ReceiveRaw(client1_.get()); 113 return ReceiveRaw(client1_.get());
113 } 114 }
114 std::string ReceiveRaw2() { 115 std::string ReceiveRaw2() {
115 return ReceiveRaw(client2_.get()); 116 return ReceiveRaw(client2_.get());
116 } 117 }
117 StunMessage* Receive(rtc::TestClient* client) { 118 StunMessage* Receive(rtc::TestClient* client) {
118 StunMessage* msg = NULL; 119 StunMessage* msg = NULL;
119 rtc::TestClient::Packet* packet = 120 std::unique_ptr<rtc::TestClient::Packet> packet =
120 client->NextPacket(rtc::TestClient::kTimeoutMs); 121 client->NextPacket(rtc::TestClient::kTimeoutMs);
121 if (packet) { 122 if (packet) {
122 rtc::ByteBufferWriter buf(packet->buf, packet->size); 123 rtc::ByteBufferWriter buf(packet->buf, packet->size);
123 rtc::ByteBufferReader read_buf(buf); 124 rtc::ByteBufferReader read_buf(buf);
124 msg = new RelayMessage(); 125 msg = new RelayMessage();
125 msg->Read(&read_buf); 126 msg->Read(&read_buf);
126 delete packet;
127 } 127 }
128 return msg; 128 return msg;
129 } 129 }
130 std::string ReceiveRaw(rtc::TestClient* client) { 130 std::string ReceiveRaw(rtc::TestClient* client) {
131 std::string raw; 131 std::string raw;
132 rtc::TestClient::Packet* packet = 132 std::unique_ptr<rtc::TestClient::Packet> packet =
133 client->NextPacket(rtc::TestClient::kTimeoutMs); 133 client->NextPacket(rtc::TestClient::kTimeoutMs);
134 if (packet) { 134 if (packet) {
135 raw = std::string(packet->buf, packet->size); 135 raw = std::string(packet->buf, packet->size);
136 delete packet;
137 } 136 }
138 return raw; 137 return raw;
139 } 138 }
140 139
141 static StunMessage* CreateStunMessage(int type) { 140 static StunMessage* CreateStunMessage(int type) {
142 StunMessage* msg = new RelayMessage(); 141 StunMessage* msg = new RelayMessage();
143 msg->SetType(type); 142 msg->SetType(type);
144 msg->SetTransactionID( 143 msg->SetTransactionID(
145 rtc::CreateRandomString(kStunTransactionIdLength)); 144 rtc::CreateRandomString(kStunTransactionIdLength));
146 return msg; 145 return msg;
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 const StunErrorCodeAttribute* err = res->GetErrorCode(); 507 const StunErrorCodeAttribute* err = res->GetErrorCode();
509 ASSERT_TRUE(err != NULL); 508 ASSERT_TRUE(err != NULL);
510 EXPECT_EQ(6, err->eclass()); 509 EXPECT_EQ(6, err->eclass());
511 EXPECT_EQ(0, err->number()); 510 EXPECT_EQ(0, err->number());
512 EXPECT_EQ("Operation Not Supported", err->reason()); 511 EXPECT_EQ("Operation Not Supported", err->reason());
513 512
514 // Also verify that traffic from the external client is ignored. 513 // Also verify that traffic from the external client is ignored.
515 SendRaw2(msg2, static_cast<int>(strlen(msg2))); 514 SendRaw2(msg2, static_cast<int>(strlen(msg2)));
516 EXPECT_TRUE(ReceiveRaw1().empty()); 515 EXPECT_TRUE(ReceiveRaw1().empty());
517 } 516 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698