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/base/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
) {
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
.version_min
= SSL_PROTOCOL_VERSION_SSL3
;
54 initial_config
.version_max
= SSL_PROTOCOL_VERSION_TLS1_1
;
56 scoped_refptr
<MockSSLConfigService
> mock_service(
57 new MockSSLConfigService(initial_config
));
58 MockSSLConfigServiceObserver observer
;
59 mock_service
->AddObserver(&observer
);
61 EXPECT_CALL(observer
, OnSSLConfigChanged()).Times(0);
62 mock_service
->SetSSLConfig(initial_config
);
64 mock_service
->RemoveObserver(&observer
);
67 TEST(SSLConfigServiceTest
, ConfigUpdatesNotifyObservers
) {
68 SSLConfig initial_config
;
69 initial_config
.rev_checking_enabled
= true;
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
.version_min
= SSL_PROTOCOL_VERSION_TLS1
;
84 EXPECT_CALL(observer
, OnSSLConfigChanged()).Times(1);
85 mock_service
->SetSSLConfig(initial_config
);
87 initial_config
.version_max
= SSL_PROTOCOL_VERSION_SSL3
;
88 EXPECT_CALL(observer
, OnSSLConfigChanged()).Times(1);
89 mock_service
->SetSSLConfig(initial_config
);
91 // Test that disabling certain cipher suites triggers an update.
92 std::vector
<uint16
> disabled_ciphers
;
93 disabled_ciphers
.push_back(0x0004u
);
94 disabled_ciphers
.push_back(0xBEEFu
);
95 disabled_ciphers
.push_back(0xDEADu
);
96 initial_config
.disabled_cipher_suites
= disabled_ciphers
;
97 EXPECT_CALL(observer
, OnSSLConfigChanged()).Times(1);
98 mock_service
->SetSSLConfig(initial_config
);
100 // Ensure that changing a disabled cipher suite, while still maintaining
101 // sorted order, triggers an update.
102 disabled_ciphers
[1] = 0xCAFEu
;
103 initial_config
.disabled_cipher_suites
= disabled_ciphers
;
104 EXPECT_CALL(observer
, OnSSLConfigChanged()).Times(1);
105 mock_service
->SetSSLConfig(initial_config
);
107 // Ensure that removing a disabled cipher suite, while still keeping some
108 // cipher suites disabled, triggers an update.
109 disabled_ciphers
.pop_back();
110 initial_config
.disabled_cipher_suites
= disabled_ciphers
;
111 EXPECT_CALL(observer
, OnSSLConfigChanged()).Times(1);
112 mock_service
->SetSSLConfig(initial_config
);
114 mock_service
->RemoveObserver(&observer
);