summaryrefslogtreecommitdiff
path: root/chromium/net/quic/quic_client_session_cache_unittests.cc
blob: d9b731281ce71c12527624c4eeaa56d2b1ce1c73 (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
// Copyright 2020 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/quic/quic_client_session_cache.h"

#include "base/run_loop.h"
#include "base/test/simple_test_clock.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/boringssl/src/include/openssl/ssl.h"

namespace net {

namespace {

std::unique_ptr<base::SimpleTestClock> MakeTestClock() {
  std::unique_ptr<base::SimpleTestClock> clock =
      std::make_unique<base::SimpleTestClock>();
  // SimpleTestClock starts at the null base::Time which converts to and from
  // time_t confusingly.
  clock->SetNow(base::Time::FromTimeT(1000000000));
  return clock;
}

class QuicClientSessionCacheTest : public testing::Test {
 public:
  QuicClientSessionCacheTest() : ssl_ctx_(SSL_CTX_new(TLS_method())) {}

 protected:
  bssl::UniquePtr<SSL_SESSION> NewSSLSession() {
    SSL_SESSION* session = SSL_SESSION_new(ssl_ctx_.get());
    if (!SSL_SESSION_set_protocol_version(session, TLS1_3_VERSION))
      return nullptr;
    return bssl::UniquePtr<SSL_SESSION>(session);
  }

  bssl::UniquePtr<SSL_SESSION> MakeTestSession(base::Time now,
                                               base::TimeDelta timeout) {
    bssl::UniquePtr<SSL_SESSION> session = NewSSLSession();
    SSL_SESSION_set_time(session.get(), now.ToTimeT());
    SSL_SESSION_set_timeout(session.get(), timeout.InSeconds());
    return session;
  }

  bssl::UniquePtr<SSL_CTX> ssl_ctx_;
};

}  // namespace

// Tests that simple insertion and lookup work correctly.
TEST_F(QuicClientSessionCacheTest, Basic) {
  QuicClientSessionCache cache;

  std::unique_ptr<quic::QuicResumptionState> state1 =
      std::make_unique<quic::QuicResumptionState>();
  state1->application_state.push_back('a');
  state1->tls_session = NewSSLSession();
  quic::QuicServerId id1("a.com", 443);
  std::unique_ptr<quic::QuicResumptionState> state2 =
      std::make_unique<quic::QuicResumptionState>();
  state2->application_state.push_back('b');
  state2->tls_session = NewSSLSession();
  quic::QuicServerId id2("b.com", 443);

  EXPECT_EQ(nullptr, cache.Lookup(id1, ssl_ctx_.get()));
  EXPECT_EQ(nullptr, cache.Lookup(id2, ssl_ctx_.get()));
  EXPECT_EQ(0u, cache.size());

  cache.Insert(id1, std::move(state1));
  EXPECT_EQ(1u, cache.size());
  EXPECT_EQ('a', cache.Lookup(id1, ssl_ctx_.get())->application_state.front());
  EXPECT_EQ(nullptr, cache.Lookup(id2, ssl_ctx_.get()));

  std::unique_ptr<quic::QuicResumptionState> state3 =
      std::make_unique<quic::QuicResumptionState>();
  state3->application_state.push_back('c');
  state3->tls_session = NewSSLSession();
  quic::QuicServerId id3("c.com", 443);
  cache.Insert(id3, std::move(state3));
  cache.Insert(id2, std::move(state2));
  EXPECT_EQ(2u, cache.size());
  EXPECT_EQ('b', cache.Lookup(id2, ssl_ctx_.get())->application_state.front());
  EXPECT_EQ('c', cache.Lookup(id3, ssl_ctx_.get())->application_state.front());

  // Verify that the cache is cleared after Lookups.
  EXPECT_EQ(nullptr, cache.Lookup(id1, ssl_ctx_.get()));
  EXPECT_EQ(nullptr, cache.Lookup(id2, ssl_ctx_.get()));
  EXPECT_EQ(nullptr, cache.Lookup(id3, ssl_ctx_.get()));
  EXPECT_EQ(0u, cache.size());
}

// When the size limit is exceeded, the oldest entry should be erased.
TEST_F(QuicClientSessionCacheTest, SizeLimit) {
  QuicClientSessionCache cache(2);

  std::unique_ptr<quic::QuicResumptionState> state1 =
      std::make_unique<quic::QuicResumptionState>();
  state1->application_state.push_back('a');
  state1->tls_session = NewSSLSession();
  quic::QuicServerId id1("a.com", 443);

  std::unique_ptr<quic::QuicResumptionState> state2 =
      std::make_unique<quic::QuicResumptionState>();
  state2->application_state.push_back('b');
  state2->tls_session = NewSSLSession();
  quic::QuicServerId id2("b.com", 443);

  std::unique_ptr<quic::QuicResumptionState> state3 =
      std::make_unique<quic::QuicResumptionState>();
  state3->application_state.push_back('c');
  state3->tls_session = NewSSLSession();
  quic::QuicServerId id3("c.com", 443);

  cache.Insert(id1, std::move(state1));
  cache.Insert(id2, std::move(state2));
  cache.Insert(id3, std::move(state3));

  EXPECT_EQ(2u, cache.size());
  EXPECT_EQ('b', cache.Lookup(id2, ssl_ctx_.get())->application_state.front());
  EXPECT_EQ('c', cache.Lookup(id3, ssl_ctx_.get())->application_state.front());
  EXPECT_EQ(nullptr, cache.Lookup(id1, ssl_ctx_.get()));
}

// Expired session isn't considered valid and nullptr will be returned upon
// Lookup.
TEST_F(QuicClientSessionCacheTest, Expiration) {
  const base::TimeDelta kTimeout = base::TimeDelta::FromSeconds(1000);
  QuicClientSessionCache cache;
  std::unique_ptr<base::SimpleTestClock> clock = MakeTestClock();
  cache.SetClockForTesting(clock.get());

  std::unique_ptr<quic::QuicResumptionState> state1 =
      std::make_unique<quic::QuicResumptionState>();
  state1->tls_session = MakeTestSession(clock->Now(), kTimeout);
  quic::QuicServerId id1("a.com", 443);

  std::unique_ptr<quic::QuicResumptionState> state2 =
      std::make_unique<quic::QuicResumptionState>();
  state2->tls_session = MakeTestSession(clock->Now(), 3 * kTimeout);
  ;
  state2->application_state.push_back('b');
  quic::QuicServerId id2("b.com", 443);

  cache.Insert(id1, std::move(state1));
  cache.Insert(id2, std::move(state2));

  EXPECT_EQ(2u, cache.size());
  // Expire the session.
  clock->Advance(kTimeout * 2);
  // The entry has not been removed yet.
  EXPECT_EQ(2u, cache.size());

  EXPECT_EQ(nullptr, cache.Lookup(id1, ssl_ctx_.get()));
  EXPECT_EQ(1u, cache.size());
  EXPECT_EQ('b', cache.Lookup(id2, ssl_ctx_.get())->application_state.front());
  EXPECT_EQ(0u, cache.size());
}

TEST_F(QuicClientSessionCacheTest, FlushOnMemoryNotifications) {
  base::test::TaskEnvironment task_environment;
  const base::TimeDelta kTimeout = base::TimeDelta::FromSeconds(1000);
  QuicClientSessionCache cache;
  std::unique_ptr<base::SimpleTestClock> clock = MakeTestClock();
  cache.SetClockForTesting(clock.get());

  std::unique_ptr<quic::QuicResumptionState> state1 =
      std::make_unique<quic::QuicResumptionState>();
  state1->tls_session = MakeTestSession(clock->Now(), kTimeout);
  quic::QuicServerId id1("a.com", 443);

  std::unique_ptr<quic::QuicResumptionState> state2 =
      std::make_unique<quic::QuicResumptionState>();
  state2->tls_session = MakeTestSession(clock->Now(), 3 * kTimeout);
  ;
  state2->application_state.push_back('b');
  quic::QuicServerId id2("b.com", 443);

  cache.Insert(id1, std::move(state1));
  cache.Insert(id2, std::move(state2));

  EXPECT_EQ(2u, cache.size());
  // Expire the session.
  clock->Advance(kTimeout * 2);
  // The entry has not been removed yet.
  EXPECT_EQ(2u, cache.size());

  // Fire a notification that will flush expired sessions.
  base::MemoryPressureListener::NotifyMemoryPressure(
      base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE);
  base::RunLoop().RunUntilIdle();

  // session1 is expired and should be flushed.
  EXPECT_EQ(nullptr, cache.Lookup(id1, ssl_ctx_.get()));
  EXPECT_EQ(1u, cache.size());

  // Fire notification that will flush everything.
  base::MemoryPressureListener::NotifyMemoryPressure(
      base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL);
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(0u, cache.size());
}

}  // namespace net