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

Side by Side Diff: net/websockets/websocket_throttle_unittest.cc

Issue 10309002: Reimplements net::AddressList without struct addrinfo. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: get_canonical_name -> canonical_name. iterator to indexing Created 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 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 "net/websockets/websocket_throttle.h" 5 #include "net/websockets/websocket_throttle.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "googleurl/src/gurl.h" 10 #include "googleurl/src/gurl.h"
11 #include "net/base/address_list.h" 11 #include "net/base/address_list.h"
12 #include "net/base/sys_addrinfo.h"
13 #include "net/base/test_completion_callback.h" 12 #include "net/base/test_completion_callback.h"
14 #include "net/socket_stream/socket_stream.h" 13 #include "net/socket_stream/socket_stream.h"
15 #include "net/url_request/url_request_test_util.h" 14 #include "net/url_request/url_request_test_util.h"
16 #include "net/websockets/websocket_job.h" 15 #include "net/websockets/websocket_job.h"
17 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
18 #include "testing/platform_test.h" 17 #include "testing/platform_test.h"
19 18
20 class DummySocketStreamDelegate : public net::SocketStream::Delegate { 19 class DummySocketStreamDelegate : public net::SocketStream::Delegate {
21 public: 20 public:
22 DummySocketStreamDelegate() {} 21 DummySocketStreamDelegate() {}
23 virtual ~DummySocketStreamDelegate() {} 22 virtual ~DummySocketStreamDelegate() {}
24 virtual void OnConnected( 23 virtual void OnConnected(
25 net::SocketStream* socket, int max_pending_send_allowed) {} 24 net::SocketStream* socket, int max_pending_send_allowed) {}
26 virtual void OnSentData(net::SocketStream* socket, int amount_sent) {} 25 virtual void OnSentData(net::SocketStream* socket, int amount_sent) {}
27 virtual void OnReceivedData(net::SocketStream* socket, 26 virtual void OnReceivedData(net::SocketStream* socket,
28 const char* data, int len) {} 27 const char* data, int len) {}
29 virtual void OnClose(net::SocketStream* socket) {} 28 virtual void OnClose(net::SocketStream* socket) {}
30 }; 29 };
31 30
32 namespace net { 31 namespace net {
33 32
34 class WebSocketThrottleTest : public PlatformTest { 33 class WebSocketThrottleTest : public PlatformTest {
35 protected: 34 protected:
36 struct addrinfo *AddAddr(int a1, int a2, int a3, int a4, 35 IPEndPoint MakeAddr(int a1, int a2, int a3, int a4) {
37 struct addrinfo* next) { 36 IPAddressNumber ip;
38 struct addrinfo* addrinfo = new struct addrinfo; 37 ip.push_back(a1);
39 memset(addrinfo, 0, sizeof(struct addrinfo)); 38 ip.push_back(a2);
40 addrinfo->ai_family = AF_INET; 39 ip.push_back(a3);
41 int addrlen = sizeof(struct sockaddr_in); 40 ip.push_back(a4);
42 addrinfo->ai_addrlen = addrlen; 41 return IPEndPoint(ip, 0);
43 addrinfo->ai_addr = reinterpret_cast<sockaddr*>(new char[addrlen]);
44 memset(addrinfo->ai_addr, 0, sizeof(addrlen));
45 struct sockaddr_in* addr =
46 reinterpret_cast<sockaddr_in*>(addrinfo->ai_addr);
47 int addrint = ((a1 & 0xff) << 24) |
48 ((a2 & 0xff) << 16) |
49 ((a3 & 0xff) << 8) |
50 ((a4 & 0xff));
51 memcpy(&addr->sin_addr, &addrint, sizeof(int));
52 addrinfo->ai_next = next;
53 return addrinfo;
54 }
55 void DeleteAddrInfo(struct addrinfo* head) {
56 if (!head)
57 return;
58 struct addrinfo* next;
59 for (struct addrinfo* a = head; a != NULL; a = next) {
60 next = a->ai_next;
61 delete [] a->ai_addr;
62 delete a;
63 }
64 } 42 }
65 43
66 static void MockSocketStreamConnect( 44 static void MockSocketStreamConnect(
67 SocketStream* socket, struct addrinfo* head) { 45 SocketStream* socket, const AddressList& list) {
68 socket->CopyAddrInfo(head); 46 socket->set_addresses(list);
69 // TODO(toyoshim): We should introduce additional tests on cases via proxy. 47 // TODO(toyoshim): We should introduce additional tests on cases via proxy.
70 socket->proxy_info_.UseDirect(); 48 socket->proxy_info_.UseDirect();
71 // In SocketStream::Connect(), it adds reference to socket, which is 49 // In SocketStream::Connect(), it adds reference to socket, which is
72 // balanced with SocketStream::Finish() that is finally called from 50 // balanced with SocketStream::Finish() that is finally called from
73 // SocketStream::Close() or SocketStream::DetachDelegate(), when 51 // SocketStream::Close() or SocketStream::DetachDelegate(), when
74 // next_state_ is not STATE_NONE. 52 // next_state_ is not STATE_NONE.
75 // If next_state_ is STATE_NONE, SocketStream::Close() or 53 // If next_state_ is STATE_NONE, SocketStream::Close() or
76 // SocketStream::DetachDelegate() won't call SocketStream::Finish(), 54 // SocketStream::DetachDelegate() won't call SocketStream::Finish(),
77 // so Release() won't be called. Thus, we don't need socket->AddRef() 55 // so Release() won't be called. Thus, we don't need socket->AddRef()
78 // here. 56 // here.
79 DCHECK_EQ(socket->next_state_, SocketStream::STATE_NONE); 57 DCHECK_EQ(socket->next_state_, SocketStream::STATE_NONE);
80 } 58 }
81 }; 59 };
82 60
83 TEST_F(WebSocketThrottleTest, Throttle) { 61 TEST_F(WebSocketThrottleTest, Throttle) {
84 scoped_refptr<URLRequestContext> context(new TestURLRequestContext); 62 scoped_refptr<URLRequestContext> context(new TestURLRequestContext);
85 DummySocketStreamDelegate delegate; 63 DummySocketStreamDelegate delegate;
86 // TODO(toyoshim): We need to consider both spdy-enabled and spdy-disabled 64 // TODO(toyoshim): We need to consider both spdy-enabled and spdy-disabled
87 // configuration. 65 // configuration.
88 WebSocketJob::set_websocket_over_spdy_enabled(true); 66 WebSocketJob::set_websocket_over_spdy_enabled(true);
89 67
90 // For host1: 1.2.3.4, 1.2.3.5, 1.2.3.6 68 // For host1: 1.2.3.4, 1.2.3.5, 1.2.3.6
91 struct addrinfo* addr = AddAddr(1, 2, 3, 4, NULL); 69 AddressList addr;
92 addr = AddAddr(1, 2, 3, 5, addr); 70 addr.push_back(MakeAddr(1, 2, 3, 4));
93 addr = AddAddr(1, 2, 3, 6, addr); 71 addr.push_back(MakeAddr(1, 2, 3, 5));
72 addr.push_back(MakeAddr(1, 2, 3, 6));
94 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); 73 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate));
95 scoped_refptr<SocketStream> s1( 74 scoped_refptr<SocketStream> s1(
96 new SocketStream(GURL("ws://host1/"), w1.get())); 75 new SocketStream(GURL("ws://host1/"), w1.get()));
97 s1->set_context(context.get()); 76 s1->set_context(context.get());
98 w1->InitSocketStream(s1.get()); 77 w1->InitSocketStream(s1.get());
99 WebSocketThrottleTest::MockSocketStreamConnect(s1, addr); 78 WebSocketThrottleTest::MockSocketStreamConnect(s1, addr);
100 DeleteAddrInfo(addr);
101 79
102 DVLOG(1) << "socket1"; 80 DVLOG(1) << "socket1";
103 TestCompletionCallback callback_s1; 81 TestCompletionCallback callback_s1;
104 // Trying to open connection to host1 will start without wait. 82 // Trying to open connection to host1 will start without wait.
105 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1, callback_s1.callback())); 83 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1, callback_s1.callback()));
106 84
107 // Now connecting to host1, so waiting queue looks like 85 // Now connecting to host1, so waiting queue looks like
108 // Address | head -> tail 86 // Address | head -> tail
109 // 1.2.3.4 | w1 87 // 1.2.3.4 | w1
110 // 1.2.3.5 | w1 88 // 1.2.3.5 | w1
111 // 1.2.3.6 | w1 89 // 1.2.3.6 | w1
112 90
113 // For host2: 1.2.3.4 91 // For host2: 1.2.3.4
114 addr = AddAddr(1, 2, 3, 4, NULL); 92 addr.clear();
93 addr.push_back(MakeAddr(1, 2, 3, 4));
115 scoped_refptr<WebSocketJob> w2(new WebSocketJob(&delegate)); 94 scoped_refptr<WebSocketJob> w2(new WebSocketJob(&delegate));
116 scoped_refptr<SocketStream> s2( 95 scoped_refptr<SocketStream> s2(
117 new SocketStream(GURL("ws://host2/"), w2.get())); 96 new SocketStream(GURL("ws://host2/"), w2.get()));
118 s2->set_context(context.get()); 97 s2->set_context(context.get());
119 w2->InitSocketStream(s2.get()); 98 w2->InitSocketStream(s2.get());
120 WebSocketThrottleTest::MockSocketStreamConnect(s2, addr); 99 WebSocketThrottleTest::MockSocketStreamConnect(s2, addr);
121 DeleteAddrInfo(addr);
122 100
123 DVLOG(1) << "socket2"; 101 DVLOG(1) << "socket2";
124 TestCompletionCallback callback_s2; 102 TestCompletionCallback callback_s2;
125 // Trying to open connection to host2 will wait for w1. 103 // Trying to open connection to host2 will wait for w1.
126 EXPECT_EQ(ERR_IO_PENDING, 104 EXPECT_EQ(ERR_IO_PENDING,
127 w2->OnStartOpenConnection(s2, callback_s2.callback())); 105 w2->OnStartOpenConnection(s2, callback_s2.callback()));
128 // Now waiting queue looks like 106 // Now waiting queue looks like
129 // Address | head -> tail 107 // Address | head -> tail
130 // 1.2.3.4 | w1 w2 108 // 1.2.3.4 | w1 w2
131 // 1.2.3.5 | w1 109 // 1.2.3.5 | w1
132 // 1.2.3.6 | w1 110 // 1.2.3.6 | w1
133 111
134 // For host3: 1.2.3.5 112 // For host3: 1.2.3.5
135 addr = AddAddr(1, 2, 3, 5, NULL); 113 addr.clear();
114 addr.push_back(MakeAddr(1, 2, 3, 5));
136 scoped_refptr<WebSocketJob> w3(new WebSocketJob(&delegate)); 115 scoped_refptr<WebSocketJob> w3(new WebSocketJob(&delegate));
137 scoped_refptr<SocketStream> s3( 116 scoped_refptr<SocketStream> s3(
138 new SocketStream(GURL("ws://host3/"), w3.get())); 117 new SocketStream(GURL("ws://host3/"), w3.get()));
139 s3->set_context(context.get()); 118 s3->set_context(context.get());
140 w3->InitSocketStream(s3.get()); 119 w3->InitSocketStream(s3.get());
141 WebSocketThrottleTest::MockSocketStreamConnect(s3, addr); 120 WebSocketThrottleTest::MockSocketStreamConnect(s3, addr);
142 DeleteAddrInfo(addr);
143 121
144 DVLOG(1) << "socket3"; 122 DVLOG(1) << "socket3";
145 TestCompletionCallback callback_s3; 123 TestCompletionCallback callback_s3;
146 // Trying to open connection to host3 will wait for w1. 124 // Trying to open connection to host3 will wait for w1.
147 EXPECT_EQ(ERR_IO_PENDING, 125 EXPECT_EQ(ERR_IO_PENDING,
148 w3->OnStartOpenConnection(s3, callback_s3.callback())); 126 w3->OnStartOpenConnection(s3, callback_s3.callback()));
149 // Address | head -> tail 127 // Address | head -> tail
150 // 1.2.3.4 | w1 w2 128 // 1.2.3.4 | w1 w2
151 // 1.2.3.5 | w1 w3 129 // 1.2.3.5 | w1 w3
152 // 1.2.3.6 | w1 130 // 1.2.3.6 | w1
153 131
154 // For host4: 1.2.3.4, 1.2.3.6 132 // For host4: 1.2.3.4, 1.2.3.6
155 addr = AddAddr(1, 2, 3, 4, NULL); 133 addr.clear();
156 addr = AddAddr(1, 2, 3, 6, addr); 134 addr.push_back(MakeAddr(1, 2, 3, 4));
135 addr.push_back(MakeAddr(1, 2, 3, 6));
157 scoped_refptr<WebSocketJob> w4(new WebSocketJob(&delegate)); 136 scoped_refptr<WebSocketJob> w4(new WebSocketJob(&delegate));
158 scoped_refptr<SocketStream> s4( 137 scoped_refptr<SocketStream> s4(
159 new SocketStream(GURL("ws://host4/"), w4.get())); 138 new SocketStream(GURL("ws://host4/"), w4.get()));
160 s4->set_context(context.get()); 139 s4->set_context(context.get());
161 w4->InitSocketStream(s4.get()); 140 w4->InitSocketStream(s4.get());
162 WebSocketThrottleTest::MockSocketStreamConnect(s4, addr); 141 WebSocketThrottleTest::MockSocketStreamConnect(s4, addr);
163 DeleteAddrInfo(addr);
164 142
165 DVLOG(1) << "socket4"; 143 DVLOG(1) << "socket4";
166 TestCompletionCallback callback_s4; 144 TestCompletionCallback callback_s4;
167 // Trying to open connection to host4 will wait for w1, w2. 145 // Trying to open connection to host4 will wait for w1, w2.
168 EXPECT_EQ(ERR_IO_PENDING, 146 EXPECT_EQ(ERR_IO_PENDING,
169 w4->OnStartOpenConnection(s4, callback_s4.callback())); 147 w4->OnStartOpenConnection(s4, callback_s4.callback()));
170 // Address | head -> tail 148 // Address | head -> tail
171 // 1.2.3.4 | w1 w2 w4 149 // 1.2.3.4 | w1 w2 w4
172 // 1.2.3.5 | w1 w3 150 // 1.2.3.5 | w1 w3
173 // 1.2.3.6 | w1 w4 151 // 1.2.3.6 | w1 w4
174 152
175 // For host5: 1.2.3.6 153 // For host5: 1.2.3.6
176 addr = AddAddr(1, 2, 3, 6, NULL); 154 addr.clear();
155 addr.push_back(MakeAddr(1, 2, 3, 6));
177 scoped_refptr<WebSocketJob> w5(new WebSocketJob(&delegate)); 156 scoped_refptr<WebSocketJob> w5(new WebSocketJob(&delegate));
178 scoped_refptr<SocketStream> s5( 157 scoped_refptr<SocketStream> s5(
179 new SocketStream(GURL("ws://host5/"), w5.get())); 158 new SocketStream(GURL("ws://host5/"), w5.get()));
180 s5->set_context(context.get()); 159 s5->set_context(context.get());
181 w5->InitSocketStream(s5.get()); 160 w5->InitSocketStream(s5.get());
182 WebSocketThrottleTest::MockSocketStreamConnect(s5, addr); 161 WebSocketThrottleTest::MockSocketStreamConnect(s5, addr);
183 DeleteAddrInfo(addr);
184 162
185 DVLOG(1) << "socket5"; 163 DVLOG(1) << "socket5";
186 TestCompletionCallback callback_s5; 164 TestCompletionCallback callback_s5;
187 // Trying to open connection to host5 will wait for w1, w4 165 // Trying to open connection to host5 will wait for w1, w4
188 EXPECT_EQ(ERR_IO_PENDING, 166 EXPECT_EQ(ERR_IO_PENDING,
189 w5->OnStartOpenConnection(s5, callback_s5.callback())); 167 w5->OnStartOpenConnection(s5, callback_s5.callback()));
190 // Address | head -> tail 168 // Address | head -> tail
191 // 1.2.3.4 | w1 w2 w4 169 // 1.2.3.4 | w1 w2 w4
192 // 1.2.3.5 | w1 w3 170 // 1.2.3.5 | w1 w3
193 // 1.2.3.6 | w1 w4 w5 171 // 1.2.3.6 | w1 w4 w5
194 172
195 // For host6: 1.2.3.6 173 // For host6: 1.2.3.6
196 addr = AddAddr(1, 2, 3, 6, NULL); 174 addr.clear();
175 addr.push_back(MakeAddr(1, 2, 3, 6));
197 scoped_refptr<WebSocketJob> w6(new WebSocketJob(&delegate)); 176 scoped_refptr<WebSocketJob> w6(new WebSocketJob(&delegate));
198 scoped_refptr<SocketStream> s6( 177 scoped_refptr<SocketStream> s6(
199 new SocketStream(GURL("ws://host6/"), w6.get())); 178 new SocketStream(GURL("ws://host6/"), w6.get()));
200 s6->set_context(context.get()); 179 s6->set_context(context.get());
201 w6->InitSocketStream(s6.get()); 180 w6->InitSocketStream(s6.get());
202 WebSocketThrottleTest::MockSocketStreamConnect(s6, addr); 181 WebSocketThrottleTest::MockSocketStreamConnect(s6, addr);
203 DeleteAddrInfo(addr);
204 182
205 DVLOG(1) << "socket6"; 183 DVLOG(1) << "socket6";
206 TestCompletionCallback callback_s6; 184 TestCompletionCallback callback_s6;
207 // Trying to open connection to host6 will wait for w1, w4, w5 185 // Trying to open connection to host6 will wait for w1, w4, w5
208 EXPECT_EQ(ERR_IO_PENDING, 186 EXPECT_EQ(ERR_IO_PENDING,
209 w6->OnStartOpenConnection(s6, callback_s6.callback())); 187 w6->OnStartOpenConnection(s6, callback_s6.callback()));
210 // Address | head -> tail 188 // Address | head -> tail
211 // 1.2.3.4 | w1 w2 w4 189 // 1.2.3.4 | w1 w2 w4
212 // 1.2.3.5 | w1 w3 190 // 1.2.3.5 | w1 w3
213 // 1.2.3.6 | w1 w4 w5 w6 191 // 1.2.3.6 | w1 w4 w5 w6
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 DVLOG(1) << "Done"; 276 DVLOG(1) << "Done";
299 MessageLoopForIO::current()->RunAllPending(); 277 MessageLoopForIO::current()->RunAllPending();
300 } 278 }
301 279
302 TEST_F(WebSocketThrottleTest, NoThrottleForDuplicateAddress) { 280 TEST_F(WebSocketThrottleTest, NoThrottleForDuplicateAddress) {
303 scoped_refptr<URLRequestContext> context(new TestURLRequestContext); 281 scoped_refptr<URLRequestContext> context(new TestURLRequestContext);
304 DummySocketStreamDelegate delegate; 282 DummySocketStreamDelegate delegate;
305 WebSocketJob::set_websocket_over_spdy_enabled(true); 283 WebSocketJob::set_websocket_over_spdy_enabled(true);
306 284
307 // For localhost: 127.0.0.1, 127.0.0.1 285 // For localhost: 127.0.0.1, 127.0.0.1
308 struct addrinfo* addr = AddAddr(127, 0, 0, 1, NULL); 286 AddressList addr;
309 addr = AddAddr(127, 0, 0, 1, addr); 287 addr.push_back(MakeAddr(127, 0, 0, 1));
288 addr.push_back(MakeAddr(127, 0, 0, 1));
310 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); 289 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate));
311 scoped_refptr<SocketStream> s1( 290 scoped_refptr<SocketStream> s1(
312 new SocketStream(GURL("ws://localhost/"), w1.get())); 291 new SocketStream(GURL("ws://localhost/"), w1.get()));
313 s1->set_context(context.get()); 292 s1->set_context(context.get());
314 w1->InitSocketStream(s1.get()); 293 w1->InitSocketStream(s1.get());
315 WebSocketThrottleTest::MockSocketStreamConnect(s1, addr); 294 WebSocketThrottleTest::MockSocketStreamConnect(s1, addr);
316 DeleteAddrInfo(addr);
317 295
318 DVLOG(1) << "socket1"; 296 DVLOG(1) << "socket1";
319 TestCompletionCallback callback_s1; 297 TestCompletionCallback callback_s1;
320 // Trying to open connection to localhost will start without wait. 298 // Trying to open connection to localhost will start without wait.
321 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1, callback_s1.callback())); 299 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1, callback_s1.callback()));
322 300
323 DVLOG(1) << "socket1 close"; 301 DVLOG(1) << "socket1 close";
324 w1->OnClose(s1.get()); 302 w1->OnClose(s1.get());
325 s1->DetachDelegate(); 303 s1->DetachDelegate();
326 DVLOG(1) << "Done"; 304 DVLOG(1) << "Done";
327 MessageLoopForIO::current()->RunAllPending(); 305 MessageLoopForIO::current()->RunAllPending();
328 } 306 }
329 307
330 } 308 }
OLDNEW
« no previous file with comments | « net/websockets/websocket_throttle.cc ('k') | ppapi/shared_impl/private/net_address_private_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698