summaryrefslogtreecommitdiff
path: root/src/components/policy/policy_external/src/policy_table/validation.cc
blob: 825fd20b848792d10b385c26ff7af293eb9d6a10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include <algorithm>
#include <iostream>
#include "policy/policy_table/types.h"
#include "utils/helpers.h"
#include "utils/logger.h"

namespace {
bool IsPredefinedApplication(const std::string& app_id) {
  using namespace rpc::policy_table_interface_base;
  return kPreDataConsentApp == app_id || kDefaultApp == app_id;
}
}  // namespace

namespace rpc {
namespace policy_table_interface_base {

SDL_CREATE_LOG_VARIABLE("Policy")

bool VerifyPredefinedApp(ApplicationPolicies::value_type& app_policies) {
  const std::string& app_id = app_policies.first;
  if (!IsPredefinedApplication(app_id)) {
    return true;
  }

  RequestTypes& predefined_request_types = *app_policies.second.RequestType;

  if (!predefined_request_types.is_valid()) {
    SDL_LOG_WARN(app_id << " policy invalid RequestTypes will be cleaned.");
    predefined_request_types.CleanUp();
    if (PT_PRELOADED == app_policies.second.GetPolicyTableType() &&
        predefined_request_types.is_cleaned_up()) {
      SDL_LOG_ERROR(
          app_id << " policy RequestTypes is empty after clean-up. Exiting.");
      return false;
    }

    SDL_LOG_WARN(app_id << " request types have cleaned up.");
  }
  return true;
}

bool PolicyBase::Validate() const {
  // Check for empty "groups" sub-sections
  if (groups.empty()) {
    return false;
  }
  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) {
    SDL_LOG_ERROR("Default or preData policy is not present.");
    return false;
  }

  // Device policy is mandatory
  if (!device.is_initialized()) {
    SDL_LOG_ERROR("Device policy is not present.");
    return false;
  }

  PolicyTableType pt_type = GetPolicyTableType();
  if (PT_PRELOADED != pt_type && PT_UPDATE != pt_type) {
    return true;
  }

  if (!VerifyPredefinedApp(*it_default_policy)) {
    return false;
  }

  if (!VerifyPredefinedApp(*it_pre_data_policy)) {
    return false;
  }

  ApplicationPolicies::iterator iter = apps.begin();
  ApplicationPolicies::iterator end_iter = apps.end();

  while (iter != end_iter) {
    const std::string app_id = iter->first;
    if (IsPredefinedApplication(app_id)) {
      ++iter;
      continue;
    }

    SDL_LOG_TRACE("Checking app Request Types...");
    RequestTypes& app_request_types = *iter->second.RequestType;

    if (app_request_types.is_omitted()) {
      SDL_LOG_WARN("RequestTypes omitted for "
                   << app_id << " Will be replaced with default.");
      app_request_types = *apps[kDefaultApp].RequestType;
      ++iter;
      continue;
    }

    if (!app_request_types.is_valid()) {
      SDL_LOG_WARN("Invalid RequestTypes for " << app_id
                                               << " Will be cleaned up.");
      app_request_types.CleanUp();
      if (app_request_types.is_cleaned_up()) {
        if (PT_PRELOADED == pt_type) {
          SDL_LOG_ERROR("RequestTypes empty after clean-up for "
                        << app_id << " Exiting.");
          return false;
        }

        SDL_LOG_WARN("RequestTypes empty after clean-up for "
                     << app_id << " Will be replaced with default.");

        app_request_types = *apps[kDefaultApp].RequestType;
      }

      SDL_LOG_DEBUG("Clean up for " << app_id << " is done.");

      ++iter;
      continue;
    }

    if (app_request_types.is_empty()) {
      SDL_LOG_WARN("RequestTypes is empty for " << app_id);
    }

    ++iter;
  }

  SDL_LOG_TRACE("Checking app Request SubTypes...");
  iter = apps.begin();
  while (iter != end_iter) {
    if (it_default_policy == iter || it_pre_data_policy == iter) {
      ++iter;
      continue;
    }
    ApplicationParams& app_params = (*iter).second;
    const bool is_request_subtype_omitted =
        !app_params.RequestSubType.is_initialized();

    if (is_request_subtype_omitted) {
      SDL_LOG_WARN(
          "App policy RequestSubTypes omitted."
          " Will be replaced with default.");
      app_params.RequestSubType = apps[kDefaultApp].RequestSubType;
      ++iter;
      continue;
    }

    const bool is_request_subtype_empty = app_params.RequestSubType->empty();
    if (is_request_subtype_empty) {
      SDL_LOG_WARN("App policy RequestSubTypes empty.");
    }
    ++iter;
  }

  return true;
}

bool ApplicationParams::ValidateModuleTypes() const {
  // moduleType is optional so see Optional<T>::is_valid()
  bool is_initialized = moduleType->is_initialized();
  if (!is_initialized) {
    // valid if not initialized
    return true;
  }
  bool is_valid = moduleType->is_valid();
  if (is_valid) {
    return true;
  }

  struct IsInvalid {
    bool operator()(Enum<ModuleType> item) const {
      return !item.is_valid();
    }
  };
  // cut invalid items
  moduleType->erase(
      std::remove_if(moduleType->begin(), moduleType->end(), IsInvalid()),
      moduleType->end());
  bool empty = moduleType->empty();
  if (empty) {
    // set non initialized value
    ModuleTypes non_initialized;
    moduleType = Optional<ModuleTypes>(non_initialized);
  }
  return true;
}

bool AppServiceHandledRpc::Validate() const {
  return true;
}

bool AppServiceInfo::Validate() const {
  return true;
}

bool ApplicationParams::Validate() const {
  if (is_initialized()) {
    if (preconsented_groups.is_initialized()) {
      const Strings& all = groups;
      const Strings& preconsented = *preconsented_groups;
      if (preconsented.size() > all.size()) {
        return false;
      }
    }
  }
  return ValidateModuleTypes();
}

bool RpcParameters::Validate() const {
  return true;
}
bool Rpcs::Validate() const {
  return true;
}

bool EndpointProperty::Validate() const {
  if (!version.is_valid()) {
    return false;
  }

  return true;
}

bool ModuleConfig::Validate() const {
  switch (GetPolicyTableType()) {
    case PT_PRELOADED: {
      if (helpers::Compare<bool, helpers::EQ, helpers::ONE>(
              true,
              vehicle_make.is_initialized(),
              vehicle_year.is_initialized(),
              vehicle_model.is_initialized())) {
        return false;
      }
      break;
    }
    case PT_UPDATE: {
      if (preloaded_pt->is_initialized()) {
        return false;
      }
      if (preloaded_date->is_initialized()) {
        return false;
      }
      break;
    }
    default:
      break;
  }

  for (ServiceEndpoints::const_iterator it_endpoints = endpoints.begin();
       it_endpoints != endpoints.end();
       ++it_endpoints) {
    const URLList& endpoint_list = it_endpoints->second;
    if (endpoint_list.end() == endpoint_list.find(kDefaultApp)) {
      SDL_LOG_ERROR("Endpoint " << it_endpoints->first
                                << "does not contain default group");
      return false;
    }
  }

  if (endpoint_properties.is_initialized()) {
    const auto& endpoint_property =
        endpoint_properties->find(kDefaultOemMappingServiceName);
    if (endpoint_properties->end() != endpoint_property) {
      return (*endpoint_property).second.version.is_initialized();
    }
  }

  return true;
}

bool MessageString::Validate() const {
  return true;
}
bool MessageLanguages::Validate() const {
  if (PT_SNAPSHOT == GetPolicyTableType()) {
    return false;
  }
  return true;
}
bool ConsumerFriendlyMessages::Validate() const {
  return true;
}
bool ModuleMeta::Validate() const {
  if (GetPolicyTableType() == PT_UPDATE ||
      GetPolicyTableType() == PT_PRELOADED) {
    return false;
  }
  return true;
}
bool AppLevel::Validate() const {
  if (PT_PRELOADED == GetPolicyTableType() ||
      PT_UPDATE == GetPolicyTableType()) {
    return false;
  }
  return true;
}
bool UsageAndErrorCounts::Validate() const {
  if (PT_PRELOADED == GetPolicyTableType() ||
      PT_UPDATE == GetPolicyTableType()) {
    return false;
  }
  return true;
}
bool ConsentRecords::Validate() const {
  if (PT_SNAPSHOT != GetPolicyTableType()) {
    return !external_consent_status_groups->is_initialized();
  }

  return true;
}
bool DeviceParams::Validate() const {
  return true;
}

bool VehicleDataItem::Validate() const {
  if (!ValidateNaming(std::string(name))) {
    return false;
  };

  if (!ValidateNaming(std::string(key))) {
    return false;
  };

  if (!ValidateTypes()) {
    SDL_LOG_ERROR("Unknown type: " << std::string(type) << " of "
                                   << std::string(key));
    return false;
  }
  return true;
}

bool VehicleData::Validate() const {
  const PolicyTableType policy_table_type = GetPolicyTableType();
  bool result = true;
  if (PT_SNAPSHOT == policy_table_type) {
    result =
        (!schema_items.is_initialized()) && schema_version.is_initialized();
  }
  if (PT_UPDATE == policy_table_type || PT_PRELOADED == policy_table_type) {
    result =
        (schema_version.is_initialized() && schema_items.is_initialized()) ||
        (!schema_version.is_initialized() && !schema_items.is_initialized());
  }
  return result;
}

bool PolicyTable::Validate() const {
  PolicyTableType policy_table_type = GetPolicyTableType();

  if (PT_PRELOADED == policy_table_type || PT_UPDATE == policy_table_type) {
    if (device_data.is_initialized()) {
      return false;
    }
  }

  if (PT_PRELOADED == policy_table_type || PT_SNAPSHOT == policy_table_type) {
    // Check upper bound of each "groups" sub section in the app policies
    const FunctionalGroupings::size_type functional_groupings_count =
        functional_groupings.size();
    for (ApplicationPolicies::const_iterator app_policiies_it =
             app_policies_section.apps.begin();
         app_policies_section.apps.end() != app_policiies_it;
         ++app_policiies_it) {
      if (app_policiies_it->second.groups.size() > functional_groupings_count) {
        return false;
      }
    }
  }

  return true;
}

bool Table::Validate() const {
  return true;
}
}  // namespace policy_table_interface_base
}  // namespace rpc