Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / net / websockets / websocket_test_util.h
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 #ifndef NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_
6 #define NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_
7
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "net/url_request/url_request_test_util.h"
13 #include "net/websockets/websocket_stream.h"
14
15 class GURL;
16
17 namespace base {
18 class Timer;
19 }  // namespace base
20
21 namespace url {
22 class Origin;
23 }  // namespace url
24
25 namespace net {
26
27 class BoundNetLog;
28 class DeterministicMockClientSocketFactory;
29 class DeterministicSocketData;
30 class URLRequestContext;
31 class WebSocketHandshakeStreamCreateHelper;
32 struct SSLSocketDataProvider;
33
34 class LinearCongruentialGenerator {
35  public:
36   explicit LinearCongruentialGenerator(uint32 seed);
37   uint32 Generate();
38
39  private:
40   uint64 current_;
41 };
42
43 // Alternate version of WebSocketStream::CreateAndConnectStream() for testing
44 // use only. The differences are the use of a |create_helper| argument in place
45 // of |requested_subprotocols| and taking |timer| as the handshake timeout
46 // timer. Implemented in websocket_stream.cc.
47 NET_EXPORT_PRIVATE extern scoped_ptr<WebSocketStreamRequest>
48     CreateAndConnectStreamForTesting(
49         const GURL& socket_url,
50         scoped_ptr<WebSocketHandshakeStreamCreateHelper> create_helper,
51         const url::Origin& origin,
52         URLRequestContext* url_request_context,
53         const BoundNetLog& net_log,
54         scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate,
55         scoped_ptr<base::Timer> timer);
56
57 // Generates a standard WebSocket handshake request. The challenge key used is
58 // "dGhlIHNhbXBsZSBub25jZQ==". Each header in |extra_headers| must be terminated
59 // with "\r\n".
60 extern std::string WebSocketStandardRequest(const std::string& path,
61                                             const std::string& origin,
62                                             const std::string& extra_headers);
63
64 // A response with the appropriate accept header to match the above challenge
65 // key. Each header in |extra_headers| must be terminated with "\r\n".
66 extern std::string WebSocketStandardResponse(const std::string& extra_headers);
67
68 // This class provides a convenient way to construct a
69 // DeterministicMockClientSocketFactory for WebSocket tests.
70 class WebSocketDeterministicMockClientSocketFactoryMaker {
71  public:
72   WebSocketDeterministicMockClientSocketFactoryMaker();
73   ~WebSocketDeterministicMockClientSocketFactoryMaker();
74
75   // Tell the factory to create a socket which expects |expect_written| to be
76   // written, and responds with |return_to_read|. The test will fail if the
77   // expected text is not written, or all the bytes are not read. This adds data
78   // for a new mock-socket using AddRawExpections(), and so can be called
79   // multiple times to queue up multiple mock sockets, but usually in those
80   // cases the lower-level AddRawExpections() interface is more appropriate.
81   void SetExpectations(const std::string& expect_written,
82                        const std::string& return_to_read);
83
84   // A low-level interface to permit arbitrary expectations to be added. The
85   // mock sockets will be created in the same order that they were added.
86   void AddRawExpectations(scoped_ptr<DeterministicSocketData> socket_data);
87
88   // Allow an SSL socket data provider to be added. You must also supply a mock
89   // transport socket for it to use. If the mock SSL handshake fails then the
90   // mock transport socket will connect but have nothing read or written. If the
91   // mock handshake succeeds then the data from the underlying transport socket
92   // will be passed through unchanged (without encryption).
93   void AddSSLSocketDataProvider(
94       scoped_ptr<SSLSocketDataProvider> ssl_socket_data);
95
96   // Call to get a pointer to the factory, which remains owned by this object.
97   DeterministicMockClientSocketFactory* factory();
98
99  private:
100   struct Detail;
101   scoped_ptr<Detail> detail_;
102
103   DISALLOW_COPY_AND_ASSIGN(WebSocketDeterministicMockClientSocketFactoryMaker);
104 };
105
106 // This class encapsulates the details of creating a
107 // TestURLRequestContext that returns mock ClientSocketHandles that do what is
108 // required by the tests.
109 struct WebSocketTestURLRequestContextHost {
110  public:
111   WebSocketTestURLRequestContextHost();
112   ~WebSocketTestURLRequestContextHost();
113
114   void SetExpectations(const std::string& expect_written,
115                        const std::string& return_to_read) {
116     maker_.SetExpectations(expect_written, return_to_read);
117   }
118
119   void AddRawExpectations(scoped_ptr<DeterministicSocketData> socket_data);
120
121   // Allow an SSL socket data provider to be added.
122   void AddSSLSocketDataProvider(
123       scoped_ptr<SSLSocketDataProvider> ssl_socket_data);
124
125   // Call after calling one of SetExpections() or AddRawExpectations(). The
126   // returned pointer remains owned by this object.
127   TestURLRequestContext* GetURLRequestContext();
128
129  private:
130   WebSocketDeterministicMockClientSocketFactoryMaker maker_;
131   TestURLRequestContext url_request_context_;
132   TestNetworkDelegate network_delegate_;
133   bool url_request_context_initialized_;
134
135   DISALLOW_COPY_AND_ASSIGN(WebSocketTestURLRequestContextHost);
136 };
137
138 }  // namespace net
139
140 #endif  // NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_