Fix build.
[chromium-blink-merge.git] / net / ssl / ssl_config_service_unittest.cc
blob7306c68d71319a596f7c6028651413ef1523e8b2
1 // Copyright (c) 2012 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/ssl/ssl_config_service.h"
7 #include <vector>
9 #include "base/basictypes.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace net {
15 namespace {
17 class MockSSLConfigService : public SSLConfigService {
18 public:
19 explicit MockSSLConfigService(const SSLConfig& config) : config_(config) {}
21 // SSLConfigService implementation
22 void GetSSLConfig(SSLConfig* config) override { *config = config_; }
24 // Sets the SSLConfig to be returned by GetSSLConfig and processes any
25 // updates.
26 void SetSSLConfig(const SSLConfig& config) {
27 SSLConfig old_config = config_;
28 config_ = config;
29 ProcessConfigUpdate(old_config, config_);
32 private:
33 ~MockSSLConfigService() override {}
35 SSLConfig config_;
38 class MockSSLConfigServiceObserver : public SSLConfigService::Observer {
39 public:
40 MockSSLConfigServiceObserver() {}
41 virtual ~MockSSLConfigServiceObserver() {}
43 MOCK_METHOD0(OnSSLConfigChanged, void());
46 } // namespace
48 TEST(SSLConfigServiceTest, NoChangesWontNotifyObservers) {
49 SSLConfig initial_config;
50 initial_config.rev_checking_enabled = true;
51 initial_config.false_start_enabled = false;
52 initial_config.version_min = SSL_PROTOCOL_VERSION_SSL3;
53 initial_config.version_max = SSL_PROTOCOL_VERSION_TLS1_1;
55 scoped_refptr<MockSSLConfigService> mock_service(
56 new MockSSLConfigService(initial_config));
57 MockSSLConfigServiceObserver observer;
58 mock_service->AddObserver(&observer);
60 EXPECT_CALL(observer, OnSSLConfigChanged()).Times(0);
61 mock_service->SetSSLConfig(initial_config);
63 mock_service->RemoveObserver(&observer);
66 TEST(SSLConfigServiceTest, ConfigUpdatesNotifyObservers) {
67 SSLConfig initial_config;
68 initial_config.rev_checking_enabled = true;
69 initial_config.false_start_enabled = false;
70 initial_config.version_min = SSL_PROTOCOL_VERSION_SSL3;
71 initial_config.version_max = SSL_PROTOCOL_VERSION_TLS1_1;
73 scoped_refptr<MockSSLConfigService> mock_service(
74 new MockSSLConfigService(initial_config));
75 MockSSLConfigServiceObserver observer;
76 mock_service->AddObserver(&observer);
78 // Test that the basic boolean preferences trigger updates.
79 initial_config.rev_checking_enabled = false;
80 EXPECT_CALL(observer, OnSSLConfigChanged()).Times(1);
81 mock_service->SetSSLConfig(initial_config);
83 initial_config.false_start_enabled = true;
84 EXPECT_CALL(observer, OnSSLConfigChanged()).Times(1);
85 mock_service->SetSSLConfig(initial_config);
87 // Test that changing the SSL version range triggers updates.
88 initial_config.version_min = SSL_PROTOCOL_VERSION_TLS1;
89 EXPECT_CALL(observer, OnSSLConfigChanged()).Times(1);
90 mock_service->SetSSLConfig(initial_config);
92 initial_config.version_max = SSL_PROTOCOL_VERSION_SSL3;
93 EXPECT_CALL(observer, OnSSLConfigChanged()).Times(1);
94 mock_service->SetSSLConfig(initial_config);
96 // Test that disabling certain cipher suites triggers an update.
97 std::vector<uint16> disabled_ciphers;
98 disabled_ciphers.push_back(0x0004u);
99 disabled_ciphers.push_back(0xBEEFu);
100 disabled_ciphers.push_back(0xDEADu);
101 initial_config.disabled_cipher_suites = disabled_ciphers;
102 EXPECT_CALL(observer, OnSSLConfigChanged()).Times(1);
103 mock_service->SetSSLConfig(initial_config);
105 // Ensure that changing a disabled cipher suite, while still maintaining
106 // sorted order, triggers an update.
107 disabled_ciphers[1] = 0xCAFEu;
108 initial_config.disabled_cipher_suites = disabled_ciphers;
109 EXPECT_CALL(observer, OnSSLConfigChanged()).Times(1);
110 mock_service->SetSSLConfig(initial_config);
112 // Ensure that removing a disabled cipher suite, while still keeping some
113 // cipher suites disabled, triggers an update.
114 disabled_ciphers.pop_back();
115 initial_config.disabled_cipher_suites = disabled_ciphers;
116 EXPECT_CALL(observer, OnSSLConfigChanged()).Times(1);
117 mock_service->SetSSLConfig(initial_config);
119 mock_service->RemoveObserver(&observer);
122 } // namespace net