Chromium Code Reviews| 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chromeos/network/onc/onc_signature.h" | 15 #include "chromeos/network/onc/onc_signature.h" |
| 16 #include "components/onc/onc_constants.h" | 16 #include "components/onc/onc_constants.h" |
| 17 | 17 |
| 18 namespace chromeos { | 18 namespace chromeos { |
| 19 namespace onc { | 19 namespace onc { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 const int kMaximumSSIDLengthInBytes = 32; | |
|
pneubeck (no reviews)
2014/11/26 10:15:34
nit: add a comment where this comes from (802.11 s
cschuet (SLOW)
2014/11/27 11:03:58
Done.
| |
| 24 | |
| 23 template <typename T, size_t N> | 25 template <typename T, size_t N> |
| 24 std::vector<T> toVector(T const (&array)[N]) { | 26 std::vector<T> toVector(T const (&array)[N]) { |
| 25 return std::vector<T>(array, array + N); | 27 return std::vector<T>(array, array + N); |
| 26 } | 28 } |
| 27 | 29 |
| 28 // Copied from policy/configuration_policy_handler.cc. | 30 // Copied from policy/configuration_policy_handler.cc. |
| 29 // TODO(pneubeck): move to a common place like base/. | 31 // TODO(pneubeck): move to a common place like base/. |
| 30 std::string ValueTypeToString(base::Value::Type type) { | 32 std::string ValueTypeToString(base::Value::Type type) { |
| 31 const char* const strings[] = {"null", "boolean", "integer", "double", | 33 const char* const strings[] = {"null", "boolean", "integer", "double", |
| 32 "string", "binary", "dictionary", "list"}; | 34 "string", "binary", "dictionary", "list"}; |
| (...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 389 } | 391 } |
| 390 | 392 |
| 391 error_or_warning_found_ = true; | 393 error_or_warning_found_ = true; |
| 392 path_.push_back(field_name); | 394 path_.push_back(field_name); |
| 393 LOG(ERROR) << MessageHeader() << "Found an empty string, but expected a " | 395 LOG(ERROR) << MessageHeader() << "Found an empty string, but expected a " |
| 394 << "non-empty string."; | 396 << "non-empty string."; |
| 395 path_.pop_back(); | 397 path_.pop_back(); |
| 396 return true; | 398 return true; |
| 397 } | 399 } |
| 398 | 400 |
| 401 bool Validator::IsSSIDOrHexSSIDValid(const base::DictionaryValue& object, | |
| 402 const std::string& ssid_field_name, | |
|
pneubeck (no reviews)
2014/11/26 10:15:34
as you always pass the same strings for the field
cschuet (SLOW)
2014/11/27 11:03:58
Done.
| |
| 403 const std::string& hex_ssid_field_name) { | |
| 404 // SSID validity | |
|
pneubeck (no reviews)
2014/11/26 10:15:34
nit: use whole sentences (ending with fullstop).
cschuet (SLOW)
2014/11/27 11:03:58
Done.
| |
| 405 std::string ssid_string; | |
| 406 const base::Value* value = NULL; | |
| 407 if (object.GetWithoutPathExpansion(ssid_field_name, &value)) { | |
| 408 if (!value->GetAsString(&ssid_string)) { | |
|
pneubeck (no reviews)
2014/11/26 10:15:34
type is verified already by the generic/default ve
cschuet (SLOW)
2014/11/27 11:03:58
Done.
| |
| 409 LOG(ERROR) << MessageHeader() << "Field " << ssid_field_name | |
| 410 << " does not contain a string."; | |
| 411 error_or_warning_found_ = true; | |
| 412 return false; | |
| 413 } | |
| 414 if (ssid_string.size() <= 0 || | |
| 415 ssid_string.size() > kMaximumSSIDLengthInBytes) { | |
| 416 LOG(ERROR) << MessageHeader() << ssid_field_name | |
| 417 << " has an invalid length."; | |
| 418 error_or_warning_found_ = true; | |
| 419 return false; | |
| 420 } | |
| 421 } | |
| 422 | |
| 423 // HexSSID validity | |
| 424 std::string hex_ssid_string; | |
| 425 if (object.GetWithoutPathExpansion(hex_ssid_field_name, &value)) { | |
|
pneubeck (no reviews)
2014/11/26 10:15:34
use GetStringWithoutPathExpansion
cschuet (SLOW)
2014/11/27 11:03:58
Done.
| |
| 426 if (!value->GetAsString(&hex_ssid_string)) { | |
|
pneubeck (no reviews)
2014/11/26 10:15:34
again not required
cschuet (SLOW)
2014/11/27 11:03:58
Done.
| |
| 427 LOG(ERROR) << MessageHeader() << "Field " << hex_ssid_field_name | |
| 428 << " does not contain a string."; | |
| 429 error_or_warning_found_ = true; | |
| 430 return false; | |
| 431 } | |
| 432 std::vector<uint8> bytes; | |
| 433 if (!base::HexStringToBytes(hex_ssid_string, &bytes)) { | |
| 434 LOG(ERROR) << MessageHeader() << "Field " << hex_ssid_field_name | |
| 435 << " is not a valid hex representation: \"" << hex_ssid_string | |
| 436 << "\""; | |
| 437 error_or_warning_found_ = true; | |
| 438 return false; | |
| 439 } | |
| 440 if (bytes.size() <= 0 || bytes.size() > kMaximumSSIDLengthInBytes) { | |
| 441 LOG(ERROR) << MessageHeader() << hex_ssid_field_name | |
| 442 << " has an invalid length."; | |
| 443 error_or_warning_found_ = true; | |
| 444 return false; | |
| 445 } | |
| 446 } | |
| 447 | |
| 448 if (ssid_string.length() > 0 && hex_ssid_string.length() > 0) { | |
| 449 // both set | |
|
pneubeck (no reviews)
2014/11/26 10:15:34
nit: make the comment a bit more verbose:
"If bot
cschuet (SLOW)
2014/11/27 11:03:58
Done.
| |
| 450 std::string hexified = | |
| 451 base::HexEncode(ssid_string.c_str(), ssid_string.size()); | |
| 452 if (hexified != hex_ssid_string) { | |
| 453 LOG(ERROR) << MessageHeader() << "Fields " << ssid_field_name << " and " | |
| 454 << hex_ssid_field_name << " contain inconsistent values."; | |
| 455 error_or_warning_found_ = true; | |
| 456 return false; | |
| 457 } | |
| 458 } | |
| 459 | |
| 460 return true; | |
| 461 } | |
| 462 | |
| 399 bool Validator::RequireField(const base::DictionaryValue& dict, | 463 bool Validator::RequireField(const base::DictionaryValue& dict, |
| 400 const std::string& field_name) { | 464 const std::string& field_name) { |
| 401 if (dict.HasKey(field_name)) | 465 if (dict.HasKey(field_name)) |
| 402 return true; | 466 return true; |
| 403 std::string message = MessageHeader() + "The required field '" + field_name + | 467 std::string message = MessageHeader() + "The required field '" + field_name + |
| 404 "' is missing."; | 468 "' is missing."; |
| 405 if (error_on_missing_field_) { | 469 if (error_on_missing_field_) { |
| 406 error_or_warning_found_ = true; | 470 error_or_warning_found_ = true; |
| 407 LOG(ERROR) << message; | 471 LOG(ERROR) << message; |
| 408 } else { | 472 } else { |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 565 | 629 |
| 566 bool Validator::ValidateWiFi(base::DictionaryValue* result) { | 630 bool Validator::ValidateWiFi(base::DictionaryValue* result) { |
| 567 using namespace ::onc::wifi; | 631 using namespace ::onc::wifi; |
| 568 | 632 |
| 569 const char* const kValidSecurities[] = {kSecurityNone, kWEP_PSK, kWEP_8021X, | 633 const char* const kValidSecurities[] = {kSecurityNone, kWEP_PSK, kWEP_8021X, |
| 570 kWPA_PSK, kWPA_EAP}; | 634 kWPA_PSK, kWPA_EAP}; |
| 571 const std::vector<const char*> valid_securities(toVector(kValidSecurities)); | 635 const std::vector<const char*> valid_securities(toVector(kValidSecurities)); |
| 572 if (FieldExistsAndHasNoValidValue(*result, kSecurity, valid_securities)) | 636 if (FieldExistsAndHasNoValidValue(*result, kSecurity, valid_securities)) |
| 573 return false; | 637 return false; |
| 574 | 638 |
| 575 bool all_required_exist = | 639 // validate SSID and HexSSID fields |
|
pneubeck (no reviews)
2014/11/26 10:15:34
nit: start with capital case and end with fullstop
cschuet (SLOW)
2014/11/27 11:03:58
Done.
| |
| 576 RequireField(*result, kSecurity) && RequireField(*result, kSSID); | 640 if (!IsSSIDOrHexSSIDValid(*result, kSSID, kHexSSID)) |
| 641 return false; | |
| 642 | |
| 643 bool all_required_exist = RequireField(*result, kSecurity); | |
| 644 if (!result->HasKey(kSSID)) | |
|
pneubeck (no reviews)
2014/11/26 10:15:34
prepend a comment that says that "One of {kSSID, k
cschuet (SLOW)
2014/11/27 11:03:58
Done.
| |
| 645 all_required_exist &= RequireField(*result, kHexSSID); | |
| 646 if (!result->HasKey(kHexSSID)) | |
| 647 all_required_exist &= RequireField(*result, kSSID); | |
| 577 | 648 |
| 578 std::string security; | 649 std::string security; |
| 579 result->GetStringWithoutPathExpansion(kSecurity, &security); | 650 result->GetStringWithoutPathExpansion(kSecurity, &security); |
| 580 if (security == kWEP_8021X || security == kWPA_EAP) | 651 if (security == kWEP_8021X || security == kWPA_EAP) |
| 581 all_required_exist &= RequireField(*result, kEAP); | 652 all_required_exist &= RequireField(*result, kEAP); |
| 582 else if (security == kWEP_PSK || security == kWPA_PSK) | 653 else if (security == kWEP_PSK || security == kWPA_PSK) |
| 583 all_required_exist &= RequireField(*result, kPassphrase); | 654 all_required_exist &= RequireField(*result, kPassphrase); |
| 584 | 655 |
| 585 return !error_on_missing_field_ || all_required_exist; | 656 return !error_on_missing_field_ || all_required_exist; |
| 586 } | 657 } |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 844 } | 915 } |
| 845 | 916 |
| 846 std::string Validator::MessageHeader() { | 917 std::string Validator::MessageHeader() { |
| 847 std::string path = path_.empty() ? "toplevel" : JoinString(path_, "."); | 918 std::string path = path_.empty() ? "toplevel" : JoinString(path_, "."); |
| 848 std::string message = "At " + path + ": "; | 919 std::string message = "At " + path + ": "; |
| 849 return message; | 920 return message; |
| 850 } | 921 } |
| 851 | 922 |
| 852 } // namespace onc | 923 } // namespace onc |
| 853 } // namespace chromeos | 924 } // namespace chromeos |
| OLD | NEW |