Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(787)

Side by Side Diff: chrome/browser/chromeos/network_settings/onc_validator_unittest.cc

Issue 11469026: Extending ONC validator's logging. Completing toplevel validation. (Closed) Base URL: http://git.chromium.org/chromium/src.git@add_error_handling_to_validator
Patch Set: Initial patch. Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/chromeos/network_settings/onc_validator.h" 5 #include "chrome/browser/chromeos/network_settings/onc_validator.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/logging.h"
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 "chrome/browser/chromeos/cros/onc_constants.h" 12 #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_signature.h"
13 #include "chrome/browser/chromeos/network_settings/onc_test_utils.h" 14 #include "chrome/browser/chromeos/network_settings/onc_test_utils.h"
15 #include "chrome/browser/chromeos/network_settings/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 std::string error;
36 scoped_ptr<const base::DictionaryValue> original(
37 ReadDictionaryFromJson(kEmptyUnencryptedConfiguration, &error));
38
39 Validator::Result result;
40 scoped_ptr<const base::DictionaryValue> repaired(
41 validator->ValidateAndRepairObject(&kToplevelConfigurationSignature,
42 *original, &result));
43 EXPECT_EQ(Validator::VALID, result);
44 EXPECT_TRUE(test_utils::Equals(original.get(), repaired.get()));
45 }
46
31 // This test case is about validating valid ONC objects. 47 // This test case is about validating valid ONC objects.
32 TEST(ONCValidatorValidTest, ValidPolicyOnc) { 48 class ONCValidatorValidTest
49 : public ::testing::TestWithParam<
50 std::pair<std::string, const OncValueSignature*> > {
51 protected:
52 std::string GetFilename() const {
53 return GetParam().first;
54 }
55
56 const OncValueSignature* GetSignature() const {
57 return GetParam().second;
58 }
59 };
60
61 TEST_P(ONCValidatorValidTest, ValidPolicyOnc) {
33 scoped_ptr<Validator> validator(CreateStrictValidator(true)); 62 scoped_ptr<Validator> validator(CreateStrictValidator(true));
34 scoped_ptr<const base::DictionaryValue> network; 63 scoped_ptr<const base::DictionaryValue> original(
35 scoped_ptr<base::DictionaryValue> repaired; 64 test_utils::ReadTestDictionary(GetFilename()));
36 65
37 network = test_utils::ReadTestDictionary("policy.onc"); 66 Validator::Result result;
38 repaired = validator->ValidateAndRepairObject( 67 scoped_ptr<const base::DictionaryValue> repaired(
39 &kNetworkConfigurationSignature, 68 validator->ValidateAndRepairObject(GetSignature(), *original, &result));
40 *network); 69 EXPECT_EQ(Validator::VALID, result);
41 EXPECT_TRUE(test_utils::Equals(network.get(), repaired.get())); 70 EXPECT_TRUE(test_utils::Equals(original.get(), repaired.get()));
71 }
42 72
43 network = test_utils::ReadTestDictionary("valid.onc"); 73 INSTANTIATE_TEST_CASE_P(
44 repaired = validator->ValidateAndRepairObject( 74 ONCValidatorValidTest,
45 &kNetworkConfigurationSignature, 75 ONCValidatorValidTest,
46 *network); 76 ::testing::Values(std::make_pair("managed_toplevel.onc",
47 EXPECT_TRUE(test_utils::Equals(network.get(), repaired.get())); 77 &kToplevelConfigurationSignature),
48 } 78 std::make_pair("encrypted.onc",
79 &kToplevelConfigurationSignature),
80 std::make_pair("managed_vpn.onc",
81 &kNetworkConfigurationSignature),
82 std::make_pair("managed_ethernet.onc",
83 &kNetworkConfigurationSignature),
84 // Ignore recommended arrays in unmanaged ONC:
85 std::make_pair("recommended_in_unmanaged.onc",
86 &kNetworkConfigurationSignature)));
49 87
50 // Validate invalid ONC objects and check the resulting repaired object. This 88 // Validate invalid ONC objects and check the resulting repaired object. This
51 // test fixture loads a test json file into |invalid_| containing several test 89 // 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 90 // objects which can be accessed by their path. The test boolean parameter
53 // determines wether to use the strict or the liberal validator. 91 // determines wether to use the strict or the liberal validator.
54 class ONCValidatorInvalidTest : public ::testing::TestWithParam<bool> { 92 class ONCValidatorInvalidTest : public ::testing::TestWithParam<bool> {
55 public: 93 public:
56 // Validate the entry at |path_to_object| with the given 94 // Validate the entry at |path_to_object| with the given
57 // |signature|. Depending on |managed_onc| the object is interpreted as a 95 // |signature|. Depending on |managed_onc| the object is interpreted as a
58 // managed onc (with recommended fields) or not. The resulting repaired object 96 // 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 97 // must match the entry at |path_to_repaired| if the liberal validator is
60 // used. 98 // used.
61 void ValidateInvalid(const std::string& path_to_object, 99 void ValidateInvalid(const std::string& path_to_object,
62 const std::string& path_to_repaired, 100 const std::string& path_to_repaired,
63 const OncValueSignature* signature, 101 const OncValueSignature* signature,
64 bool managed_onc) { 102 bool managed_onc) {
65 scoped_ptr<Validator> validator; 103 scoped_ptr<Validator> validator;
66 if (GetParam()) 104 if (GetParam())
67 validator = CreateStrictValidator(managed_onc); 105 validator = CreateStrictValidator(managed_onc);
68 else 106 else
69 validator = CreateLiberalValidator(managed_onc); 107 validator = CreateLiberalValidator(managed_onc);
70 108
71 const base::DictionaryValue* object = NULL; 109 const base::DictionaryValue* object = NULL;
72 ASSERT_TRUE(invalid_->GetDictionary(path_to_object, &object)); 110 ASSERT_TRUE(invalid_->GetDictionary(path_to_object, &object));
73 111
112 Validator::Result result;
74 scoped_ptr<base::DictionaryValue> actual_repaired = 113 scoped_ptr<base::DictionaryValue> actual_repaired =
75 validator->ValidateAndRepairObject(signature, *object); 114 validator->ValidateAndRepairObject(signature, *object, &result);
76 if (GetParam() || path_to_repaired == "") { 115 if (GetParam() || path_to_repaired == "") {
116 EXPECT_EQ(Validator::INVALID, result);
77 EXPECT_EQ(NULL, actual_repaired.get()); 117 EXPECT_EQ(NULL, actual_repaired.get());
78 } else { 118 } else {
119 EXPECT_EQ(Validator::VALID_WITH_WARNINGS, result);
79 const base::DictionaryValue* expected_repaired = NULL; 120 const base::DictionaryValue* expected_repaired = NULL;
80 invalid_->GetDictionary(path_to_repaired, &expected_repaired); 121 invalid_->GetDictionary(path_to_repaired, &expected_repaired);
81 EXPECT_TRUE(test_utils::Equals(expected_repaired, actual_repaired.get())); 122 EXPECT_TRUE(test_utils::Equals(expected_repaired, actual_repaired.get()));
82 } 123 }
83 } 124 }
84 125
85 virtual void SetUp() { 126 virtual void SetUp() {
86 invalid_ = 127 invalid_ =
87 test_utils::ReadTestDictionary("invalid_settings_with_repairs.json"); 128 test_utils::ReadTestDictionary("invalid_settings_with_repairs.json");
88 } 129 }
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 "network-repaired", 175 "network-repaired",
135 &kNetworkConfigurationSignature, false); 176 &kNetworkConfigurationSignature, false);
136 } 177 }
137 178
138 INSTANTIATE_TEST_CASE_P(ONCValidatorInvalidTest, 179 INSTANTIATE_TEST_CASE_P(ONCValidatorInvalidTest,
139 ONCValidatorInvalidTest, 180 ONCValidatorInvalidTest,
140 ::testing::Bool()); 181 ::testing::Bool());
141 182
142 } // namespace onc 183 } // namespace onc
143 } // namespace chromeos 184 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698