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

Side by Side Diff: webrtc/base/virtualsocket_unittest.cc

Issue 2718663005: Replace NULL with nullptr or null in webrtc/base/. (Closed)
Patch Set: Fixing Windows and formatting issues. Created 3 years, 9 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
« no previous file with comments | « webrtc/base/unixfilesystem.cc ('k') | webrtc/base/virtualsocketserver.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2006 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2006 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
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 bool done; 130 bool done;
131 size_t count; 131 size_t count;
132 size_t sec_count; 132 size_t sec_count;
133 double sum; 133 double sum;
134 double sum_sq; 134 double sum_sq;
135 uint32_t samples; 135 uint32_t samples;
136 }; 136 };
137 137
138 class VirtualSocketServerTest : public testing::Test { 138 class VirtualSocketServerTest : public testing::Test {
139 public: 139 public:
140 VirtualSocketServerTest() : ss_(new VirtualSocketServer(NULL)), 140 VirtualSocketServerTest()
141 kIPv4AnyAddress(IPAddress(INADDR_ANY), 0), 141 : ss_(new VirtualSocketServer(nullptr)),
142 kIPv6AnyAddress(IPAddress(in6addr_any), 0) { 142 kIPv4AnyAddress(IPAddress(INADDR_ANY), 0),
143 } 143 kIPv6AnyAddress(IPAddress(in6addr_any), 0) {}
144 144
145 void CheckPortIncrementalization(const SocketAddress& post, 145 void CheckPortIncrementalization(const SocketAddress& post,
146 const SocketAddress& pre) { 146 const SocketAddress& pre) {
147 EXPECT_EQ(post.port(), pre.port() + 1); 147 EXPECT_EQ(post.port(), pre.port() + 1);
148 IPAddress post_ip = post.ipaddr(); 148 IPAddress post_ip = post.ipaddr();
149 IPAddress pre_ip = pre.ipaddr(); 149 IPAddress pre_ip = pre.ipaddr();
150 EXPECT_EQ(pre_ip.family(), post_ip.family()); 150 EXPECT_EQ(pre_ip.family(), post_ip.family());
151 if (post_ip.family() == AF_INET) { 151 if (post_ip.family() == AF_INET) {
152 in_addr pre_ipv4 = pre_ip.ipv4_address(); 152 in_addr pre_ipv4 = pre_ip.ipv4_address();
153 in_addr post_ipv4 = post_ip.ipv4_address(); 153 in_addr post_ipv4 = post_ip.ipv4_address();
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 SOCK_STREAM); 256 SOCK_STREAM);
257 sink.Monitor(server); 257 sink.Monitor(server);
258 EXPECT_NE(0, server->Listen(5)); // Bind required 258 EXPECT_NE(0, server->Listen(5)); // Bind required
259 EXPECT_EQ(0, server->Bind(initial_addr)); 259 EXPECT_EQ(0, server->Bind(initial_addr));
260 EXPECT_EQ(server->GetLocalAddress().family(), initial_addr.family()); 260 EXPECT_EQ(server->GetLocalAddress().family(), initial_addr.family());
261 EXPECT_EQ(0, server->Listen(5)); 261 EXPECT_EQ(0, server->Listen(5));
262 EXPECT_EQ(server->GetState(), AsyncSocket::CS_CONNECTING); 262 EXPECT_EQ(server->GetState(), AsyncSocket::CS_CONNECTING);
263 263
264 // No pending server connections 264 // No pending server connections
265 EXPECT_FALSE(sink.Check(server, testing::SSE_READ)); 265 EXPECT_FALSE(sink.Check(server, testing::SSE_READ));
266 EXPECT_TRUE(NULL == server->Accept(&accept_addr)); 266 EXPECT_TRUE(nullptr == server->Accept(&accept_addr));
267 EXPECT_EQ(AF_UNSPEC, accept_addr.family()); 267 EXPECT_EQ(AF_UNSPEC, accept_addr.family());
268 268
269 // Attempt connect to listening socket 269 // Attempt connect to listening socket
270 EXPECT_EQ(0, client->Connect(server->GetLocalAddress())); 270 EXPECT_EQ(0, client->Connect(server->GetLocalAddress()));
271 EXPECT_NE(client->GetLocalAddress(), kEmptyAddr); // Implicit Bind 271 EXPECT_NE(client->GetLocalAddress(), kEmptyAddr); // Implicit Bind
272 EXPECT_NE(AF_UNSPEC, client->GetLocalAddress().family()); // Implicit Bind 272 EXPECT_NE(AF_UNSPEC, client->GetLocalAddress().family()); // Implicit Bind
273 EXPECT_NE(client->GetLocalAddress(), server->GetLocalAddress()); 273 EXPECT_NE(client->GetLocalAddress(), server->GetLocalAddress());
274 274
275 // Client is connecting 275 // Client is connecting
276 EXPECT_EQ(client->GetState(), AsyncSocket::CS_CONNECTING); 276 EXPECT_EQ(client->GetState(), AsyncSocket::CS_CONNECTING);
277 EXPECT_FALSE(sink.Check(client, testing::SSE_OPEN)); 277 EXPECT_FALSE(sink.Check(client, testing::SSE_OPEN));
278 EXPECT_FALSE(sink.Check(client, testing::SSE_CLOSE)); 278 EXPECT_FALSE(sink.Check(client, testing::SSE_CLOSE));
279 279
280 ss_->ProcessMessagesUntilIdle(); 280 ss_->ProcessMessagesUntilIdle();
281 281
282 // Client still connecting 282 // Client still connecting
283 EXPECT_EQ(client->GetState(), AsyncSocket::CS_CONNECTING); 283 EXPECT_EQ(client->GetState(), AsyncSocket::CS_CONNECTING);
284 EXPECT_FALSE(sink.Check(client, testing::SSE_OPEN)); 284 EXPECT_FALSE(sink.Check(client, testing::SSE_OPEN));
285 EXPECT_FALSE(sink.Check(client, testing::SSE_CLOSE)); 285 EXPECT_FALSE(sink.Check(client, testing::SSE_CLOSE));
286 286
287 // Server has pending connection 287 // Server has pending connection
288 EXPECT_TRUE(sink.Check(server, testing::SSE_READ)); 288 EXPECT_TRUE(sink.Check(server, testing::SSE_READ));
289 Socket* accepted = server->Accept(&accept_addr); 289 Socket* accepted = server->Accept(&accept_addr);
290 EXPECT_TRUE(NULL != accepted); 290 EXPECT_TRUE(nullptr != accepted);
291 EXPECT_NE(accept_addr, kEmptyAddr); 291 EXPECT_NE(accept_addr, kEmptyAddr);
292 EXPECT_EQ(accepted->GetRemoteAddress(), accept_addr); 292 EXPECT_EQ(accepted->GetRemoteAddress(), accept_addr);
293 293
294 EXPECT_EQ(accepted->GetState(), AsyncSocket::CS_CONNECTED); 294 EXPECT_EQ(accepted->GetState(), AsyncSocket::CS_CONNECTED);
295 EXPECT_EQ(accepted->GetLocalAddress(), server->GetLocalAddress()); 295 EXPECT_EQ(accepted->GetLocalAddress(), server->GetLocalAddress());
296 EXPECT_EQ(accepted->GetRemoteAddress(), client->GetLocalAddress()); 296 EXPECT_EQ(accepted->GetRemoteAddress(), client->GetLocalAddress());
297 297
298 ss_->ProcessMessagesUntilIdle(); 298 ss_->ProcessMessagesUntilIdle();
299 299
300 // Client has connected 300 // Client has connected
(...skipping 22 matching lines...) Expand all
323 sink.Monitor(server); 323 sink.Monitor(server);
324 EXPECT_EQ(0, server->Bind(initial_addr)); 324 EXPECT_EQ(0, server->Bind(initial_addr));
325 EXPECT_EQ(server->GetLocalAddress().family(), initial_addr.family()); 325 EXPECT_EQ(server->GetLocalAddress().family(), initial_addr.family());
326 // Attempt connect to non-listening socket 326 // Attempt connect to non-listening socket
327 EXPECT_EQ(0, client->Connect(server->GetLocalAddress())); 327 EXPECT_EQ(0, client->Connect(server->GetLocalAddress()));
328 328
329 ss_->ProcessMessagesUntilIdle(); 329 ss_->ProcessMessagesUntilIdle();
330 330
331 // No pending server connections 331 // No pending server connections
332 EXPECT_FALSE(sink.Check(server, testing::SSE_READ)); 332 EXPECT_FALSE(sink.Check(server, testing::SSE_READ));
333 EXPECT_TRUE(NULL == server->Accept(&accept_addr)); 333 EXPECT_TRUE(nullptr == server->Accept(&accept_addr));
334 EXPECT_EQ(accept_addr, nil_addr); 334 EXPECT_EQ(accept_addr, nil_addr);
335 335
336 // Connection failed 336 // Connection failed
337 EXPECT_EQ(client->GetState(), AsyncSocket::CS_CLOSED); 337 EXPECT_EQ(client->GetState(), AsyncSocket::CS_CLOSED);
338 EXPECT_FALSE(sink.Check(client, testing::SSE_OPEN)); 338 EXPECT_FALSE(sink.Check(client, testing::SSE_OPEN));
339 EXPECT_TRUE(sink.Check(client, testing::SSE_ERROR)); 339 EXPECT_TRUE(sink.Check(client, testing::SSE_ERROR));
340 EXPECT_EQ(client->GetRemoteAddress(), nil_addr); 340 EXPECT_EQ(client->GetRemoteAddress(), nil_addr);
341 } 341 }
342 342
343 void CloseDuringConnectTest(const SocketAddress& initial_addr) { 343 void CloseDuringConnectTest(const SocketAddress& initial_addr) {
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 EXPECT_EQ(server->GetLocalAddress().family(), initial_addr.family()); 402 EXPECT_EQ(server->GetLocalAddress().family(), initial_addr.family());
403 403
404 EXPECT_EQ(0, server->Listen(5)); 404 EXPECT_EQ(0, server->Listen(5));
405 EXPECT_EQ(0, client->Connect(server->GetLocalAddress())); 405 EXPECT_EQ(0, client->Connect(server->GetLocalAddress()));
406 406
407 ss_->ProcessMessagesUntilIdle(); 407 ss_->ProcessMessagesUntilIdle();
408 408
409 // Server accepts connection 409 // Server accepts connection
410 EXPECT_TRUE(sink.Check(server.get(), testing::SSE_READ)); 410 EXPECT_TRUE(sink.Check(server.get(), testing::SSE_READ));
411 std::unique_ptr<AsyncSocket> accepted(server->Accept(&accept_addr)); 411 std::unique_ptr<AsyncSocket> accepted(server->Accept(&accept_addr));
412 ASSERT_TRUE(NULL != accepted.get()); 412 ASSERT_TRUE(nullptr != accepted.get());
413 sink.Monitor(accepted.get()); 413 sink.Monitor(accepted.get());
414 414
415 // Client closes before connection complets 415 // Client closes before connection complets
416 EXPECT_EQ(accepted->GetState(), AsyncSocket::CS_CONNECTED); 416 EXPECT_EQ(accepted->GetState(), AsyncSocket::CS_CONNECTED);
417 417
418 // Connected message has not been processed yet. 418 // Connected message has not been processed yet.
419 EXPECT_EQ(client->GetState(), AsyncSocket::CS_CONNECTING); 419 EXPECT_EQ(client->GetState(), AsyncSocket::CS_CONNECTING);
420 client->Close(); 420 client->Close();
421 421
422 ss_->ProcessMessagesUntilIdle(); 422 ss_->ProcessMessagesUntilIdle();
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
679 ASSERT_TRUE(receiver.count >= 5 * 3 * bandwidth / 4); 679 ASSERT_TRUE(receiver.count >= 5 * 3 * bandwidth / 4);
680 ASSERT_TRUE(receiver.count <= 6 * bandwidth); // queue could drain for 1s 680 ASSERT_TRUE(receiver.count <= 6 * bandwidth); // queue could drain for 1s
681 681
682 ss_->set_bandwidth(0); 682 ss_->set_bandwidth(0);
683 } 683 }
684 684
685 // It is important that initial_addr's port has to be 0 such that the 685 // It is important that initial_addr's port has to be 0 such that the
686 // incremental port behavior could ensure the 2 Binds result in different 686 // incremental port behavior could ensure the 2 Binds result in different
687 // address. 687 // address.
688 void DelayTest(const SocketAddress& initial_addr) { 688 void DelayTest(const SocketAddress& initial_addr) {
689 time_t seed = ::time(NULL); 689 time_t seed = ::time(nullptr);
690 LOG(LS_VERBOSE) << "seed = " << seed; 690 LOG(LS_VERBOSE) << "seed = " << seed;
691 srand(static_cast<unsigned int>(seed)); 691 srand(static_cast<unsigned int>(seed));
692 692
693 const uint32_t mean = 2000; 693 const uint32_t mean = 2000;
694 const uint32_t stddev = 500; 694 const uint32_t stddev = 500;
695 695
696 ss_->set_delay_mean(mean); 696 ss_->set_delay_mean(mean);
697 ss_->set_delay_stddev(stddev); 697 ss_->set_delay_stddev(stddev);
698 ss_->UpdateDelayDistribution(); 698 ss_->UpdateDelayDistribution();
699 699
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
759 SOCK_STREAM); 759 SOCK_STREAM);
760 sink.Monitor(server); 760 sink.Monitor(server);
761 server->Bind(server_addr); 761 server->Bind(server_addr);
762 server->Listen(5); 762 server->Listen(5);
763 763
764 if (shouldSucceed) { 764 if (shouldSucceed) {
765 EXPECT_EQ(0, client->Connect(server->GetLocalAddress())); 765 EXPECT_EQ(0, client->Connect(server->GetLocalAddress()));
766 ss_->ProcessMessagesUntilIdle(); 766 ss_->ProcessMessagesUntilIdle();
767 EXPECT_TRUE(sink.Check(server, testing::SSE_READ)); 767 EXPECT_TRUE(sink.Check(server, testing::SSE_READ));
768 Socket* accepted = server->Accept(&accept_address); 768 Socket* accepted = server->Accept(&accept_address);
769 EXPECT_TRUE(NULL != accepted); 769 EXPECT_TRUE(nullptr != accepted);
770 EXPECT_NE(kEmptyAddr, accept_address); 770 EXPECT_NE(kEmptyAddr, accept_address);
771 ss_->ProcessMessagesUntilIdle(); 771 ss_->ProcessMessagesUntilIdle();
772 EXPECT_TRUE(sink.Check(client, testing::SSE_OPEN)); 772 EXPECT_TRUE(sink.Check(client, testing::SSE_OPEN));
773 EXPECT_EQ(client->GetRemoteAddress(), server->GetLocalAddress()); 773 EXPECT_EQ(client->GetRemoteAddress(), server->GetLocalAddress());
774 } else { 774 } else {
775 // Check that the connection failed. 775 // Check that the connection failed.
776 EXPECT_EQ(-1, client->Connect(server->GetLocalAddress())); 776 EXPECT_EQ(-1, client->Connect(server->GetLocalAddress()));
777 ss_->ProcessMessagesUntilIdle(); 777 ss_->ProcessMessagesUntilIdle();
778 778
779 EXPECT_FALSE(sink.Check(server, testing::SSE_READ)); 779 EXPECT_FALSE(sink.Check(server, testing::SSE_READ));
780 EXPECT_TRUE(NULL == server->Accept(&accept_address)); 780 EXPECT_TRUE(nullptr == server->Accept(&accept_address));
781 EXPECT_EQ(accept_address, kEmptyAddr); 781 EXPECT_EQ(accept_address, kEmptyAddr);
782 EXPECT_EQ(client->GetState(), AsyncSocket::CS_CLOSED); 782 EXPECT_EQ(client->GetState(), AsyncSocket::CS_CLOSED);
783 EXPECT_FALSE(sink.Check(client, testing::SSE_OPEN)); 783 EXPECT_FALSE(sink.Check(client, testing::SSE_OPEN));
784 EXPECT_EQ(client->GetRemoteAddress(), kEmptyAddr); 784 EXPECT_EQ(client->GetRemoteAddress(), kEmptyAddr);
785 } 785 }
786 } 786 }
787 787
788 // Test cross-family datagram sending between a client bound to client_addr 788 // Test cross-family datagram sending between a client bound to client_addr
789 // and a server bound to server_addr. shouldSucceed indicates if sending is 789 // and a server bound to server_addr. shouldSucceed indicates if sending is
790 // expected to succeed or not. 790 // expected to succeed or not.
(...skipping 20 matching lines...) Expand all
811 } else { 811 } else {
812 EXPECT_EQ(-1, client2->SendTo("foo", 3, bound_server_addr)); 812 EXPECT_EQ(-1, client2->SendTo("foo", 3, bound_server_addr));
813 EXPECT_TRUE(client1->CheckNoPacket()); 813 EXPECT_TRUE(client1->CheckNoPacket());
814 } 814 }
815 } 815 }
816 816
817 protected: 817 protected:
818 virtual void SetUp() { 818 virtual void SetUp() {
819 Thread::Current()->set_socketserver(ss_); 819 Thread::Current()->set_socketserver(ss_);
820 } 820 }
821 virtual void TearDown() { 821 virtual void TearDown() { Thread::Current()->set_socketserver(nullptr); }
822 Thread::Current()->set_socketserver(NULL);
823 }
824 822
825 VirtualSocketServer* ss_; 823 VirtualSocketServer* ss_;
826 const SocketAddress kIPv4AnyAddress; 824 const SocketAddress kIPv4AnyAddress;
827 const SocketAddress kIPv6AnyAddress; 825 const SocketAddress kIPv6AnyAddress;
828 }; 826 };
829 827
830 TEST_F(VirtualSocketServerTest, basic_v4) { 828 TEST_F(VirtualSocketServerTest, basic_v4) {
831 SocketAddress ipv4_test_addr(IPAddress(INADDR_ANY), 5000); 829 SocketAddress ipv4_test_addr(IPAddress(INADDR_ANY), 5000);
832 BasicTest(ipv4_test_addr); 830 BasicTest(ipv4_test_addr);
833 } 831 }
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
1089 for (size_t midx = 0; midx < arraysize(kTestMean); ++midx) { 1087 for (size_t midx = 0; midx < arraysize(kTestMean); ++midx) {
1090 for (size_t didx = 0; didx < arraysize(kTestDev); ++didx) { 1088 for (size_t didx = 0; didx < arraysize(kTestDev); ++didx) {
1091 for (size_t sidx = 0; sidx < arraysize(kTestSamples); ++sidx) { 1089 for (size_t sidx = 0; sidx < arraysize(kTestSamples); ++sidx) {
1092 ASSERT_LT(0u, kTestSamples[sidx]); 1090 ASSERT_LT(0u, kTestSamples[sidx]);
1093 const uint32_t kStdDev = 1091 const uint32_t kStdDev =
1094 static_cast<uint32_t>(kTestDev[didx] * kTestMean[midx]); 1092 static_cast<uint32_t>(kTestDev[didx] * kTestMean[midx]);
1095 VirtualSocketServer::Function* f = 1093 VirtualSocketServer::Function* f =
1096 VirtualSocketServer::CreateDistribution(kTestMean[midx], 1094 VirtualSocketServer::CreateDistribution(kTestMean[midx],
1097 kStdDev, 1095 kStdDev,
1098 kTestSamples[sidx]); 1096 kTestSamples[sidx]);
1099 ASSERT_TRUE(NULL != f); 1097 ASSERT_TRUE(nullptr != f);
1100 ASSERT_EQ(kTestSamples[sidx], f->size()); 1098 ASSERT_EQ(kTestSamples[sidx], f->size());
1101 double sum = 0; 1099 double sum = 0;
1102 for (uint32_t i = 0; i < f->size(); ++i) { 1100 for (uint32_t i = 0; i < f->size(); ++i) {
1103 sum += (*f)[i].second; 1101 sum += (*f)[i].second;
1104 } 1102 }
1105 const double mean = sum / f->size(); 1103 const double mean = sum / f->size();
1106 double sum_sq_dev = 0; 1104 double sum_sq_dev = 0;
1107 for (uint32_t i = 0; i < f->size(); ++i) { 1105 for (uint32_t i = 0; i < f->size(); ++i) {
1108 double dev = (*f)[i].second - mean; 1106 double dev = (*f)[i].second - mean;
1109 sum_sq_dev += dev * dev; 1107 sum_sq_dev += dev * dev;
1110 } 1108 }
1111 const double stddev = sqrt(sum_sq_dev / f->size()); 1109 const double stddev = sqrt(sum_sq_dev / f->size());
1112 EXPECT_NEAR(kTestMean[midx], mean, 0.1 * kTestMean[midx]) 1110 EXPECT_NEAR(kTestMean[midx], mean, 0.1 * kTestMean[midx])
1113 << "M=" << kTestMean[midx] 1111 << "M=" << kTestMean[midx]
1114 << " SD=" << kStdDev 1112 << " SD=" << kStdDev
1115 << " N=" << kTestSamples[sidx]; 1113 << " N=" << kTestSamples[sidx];
1116 EXPECT_NEAR(kStdDev, stddev, 0.1 * kStdDev) 1114 EXPECT_NEAR(kStdDev, stddev, 0.1 * kStdDev)
1117 << "M=" << kTestMean[midx] 1115 << "M=" << kTestMean[midx]
1118 << " SD=" << kStdDev 1116 << " SD=" << kStdDev
1119 << " N=" << kTestSamples[sidx]; 1117 << " N=" << kTestSamples[sidx];
1120 delete f; 1118 delete f;
1121 } 1119 }
1122 } 1120 }
1123 } 1121 }
1124 } 1122 }
OLDNEW
« no previous file with comments | « webrtc/base/unixfilesystem.cc ('k') | webrtc/base/virtualsocketserver.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698