| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_test_util.h" | 5 #include "net/websockets/websocket_test_util.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "net/proxy/proxy_service.h" |
| 14 #include "net/socket/socket_test_util.h" | 15 #include "net/socket/socket_test_util.h" |
| 15 | 16 |
| 16 namespace net { | 17 namespace net { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 const uint64 kA = | 20 const uint64 kA = |
| 20 (static_cast<uint64>(0x5851f42d) << 32) + static_cast<uint64>(0x4c957f2d); | 21 (static_cast<uint64>(0x5851f42d) << 32) + static_cast<uint64>(0x4c957f2d); |
| 21 const uint64 kC = 12345; | 22 const uint64 kC = 12345; |
| 22 const uint64 kM = static_cast<uint64>(1) << 48; | 23 const uint64 kM = static_cast<uint64>(1) << 48; |
| 23 | 24 |
| 24 } // namespace | 25 } // namespace |
| 25 | 26 |
| 26 LinearCongruentialGenerator::LinearCongruentialGenerator(uint32 seed) | 27 LinearCongruentialGenerator::LinearCongruentialGenerator(uint32 seed) |
| 27 : current_(seed) {} | 28 : current_(seed) {} |
| 28 | 29 |
| 29 uint32 LinearCongruentialGenerator::Generate() { | 30 uint32 LinearCongruentialGenerator::Generate() { |
| 30 uint64 result = current_; | 31 uint64 result = current_; |
| 31 current_ = (current_ * kA + kC) % kM; | 32 current_ = (current_ * kA + kC) % kM; |
| 32 return static_cast<uint32>(result >> 16); | 33 return static_cast<uint32>(result >> 16); |
| 33 } | 34 } |
| 34 | 35 |
| 35 std::string WebSocketStandardRequest(const std::string& path, | 36 std::string WebSocketStandardRequest(const std::string& path, |
| 36 const std::string& host, | 37 const std::string& host, |
| 37 const std::string& origin, | 38 const std::string& origin, |
| 38 const std::string& extra_headers) { | 39 const std::string& extra_headers) { |
| 40 return WebSocketStandardRequestWithCookies(path, host, origin, std::string(), |
| 41 extra_headers); |
| 42 } |
| 43 |
| 44 std::string WebSocketStandardRequestWithCookies( |
| 45 const std::string& path, |
| 46 const std::string& host, |
| 47 const std::string& origin, |
| 48 const std::string& cookies, |
| 49 const std::string& extra_headers) { |
| 39 // Unrelated changes in net/http may change the order and default-values of | 50 // Unrelated changes in net/http may change the order and default-values of |
| 40 // HTTP headers, causing WebSocket tests to fail. It is safe to update this | 51 // HTTP headers, causing WebSocket tests to fail. It is safe to update this |
| 41 // string in that case. | 52 // string in that case. |
| 42 return base::StringPrintf( | 53 return base::StringPrintf( |
| 43 "GET %s HTTP/1.1\r\n" | 54 "GET %s HTTP/1.1\r\n" |
| 44 "Host: %s\r\n" | 55 "Host: %s\r\n" |
| 45 "Connection: Upgrade\r\n" | 56 "Connection: Upgrade\r\n" |
| 46 "Pragma: no-cache\r\n" | 57 "Pragma: no-cache\r\n" |
| 47 "Cache-Control: no-cache\r\n" | 58 "Cache-Control: no-cache\r\n" |
| 48 "Upgrade: websocket\r\n" | 59 "Upgrade: websocket\r\n" |
| 49 "Origin: %s\r\n" | 60 "Origin: %s\r\n" |
| 50 "Sec-WebSocket-Version: 13\r\n" | 61 "Sec-WebSocket-Version: 13\r\n" |
| 51 "User-Agent:\r\n" | 62 "User-Agent:\r\n" |
| 52 "Accept-Encoding: gzip, deflate\r\n" | 63 "Accept-Encoding: gzip, deflate\r\n" |
| 53 "Accept-Language: en-us,fr\r\n" | 64 "Accept-Language: en-us,fr\r\n" |
| 65 "%s" |
| 54 "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" | 66 "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" |
| 55 "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n" | 67 "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n" |
| 56 "%s\r\n", | 68 "%s\r\n", |
| 57 path.c_str(), host.c_str(), origin.c_str(), extra_headers.c_str()); | 69 path.c_str(), host.c_str(), origin.c_str(), cookies.c_str(), |
| 70 extra_headers.c_str()); |
| 58 } | 71 } |
| 59 | 72 |
| 60 std::string WebSocketStandardResponse(const std::string& extra_headers) { | 73 std::string WebSocketStandardResponse(const std::string& extra_headers) { |
| 61 return base::StringPrintf( | 74 return base::StringPrintf( |
| 62 "HTTP/1.1 101 Switching Protocols\r\n" | 75 "HTTP/1.1 101 Switching Protocols\r\n" |
| 63 "Upgrade: websocket\r\n" | 76 "Upgrade: websocket\r\n" |
| 64 "Connection: Upgrade\r\n" | 77 "Connection: Upgrade\r\n" |
| 65 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n" | 78 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n" |
| 66 "%s\r\n", | 79 "%s\r\n", |
| 67 extra_headers.c_str()); | 80 extra_headers.c_str()); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 void WebSocketTestURLRequestContextHost::AddRawExpectations( | 157 void WebSocketTestURLRequestContextHost::AddRawExpectations( |
| 145 scoped_ptr<DeterministicSocketData> socket_data) { | 158 scoped_ptr<DeterministicSocketData> socket_data) { |
| 146 maker_.AddRawExpectations(socket_data.Pass()); | 159 maker_.AddRawExpectations(socket_data.Pass()); |
| 147 } | 160 } |
| 148 | 161 |
| 149 void WebSocketTestURLRequestContextHost::AddSSLSocketDataProvider( | 162 void WebSocketTestURLRequestContextHost::AddSSLSocketDataProvider( |
| 150 scoped_ptr<SSLSocketDataProvider> ssl_socket_data) { | 163 scoped_ptr<SSLSocketDataProvider> ssl_socket_data) { |
| 151 maker_.AddSSLSocketDataProvider(ssl_socket_data.Pass()); | 164 maker_.AddSSLSocketDataProvider(ssl_socket_data.Pass()); |
| 152 } | 165 } |
| 153 | 166 |
| 167 void WebSocketTestURLRequestContextHost::SetProxyConfig( |
| 168 const std::string& proxy_rules) { |
| 169 DCHECK(!url_request_context_initialized_); |
| 170 proxy_service_.reset(ProxyService::CreateFixed(proxy_rules)); |
| 171 url_request_context_.set_proxy_service(proxy_service_.get()); |
| 172 } |
| 173 |
| 154 TestURLRequestContext* | 174 TestURLRequestContext* |
| 155 WebSocketTestURLRequestContextHost::GetURLRequestContext() { | 175 WebSocketTestURLRequestContextHost::GetURLRequestContext() { |
| 156 if (!url_request_context_initialized_) { | 176 if (!url_request_context_initialized_) { |
| 157 url_request_context_.Init(); | 177 url_request_context_.Init(); |
| 158 // A Network Delegate is required to make the URLRequest::Delegate work. | 178 // A Network Delegate is required to make the URLRequest::Delegate work. |
| 159 url_request_context_.set_network_delegate(&network_delegate_); | 179 url_request_context_.set_network_delegate(&network_delegate_); |
| 160 url_request_context_initialized_ = true; | 180 url_request_context_initialized_ = true; |
| 161 } | 181 } |
| 162 return &url_request_context_; | 182 return &url_request_context_; |
| 163 } | 183 } |
| 164 | 184 |
| 165 } // namespace net | 185 } // namespace net |
| OLD | NEW |