Chromium Code Reviews| 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> | |
|
Mattias Nissler (ping if slow)
2012/09/18 16:29:08
newline
pneubeck (no reviews)
2012/10/02 15:03:02
Done.
| |
| 8 #include "base/file_path.h" | |
| 9 #include "base/file_util.h" | |
| 10 #include "base/json/json_file_value_serializer.h" | |
| 11 #include "base/path_service.h" | |
| 12 #include "base/values.h" | |
| 13 #include "chrome/browser/chromeos/cros/onc_constants.h" | |
| 14 #include "chrome/common/chrome_paths.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace chromeos { | |
| 18 namespace onc { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // This method was taken from onc_network_parser_unittest.cc and the folder path | |
| 23 // adapted. | |
| 24 | |
| 25 ::testing::AssertionResult ReadTestDictionary( | |
| 26 const std::string& filename, | |
| 27 const base::DictionaryValue** dict) { | |
| 28 FilePath path; | |
| 29 PathService::Get(chrome::DIR_TEST_DATA, &path); | |
| 30 path = path.AppendASCII("chromeos").AppendASCII("network_settings"). | |
| 31 Append(filename); | |
| 32 JSONFileValueSerializer serializer(path); | |
| 33 serializer.set_allow_trailing_comma(true); | |
| 34 | |
| 35 std::string error_message; | |
| 36 base::Value* content = serializer.Deserialize(NULL, &error_message); | |
| 37 if (content == NULL) | |
| 38 return ::testing::AssertionFailure() << error_message; | |
| 39 | |
| 40 if (content->GetAsDictionary(dict)) { | |
| 41 return ::testing::AssertionSuccess(); | |
| 42 } else { | |
| 43 return ::testing::AssertionFailure() | |
| 44 << "File '" << filename | |
| 45 << "' does not contain a dictionary as expected, but type " | |
| 46 << content->GetType(); | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 ::testing::AssertionResult IsEntryEqual(const base::DictionaryValue* a, | |
| 51 const base::DictionaryValue* b, | |
| 52 const std::string& path) { | |
| 53 const base::Value* a_value = NULL; | |
| 54 const base::Value* b_value = NULL; | |
| 55 | |
| 56 if (!a->Get(path, &a_value)) { | |
| 57 return ::testing::AssertionFailure() | |
| 58 << "First dictionary '" << *a | |
| 59 << "' doesn't contain " << path; | |
| 60 } | |
| 61 | |
| 62 if (!b->Get(path, &b_value)) { | |
| 63 return ::testing::AssertionFailure() | |
| 64 << "Second dictionary '" << *b | |
| 65 << "' doesn't contain " << path; | |
| 66 } | |
| 67 | |
| 68 if (base::Value::Equals(a_value, b_value)) { | |
| 69 return ::testing::AssertionSuccess() | |
| 70 << "Entries at '" << path << "' are equal"; | |
| 71 } else { | |
| 72 return ::testing::AssertionFailure() | |
| 73 << "Entries at '" << path << "' not equal but are '" | |
| 74 << *a_value << "' and '" << *b_value << "'"; | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 base::DictionaryValue* DeepDropRecommended(const base::DictionaryValue& dict) { | |
|
Mattias Nissler (ping if slow)
2012/09/18 16:29:08
What about dictionaries in lists? you don't seem t
pneubeck (no reviews)
2012/10/02 15:03:02
Added a comment explaining this.
| |
| 79 base::DictionaryValue* result = new base::DictionaryValue; | |
| 80 | |
| 81 for (base::DictionaryValue::key_iterator it = dict.begin_keys(); | |
| 82 it != dict.end_keys(); ++it) { | |
| 83 const std::string& field_name = *it; | |
| 84 if (field_name == chromeos::onc::kRecommended) | |
| 85 continue; | |
| 86 | |
| 87 const base::Value* value = NULL; | |
| 88 dict.GetWithoutPathExpansion(field_name, &value); | |
| 89 const base::DictionaryValue* dict_value = NULL; | |
| 90 | |
| 91 if (value->GetAsDictionary(&dict_value)) { | |
| 92 result->Set(field_name, DeepDropRecommended(*dict_value)); | |
| 93 } else { | |
| 94 result->Set(field_name, value->DeepCopy()); | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 return result; | |
| 99 } | |
| 100 | |
| 101 } // namespace | |
| 102 | |
| 103 class ONCMergerTest : public testing::Test { | |
| 104 public: | |
| 105 static const base::DictionaryValue* user_; | |
| 106 static const base::DictionaryValue* policy_; | |
| 107 static const base::DictionaryValue* policy_without_recommended_; | |
| 108 static const base::DictionaryValue* result_; | |
| 109 | |
| 110 static void SetUpTestCase() { | |
| 111 ASSERT_TRUE(ReadTestDictionary("policy.onc", &policy_)); | |
| 112 policy_without_recommended_ = DeepDropRecommended(*policy_); | |
| 113 ASSERT_TRUE(ReadTestDictionary("user.onc", &user_)); | |
| 114 result_ = ONCMerger::MergeUserSettingsWithPolicy(*user_, *policy_); | |
| 115 ASSERT_TRUE(result_->GetType() == base::Value::TYPE_DICTIONARY); | |
| 116 } | |
| 117 | |
| 118 static void TearDownTestCase() { | |
| 119 delete user_; | |
| 120 delete policy_; | |
| 121 delete result_; | |
|
Mattias Nissler (ping if slow)
2012/09/18 16:29:08
I'd advise to use scoped_ptrs, but that's not poss
pneubeck (no reviews)
2012/10/02 15:03:02
Done.
| |
| 122 } | |
| 123 }; | |
| 124 | |
| 125 const base::DictionaryValue* ONCMergerTest::user_ = NULL; | |
| 126 const base::DictionaryValue* ONCMergerTest::policy_ = NULL; | |
| 127 const base::DictionaryValue* ONCMergerTest::policy_without_recommended_ = NULL; | |
| 128 const base::DictionaryValue* ONCMergerTest::result_ = NULL; | |
| 129 | |
| 130 TEST_F(ONCMergerTest, MandatoryValueOverwritesUserValue) { | |
| 131 EXPECT_TRUE(IsEntryEqual(result_, policy_, "Type")); | |
| 132 EXPECT_TRUE(IsEntryEqual(result_, policy_, "IPConfigs")); | |
| 133 } | |
| 134 | |
| 135 TEST_F(ONCMergerTest, MandatoryValueAndNoUserValue) { | |
| 136 EXPECT_TRUE(IsEntryEqual(result_, policy_, "GUID")); | |
| 137 EXPECT_TRUE(IsEntryEqual(result_, policy_, "VPN.OpenVPN.Username")); | |
| 138 } | |
| 139 | |
| 140 TEST_F(ONCMergerTest, MandatoryDictionaryAndNoUserValue) { | |
| 141 EXPECT_TRUE(IsEntryEqual(result_, policy_without_recommended_, | |
| 142 "VPN.OpenVPN.ClientCertPattern")); | |
| 143 } | |
| 144 | |
| 145 TEST_F(ONCMergerTest, UserValueOverwritesRecommendedValue) { | |
| 146 EXPECT_TRUE(IsEntryEqual(result_, user_, "VPN.Host")); | |
| 147 } | |
| 148 | |
| 149 TEST_F(ONCMergerTest, UserValueAndRecommendedUnset) { | |
| 150 EXPECT_TRUE(IsEntryEqual(result_, user_, "VPN.OpenVPN.Password")); | |
| 151 } | |
| 152 | |
| 153 TEST_F(ONCMergerTest, UserDictionaryAndNoPolicyValue) { | |
| 154 EXPECT_TRUE(IsEntryEqual(result_, user_, "ProxySettings")); | |
| 155 } | |
| 156 | |
| 157 TEST_F(ONCMergerTest, RemovedIgnoredField) { | |
| 158 const base::Value* value = NULL; | |
| 159 EXPECT_FALSE(result_->Get("VPN.IPsec", &value)); | |
| 160 } | |
| 161 | |
| 162 } // namespace onc | |
| 163 } // namespace chromeos | |
| OLD | NEW |