OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/net/pref_proxy_config_service.h" | 5 #include "chrome/browser/net/pref_proxy_config_service.h" |
6 | 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/memory/ref_counted.h" |
7 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
8 #include "base/values.h" | 10 #include "base/values.h" |
9 #include "chrome/browser/net/ssl_config_service_manager.h" | 11 #include "chrome/browser/net/ssl_config_service_manager.h" |
| 12 #include "chrome/browser/prefs/pref_service_mock_builder.h" |
| 13 #include "chrome/browser/prefs/testing_pref_store.h" |
| 14 #include "chrome/common/chrome_switches.h" |
10 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
11 #include "chrome/test/base/testing_pref_service.h" | 16 #include "chrome/test/base/testing_pref_service.h" |
12 #include "content/test/test_browser_thread.h" | 17 #include "content/test/test_browser_thread.h" |
13 #include "net/base/ssl_config_service.h" | 18 #include "net/base/ssl_config_service.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
15 | 20 |
16 using base::ListValue; | 21 using base::ListValue; |
17 using base::Value; | 22 using base::Value; |
18 using net::SSLConfig; | 23 using net::SSLConfig; |
19 using net::SSLConfigService; | 24 using net::SSLConfigService; |
20 | 25 |
21 class SSLConfigServiceManagerPrefTest : public testing::Test { | 26 class SSLConfigServiceManagerPrefTest : public testing::Test { |
22 public: | 27 public: |
23 SSLConfigServiceManagerPrefTest() {} | 28 SSLConfigServiceManagerPrefTest() |
24 | 29 : ui_thread_(BrowserThread::UI, &message_loop_), |
25 virtual void SetUp() { | 30 io_thread_(BrowserThread::IO, &message_loop_) {} |
26 message_loop_.reset(new MessageLoop()); | |
27 ui_thread_.reset( | |
28 new content::TestBrowserThread(BrowserThread::UI, message_loop_.get())); | |
29 io_thread_.reset( | |
30 new content::TestBrowserThread(BrowserThread::IO, message_loop_.get())); | |
31 pref_service_.reset(new TestingPrefService()); | |
32 SSLConfigServiceManager::RegisterPrefs(pref_service_.get()); | |
33 } | |
34 | |
35 virtual void TearDown() { | |
36 pref_service_.reset(); | |
37 io_thread_.reset(); | |
38 ui_thread_.reset(); | |
39 message_loop_.reset(); | |
40 } | |
41 | 31 |
42 protected: | 32 protected: |
43 scoped_ptr<MessageLoop> message_loop_; | 33 MessageLoop message_loop_; |
44 scoped_ptr<content::TestBrowserThread> ui_thread_; | 34 content::TestBrowserThread ui_thread_; |
45 scoped_ptr<content::TestBrowserThread> io_thread_; | 35 content::TestBrowserThread io_thread_; |
46 scoped_ptr<TestingPrefService> pref_service_; | |
47 }; | 36 }; |
48 | 37 |
49 // Test that cipher suites can be disabled. "Good" refers to the fact that | 38 // Test that cipher suites can be disabled. "Good" refers to the fact that |
50 // every value is expected to be successfully parsed into a cipher suite. | 39 // every value is expected to be successfully parsed into a cipher suite. |
51 TEST_F(SSLConfigServiceManagerPrefTest, GoodDisabledCipherSuites) { | 40 TEST_F(SSLConfigServiceManagerPrefTest, GoodDisabledCipherSuites) { |
| 41 TestingPrefService pref_service; |
| 42 SSLConfigServiceManager::RegisterPrefs(&pref_service); |
| 43 |
52 scoped_ptr<SSLConfigServiceManager> config_manager( | 44 scoped_ptr<SSLConfigServiceManager> config_manager( |
53 SSLConfigServiceManager::CreateDefaultManager(pref_service_.get())); | 45 SSLConfigServiceManager::CreateDefaultManager(&pref_service)); |
54 ASSERT_TRUE(config_manager.get()); | 46 ASSERT_TRUE(config_manager.get()); |
55 scoped_refptr<SSLConfigService> config_service(config_manager->Get()); | 47 scoped_refptr<SSLConfigService> config_service(config_manager->Get()); |
56 ASSERT_TRUE(config_service.get()); | 48 ASSERT_TRUE(config_service.get()); |
57 | 49 |
58 SSLConfig old_config; | 50 SSLConfig old_config; |
59 config_service->GetSSLConfig(&old_config); | 51 config_service->GetSSLConfig(&old_config); |
60 EXPECT_TRUE(old_config.disabled_cipher_suites.empty()); | 52 EXPECT_TRUE(old_config.disabled_cipher_suites.empty()); |
61 | 53 |
62 ListValue* list_value = new ListValue(); | 54 ListValue* list_value = new ListValue(); |
63 list_value->Append(Value::CreateStringValue("0x0004")); | 55 list_value->Append(Value::CreateStringValue("0x0004")); |
64 list_value->Append(Value::CreateStringValue("0x0005")); | 56 list_value->Append(Value::CreateStringValue("0x0005")); |
65 pref_service_->SetUserPref(prefs::kCipherSuiteBlacklist, list_value); | 57 pref_service.SetUserPref(prefs::kCipherSuiteBlacklist, list_value); |
66 | 58 |
67 // Pump the message loop to notify the SSLConfigServiceManagerPref that the | 59 // Pump the message loop to notify the SSLConfigServiceManagerPref that the |
68 // preferences changed. | 60 // preferences changed. |
69 message_loop_->RunAllPending(); | 61 message_loop_.RunAllPending(); |
70 | 62 |
71 SSLConfig config; | 63 SSLConfig config; |
72 config_service->GetSSLConfig(&config); | 64 config_service->GetSSLConfig(&config); |
73 | 65 |
74 EXPECT_NE(old_config.disabled_cipher_suites, config.disabled_cipher_suites); | 66 EXPECT_NE(old_config.disabled_cipher_suites, config.disabled_cipher_suites); |
75 ASSERT_EQ(2u, config.disabled_cipher_suites.size()); | 67 ASSERT_EQ(2u, config.disabled_cipher_suites.size()); |
76 EXPECT_EQ(0x0004, config.disabled_cipher_suites[0]); | 68 EXPECT_EQ(0x0004, config.disabled_cipher_suites[0]); |
77 EXPECT_EQ(0x0005, config.disabled_cipher_suites[1]); | 69 EXPECT_EQ(0x0005, config.disabled_cipher_suites[1]); |
78 } | 70 } |
79 | 71 |
80 // Test that cipher suites can be disabled. "Bad" refers to the fact that | 72 // Test that cipher suites can be disabled. "Bad" refers to the fact that |
81 // there are one or more non-cipher suite strings in the preference. They | 73 // there are one or more non-cipher suite strings in the preference. They |
82 // should be ignored. | 74 // should be ignored. |
83 TEST_F(SSLConfigServiceManagerPrefTest, BadDisabledCipherSuites) { | 75 TEST_F(SSLConfigServiceManagerPrefTest, BadDisabledCipherSuites) { |
| 76 TestingPrefService pref_service; |
| 77 SSLConfigServiceManager::RegisterPrefs(&pref_service); |
| 78 |
84 scoped_ptr<SSLConfigServiceManager> config_manager( | 79 scoped_ptr<SSLConfigServiceManager> config_manager( |
85 SSLConfigServiceManager::CreateDefaultManager(pref_service_.get())); | 80 SSLConfigServiceManager::CreateDefaultManager(&pref_service)); |
86 ASSERT_TRUE(config_manager.get()); | 81 ASSERT_TRUE(config_manager.get()); |
87 scoped_refptr<SSLConfigService> config_service(config_manager->Get()); | 82 scoped_refptr<SSLConfigService> config_service(config_manager->Get()); |
88 ASSERT_TRUE(config_service.get()); | 83 ASSERT_TRUE(config_service.get()); |
89 | 84 |
90 SSLConfig old_config; | 85 SSLConfig old_config; |
91 config_service->GetSSLConfig(&old_config); | 86 config_service->GetSSLConfig(&old_config); |
92 EXPECT_TRUE(old_config.disabled_cipher_suites.empty()); | 87 EXPECT_TRUE(old_config.disabled_cipher_suites.empty()); |
93 | 88 |
94 ListValue* list_value = new ListValue(); | 89 ListValue* list_value = new ListValue(); |
95 list_value->Append(Value::CreateStringValue("0x0004")); | 90 list_value->Append(Value::CreateStringValue("0x0004")); |
96 list_value->Append(Value::CreateStringValue("TLS_NOT_WITH_A_CIPHER_SUITE")); | 91 list_value->Append(Value::CreateStringValue("TLS_NOT_WITH_A_CIPHER_SUITE")); |
97 list_value->Append(Value::CreateStringValue("0x0005")); | 92 list_value->Append(Value::CreateStringValue("0x0005")); |
98 list_value->Append(Value::CreateStringValue("0xBEEFY")); | 93 list_value->Append(Value::CreateStringValue("0xBEEFY")); |
99 pref_service_->SetUserPref(prefs::kCipherSuiteBlacklist, list_value); | 94 pref_service.SetUserPref(prefs::kCipherSuiteBlacklist, list_value); |
100 | 95 |
101 // Pump the message loop to notify the SSLConfigServiceManagerPref that the | 96 // Pump the message loop to notify the SSLConfigServiceManagerPref that the |
102 // preferences changed. | 97 // preferences changed. |
103 message_loop_->RunAllPending(); | 98 message_loop_.RunAllPending(); |
104 | 99 |
105 SSLConfig config; | 100 SSLConfig config; |
106 config_service->GetSSLConfig(&config); | 101 config_service->GetSSLConfig(&config); |
107 | 102 |
108 EXPECT_NE(old_config.disabled_cipher_suites, config.disabled_cipher_suites); | 103 EXPECT_NE(old_config.disabled_cipher_suites, config.disabled_cipher_suites); |
109 ASSERT_EQ(2u, config.disabled_cipher_suites.size()); | 104 ASSERT_EQ(2u, config.disabled_cipher_suites.size()); |
110 EXPECT_EQ(0x0004, config.disabled_cipher_suites[0]); | 105 EXPECT_EQ(0x0004, config.disabled_cipher_suites[0]); |
111 EXPECT_EQ(0x0005, config.disabled_cipher_suites[1]); | 106 EXPECT_EQ(0x0005, config.disabled_cipher_suites[1]); |
112 } | 107 } |
| 108 |
| 109 // Test that existing user settings for TLS1.0/SSL3.0 are both ignored and |
| 110 // cleared from user preferences. |
| 111 TEST_F(SSLConfigServiceManagerPrefTest, IgnoreLegacySSLSettings) { |
| 112 scoped_refptr<TestingPrefStore> user_prefs(new TestingPrefStore()); |
| 113 |
| 114 // SSL3.0 and TLS1.0 used to be user-definable prefs. They are now used as |
| 115 // command-line options. Ensure any existing user prefs are ignored in |
| 116 // favour of the command-line flags. |
| 117 user_prefs->SetBoolean(prefs::kSSL3Enabled, false); |
| 118 user_prefs->SetBoolean(prefs::kTLS1Enabled, false); |
| 119 |
| 120 // Ensure the preferences exist initially. |
| 121 bool is_ssl3_enabled = true; |
| 122 EXPECT_TRUE(user_prefs->GetBoolean(prefs::kSSL3Enabled, &is_ssl3_enabled)); |
| 123 EXPECT_FALSE(is_ssl3_enabled); |
| 124 |
| 125 bool is_tls1_enabled = true; |
| 126 EXPECT_TRUE(user_prefs->GetBoolean(prefs::kTLS1Enabled, &is_tls1_enabled)); |
| 127 EXPECT_FALSE(is_tls1_enabled); |
| 128 |
| 129 PrefServiceMockBuilder builder; |
| 130 builder.WithUserPrefs(user_prefs.get()); |
| 131 scoped_ptr<PrefService> pref_service(builder.Create()); |
| 132 |
| 133 SSLConfigServiceManager::RegisterPrefs(pref_service.get()); |
| 134 |
| 135 scoped_ptr<SSLConfigServiceManager> config_manager( |
| 136 SSLConfigServiceManager::CreateDefaultManager(pref_service.get())); |
| 137 ASSERT_TRUE(config_manager.get()); |
| 138 scoped_refptr<SSLConfigService> config_service(config_manager->Get()); |
| 139 ASSERT_TRUE(config_service.get()); |
| 140 |
| 141 SSLConfig ssl_config; |
| 142 config_service->GetSSLConfig(&ssl_config); |
| 143 // The default value in the absence of command-line options is that both |
| 144 // protocols are enabled. |
| 145 EXPECT_TRUE(ssl_config.ssl3_enabled); |
| 146 EXPECT_TRUE(ssl_config.tls1_enabled); |
| 147 |
| 148 // The existing user settings should be removed from the pref_service. |
| 149 EXPECT_FALSE(pref_service->HasPrefPath(prefs::kSSL3Enabled)); |
| 150 EXPECT_FALSE(pref_service->HasPrefPath(prefs::kTLS1Enabled)); |
| 151 |
| 152 // Explicitly double-check the settings are not in the user preference |
| 153 // store. |
| 154 EXPECT_FALSE(user_prefs->GetBoolean(prefs::kSSL3Enabled, &is_ssl3_enabled)); |
| 155 EXPECT_FALSE(user_prefs->GetBoolean(prefs::kTLS1Enabled, &is_tls1_enabled)); |
| 156 } |
| 157 |
| 158 // Test that command-line settings for TLS1.0/SSL3.0 are respected, that they |
| 159 // disregard any existing user preferences, and that they do not persist to |
| 160 // the user preferences files. |
| 161 TEST_F(SSLConfigServiceManagerPrefTest, CommandLineOverridesUserPrefs) { |
| 162 scoped_refptr<TestingPrefStore> user_prefs(new TestingPrefStore()); |
| 163 |
| 164 // Explicitly enable SSL3.0/TLS1.0 in the user preferences, to mirror the |
| 165 // more common legacy file. |
| 166 user_prefs->SetBoolean(prefs::kSSL3Enabled, true); |
| 167 user_prefs->SetBoolean(prefs::kTLS1Enabled, true); |
| 168 |
| 169 // Ensure the preferences exist initially. |
| 170 bool is_ssl3_enabled = false; |
| 171 EXPECT_TRUE(user_prefs->GetBoolean(prefs::kSSL3Enabled, &is_ssl3_enabled)); |
| 172 EXPECT_TRUE(is_ssl3_enabled); |
| 173 |
| 174 bool is_tls1_enabled = false; |
| 175 EXPECT_TRUE(user_prefs->GetBoolean(prefs::kTLS1Enabled, &is_tls1_enabled)); |
| 176 EXPECT_TRUE(is_tls1_enabled); |
| 177 |
| 178 CommandLine command_line(CommandLine::NO_PROGRAM); |
| 179 command_line.AppendSwitch(switches::kDisableSSL3); |
| 180 command_line.AppendSwitch(switches::kDisableTLS1); |
| 181 |
| 182 PrefServiceMockBuilder builder; |
| 183 builder.WithUserPrefs(user_prefs.get()); |
| 184 builder.WithCommandLine(&command_line); |
| 185 scoped_ptr<PrefService> pref_service(builder.Create()); |
| 186 |
| 187 SSLConfigServiceManager::RegisterPrefs(pref_service.get()); |
| 188 |
| 189 scoped_ptr<SSLConfigServiceManager> config_manager( |
| 190 SSLConfigServiceManager::CreateDefaultManager(pref_service.get())); |
| 191 ASSERT_TRUE(config_manager.get()); |
| 192 scoped_refptr<SSLConfigService> config_service(config_manager->Get()); |
| 193 ASSERT_TRUE(config_service.get()); |
| 194 |
| 195 SSLConfig ssl_config; |
| 196 config_service->GetSSLConfig(&ssl_config); |
| 197 // Command-line flags to disable should override the user preferences to |
| 198 // enable. |
| 199 EXPECT_FALSE(ssl_config.ssl3_enabled); |
| 200 EXPECT_FALSE(ssl_config.tls1_enabled); |
| 201 |
| 202 // Explicitly double-check the settings are not in the user preference |
| 203 // store. |
| 204 const PrefService::Preference* ssl3_enabled_pref = |
| 205 pref_service->FindPreference(prefs::kSSL3Enabled); |
| 206 EXPECT_FALSE(ssl3_enabled_pref->IsUserModifiable()); |
| 207 |
| 208 const PrefService::Preference* tls1_enabled_pref = |
| 209 pref_service->FindPreference(prefs::kTLS1Enabled); |
| 210 EXPECT_FALSE(tls1_enabled_pref->IsUserModifiable()); |
| 211 |
| 212 EXPECT_FALSE(user_prefs->GetBoolean(prefs::kSSL3Enabled, &is_ssl3_enabled)); |
| 213 EXPECT_FALSE(user_prefs->GetBoolean(prefs::kTLS1Enabled, &is_tls1_enabled)); |
| 214 } |
OLD | NEW |