| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/chromeos/policy/network_configuration_updater.h" | 5 #include "chrome/browser/chromeos/policy/network_configuration_updater.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/file_util.h" |
| 9 #include "base/files/file_path.h" |
| 7 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 9 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 10 #include "chrome/browser/chromeos/cros/mock_network_library.h" | 13 #include "chrome/browser/chromeos/cros/mock_network_library.h" |
| 11 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 14 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 12 #include "chrome/browser/policy/policy_map.h" | 15 #include "chrome/browser/policy/policy_map.h" |
| 13 #include "chrome/browser/policy/policy_service_impl.h" | 16 #include "chrome/browser/policy/policy_service_impl.h" |
| 17 #include "chrome/common/chrome_switches.h" |
| 14 #include "chromeos/network/onc/onc_constants.h" | 18 #include "chromeos/network/onc/onc_constants.h" |
| 15 #include "chromeos/network/onc/onc_utils.h" | 19 #include "chromeos/network/onc/onc_utils.h" |
| 20 #include "content/public/test/test_browser_thread.h" |
| 21 #include "content/public/test/test_utils.h" |
| 22 #include "net/base/test_data_directory.h" |
| 23 #include "net/cert/cert_trust_anchor_provider.h" |
| 24 #include "net/cert/x509_certificate.h" |
| 25 #include "net/test/cert_test_util.h" |
| 16 #include "policy/policy_constants.h" | 26 #include "policy/policy_constants.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 29 |
| 20 using testing::AtLeast; | 30 using testing::AnyNumber; |
| 21 using testing::Mock; | 31 using testing::Mock; |
| 22 using testing::Ne; | 32 using testing::Ne; |
| 23 using testing::Return; | 33 using testing::Return; |
| 24 using testing::_; | 34 using testing::_; |
| 25 | 35 |
| 26 namespace policy { | 36 namespace policy { |
| 27 | 37 |
| 28 static const char kFakeONC[] = "{ \"GUID\": \"1234\" }"; | 38 namespace { |
| 39 |
| 40 const char kFakeONC[] = "{ \"GUID\": \"1234\" }"; |
| 41 |
| 42 ACTION_P(SetCertificateList, list) { |
| 43 *arg3 = list; |
| 44 return true; |
| 45 } |
| 46 |
| 47 } // namespace |
| 29 | 48 |
| 30 class NetworkConfigurationUpdaterTest | 49 class NetworkConfigurationUpdaterTest |
| 31 : public testing::TestWithParam<const char*>{ | 50 : public testing::TestWithParam<const char*>{ |
| 32 protected: | 51 protected: |
| 52 NetworkConfigurationUpdaterTest() |
| 53 : ui_thread_(content::BrowserThread::UI, &loop_), |
| 54 io_thread_(content::BrowserThread::IO, &loop_) {} |
| 55 |
| 33 virtual void SetUp() OVERRIDE { | 56 virtual void SetUp() OVERRIDE { |
| 34 EXPECT_CALL(provider_, IsInitializationComplete(_)) | 57 EXPECT_CALL(provider_, IsInitializationComplete(_)) |
| 35 .WillRepeatedly(Return(true)); | 58 .WillRepeatedly(Return(true)); |
| 36 provider_.Init(); | 59 provider_.Init(); |
| 37 PolicyServiceImpl::Providers providers; | 60 PolicyServiceImpl::Providers providers; |
| 38 providers.push_back(&provider_); | 61 providers.push_back(&provider_); |
| 39 policy_service_.reset(new PolicyServiceImpl(providers)); | 62 policy_service_.reset(new PolicyServiceImpl(providers)); |
| 63 |
| 64 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 65 command_line->AppendSwitch(switches::kEnableWebTrustCerts); |
| 40 } | 66 } |
| 41 | 67 |
| 42 virtual void TearDown() OVERRIDE { | 68 virtual void TearDown() OVERRIDE { |
| 43 provider_.Shutdown(); | 69 provider_.Shutdown(); |
| 70 content::RunAllPendingInMessageLoop(content::BrowserThread::IO); |
| 44 } | 71 } |
| 45 | 72 |
| 46 void UpdateProviderPolicy(const PolicyMap& policy) { | 73 void UpdateProviderPolicy(const PolicyMap& policy) { |
| 47 provider_.UpdateChromePolicy(policy); | 74 provider_.UpdateChromePolicy(policy); |
| 48 base::RunLoop loop; | 75 base::RunLoop loop; |
| 49 loop.RunUntilIdle(); | 76 loop.RunUntilIdle(); |
| 50 } | 77 } |
| 51 | 78 |
| 52 // Maps configuration policy name to corresponding ONC source. | 79 // Maps configuration policy name to corresponding ONC source. |
| 53 static chromeos::onc::ONCSource NameToONCSource( | 80 static chromeos::onc::ONCSource NameToONCSource( |
| 54 const std::string& name) { | 81 const std::string& name) { |
| 55 if (name == key::kDeviceOpenNetworkConfiguration) | 82 if (name == key::kDeviceOpenNetworkConfiguration) |
| 56 return chromeos::onc::ONC_SOURCE_DEVICE_POLICY; | 83 return chromeos::onc::ONC_SOURCE_DEVICE_POLICY; |
| 57 if (name == key::kOpenNetworkConfiguration) | 84 if (name == key::kOpenNetworkConfiguration) |
| 58 return chromeos::onc::ONC_SOURCE_USER_POLICY; | 85 return chromeos::onc::ONC_SOURCE_USER_POLICY; |
| 59 return chromeos::onc::ONC_SOURCE_NONE; | 86 return chromeos::onc::ONC_SOURCE_NONE; |
| 60 } | 87 } |
| 61 | 88 |
| 62 chromeos::MockNetworkLibrary network_library_; | 89 chromeos::MockNetworkLibrary network_library_; |
| 63 MockConfigurationPolicyProvider provider_; | 90 MockConfigurationPolicyProvider provider_; |
| 64 scoped_ptr<PolicyServiceImpl> policy_service_; | 91 scoped_ptr<PolicyServiceImpl> policy_service_; |
| 65 MessageLoop loop_; | 92 MessageLoop loop_; |
| 93 content::TestBrowserThread ui_thread_; |
| 94 content::TestBrowserThread io_thread_; |
| 66 }; | 95 }; |
| 67 | 96 |
| 68 TEST_P(NetworkConfigurationUpdaterTest, InitialUpdates) { | 97 TEST_P(NetworkConfigurationUpdaterTest, InitialUpdates) { |
| 69 PolicyMap policy; | 98 PolicyMap policy; |
| 70 policy.Set(GetParam(), POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 99 policy.Set(GetParam(), POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 71 Value::CreateStringValue(kFakeONC)); | 100 Value::CreateStringValue(kFakeONC)); |
| 72 UpdateProviderPolicy(policy); | 101 UpdateProviderPolicy(policy); |
| 73 | 102 |
| 74 EXPECT_CALL(network_library_, AddNetworkProfileObserver(_)); | 103 EXPECT_CALL(network_library_, AddNetworkProfileObserver(_)); |
| 75 | 104 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 95 Ne(NameToONCSource(GetParam())), | 124 Ne(NameToONCSource(GetParam())), |
| 96 _)); | 125 _)); |
| 97 | 126 |
| 98 EXPECT_CALL(network_library_, RemoveNetworkProfileObserver(_)); | 127 EXPECT_CALL(network_library_, RemoveNetworkProfileObserver(_)); |
| 99 | 128 |
| 100 updater.OnUserPolicyInitialized(); | 129 updater.OnUserPolicyInitialized(); |
| 101 } | 130 } |
| 102 Mock::VerifyAndClearExpectations(&network_library_); | 131 Mock::VerifyAndClearExpectations(&network_library_); |
| 103 } | 132 } |
| 104 | 133 |
| 105 TEST_P(NetworkConfigurationUpdaterTest, AllowWebTrust) { | 134 TEST_P(NetworkConfigurationUpdaterTest, AllowTrustedCertificatesFromPolicy) { |
| 106 { | 135 { |
| 107 EXPECT_CALL(network_library_, AddNetworkProfileObserver(_)); | 136 EXPECT_CALL(network_library_, AddNetworkProfileObserver(_)); |
| 108 | 137 |
| 109 // Initially web trust is disabled. | 138 const net::CertificateList empty_cert_list; |
| 110 EXPECT_CALL(network_library_, LoadOncNetworks(_, _, _, false)) | 139 |
| 111 .Times(AtLeast(0)); | 140 const net::CertificateList cert_list = |
| 141 net::CreateCertificateListFromFile(net::GetTestCertsDirectory(), |
| 142 "ok_cert.pem", |
| 143 net::X509Certificate::FORMAT_AUTO); |
| 144 ASSERT_EQ(1u, cert_list.size()); |
| 145 |
| 146 EXPECT_CALL(network_library_, LoadOncNetworks(_, _, _, _)) |
| 147 .WillRepeatedly(SetCertificateList(empty_cert_list)); |
| 112 NetworkConfigurationUpdater updater(policy_service_.get(), | 148 NetworkConfigurationUpdater updater(policy_service_.get(), |
| 113 &network_library_); | 149 &network_library_); |
| 150 net::CertTrustAnchorProvider* trust_provider = |
| 151 updater.GetCertTrustAnchorProvider(); |
| 152 ASSERT_TRUE(trust_provider); |
| 153 // The initial list of trust anchors is empty. |
| 154 content::RunAllPendingInMessageLoop(content::BrowserThread::IO); |
| 155 EXPECT_TRUE(trust_provider->GetAdditionalTrustAnchors().empty()); |
| 156 |
| 157 // Initially, certificates imported from policy don't have trust flags. |
| 114 updater.OnUserPolicyInitialized(); | 158 updater.OnUserPolicyInitialized(); |
| 159 content::RunAllPendingInMessageLoop(content::BrowserThread::IO); |
| 160 Mock::VerifyAndClearExpectations(&network_library_); |
| 161 EXPECT_TRUE(trust_provider->GetAdditionalTrustAnchors().empty()); |
| 162 |
| 163 // Certificates with the "Web" trust flag set should be forwarded to the |
| 164 // trust provider. |
| 165 EXPECT_CALL(network_library_, LoadOncNetworks(_, _, _, _)) |
| 166 .WillRepeatedly(SetCertificateList(empty_cert_list)); |
| 167 chromeos::onc::ONCSource current_source = NameToONCSource(GetParam()); |
| 168 EXPECT_CALL(network_library_, LoadOncNetworks(_, _, current_source, _)) |
| 169 .WillRepeatedly(SetCertificateList(cert_list)); |
| 170 updater.set_allow_trusted_certificates_from_policy(true); |
| 171 // Trigger a policy update. |
| 172 PolicyMap policy; |
| 173 policy.Set(GetParam(), POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 174 base::Value::CreateStringValue(kFakeONC)); |
| 175 UpdateProviderPolicy(policy); |
| 115 Mock::VerifyAndClearExpectations(&network_library_); | 176 Mock::VerifyAndClearExpectations(&network_library_); |
| 116 | 177 |
| 117 // Web trust should be forwarded to LoadOncNetworks. | 178 // Certificates are only provided as trust anchors if they come from user |
| 118 EXPECT_CALL(network_library_, LoadOncNetworks(_, _, _, true)) | 179 // policy. |
| 119 .Times(AtLeast(0)); | 180 size_t expected_certs = 0u; |
| 120 | 181 if (GetParam() == key::kOpenNetworkConfiguration) |
| 121 updater.set_allow_web_trust(true); | 182 expected_certs = 1u; |
| 122 | 183 EXPECT_EQ(expected_certs, |
| 123 PolicyMap policy; | 184 trust_provider->GetAdditionalTrustAnchors().size()); |
| 124 policy.Set(GetParam(), POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
| 125 Value::CreateStringValue(kFakeONC)); | |
| 126 UpdateProviderPolicy(policy); | |
| 127 Mock::VerifyAndClearExpectations(&network_library_); | |
| 128 | 185 |
| 129 EXPECT_CALL(network_library_, RemoveNetworkProfileObserver(_)); | 186 EXPECT_CALL(network_library_, RemoveNetworkProfileObserver(_)); |
| 130 } | 187 } |
| 131 Mock::VerifyAndClearExpectations(&network_library_); | 188 Mock::VerifyAndClearExpectations(&network_library_); |
| 132 } | 189 } |
| 133 | 190 |
| 134 TEST_P(NetworkConfigurationUpdaterTest, PolicyChange) { | 191 TEST_P(NetworkConfigurationUpdaterTest, PolicyChange) { |
| 135 { | 192 { |
| 136 EXPECT_CALL(network_library_, AddNetworkProfileObserver(_)); | 193 EXPECT_CALL(network_library_, AddNetworkProfileObserver(_)); |
| 137 | 194 |
| 138 // Ignore the initial updates. | 195 // Ignore the initial updates. |
| 139 EXPECT_CALL(network_library_, LoadOncNetworks(_, _, _, _)) | 196 EXPECT_CALL(network_library_, LoadOncNetworks(_, _, _, _)) |
| 140 .Times(AtLeast(0)); | 197 .Times(AnyNumber()); |
| 141 NetworkConfigurationUpdater updater(policy_service_.get(), | 198 NetworkConfigurationUpdater updater(policy_service_.get(), |
| 142 &network_library_); | 199 &network_library_); |
| 143 updater.OnUserPolicyInitialized(); | 200 updater.OnUserPolicyInitialized(); |
| 144 Mock::VerifyAndClearExpectations(&network_library_); | 201 Mock::VerifyAndClearExpectations(&network_library_); |
| 145 | 202 |
| 146 // We should update if policy changes. | 203 // We should update if policy changes. |
| 147 EXPECT_CALL(network_library_, LoadOncNetworks( | 204 EXPECT_CALL(network_library_, LoadOncNetworks( |
| 148 kFakeONC, "", NameToONCSource(GetParam()), _)); | 205 kFakeONC, "", NameToONCSource(GetParam()), _)); |
| 149 | 206 |
| 150 // In the current implementation, we always apply both policies. | 207 // In the current implementation, we always apply both policies. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 178 Mock::VerifyAndClearExpectations(&network_library_); | 235 Mock::VerifyAndClearExpectations(&network_library_); |
| 179 } | 236 } |
| 180 | 237 |
| 181 INSTANTIATE_TEST_CASE_P( | 238 INSTANTIATE_TEST_CASE_P( |
| 182 NetworkConfigurationUpdaterTestInstance, | 239 NetworkConfigurationUpdaterTestInstance, |
| 183 NetworkConfigurationUpdaterTest, | 240 NetworkConfigurationUpdaterTest, |
| 184 testing::Values(key::kDeviceOpenNetworkConfiguration, | 241 testing::Values(key::kDeviceOpenNetworkConfiguration, |
| 185 key::kOpenNetworkConfiguration)); | 242 key::kOpenNetworkConfiguration)); |
| 186 | 243 |
| 187 } // namespace policy | 244 } // namespace policy |
| OLD | NEW |