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/ssl/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) OVERRIDE { 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.unrestricted_ssl3_fallback_enabled = false; 73 initial_config.version_min = SSL_PROTOCOL_VERSION_SSL3; 74 initial_config.version_max = SSL_PROTOCOL_VERSION_TLS1_1; 75 76 scoped_refptr<MockSSLConfigService> mock_service( 77 new MockSSLConfigService(initial_config)); 78 MockSSLConfigServiceObserver observer; 79 mock_service->AddObserver(&observer); 80 81 // Test that the basic boolean preferences trigger updates. 82 initial_config.rev_checking_enabled = false; 83 EXPECT_CALL(observer, OnSSLConfigChanged()).Times(1); 84 mock_service->SetSSLConfig(initial_config); 85 86 initial_config.false_start_enabled = true; 87 EXPECT_CALL(observer, OnSSLConfigChanged()).Times(1); 88 mock_service->SetSSLConfig(initial_config); 89 90 initial_config.unrestricted_ssl3_fallback_enabled = true; 91 EXPECT_CALL(observer, OnSSLConfigChanged()).Times(1); 92 mock_service->SetSSLConfig(initial_config); 93 94 // Test that changing the SSL version range triggers updates. 95 initial_config.version_min = SSL_PROTOCOL_VERSION_TLS1; 96 EXPECT_CALL(observer, OnSSLConfigChanged()).Times(1); 97 mock_service->SetSSLConfig(initial_config); 98 99 initial_config.version_max = SSL_PROTOCOL_VERSION_SSL3; 100 EXPECT_CALL(observer, OnSSLConfigChanged()).Times(1); 101 mock_service->SetSSLConfig(initial_config); 102 103 // Test that disabling certain cipher suites triggers an update. 104 std::vector<uint16> disabled_ciphers; 105 disabled_ciphers.push_back(0x0004u); 106 disabled_ciphers.push_back(0xBEEFu); 107 disabled_ciphers.push_back(0xDEADu); 108 initial_config.disabled_cipher_suites = disabled_ciphers; 109 EXPECT_CALL(observer, OnSSLConfigChanged()).Times(1); 110 mock_service->SetSSLConfig(initial_config); 111 112 // Ensure that changing a disabled cipher suite, while still maintaining 113 // sorted order, triggers an update. 114 disabled_ciphers[1] = 0xCAFEu; 115 initial_config.disabled_cipher_suites = disabled_ciphers; 116 EXPECT_CALL(observer, OnSSLConfigChanged()).Times(1); 117 mock_service->SetSSLConfig(initial_config); 118 119 // Ensure that removing a disabled cipher suite, while still keeping some 120 // cipher suites disabled, triggers an update. 121 disabled_ciphers.pop_back(); 122 initial_config.disabled_cipher_suites = disabled_ciphers; 123 EXPECT_CALL(observer, OnSSLConfigChanged()).Times(1); 124 mock_service->SetSSLConfig(initial_config); 125 126 mock_service->RemoveObserver(&observer); 127 } 128 129 } // namespace net 130