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/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "net/socket/socket_test_util.h" | 13 #include "net/socket/socket_test_util.h" |
14 | 14 |
15 namespace net { | 15 namespace net { |
16 | 16 |
17 namespace { | 17 namespace { |
18 const uint64 kA = | 18 const uint64 kA = |
19 (static_cast<uint64>(0x5851f42d) << 32) + static_cast<uint64>(0x4c957f2d); | 19 (static_cast<uint64>(0x5851f42d) << 32) + static_cast<uint64>(0x4c957f2d); |
20 const uint64 kC = 12345; | 20 const uint64 kC = 12345; |
21 const uint64 kM = static_cast<uint64>(1) << 48; | 21 const uint64 kM = static_cast<uint64>(1) << 48; |
22 | 22 |
23 } // namespace | 23 } // namespace |
24 | 24 |
25 LinearCongruentialGenerator::LinearCongruentialGenerator(uint32 seed) | 25 LinearCongruentialGenerator::LinearCongruentialGenerator(uint32 seed) |
26 : current_(seed) {} | 26 : current_(seed) { |
| 27 } |
27 | 28 |
28 uint32 LinearCongruentialGenerator::Generate() { | 29 uint32 LinearCongruentialGenerator::Generate() { |
29 uint64 result = current_; | 30 uint64 result = current_; |
30 current_ = (current_ * kA + kC) % kM; | 31 current_ = (current_ * kA + kC) % kM; |
31 return static_cast<uint32>(result >> 16); | 32 return static_cast<uint32>(result >> 16); |
32 } | 33 } |
33 | 34 |
34 std::string WebSocketStandardRequest(const std::string& path, | 35 std::string WebSocketStandardRequest(const std::string& path, |
35 const std::string& origin, | 36 const std::string& origin, |
36 const std::string& extra_headers) { | 37 const std::string& extra_headers) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 std::string expect_written; | 72 std::string expect_written; |
72 std::string return_to_read; | 73 std::string return_to_read; |
73 std::vector<MockRead> reads; | 74 std::vector<MockRead> reads; |
74 MockWrite write; | 75 MockWrite write; |
75 scoped_ptr<DeterministicSocketData> data; | 76 scoped_ptr<DeterministicSocketData> data; |
76 DeterministicMockClientSocketFactory factory; | 77 DeterministicMockClientSocketFactory factory; |
77 }; | 78 }; |
78 | 79 |
79 WebSocketDeterministicMockClientSocketFactoryMaker:: | 80 WebSocketDeterministicMockClientSocketFactoryMaker:: |
80 WebSocketDeterministicMockClientSocketFactoryMaker() | 81 WebSocketDeterministicMockClientSocketFactoryMaker() |
81 : detail_(new Detail) {} | 82 : detail_(new Detail) { |
| 83 } |
82 | 84 |
83 WebSocketDeterministicMockClientSocketFactoryMaker:: | 85 WebSocketDeterministicMockClientSocketFactoryMaker:: |
84 ~WebSocketDeterministicMockClientSocketFactoryMaker() {} | 86 ~WebSocketDeterministicMockClientSocketFactoryMaker() { |
| 87 } |
85 | 88 |
86 DeterministicMockClientSocketFactory* | 89 DeterministicMockClientSocketFactory* |
87 WebSocketDeterministicMockClientSocketFactoryMaker::factory() { | 90 WebSocketDeterministicMockClientSocketFactoryMaker::factory() { |
88 return &detail_->factory; | 91 return &detail_->factory; |
89 } | 92 } |
90 | 93 |
91 void WebSocketDeterministicMockClientSocketFactoryMaker::SetExpectations( | 94 void WebSocketDeterministicMockClientSocketFactoryMaker::SetExpectations( |
92 const std::string& expect_written, | 95 const std::string& expect_written, |
93 const std::string& return_to_read) { | 96 const std::string& return_to_read) { |
94 const size_t kHttpStreamParserBufferSize = 4096; | 97 const size_t kHttpStreamParserBufferSize = 4096; |
95 // We need to extend the lifetime of these strings. | 98 // We need to extend the lifetime of these strings. |
96 detail_->expect_written = expect_written; | 99 detail_->expect_written = expect_written; |
97 detail_->return_to_read = return_to_read; | 100 detail_->return_to_read = return_to_read; |
98 int sequence = 0; | 101 int sequence = 0; |
99 detail_->write = MockWrite(SYNCHRONOUS, | 102 detail_->write = MockWrite(SYNCHRONOUS, |
100 detail_->expect_written.data(), | 103 detail_->expect_written.data(), |
101 detail_->expect_written.size(), | 104 detail_->expect_written.size(), |
102 sequence++); | 105 sequence++); |
103 // HttpStreamParser reads 4KB at a time. We need to take this implementation | 106 // HttpStreamParser reads 4KB at a time. We need to take this implementation |
104 // detail into account if |return_to_read| is big enough. | 107 // detail into account if |return_to_read| is big enough. |
105 for (size_t place = 0; place < detail_->return_to_read.size(); | 108 for (size_t place = 0; place < detail_->return_to_read.size(); |
106 place += kHttpStreamParserBufferSize) { | 109 place += kHttpStreamParserBufferSize) { |
107 detail_->reads.push_back( | 110 detail_->reads.push_back( |
108 MockRead(SYNCHRONOUS, detail_->return_to_read.data() + place, | 111 MockRead(SYNCHRONOUS, |
| 112 detail_->return_to_read.data() + place, |
109 std::min(detail_->return_to_read.size() - place, | 113 std::min(detail_->return_to_read.size() - place, |
110 kHttpStreamParserBufferSize), | 114 kHttpStreamParserBufferSize), |
111 sequence++)); | 115 sequence++)); |
112 } | 116 } |
113 scoped_ptr<DeterministicSocketData> socket_data( | 117 scoped_ptr<DeterministicSocketData> socket_data( |
114 new DeterministicSocketData(vector_as_array(&detail_->reads), | 118 new DeterministicSocketData(vector_as_array(&detail_->reads), |
115 detail_->reads.size(), | 119 detail_->reads.size(), |
116 &detail_->write, | 120 &detail_->write, |
117 1)); | 121 1)); |
118 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); | 122 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); |
119 socket_data->SetStop(sequence); | 123 socket_data->SetStop(sequence); |
120 SetRawExpectations(socket_data.Pass()); | 124 SetRawExpectations(socket_data.Pass()); |
121 } | 125 } |
122 | 126 |
123 void WebSocketDeterministicMockClientSocketFactoryMaker::SetRawExpectations( | 127 void WebSocketDeterministicMockClientSocketFactoryMaker::SetRawExpectations( |
124 scoped_ptr<DeterministicSocketData> socket_data) { | 128 scoped_ptr<DeterministicSocketData> socket_data) { |
125 detail_->data = socket_data.Pass(); | 129 detail_->data = socket_data.Pass(); |
126 detail_->factory.AddSocketDataProvider(detail_->data.get()); | 130 detail_->factory.AddSocketDataProvider(detail_->data.get()); |
127 } | 131 } |
128 | 132 |
129 WebSocketTestURLRequestContextHost::WebSocketTestURLRequestContextHost() | 133 WebSocketTestURLRequestContextHost::WebSocketTestURLRequestContextHost() |
130 : url_request_context_(true) { | 134 : url_request_context_(true) { |
131 url_request_context_.set_client_socket_factory(maker_.factory()); | 135 url_request_context_.set_client_socket_factory(maker_.factory()); |
132 } | 136 } |
133 | 137 |
134 WebSocketTestURLRequestContextHost::~WebSocketTestURLRequestContextHost() {} | 138 WebSocketTestURLRequestContextHost::~WebSocketTestURLRequestContextHost() { |
| 139 } |
135 | 140 |
136 void WebSocketTestURLRequestContextHost::SetRawExpectations( | 141 void WebSocketTestURLRequestContextHost::SetRawExpectations( |
137 scoped_ptr<DeterministicSocketData> socket_data) { | 142 scoped_ptr<DeterministicSocketData> socket_data) { |
138 maker_.SetRawExpectations(socket_data.Pass()); | 143 maker_.SetRawExpectations(socket_data.Pass()); |
139 } | 144 } |
140 | 145 |
141 TestURLRequestContext* | 146 TestURLRequestContext* |
142 WebSocketTestURLRequestContextHost::GetURLRequestContext() { | 147 WebSocketTestURLRequestContextHost::GetURLRequestContext() { |
143 url_request_context_.Init(); | 148 url_request_context_.Init(); |
144 // A Network Delegate is required to make the URLRequest::Delegate work. | 149 // A Network Delegate is required to make the URLRequest::Delegate work. |
145 url_request_context_.set_network_delegate(&network_delegate_); | 150 url_request_context_.set_network_delegate(&network_delegate_); |
146 return &url_request_context_; | 151 return &url_request_context_; |
147 } | 152 } |
148 | 153 |
149 } // namespace net | 154 } // namespace net |
OLD | NEW |