| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/network_settings/onc_merger.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/values.h" | |
| 11 #include "chrome/browser/chromeos/cros/onc_constants.h" | |
| 12 #include "chrome/browser/chromeos/network_settings/onc_test_utils.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 namespace chromeos { | |
| 16 namespace onc { | |
| 17 namespace { | |
| 18 | |
| 19 // Checks that both dictionaries contain an entry at |path| with the same value. | |
| 20 ::testing::AssertionResult HaveSameValueAt(const base::DictionaryValue& a, | |
| 21 const base::DictionaryValue& b, | |
| 22 const std::string& path) { | |
| 23 const base::Value* a_value = NULL; | |
| 24 if (!a.Get(path, &a_value)) { | |
| 25 return ::testing::AssertionFailure() | |
| 26 << "First dictionary '" << a << "' doesn't contain " << path; | |
| 27 } | |
| 28 | |
| 29 const base::Value* b_value = NULL; | |
| 30 if (!b.Get(path, &b_value)) { | |
| 31 return ::testing::AssertionFailure() | |
| 32 << "Second dictionary '" << b << "' doesn't contain " << path; | |
| 33 } | |
| 34 | |
| 35 if (base::Value::Equals(a_value, b_value)) { | |
| 36 return ::testing::AssertionSuccess() | |
| 37 << "Entries at '" << path << "' are equal"; | |
| 38 } else { | |
| 39 return ::testing::AssertionFailure() | |
| 40 << "Entries at '" << path << "' not equal but are '" | |
| 41 << *a_value << "' and '" << *b_value << "'"; | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 namespace merger { | |
| 48 | |
| 49 class ONCMergerTest : public testing::Test { | |
| 50 public: | |
| 51 scoped_ptr<const base::DictionaryValue> user_; | |
| 52 scoped_ptr<const base::DictionaryValue> policy_; | |
| 53 scoped_ptr<const base::DictionaryValue> policy_without_recommended_; | |
| 54 scoped_ptr<const base::DictionaryValue> device_policy_; | |
| 55 | |
| 56 virtual void SetUp() { | |
| 57 policy_ = test_utils::ReadTestDictionary("policy.onc"); | |
| 58 policy_without_recommended_ = | |
| 59 test_utils::ReadTestDictionary("policy_without_recommended.onc"); | |
| 60 user_ = test_utils::ReadTestDictionary("user.onc"); | |
| 61 device_policy_ = test_utils::ReadTestDictionary("device_policy.onc"); | |
| 62 } | |
| 63 }; | |
| 64 | |
| 65 TEST_F(ONCMergerTest, MandatoryValueOverwritesUserValue) { | |
| 66 scoped_ptr<base::DictionaryValue> merged( | |
| 67 MergeSettingsWithPolicies(policy_.get(), NULL, user_.get(), NULL)); | |
| 68 EXPECT_TRUE(HaveSameValueAt(*merged, *policy_, "Type")); | |
| 69 EXPECT_TRUE(HaveSameValueAt(*merged, *policy_, "IPConfigs")); | |
| 70 } | |
| 71 | |
| 72 TEST_F(ONCMergerTest, MandatoryValueAndNoUserValue) { | |
| 73 scoped_ptr<base::DictionaryValue> merged( | |
| 74 MergeSettingsWithPolicies(policy_.get(), NULL, user_.get(), NULL)); | |
| 75 EXPECT_TRUE(HaveSameValueAt(*merged, *policy_, "GUID")); | |
| 76 EXPECT_TRUE(HaveSameValueAt(*merged, *policy_, "VPN.OpenVPN.Username")); | |
| 77 } | |
| 78 | |
| 79 TEST_F(ONCMergerTest, MandatoryDictionaryAndNoUserValue) { | |
| 80 scoped_ptr<base::DictionaryValue> merged( | |
| 81 MergeSettingsWithPolicies(policy_.get(), NULL, user_.get(), NULL)); | |
| 82 EXPECT_TRUE(HaveSameValueAt(*merged, *policy_without_recommended_, | |
| 83 "VPN.OpenVPN.ClientCertPattern")); | |
| 84 } | |
| 85 | |
| 86 TEST_F(ONCMergerTest, UserValueOverwritesRecommendedValue) { | |
| 87 scoped_ptr<base::DictionaryValue> merged( | |
| 88 MergeSettingsWithPolicies(policy_.get(), NULL, user_.get(), NULL)); | |
| 89 EXPECT_TRUE(HaveSameValueAt(*merged, *user_, "VPN.Host")); | |
| 90 } | |
| 91 | |
| 92 TEST_F(ONCMergerTest, UserValueAndRecommendedUnset) { | |
| 93 scoped_ptr<base::DictionaryValue> merged( | |
| 94 MergeSettingsWithPolicies(policy_.get(), NULL, user_.get(), NULL)); | |
| 95 EXPECT_TRUE(HaveSameValueAt(*merged, *user_, "VPN.OpenVPN.Password")); | |
| 96 } | |
| 97 | |
| 98 TEST_F(ONCMergerTest, UserDictionaryAndNoPolicyValue) { | |
| 99 scoped_ptr<base::DictionaryValue> merged( | |
| 100 MergeSettingsWithPolicies(policy_.get(), NULL, user_.get(), NULL)); | |
| 101 const base::Value* value = NULL; | |
| 102 EXPECT_FALSE(merged->Get("ProxySettings", &value)); | |
| 103 } | |
| 104 | |
| 105 TEST_F(ONCMergerTest, MergeWithEmptyPolicyProhibitsEverything) { | |
| 106 base::DictionaryValue emptyDict; | |
| 107 scoped_ptr<base::DictionaryValue> merged( | |
| 108 MergeSettingsWithPolicies(&emptyDict, NULL, user_.get(), NULL)); | |
| 109 EXPECT_TRUE(merged->empty()); | |
| 110 } | |
| 111 | |
| 112 TEST_F(ONCMergerTest, MergeWithoutPolicyAllowsAnything) { | |
| 113 scoped_ptr<base::DictionaryValue> merged( | |
| 114 MergeSettingsWithPolicies(NULL, NULL, user_.get(), NULL)); | |
| 115 EXPECT_TRUE(test_utils::Equals(user_.get(), merged.get())); | |
| 116 } | |
| 117 | |
| 118 TEST_F(ONCMergerTest, MergeWithoutUserSettings) { | |
| 119 base::DictionaryValue emptyDict; | |
| 120 scoped_ptr<base::DictionaryValue> merged; | |
| 121 | |
| 122 merged = MergeSettingsWithPolicies(policy_.get(), NULL, &emptyDict, NULL); | |
| 123 EXPECT_TRUE(test_utils::Equals(policy_without_recommended_.get(), | |
| 124 merged.get())); | |
| 125 | |
| 126 merged = MergeSettingsWithPolicies(policy_.get(), NULL, NULL, NULL); | |
| 127 EXPECT_TRUE(test_utils::Equals(policy_without_recommended_.get(), | |
| 128 merged.get())); | |
| 129 } | |
| 130 | |
| 131 TEST_F(ONCMergerTest, MandatoryUserPolicyOverwriteDevicePolicy) { | |
| 132 scoped_ptr<base::DictionaryValue> merged(MergeSettingsWithPolicies( | |
| 133 policy_.get(), device_policy_.get(), user_.get(), NULL)); | |
| 134 EXPECT_TRUE(HaveSameValueAt(*merged, *policy_, "VPN.OpenVPN.Port")); | |
| 135 } | |
| 136 | |
| 137 TEST_F(ONCMergerTest, MandatoryDevicePolicyOverwritesRecommendedUserPolicy) { | |
| 138 scoped_ptr<base::DictionaryValue> merged(MergeSettingsWithPolicies( | |
| 139 policy_.get(), device_policy_.get(), user_.get(), NULL)); | |
| 140 EXPECT_TRUE(HaveSameValueAt(*merged, *device_policy_, | |
| 141 "VPN.OpenVPN.Username")); | |
| 142 } | |
| 143 | |
| 144 } // namespace merger | |
| 145 } // namespace onc | |
| 146 } // namespace chromeos | |
| OLD | NEW |