| 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_validator.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/browser/chromeos/cros/onc_constants.h" |
| 12 #include "chrome/browser/chromeos/network_settings/onc_signature.h" |
| 13 #include "chrome/browser/chromeos/network_settings/onc_test_utils.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 #include <iostream> |
| 17 |
| 18 namespace chromeos { |
| 19 namespace onc { |
| 20 |
| 21 // This test case is about validating valid ONC objects. |
| 22 TEST(ONCValidatorValidTest, ValidPolicyOnc) { |
| 23 scoped_ptr<Validator> validator(Validator::CreateStrictValidator(true)); |
| 24 scoped_ptr<const base::DictionaryValue> network; |
| 25 scoped_ptr<base::DictionaryValue> repaired; |
| 26 |
| 27 network = test_utils::ReadTestDictionary("policy.onc"); |
| 28 repaired = validator->ValidateAndRepairObject( |
| 29 &network_configuration_signature, |
| 30 *network); |
| 31 EXPECT_TRUE(repaired.get() != NULL && network->Equals(repaired.get())); |
| 32 |
| 33 network = test_utils::ReadTestDictionary("valid.onc"); |
| 34 repaired = validator->ValidateAndRepairObject( |
| 35 &network_configuration_signature, |
| 36 *network); |
| 37 EXPECT_TRUE(repaired.get() != NULL && network->Equals(repaired.get())); |
| 38 } |
| 39 |
| 40 // Validate invalid ONC objects and check the resulting repaired object. This |
| 41 // test fixture loads a test json file into |invalid_| containing several test |
| 42 // objects which can be accessed by their path. The test boolean parameter |
| 43 // determines wether to use the strict or the liberal validator. |
| 44 class ONCValidatorInvalidTest : public ::testing::TestWithParam<bool> { |
| 45 public: |
| 46 // Validate the entry at |path_to_object| with the given |
| 47 // |signature|. Depending on |managed_onc| the object is interpreted as a |
| 48 // managed onc (with recommended fields) or not. The resulting repaired object |
| 49 // must match the entry at |path_to_repaired| if the liberal validator is |
| 50 // used. |
| 51 void ValidateInvalid(const std::string& path_to_object, |
| 52 const std::string& path_to_repaired, |
| 53 const OncValueSignature* signature, |
| 54 bool managed_onc) { |
| 55 scoped_ptr<Validator> validator; |
| 56 if (GetParam()) |
| 57 validator = Validator::CreateStrictValidator(managed_onc); |
| 58 else |
| 59 validator = Validator::CreateLiberalValidator(managed_onc); |
| 60 |
| 61 const base::DictionaryValue* object = NULL; |
| 62 ASSERT_TRUE(invalid_->GetDictionary(path_to_object, &object)); |
| 63 |
| 64 scoped_ptr<base::DictionaryValue> actual_repaired = |
| 65 validator->ValidateAndRepairObject(signature, *object); |
| 66 if (GetParam() || path_to_repaired == "") { |
| 67 EXPECT_EQ(NULL, actual_repaired.get()); |
| 68 } else { |
| 69 const base::DictionaryValue* expected_repaired = NULL; |
| 70 invalid_->GetDictionary(path_to_repaired, &expected_repaired); |
| 71 ASSERT_TRUE(expected_repaired != NULL); |
| 72 EXPECT_TRUE(actual_repaired.get() && |
| 73 actual_repaired->Equals(expected_repaired)); |
| 74 } |
| 75 } |
| 76 |
| 77 virtual void SetUp() { |
| 78 invalid_ = test_utils::ReadTestDictionary("invalid.onc"); |
| 79 } |
| 80 |
| 81 scoped_ptr<const base::DictionaryValue> invalid_; |
| 82 }; |
| 83 |
| 84 TEST_P(ONCValidatorInvalidTest, UnknownFieldName) { |
| 85 ValidateInvalid("network-unknown-fieldname", |
| 86 "network-repaired", |
| 87 &network_configuration_signature, false); |
| 88 ValidateInvalid("managed-network-unknown-fieldname", |
| 89 "managed-network-repaired", |
| 90 &network_configuration_signature, true); |
| 91 } |
| 92 |
| 93 TEST_P(ONCValidatorInvalidTest, UnknownRecommendedFieldName) { |
| 94 ValidateInvalid("managed-network-unknown-recommended", |
| 95 "managed-network-repaired", |
| 96 &network_configuration_signature, true); |
| 97 } |
| 98 |
| 99 TEST_P(ONCValidatorInvalidTest, DictionaryRecommended) { |
| 100 ValidateInvalid("managed-network-dict-recommended", |
| 101 "managed-network-repaired", |
| 102 &network_configuration_signature, true); |
| 103 } |
| 104 |
| 105 TEST_P(ONCValidatorInvalidTest, MissingRequiredField) { |
| 106 ValidateInvalid("network-missing-required", |
| 107 "network-missing-required", |
| 108 &network_configuration_signature, false); |
| 109 ValidateInvalid("managed-network-missing-required", |
| 110 "managed-network-missing-required", |
| 111 &network_configuration_signature, true); |
| 112 } |
| 113 |
| 114 TEST_P(ONCValidatorInvalidTest, RecommendedInUnmanaged) { |
| 115 ValidateInvalid("network-illegal-recommended", |
| 116 "network-repaired", |
| 117 &network_configuration_signature, false); |
| 118 } |
| 119 |
| 120 INSTANTIATE_TEST_CASE_P(ONCValidatorInvalidTest, |
| 121 ONCValidatorInvalidTest, |
| 122 ::testing::Bool()); |
| 123 } // namespace onc |
| 124 } // namespace chromeos |
| OLD | NEW |