Chromium Code Reviews| 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/cert_trust_anchor_provider.h" | |
| 23 #include "net/base/test_data_directory.h" | |
| 24 #include "net/base/x509_certificate.h" | |
| 16 #include "policy/policy_constants.h" | 25 #include "policy/policy_constants.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 28 |
| 20 using testing::AtLeast; | 29 using testing::AnyNumber; |
| 21 using testing::Mock; | 30 using testing::Mock; |
| 22 using testing::Ne; | 31 using testing::Ne; |
| 23 using testing::Return; | 32 using testing::Return; |
| 24 using testing::_; | 33 using testing::_; |
| 25 | 34 |
| 26 namespace policy { | 35 namespace policy { |
| 27 | 36 |
| 28 static const char kFakeONC[] = "{ \"GUID\": \"1234\" }"; | 37 namespace { |
| 38 | |
| 39 const char kFakeONC[] = "{ \"GUID\": \"1234\" }"; | |
| 40 | |
| 41 ACTION_P(SetCertificateList, list) { | |
| 42 *arg3 = list; | |
| 43 return true; | |
| 44 } | |
| 45 | |
| 46 } // namespace | |
| 29 | 47 |
| 30 class NetworkConfigurationUpdaterTest | 48 class NetworkConfigurationUpdaterTest |
| 31 : public testing::TestWithParam<const char*>{ | 49 : public testing::TestWithParam<const char*>{ |
| 32 protected: | 50 protected: |
| 51 NetworkConfigurationUpdaterTest() | |
| 52 : ui_thread_(content::BrowserThread::UI, &loop_), | |
| 53 io_thread_(content::BrowserThread::IO, &loop_) {} | |
| 54 | |
| 33 virtual void SetUp() OVERRIDE { | 55 virtual void SetUp() OVERRIDE { |
| 34 EXPECT_CALL(provider_, IsInitializationComplete(_)) | 56 EXPECT_CALL(provider_, IsInitializationComplete(_)) |
| 35 .WillRepeatedly(Return(true)); | 57 .WillRepeatedly(Return(true)); |
| 36 provider_.Init(); | 58 provider_.Init(); |
| 37 PolicyServiceImpl::Providers providers; | 59 PolicyServiceImpl::Providers providers; |
| 38 providers.push_back(&provider_); | 60 providers.push_back(&provider_); |
| 39 policy_service_.reset(new PolicyServiceImpl(providers)); | 61 policy_service_.reset(new PolicyServiceImpl(providers)); |
| 62 | |
| 63 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 64 command_line->AppendSwitch(switches::kEnableWebTrustCerts); | |
| 40 } | 65 } |
| 41 | 66 |
| 42 virtual void TearDown() OVERRIDE { | 67 virtual void TearDown() OVERRIDE { |
| 43 provider_.Shutdown(); | 68 provider_.Shutdown(); |
| 69 content::RunAllPendingInMessageLoop(content::BrowserThread::IO); | |
| 44 } | 70 } |
| 45 | 71 |
| 46 void UpdateProviderPolicy(const PolicyMap& policy) { | 72 void UpdateProviderPolicy(const PolicyMap& policy) { |
| 47 provider_.UpdateChromePolicy(policy); | 73 provider_.UpdateChromePolicy(policy); |
| 48 base::RunLoop loop; | 74 base::RunLoop loop; |
| 49 loop.RunUntilIdle(); | 75 loop.RunUntilIdle(); |
| 50 } | 76 } |
| 51 | 77 |
| 52 // Maps configuration policy name to corresponding ONC source. | 78 // Maps configuration policy name to corresponding ONC source. |
| 53 static chromeos::onc::ONCSource NameToONCSource( | 79 static chromeos::onc::ONCSource NameToONCSource( |
| 54 const std::string& name) { | 80 const std::string& name) { |
| 55 if (name == key::kDeviceOpenNetworkConfiguration) | 81 if (name == key::kDeviceOpenNetworkConfiguration) |
| 56 return chromeos::onc::ONC_SOURCE_DEVICE_POLICY; | 82 return chromeos::onc::ONC_SOURCE_DEVICE_POLICY; |
| 57 if (name == key::kOpenNetworkConfiguration) | 83 if (name == key::kOpenNetworkConfiguration) |
| 58 return chromeos::onc::ONC_SOURCE_USER_POLICY; | 84 return chromeos::onc::ONC_SOURCE_USER_POLICY; |
| 59 return chromeos::onc::ONC_SOURCE_NONE; | 85 return chromeos::onc::ONC_SOURCE_NONE; |
| 60 } | 86 } |
| 61 | 87 |
| 62 chromeos::MockNetworkLibrary network_library_; | 88 chromeos::MockNetworkLibrary network_library_; |
| 63 MockConfigurationPolicyProvider provider_; | 89 MockConfigurationPolicyProvider provider_; |
| 64 scoped_ptr<PolicyServiceImpl> policy_service_; | 90 scoped_ptr<PolicyServiceImpl> policy_service_; |
| 65 MessageLoop loop_; | 91 MessageLoop loop_; |
| 92 content::TestBrowserThread ui_thread_; | |
| 93 content::TestBrowserThread io_thread_; | |
| 66 }; | 94 }; |
| 67 | 95 |
| 68 TEST_P(NetworkConfigurationUpdaterTest, InitialUpdates) { | 96 TEST_P(NetworkConfigurationUpdaterTest, InitialUpdates) { |
| 69 PolicyMap policy; | 97 PolicyMap policy; |
| 70 policy.Set(GetParam(), POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 98 policy.Set(GetParam(), POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 71 Value::CreateStringValue(kFakeONC)); | 99 Value::CreateStringValue(kFakeONC)); |
| 72 UpdateProviderPolicy(policy); | 100 UpdateProviderPolicy(policy); |
| 73 | 101 |
| 74 EXPECT_CALL(network_library_, AddNetworkProfileObserver(_)); | 102 EXPECT_CALL(network_library_, AddNetworkProfileObserver(_)); |
| 75 | 103 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 99 | 127 |
| 100 updater.OnUserPolicyInitialized(); | 128 updater.OnUserPolicyInitialized(); |
| 101 } | 129 } |
| 102 Mock::VerifyAndClearExpectations(&network_library_); | 130 Mock::VerifyAndClearExpectations(&network_library_); |
| 103 } | 131 } |
| 104 | 132 |
| 105 TEST_P(NetworkConfigurationUpdaterTest, AllowWebTrust) { | 133 TEST_P(NetworkConfigurationUpdaterTest, AllowWebTrust) { |
| 106 { | 134 { |
| 107 EXPECT_CALL(network_library_, AddNetworkProfileObserver(_)); | 135 EXPECT_CALL(network_library_, AddNetworkProfileObserver(_)); |
| 108 | 136 |
| 109 // Initially web trust is disabled. | 137 const net::CertificateList empty_cert_list; |
| 110 EXPECT_CALL(network_library_, LoadOncNetworks(_, _, _, false)) | 138 |
| 111 .Times(AtLeast(0)); | 139 base::FilePath cert_path = |
| 140 net::GetTestCertsDirectory().AppendASCII("ok_cert.pem"); | |
| 141 std::string cert_data; | |
| 142 ASSERT_TRUE(file_util::ReadFileToString(cert_path, &cert_data)); | |
|
pneubeck (no reviews)
2013/03/26 10:01:25
is there a way to create fake certs?
how about tha
Joao da Silva
2013/03/31 19:22:14
These certificate files are used in several unit t
| |
| 143 net::CertificateList cert_list = | |
| 144 net::X509Certificate::CreateCertificateListFromBytes( | |
| 145 cert_data.data(), | |
| 146 cert_data.size(), | |
| 147 net::X509Certificate::FORMAT_AUTO); | |
| 148 ASSERT_EQ(1u, cert_list.size()); | |
|
Ryan Sleevi
2013/03/25 21:09:53
Use https://code.google.com/p/chromium/codesearch#
Joao da Silva
2013/03/31 19:22:14
Thanks for the pointer, done.
| |
| 149 | |
| 150 EXPECT_CALL(network_library_, LoadOncNetworks(_, _, _, _)) | |
| 151 .WillRepeatedly(SetCertificateList(empty_cert_list)); | |
| 112 NetworkConfigurationUpdater updater(policy_service_.get(), | 152 NetworkConfigurationUpdater updater(policy_service_.get(), |
| 113 &network_library_); | 153 &network_library_); |
| 154 net::CertTrustAnchorProvider* trust_provider = | |
| 155 updater.GetCertTrustAnchorProvider(); | |
| 156 ASSERT_TRUE(trust_provider); | |
| 157 // The initial list of trust anchors is empty. | |
| 158 content::RunAllPendingInMessageLoop(content::BrowserThread::IO); | |
| 159 EXPECT_TRUE(trust_provider->GetAdditionalTrustAnchors().empty()); | |
| 160 | |
| 161 // Initially web trust is disabled. | |
|
Ryan Sleevi
2013/03/25 21:09:53
nit: same comments re: "web trust"
Joao da Silva
2013/03/31 19:22:14
Done.
| |
| 114 updater.OnUserPolicyInitialized(); | 162 updater.OnUserPolicyInitialized(); |
| 163 content::RunAllPendingInMessageLoop(content::BrowserThread::IO); | |
| 164 Mock::VerifyAndClearExpectations(&network_library_); | |
| 165 EXPECT_TRUE(trust_provider->GetAdditionalTrustAnchors().empty()); | |
| 166 | |
| 167 // Certificates with web trust should be forwarded to the trust provider. | |
| 168 EXPECT_CALL(network_library_, LoadOncNetworks(_, _, _, _)) | |
| 169 .WillRepeatedly(SetCertificateList(cert_list)); | |
| 170 updater.set_allow_web_trust(true); | |
| 171 updater.OnUserPolicyInitialized(); | |
|
pneubeck (no reviews)
2013/03/26 10:01:25
this call shouldn't be used a second time.
the imp
Joao da Silva
2013/03/31 19:22:14
Using the old trigger.
| |
| 172 content::RunAllPendingInMessageLoop(content::BrowserThread::IO); | |
| 115 Mock::VerifyAndClearExpectations(&network_library_); | 173 Mock::VerifyAndClearExpectations(&network_library_); |
| 116 | 174 |
| 117 // Web trust should be forwarded to LoadOncNetworks. | 175 // Certificates are only provided as trust anchors if they come from user |
| 118 EXPECT_CALL(network_library_, LoadOncNetworks(_, _, _, true)) | 176 // policy. |
| 119 .Times(AtLeast(0)); | 177 size_t expected_certs = 0u; |
| 120 | 178 if (GetParam() == key::kOpenNetworkConfiguration) |
| 121 updater.set_allow_web_trust(true); | 179 expected_certs = 1u; |
| 122 | 180 EXPECT_EQ(expected_certs, |
| 123 PolicyMap policy; | 181 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 | 182 |
| 129 EXPECT_CALL(network_library_, RemoveNetworkProfileObserver(_)); | 183 EXPECT_CALL(network_library_, RemoveNetworkProfileObserver(_)); |
| 130 } | 184 } |
| 131 Mock::VerifyAndClearExpectations(&network_library_); | 185 Mock::VerifyAndClearExpectations(&network_library_); |
| 132 } | 186 } |
| 133 | 187 |
| 134 TEST_P(NetworkConfigurationUpdaterTest, PolicyChange) { | 188 TEST_P(NetworkConfigurationUpdaterTest, PolicyChange) { |
| 135 { | 189 { |
| 136 EXPECT_CALL(network_library_, AddNetworkProfileObserver(_)); | 190 EXPECT_CALL(network_library_, AddNetworkProfileObserver(_)); |
| 137 | 191 |
| 138 // Ignore the initial updates. | 192 // Ignore the initial updates. |
| 139 EXPECT_CALL(network_library_, LoadOncNetworks(_, _, _, _)) | 193 EXPECT_CALL(network_library_, LoadOncNetworks(_, _, _, _)) |
| 140 .Times(AtLeast(0)); | 194 .Times(AnyNumber()); |
| 141 NetworkConfigurationUpdater updater(policy_service_.get(), | 195 NetworkConfigurationUpdater updater(policy_service_.get(), |
| 142 &network_library_); | 196 &network_library_); |
| 143 updater.OnUserPolicyInitialized(); | 197 updater.OnUserPolicyInitialized(); |
| 144 Mock::VerifyAndClearExpectations(&network_library_); | 198 Mock::VerifyAndClearExpectations(&network_library_); |
| 145 | 199 |
| 146 // We should update if policy changes. | 200 // We should update if policy changes. |
| 147 EXPECT_CALL(network_library_, LoadOncNetworks( | 201 EXPECT_CALL(network_library_, LoadOncNetworks( |
| 148 kFakeONC, "", NameToONCSource(GetParam()), _)); | 202 kFakeONC, "", NameToONCSource(GetParam()), _)); |
| 149 | 203 |
| 150 // In the current implementation, we always apply both policies. | 204 // In the current implementation, we always apply both policies. |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 178 Mock::VerifyAndClearExpectations(&network_library_); | 232 Mock::VerifyAndClearExpectations(&network_library_); |
| 179 } | 233 } |
| 180 | 234 |
| 181 INSTANTIATE_TEST_CASE_P( | 235 INSTANTIATE_TEST_CASE_P( |
| 182 NetworkConfigurationUpdaterTestInstance, | 236 NetworkConfigurationUpdaterTestInstance, |
| 183 NetworkConfigurationUpdaterTest, | 237 NetworkConfigurationUpdaterTest, |
| 184 testing::Values(key::kDeviceOpenNetworkConfiguration, | 238 testing::Values(key::kDeviceOpenNetworkConfiguration, |
| 185 key::kOpenNetworkConfiguration)); | 239 key::kOpenNetworkConfiguration)); |
| 186 | 240 |
| 187 } // namespace policy | 241 } // namespace policy |
| OLD | NEW |