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"
9 #include "base/basictypes.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
17 class MockSSLConfigService
: public SSLConfigService
{
19 explicit MockSSLConfigService(const SSLConfig
& config
) : config_(config
) {}
21 // SSLConfigService implementation
22 virtual void GetSSLConfig(SSLConfig
* config
) OVERRIDE
{
26 // Sets the SSLConfig to be returned by GetSSLConfig and processes any
28 void SetSSLConfig(const SSLConfig
& config
) {
29 SSLConfig old_config
= config_
;
31 ProcessConfigUpdate(old_config
, config_
);
35 virtual ~MockSSLConfigService() {}
40 class MockSSLConfigServiceObserver
: public SSLConfigService::Observer
{
42 MockSSLConfigServiceObserver() {}
43 virtual ~MockSSLConfigServiceObserver() {}
45 MOCK_METHOD0(OnSSLConfigChanged
, void());
50 TEST(SSLConfigServiceTest
, NoChangesWontNotifyObservers
) {
51 SSLConfig initial_config
;
52 initial_config
.rev_checking_enabled
= true;
53 initial_config
.false_start_enabled
= false;
54 initial_config
.version_min
= SSL_PROTOCOL_VERSION_SSL3
;
55 initial_config
.version_max
= SSL_PROTOCOL_VERSION_TLS1_1
;
57 scoped_refptr
<MockSSLConfigService
> mock_service(
58 new MockSSLConfigService(initial_config
));
59 MockSSLConfigServiceObserver observer
;
60 mock_service
->AddObserver(&observer
);
62 EXPECT_CALL(observer
, OnSSLConfigChanged()).Times(0);
63 mock_service
->SetSSLConfig(initial_config
);
65 mock_service
->RemoveObserver(&observer
);
68 TEST(SSLConfigServiceTest
, ConfigUpdatesNotifyObservers
) {
69 SSLConfig initial_config
;
70 initial_config
.rev_checking_enabled
= true;
71 initial_config
.false_start_enabled
= false;
72 initial_config
.version_min
= SSL_PROTOCOL_VERSION_SSL3
;
73 initial_config
.version_max
= SSL_PROTOCOL_VERSION_TLS1_1
;
75 scoped_refptr
<MockSSLConfigService
> mock_service(
76 new MockSSLConfigService(initial_config
));
77 MockSSLConfigServiceObserver observer
;
78 mock_service
->AddObserver(&observer
);
80 // Test that the basic boolean preferences trigger updates.
81 initial_config
.rev_checking_enabled
= false;
82 EXPECT_CALL(observer
, OnSSLConfigChanged()).Times(1);
83 mock_service
->SetSSLConfig(initial_config
);
85 initial_config
.false_start_enabled
= true;
86 EXPECT_CALL(observer
, OnSSLConfigChanged()).Times(1);
87 mock_service
->SetSSLConfig(initial_config
);
89 // Test that changing the SSL version range triggers updates.
90 initial_config
.version_min
= SSL_PROTOCOL_VERSION_TLS1
;
91 EXPECT_CALL(observer
, OnSSLConfigChanged()).Times(1);
92 mock_service
->SetSSLConfig(initial_config
);
94 initial_config
.version_max
= SSL_PROTOCOL_VERSION_SSL3
;
95 EXPECT_CALL(observer
, OnSSLConfigChanged()).Times(1);
96 mock_service
->SetSSLConfig(initial_config
);
98 // Test that disabling certain cipher suites triggers an update.
99 std::vector
<uint16
> disabled_ciphers
;
100 disabled_ciphers
.push_back(0x0004u
);
101 disabled_ciphers
.push_back(0xBEEFu
);
102 disabled_ciphers
.push_back(0xDEADu
);
103 initial_config
.disabled_cipher_suites
= disabled_ciphers
;
104 EXPECT_CALL(observer
, OnSSLConfigChanged()).Times(1);
105 mock_service
->SetSSLConfig(initial_config
);
107 // Ensure that changing a disabled cipher suite, while still maintaining
108 // sorted order, triggers an update.
109 disabled_ciphers
[1] = 0xCAFEu
;
110 initial_config
.disabled_cipher_suites
= disabled_ciphers
;
111 EXPECT_CALL(observer
, OnSSLConfigChanged()).Times(1);
112 mock_service
->SetSSLConfig(initial_config
);
114 // Ensure that removing a disabled cipher suite, while still keeping some
115 // cipher suites disabled, triggers an update.
116 disabled_ciphers
.pop_back();
117 initial_config
.disabled_cipher_suites
= disabled_ciphers
;
118 EXPECT_CALL(observer
, OnSSLConfigChanged()).Times(1);
119 mock_service
->SetSSLConfig(initial_config
);
121 mock_service
->RemoveObserver(&observer
);