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

Side by Side Diff: chromeos/network/onc/onc_validator.h

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: Rebased (Greg moved ONC tools to chromeos/network/onc/). 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 #ifndef CHROMEOS_NETWORK_ONC_ONC_VALIDATOR_H_ 5 #ifndef CHROMEOS_NETWORK_ONC_ONC_VALIDATOR_H_
6 #define CHROMEOS_NETWORK_ONC_ONC_VALIDATOR_H_ 6 #define CHROMEOS_NETWORK_ONC_ONC_VALIDATOR_H_
7 7
8 #include <vector>
Joao da Silva 2012/12/13 09:51:35 Also <string>
pneubeck (no reviews) 2012/12/13 14:10:03 Done.
9
8 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
9 #include "chromeos/chromeos_export.h" 11 #include "chromeos/chromeos_export.h"
10 #include "chromeos/network/onc/onc_mapper.h" 12 #include "chromeos/network/onc/onc_mapper.h"
11 13
12 namespace base { 14 namespace base {
13 class Value; 15 class Value;
14 class DictionaryValue; 16 class DictionaryValue;
Joao da Silva 2012/12/13 09:51:35 nit: order
pneubeck (no reviews) 2012/12/13 14:10:03 Done.
15 } 17 }
16 18
17 namespace chromeos { 19 namespace chromeos {
18 namespace onc { 20 namespace onc {
19 21
20 struct OncValueSignature; 22 struct OncValueSignature;
21 23
22 class CHROMEOS_EXPORT Validator : public Mapper { 24 class CHROMEOS_EXPORT Validator : public Mapper {
23 public: 25 public:
26 enum Result {
27 VALID,
28 VALID_WITH_WARNINGS,
29 INVALID
30 };
31
24 // Creates a Validator that searches for the following invalid cases: 32 // Creates a Validator that searches for the following invalid cases:
pastarmovj 2012/12/13 10:15:44 This seems more like a documentation on the class
pneubeck (no reviews) 2012/12/13 14:10:03 Done and added a missing case in the comment.
25 // - a field name is found that is not part of the signature 33 // - a field name is found that is not part of the signature
26 // (controlled by |error_on_unknown_field|) 34 // (controlled by |error_on_unknown_field|)
27 // 35 //
28 // - a kRecommended array contains a field name that is not part of the 36 // - a kRecommended array contains a field name that is not part of the
29 // enclosing object's signature or if that field is dictionary typed 37 // enclosing object's signature or if that field is dictionary typed
30 // (controlled by |error_on_wrong_recommended|) 38 // (controlled by |error_on_wrong_recommended|)
31 // 39 //
32 // - |managed_onc| is false and a field with name kRecommended is found 40 // - |managed_onc| is false and a field with name kRecommended is found
33 // (always ignored) 41 // (always ignored)
34 // 42 //
35 // - a required field is missing (controlled by |error_on_missing_field|) 43 // - a required field is missing (controlled by |error_on_missing_field|)
36 // 44 //
37 // If one of these invalid cases occurs and the controlling flag is true, then 45 // If one of these invalid cases occurs and the controlling flag is true, then
38 // it is an error and the validation stops. The function 46 // it is an error. The function ValidateAndRepairObject sets |result| to
39 // ValidateAndRepairObject returns NULL. 47 // INVALID and returns NULL.
40 // 48 //
41 // If no error occurred, then a DeepCopy of the validated object is created, 49 // Otherwise, a DeepCopy of the validated object is created, which contains
42 // which contains all but the invalid fields and values. 50 // all but the invalid fields and values.
51 //
52 // If one of the invalid cases occurs and the controlling flag is false, then
53 // it is a warning. The function ValidateAndRepairObject sets |result| to
54 // VALID_WITH_WARNINGS and returns the repaired copy.
55 //
56 // If no error occurred, |result| is set to VALID and an exact DeepCopy is
57 // returned.
43 Validator(bool error_on_unknown_field, 58 Validator(bool error_on_unknown_field,
44 bool error_on_wrong_recommended, 59 bool error_on_wrong_recommended,
45 bool error_on_missing_field, 60 bool error_on_missing_field,
46 bool managed_onc); 61 bool managed_onc);
47 62
48 virtual ~Validator(); 63 virtual ~Validator();
49 64
50 // Validate the given |onc_object| according to |object_signature|. The 65 // Validate the given |onc_object| according to |object_signature|. The
51 // |object_signature| has to be a pointer to one of the signatures in 66 // |object_signature| has to be a pointer to one of the signatures in
52 // |onc_signature.h|. If an error is found, the function returns NULL. If 67 // |onc_signature.h|. If an error is found, the function returns NULL. If
53 // possible (no error encountered) a DeepCopy is created that contains all but 68 // possible (no error encountered) a DeepCopy is created that contains all but
54 // the invalid fields and values and returns this "repaired" object. 69 // the invalid fields and values and returns this "repaired" object.
55 // That means, if not handled as an error, then the following are ignored: 70 // That means, if not handled as an error, then the following are dropped from
71 // the copy:
56 // - unknown fields 72 // - unknown fields
57 // - invalid field names in kRecommended arrays 73 // - invalid field names in kRecommended arrays
58 // - kRecommended fields in an unmanaged ONC 74 // - kRecommended fields in an unmanaged ONC
59 // For details, see the comment at the Constructor. 75 // For details, see the comment at the Constructor.
60 scoped_ptr<base::DictionaryValue> ValidateAndRepairObject( 76 scoped_ptr<base::DictionaryValue> ValidateAndRepairObject(
61 const OncValueSignature* object_signature, 77 const OncValueSignature* object_signature,
62 const base::DictionaryValue& onc_object); 78 const base::DictionaryValue& onc_object,
79 Result* result);
63 80
64 private: 81 private:
65 // Overriden from Mapper: 82 // Overridden from Mapper:
66 // Compare |onc_value|s type with |onc_type| and validate/repair according to 83 // Compare |onc_value|s type with |onc_type| and validate/repair according to
67 // |signature|. On error returns NULL. 84 // |signature|. On error returns NULL.
68 virtual scoped_ptr<base::Value> MapValue( 85 virtual scoped_ptr<base::Value> MapValue(
69 const OncValueSignature& signature, 86 const OncValueSignature& signature,
70 const base::Value& onc_value) OVERRIDE; 87 const base::Value& onc_value,
88 bool* error) OVERRIDE;
71 89
72 // Dispatch to the right validation function according to 90 // Dispatch to the right validation function according to
73 // |signature|. Iterates over all fields and recursively validates/repairs 91 // |signature|. Iterates over all fields and recursively validates/repairs
74 // these. All valid fields are added to the result dictionary. Returns the 92 // these. All valid fields are added to the result dictionary. Returns the
75 // repaired dictionary. On error returns NULL. 93 // repaired dictionary. On error returns NULL.
76 virtual scoped_ptr<base::DictionaryValue> MapObject( 94 virtual scoped_ptr<base::DictionaryValue> MapObject(
77 const OncValueSignature& signature, 95 const OncValueSignature& signature,
78 const base::DictionaryValue& onc_object) OVERRIDE; 96 const base::DictionaryValue& onc_object,
97 bool* error) OVERRIDE;
98
99 // Pushes/pops the |field_name| to |path_|, otherwise like |Mapper::MapField|.
100 virtual scoped_ptr<base::Value> MapField(
101 const std::string& field_name,
102 const OncValueSignature& object_signature,
103 const base::Value& onc_value,
104 bool* found_unknown_field,
105 bool* error) OVERRIDE;
106
107 // Pushes/pops the index to |path_|, otherwise like |Mapper::MapEntry|.
108 virtual scoped_ptr<base::Value> MapEntry(
109 int index,
110 const OncValueSignature& signature,
111 const base::Value& onc_value,
112 bool* error) OVERRIDE;
79 113
80 // This is the default validation of objects/dictionaries. Validates 114 // This is the default validation of objects/dictionaries. Validates
81 // |onc_object| according to |object_signature|. |result| must point to a 115 // |onc_object| according to |object_signature|. |result| must point to a
82 // dictionary into which the repaired fields are written. 116 // dictionary into which the repaired fields are written.
83 bool ValidateObjectDefault( 117 bool ValidateObjectDefault(
84 const OncValueSignature& object_signature, 118 const OncValueSignature& object_signature,
85 const base::DictionaryValue& onc_object, 119 const base::DictionaryValue& onc_object,
86 base::DictionaryValue* result); 120 base::DictionaryValue* result);
87 121
88 // Validates/repairs the kRecommended array in |result| according to 122 // Validates/repairs the kRecommended array in |result| according to
89 // |object_signature| of the enclosing object. 123 // |object_signature| of the enclosing object.
90 bool ValidateRecommendedField( 124 bool ValidateRecommendedField(
91 const OncValueSignature& object_signature, 125 const OncValueSignature& object_signature,
92 base::DictionaryValue* result); 126 base::DictionaryValue* result);
93 127
128 bool ValidateToplevelConfiguration(
129 const base::DictionaryValue& onc_object,
130 base::DictionaryValue* result);
131
94 bool ValidateNetworkConfiguration( 132 bool ValidateNetworkConfiguration(
95 const base::DictionaryValue& onc_object, 133 const base::DictionaryValue& onc_object,
96 base::DictionaryValue* result); 134 base::DictionaryValue* result);
97 135
98 bool ValidateEthernet( 136 bool ValidateEthernet(
99 const base::DictionaryValue& onc_object, 137 const base::DictionaryValue& onc_object,
100 base::DictionaryValue* result); 138 base::DictionaryValue* result);
101 139
102 bool ValidateIPConfig( 140 bool ValidateIPConfig(
103 const base::DictionaryValue& onc_object, 141 const base::DictionaryValue& onc_object,
(...skipping 28 matching lines...) Expand all
132 base::DictionaryValue* result); 170 base::DictionaryValue* result);
133 171
134 bool ValidateEAP( 172 bool ValidateEAP(
135 const base::DictionaryValue& onc_object, 173 const base::DictionaryValue& onc_object,
136 base::DictionaryValue* result); 174 base::DictionaryValue* result);
137 175
138 bool ValidateCertificate( 176 bool ValidateCertificate(
139 const base::DictionaryValue& onc_object, 177 const base::DictionaryValue& onc_object,
140 base::DictionaryValue* result); 178 base::DictionaryValue* result);
141 179
180 bool FieldExistsAndHasNoValueOf(const base::DictionaryValue& object,
181 const std::string &field_name,
182 const char** valid_values);
183
184 bool FieldExistsAndIsNotInRange(const base::DictionaryValue& object,
185 const std::string &field_name,
186 int lower_bound,
187 int upper_bound);
188
189 bool RequireField(const base::DictionaryValue& dict, const std::string& key);
190
191 std::string WarningHeader();
192 std::string ErrorHeader();
193 std::string MessageHeader(bool is_error);
194
142 const bool error_on_unknown_field_; 195 const bool error_on_unknown_field_;
143 const bool error_on_wrong_recommended_; 196 const bool error_on_wrong_recommended_;
144 const bool error_on_missing_field_; 197 const bool error_on_missing_field_;
145 const bool managed_onc_; 198 const bool managed_onc_;
146 199
200 // The path of field names and indices to the current value. Indices
201 // are stored as strings in decimal notation.
202 std::vector<std::string> path_;
203
204 // Tracks if an error or warning occurred within validation initiated by
205 // function ValidateAndRepairObject.
206 bool error_or_warning_found_;
207
147 DISALLOW_COPY_AND_ASSIGN(Validator); 208 DISALLOW_COPY_AND_ASSIGN(Validator);
148 }; 209 };
149 210
150 } // namespace onc 211 } // namespace onc
151 } // namespace chromeos 212 } // namespace chromeos
152 213
153 #endif // CHROMEOS_NETWORK_ONC_ONC_VALIDATOR_H_ 214 #endif // CHROMEOS_NETWORK_ONC_ONC_VALIDATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698