Chromium Code Reviews| Index: chrome/browser/chromeos/network_settings/onc_merger_unittest.cc |
| diff --git a/chrome/browser/chromeos/network_settings/onc_merger_unittest.cc b/chrome/browser/chromeos/network_settings/onc_merger_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ae5e5c6154a8021b1c9f2bbf8613cf829c3040a6 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/network_settings/onc_merger_unittest.cc |
| @@ -0,0 +1,163 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/network_settings/onc_merger.h" |
| + |
| +#include <string> |
|
Mattias Nissler (ping if slow)
2012/09/18 16:29:08
newline
pneubeck (no reviews)
2012/10/02 15:03:02
Done.
|
| +#include "base/file_path.h" |
| +#include "base/file_util.h" |
| +#include "base/json/json_file_value_serializer.h" |
| +#include "base/path_service.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/chromeos/cros/onc_constants.h" |
| +#include "chrome/common/chrome_paths.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace chromeos { |
| +namespace onc { |
| + |
| +namespace { |
| + |
| +// This method was taken from onc_network_parser_unittest.cc and the folder path |
| +// adapted. |
| + |
| +::testing::AssertionResult ReadTestDictionary( |
| + const std::string& filename, |
| + const base::DictionaryValue** dict) { |
| + FilePath path; |
| + PathService::Get(chrome::DIR_TEST_DATA, &path); |
| + path = path.AppendASCII("chromeos").AppendASCII("network_settings"). |
| + Append(filename); |
| + JSONFileValueSerializer serializer(path); |
| + serializer.set_allow_trailing_comma(true); |
| + |
| + std::string error_message; |
| + base::Value* content = serializer.Deserialize(NULL, &error_message); |
| + if (content == NULL) |
| + return ::testing::AssertionFailure() << error_message; |
| + |
| + if (content->GetAsDictionary(dict)) { |
| + return ::testing::AssertionSuccess(); |
| + } else { |
| + return ::testing::AssertionFailure() |
| + << "File '" << filename |
| + << "' does not contain a dictionary as expected, but type " |
| + << content->GetType(); |
| + } |
| +} |
| + |
| +::testing::AssertionResult IsEntryEqual(const base::DictionaryValue* a, |
| + const base::DictionaryValue* b, |
| + const std::string& path) { |
| + const base::Value* a_value = NULL; |
| + const base::Value* b_value = NULL; |
| + |
| + if (!a->Get(path, &a_value)) { |
| + return ::testing::AssertionFailure() |
| + << "First dictionary '" << *a |
| + << "' doesn't contain " << path; |
| + } |
| + |
| + if (!b->Get(path, &b_value)) { |
| + return ::testing::AssertionFailure() |
| + << "Second dictionary '" << *b |
| + << "' doesn't contain " << path; |
| + } |
| + |
| + if (base::Value::Equals(a_value, b_value)) { |
| + return ::testing::AssertionSuccess() |
| + << "Entries at '" << path << "' are equal"; |
| + } else { |
| + return ::testing::AssertionFailure() |
| + << "Entries at '" << path << "' not equal but are '" |
| + << *a_value << "' and '" << *b_value << "'"; |
| + } |
| +} |
| + |
| +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.
|
| + base::DictionaryValue* result = new base::DictionaryValue; |
| + |
| + for (base::DictionaryValue::key_iterator it = dict.begin_keys(); |
| + it != dict.end_keys(); ++it) { |
| + const std::string& field_name = *it; |
| + if (field_name == chromeos::onc::kRecommended) |
| + continue; |
| + |
| + const base::Value* value = NULL; |
| + dict.GetWithoutPathExpansion(field_name, &value); |
| + const base::DictionaryValue* dict_value = NULL; |
| + |
| + if (value->GetAsDictionary(&dict_value)) { |
| + result->Set(field_name, DeepDropRecommended(*dict_value)); |
| + } else { |
| + result->Set(field_name, value->DeepCopy()); |
| + } |
| + } |
| + |
| + return result; |
| +} |
| + |
| +} // namespace |
| + |
| +class ONCMergerTest : public testing::Test { |
| + public: |
| + static const base::DictionaryValue* user_; |
| + static const base::DictionaryValue* policy_; |
| + static const base::DictionaryValue* policy_without_recommended_; |
| + static const base::DictionaryValue* result_; |
| + |
| + static void SetUpTestCase() { |
| + ASSERT_TRUE(ReadTestDictionary("policy.onc", &policy_)); |
| + policy_without_recommended_ = DeepDropRecommended(*policy_); |
| + ASSERT_TRUE(ReadTestDictionary("user.onc", &user_)); |
| + result_ = ONCMerger::MergeUserSettingsWithPolicy(*user_, *policy_); |
| + ASSERT_TRUE(result_->GetType() == base::Value::TYPE_DICTIONARY); |
| + } |
| + |
| + static void TearDownTestCase() { |
| + delete user_; |
| + delete policy_; |
| + 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.
|
| + } |
| +}; |
| + |
| +const base::DictionaryValue* ONCMergerTest::user_ = NULL; |
| +const base::DictionaryValue* ONCMergerTest::policy_ = NULL; |
| +const base::DictionaryValue* ONCMergerTest::policy_without_recommended_ = NULL; |
| +const base::DictionaryValue* ONCMergerTest::result_ = NULL; |
| + |
| +TEST_F(ONCMergerTest, MandatoryValueOverwritesUserValue) { |
| + EXPECT_TRUE(IsEntryEqual(result_, policy_, "Type")); |
| + EXPECT_TRUE(IsEntryEqual(result_, policy_, "IPConfigs")); |
| +} |
| + |
| +TEST_F(ONCMergerTest, MandatoryValueAndNoUserValue) { |
| + EXPECT_TRUE(IsEntryEqual(result_, policy_, "GUID")); |
| + EXPECT_TRUE(IsEntryEqual(result_, policy_, "VPN.OpenVPN.Username")); |
| +} |
| + |
| +TEST_F(ONCMergerTest, MandatoryDictionaryAndNoUserValue) { |
| + EXPECT_TRUE(IsEntryEqual(result_, policy_without_recommended_, |
| + "VPN.OpenVPN.ClientCertPattern")); |
| +} |
| + |
| +TEST_F(ONCMergerTest, UserValueOverwritesRecommendedValue) { |
| + EXPECT_TRUE(IsEntryEqual(result_, user_, "VPN.Host")); |
| +} |
| + |
| +TEST_F(ONCMergerTest, UserValueAndRecommendedUnset) { |
| + EXPECT_TRUE(IsEntryEqual(result_, user_, "VPN.OpenVPN.Password")); |
| +} |
| + |
| +TEST_F(ONCMergerTest, UserDictionaryAndNoPolicyValue) { |
| + EXPECT_TRUE(IsEntryEqual(result_, user_, "ProxySettings")); |
| +} |
| + |
| +TEST_F(ONCMergerTest, RemovedIgnoredField) { |
| + const base::Value* value = NULL; |
| + EXPECT_FALSE(result_->Get("VPN.IPsec", &value)); |
| +} |
| + |
| +} // namespace onc |
| +} // namespace chromeos |