David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "net/base/ssl_config_service.h" |
| 6 | |
| 7 | #include <vector> |
| 8 | |
| 9 | #include "base/basictypes.h" |
| 10 | #include "testing/gmock/include/gmock/gmock.h" |
| 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | namespace net { |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | class MockSSLConfigService : public SSLConfigService { |
| 18 | public: |
| 19 | explicit MockSSLConfigService(const SSLConfig& config) : config_(config) {} |
| 20 | |
| 21 | // SSLConfigService implementation |
| 22 | virtual void GetSSLConfig(SSLConfig* config) { |
| 23 | *config = config_; |
| 24 | } |
| 25 | |
| 26 | // Sets the SSLConfig to be returned by GetSSLConfig and processes any |
| 27 | // updates. |
| 28 | void SetSSLConfig(const SSLConfig& config) { |
| 29 | SSLConfig old_config = config_; |
| 30 | config_ = config; |
| 31 | ProcessConfigUpdate(old_config, config_); |
| 32 | } |
| 33 | |
| 34 | private: |
| 35 | virtual ~MockSSLConfigService() {} |
| 36 | |
| 37 | SSLConfig config_; |
| 38 | }; |
| 39 | |
| 40 | class MockSSLConfigServiceObserver : public SSLConfigService::Observer { |
| 41 | public: |
| 42 | MockSSLConfigServiceObserver() {} |
| 43 | virtual ~MockSSLConfigServiceObserver() {} |
| 44 | |
| 45 | MOCK_METHOD0(OnSSLConfigChanged, void()); |
| 46 | }; |
| 47 | |
| 48 | } // namespace |
| 49 | |
| 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; |
| 56 | |
| 57 | scoped_refptr<MockSSLConfigService> mock_service( |
| 58 | new MockSSLConfigService(initial_config)); |
| 59 | MockSSLConfigServiceObserver observer; |
| 60 | mock_service->AddObserver(&observer); |
| 61 | |
| 62 | EXPECT_CALL(observer, OnSSLConfigChanged()).Times(0); |
| 63 | mock_service->SetSSLConfig(initial_config); |
| 64 | |
| 65 | mock_service->RemoveObserver(&observer); |
| 66 | } |
| 67 | |
| 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; |
| 74 | |
| 75 | scoped_refptr<MockSSLConfigService> mock_service( |
| 76 | new MockSSLConfigService(initial_config)); |
| 77 | MockSSLConfigServiceObserver observer; |
| 78 | mock_service->AddObserver(&observer); |
| 79 | |
| 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); |
| 84 | |
| 85 | initial_config.false_start_enabled = true; |
| 86 | EXPECT_CALL(observer, OnSSLConfigChanged()).Times(1); |
| 87 | mock_service->SetSSLConfig(initial_config); |
| 88 | |
| 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); |
| 93 | |
| 94 | initial_config.version_max = SSL_PROTOCOL_VERSION_SSL3; |
| 95 | EXPECT_CALL(observer, OnSSLConfigChanged()).Times(1); |
| 96 | mock_service->SetSSLConfig(initial_config); |
| 97 | |
| 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); |
| 106 | |
| 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); |
| 113 | |
| 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); |
| 120 | |
| 121 | mock_service->RemoveObserver(&observer); |
| 122 | } |
| 123 | |
| 124 | } // namespace net |