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