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

Side by Side Diff: chromeos/network/onc/onc_validator_unittest.cc

Issue 11578052: Replace OncNetworkParser by the new ONC translator. (Closed) Base URL: http://git.chromium.org/chromium/src.git@extend_onc_to_shill
Patch Set: Rebased. Created 7 years, 11 months 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
« no previous file with comments | « chromeos/network/onc/onc_validator.cc ('k') | chromeos/test/data/network/managed_toplevel1.onc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "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 #include <utility>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 12 matching lines...) Expand all
23 public: 23 public:
24 // Validate |onc_object| with the given |signature|. The object is considered 24 // Validate |onc_object| with the given |signature|. The object is considered
25 // to be managed if |managed_onc| is true. A strict validator is used if 25 // to be managed if |managed_onc| is true. A strict validator is used if
26 // |strict| is true. |onc_object| and the resulting repaired object of the 26 // |strict| is true. |onc_object| and the resulting repaired object of the
27 // validation is stored, so that expectations can be checked afterwards using 27 // validation is stored, so that expectations can be checked afterwards using
28 // one of the Expect* functions below. 28 // one of the Expect* functions below.
29 void Validate(bool strict, 29 void Validate(bool strict,
30 scoped_ptr<base::DictionaryValue> onc_object, 30 scoped_ptr<base::DictionaryValue> onc_object,
31 const OncValueSignature* signature, 31 const OncValueSignature* signature,
32 bool managed_onc) { 32 bool managed_onc) {
33 Validate(strict, onc_object.Pass(), signature, managed_onc,
34 ONC_SOURCE_NONE);
35 }
36
37 void Validate(bool strict,
38 scoped_ptr<base::DictionaryValue> onc_object,
39 const OncValueSignature* signature,
40 bool managed_onc,
41 ONCSource onc_source) {
33 scoped_ptr<Validator> validator; 42 scoped_ptr<Validator> validator;
34 if (strict) { 43 if (strict) {
35 // Create a strict validator that complains about every error. 44 // Create a strict validator that complains about every error.
36 validator.reset(new Validator(true, true, true, managed_onc)); 45 validator.reset(new Validator(true, true, true, managed_onc));
37 } else { 46 } else {
38 // Create a liberal validator that ignores or repairs non-critical errors. 47 // Create a liberal validator that ignores or repairs non-critical errors.
39 validator.reset(new Validator(false, false, false, managed_onc)); 48 validator.reset(new Validator(false, false, false, managed_onc));
40 } 49 }
50 validator->SetOncSource(onc_source);
41 original_object_ = onc_object.Pass(); 51 original_object_ = onc_object.Pass();
42 repaired_object_ = validator->ValidateAndRepairObject(signature, 52 repaired_object_ = validator->ValidateAndRepairObject(signature,
43 *original_object_, 53 *original_object_,
44 &validation_result_); 54 &validation_result_);
45 } 55 }
46 56
47 void ExpectValid() { 57 void ExpectValid() {
48 EXPECT_EQ(Validator::VALID, validation_result_); 58 EXPECT_EQ(Validator::VALID, validation_result_);
49 EXPECT_TRUE(test_utils::Equals(original_object_.get(), 59 EXPECT_TRUE(test_utils::Equals(original_object_.get(),
50 repaired_object_.get())); 60 repaired_object_.get()));
(...skipping 14 matching lines...) Expand all
65 Validator::Result validation_result_; 75 Validator::Result validation_result_;
66 scoped_ptr<const base::DictionaryValue> original_object_; 76 scoped_ptr<const base::DictionaryValue> original_object_;
67 scoped_ptr<const base::DictionaryValue> repaired_object_; 77 scoped_ptr<const base::DictionaryValue> repaired_object_;
68 }; 78 };
69 79
70 namespace { 80 namespace {
71 81
72 struct OncParams { 82 struct OncParams {
73 // |location_of_object| is a string to identify the object to be tested. It 83 // |location_of_object| is a string to identify the object to be tested. It
74 // may be used as a filename or as a dictionary key. 84 // may be used as a filename or as a dictionary key.
75 OncParams(std::string location_of_object, 85 OncParams(const std::string& location_of_object,
76 const OncValueSignature* onc_signature, 86 const OncValueSignature* onc_signature,
77 bool is_managed_onc) 87 bool is_managed_onc)
78 : location(location_of_object), 88 : location(location_of_object),
79 signature(onc_signature), 89 signature(onc_signature),
80 is_managed(is_managed_onc) { 90 is_managed(is_managed_onc) {
81 } 91 }
82 92
83 std::string location; 93 std::string location;
84 const OncValueSignature* signature; 94 const OncValueSignature* signature;
85 bool is_managed; 95 bool is_managed;
86 }; 96 };
87 97
88 ::std::ostream& operator<<(::std::ostream& os, const OncParams& onc) { 98 ::std::ostream& operator<<(::std::ostream& os, const OncParams& onc) {
89 return os << "(" << onc.location << ", " << onc.signature << ", " 99 return os << "(" << onc.location << ", " << onc.signature << ", "
90 << (onc.is_managed ? "managed" : "unmanaged") << ")"; 100 << (onc.is_managed ? "managed" : "unmanaged") << ")";
91 } 101 }
92 102
93 } // namespace 103 } // namespace
94 104
95 // Ensure that the constant |kEmptyUnencryptedConfiguration| describes a valid 105 // Ensure that the constant |kEmptyUnencryptedConfiguration| describes a valid
96 // ONC toplevel object. 106 // ONC toplevel object.
97 TEST_F(ONCValidatorTest, EmptyUnencryptedConfiguration) { 107 TEST_F(ONCValidatorTest, EmptyUnencryptedConfiguration) {
98 Validate(true, ReadDictionaryFromJson(kEmptyUnencryptedConfiguration), 108 Validate(true, ReadDictionaryFromJson(kEmptyUnencryptedConfiguration),
99 &kToplevelConfigurationSignature, false); 109 &kToplevelConfigurationSignature, false);
100 ExpectValid(); 110 ExpectValid();
101 } 111 }
102 112
113 // Ensure that VPN is rejected in device policies.
114 TEST_F(ONCValidatorTest, VPNInDevicePolicyInvalid) {
115 Validate(true, test_utils::ReadTestDictionary("valid_openvpn.onc"),
116 &kNetworkConfigurationSignature, true, ONC_SOURCE_DEVICE_POLICY);
117 ExpectInvalid();
118 }
119
120 // Ensure that client certificate patterns are rejected in device policies.
121 TEST_F(ONCValidatorTest, ClientCertPatternInDevicePolicyInvalid) {
122 Validate(true, test_utils::ReadTestDictionary("valid_wifi_clientcert.onc"),
123 &kNetworkConfigurationSignature, true, ONC_SOURCE_DEVICE_POLICY);
124 ExpectInvalid();
125 }
126
127 // Check that at least one configuration is accepted for device policies.
128 TEST_F(ONCValidatorTest, ValidNetworkInDevicePolicy) {
129 Validate(true, test_utils::ReadTestDictionary("valid_wifi_psk.onc"),
130 &kNetworkConfigurationSignature, true, ONC_SOURCE_DEVICE_POLICY);
131 ExpectValid();
132 }
133
103 // This test case is about validating valid ONC objects without any errors. Both 134 // This test case is about validating valid ONC objects without any errors. Both
104 // the strict and the liberal validator accept the object. 135 // the strict and the liberal validator accept the object.
105 class ONCValidatorValidTest : public ONCValidatorTest, 136 class ONCValidatorValidTest : public ONCValidatorTest,
106 public ::testing::WithParamInterface<OncParams> { 137 public ::testing::WithParamInterface<OncParams> {
107 }; 138 };
108 139
109 TEST_P(ONCValidatorValidTest, StrictValidationValid) { 140 TEST_P(ONCValidatorValidTest, StrictValidationValid) {
110 OncParams onc = GetParam(); 141 OncParams onc = GetParam();
111 Validate(true, test_utils::ReadTestDictionary(onc.location), onc.signature, 142 Validate(true, test_utils::ReadTestDictionary(onc.location), onc.signature,
112 onc.is_managed); 143 onc.is_managed);
(...skipping 13 matching lines...) Expand all
126 // bool: true if the ONC is managed). 157 // bool: true if the ONC is managed).
127 INSTANTIATE_TEST_CASE_P( 158 INSTANTIATE_TEST_CASE_P(
128 ONCValidatorValidTest, 159 ONCValidatorValidTest,
129 ONCValidatorValidTest, 160 ONCValidatorValidTest,
130 ::testing::Values(OncParams("managed_toplevel1.onc", 161 ::testing::Values(OncParams("managed_toplevel1.onc",
131 &kToplevelConfigurationSignature, 162 &kToplevelConfigurationSignature,
132 true), 163 true),
133 OncParams("managed_toplevel2.onc", 164 OncParams("managed_toplevel2.onc",
134 &kToplevelConfigurationSignature, 165 &kToplevelConfigurationSignature,
135 true), 166 true),
136 // Test a configuration generated by CPanel. 167 OncParams("toplevel_wifi_wpa_psk.onc",
137 OncParams("managed_toplevel_cpanel.onc",
138 &kToplevelConfigurationSignature, 168 &kToplevelConfigurationSignature,
139 true), 169 false),
140 OncParams("encrypted.onc", 170 OncParams("encrypted.onc",
141 &kToplevelConfigurationSignature, 171 &kToplevelConfigurationSignature,
142 true), 172 true),
143 OncParams("managed_vpn.onc", 173 OncParams("managed_vpn.onc",
144 &kNetworkConfigurationSignature, 174 &kNetworkConfigurationSignature,
145 true), 175 true),
146 OncParams("managed_ethernet.onc", 176 OncParams("managed_ethernet.onc",
147 &kNetworkConfigurationSignature, 177 &kNetworkConfigurationSignature,
148 true))); 178 true)));
149 179
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 RepairParams("", "")), 342 RepairParams("", "")),
313 std::make_pair(OncParams("network-wrong-type", 343 std::make_pair(OncParams("network-wrong-type",
314 &kNetworkConfigurationSignature, false), 344 &kNetworkConfigurationSignature, false),
315 RepairParams("", "")), 345 RepairParams("", "")),
316 std::make_pair(OncParams("managed-network-wrong-type", 346 std::make_pair(OncParams("managed-network-wrong-type",
317 &kNetworkConfigurationSignature, true), 347 &kNetworkConfigurationSignature, true),
318 RepairParams("", "")))); 348 RepairParams("", ""))));
319 349
320 } // namespace onc 350 } // namespace onc
321 } // namespace chromeos 351 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/network/onc/onc_validator.cc ('k') | chromeos/test/data/network/managed_toplevel1.onc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698