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