forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssl_config_service_mojo_unittest.cc
299 lines (253 loc) · 11.7 KB
/
ssl_config_service_mojo_unittest.cc
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
// Copyright 2018 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 "services/network/ssl_config_service_mojo.h"
#include "base/run_loop.h"
#include "base/stl_util.h"
#include "base/test/scoped_task_environment.h"
#include "mojo/public/cpp/bindings/interface_request.h"
#include "net/ssl/ssl_config.h"
#include "net/ssl/ssl_config_service.h"
#include "net/url_request/url_request_context.h"
#include "services/network/network_context.h"
#include "services/network/network_service.h"
#include "services/network/public/mojom/network_service.mojom.h"
#include "services/network/public/mojom/ssl_config.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace network {
namespace {
class TestSSLConfigServiceObserver : public net::SSLConfigService::Observer {
public:
explicit TestSSLConfigServiceObserver(
net::SSLConfigService* ssl_config_service)
: ssl_config_service_(ssl_config_service) {
ssl_config_service_->AddObserver(this);
}
~TestSSLConfigServiceObserver() override {
EXPECT_EQ(observed_changes_, changes_to_wait_for_);
ssl_config_service_->RemoveObserver(this);
}
// net::SSLConfigService::Observer implementation:
void OnSSLConfigChanged() override {
++observed_changes_;
ssl_config_service_->GetSSLConfig(&ssl_config_during_change_);
if (run_loop_)
run_loop_->Quit();
}
// Waits for a SSLConfig change. The first time it's called, waits for the
// first change, if one hasn't been observed already, the second time, waits
// for the second, etc. Must be called once for each change that happens, and
// fails it more than once change happens between calls, or during a call.
void WaitForChange() {
EXPECT_FALSE(run_loop_);
++changes_to_wait_for_;
if (changes_to_wait_for_ == observed_changes_)
return;
EXPECT_LT(observed_changes_, changes_to_wait_for_);
run_loop_ = std::make_unique<base::RunLoop>();
run_loop_->Run();
run_loop_.reset();
EXPECT_EQ(observed_changes_, changes_to_wait_for_);
}
const net::SSLConfig& ssl_config_during_change() const {
return ssl_config_during_change_;
}
int observed_changes() const { return observed_changes_; }
private:
net::SSLConfigService* const ssl_config_service_;
int observed_changes_ = 0;
int changes_to_wait_for_ = 0;
net::SSLConfig ssl_config_during_change_;
std::unique_ptr<base::RunLoop> run_loop_;
};
class NetworkServiceSSLConfigServiceTest : public testing::Test {
public:
NetworkServiceSSLConfigServiceTest()
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::IO),
network_service_(NetworkService::CreateForTesting()) {}
// Creates a NetworkContext using the specified NetworkContextParams, and
// stores it in |network_context_|.
void SetUpNetworkContext(
mojom::NetworkContextParamsPtr network_context_params) {
network_context_params->ssl_config_client_request =
mojo::MakeRequest(&ssl_config_client_);
network_context_ = std::make_unique<NetworkContext>(
network_service_.get(), mojo::MakeRequest(&network_context_ptr_),
std::move(network_context_params));
}
// Returns the current SSLConfig for |network_context_|.
net::SSLConfig GetSSLConfig() {
net::SSLConfig ssl_config;
network_context_->url_request_context()->ssl_config_service()->GetSSLConfig(
&ssl_config);
return ssl_config;
}
// Runs two conversion tests for |mojo_config|. Uses it as a initial
// SSLConfig for a NetworkContext, making sure it matches
// |expected_net_config|. Then switches to the default configuration and then
// back to |mojo_config|, to make sure it works as a new configuration. The
// expected configuration must not be the default configuration.
void RunConversionTests(const mojom::SSLConfig& mojo_config,
const net::SSLConfig& expected_net_config) {
// The expected configuration must not be the default configuration, or the
// change test won't send an event.
EXPECT_FALSE(net::SSLConfigService::SSLConfigsAreEqualForTesting(
net::SSLConfig(), expected_net_config));
// Set up |mojo_config| as the initial configuration of a NetworkContext.
mojom::NetworkContextParamsPtr network_context_params =
mojom::NetworkContextParams::New();
network_context_params->initial_ssl_config = mojo_config.Clone();
SetUpNetworkContext(std::move(network_context_params));
EXPECT_TRUE(net::SSLConfigService::SSLConfigsAreEqualForTesting(
GetSSLConfig(), expected_net_config));
// Sanity check.
EXPECT_FALSE(net::SSLConfigService::SSLConfigsAreEqualForTesting(
GetSSLConfig(), net::SSLConfig()));
// Reset the configuration to the default ones, and check the results.
TestSSLConfigServiceObserver observer(
network_context_->url_request_context()->ssl_config_service());
ssl_config_client_->OnSSLConfigUpdated(mojom::SSLConfig::New());
observer.WaitForChange();
EXPECT_TRUE(net::SSLConfigService::SSLConfigsAreEqualForTesting(
GetSSLConfig(), net::SSLConfig()));
EXPECT_TRUE(net::SSLConfigService::SSLConfigsAreEqualForTesting(
observer.ssl_config_during_change(), net::SSLConfig()));
// Sanity check.
EXPECT_FALSE(net::SSLConfigService::SSLConfigsAreEqualForTesting(
GetSSLConfig(), expected_net_config));
// Set the configuration to |mojo_config| again, and check the results.
ssl_config_client_->OnSSLConfigUpdated(mojo_config.Clone());
observer.WaitForChange();
EXPECT_TRUE(net::SSLConfigService::SSLConfigsAreEqualForTesting(
GetSSLConfig(), expected_net_config));
EXPECT_TRUE(net::SSLConfigService::SSLConfigsAreEqualForTesting(
observer.ssl_config_during_change(), expected_net_config));
}
protected:
base::test::ScopedTaskEnvironment scoped_task_environment_;
std::unique_ptr<NetworkService> network_service_;
mojom::SSLConfigClientPtr ssl_config_client_;
mojom::NetworkContextPtr network_context_ptr_;
std::unique_ptr<NetworkContext> network_context_;
};
// Check that passing in a no mojom::SSLConfig matches the default
// net::SSLConfig.
TEST_F(NetworkServiceSSLConfigServiceTest, NoSSLConfig) {
SetUpNetworkContext(mojom::NetworkContextParams::New());
EXPECT_TRUE(net::SSLConfigService::SSLConfigsAreEqualForTesting(
GetSSLConfig(), net::SSLConfig()));
// Make sure the default TLS version range is as expected.
EXPECT_EQ(net::kDefaultSSLVersionMin, GetSSLConfig().version_min);
EXPECT_EQ(net::kDefaultSSLVersionMax, GetSSLConfig().version_max);
EXPECT_EQ(net::kDefaultTLS13Variant, GetSSLConfig().tls13_variant);
}
// Check that passing in the default mojom::SSLConfig matches the default
// net::SSLConfig.
TEST_F(NetworkServiceSSLConfigServiceTest, Default) {
mojom::NetworkContextParamsPtr network_context_params =
mojom::NetworkContextParams::New();
network_context_params->initial_ssl_config = mojom::SSLConfig::New();
SetUpNetworkContext(std::move(network_context_params));
EXPECT_TRUE(net::SSLConfigService::SSLConfigsAreEqualForTesting(
GetSSLConfig(), net::SSLConfig()));
// Make sure the default TLS version range is as expected.
EXPECT_EQ(net::kDefaultSSLVersionMin, GetSSLConfig().version_min);
EXPECT_EQ(net::kDefaultSSLVersionMax, GetSSLConfig().version_max);
EXPECT_EQ(net::kDefaultTLS13Variant, GetSSLConfig().tls13_variant);
}
TEST_F(NetworkServiceSSLConfigServiceTest, RevCheckingEnabled) {
net::SSLConfig expected_net_config;
// Use the opposite of the default value.
expected_net_config.rev_checking_enabled =
!expected_net_config.rev_checking_enabled;
mojom::SSLConfigPtr mojo_config = mojom::SSLConfig::New();
mojo_config->rev_checking_enabled = expected_net_config.rev_checking_enabled;
RunConversionTests(*mojo_config, expected_net_config);
}
TEST_F(NetworkServiceSSLConfigServiceTest,
RevCheckingRequiredLocalTrustAnchors) {
net::SSLConfig expected_net_config;
// Use the opposite of the default value.
expected_net_config.rev_checking_required_local_anchors =
!expected_net_config.rev_checking_required_local_anchors;
mojom::SSLConfigPtr mojo_config = mojom::SSLConfig::New();
mojo_config->rev_checking_required_local_anchors =
expected_net_config.rev_checking_required_local_anchors;
RunConversionTests(*mojo_config, expected_net_config);
}
TEST_F(NetworkServiceSSLConfigServiceTest, Sha1LocalAnchorsEnabled) {
net::SSLConfig expected_net_config;
// Use the opposite of the default value.
expected_net_config.sha1_local_anchors_enabled =
!expected_net_config.sha1_local_anchors_enabled;
mojom::SSLConfigPtr mojo_config = mojom::SSLConfig::New();
mojo_config->sha1_local_anchors_enabled =
expected_net_config.sha1_local_anchors_enabled;
RunConversionTests(*mojo_config, expected_net_config);
}
TEST_F(NetworkServiceSSLConfigServiceTest, SymantecEnforcementDisabled) {
net::SSLConfig expected_net_config;
// Use the opposite of the default value.
expected_net_config.symantec_enforcement_disabled =
!expected_net_config.symantec_enforcement_disabled;
mojom::SSLConfigPtr mojo_config = mojom::SSLConfig::New();
mojo_config->symantec_enforcement_disabled =
expected_net_config.symantec_enforcement_disabled;
RunConversionTests(*mojo_config, expected_net_config);
}
TEST_F(NetworkServiceSSLConfigServiceTest, SSLVersion) {
const struct {
mojom::SSLVersion mojo_ssl_version;
int net_ssl_version;
} kVersionTable[] = {
{mojom::SSLVersion::kTLS1, net::SSL_PROTOCOL_VERSION_TLS1},
{mojom::SSLVersion::kTLS11, net::SSL_PROTOCOL_VERSION_TLS1_1},
{mojom::SSLVersion::kTLS12, net::SSL_PROTOCOL_VERSION_TLS1_2},
{mojom::SSLVersion::kTLS13, net::SSL_PROTOCOL_VERSION_TLS1_3},
};
for (size_t min_index = 0; min_index < base::size(kVersionTable);
++min_index) {
for (size_t max_index = min_index; max_index < base::size(kVersionTable);
++max_index) {
// If the versions match the default values, skip this value in the table.
// The defaults will get plenty of testing anyways, when switching back to
// the default values in RunConversionTests().
if (kVersionTable[min_index].net_ssl_version ==
net::SSLConfig().version_min &&
kVersionTable[max_index].net_ssl_version ==
net::SSLConfig().version_max) {
continue;
}
net::SSLConfig expected_net_config;
expected_net_config.version_min =
kVersionTable[min_index].net_ssl_version;
expected_net_config.version_max =
kVersionTable[max_index].net_ssl_version;
mojom::SSLConfigPtr mojo_config = mojom::SSLConfig::New();
mojo_config->version_min = kVersionTable[min_index].mojo_ssl_version;
mojo_config->version_max = kVersionTable[max_index].mojo_ssl_version;
RunConversionTests(*mojo_config, expected_net_config);
}
}
}
TEST_F(NetworkServiceSSLConfigServiceTest, InitialConfigDisableCipherSuite) {
net::SSLConfig expected_net_config;
expected_net_config.disabled_cipher_suites.push_back(0x0004);
mojom::SSLConfigPtr mojo_config = mojom::SSLConfig::New();
mojo_config->disabled_cipher_suites =
expected_net_config.disabled_cipher_suites;
RunConversionTests(*mojo_config, expected_net_config);
}
TEST_F(NetworkServiceSSLConfigServiceTest,
InitialConfigDisableTwoCipherSuites) {
net::SSLConfig expected_net_config;
expected_net_config.disabled_cipher_suites.push_back(0x0004);
expected_net_config.disabled_cipher_suites.push_back(0x0005);
mojom::SSLConfigPtr mojo_config = mojom::SSLConfig::New();
mojo_config->disabled_cipher_suites =
expected_net_config.disabled_cipher_suites;
RunConversionTests(*mojo_config, expected_net_config);
}
} // namespace
} // namespace network