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

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

Issue 1616153007: Stay writable after partial socket writes. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: More feedback. Created 4 years, 10 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/physicalsocketserver.cc ('k') | webrtc/base/socket_unittest.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 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
(...skipping 11 matching lines...) Expand all
22 namespace rtc { 22 namespace rtc {
23 23
24 class PhysicalSocketTest; 24 class PhysicalSocketTest;
25 25
26 class FakeSocketDispatcher : public SocketDispatcher { 26 class FakeSocketDispatcher : public SocketDispatcher {
27 public: 27 public:
28 explicit FakeSocketDispatcher(PhysicalSocketServer* ss) 28 explicit FakeSocketDispatcher(PhysicalSocketServer* ss)
29 : SocketDispatcher(ss) { 29 : SocketDispatcher(ss) {
30 } 30 }
31 31
32 FakeSocketDispatcher(SOCKET s, PhysicalSocketServer* ss)
33 : SocketDispatcher(s, ss) {
34 }
35
32 protected: 36 protected:
33 SOCKET DoAccept(SOCKET socket, sockaddr* addr, socklen_t* addrlen) override; 37 SOCKET DoAccept(SOCKET socket, sockaddr* addr, socklen_t* addrlen) override;
38 int DoSend(SOCKET socket, const char* buf, int len, int flags) override;
39 int DoSendTo(SOCKET socket, const char* buf, int len, int flags,
40 const struct sockaddr* dest_addr, socklen_t addrlen) override;
34 }; 41 };
35 42
36 class FakePhysicalSocketServer : public PhysicalSocketServer { 43 class FakePhysicalSocketServer : public PhysicalSocketServer {
37 public: 44 public:
38 explicit FakePhysicalSocketServer(PhysicalSocketTest* test) 45 explicit FakePhysicalSocketServer(PhysicalSocketTest* test)
39 : test_(test) { 46 : test_(test) {
40 } 47 }
41 48
42 AsyncSocket* CreateAsyncSocket(int type) override { 49 AsyncSocket* CreateAsyncSocket(int type) override {
43 SocketDispatcher* dispatcher = new FakeSocketDispatcher(this); 50 SocketDispatcher* dispatcher = new FakeSocketDispatcher(this);
44 if (dispatcher->Create(type)) { 51 if (!dispatcher->Create(type)) {
45 return dispatcher;
46 } else {
47 delete dispatcher; 52 delete dispatcher;
48 return nullptr; 53 return nullptr;
49 } 54 }
55 return dispatcher;
50 } 56 }
51 57
52 AsyncSocket* CreateAsyncSocket(int family, int type) override { 58 AsyncSocket* CreateAsyncSocket(int family, int type) override {
53 SocketDispatcher* dispatcher = new FakeSocketDispatcher(this); 59 SocketDispatcher* dispatcher = new FakeSocketDispatcher(this);
54 if (dispatcher->Create(family, type)) { 60 if (!dispatcher->Create(family, type)) {
55 return dispatcher;
56 } else {
57 delete dispatcher; 61 delete dispatcher;
58 return nullptr; 62 return nullptr;
59 } 63 }
64 return dispatcher;
65 }
66
67 AsyncSocket* WrapSocket(SOCKET s) override {
68 SocketDispatcher* dispatcher = new FakeSocketDispatcher(s, this);
69 if (!dispatcher->Initialize()) {
70 delete dispatcher;
71 return nullptr;
72 }
73 return dispatcher;
60 } 74 }
61 75
62 PhysicalSocketTest* GetTest() const { return test_; } 76 PhysicalSocketTest* GetTest() const { return test_; }
63 77
64 private: 78 private:
65 PhysicalSocketTest* test_; 79 PhysicalSocketTest* test_;
66 }; 80 };
67 81
68 class PhysicalSocketTest : public SocketTest { 82 class PhysicalSocketTest : public SocketTest {
69 public: 83 public:
70 // Set flag to simluate failures when calling "::accept" on a AsyncSocket. 84 // Set flag to simluate failures when calling "::accept" on a AsyncSocket.
71 void SetFailAccept(bool fail) { fail_accept_ = fail; } 85 void SetFailAccept(bool fail) { fail_accept_ = fail; }
72 bool FailAccept() const { return fail_accept_; } 86 bool FailAccept() const { return fail_accept_; }
73 87
88 // Maximum size to ::send to a socket. Set to < 0 to disable limiting.
89 void SetMaxSendSize(int max_size) { max_send_size_ = max_size; }
90 int MaxSendSize() const { return max_send_size_; }
91
74 protected: 92 protected:
75 PhysicalSocketTest() 93 PhysicalSocketTest()
76 : server_(new FakePhysicalSocketServer(this)), 94 : server_(new FakePhysicalSocketServer(this)),
77 scope_(server_.get()), 95 scope_(server_.get()),
78 fail_accept_(false) { 96 fail_accept_(false),
97 max_send_size_(-1) {
79 } 98 }
80 99
81 void ConnectInternalAcceptError(const IPAddress& loopback); 100 void ConnectInternalAcceptError(const IPAddress& loopback);
101 void WritableAfterPartialWrite(const IPAddress& loopback);
82 102
83 rtc::scoped_ptr<FakePhysicalSocketServer> server_; 103 rtc::scoped_ptr<FakePhysicalSocketServer> server_;
84 SocketServerScope scope_; 104 SocketServerScope scope_;
85 bool fail_accept_; 105 bool fail_accept_;
106 int max_send_size_;
86 }; 107 };
87 108
88 SOCKET FakeSocketDispatcher::DoAccept(SOCKET socket, 109 SOCKET FakeSocketDispatcher::DoAccept(SOCKET socket,
89 sockaddr* addr, 110 sockaddr* addr,
90 socklen_t* addrlen) { 111 socklen_t* addrlen) {
91 FakePhysicalSocketServer* ss = 112 FakePhysicalSocketServer* ss =
92 static_cast<FakePhysicalSocketServer*>(socketserver()); 113 static_cast<FakePhysicalSocketServer*>(socketserver());
93 if (ss->GetTest()->FailAccept()) { 114 if (ss->GetTest()->FailAccept()) {
94 return INVALID_SOCKET; 115 return INVALID_SOCKET;
95 } 116 }
96 117
97 return SocketDispatcher::DoAccept(socket, addr, addrlen); 118 return SocketDispatcher::DoAccept(socket, addr, addrlen);
98 } 119 }
99 120
121 int FakeSocketDispatcher::DoSend(SOCKET socket, const char* buf, int len,
122 int flags) {
123 FakePhysicalSocketServer* ss =
124 static_cast<FakePhysicalSocketServer*>(socketserver());
125 if (ss->GetTest()->MaxSendSize() >= 0) {
126 len = std::min(len, ss->GetTest()->MaxSendSize());
127 }
128
129 return SocketDispatcher::DoSend(socket, buf, len, flags);
130 }
131
132 int FakeSocketDispatcher::DoSendTo(SOCKET socket, const char* buf, int len,
133 int flags, const struct sockaddr* dest_addr, socklen_t addrlen) {
134 FakePhysicalSocketServer* ss =
135 static_cast<FakePhysicalSocketServer*>(socketserver());
136 if (ss->GetTest()->MaxSendSize() >= 0) {
137 len = std::min(len, ss->GetTest()->MaxSendSize());
138 }
139
140 return SocketDispatcher::DoSendTo(socket, buf, len, flags, dest_addr,
141 addrlen);
142 }
143
100 TEST_F(PhysicalSocketTest, TestConnectIPv4) { 144 TEST_F(PhysicalSocketTest, TestConnectIPv4) {
101 SocketTest::TestConnectIPv4(); 145 SocketTest::TestConnectIPv4();
102 } 146 }
103 147
104 // Crashes on Linux. See webrtc:4923. 148 // Crashes on Linux. See webrtc:4923.
105 #if defined(WEBRTC_LINUX) 149 #if defined(WEBRTC_LINUX)
106 #define MAYBE_TestConnectIPv6 DISABLED_TestConnectIPv6 150 #define MAYBE_TestConnectIPv6 DISABLED_TestConnectIPv6
107 #else 151 #else
108 #define MAYBE_TestConnectIPv6 TestConnectIPv6 152 #define MAYBE_TestConnectIPv6 TestConnectIPv6
109 #endif 153 #endif
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 // Crashes on Linux. See webrtc:4923. 246 // Crashes on Linux. See webrtc:4923.
203 #if defined(WEBRTC_LINUX) 247 #if defined(WEBRTC_LINUX)
204 #define MAYBE_TestConnectAcceptErrorIPv6 DISABLED_TestConnectAcceptErrorIPv6 248 #define MAYBE_TestConnectAcceptErrorIPv6 DISABLED_TestConnectAcceptErrorIPv6
205 #else 249 #else
206 #define MAYBE_TestConnectAcceptErrorIPv6 TestConnectAcceptErrorIPv6 250 #define MAYBE_TestConnectAcceptErrorIPv6 TestConnectAcceptErrorIPv6
207 #endif 251 #endif
208 TEST_F(PhysicalSocketTest, MAYBE_TestConnectAcceptErrorIPv6) { 252 TEST_F(PhysicalSocketTest, MAYBE_TestConnectAcceptErrorIPv6) {
209 ConnectInternalAcceptError(kIPv6Loopback); 253 ConnectInternalAcceptError(kIPv6Loopback);
210 } 254 }
211 255
256 void PhysicalSocketTest::WritableAfterPartialWrite(const IPAddress& loopback) {
257 // Simulate a really small maximum send size.
258 const int kMaxSendSize = 128;
259 SetMaxSendSize(kMaxSendSize);
260
261 // Run the default send/receive socket tests with a smaller amount of data
262 // to avoid long running times due to the small maximum send size.
263 const size_t kDataSize = 128 * 1024;
264 TcpInternal(loopback, kDataSize, kMaxSendSize);
265 }
266
267 TEST_F(PhysicalSocketTest, TestWritableAfterPartialWriteIPv4) {
268 WritableAfterPartialWrite(kIPv4Loopback);
269 }
270
271 // Crashes on Linux. See webrtc:4923.
272 #if defined(WEBRTC_LINUX)
273 #define MAYBE_TestWritableAfterPartialWriteIPv6 \
274 DISABLED_TestWritableAfterPartialWriteIPv6
275 #else
276 #define MAYBE_TestWritableAfterPartialWriteIPv6 \
277 TestWritableAfterPartialWriteIPv6
278 #endif
279 TEST_F(PhysicalSocketTest, MAYBE_TestWritableAfterPartialWriteIPv6) {
280 WritableAfterPartialWrite(kIPv6Loopback);
281 }
282
212 // Crashes on Linux. See webrtc:4923. 283 // Crashes on Linux. See webrtc:4923.
213 #if defined(WEBRTC_LINUX) 284 #if defined(WEBRTC_LINUX)
214 #define MAYBE_TestConnectFailIPv6 DISABLED_TestConnectFailIPv6 285 #define MAYBE_TestConnectFailIPv6 DISABLED_TestConnectFailIPv6
215 #else 286 #else
216 #define MAYBE_TestConnectFailIPv6 TestConnectFailIPv6 287 #define MAYBE_TestConnectFailIPv6 TestConnectFailIPv6
217 #endif 288 #endif
218 TEST_F(PhysicalSocketTest, MAYBE_TestConnectFailIPv6) { 289 TEST_F(PhysicalSocketTest, MAYBE_TestConnectFailIPv6) {
219 SocketTest::TestConnectFailIPv6(); 290 SocketTest::TestConnectFailIPv6();
220 } 291 }
221 292
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
517 thread->Start(runnable.get()); 588 thread->Start(runnable.get());
518 EXPECT_TRUE(ss_->Wait(1500, true)); 589 EXPECT_TRUE(ss_->Wait(1500, true));
519 EXPECT_TRUE(ExpectSignal(SIGTERM)); 590 EXPECT_TRUE(ExpectSignal(SIGTERM));
520 EXPECT_EQ(Thread::Current(), signaled_thread_); 591 EXPECT_EQ(Thread::Current(), signaled_thread_);
521 EXPECT_TRUE(ExpectNone()); 592 EXPECT_TRUE(ExpectNone());
522 } 593 }
523 594
524 #endif 595 #endif
525 596
526 } // namespace rtc 597 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/physicalsocketserver.cc ('k') | webrtc/base/socket_unittest.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698