Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/network/onc/onc_validator.h" | 5 #include "chromeos/network/onc/onc_validator.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chromeos/network/onc/onc_constants.h" | 11 #include "chromeos/network/onc/onc_constants.h" |
| 12 #include "chromeos/network/onc/onc_signature.h" | 12 #include "chromeos/network/onc/onc_signature.h" |
| 13 #include "chromeos/network/onc/onc_test_utils.h" | 13 #include "chromeos/network/onc/onc_test_utils.h" |
| 14 #include "chromeos/network/onc/onc_utils.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 namespace chromeos { | 17 namespace chromeos { |
| 17 namespace onc { | 18 namespace onc { |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 // Create a strict validator that complains about every error. | 21 // Create a strict validator that complains about every error. |
| 21 scoped_ptr<Validator> CreateStrictValidator(bool managed_onc) { | 22 scoped_ptr<Validator> CreateStrictValidator(bool managed_onc) { |
| 22 return make_scoped_ptr(new Validator(true, true, true, managed_onc)); | 23 return make_scoped_ptr(new Validator(true, true, true, managed_onc)); |
| 23 } | 24 } |
| 24 | 25 |
| 25 // Create a liberal validator that ignores or repairs non-critical errors. | 26 // Create a liberal validator that ignores or repairs non-critical errors. |
| 26 scoped_ptr<Validator> CreateLiberalValidator(bool managed_onc) { | 27 scoped_ptr<Validator> CreateLiberalValidator(bool managed_onc) { |
| 27 return make_scoped_ptr(new Validator(false, false, false, managed_onc)); | 28 return make_scoped_ptr(new Validator(false, false, false, managed_onc)); |
| 28 } | 29 } |
| 29 } // namespace | 30 } // namespace |
| 30 | 31 |
| 32 TEST(ONCValidatorValidTest, EmptyUnencryptedConfiguration) { | |
| 33 scoped_ptr<Validator> validator(CreateStrictValidator(true)); | |
| 34 scoped_ptr<const base::DictionaryValue> original( | |
| 35 ReadDictionaryFromJson(kEmptyUnencryptedConfiguration)); | |
| 36 | |
| 37 Validator::Result result; | |
| 38 scoped_ptr<const base::DictionaryValue> repaired( | |
| 39 validator->ValidateAndRepairObject(&kToplevelConfigurationSignature, | |
| 40 *original, &result)); | |
| 41 EXPECT_EQ(Validator::VALID, result); | |
| 42 EXPECT_TRUE(test_utils::Equals(original.get(), repaired.get())); | |
| 43 } | |
| 44 | |
| 31 // This test case is about validating valid ONC objects. | 45 // This test case is about validating valid ONC objects. |
| 32 TEST(ONCValidatorValidTest, ValidPolicyOnc) { | 46 class ONCValidatorValidTest |
| 47 : public ::testing::TestWithParam< | |
| 48 std::pair<std::string, const OncValueSignature*> > { | |
|
Joao da Silva
2012/12/13 09:51:35
#include <utility>
pneubeck (no reviews)
2012/12/13 14:10:03
Done.
| |
| 49 protected: | |
| 50 std::string GetFilename() const { | |
| 51 return GetParam().first; | |
| 52 } | |
| 53 | |
| 54 const OncValueSignature* GetSignature() const { | |
| 55 return GetParam().second; | |
| 56 } | |
| 57 }; | |
| 58 | |
| 59 TEST_P(ONCValidatorValidTest, ValidPolicyOnc) { | |
| 33 scoped_ptr<Validator> validator(CreateStrictValidator(true)); | 60 scoped_ptr<Validator> validator(CreateStrictValidator(true)); |
| 34 scoped_ptr<const base::DictionaryValue> network; | 61 scoped_ptr<const base::DictionaryValue> original( |
| 35 scoped_ptr<base::DictionaryValue> repaired; | 62 test_utils::ReadTestDictionary(GetFilename())); |
| 36 | 63 |
| 37 network = test_utils::ReadTestDictionary("policy.onc"); | 64 Validator::Result result; |
| 38 repaired = validator->ValidateAndRepairObject( | 65 scoped_ptr<const base::DictionaryValue> repaired( |
| 39 &kNetworkConfigurationSignature, | 66 validator->ValidateAndRepairObject(GetSignature(), *original, &result)); |
| 40 *network); | 67 EXPECT_EQ(Validator::VALID, result); |
| 41 EXPECT_TRUE(test_utils::Equals(network.get(), repaired.get())); | 68 EXPECT_TRUE(test_utils::Equals(original.get(), repaired.get())); |
| 69 } | |
| 42 | 70 |
| 43 network = test_utils::ReadTestDictionary("valid.onc"); | 71 INSTANTIATE_TEST_CASE_P( |
| 44 repaired = validator->ValidateAndRepairObject( | 72 ONCValidatorValidTest, |
| 45 &kNetworkConfigurationSignature, | 73 ONCValidatorValidTest, |
| 46 *network); | 74 ::testing::Values(std::make_pair("managed_toplevel.onc", |
| 47 EXPECT_TRUE(test_utils::Equals(network.get(), repaired.get())); | 75 &kToplevelConfigurationSignature), |
| 48 } | 76 std::make_pair("encrypted.onc", |
| 77 &kToplevelConfigurationSignature), | |
| 78 std::make_pair("managed_vpn.onc", | |
| 79 &kNetworkConfigurationSignature), | |
| 80 std::make_pair("managed_ethernet.onc", | |
| 81 &kNetworkConfigurationSignature), | |
| 82 // Ignore recommended arrays in unmanaged ONC: | |
| 83 std::make_pair("recommended_in_unmanaged.onc", | |
| 84 &kNetworkConfigurationSignature))); | |
| 49 | 85 |
| 50 // Validate invalid ONC objects and check the resulting repaired object. This | 86 // Validate invalid ONC objects and check the resulting repaired object. This |
| 51 // test fixture loads a test json file into |invalid_| containing several test | 87 // test fixture loads a test json file into |invalid_| containing several test |
| 52 // objects which can be accessed by their path. The test boolean parameter | 88 // objects which can be accessed by their path. The test boolean parameter |
| 53 // determines wether to use the strict or the liberal validator. | 89 // determines wether to use the strict or the liberal validator. |
| 54 class ONCValidatorInvalidTest : public ::testing::TestWithParam<bool> { | 90 class ONCValidatorInvalidTest : public ::testing::TestWithParam<bool> { |
| 55 public: | 91 public: |
| 56 // Validate the entry at |path_to_object| with the given | 92 // Validate the entry at |path_to_object| with the given |
| 57 // |signature|. Depending on |managed_onc| the object is interpreted as a | 93 // |signature|. Depending on |managed_onc| the object is interpreted as a |
| 58 // managed onc (with recommended fields) or not. The resulting repaired object | 94 // managed onc (with recommended fields) or not. The resulting repaired object |
| 59 // must match the entry at |path_to_repaired| if the liberal validator is | 95 // must match the entry at |path_to_repaired| if the liberal validator is |
| 60 // used. | 96 // used. |
| 61 void ValidateInvalid(const std::string& path_to_object, | 97 void ValidateInvalid(const std::string& path_to_object, |
| 62 const std::string& path_to_repaired, | 98 const std::string& path_to_repaired, |
| 63 const OncValueSignature* signature, | 99 const OncValueSignature* signature, |
| 64 bool managed_onc) { | 100 bool managed_onc) { |
| 65 scoped_ptr<Validator> validator; | 101 scoped_ptr<Validator> validator; |
| 66 if (GetParam()) | 102 if (GetParam()) |
| 67 validator = CreateStrictValidator(managed_onc); | 103 validator = CreateStrictValidator(managed_onc); |
| 68 else | 104 else |
| 69 validator = CreateLiberalValidator(managed_onc); | 105 validator = CreateLiberalValidator(managed_onc); |
| 70 | 106 |
| 71 const base::DictionaryValue* object = NULL; | 107 const base::DictionaryValue* object = NULL; |
| 72 ASSERT_TRUE(invalid_->GetDictionary(path_to_object, &object)); | 108 ASSERT_TRUE(invalid_->GetDictionary(path_to_object, &object)); |
| 73 | 109 |
| 110 Validator::Result result; | |
| 74 scoped_ptr<base::DictionaryValue> actual_repaired = | 111 scoped_ptr<base::DictionaryValue> actual_repaired = |
| 75 validator->ValidateAndRepairObject(signature, *object); | 112 validator->ValidateAndRepairObject(signature, *object, &result); |
| 76 if (GetParam() || path_to_repaired == "") { | 113 if (GetParam() || path_to_repaired == "") { |
| 114 EXPECT_EQ(Validator::INVALID, result); | |
| 77 EXPECT_EQ(NULL, actual_repaired.get()); | 115 EXPECT_EQ(NULL, actual_repaired.get()); |
| 78 } else { | 116 } else { |
| 117 EXPECT_EQ(Validator::VALID_WITH_WARNINGS, result); | |
| 79 const base::DictionaryValue* expected_repaired = NULL; | 118 const base::DictionaryValue* expected_repaired = NULL; |
| 80 invalid_->GetDictionary(path_to_repaired, &expected_repaired); | 119 invalid_->GetDictionary(path_to_repaired, &expected_repaired); |
| 81 EXPECT_TRUE(test_utils::Equals(expected_repaired, actual_repaired.get())); | 120 EXPECT_TRUE(test_utils::Equals(expected_repaired, actual_repaired.get())); |
| 82 } | 121 } |
| 83 } | 122 } |
| 84 | 123 |
| 85 virtual void SetUp() { | 124 virtual void SetUp() { |
| 86 invalid_ = | 125 invalid_ = |
| 87 test_utils::ReadTestDictionary("invalid_settings_with_repairs.json"); | 126 test_utils::ReadTestDictionary("invalid_settings_with_repairs.json"); |
| 88 } | 127 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 "network-repaired", | 173 "network-repaired", |
| 135 &kNetworkConfigurationSignature, false); | 174 &kNetworkConfigurationSignature, false); |
| 136 } | 175 } |
| 137 | 176 |
| 138 INSTANTIATE_TEST_CASE_P(ONCValidatorInvalidTest, | 177 INSTANTIATE_TEST_CASE_P(ONCValidatorInvalidTest, |
| 139 ONCValidatorInvalidTest, | 178 ONCValidatorInvalidTest, |
| 140 ::testing::Bool()); | 179 ::testing::Bool()); |
| 141 | 180 |
| 142 } // namespace onc | 181 } // namespace onc |
| 143 } // namespace chromeos | 182 } // namespace chromeos |
| OLD | NEW |