summaryrefslogtreecommitdiffstats
path: root/net/flip/flip_network_transaction_unittest.cc
blob: 5e344a123b5853ea8e3a659f828904039a1eb8bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/flip/flip_network_transaction.h"

#include "net/base/completion_callback.h"
#include "net/base/mock_host_resolver.h"
#include "net/base/ssl_config_service_defaults.h"
#include "net/base/test_completion_callback.h"
#include "net/base/upload_data.h"
#include "net/flip/flip_protocol.h"
#include "net/http/http_network_session.h"
#include "net/http/http_transaction_unittest.h"
#include "net/proxy/proxy_config_service_fixed.h"
#include "net/socket/socket_test_util.h"
#include "testing/platform_test.h"

//-----------------------------------------------------------------------------

namespace net {

class FlipStreamParserPeer {
 public:
  explicit FlipStreamParserPeer(FlipStreamParser* flip_stream_parser)
      : flip_stream_parser_(flip_stream_parser) {}

  int flip_stream_id() const { return flip_stream_parser_->flip_stream_id_; }

 private:
  FlipStreamParser* const flip_stream_parser_;

  DISALLOW_COPY_AND_ASSIGN(FlipStreamParserPeer);
};

namespace {

// Create a proxy service which fails on all requests (falls back to direct).
ProxyService* CreateNullProxyService() {
  return ProxyService::CreateNull();
}

// Helper to manage the lifetimes of the dependencies for a
// FlipNetworkTransaction.
class SessionDependencies {
 public:
  // Default set of dependencies -- "null" proxy service.
  SessionDependencies()
      : host_resolver(new MockHostResolver),
        proxy_service(CreateNullProxyService()),
        ssl_config_service(new SSLConfigServiceDefaults),
        flip_session_pool(new FlipSessionPool) {}

  // Custom proxy service dependency.
  explicit SessionDependencies(ProxyService* proxy_service)
      : host_resolver(new MockHostResolver),
        proxy_service(proxy_service),
        ssl_config_service(new SSLConfigServiceDefaults),
        flip_session_pool(new FlipSessionPool) {}

  scoped_refptr<MockHostResolverBase> host_resolver;
  scoped_refptr<ProxyService> proxy_service;
  scoped_refptr<SSLConfigService> ssl_config_service;
  MockClientSocketFactory socket_factory;
  scoped_refptr<FlipSessionPool> flip_session_pool;
};

ProxyService* CreateFixedProxyService(const std::string& proxy) {
  ProxyConfig proxy_config;
  proxy_config.proxy_rules.ParseFromString(proxy);
  return ProxyService::CreateFixed(proxy_config);
}


HttpNetworkSession* CreateSession(SessionDependencies* session_deps) {
  return new HttpNetworkSession(session_deps->host_resolver,
                                session_deps->proxy_service,
                                &session_deps->socket_factory,
                                session_deps->ssl_config_service,
                                session_deps->flip_session_pool);
}

class FlipStreamParserTest : public PlatformTest {
 protected:
  FlipStreamParserTest()
      : session_(CreateSession(&session_deps_)),
        parser_peer_(&parser_) {}

  FlipSession* CreateFlipSession() {
    HostResolver::RequestInfo resolve_info("www.google.com", 80);
    FlipSession* session = session_->flip_session_pool()->Get(
        resolve_info, session_);
    return session;
  }

  virtual void TearDown() {
    MessageLoop::current()->RunAllPending();
    PlatformTest::TearDown();
  }

  SessionDependencies session_deps_;
  scoped_refptr<HttpNetworkSession> session_;
  FlipStreamParser parser_;
  FlipStreamParserPeer parser_peer_;
};

// TODO(willchan): Look into why TCPConnectJobs are still alive when this test
// goes away.  They're calling into the ClientSocketFactory which doesn't exist
// anymore, so it crashes.
TEST_F(FlipStreamParserTest, DISABLED_SendRequest) {
  scoped_refptr<FlipSession> flip(CreateFlipSession());
  HttpRequestInfo request;
  request.method = "GET";
  request.url = GURL("http://www.google.com/");
  TestCompletionCallback callback;

  EXPECT_EQ(ERR_IO_PENDING, parser_.SendRequest(flip, &request, &callback));
  EXPECT_TRUE(flip->IsStreamActive(parser_peer_.flip_stream_id()));
}

// TODO(willchan): Write a longer test for FlipStreamParser that exercises all
// methods.

}  // namespace

class FlipNetworkTransactionTest : public PlatformTest {
 protected:
  virtual void SetUp() {
    // Disable compression on this test.
    flip::FlipFramer::set_enable_compression_default(false);
  }

  virtual void TearDown() {
    // Empty the current queue.
    MessageLoop::current()->RunAllPending();
    PlatformTest::TearDown();
  }

  void KeepAliveConnectionResendRequestTest(const MockRead& read_failure);

  struct TransactionHelperResult {
    int rv;
    std::string status_line;
    std::string response_data;
  };

  TransactionHelperResult TransactionHelper(const HttpRequestInfo& request,
                                            MockRead reads[],
                                            MockWrite writes[]) {
    TransactionHelperResult out;

    // We disable SSL for this test.
    FlipSession::SetSSLMode(false);

    SessionDependencies session_deps;
    scoped_ptr<FlipNetworkTransaction> trans(
        new FlipNetworkTransaction(CreateSession(&session_deps)));

    StaticSocketDataProvider data(reads, writes);
    session_deps.socket_factory.AddSocketDataProvider(&data);

    TestCompletionCallback callback;

    int rv = trans->Start(&request, &callback, NULL);
    EXPECT_EQ(ERR_IO_PENDING, rv);

    out.rv = callback.WaitForResult();
    if (out.rv != OK)
      return out;

    const HttpResponseInfo* response = trans->GetResponseInfo();
    EXPECT_TRUE(response->headers != NULL);
    out.status_line = response->headers->GetStatusLine();

    rv = ReadTransaction(trans.get(), &out.response_data);
    EXPECT_EQ(OK, rv);

    return out;
  }

  void ConnectStatusHelperWithExpectedStatus(const MockRead& status,
                                             int expected_status);

  void ConnectStatusHelper(const MockRead& status);
};

//-----------------------------------------------------------------------------

// Verify FlipNetworkTransaction constructor.
TEST_F(FlipNetworkTransactionTest, Constructor) {
  SessionDependencies session_deps;
  scoped_refptr<HttpNetworkSession> session =
      CreateSession(&session_deps);
  scoped_ptr<HttpTransaction> trans(new FlipNetworkTransaction(session));
}

TEST_F(FlipNetworkTransactionTest, Get) {
  static const unsigned char syn[] = {
    0x80, 0x01, 0x00, 0x01,                                        // header
    0x01, 0x00, 0x00, 0x45,                                        // FIN, len
    0x00, 0x00, 0x00, 0x01,                                        // stream id
    0xc0, 0x00, 0x00, 0x03,                                        // 4 headers
    0x00, 0x06, 'm', 'e', 't', 'h', 'o', 'd',
    0x00, 0x03, 'G', 'E', 'T',
    0x00, 0x03, 'u', 'r', 'l',
    0x00, 0x16, 'h', 't', 't', 'p', ':', '/', '/', 'w', 'w', 'w',
                '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o',
                'm', '/',
    0x00, 0x07, 'v', 'e', 'r', 's', 'i', 'o', 'n',
    0x00, 0x08, 'H', 'T', 'T', 'P', '/', '1', '.', '1',
  };

  static const unsigned char syn_reply[] = {
    0x80, 0x01, 0x00, 0x02,                                        // header
    0x00, 0x00, 0x00, 0x45,
    0x00, 0x00, 0x00, 0x01,
    0x00, 0x00, 0x00, 0x04,                                        // 4 headers
    0x00, 0x05, 'h', 'e', 'l', 'l', 'o',                           // "hello"
    0x00, 0x03, 'b', 'y', 'e',                                     // "bye"
    0x00, 0x06, 's', 't', 'a', 't', 'u', 's',                      // "status"
    0x00, 0x03, '2', '0', '0',                                     // "200"
    0x00, 0x03, 'u', 'r', 'l',                                     // "url"
    0x00, 0x0a, '/', 'i', 'n', 'd', 'e', 'x', '.', 'p', 'h', 'p',  // "HTTP/1.1"
    0x00, 0x07, 'v', 'e', 'r', 's', 'i', 'o', 'n',                 // "version"
    0x00, 0x08, 'H', 'T', 'T', 'P', '/', '1', '.', '1',            // "HTTP/1.1"
  };
  static const unsigned char body_frame[] = {
    0x00, 0x00, 0x00, 0x01,                                        // header
    0x00, 0x00, 0x00, 0x06,
    'h', 'e', 'l', 'l', 'o', '!',                                  // "hello"
  };
  static const unsigned char fin_frame[] = {
    0x80, 0x01, 0x00, 0x03,                                        // header
    0x00, 0x00, 0x00, 0x08,
    0x00, 0x00, 0x00, 0x01,
    0x00, 0x00, 0x00, 0x00,
  };

  MockWrite writes[] = {
    MockWrite(true, reinterpret_cast<const char*>(syn), sizeof(syn)),
  };

  MockRead reads[] = {
    MockRead(true, reinterpret_cast<const char*>(syn_reply), sizeof(syn_reply)),
    MockRead(true, reinterpret_cast<const char*>(body_frame),
             sizeof(body_frame)),
    MockRead(true, reinterpret_cast<const char*>(fin_frame),
             sizeof(fin_frame)),
    MockRead(true, 0, 0)  // EOF
  };

  HttpRequestInfo request;
  request.method = "GET";
  request.url = GURL("http://www.google.com/");
  request.load_flags = 0;
  TransactionHelperResult out = TransactionHelper(request, reads, writes);
  EXPECT_EQ(OK, out.rv);
  EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
  EXPECT_EQ("hello!", out.response_data);
}

// Test that a simple POST works.
TEST_F(FlipNetworkTransactionTest, Post) {
  static const char upload[] = { "hello world" };

  // Setup the request
  HttpRequestInfo request;
  request.method = "POST";
  request.url = GURL("http://www.google.com/");
  request.upload_data = new UploadData();
  request.upload_data->AppendBytes(upload, sizeof(upload));

  // TODO(mbelshe): Hook up the write validation.

  //static const unsigned char syn[] = {
  //  0x80, 0x01, 0x00, 0x01,                                      // header
  //  0x01, 0x00, 0x00, 0x46,                                      // FIN, len
  //  0x00, 0x00, 0x00, 0x01,                                      // stream id
  //  0xc0, 0x00, 0x00, 0x04,                                      // 4 headers
  //  0x00, 0x06, 'm', 'e', 't', 'h', 'o', 'd',
  //  0x00, 0x03, 'G', 'E', 'T',
  //  0x00, 0x03, 'u', 'r', 'l',
  //  0x00, 0x16, 'h', 't', 't', 'p', ':', '/', '/', 'w', 'w', 'w',
  //              '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o',
  //              'm', '/',
  //  0x00, 0x07, 'v', 'e', 'r', 's', 'i', 'o', 'n',
  //  0x00, 0x08, 'H', 'T', 'T', 'P', '/', '1', '.', '1',
  //};

  //static const unsigned char upload_frame[] = {
  //  0x00, 0x00, 0x00, 0x01,                                        // header
  //  0x01, 0x00, 0x00, 0x06,                                        // FIN flag
  //  'h', 'e', 'l', 'l', 'o', '!',                                  // "hello"
  //};

  // The response
  static const unsigned char syn_reply[] = {
    0x80, 0x01, 0x00, 0x02,                                        // header
    0x00, 0x00, 0x00, 0x45,
    0x00, 0x00, 0x00, 0x01,
    0x00, 0x00, 0x00, 0x04,                                        // 4 headers
    0x00, 0x05, 'h', 'e', 'l', 'l', 'o',                           // "hello"
    0x00, 0x03, 'b', 'y', 'e',                                     // "bye"
    0x00, 0x06, 's', 't', 'a', 't', 'u', 's',                      // "status"
    0x00, 0x03, '2', '0', '0',                                     // "200"
    0x00, 0x03, 'u', 'r', 'l',                                     // "url"
    0x00, 0x0a, '/', 'i', 'n', 'd', 'e', 'x', '.', 'p', 'h', 'p',  // "HTTP/1.1"
    0x00, 0x07, 'v', 'e', 'r', 's', 'i', 'o', 'n',                 // "version"
    0x00, 0x08, 'H', 'T', 'T', 'P', '/', '1', '.', '1',            // "HTTP/1.1"
  };
  static const unsigned char body_frame[] = {
    0x00, 0x00, 0x00, 0x01,                                        // header
    0x00, 0x00, 0x00, 0x06,
    'h', 'e', 'l', 'l', 'o', '!',                                  // "hello"
  };
  static const unsigned char fin_frame[] = {
    0x80, 0x01, 0x00, 0x03,                                        // header
    0x00, 0x00, 0x00, 0x08,
    0x00, 0x00, 0x00, 0x01,
    0x00, 0x00, 0x00, 0x00,
  };

  //MockWrite writes[] = {
  //  MockWrite(true, reinterpret_cast<const char*>(syn), sizeof(syn)),
  //  MockWrite(true, reinterpret_cast<const char*>(upload_frame),
  //            sizeof(upload_frame)),
  //};

  MockRead data_reads[] = {
    MockRead(true, reinterpret_cast<const char*>(syn_reply), sizeof(syn_reply)),
    MockRead(true, reinterpret_cast<const char*>(body_frame),
             sizeof(body_frame)),
    MockRead(true, reinterpret_cast<const char*>(fin_frame), sizeof(fin_frame)),
    MockRead(true, 0, 0)  // EOF
  };

  TransactionHelperResult out = TransactionHelper(request, data_reads, NULL);
  EXPECT_EQ(OK, out.rv);
  EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
  EXPECT_EQ("hello!", out.response_data);
}

// Test that the transaction doesn't crash when we don't have a reply.
TEST_F(FlipNetworkTransactionTest, ResponseWithoutSynReply) {
  static const unsigned char body_frame[] = {
    0x00, 0x00, 0x00, 0x01,                                        // header
    0x00, 0x00, 0x00, 0x06,
    'h', 'e', 'l', 'l', 'o', '!',                                  // "hello"
  };
  static const unsigned char fin_frame[] = {
    0x80, 0x01, 0x00, 0x03,                                        // header
    0x00, 0x00, 0x00, 0x08,
    0x00, 0x00, 0x00, 0x01,
    0x00, 0x00, 0x00, 0x00,
  };

  MockRead data_reads[] = {
    MockRead(true, reinterpret_cast<const char*>(body_frame),
             sizeof(body_frame)),
    MockRead(true, reinterpret_cast<const char*>(fin_frame), sizeof(fin_frame)),
    MockRead(true, 0, 0)  // EOF
  };

  HttpRequestInfo request;
  request.method = "GET";
  request.url = GURL("http://www.google.com/");
  request.load_flags = 0;
  TransactionHelperResult out = TransactionHelper(request, data_reads, NULL);
  EXPECT_EQ(ERR_SYN_REPLY_NOT_RECEIVED, out.rv);
}

// TODO(willchan): Look into why TCPConnectJobs are still alive when this test
// goes away.  They're calling into the ClientSocketFactory which doesn't exist
// anymore, so it crashes.
TEST_F(FlipNetworkTransactionTest, DISABLED_CancelledTransaction) {
  static const unsigned char syn[] = {
    0x80, 0x01, 0x00, 0x01,                                        // header
    0x01, 0x00, 0x00, 0x45,                                        // FIN, len
    0x00, 0x00, 0x00, 0x01,                                        // stream id
    0xc0, 0x00, 0x00, 0x03,                                        // 4 headers
    0x00, 0x06, 'm', 'e', 't', 'h', 'o', 'd',
    0x00, 0x03, 'G', 'E', 'T',
    0x00, 0x03, 'u', 'r', 'l',
    0x00, 0x16, 'h', 't', 't', 'p', ':', '/', '/', 'w', 'w', 'w',
                '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o',
                'm', '/',
    0x00, 0x07, 'v', 'e', 'r', 's', 'i', 'o', 'n',
    0x00, 0x08, 'H', 'T', 'T', 'P', '/', '1', '.', '1',
  };

  static const unsigned char syn_reply[] = {
    0x80, 0x01, 0x00, 0x02,                                        // header
    0x00, 0x00, 0x00, 0x45,
    0x00, 0x00, 0x00, 0x01,
    0x00, 0x00, 0x00, 0x04,                                        // 4 headers
    0x00, 0x05, 'h', 'e', 'l', 'l', 'o',                           // "hello"
    0x00, 0x03, 'b', 'y', 'e',                                     // "bye"
    0x00, 0x06, 's', 't', 'a', 't', 'u', 's',                      // "status"
    0x00, 0x03, '2', '0', '0',                                     // "200"
    0x00, 0x03, 'u', 'r', 'l',                                     // "url"
    0x00, 0x0a, '/', 'i', 'n', 'd', 'e', 'x', '.', 'p', 'h', 'p',  // "HTTP/1.1"
    0x00, 0x07, 'v', 'e', 'r', 's', 'i', 'o', 'n',                 // "version"
    0x00, 0x08, 'H', 'T', 'T', 'P', '/', '1', '.', '1',            // "HTTP/1.1"
  };

  MockWrite writes[] = {
    MockWrite(true, reinterpret_cast<const char*>(syn), sizeof(syn)),
  };

  MockRead reads[] = {
    MockRead(true, reinterpret_cast<const char*>(syn_reply), sizeof(syn_reply)),
    // This following read isn't used by the test, except during the
    // RunAllPending() call at the end since the FlipSession survives the
    // FlipNetworkTransaction and still tries to continue Read()'ing.  Any
    // MockRead will do here.
    MockRead(true, 0, 0)  // EOF
  };

  HttpRequestInfo request;
  request.method = "GET";
  request.url = GURL("http://www.google.com/");
  request.load_flags = 0;

  // We disable SSL for this test.
  FlipSession::SetSSLMode(false);

  SessionDependencies session_deps;
  scoped_ptr<FlipNetworkTransaction> trans(
      new FlipNetworkTransaction(CreateSession(&session_deps)));

  StaticSocketDataProvider data(reads, writes);
  session_deps.socket_factory.AddSocketDataProvider(&data);

  TestCompletionCallback callback;

  int rv = trans->Start(&request, &callback, NULL);
  EXPECT_EQ(ERR_IO_PENDING, rv);
  trans.reset();  // Cancel the transaction.

  // Flush the MessageLoop while the SessionDependencies (in particular, the
  // MockClientSocketFactory) are still alive.
  MessageLoop::current()->RunAllPending();
}

}  // namespace net