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 #include "chromeos/network/onc/onc_validator.h" | 5 #include "chromeos/network/onc/onc_validator.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 12 matching lines...) Expand all Loading... | |
23 namespace { | 23 namespace { |
24 | 24 |
25 // According to the IEEE 802.11 standard the SSID is a series of 0 to 32 octets. | 25 // According to the IEEE 802.11 standard the SSID is a series of 0 to 32 octets. |
26 const int kMaximumSSIDLengthInBytes = 32; | 26 const int kMaximumSSIDLengthInBytes = 32; |
27 | 27 |
28 template <typename T, size_t N> | 28 template <typename T, size_t N> |
29 std::vector<T> toVector(T const (&array)[N]) { | 29 std::vector<T> toVector(T const (&array)[N]) { |
30 return std::vector<T>(array, array + N); | 30 return std::vector<T>(array, array + N); |
31 } | 31 } |
32 | 32 |
33 // Copied from policy/configuration_policy_handler.cc. | |
34 // TODO(pneubeck): move to a common place like base/. | |
35 std::string ValueTypeToString(base::Value::Type type) { | |
36 const char* const strings[] = {"null", "boolean", "integer", "double", | |
37 "string", "binary", "dictionary", "list"}; | |
38 CHECK(static_cast<size_t>(type) < arraysize(strings)); | |
39 return strings[type]; | |
40 } | |
41 | |
42 } // namespace | 33 } // namespace |
43 | 34 |
44 Validator::Validator(bool error_on_unknown_field, | 35 Validator::Validator(bool error_on_unknown_field, |
45 bool error_on_wrong_recommended, | 36 bool error_on_wrong_recommended, |
46 bool error_on_missing_field, | 37 bool error_on_missing_field, |
47 bool managed_onc) | 38 bool managed_onc) |
48 : error_on_unknown_field_(error_on_unknown_field), | 39 : error_on_unknown_field_(error_on_unknown_field), |
49 error_on_wrong_recommended_(error_on_wrong_recommended), | 40 error_on_wrong_recommended_(error_on_wrong_recommended), |
50 error_on_missing_field_(error_on_missing_field), | 41 error_on_missing_field_(error_on_missing_field), |
51 managed_onc_(managed_onc), | 42 managed_onc_(managed_onc), |
(...skipping 11 matching lines...) Expand all Loading... | |
63 bool error = false; | 54 bool error = false; |
64 std::unique_ptr<base::Value> result_value = | 55 std::unique_ptr<base::Value> result_value = |
65 MapValue(*object_signature, onc_object, &error); | 56 MapValue(*object_signature, onc_object, &error); |
66 if (error) { | 57 if (error) { |
67 *result = INVALID; | 58 *result = INVALID; |
68 result_value.reset(); | 59 result_value.reset(); |
69 } else if (error_or_warning_found_) { | 60 } else if (error_or_warning_found_) { |
70 *result = VALID_WITH_WARNINGS; | 61 *result = VALID_WITH_WARNINGS; |
71 } | 62 } |
72 // The return value should be NULL if, and only if, |result| equals INVALID. | 63 // The return value should be NULL if, and only if, |result| equals INVALID. |
73 DCHECK_EQ(result_value.get() == NULL, *result == INVALID); | 64 DCHECK_EQ(!result_value, *result == INVALID); |
74 | 65 return base::DictionaryValue::From(result_value); |
Lei Zhang
2016/07/14 10:26:41
forgot a std::move(), will load new patch set late
pneubeck (no reviews)
2016/07/14 18:51:37
Slight difference to the old version is the droppe
Lei Zhang
2016/07/15 01:27:18
base::DictionaryValue::From() internally does the
| |
75 base::DictionaryValue* result_dict = NULL; | |
76 if (result_value) { | |
77 result_value.release()->GetAsDictionary(&result_dict); | |
78 CHECK(result_dict); | |
79 } | |
80 | |
81 return base::WrapUnique(result_dict); | |
82 } | 66 } |
83 | 67 |
84 std::unique_ptr<base::Value> Validator::MapValue( | 68 std::unique_ptr<base::Value> Validator::MapValue( |
85 const OncValueSignature& signature, | 69 const OncValueSignature& signature, |
86 const base::Value& onc_value, | 70 const base::Value& onc_value, |
87 bool* error) { | 71 bool* error) { |
88 if (onc_value.GetType() != signature.onc_type) { | 72 if (onc_value.GetType() != signature.onc_type) { |
89 LOG(ERROR) << MessageHeader() << "Found value '" << onc_value | 73 LOG(ERROR) << MessageHeader() << "Found value '" << onc_value |
90 << "' of type '" << ValueTypeToString(onc_value.GetType()) | 74 << "' of type '" << base::Value::GetTypeName(onc_value.GetType()) |
91 << "', but type '" << ValueTypeToString(signature.onc_type) | 75 << "', but type '" |
76 << base::Value::GetTypeName(signature.onc_type) | |
92 << "' is required."; | 77 << "' is required."; |
93 error_or_warning_found_ = *error = true; | 78 error_or_warning_found_ = *error = true; |
94 return std::unique_ptr<base::Value>(); | 79 return std::unique_ptr<base::Value>(); |
95 } | 80 } |
96 | 81 |
97 std::unique_ptr<base::Value> repaired = | 82 std::unique_ptr<base::Value> repaired = |
98 Mapper::MapValue(signature, onc_value, error); | 83 Mapper::MapValue(signature, onc_value, error); |
99 if (repaired) | 84 if (repaired) |
100 CHECK_EQ(repaired->GetType(), signature.onc_type); | 85 CHECK_EQ(repaired->GetType(), signature.onc_type); |
101 return repaired; | 86 return repaired; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 valid = ValidateProxySettings(repaired.get()); | 124 valid = ValidateProxySettings(repaired.get()); |
140 } else if (&signature == &kProxyLocationSignature) { | 125 } else if (&signature == &kProxyLocationSignature) { |
141 valid = ValidateProxyLocation(repaired.get()); | 126 valid = ValidateProxyLocation(repaired.get()); |
142 } else if (&signature == &kEAPSignature) { | 127 } else if (&signature == &kEAPSignature) { |
143 valid = ValidateEAP(repaired.get()); | 128 valid = ValidateEAP(repaired.get()); |
144 } else if (&signature == &kCertificateSignature) { | 129 } else if (&signature == &kCertificateSignature) { |
145 valid = ValidateCertificate(repaired.get()); | 130 valid = ValidateCertificate(repaired.get()); |
146 } | 131 } |
147 } | 132 } |
148 | 133 |
149 if (valid) { | 134 if (valid) |
150 return repaired; | 135 return repaired; |
151 } else { | 136 |
152 DCHECK(error_or_warning_found_); | 137 DCHECK(error_or_warning_found_); |
153 error_or_warning_found_ = *error = true; | 138 error_or_warning_found_ = *error = true; |
154 return std::unique_ptr<base::DictionaryValue>(); | 139 return std::unique_ptr<base::DictionaryValue>(); |
155 } | |
156 } | 140 } |
157 | 141 |
158 std::unique_ptr<base::Value> Validator::MapField( | 142 std::unique_ptr<base::Value> Validator::MapField( |
159 const std::string& field_name, | 143 const std::string& field_name, |
160 const OncValueSignature& object_signature, | 144 const OncValueSignature& object_signature, |
161 const base::Value& onc_value, | 145 const base::Value& onc_value, |
162 bool* found_unknown_field, | 146 bool* found_unknown_field, |
163 bool* error) { | 147 bool* error) { |
164 path_.push_back(field_name); | 148 path_.push_back(field_name); |
165 bool current_field_unknown = false; | 149 bool current_field_unknown = false; |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
281 | 265 |
282 if (found_error) { | 266 if (found_error) { |
283 error_or_warning_found_ = true; | 267 error_or_warning_found_ = true; |
284 path_.push_back(::onc::kRecommended); | 268 path_.push_back(::onc::kRecommended); |
285 std::string message = MessageHeader() + "The " + error_cause + | 269 std::string message = MessageHeader() + "The " + error_cause + |
286 " field '" + field_name + "' cannot be recommended."; | 270 " field '" + field_name + "' cannot be recommended."; |
287 path_.pop_back(); | 271 path_.pop_back(); |
288 if (error_on_wrong_recommended_) { | 272 if (error_on_wrong_recommended_) { |
289 LOG(ERROR) << message; | 273 LOG(ERROR) << message; |
290 return false; | 274 return false; |
291 } else { | |
292 LOG(WARNING) << message; | |
293 continue; | |
294 } | 275 } |
276 | |
277 LOG(WARNING) << message; | |
278 continue; | |
295 } | 279 } |
296 | 280 |
297 repaired_recommended->AppendString(field_name); | 281 repaired_recommended->AppendString(field_name); |
298 } | 282 } |
299 | 283 |
300 result->Set(::onc::kRecommended, repaired_recommended.release()); | 284 result->Set(::onc::kRecommended, repaired_recommended.release()); |
301 return true; | 285 return true; |
302 } | 286 } |
303 | 287 |
304 bool Validator::ValidateClientCertFields(bool allow_cert_type_none, | 288 bool Validator::ValidateClientCertFields(bool allow_cert_type_none, |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
439 std::string ssid_string; | 423 std::string ssid_string; |
440 if (object->GetStringWithoutPathExpansion(::onc::wifi::kSSID, &ssid_string) && | 424 if (object->GetStringWithoutPathExpansion(::onc::wifi::kSSID, &ssid_string) && |
441 (ssid_string.size() <= 0 || | 425 (ssid_string.size() <= 0 || |
442 ssid_string.size() > kMaximumSSIDLengthInBytes)) { | 426 ssid_string.size() > kMaximumSSIDLengthInBytes)) { |
443 error_or_warning_found_ = true; | 427 error_or_warning_found_ = true; |
444 const std::string msg = | 428 const std::string msg = |
445 MessageHeader() + ::onc::wifi::kSSID + " has an invalid length."; | 429 MessageHeader() + ::onc::wifi::kSSID + " has an invalid length."; |
446 // If the HexSSID field is present, ignore errors in SSID because these | 430 // If the HexSSID field is present, ignore errors in SSID because these |
447 // might be caused by the usage of a non-UTF-8 encoding when the SSID | 431 // might be caused by the usage of a non-UTF-8 encoding when the SSID |
448 // field was automatically added (see FillInHexSSIDField). | 432 // field was automatically added (see FillInHexSSIDField). |
449 if (object->HasKey(::onc::wifi::kHexSSID)) { | 433 if (!object->HasKey(::onc::wifi::kHexSSID)) { |
450 LOG(WARNING) << msg; | |
451 } else { | |
452 LOG(ERROR) << msg; | 434 LOG(ERROR) << msg; |
453 return false; | 435 return false; |
454 } | 436 } |
437 LOG(WARNING) << msg; | |
455 } | 438 } |
456 | 439 |
457 // Check HexSSID validity. | 440 // Check HexSSID validity. |
458 std::string hex_ssid_string; | 441 std::string hex_ssid_string; |
459 if (object->GetStringWithoutPathExpansion(::onc::wifi::kHexSSID, | 442 if (object->GetStringWithoutPathExpansion(::onc::wifi::kHexSSID, |
460 &hex_ssid_string)) { | 443 &hex_ssid_string)) { |
461 std::vector<uint8_t> decoded_ssid; | 444 std::vector<uint8_t> decoded_ssid; |
462 if (!base::HexStringToBytes(hex_ssid_string, &decoded_ssid)) { | 445 if (!base::HexStringToBytes(hex_ssid_string, &decoded_ssid)) { |
463 LOG(ERROR) << MessageHeader() << "Field " << ::onc::wifi::kHexSSID | 446 LOG(ERROR) << MessageHeader() << "Field " << ::onc::wifi::kHexSSID |
464 << " is not a valid hex representation: \"" << hex_ssid_string | 447 << " is not a valid hex representation: \"" << hex_ssid_string |
(...skipping 568 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1033 } | 1016 } |
1034 | 1017 |
1035 std::string Validator::MessageHeader() { | 1018 std::string Validator::MessageHeader() { |
1036 std::string path = path_.empty() ? "toplevel" : base::JoinString(path_, "."); | 1019 std::string path = path_.empty() ? "toplevel" : base::JoinString(path_, "."); |
1037 std::string message = "At " + path + ": "; | 1020 std::string message = "At " + path + ": "; |
1038 return message; | 1021 return message; |
1039 } | 1022 } |
1040 | 1023 |
1041 } // namespace onc | 1024 } // namespace onc |
1042 } // namespace chromeos | 1025 } // namespace chromeos |
OLD | NEW |