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.
5 #include "chromeos/network/onc/onc_merger.h"
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"
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
;
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
;
36 if (base::Value::Equals(a_value
, b_value
)) {
37 return ::testing::AssertionSuccess()
38 << "Entries at '" << path
<< "' are equal";
40 return ::testing::AssertionFailure()
41 << "Entries at '" << path
<< "' not equal but are '"
42 << *a_value
<< "' and '" << *b_value
<< "'";
50 class ONCMergerTest
: public testing::Test
{
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_
;
58 void SetUp() override
{
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");
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"));
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"));
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"));
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"));
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"));
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
));
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());
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()));
121 TEST_F(ONCMergerTest
, MergeWithoutUserSettings
) {
122 base::DictionaryValue emptyDict
;
123 scoped_ptr
<base::DictionaryValue
> merged
;
125 merged
= MergeSettingsAndPoliciesToEffective(
126 policy_
.get(), NULL
, &emptyDict
, NULL
);
127 EXPECT_TRUE(test_utils::Equals(policy_without_recommended_
.get(),
130 merged
= MergeSettingsAndPoliciesToEffective(policy_
.get(), NULL
, NULL
, NULL
);
131 EXPECT_TRUE(test_utils::Equals(policy_without_recommended_
.get(),
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"));
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"));
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()));
157 } // namespace merger
159 } // namespace chromeos