| 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/policy/network_configuration_updater.h" | 5 #include "chrome/browser/policy/network_configuration_updater.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "chrome/browser/chromeos/cros/mock_network_library.h" | 8 #include "chrome/browser/chromeos/cros/mock_network_library.h" |
| 9 #include "chrome/browser/chromeos/login/mock_user_manager.h" |
| 10 #include "chrome/browser/policy/browser_policy_connector.h" |
| 9 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 11 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 10 #include "chrome/browser/policy/policy_map.h" | 12 #include "chrome/browser/policy/policy_map.h" |
| 11 #include "chrome/browser/policy/policy_service_impl.h" | 13 #include "chrome/browser/policy/policy_service_impl.h" |
| 12 #include "policy/policy_constants.h" | 14 #include "policy/policy_constants.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 17 |
| 16 using testing::Mock; | 18 using testing::Mock; |
| 17 using testing::Return; | 19 using testing::Return; |
| 18 using testing::_; | 20 using testing::_; |
| 19 | 21 |
| 20 namespace policy { | 22 namespace policy { |
| 21 | 23 |
| 22 static const char kFakeONC[] = "{ \"GUID\": \"1234\" }"; | 24 static const char kFakeONC[] = "{ \"GUID\": \"1234\" }"; |
| 23 | 25 |
| 24 class NetworkConfigurationUpdaterTest | 26 class NetworkConfigurationUpdaterTest |
| 25 : public testing::TestWithParam<const char*> { | 27 : public testing::TestWithParam<const char*> { |
| 26 protected: | 28 protected: |
| 27 virtual void SetUp() OVERRIDE { | 29 virtual void SetUp() OVERRIDE { |
| 28 EXPECT_CALL(network_library_, LoadOncNetworks(_, "", _, _)) | 30 EXPECT_CALL(network_library_, LoadOncNetworks(_, "", _, _, _)) |
| 29 .WillRepeatedly(Return(true)); | 31 .WillRepeatedly(Return(true)); |
| 30 EXPECT_CALL(provider_, IsInitializationComplete()) | 32 EXPECT_CALL(provider_, IsInitializationComplete()) |
| 31 .WillRepeatedly(Return(true)); | 33 .WillRepeatedly(Return(true)); |
| 32 PolicyServiceImpl::Providers providers; | 34 PolicyServiceImpl::Providers providers; |
| 33 providers.push_back(&provider_); | 35 providers.push_back(&provider_); |
| 34 policy_service_.reset(new PolicyServiceImpl(providers)); | 36 policy_service_.reset(new PolicyServiceImpl(providers)); |
| 35 } | 37 } |
| 36 | 38 |
| 37 // Maps configuration policy name to corresponding ONC source. | 39 // Maps configuration policy name to corresponding ONC source. |
| 38 static chromeos::NetworkUIData::ONCSource NameToONCSource( | 40 static chromeos::NetworkUIData::ONCSource NameToONCSource( |
| 39 const std::string& name) { | 41 const std::string& name) { |
| 40 if (name == key::kDeviceOpenNetworkConfiguration) | 42 if (name == key::kDeviceOpenNetworkConfiguration) |
| 41 return chromeos::NetworkUIData::ONC_SOURCE_DEVICE_POLICY; | 43 return chromeos::NetworkUIData::ONC_SOURCE_DEVICE_POLICY; |
| 42 if (name == key::kOpenNetworkConfiguration) | 44 if (name == key::kOpenNetworkConfiguration) |
| 43 return chromeos::NetworkUIData::ONC_SOURCE_USER_POLICY; | 45 return chromeos::NetworkUIData::ONC_SOURCE_USER_POLICY; |
| 44 return chromeos::NetworkUIData::ONC_SOURCE_NONE; | 46 return chromeos::NetworkUIData::ONC_SOURCE_NONE; |
| 45 } | 47 } |
| 46 | 48 |
| 49 BrowserPolicyConnector connector_; |
| 47 chromeos::MockNetworkLibrary network_library_; | 50 chromeos::MockNetworkLibrary network_library_; |
| 51 chromeos::MockUserManager user_manager_; |
| 48 MockConfigurationPolicyProvider provider_; | 52 MockConfigurationPolicyProvider provider_; |
| 49 scoped_ptr<PolicyServiceImpl> policy_service_; | 53 scoped_ptr<PolicyServiceImpl> policy_service_; |
| 50 }; | 54 }; |
| 51 | 55 |
| 52 TEST_P(NetworkConfigurationUpdaterTest, InitialUpdate) { | 56 TEST_P(NetworkConfigurationUpdaterTest, InitialUpdate) { |
| 53 PolicyMap policy; | 57 PolicyMap policy; |
| 54 policy.Set(GetParam(), POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 58 policy.Set(GetParam(), POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 55 Value::CreateStringValue(kFakeONC)); | 59 Value::CreateStringValue(kFakeONC)); |
| 56 provider_.UpdateChromePolicy(policy); | 60 provider_.UpdateChromePolicy(policy); |
| 57 | 61 |
| 58 EXPECT_CALL(network_library_, | 62 EXPECT_CALL(network_library_, |
| 59 LoadOncNetworks(kFakeONC, "", NameToONCSource(GetParam()), _)) | 63 LoadOncNetworks(kFakeONC, "", NameToONCSource(GetParam()), _, _)) |
| 60 .WillOnce(Return(true)); | 64 .WillOnce(Return(true)); |
| 61 | 65 |
| 62 NetworkConfigurationUpdater updater(policy_service_.get(), &network_library_); | 66 NetworkConfigurationUpdater updater(policy_service_.get(), |
| 67 &connector_, |
| 68 &network_library_, |
| 69 &user_manager_); |
| 63 Mock::VerifyAndClearExpectations(&network_library_); | 70 Mock::VerifyAndClearExpectations(&network_library_); |
| 64 } | 71 } |
| 65 | 72 |
| 66 TEST_P(NetworkConfigurationUpdaterTest, PolicyChange) { | 73 TEST_P(NetworkConfigurationUpdaterTest, PolicyChange) { |
| 67 NetworkConfigurationUpdater updater(policy_service_.get(), &network_library_); | 74 NetworkConfigurationUpdater updater(policy_service_.get(), |
| 75 &connector_, |
| 76 &network_library_, |
| 77 &user_manager_); |
| 68 | 78 |
| 69 // We should update if policy changes. | 79 // We should update if policy changes. |
| 70 EXPECT_CALL(network_library_, | 80 EXPECT_CALL(network_library_, |
| 71 LoadOncNetworks(kFakeONC, "", NameToONCSource(GetParam()), _)) | 81 LoadOncNetworks(kFakeONC, "", NameToONCSource(GetParam()), _, _)) |
| 72 .WillOnce(Return(true)); | 82 .WillOnce(Return(true)); |
| 73 PolicyMap policy; | 83 PolicyMap policy; |
| 74 policy.Set(GetParam(), POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 84 policy.Set(GetParam(), POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 75 Value::CreateStringValue(kFakeONC)); | 85 Value::CreateStringValue(kFakeONC)); |
| 76 provider_.UpdateChromePolicy(policy); | 86 provider_.UpdateChromePolicy(policy); |
| 77 Mock::VerifyAndClearExpectations(&network_library_); | 87 Mock::VerifyAndClearExpectations(&network_library_); |
| 78 | 88 |
| 79 // No update if the set the same value again. | 89 // No update if the set the same value again. |
| 80 EXPECT_CALL(network_library_, | 90 EXPECT_CALL(network_library_, |
| 81 LoadOncNetworks(kFakeONC, "", NameToONCSource(GetParam()), _)) | 91 LoadOncNetworks(kFakeONC, "", NameToONCSource(GetParam()), _, _)) |
| 82 .Times(0); | 92 .Times(0); |
| 83 provider_.UpdateChromePolicy(policy); | 93 provider_.UpdateChromePolicy(policy); |
| 84 Mock::VerifyAndClearExpectations(&network_library_); | 94 Mock::VerifyAndClearExpectations(&network_library_); |
| 85 | 95 |
| 86 // Another update is expected if the policy goes away. | 96 // Another update is expected if the policy goes away. |
| 87 EXPECT_CALL(network_library_, | 97 EXPECT_CALL(network_library_, |
| 88 LoadOncNetworks(NetworkConfigurationUpdater::kEmptyConfiguration, | 98 LoadOncNetworks(NetworkConfigurationUpdater::kEmptyConfiguration, |
| 89 "", NameToONCSource(GetParam()), _)) | 99 "", NameToONCSource(GetParam()), _, _)) |
| 90 .WillOnce(Return(true)); | 100 .WillOnce(Return(true)); |
| 91 policy.Erase(GetParam()); | 101 policy.Erase(GetParam()); |
| 92 provider_.UpdateChromePolicy(policy); | 102 provider_.UpdateChromePolicy(policy); |
| 93 Mock::VerifyAndClearExpectations(&network_library_); | 103 Mock::VerifyAndClearExpectations(&network_library_); |
| 94 } | 104 } |
| 95 | 105 |
| 96 INSTANTIATE_TEST_CASE_P( | 106 INSTANTIATE_TEST_CASE_P( |
| 97 NetworkConfigurationUpdaterTestInstance, | 107 NetworkConfigurationUpdaterTestInstance, |
| 98 NetworkConfigurationUpdaterTest, | 108 NetworkConfigurationUpdaterTest, |
| 99 testing::Values(key::kDeviceOpenNetworkConfiguration, | 109 testing::Values(key::kDeviceOpenNetworkConfiguration, |
| 100 key::kOpenNetworkConfiguration)); | 110 key::kOpenNetworkConfiguration)); |
| 101 | 111 |
| 102 } // namespace policy | 112 } // namespace policy |
| OLD | NEW |