1 // Copyright 2014 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.
5 #include "net/http/disk_cache_based_quic_server_info.h"
8 #include "base/bind_helpers.h"
9 #include "base/compiler_specific.h"
10 #include "base/message_loop/message_loop.h"
11 #include "net/base/net_errors.h"
12 #include "net/http/mock_http_cache.h"
13 #include "net/quic/crypto/quic_server_info.h"
14 #include "net/quic/quic_session_key.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 // This is an empty transaction, needed to register the URL and the test mode.
20 const MockTransaction kHostInfoTransaction1
= {
21 "quicserverinfo:https://www.google.com:443",
35 const MockTransaction kHostInfoTransaction2
= {
36 "quicserverinfo:http://www.google.com:80",
50 // Tests that we can delete a DiskCacheBasedQuicServerInfo object in a
51 // completion callback for DiskCacheBasedQuicServerInfo::WaitForDataReady.
52 TEST(DiskCacheBasedQuicServerInfo
, DeleteInCallback
) {
53 // Use the blocking mock backend factory to force asynchronous completion
54 // of quic_server_info->WaitForDataReady(), so that the callback will run.
55 MockBlockingBackendFactory
* factory
= new MockBlockingBackendFactory();
56 MockHttpCache
cache(factory
);
57 net::QuicSessionKey
server_key("www.verisign.com", 443, true);
58 scoped_ptr
<net::QuicServerInfo
> quic_server_info(
59 new net::DiskCacheBasedQuicServerInfo(server_key
, cache
.http_cache()));
60 quic_server_info
->Start();
61 net::TestCompletionCallback callback
;
62 int rv
= quic_server_info
->WaitForDataReady(callback
.callback());
63 EXPECT_EQ(net::ERR_IO_PENDING
, rv
);
64 // Now complete the backend creation and let the callback run.
65 factory
->FinishCreation();
66 EXPECT_EQ(net::OK
, callback
.GetResult(rv
));
69 // Tests the basic logic of storing, retrieving and updating data.
70 TEST(DiskCacheBasedQuicServerInfo
, Update
) {
72 AddMockTransaction(&kHostInfoTransaction1
);
73 net::TestCompletionCallback callback
;
75 net::QuicSessionKey
server_key("www.google.com", 443, true);
76 scoped_ptr
<net::QuicServerInfo
> quic_server_info(
77 new net::DiskCacheBasedQuicServerInfo(server_key
, cache
.http_cache()));
78 quic_server_info
->Start();
79 int rv
= quic_server_info
->WaitForDataReady(callback
.callback());
80 EXPECT_EQ(net::OK
, callback
.GetResult(rv
));
82 net::QuicServerInfo::State
* state
= quic_server_info
->mutable_state();
83 EXPECT_TRUE(state
->certs
.empty());
84 const string server_config_a
= "server_config_a";
85 const string source_address_token_a
= "source_address_token_a";
86 const string server_config_sig_a
= "server_config_sig_a";
87 const string cert_a
= "cert_a";
88 const string cert_b
= "cert_b";
90 state
->server_config
= server_config_a
;
91 state
->source_address_token
= source_address_token_a
;
92 state
->server_config_sig
= server_config_sig_a
;
93 state
->certs
.push_back(cert_a
);
94 quic_server_info
->Persist();
96 // Wait until Persist() does the work.
97 base::MessageLoop::current()->RunUntilIdle();
99 // Open the stored QuicServerInfo.
100 quic_server_info
.reset(
101 new net::DiskCacheBasedQuicServerInfo(server_key
, cache
.http_cache()));
102 quic_server_info
->Start();
103 rv
= quic_server_info
->WaitForDataReady(callback
.callback());
104 EXPECT_EQ(net::OK
, callback
.GetResult(rv
));
106 // And now update the data.
107 state
= quic_server_info
->mutable_state();
108 state
->certs
.push_back(cert_b
);
110 // Fail instead of DCHECKing double creates.
111 cache
.disk_cache()->set_double_create_check(false);
112 quic_server_info
->Persist();
113 base::MessageLoop::current()->RunUntilIdle();
115 // Verify that the state was updated.
116 quic_server_info
.reset(
117 new net::DiskCacheBasedQuicServerInfo(server_key
, cache
.http_cache()));
118 quic_server_info
->Start();
119 rv
= quic_server_info
->WaitForDataReady(callback
.callback());
120 EXPECT_EQ(net::OK
, callback
.GetResult(rv
));
121 EXPECT_TRUE(quic_server_info
->IsDataReady());
123 const net::QuicServerInfo::State
& state1
= quic_server_info
->state();
124 EXPECT_EQ(server_config_a
, state1
.server_config
);
125 EXPECT_EQ(source_address_token_a
, state1
.source_address_token
);
126 EXPECT_EQ(server_config_sig_a
, state1
.server_config_sig
);
127 EXPECT_EQ(2U, state1
.certs
.size());
128 EXPECT_EQ(cert_a
, state1
.certs
[0]);
129 EXPECT_EQ(cert_b
, state1
.certs
[1]);
131 RemoveMockTransaction(&kHostInfoTransaction1
);
134 // Test that demonstrates different info is returned when the ports differ.
135 TEST(DiskCacheBasedQuicServerInfo
, UpdateDifferentPorts
) {
137 AddMockTransaction(&kHostInfoTransaction1
);
138 AddMockTransaction(&kHostInfoTransaction2
);
139 net::TestCompletionCallback callback
;
141 // Persist data for port 443.
142 net::QuicSessionKey
server_key1("www.google.com", 443, true);
143 scoped_ptr
<net::QuicServerInfo
> quic_server_info1(
144 new net::DiskCacheBasedQuicServerInfo(server_key1
, cache
.http_cache()));
145 quic_server_info1
->Start();
146 int rv
= quic_server_info1
->WaitForDataReady(callback
.callback());
147 EXPECT_EQ(net::OK
, callback
.GetResult(rv
));
149 net::QuicServerInfo::State
* state1
= quic_server_info1
->mutable_state();
150 EXPECT_TRUE(state1
->certs
.empty());
151 const string server_config_a
= "server_config_a";
152 const string source_address_token_a
= "source_address_token_a";
153 const string server_config_sig_a
= "server_config_sig_a";
154 const string cert_a
= "cert_a";
156 state1
->server_config
= server_config_a
;
157 state1
->source_address_token
= source_address_token_a
;
158 state1
->server_config_sig
= server_config_sig_a
;
159 state1
->certs
.push_back(cert_a
);
160 quic_server_info1
->Persist();
162 // Wait until Persist() does the work.
163 base::MessageLoop::current()->RunUntilIdle();
165 // Persist data for port 80.
166 net::QuicSessionKey
server_key2("www.google.com", 80, false);
167 scoped_ptr
<net::QuicServerInfo
> quic_server_info2(
168 new net::DiskCacheBasedQuicServerInfo(server_key2
, cache
.http_cache()));
169 quic_server_info2
->Start();
170 rv
= quic_server_info2
->WaitForDataReady(callback
.callback());
171 EXPECT_EQ(net::OK
, callback
.GetResult(rv
));
173 net::QuicServerInfo::State
* state2
= quic_server_info2
->mutable_state();
174 EXPECT_TRUE(state2
->certs
.empty());
175 const string server_config_b
= "server_config_b";
176 const string source_address_token_b
= "source_address_token_b";
177 const string server_config_sig_b
= "server_config_sig_b";
178 const string cert_b
= "cert_b";
180 state2
->server_config
= server_config_b
;
181 state2
->source_address_token
= source_address_token_b
;
182 state2
->server_config_sig
= server_config_sig_b
;
183 state2
->certs
.push_back(cert_b
);
184 quic_server_info2
->Persist();
186 // Wait until Persist() does the work.
187 base::MessageLoop::current()->RunUntilIdle();
189 // Verify the stored QuicServerInfo for port 443.
190 scoped_ptr
<net::QuicServerInfo
> quic_server_info(
191 new net::DiskCacheBasedQuicServerInfo(server_key1
, cache
.http_cache()));
192 quic_server_info
->Start();
193 rv
= quic_server_info
->WaitForDataReady(callback
.callback());
194 EXPECT_EQ(net::OK
, callback
.GetResult(rv
));
195 EXPECT_TRUE(quic_server_info
->IsDataReady());
197 const net::QuicServerInfo::State
& state_a
= quic_server_info
->state();
198 EXPECT_EQ(server_config_a
, state_a
.server_config
);
199 EXPECT_EQ(source_address_token_a
, state_a
.source_address_token
);
200 EXPECT_EQ(server_config_sig_a
, state_a
.server_config_sig
);
201 EXPECT_EQ(1U, state_a
.certs
.size());
202 EXPECT_EQ(cert_a
, state_a
.certs
[0]);
204 // Verify the stored QuicServerInfo for port 80.
205 quic_server_info
.reset(
206 new net::DiskCacheBasedQuicServerInfo(server_key2
, cache
.http_cache()));
207 quic_server_info
->Start();
208 rv
= quic_server_info
->WaitForDataReady(callback
.callback());
209 EXPECT_EQ(net::OK
, callback
.GetResult(rv
));
210 EXPECT_TRUE(quic_server_info
->IsDataReady());
212 const net::QuicServerInfo::State
& state_b
= quic_server_info
->state();
213 EXPECT_EQ(server_config_b
, state_b
.server_config
);
214 EXPECT_EQ(source_address_token_b
, state_b
.source_address_token
);
215 EXPECT_EQ(server_config_sig_b
, state_b
.server_config_sig
);
216 EXPECT_EQ(1U, state_b
.certs
.size());
217 EXPECT_EQ(cert_b
, state_b
.certs
[0]);
219 RemoveMockTransaction(&kHostInfoTransaction2
);
220 RemoveMockTransaction(&kHostInfoTransaction1
);
223 // Test IsReadyToPersist when there is a pending write.
224 TEST(DiskCacheBasedQuicServerInfo
, IsReadyToPersist
) {
226 AddMockTransaction(&kHostInfoTransaction1
);
227 net::TestCompletionCallback callback
;
229 net::QuicSessionKey
server_key("www.google.com", 443, true);
230 scoped_ptr
<net::QuicServerInfo
> quic_server_info(
231 new net::DiskCacheBasedQuicServerInfo(server_key
, cache
.http_cache()));
232 EXPECT_FALSE(quic_server_info
->IsDataReady());
233 quic_server_info
->Start();
234 int rv
= quic_server_info
->WaitForDataReady(callback
.callback());
235 EXPECT_EQ(net::OK
, callback
.GetResult(rv
));
236 EXPECT_TRUE(quic_server_info
->IsDataReady());
238 net::QuicServerInfo::State
* state
= quic_server_info
->mutable_state();
239 EXPECT_TRUE(state
->certs
.empty());
240 const string server_config_a
= "server_config_a";
241 const string source_address_token_a
= "source_address_token_a";
242 const string server_config_sig_a
= "server_config_sig_a";
243 const string cert_a
= "cert_a";
245 state
->server_config
= server_config_a
;
246 state
->source_address_token
= source_address_token_a
;
247 state
->server_config_sig
= server_config_sig_a
;
248 state
->certs
.push_back(cert_a
);
249 EXPECT_TRUE(quic_server_info
->IsReadyToPersist());
250 quic_server_info
->Persist();
252 // Once we call Persist, IsReadyToPersist should return false until Persist
254 EXPECT_FALSE(quic_server_info
->IsReadyToPersist());
256 // Wait until Persist() does the work.
257 base::MessageLoop::current()->RunUntilIdle();
259 EXPECT_TRUE(quic_server_info
->IsReadyToPersist());
261 // Verify that the state was updated.
262 quic_server_info
.reset(
263 new net::DiskCacheBasedQuicServerInfo(server_key
, cache
.http_cache()));
264 quic_server_info
->Start();
265 rv
= quic_server_info
->WaitForDataReady(callback
.callback());
266 EXPECT_EQ(net::OK
, callback
.GetResult(rv
));
267 EXPECT_TRUE(quic_server_info
->IsDataReady());
269 const net::QuicServerInfo::State
& state1
= quic_server_info
->state();
270 EXPECT_EQ(server_config_a
, state1
.server_config
);
271 EXPECT_EQ(source_address_token_a
, state1
.source_address_token
);
272 EXPECT_EQ(server_config_sig_a
, state1
.server_config_sig
);
273 EXPECT_EQ(1U, state1
.certs
.size());
274 EXPECT_EQ(cert_a
, state1
.certs
[0]);
276 RemoveMockTransaction(&kHostInfoTransaction1
);