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