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
|
// Copyright 2014 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/http/disk_cache_based_quic_server_info.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/compiler_specific.h"
#include "base/message_loop/message_loop.h"
#include "net/base/net_errors.h"
#include "net/http/mock_http_cache.h"
#include "net/quic/crypto/quic_server_info.h"
#include "net/quic/quic_server_id.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
// This is an empty transaction, needed to register the URL and the test mode.
const MockTransaction kHostInfoTransaction1 = {
"quicserverinfo:https://www.google.com:443",
"",
base::Time(),
"",
LOAD_NORMAL,
"",
"",
base::Time(),
"",
TEST_MODE_NORMAL,
NULL,
0
};
const MockTransaction kHostInfoTransaction2 = {
"quicserverinfo:http://www.google.com:80",
"",
base::Time(),
"",
LOAD_NORMAL,
"",
"",
base::Time(),
"",
TEST_MODE_NORMAL,
NULL,
0
};
} // namespace
// Tests that we can delete a DiskCacheBasedQuicServerInfo object in a
// completion callback for DiskCacheBasedQuicServerInfo::WaitForDataReady.
TEST(DiskCacheBasedQuicServerInfo, DeleteInCallback) {
// Use the blocking mock backend factory to force asynchronous completion
// of quic_server_info->WaitForDataReady(), so that the callback will run.
MockBlockingBackendFactory* factory = new MockBlockingBackendFactory();
MockHttpCache cache(factory);
QuicServerId server_id("www.verisign.com", 443, true, PRIVACY_MODE_DISABLED);
scoped_ptr<QuicServerInfo> quic_server_info(
new DiskCacheBasedQuicServerInfo(server_id, cache.http_cache()));
quic_server_info->Start();
TestCompletionCallback callback;
int rv = quic_server_info->WaitForDataReady(callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
// Now complete the backend creation and let the callback run.
factory->FinishCreation();
EXPECT_EQ(OK, callback.GetResult(rv));
}
// Tests the basic logic of storing, retrieving and updating data.
TEST(DiskCacheBasedQuicServerInfo, Update) {
MockHttpCache cache;
AddMockTransaction(&kHostInfoTransaction1);
TestCompletionCallback callback;
QuicServerId server_id("www.google.com", 443, true, PRIVACY_MODE_DISABLED);
scoped_ptr<QuicServerInfo> quic_server_info(
new DiskCacheBasedQuicServerInfo(server_id, cache.http_cache()));
quic_server_info->Start();
int rv = quic_server_info->WaitForDataReady(callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
QuicServerInfo::State* state = quic_server_info->mutable_state();
EXPECT_TRUE(state->certs.empty());
const string server_config_a = "server_config_a";
const string source_address_token_a = "source_address_token_a";
const string server_config_sig_a = "server_config_sig_a";
const string cert_a = "cert_a";
const string cert_b = "cert_b";
state->server_config = server_config_a;
state->source_address_token = source_address_token_a;
state->server_config_sig = server_config_sig_a;
state->certs.push_back(cert_a);
quic_server_info->Persist();
// Wait until Persist() does the work.
base::MessageLoop::current()->RunUntilIdle();
// Open the stored QuicServerInfo.
quic_server_info.reset(
new DiskCacheBasedQuicServerInfo(server_id, cache.http_cache()));
quic_server_info->Start();
rv = quic_server_info->WaitForDataReady(callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
// And now update the data.
state = quic_server_info->mutable_state();
state->certs.push_back(cert_b);
// Fail instead of DCHECKing double creates.
cache.disk_cache()->set_double_create_check(false);
quic_server_info->Persist();
base::MessageLoop::current()->RunUntilIdle();
// Verify that the state was updated.
quic_server_info.reset(
new DiskCacheBasedQuicServerInfo(server_id, cache.http_cache()));
quic_server_info->Start();
rv = quic_server_info->WaitForDataReady(callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
EXPECT_TRUE(quic_server_info->IsDataReady());
const QuicServerInfo::State& state1 = quic_server_info->state();
EXPECT_EQ(server_config_a, state1.server_config);
EXPECT_EQ(source_address_token_a, state1.source_address_token);
EXPECT_EQ(server_config_sig_a, state1.server_config_sig);
EXPECT_EQ(2U, state1.certs.size());
EXPECT_EQ(cert_a, state1.certs[0]);
EXPECT_EQ(cert_b, state1.certs[1]);
RemoveMockTransaction(&kHostInfoTransaction1);
}
// Test that demonstrates different info is returned when the ports differ.
TEST(DiskCacheBasedQuicServerInfo, UpdateDifferentPorts) {
MockHttpCache cache;
AddMockTransaction(&kHostInfoTransaction1);
AddMockTransaction(&kHostInfoTransaction2);
TestCompletionCallback callback;
// Persist data for port 443.
QuicServerId server_id1("www.google.com", 443, true, PRIVACY_MODE_DISABLED);
scoped_ptr<QuicServerInfo> quic_server_info1(
new DiskCacheBasedQuicServerInfo(server_id1, cache.http_cache()));
quic_server_info1->Start();
int rv = quic_server_info1->WaitForDataReady(callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
QuicServerInfo::State* state1 = quic_server_info1->mutable_state();
EXPECT_TRUE(state1->certs.empty());
const string server_config_a = "server_config_a";
const string source_address_token_a = "source_address_token_a";
const string server_config_sig_a = "server_config_sig_a";
const string cert_a = "cert_a";
state1->server_config = server_config_a;
state1->source_address_token = source_address_token_a;
state1->server_config_sig = server_config_sig_a;
state1->certs.push_back(cert_a);
quic_server_info1->Persist();
// Wait until Persist() does the work.
base::MessageLoop::current()->RunUntilIdle();
// Persist data for port 80.
QuicServerId server_id2("www.google.com", 80, false, PRIVACY_MODE_DISABLED);
scoped_ptr<QuicServerInfo> quic_server_info2(
new DiskCacheBasedQuicServerInfo(server_id2, cache.http_cache()));
quic_server_info2->Start();
rv = quic_server_info2->WaitForDataReady(callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
QuicServerInfo::State* state2 = quic_server_info2->mutable_state();
EXPECT_TRUE(state2->certs.empty());
const string server_config_b = "server_config_b";
const string source_address_token_b = "source_address_token_b";
const string server_config_sig_b = "server_config_sig_b";
const string cert_b = "cert_b";
state2->server_config = server_config_b;
state2->source_address_token = source_address_token_b;
state2->server_config_sig = server_config_sig_b;
state2->certs.push_back(cert_b);
quic_server_info2->Persist();
// Wait until Persist() does the work.
base::MessageLoop::current()->RunUntilIdle();
// Verify the stored QuicServerInfo for port 443.
scoped_ptr<QuicServerInfo> quic_server_info(
new DiskCacheBasedQuicServerInfo(server_id1, cache.http_cache()));
quic_server_info->Start();
rv = quic_server_info->WaitForDataReady(callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
EXPECT_TRUE(quic_server_info->IsDataReady());
const QuicServerInfo::State& state_a = quic_server_info->state();
EXPECT_EQ(server_config_a, state_a.server_config);
EXPECT_EQ(source_address_token_a, state_a.source_address_token);
EXPECT_EQ(server_config_sig_a, state_a.server_config_sig);
EXPECT_EQ(1U, state_a.certs.size());
EXPECT_EQ(cert_a, state_a.certs[0]);
// Verify the stored QuicServerInfo for port 80.
quic_server_info.reset(
new DiskCacheBasedQuicServerInfo(server_id2, cache.http_cache()));
quic_server_info->Start();
rv = quic_server_info->WaitForDataReady(callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
EXPECT_TRUE(quic_server_info->IsDataReady());
const QuicServerInfo::State& state_b = quic_server_info->state();
EXPECT_EQ(server_config_b, state_b.server_config);
EXPECT_EQ(source_address_token_b, state_b.source_address_token);
EXPECT_EQ(server_config_sig_b, state_b.server_config_sig);
EXPECT_EQ(1U, state_b.certs.size());
EXPECT_EQ(cert_b, state_b.certs[0]);
RemoveMockTransaction(&kHostInfoTransaction2);
RemoveMockTransaction(&kHostInfoTransaction1);
}
// Test IsReadyToPersist when there is a pending write.
TEST(DiskCacheBasedQuicServerInfo, IsReadyToPersist) {
MockHttpCache cache;
AddMockTransaction(&kHostInfoTransaction1);
TestCompletionCallback callback;
QuicServerId server_id("www.google.com", 443, true, PRIVACY_MODE_DISABLED);
scoped_ptr<QuicServerInfo> quic_server_info(
new DiskCacheBasedQuicServerInfo(server_id, cache.http_cache()));
EXPECT_FALSE(quic_server_info->IsDataReady());
quic_server_info->Start();
int rv = quic_server_info->WaitForDataReady(callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
EXPECT_TRUE(quic_server_info->IsDataReady());
QuicServerInfo::State* state = quic_server_info->mutable_state();
EXPECT_TRUE(state->certs.empty());
const string server_config_a = "server_config_a";
const string source_address_token_a = "source_address_token_a";
const string server_config_sig_a = "server_config_sig_a";
const string cert_a = "cert_a";
state->server_config = server_config_a;
state->source_address_token = source_address_token_a;
state->server_config_sig = server_config_sig_a;
state->certs.push_back(cert_a);
EXPECT_TRUE(quic_server_info->IsReadyToPersist());
quic_server_info->Persist();
// Once we call Persist, IsReadyToPersist should return false until Persist
// has completed.
EXPECT_FALSE(quic_server_info->IsReadyToPersist());
// Wait until Persist() does the work.
base::MessageLoop::current()->RunUntilIdle();
EXPECT_TRUE(quic_server_info->IsReadyToPersist());
// Verify that the state was updated.
quic_server_info.reset(
new DiskCacheBasedQuicServerInfo(server_id, cache.http_cache()));
quic_server_info->Start();
rv = quic_server_info->WaitForDataReady(callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
EXPECT_TRUE(quic_server_info->IsDataReady());
const QuicServerInfo::State& state1 = quic_server_info->state();
EXPECT_EQ(server_config_a, state1.server_config);
EXPECT_EQ(source_address_token_a, state1.source_address_token);
EXPECT_EQ(server_config_sig_a, state1.server_config_sig);
EXPECT_EQ(1U, state1.certs.size());
EXPECT_EQ(cert_a, state1.certs[0]);
RemoveMockTransaction(&kHostInfoTransaction1);
}
} // namespace net
|