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