summaryrefslogtreecommitdiffstats
path: root/net/spdy/spdy_session_unittest.cc
blob: 71c42bf50c64f57d9bb6fe4d096b2218627bd5c3 (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
// 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/spdy/spdy_io_buffer.h"

#include "googleurl/src/gurl.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/http/http_network_session.h"
#include "net/http/http_response_info.h"
#include "net/proxy/proxy_service.h"
#include "net/socket/socket_test_util.h"
#include "net/spdy/spdy_session.h"
#include "net/spdy/spdy_session_pool.h"
#include "net/spdy/spdy_stream.h"
#include "net/spdy/spdy_test_util.h"
#include "testing/platform_test.h"

namespace net {

// TODO(cbentzel): Expose compression setter/getter in public SpdySession
//                 interface rather than going through all these contortions.
class SpdySessionTest : public PlatformTest {
 public:
  static void TurnOffCompression() {
    spdy::SpdyFramer::set_enable_compression_default(false);
  }
};

namespace {

// Helper to manage the lifetimes of the dependencies for a
// SpdyNetworkTransaction.
class SessionDependencies {
 public:
  // Default set of dependencies -- "null" proxy service.
  SessionDependencies()
      : host_resolver(new MockHostResolver),
        proxy_service(ProxyService::CreateNull()),
        ssl_config_service(new SSLConfigServiceDefaults),
        spdy_session_pool(new SpdySessionPool) {
  }

  scoped_refptr<MockHostResolverBase> host_resolver;
  scoped_refptr<ProxyService> proxy_service;
  scoped_refptr<SSLConfigService> ssl_config_service;
  MockClientSocketFactory socket_factory;
  scoped_refptr<SpdySessionPool> spdy_session_pool;
};

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

// Test the SpdyIOBuffer class.
TEST_F(SpdySessionTest, SpdyIOBuffer) {
  std::priority_queue<SpdyIOBuffer> queue_;
  const size_t kQueueSize = 100;

  // Insert 100 items; pri 100 to 1.
  for (size_t index = 0; index < kQueueSize; ++index) {
    SpdyIOBuffer buffer(new IOBuffer(), 0, kQueueSize - index, NULL);
    queue_.push(buffer);
  }

  // Insert several priority 0 items last.
  const size_t kNumDuplicates = 12;
  IOBufferWithSize* buffers[kNumDuplicates];
  for (size_t index = 0; index < kNumDuplicates; ++index) {
    buffers[index] = new IOBufferWithSize(index+1);
    queue_.push(SpdyIOBuffer(buffers[index], buffers[index]->size(), 0, NULL));
  }

  EXPECT_EQ(kQueueSize + kNumDuplicates, queue_.size());

  // Verify the P0 items come out in FIFO order.
  for (size_t index = 0; index < kNumDuplicates; ++index) {
    SpdyIOBuffer buffer = queue_.top();
    EXPECT_EQ(0, buffer.priority());
    EXPECT_EQ(index + 1, buffer.size());
    queue_.pop();
  }

  int priority = 1;
  while (queue_.size()) {
    SpdyIOBuffer buffer = queue_.top();
    EXPECT_EQ(priority++, buffer.priority());
    queue_.pop();
  }
}

static const unsigned char kGoAway[] = {
  0x80, 0x01, 0x00, 0x07,  // header
  0x00, 0x00, 0x00, 0x04,  // flags, len
  0x00, 0x00, 0x00, 0x00,  // last-accepted-stream-id
};

TEST_F(SpdySessionTest, GoAway) {
  SessionDependencies session_deps;
  session_deps.host_resolver->set_synchronous_mode(true);

  MockConnect connect_data(false, OK);
  MockRead reads[] = {
    MockRead(false, reinterpret_cast<const char*>(kGoAway),
             arraysize(kGoAway)),
    MockRead(false, 0, 0)  // EOF
  };
  StaticSocketDataProvider data(reads, arraysize(reads), NULL, 0);
  data.set_connect_data(connect_data);
  session_deps.socket_factory.AddSocketDataProvider(&data);

  SSLSocketDataProvider ssl(false, OK);
  session_deps.socket_factory.AddSSLSocketDataProvider(&ssl);

  scoped_refptr<HttpNetworkSession> http_session(CreateSession(&session_deps));

  const std::string kTestHost("www.foo.com");
  const int kTestPort = 80;
  HostPortPair test_host_port_pair;
  test_host_port_pair.host = kTestHost;
  test_host_port_pair.port = kTestPort;

  scoped_refptr<SpdySessionPool> spdy_session_pool(
      http_session->spdy_session_pool());
  EXPECT_FALSE(spdy_session_pool->HasSession(test_host_port_pair));
  scoped_refptr<SpdySession> session =
      spdy_session_pool->Get(
          test_host_port_pair, http_session.get(), BoundNetLog());
  EXPECT_TRUE(spdy_session_pool->HasSession(test_host_port_pair));

  TCPSocketParams tcp_params(kTestHost, kTestPort, MEDIUM, GURL(), false);
  int rv = session->Connect(kTestHost, tcp_params, MEDIUM);
  ASSERT_EQ(OK, rv);

  // Flush the SpdySession::OnReadComplete() task.
  MessageLoop::current()->RunAllPending();

  EXPECT_FALSE(spdy_session_pool->HasSession(test_host_port_pair));

  scoped_refptr<SpdySession> session2 =
      spdy_session_pool->Get(
          test_host_port_pair, http_session.get(), BoundNetLog());

  // Delete the first session.
  session = NULL;

  // Delete the second session.
  spdy_session_pool->Remove(session2);
  session2 = NULL;
}

// StreamCanceler is used for SpdySessionTest.CancelStreamOnClose.  It is used
// for two callbacks: the stream's SendRequest() and ReadResponseBody()
// callbacks.
class StreamCanceler {
 public:
  enum State {
    WAITING_FOR_CONNECT,
    WAITING_FOR_RESPONSE,
    STATE_DONE
  };

  explicit StreamCanceler(const scoped_refptr<SpdyStream>& stream)
      : stream_(stream),
        ALLOW_THIS_IN_INITIALIZER_LIST(
            callback_(this, &StreamCanceler::OnIOComplete)),
        buf_(new IOBufferWithSize(64)),
        state_(WAITING_FOR_CONNECT) {}

  CompletionCallback* callback() { return &callback_; }

  State state() const { return state_; }

 private:
  void OnIOComplete(int result) {
    MessageLoop::current()->Quit();
    switch (state_) {
      case WAITING_FOR_CONNECT:
        // After receiving this callback, start the ReadResponseBody() request.
        // We need to do this here rather than elsewhere, since the MessageLoop
        // will keep processing the pending read tasks until there aren't any
        // more, so we won't get a chance to get an asynchronous callback for
        // ReadResponseBody() unless we call it here in the callback for
        // SendRequest().
        EXPECT_EQ(OK, result);
        state_ = WAITING_FOR_RESPONSE;
        EXPECT_EQ(ERR_IO_PENDING, stream_->ReadResponseBody(
            buf_.get(), buf_->size(), &callback_));
        break;
      case WAITING_FOR_RESPONSE:
        // The result should be the 6 bytes of the body.  The next read will
        // succeed synchronously, indicating the stream is closed.  We cancel
        // the stream during the callback for the first ReadResponseBody() call
        // which will deactivate the stream.  The code should handle this case.
        EXPECT_EQ(6, result);
        EXPECT_EQ(OK,
                  stream_->ReadResponseBody(
                      buf_.get(), buf_->size(), &callback_));
        stream_->Cancel();
        state_ = STATE_DONE;
        break;
      default:
        NOTREACHED();
        break;
    }
  }

  const scoped_refptr<SpdyStream> stream_;
  CompletionCallbackImpl<StreamCanceler> callback_;
  scoped_refptr<IOBufferWithSize> buf_;
  State state_;

  DISALLOW_COPY_AND_ASSIGN(StreamCanceler);
};

TEST_F(SpdySessionTest, CancelStreamOnClose) {
  SpdySessionTest::TurnOffCompression();
  SessionDependencies session_deps;
  session_deps.host_resolver->set_synchronous_mode(true);

  MockConnect connect_data(false, OK);
  MockRead reads[] = {
    MockRead(true, reinterpret_cast<const char*>(kGetSynReply),
             arraysize(kGetSynReply)),
    MockRead(true, reinterpret_cast<const char*>(kGetBodyFrame),
             arraysize(kGetBodyFrame)),
    MockRead(true, 0, 0)  // EOF
  };
  StaticSocketDataProvider data(reads, arraysize(reads), NULL, 0);
  data.set_connect_data(connect_data);
  session_deps.socket_factory.AddSocketDataProvider(&data);

  SSLSocketDataProvider ssl(false, OK);
  session_deps.socket_factory.AddSSLSocketDataProvider(&ssl);

  scoped_refptr<HttpNetworkSession> http_session(CreateSession(&session_deps));

  const std::string kTestHost("www.google.com");
  const int kTestPort = 80;
  HostPortPair test_host_port_pair;
  test_host_port_pair.host = kTestHost;
  test_host_port_pair.port = kTestPort;

  scoped_refptr<SpdySessionPool> spdy_session_pool(
      http_session->spdy_session_pool());
  EXPECT_FALSE(spdy_session_pool->HasSession(test_host_port_pair));
  scoped_refptr<SpdySession> session =
      spdy_session_pool->Get(
          test_host_port_pair, http_session.get(), BoundNetLog());

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

  scoped_refptr<SpdyStream> stream =
      session->GetOrCreateStream(request, NULL, BoundNetLog());
  TCPSocketParams tcp_params(kTestHost, kTestPort, MEDIUM, GURL(), false);
  int rv = session->Connect(kTestHost, tcp_params, MEDIUM);
  ASSERT_EQ(OK, rv);

  HttpResponseInfo response;
  TestCompletionCallback callback;
  StreamCanceler canceler(stream);
  rv = stream->SendRequest(NULL, &response, canceler.callback());
  ASSERT_EQ(ERR_IO_PENDING, rv);
  while (canceler.state() != StreamCanceler::STATE_DONE)
    MessageLoop::current()->Run();
}

// kPush is a server-issued SYN_STREAM with stream id 2, and
// associated stream id 1. It also includes 3 headers of path,
// status, and HTTP version.
static const uint8 kPush[] = {
  0x80, 0x01, 0x00, 0x01,  // SYN_STREAM for SPDY v1.
  0x00, 0x00, 0x00, 0x3b,  // No flags 59 bytes after this 8 byte header.
  0x00, 0x00, 0x00, 0x02,  // Stream ID of 2
  0x00, 0x00, 0x00, 0x01,  // Associate Stream ID of 1
  0x00, 0x00, 0x00, 0x03,  // Priority 0, 3 name/value pairs in block below.
  0x00, 0x04, 'p', 'a', 't', 'h',
  0x00, 0x07, '/', 'f', 'o', 'o', '.', 'j', 's',
  0x00, 0x06, 's', 't', 'a', 't', 'u', 's',
  0x00, 0x03, '2', '0', '0',
  0x00, 0x07, 'v', 'e', 'r', 's', 'i', 'o', 'n',
  0x00, 0x08, 'H', 'T', 'T', 'P', '/', '1', '.', '1',
};

}  // namespace

TEST_F(SpdySessionTest, GetPushStream) {
  SpdySessionTest::TurnOffCompression();

  SessionDependencies session_deps;
  session_deps.host_resolver->set_synchronous_mode(true);

  MockConnect connect_data(false, OK);
  MockRead reads[] = {
    MockRead(false, reinterpret_cast<const char*>(kPush),
             arraysize(kPush)),
    MockRead(true, ERR_IO_PENDING, 0)  // EOF
  };
  StaticSocketDataProvider data(reads, arraysize(reads), NULL, 0);
  data.set_connect_data(connect_data);
  session_deps.socket_factory.AddSocketDataProvider(&data);

  SSLSocketDataProvider ssl(false, OK);
  session_deps.socket_factory.AddSSLSocketDataProvider(&ssl);

  scoped_refptr<HttpNetworkSession> http_session(CreateSession(&session_deps));

  const std::string kTestHost("www.foo.com");
  const int kTestPort = 80;
  HostPortPair test_host_port_pair;
  test_host_port_pair.host = kTestHost;
  test_host_port_pair.port = kTestPort;

  scoped_refptr<SpdySessionPool> spdy_session_pool(
      http_session->spdy_session_pool());
  EXPECT_FALSE(spdy_session_pool->HasSession(test_host_port_pair));
  scoped_refptr<SpdySession> session =
      spdy_session_pool->Get(
          test_host_port_pair, http_session.get(), BoundNetLog());
  EXPECT_TRUE(spdy_session_pool->HasSession(test_host_port_pair));

  // No push streams should exist in the beginning.
  std::string test_push_path = "/foo.js";
  scoped_refptr<SpdyStream> first_stream = session->GetPushStream(
      test_push_path);
  EXPECT_EQ(static_cast<SpdyStream*>(NULL), first_stream.get());

  // Read in the data which contains a server-issued SYN_STREAM.
  TCPSocketParams tcp_params(test_host_port_pair, MEDIUM, GURL(), false);
  int rv = session->Connect(kTestHost, tcp_params, MEDIUM);
  ASSERT_EQ(OK, rv);
  MessageLoop::current()->RunAllPending();

  // An unpushed path should not work.
  scoped_refptr<SpdyStream> unpushed_stream = session->GetPushStream(
      "/unpushed_path");
  EXPECT_EQ(static_cast<SpdyStream*>(NULL), unpushed_stream.get());

  // The pushed path should be found.
  scoped_refptr<SpdyStream> second_stream = session->GetPushStream(
      test_push_path);
  ASSERT_NE(static_cast<SpdyStream*>(NULL), second_stream.get());
  EXPECT_EQ(test_push_path, second_stream->path());
  EXPECT_EQ(2U, second_stream->stream_id());
  EXPECT_EQ(0, second_stream->priority());

  // Clean up
  second_stream = NULL;
  session = NULL;
  spdy_session_pool->CloseAllSessions();

  // RunAllPending needs to be called here because the
  // ClientSocketPoolBase posts a task to clean up and destroy the
  // underlying socket.
  MessageLoop::current()->RunAllPending();
}

}  // namespace net