diff options
author | Andrey Oleynik <aoleynik@luxoft.com> | 2015-05-18 11:29:37 +0300 |
---|---|---|
committer | Andrey Oleynik <aoleynik@luxoft.com> | 2015-06-17 10:53:00 +0300 |
commit | a118bca86a1e3e584fb23ac128d21fa7a256c258 (patch) | |
tree | 8f34dc95c7dc9c4a40e37ac7fbf7ae36ced1187e | |
parent | bec28cc3fe0f6563d20770d18789ae8c96787f1b (diff) | |
download | smartdevicelink-a118bca86a1e3e584fb23ac128d21fa7a256c258.tar.gz |
Edge cases validation for RequestType parameter is added.
3 files changed, 107 insertions, 5 deletions
diff --git a/src/components/policy/src/policy/policy_table/table_struct/types.cc b/src/components/policy/src/policy/policy_table/table_struct/types.cc index e91c18321..dcbd66dce 100644 --- a/src/components/policy/src/policy/policy_table/table_struct/types.cc +++ b/src/components/policy/src/policy/policy_table/table_struct/types.cc @@ -237,6 +237,8 @@ Json::Value ApplicationParams::ToJsonValue() const { return result__; } bool ApplicationParams::is_valid() const { + // RequestType is not validated since there is high-level validation logic, + // which takes into account information not available here. if (!PolicyBase::is_valid()) { return false; } @@ -246,9 +248,6 @@ bool ApplicationParams::is_valid() const { if (!AppHMIType.is_valid()) { return false; } - if (!RequestType.is_valid()) { - return false; - } if (!memory_kb.is_valid()) { return false; } diff --git a/src/components/policy/src/policy/policy_table/table_struct/types.h b/src/components/policy/src/policy/policy_table/table_struct/types.h index 814bcaefa..622775399 100644 --- a/src/components/policy/src/policy/policy_table/table_struct/types.h +++ b/src/components/policy/src/policy/policy_table/table_struct/types.h @@ -112,7 +112,7 @@ struct ApplicationParams : PolicyBase { struct ApplicationPoliciesSection : CompositeType { public: - ApplicationPolicies apps; + mutable ApplicationPolicies apps; DevicePolicy device; public: ApplicationPoliciesSection(); diff --git a/src/components/policy/src/policy/policy_table/table_struct/validation.cc b/src/components/policy/src/policy/policy_table/table_struct/validation.cc index 28ce4f13a..9cbccaea6 100644 --- a/src/components/policy/src/policy/policy_table/table_struct/validation.cc +++ b/src/components/policy/src/policy/policy_table/table_struct/validation.cc @@ -1,14 +1,117 @@ -// This file is generated, do not edit +#include <algorithm> #include "./types.h" +#include "utils/logger.h" + +namespace { +bool IsTypeInvalid(rpc::Enum<rpc::policy_table_interface_base::RequestType> request) { return !request.is_valid(); } +} namespace rpc { namespace policy_table_interface_base { + +CREATE_LOGGERPTR_GLOBAL(logger_, "PolicyTableValidation") + +void RemoveInvalidTypes(RequestTypes& types) { + types.erase( + std::remove_if(types.begin(), types.end(), &IsTypeInvalid), + types.end()); +} + bool PolicyBase::Validate() const { return true; } + bool ApplicationPoliciesSection::Validate() const { + ApplicationPolicies::iterator it_default_policy = + apps.find(kDefaultApp); + ApplicationPolicies::iterator it_pre_data_policy = + apps.find(kPreDataConsentApp); + + // Default and PreData policies are mandatory + if (apps.end() == it_default_policy || apps.end() == it_pre_data_policy) { + LOG4CXX_ERROR(logger_, "Default or preData policy is not present."); + return false; + } + + // Device policy is mandatory + if (!device.is_initialized()) { + LOG4CXX_ERROR(logger_, "Device policy is not present."); + return false; + } + + PolicyTableType pt_type = GetPolicyTableType(); + if (PT_PRELOADED != pt_type && PT_UPDATE != pt_type) { + return true; + } + + if (!it_default_policy->second.RequestType.is_valid()) { + LOG4CXX_WARN(logger_, + "Default policy RequestTypes are not valid. Will be cleaned."); + RemoveInvalidTypes(*it_default_policy->second.RequestType); + // If preloaded does not have valid default types - validation fails + // Otherwise default will be empty, i.e. all types allowed + if (PT_PRELOADED == pt_type) { + if (it_default_policy->second.RequestType->empty()) { + LOG4CXX_ERROR( + logger_, + "Default policy RequestTypes empty after clean-up. Exiting."); + return false; + } + } + } + + ApplicationPolicies::iterator iter = apps.begin(); + ApplicationPolicies::iterator end_iter = apps.end(); + + + while(iter != end_iter) { + ApplicationParams& app_params = (*iter).second; + bool is_request_type_ommited = !app_params.RequestType.is_initialized(); + bool is_request_type_valid = app_params.RequestType.is_valid(); + bool is_request_type_empty = app_params.RequestType->empty(); + + if (PT_PRELOADED == pt_type) { + if (!is_request_type_valid) { + LOG4CXX_WARN( + logger_, + "App policy RequestTypes are not valid. Will be cleaned."); + RemoveInvalidTypes(*app_params.RequestType); + if (app_params.RequestType->empty()) { + LOG4CXX_ERROR( + logger_, + "App policy RequestTypes empty after clean-up. Exiting."); + return false; + } + } + } else { + if (is_request_type_ommited) { + LOG4CXX_WARN(logger_, "App policy RequestTypes ommited." + " Will be replaced with default."); + app_params.RequestType = apps[kDefaultApp].RequestType; + continue; + } + if (!is_request_type_valid) { + LOG4CXX_WARN( + logger_, + "App policy RequestTypes are invalid. Will be cleaned."); + RemoveInvalidTypes(*app_params.RequestType); + if (app_params.RequestType->empty()) { + LOG4CXX_WARN(logger_, "App policy RequestTypes empty after clean-up." + " Will be replaced with default."); + app_params.RequestType = apps[kDefaultApp].RequestType; + continue; + } + } + if (is_request_type_empty) { + LOG4CXX_WARN(logger_, "App policy RequestTypes empty."); + } + } + ++iter; + } + return true; } + bool ApplicationParams::Validate() const { return true; } |