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