onc_merger_unittest.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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_constants.h" 12#include "chromeos/network/onc/onc_test_utils.h" 13#include "testing/gtest/include/gtest/gtest.h" 14 15namespace chromeos { 16namespace onc { 17namespace { 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 47namespace merger { 48 49class 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("managed_vpn.onc"); 58 policy_without_recommended_ = 59 test_utils::ReadTestDictionary("managed_vpn_without_recommended.onc"); 60 user_ = test_utils::ReadTestDictionary("user.onc"); 61 device_policy_ = test_utils::ReadTestDictionary("device_policy.onc"); 62 } 63}; 64 65TEST_F(ONCMergerTest, MandatoryValueOverwritesUserValue) { 66 scoped_ptr<base::DictionaryValue> merged(MergeSettingsAndPoliciesToEffective( 67 policy_.get(), NULL, user_.get(), NULL)); 68 EXPECT_TRUE(HaveSameValueAt(*merged, *policy_, "Type")); 69 EXPECT_TRUE(HaveSameValueAt(*merged, *policy_, "IPConfigs")); 70} 71 72TEST_F(ONCMergerTest, MandatoryValueAndNoUserValue) { 73 scoped_ptr<base::DictionaryValue> merged(MergeSettingsAndPoliciesToEffective( 74 policy_.get(), NULL, user_.get(), NULL)); 75 EXPECT_TRUE(HaveSameValueAt(*merged, *policy_, "GUID")); 76 EXPECT_TRUE(HaveSameValueAt(*merged, *policy_, "VPN.OpenVPN.Username")); 77} 78 79TEST_F(ONCMergerTest, MandatoryDictionaryAndNoUserValue) { 80 scoped_ptr<base::DictionaryValue> merged(MergeSettingsAndPoliciesToEffective( 81 policy_.get(), NULL, user_.get(), NULL)); 82 EXPECT_TRUE(HaveSameValueAt(*merged, *policy_without_recommended_, 83 "VPN.OpenVPN.ClientCertPattern")); 84} 85 86TEST_F(ONCMergerTest, UserValueOverwritesRecommendedValue) { 87 scoped_ptr<base::DictionaryValue> merged(MergeSettingsAndPoliciesToEffective( 88 policy_.get(), NULL, user_.get(), NULL)); 89 EXPECT_TRUE(HaveSameValueAt(*merged, *user_, "VPN.Host")); 90} 91 92TEST_F(ONCMergerTest, UserValueAndRecommendedUnset) { 93 scoped_ptr<base::DictionaryValue> merged(MergeSettingsAndPoliciesToEffective( 94 policy_.get(), NULL, user_.get(), NULL)); 95 EXPECT_TRUE(HaveSameValueAt(*merged, *user_, "VPN.OpenVPN.Password")); 96} 97 98TEST_F(ONCMergerTest, UserDictionaryAndNoPolicyValue) { 99 scoped_ptr<base::DictionaryValue> merged(MergeSettingsAndPoliciesToEffective( 100 policy_.get(), NULL, user_.get(), NULL)); 101 const base::Value* value = NULL; 102 EXPECT_FALSE(merged->Get("ProxySettings", &value)); 103} 104 105TEST_F(ONCMergerTest, MergeWithEmptyPolicyProhibitsEverything) { 106 base::DictionaryValue emptyDict; 107 scoped_ptr<base::DictionaryValue> merged( 108 MergeSettingsAndPoliciesToEffective(&emptyDict, NULL, user_.get(), NULL)); 109 EXPECT_TRUE(merged->empty()); 110} 111 112TEST_F(ONCMergerTest, MergeWithoutPolicyAllowsAnything) { 113 scoped_ptr<base::DictionaryValue> merged( 114 MergeSettingsAndPoliciesToEffective(NULL, NULL, user_.get(), NULL)); 115 EXPECT_TRUE(test_utils::Equals(user_.get(), merged.get())); 116} 117 118TEST_F(ONCMergerTest, MergeWithoutUserSettings) { 119 base::DictionaryValue emptyDict; 120 scoped_ptr<base::DictionaryValue> merged; 121 122 merged = MergeSettingsAndPoliciesToEffective( 123 policy_.get(), NULL, &emptyDict, NULL); 124 EXPECT_TRUE(test_utils::Equals(policy_without_recommended_.get(), 125 merged.get())); 126 127 merged = MergeSettingsAndPoliciesToEffective(policy_.get(), NULL, NULL, NULL); 128 EXPECT_TRUE(test_utils::Equals(policy_without_recommended_.get(), 129 merged.get())); 130} 131 132TEST_F(ONCMergerTest, MandatoryUserPolicyOverwritesDevicePolicy) { 133 scoped_ptr<base::DictionaryValue> merged(MergeSettingsAndPoliciesToEffective( 134 policy_.get(), device_policy_.get(), user_.get(), NULL)); 135 EXPECT_TRUE(HaveSameValueAt(*merged, *policy_, "VPN.OpenVPN.Port")); 136} 137 138TEST_F(ONCMergerTest, MandatoryDevicePolicyOverwritesRecommendedUserPolicy) { 139 scoped_ptr<base::DictionaryValue> merged(MergeSettingsAndPoliciesToEffective( 140 policy_.get(), device_policy_.get(), user_.get(), NULL)); 141 EXPECT_TRUE(HaveSameValueAt(*merged, *device_policy_, 142 "VPN.OpenVPN.Username")); 143} 144 145TEST_F(ONCMergerTest, MergeToAugmented) { 146 scoped_ptr<base::DictionaryValue> expected_augmented = 147 test_utils::ReadTestDictionary("augmented_merge.json"); 148 scoped_ptr<base::DictionaryValue> merged(MergeSettingsAndPoliciesToAugmented( 149 policy_.get(), device_policy_.get(), user_.get(), NULL)); 150 EXPECT_TRUE(test_utils::Equals(expected_augmented.get(), merged.get())); 151} 152 153} // namespace merger 154} // namespace onc 155} // namespace chromeos 156