Home | History | Annotate | Download | only in websockets
      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
      3 // found in the LICENSE file.
      4 
      5 #include "net/websockets/websocket_test_util.h"
      6 
      7 #include "base/basictypes.h"
      8 #include "base/strings/stringprintf.h"
      9 #include "net/socket/socket_test_util.h"
     10 
     11 namespace net {
     12 
     13 namespace {
     14 const uint64 kA =
     15     (static_cast<uint64>(0x5851f42d) << 32) + static_cast<uint64>(0x4c957f2d);
     16 const uint64 kC = 12345;
     17 const uint64 kM = static_cast<uint64>(1) << 48;
     18 
     19 }  // namespace
     20 
     21 LinearCongruentialGenerator::LinearCongruentialGenerator(uint32 seed)
     22     : current_(seed) {}
     23 
     24 uint32 LinearCongruentialGenerator::Generate() {
     25   uint64 result = current_;
     26   current_ = (current_ * kA + kC) % kM;
     27   return static_cast<uint32>(result >> 16);
     28 }
     29 
     30 std::string WebSocketStandardRequest(const std::string& path,
     31                                      const std::string& origin,
     32                                      const std::string& extra_headers) {
     33   // Unrelated changes in net/http may change the order and default-values of
     34   // HTTP headers, causing WebSocket tests to fail. It is safe to update this
     35   // string in that case.
     36   return base::StringPrintf(
     37       "GET %s HTTP/1.1\r\n"
     38       "Host: localhost\r\n"
     39       "Connection: Upgrade\r\n"
     40       "Upgrade: websocket\r\n"
     41       "Origin: %s\r\n"
     42       "Sec-WebSocket-Version: 13\r\n"
     43       "User-Agent:\r\n"
     44       "Accept-Encoding: gzip,deflate\r\n"
     45       "Accept-Language: en-us,fr\r\n"
     46       "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
     47       "%s\r\n",
     48       path.c_str(),
     49       origin.c_str(),
     50       extra_headers.c_str());
     51 }
     52 
     53 std::string WebSocketStandardResponse(const std::string& extra_headers) {
     54   return base::StringPrintf(
     55       "HTTP/1.1 101 Switching Protocols\r\n"
     56       "Upgrade: websocket\r\n"
     57       "Connection: Upgrade\r\n"
     58       "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"
     59       "%s\r\n",
     60       extra_headers.c_str());
     61 }
     62 
     63 struct WebSocketDeterministicMockClientSocketFactoryMaker::Detail {
     64   std::string expect_written;
     65   std::string return_to_read;
     66   MockRead read;
     67   MockWrite write;
     68   scoped_ptr<DeterministicSocketData> data;
     69   DeterministicMockClientSocketFactory factory;
     70 };
     71 
     72 WebSocketDeterministicMockClientSocketFactoryMaker::
     73     WebSocketDeterministicMockClientSocketFactoryMaker()
     74     : detail_(new Detail) {}
     75 
     76 WebSocketDeterministicMockClientSocketFactoryMaker::
     77     ~WebSocketDeterministicMockClientSocketFactoryMaker() {}
     78 
     79 DeterministicMockClientSocketFactory*
     80 WebSocketDeterministicMockClientSocketFactoryMaker::factory() {
     81   return &detail_->factory;
     82 }
     83 
     84 void WebSocketDeterministicMockClientSocketFactoryMaker::SetExpectations(
     85     const std::string& expect_written,
     86     const std::string& return_to_read) {
     87   // We need to extend the lifetime of these strings.
     88   detail_->expect_written = expect_written;
     89   detail_->return_to_read = return_to_read;
     90   detail_->write = MockWrite(SYNCHRONOUS, 0, detail_->expect_written.c_str());
     91   detail_->read = MockRead(SYNCHRONOUS, 1, detail_->return_to_read.c_str());
     92   scoped_ptr<DeterministicSocketData> socket_data(
     93       new DeterministicSocketData(&detail_->read, 1, &detail_->write, 1));
     94   socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK));
     95   socket_data->SetStop(2);
     96   SetRawExpectations(socket_data.Pass());
     97 }
     98 
     99 void WebSocketDeterministicMockClientSocketFactoryMaker::SetRawExpectations(
    100     scoped_ptr<DeterministicSocketData> socket_data) {
    101   detail_->data = socket_data.Pass();
    102   detail_->factory.AddSocketDataProvider(detail_->data.get());
    103 }
    104 
    105 WebSocketTestURLRequestContextHost::WebSocketTestURLRequestContextHost()
    106     : url_request_context_(true) {
    107   url_request_context_.set_client_socket_factory(maker_.factory());
    108 }
    109 
    110 WebSocketTestURLRequestContextHost::~WebSocketTestURLRequestContextHost() {}
    111 
    112 void WebSocketTestURLRequestContextHost::SetRawExpectations(
    113     scoped_ptr<DeterministicSocketData> socket_data) {
    114   maker_.SetRawExpectations(socket_data.Pass());
    115 }
    116 
    117 TestURLRequestContext*
    118 WebSocketTestURLRequestContextHost::GetURLRequestContext() {
    119   url_request_context_.Init();
    120   // A Network Delegate is required to make the URLRequest::Delegate work.
    121   url_request_context_.set_network_delegate(&network_delegate_);
    122   return &url_request_context_;
    123 }
    124 
    125 }  // namespace net
    126