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 namespace chromeos { | |
17 namespace onc { | |
18 namespace { | |
19 | |
20 // Create a strict validator that complains about every error. | |
21 scoped_ptr<Validator> CreateStrictValidator(bool managed_onc) { | |
22 return make_scoped_ptr(new Validator(true, true, true, managed_onc)); | |
23 } | |
24 | |
25 // Create a liberal validator that ignores or repairs non-critical errors. | |
26 scoped_ptr<Validator> CreateLiberalValidator(bool managed_onc) { | |
27 return make_scoped_ptr(new Validator(false, false, false, managed_onc)); | |
28 } | |
29 } // namespace | |
30 | |
31 // This test case is about validating valid ONC objects. | |
32 TEST(ONCValidatorValidTest, ValidPolicyOnc) { | |
33 scoped_ptr<Validator> validator(CreateStrictValidator(true)); | |
34 scoped_ptr<const base::DictionaryValue> network; | |
35 scoped_ptr<base::DictionaryValue> repaired; | |
36 | |
37 network = test_utils::ReadTestDictionary("policy.onc"); | |
38 repaired = validator->ValidateAndRepairObject( | |
39 &kNetworkConfigurationSignature, | |
40 *network); | |
41 EXPECT_TRUE(test_utils::Equals(network.get(), repaired.get())); | |
42 | |
43 network = test_utils::ReadTestDictionary("valid.onc"); | |
44 repaired = validator->ValidateAndRepairObject( | |
45 &kNetworkConfigurationSignature, | |
46 *network); | |
47 EXPECT_TRUE(test_utils::Equals(network.get(), repaired.get())); | |
48 } | |
49 | |
50 // Validate invalid ONC objects and check the resulting repaired object. This | |
51 // 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 | |
53 // determines wether to use the strict or the liberal validator. | |
54 class ONCValidatorInvalidTest : public ::testing::TestWithParam<bool> { | |
55 public: | |
56 // Validate the entry at |path_to_object| with the given | |
57 // |signature|. Depending on |managed_onc| the object is interpreted as a | |
58 // 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 | |
60 // used. | |
61 void ValidateInvalid(const std::string& path_to_object, | |
62 const std::string& path_to_repaired, | |
63 const OncValueSignature* signature, | |
64 bool managed_onc) { | |
65 scoped_ptr<Validator> validator; | |
66 if (GetParam()) | |
67 validator = CreateStrictValidator(managed_onc); | |
68 else | |
69 validator = CreateLiberalValidator(managed_onc); | |
70 | |
71 const base::DictionaryValue* object = NULL; | |
72 ASSERT_TRUE(invalid_->GetDictionary(path_to_object, &object)); | |
73 | |
74 scoped_ptr<base::DictionaryValue> actual_repaired = | |
75 validator->ValidateAndRepairObject(signature, *object); | |
76 if (GetParam() || path_to_repaired == "") { | |
77 EXPECT_EQ(NULL, actual_repaired.get()); | |
78 } else { | |
79 const base::DictionaryValue* expected_repaired = NULL; | |
80 invalid_->GetDictionary(path_to_repaired, &expected_repaired); | |
81 EXPECT_TRUE(test_utils::Equals(expected_repaired, actual_repaired.get())); | |
82 } | |
83 } | |
84 | |
85 virtual void SetUp() { | |
86 invalid_ = | |
87 test_utils::ReadTestDictionary("invalid_settings_with_repairs.json"); | |
88 } | |
89 | |
90 scoped_ptr<const base::DictionaryValue> invalid_; | |
91 }; | |
92 | |
93 TEST_P(ONCValidatorInvalidTest, UnknownFieldName) { | |
94 ValidateInvalid("network-unknown-fieldname", | |
95 "network-repaired", | |
96 &kNetworkConfigurationSignature, false); | |
97 ValidateInvalid("managed-network-unknown-fieldname", | |
98 "managed-network-repaired", | |
99 &kNetworkConfigurationSignature, true); | |
100 } | |
101 | |
102 TEST_P(ONCValidatorInvalidTest, UnknownType) { | |
103 ValidateInvalid("network-unknown-type", | |
104 "", | |
105 &kNetworkConfigurationSignature, false); | |
106 ValidateInvalid("managed-network-unknown-type", | |
107 "", | |
108 &kNetworkConfigurationSignature, true); | |
109 } | |
110 | |
111 TEST_P(ONCValidatorInvalidTest, UnknownRecommendedFieldName) { | |
112 ValidateInvalid("managed-network-unknown-recommended", | |
113 "managed-network-repaired", | |
114 &kNetworkConfigurationSignature, true); | |
115 } | |
116 | |
117 TEST_P(ONCValidatorInvalidTest, DictionaryRecommended) { | |
118 ValidateInvalid("managed-network-dict-recommended", | |
119 "managed-network-repaired", | |
120 &kNetworkConfigurationSignature, true); | |
121 } | |
122 | |
123 TEST_P(ONCValidatorInvalidTest, MissingRequiredField) { | |
124 ValidateInvalid("network-missing-required", | |
125 "network-missing-required", | |
126 &kNetworkConfigurationSignature, false); | |
127 ValidateInvalid("managed-network-missing-required", | |
128 "managed-network-missing-required", | |
129 &kNetworkConfigurationSignature, true); | |
130 } | |
131 | |
132 TEST_P(ONCValidatorInvalidTest, RecommendedInUnmanaged) { | |
133 ValidateInvalid("network-illegal-recommended", | |
134 "network-repaired", | |
135 &kNetworkConfigurationSignature, false); | |
136 } | |
137 | |
138 INSTANTIATE_TEST_CASE_P(ONCValidatorInvalidTest, | |
139 ONCValidatorInvalidTest, | |
140 ::testing::Bool()); | |
141 | |
142 } // namespace onc | |
143 } // namespace chromeos | |
OLD | NEW |