OLD | NEW |
1 // Copyright (c) 2012 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_job.h" | 5 #include "net/websockets/websocket_job.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 #include "testing/gtest/include/gtest/gtest.h" | 36 #include "testing/gtest/include/gtest/gtest.h" |
37 #include "testing/platform_test.h" | 37 #include "testing/platform_test.h" |
38 | 38 |
39 using namespace net::test_spdy3; | 39 using namespace net::test_spdy3; |
40 | 40 |
41 namespace { | 41 namespace { |
42 | 42 |
43 class MockSocketStream : public net::SocketStream { | 43 class MockSocketStream : public net::SocketStream { |
44 public: | 44 public: |
45 MockSocketStream(const GURL& url, net::SocketStream::Delegate* delegate) | 45 MockSocketStream(const GURL& url, net::SocketStream::Delegate* delegate) |
46 : SocketStream(url, delegate) {} | 46 : SocketStream(url, delegate) { |
47 virtual ~MockSocketStream() {} | 47 } |
48 | 48 |
49 virtual void Connect() OVERRIDE {} | 49 virtual void Connect() OVERRIDE {} |
50 virtual bool SendData(const char* data, int len) OVERRIDE { | 50 virtual bool SendData(const char* data, int len) OVERRIDE { |
51 sent_data_ += std::string(data, len); | 51 sent_data_ += std::string(data, len); |
52 return true; | 52 return true; |
53 } | 53 } |
54 | 54 |
55 virtual void Close() OVERRIDE {} | 55 virtual void Close() OVERRIDE {} |
56 virtual void RestartWithAuth( | 56 virtual void RestartWithAuth( |
57 const net::AuthCredentials& credentials) OVERRIDE { | 57 const net::AuthCredentials& credentials) OVERRIDE { |
58 } | 58 } |
59 | 59 |
60 virtual void DetachDelegate() OVERRIDE { | 60 virtual void DetachDelegate() OVERRIDE { |
61 delegate_ = NULL; | 61 delegate_ = NULL; |
62 } | 62 } |
63 | 63 |
64 const std::string& sent_data() const { | 64 const std::string& sent_data() const { |
65 return sent_data_; | 65 return sent_data_; |
66 } | 66 } |
67 | 67 |
| 68 protected: |
| 69 virtual ~MockSocketStream() {} |
| 70 |
68 private: | 71 private: |
69 std::string sent_data_; | 72 std::string sent_data_; |
70 }; | 73 }; |
71 | 74 |
72 class MockSocketStreamDelegate : public net::SocketStream::Delegate { | 75 class MockSocketStreamDelegate : public net::SocketStream::Delegate { |
73 public: | 76 public: |
74 MockSocketStreamDelegate() | 77 MockSocketStreamDelegate() |
75 : amount_sent_(0), allow_all_cookies_(true) {} | 78 : amount_sent_(0), |
| 79 allow_all_cookies_(true) { |
| 80 } |
| 81 |
76 void set_allow_all_cookies(bool allow_all_cookies) { | 82 void set_allow_all_cookies(bool allow_all_cookies) { |
77 allow_all_cookies_ = allow_all_cookies; | 83 allow_all_cookies_ = allow_all_cookies; |
78 } | 84 } |
79 virtual ~MockSocketStreamDelegate() {} | 85 virtual ~MockSocketStreamDelegate() {} |
80 | 86 |
81 void SetOnStartOpenConnection(const base::Closure& callback) { | 87 void SetOnStartOpenConnection(const base::Closure& callback) { |
82 on_start_open_connection_ = callback; | 88 on_start_open_connection_ = callback; |
83 } | 89 } |
84 void SetOnConnected(const base::Closure& callback) { | 90 void SetOnConnected(const base::Closure& callback) { |
85 on_connected_ = callback; | 91 on_connected_ = callback; |
86 } | 92 } |
87 void SetOnSentData(const base::Closure& callback) { | 93 void SetOnSentData(const base::Closure& callback) { |
88 on_sent_data_ = callback; | 94 on_sent_data_ = callback; |
89 } | 95 } |
90 void SetOnReceivedData(const base::Closure& callback) { | 96 void SetOnReceivedData(const base::Closure& callback) { |
91 on_received_data_ = callback; | 97 on_received_data_ = callback; |
92 } | 98 } |
93 void SetOnClose(const base::Closure& callback) { | 99 void SetOnClose(const base::Closure& callback) { |
94 on_close_ = callback; | 100 on_close_ = callback; |
95 } | 101 } |
96 | 102 |
97 virtual int OnStartOpenConnection(net::SocketStream* socket, | 103 virtual int OnStartOpenConnection( |
98 const net::CompletionCallback& callback) { | 104 net::SocketStream* socket, |
| 105 const net::CompletionCallback& callback) OVERRIDE { |
99 if (!on_start_open_connection_.is_null()) | 106 if (!on_start_open_connection_.is_null()) |
100 on_start_open_connection_.Run(); | 107 on_start_open_connection_.Run(); |
101 return net::OK; | 108 return net::OK; |
102 } | 109 } |
103 virtual void OnConnected(net::SocketStream* socket, | 110 virtual void OnConnected(net::SocketStream* socket, |
104 int max_pending_send_allowed) { | 111 int max_pending_send_allowed) OVERRIDE { |
105 if (!on_connected_.is_null()) | 112 if (!on_connected_.is_null()) |
106 on_connected_.Run(); | 113 on_connected_.Run(); |
107 } | 114 } |
108 virtual void OnSentData(net::SocketStream* socket, int amount_sent) { | 115 virtual void OnSentData(net::SocketStream* socket, |
| 116 int amount_sent) OVERRIDE { |
109 amount_sent_ += amount_sent; | 117 amount_sent_ += amount_sent; |
110 if (!on_sent_data_.is_null()) | 118 if (!on_sent_data_.is_null()) |
111 on_sent_data_.Run(); | 119 on_sent_data_.Run(); |
112 } | 120 } |
113 virtual void OnReceivedData(net::SocketStream* socket, | 121 virtual void OnReceivedData(net::SocketStream* socket, |
114 const char* data, int len) { | 122 const char* data, int len) OVERRIDE { |
115 received_data_ += std::string(data, len); | 123 received_data_ += std::string(data, len); |
116 if (!on_received_data_.is_null()) | 124 if (!on_received_data_.is_null()) |
117 on_received_data_.Run(); | 125 on_received_data_.Run(); |
118 } | 126 } |
119 virtual void OnClose(net::SocketStream* socket) { | 127 virtual void OnClose(net::SocketStream* socket) { |
120 if (!on_close_.is_null()) | 128 if (!on_close_.is_null()) |
121 on_close_.Run(); | 129 on_close_.Run(); |
122 } | 130 } |
123 virtual bool CanGetCookies(net::SocketStream* socket, const GURL& url) { | 131 virtual bool CanGetCookies(net::SocketStream* socket, |
| 132 const GURL& url) OVERRIDE { |
124 return allow_all_cookies_; | 133 return allow_all_cookies_; |
125 } | 134 } |
126 virtual bool CanSetCookie(net::SocketStream* request, | 135 virtual bool CanSetCookie(net::SocketStream* request, |
127 const GURL& url, | 136 const GURL& url, |
128 const std::string& cookie_line, | 137 const std::string& cookie_line, |
129 net::CookieOptions* options) { | 138 net::CookieOptions* options) OVERRIDE { |
130 return allow_all_cookies_; | 139 return allow_all_cookies_; |
131 } | 140 } |
132 | 141 |
133 size_t amount_sent() const { return amount_sent_; } | 142 size_t amount_sent() const { return amount_sent_; } |
134 const std::string& received_data() const { return received_data_; } | 143 const std::string& received_data() const { return received_data_; } |
135 | 144 |
136 private: | 145 private: |
137 int amount_sent_; | 146 int amount_sent_; |
138 bool allow_all_cookies_; | 147 bool allow_all_cookies_; |
139 std::string received_data_; | 148 std::string received_data_; |
140 base::Closure on_start_open_connection_; | 149 base::Closure on_start_open_connection_; |
141 base::Closure on_connected_; | 150 base::Closure on_connected_; |
142 base::Closure on_sent_data_; | 151 base::Closure on_sent_data_; |
143 base::Closure on_received_data_; | 152 base::Closure on_received_data_; |
144 base::Closure on_close_; | 153 base::Closure on_close_; |
145 }; | 154 }; |
146 | 155 |
147 class MockCookieStore : public net::CookieStore { | 156 class MockCookieStore : public net::CookieStore { |
148 public: | 157 public: |
149 struct Entry { | 158 struct Entry { |
150 GURL url; | 159 GURL url; |
151 std::string cookie_line; | 160 std::string cookie_line; |
152 net::CookieOptions options; | 161 net::CookieOptions options; |
153 }; | 162 }; |
| 163 |
154 MockCookieStore() {} | 164 MockCookieStore() {} |
155 | 165 |
156 virtual bool SetCookieWithOptions(const GURL& url, | 166 bool SetCookieWithOptions(const GURL& url, |
157 const std::string& cookie_line, | 167 const std::string& cookie_line, |
158 const net::CookieOptions& options) { | 168 const net::CookieOptions& options) { |
159 Entry entry; | 169 Entry entry; |
160 entry.url = url; | 170 entry.url = url; |
161 entry.cookie_line = cookie_line; | 171 entry.cookie_line = cookie_line; |
162 entry.options = options; | 172 entry.options = options; |
163 entries_.push_back(entry); | 173 entries_.push_back(entry); |
164 return true; | 174 return true; |
165 } | 175 } |
166 | 176 |
167 virtual void SetCookieWithOptionsAsync( | 177 std::string GetCookiesWithOptions(const GURL& url, |
168 const GURL& url, | 178 const net::CookieOptions& options) { |
169 const std::string& cookie_line, | |
170 const net::CookieOptions& options, | |
171 const SetCookiesCallback& callback) { | |
172 bool result = SetCookieWithOptions(url, cookie_line, options); | |
173 if (!callback.is_null()) | |
174 callback.Run(result); | |
175 } | |
176 virtual std::string GetCookiesWithOptions( | |
177 const GURL& url, | |
178 const net::CookieOptions& options) { | |
179 std::string result; | 179 std::string result; |
180 for (size_t i = 0; i < entries_.size(); i++) { | 180 for (size_t i = 0; i < entries_.size(); i++) { |
181 Entry &entry = entries_[i]; | 181 Entry &entry = entries_[i]; |
182 if (url == entry.url) { | 182 if (url == entry.url) { |
183 if (!result.empty()) { | 183 if (!result.empty()) { |
184 result += "; "; | 184 result += "; "; |
185 } | 185 } |
186 result += entry.cookie_line; | 186 result += entry.cookie_line; |
187 } | 187 } |
188 } | 188 } |
189 return result; | 189 return result; |
190 } | 190 } |
| 191 |
| 192 // CookieStore: |
| 193 virtual void SetCookieWithOptionsAsync( |
| 194 const GURL& url, |
| 195 const std::string& cookie_line, |
| 196 const net::CookieOptions& options, |
| 197 const SetCookiesCallback& callback) OVERRIDE { |
| 198 bool result = SetCookieWithOptions(url, cookie_line, options); |
| 199 if (!callback.is_null()) |
| 200 callback.Run(result); |
| 201 } |
| 202 |
191 virtual void GetCookiesWithOptionsAsync( | 203 virtual void GetCookiesWithOptionsAsync( |
192 const GURL& url, | 204 const GURL& url, |
193 const net::CookieOptions& options, | 205 const net::CookieOptions& options, |
194 const GetCookiesCallback& callback) { | 206 const GetCookiesCallback& callback) OVERRIDE { |
195 if (!callback.is_null()) | 207 if (!callback.is_null()) |
196 callback.Run(GetCookiesWithOptions(url, options)); | 208 callback.Run(GetCookiesWithOptions(url, options)); |
197 } | 209 } |
198 virtual void GetCookiesWithInfo(const GURL& url, | 210 |
199 const net::CookieOptions& options, | |
200 std::string* cookie_line, | |
201 std::vector<CookieInfo>* cookie_infos) { | |
202 ADD_FAILURE(); | |
203 } | |
204 virtual void GetCookiesWithInfoAsync( | 211 virtual void GetCookiesWithInfoAsync( |
205 const GURL& url, | 212 const GURL& url, |
206 const net::CookieOptions& options, | 213 const net::CookieOptions& options, |
207 const GetCookieInfoCallback& callback) { | 214 const GetCookieInfoCallback& callback) OVERRIDE { |
208 ADD_FAILURE(); | |
209 } | |
210 virtual void DeleteCookie(const GURL& url, | |
211 const std::string& cookie_name) { | |
212 ADD_FAILURE(); | |
213 } | |
214 virtual void DeleteCookieAsync(const GURL& url, | |
215 const std::string& cookie_name, | |
216 const base::Closure& callback) { | |
217 ADD_FAILURE(); | |
218 } | |
219 virtual void DeleteAllCreatedBetweenAsync(const base::Time& delete_begin, | |
220 const base::Time& delete_end, | |
221 const DeleteCallback& callback) { | |
222 ADD_FAILURE(); | |
223 } | |
224 virtual void DeleteSessionCookiesAsync(const DeleteCallback&) { | |
225 ADD_FAILURE(); | 215 ADD_FAILURE(); |
226 } | 216 } |
227 | 217 |
228 virtual net::CookieMonster* GetCookieMonster() { return NULL; } | 218 virtual void DeleteCookieAsync(const GURL& url, |
| 219 const std::string& cookie_name, |
| 220 const base::Closure& callback) OVERRIDE { |
| 221 ADD_FAILURE(); |
| 222 } |
| 223 |
| 224 virtual void DeleteAllCreatedBetweenAsync( |
| 225 const base::Time& delete_begin, |
| 226 const base::Time& delete_end, |
| 227 const DeleteCallback& callback) OVERRIDE { |
| 228 ADD_FAILURE(); |
| 229 } |
| 230 |
| 231 virtual void DeleteSessionCookiesAsync(const DeleteCallback&) OVERRIDE { |
| 232 ADD_FAILURE(); |
| 233 } |
| 234 |
| 235 virtual net::CookieMonster* GetCookieMonster() OVERRIDE { return NULL; } |
229 | 236 |
230 const std::vector<Entry>& entries() const { return entries_; } | 237 const std::vector<Entry>& entries() const { return entries_; } |
231 | 238 |
232 private: | 239 private: |
233 friend class base::RefCountedThreadSafe<MockCookieStore>; | 240 friend class base::RefCountedThreadSafe<MockCookieStore>; |
234 virtual ~MockCookieStore() {} | 241 virtual ~MockCookieStore() {} |
235 | 242 |
236 std::vector<Entry> entries_; | 243 std::vector<Entry> entries_; |
237 }; | 244 }; |
238 | 245 |
239 class MockSSLConfigService : public net::SSLConfigService { | 246 class MockSSLConfigService : public net::SSLConfigService { |
240 public: | 247 public: |
241 virtual void GetSSLConfig(net::SSLConfig* config) {}; | 248 virtual void GetSSLConfig(net::SSLConfig* config) OVERRIDE {} |
| 249 |
| 250 protected: |
| 251 virtual ~MockSSLConfigService() {} |
242 }; | 252 }; |
243 | 253 |
244 class MockURLRequestContext : public net::URLRequestContext { | 254 class MockURLRequestContext : public net::URLRequestContext { |
245 public: | 255 public: |
246 explicit MockURLRequestContext(net::CookieStore* cookie_store) | 256 explicit MockURLRequestContext(net::CookieStore* cookie_store) |
247 : transport_security_state_(std::string()) { | 257 : transport_security_state_(std::string()) { |
248 set_cookie_store(cookie_store); | 258 set_cookie_store(cookie_store); |
249 set_transport_security_state(&transport_security_state_); | 259 set_transport_security_state(&transport_security_state_); |
250 net::TransportSecurityState::DomainState state; | 260 net::TransportSecurityState::DomainState state; |
251 state.expiry = base::Time::Now() + base::TimeDelta::FromSeconds(1000); | 261 state.expiry = base::Time::Now() + base::TimeDelta::FromSeconds(1000); |
252 transport_security_state_.EnableHost("upgrademe.com", state); | 262 transport_security_state_.EnableHost("upgrademe.com", state); |
253 } | 263 } |
254 | 264 |
255 private: | 265 protected: |
256 friend class base::RefCountedThreadSafe<MockURLRequestContext>; | 266 friend class base::RefCountedThreadSafe<MockURLRequestContext>; |
257 virtual ~MockURLRequestContext() {} | 267 virtual ~MockURLRequestContext() {} |
258 | 268 |
| 269 private: |
259 net::TransportSecurityState transport_security_state_; | 270 net::TransportSecurityState transport_security_state_; |
260 }; | 271 }; |
261 | 272 |
262 class MockHttpTransactionFactory : public net::HttpTransactionFactory { | 273 class MockHttpTransactionFactory : public net::HttpTransactionFactory { |
263 public: | 274 public: |
264 MockHttpTransactionFactory(net::OrderedSocketData* data) { | 275 MockHttpTransactionFactory(net::OrderedSocketData* data) { |
265 data_ = data; | 276 data_ = data; |
266 net::MockConnect connect_data(net::SYNCHRONOUS, net::OK); | 277 net::MockConnect connect_data(net::SYNCHRONOUS, net::OK); |
267 data_->set_connect_data(connect_data); | 278 data_->set_connect_data(connect_data); |
268 session_deps_.reset(new SpdySessionDependencies); | 279 session_deps_.reset(new SpdySessionDependencies); |
(...skipping 828 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1097 | 1108 |
1098 TEST_F(WebSocketJobSpdy3Test, ThrottlingSpdySpdyEnabled) { | 1109 TEST_F(WebSocketJobSpdy3Test, ThrottlingSpdySpdyEnabled) { |
1099 WebSocketJob::set_websocket_over_spdy_enabled(true); | 1110 WebSocketJob::set_websocket_over_spdy_enabled(true); |
1100 TestConnectBySpdy(SPDY_ON, THROTTLING_ON); | 1111 TestConnectBySpdy(SPDY_ON, THROTTLING_ON); |
1101 } | 1112 } |
1102 | 1113 |
1103 // TODO(toyoshim): Add tests to verify throttling, SPDY stream limitation. | 1114 // TODO(toyoshim): Add tests to verify throttling, SPDY stream limitation. |
1104 // TODO(toyoshim,yutak): Add tests to verify closing handshake. | 1115 // TODO(toyoshim,yutak): Add tests to verify closing handshake. |
1105 | 1116 |
1106 } // namespace net | 1117 } // namespace net |
OLD | NEW |