summaryrefslogtreecommitdiff
path: root/src/components/application_manager/rpc_plugins/rc_rpc_plugin/test/rc_consent_manager_impl_test.cc
blob: a2e9f6d83baf84521346796b77d1934ac646c43b (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
/*
 * Copyright (c) 2019, Ford Motor Company
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the
 * distribution.
 *
 * Neither the name of the Ford Motor Company nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <ctime>
#include <memory>
#include <string>
#include <vector>

#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "application_manager/mock_application_manager_settings.h"
#include "rc_rpc_plugin/rc_consent_manager_impl.h"
#include "resumption/last_state_impl.h"
#include "resumption/last_state_wrapper_impl.h"
#include "utils/date_time.h"
#include "utils/file_system.h"

#include "application_manager/mock_application_manager.h"

using test::components::application_manager_test::MockApplicationManager;
using test::components::application_manager_test::
    MockApplicationManagerSettings;

using ::testing::NiceMock;
using ::testing::Return;
using ::testing::ReturnRef;

namespace {
const std::string kPolicyApp1Id = "app1_id";
const std::string kPolicyApp2Id = "app2_id";
const std::string kMacAddress1 = "device1";
const std::string kMacAddress2 = "device2";
const std::string kClimateModule = "CLIMATE";
const std::string kRadioModule = "RADIO";
const std::string kAppTestStorageFolder = "app_test_storage_folder";
const std::string kAppTestInfoStorage = "app_test_info_storage";
const uint32_t kPeriodOfConsentExpired = 30;  // in days
}  // namespace

namespace rc_rpc_plugin_test {
namespace rc_consent_manager_test {
using namespace rc_rpc_plugin::rc_rpc_types;
using namespace rc_rpc_plugin;

class RCConsentManagerImplTest : public ::testing::Test {
 public:
  RCConsentManagerImplTest()
      : current_date_(0u)
      , last_state_(std::make_shared<resumption::LastStateWrapperImpl>(
            std::make_shared<resumption::LastStateImpl>(kAppTestStorageFolder,
                                                        kAppTestInfoStorage)))
      , rc_consent_manager_(new rc_rpc_plugin::RCConsentManagerImpl(
            last_state_, mock_app_mngr_, kPeriodOfConsentExpired)) {}

  void SetUp() OVERRIDE {
    current_date_ = std::time(0);
    RemoveTemproraryTetsFiles();

    ON_CALL(mock_app_mngr_, get_settings())
        .WillByDefault(ReturnRef(mock_app_mnrg_settings_));
    ON_CALL(mock_app_mnrg_settings_, period_for_consent_expiration())
        .WillByDefault(Return(kPeriodOfConsentExpired));
  }

  void RemoveTemproraryTetsFiles() {
    // Remove exists storage file
    if (file_system::FileExists(kAppTestInfoStorage)) {
      file_system::DeleteFile(kAppTestInfoStorage);
    }

    // Remove exists storage folder
    if (file_system::DirectoryExists(kAppTestStorageFolder)) {
      file_system::RemoveDirectory(kAppTestStorageFolder);
    }

    auto last_state_accessor = last_state_->get_accessor();
    last_state_accessor.GetMutableData().set_dictionary(Json::Value());
  }

  void SaveStateToFileSystem() {
    auto last_state_accessor = last_state_->get_accessor();
    last_state_accessor.GetMutableData().SaveToFileSystem();
  }

 protected:
  time_t current_date_;
  NiceMock<MockApplicationManagerSettings> mock_app_mnrg_settings_;
  NiceMock<MockApplicationManager> mock_app_mngr_;
  resumption::LastStateWrapperPtr last_state_;
  std::unique_ptr<rc_rpc_plugin::RCConsentManagerImpl> rc_consent_manager_;
};

TEST_F(RCConsentManagerImplTest, SaveAndGetModuleConsents_SUCCESS) {
  const std::string radio_moduleId = "eb7739ea-b263-4fe1-af9c-9311d1acac2d";
  const std::string climate_moduleId = "34045662-a9dc-4823-8435-91056d4c26cb";

  ModuleIdConsent radio_module_consent{
      {kRadioModule, radio_moduleId}, ModuleConsent::CONSENTED, current_date_};

  ModuleIdConsent climate_module_consent{{kClimateModule, climate_moduleId},
                                         ModuleConsent::NOT_CONSENTED,
                                         current_date_};

  ModuleIdConsentVector module_consents{radio_module_consent,
                                        climate_module_consent};

  rc_consent_manager_->SaveModuleConsents(
      kPolicyApp1Id, kMacAddress1, module_consents);
  SaveStateToFileSystem();

  auto radio_consent = rc_consent_manager_->GetModuleConsent(
      kPolicyApp1Id, kMacAddress1, {kRadioModule, radio_moduleId});
  auto climate_consent = rc_consent_manager_->GetModuleConsent(
      kPolicyApp1Id, kMacAddress1, {kClimateModule, climate_moduleId});

  EXPECT_EQ(ModuleConsent::CONSENTED, radio_consent);
  EXPECT_EQ(ModuleConsent::NOT_CONSENTED, climate_consent);
}

TEST_F(RCConsentManagerImplTest, ModuleId_NOT_EXISTS) {
  const std::string radio_moduleId = "eb7739ea-b263-4fe1-af9c-9311d1acac2d";
  const std::string climate_moduleId = "34045662-a9dc-4823-8435-91056d4c26cb";

  ModuleIdConsent radio_module_consent{
      {kRadioModule, radio_moduleId}, ModuleConsent::CONSENTED, current_date_};
  ModuleIdConsent climate_module_consent{{kClimateModule, climate_moduleId},
                                         ModuleConsent::CONSENTED,
                                         current_date_};

  const std::string uknown_moduleId = "639f1b7f-1d25-4dca-a2ce-00530860adea";
  const std::string uknown_moduleType = "UNKNOWN_TYPE";

  ModuleUid unknown_module{uknown_moduleId, uknown_moduleType};

  ModuleIdConsentVector module_consents{radio_module_consent,
                                        climate_module_consent};

  rc_consent_manager_->SaveModuleConsents(
      kPolicyApp1Id, kMacAddress1, module_consents);
  SaveStateToFileSystem();

  ModuleConsent consent = rc_consent_manager_->GetModuleConsent(
      kPolicyApp1Id, kMacAddress1, unknown_module);

  EXPECT_EQ(ModuleConsent::NOT_EXISTS, consent);
}

TEST_F(RCConsentManagerImplTest, ConsentIsExpired) {
  time_t expired_date_45_days =
      current_date_ - (date_time::SECONDS_IN_DAY * 45);
  time_t expired_date_30_days =
      current_date_ - (date_time::SECONDS_IN_DAY * 30);
  time_t not_expired_date_29_days =
      current_date_ - (date_time::SECONDS_IN_DAY * 29);

  const std::string moduleId1 = "dbef7693-adf9-42f1-95c7-5dff314d9985";
  const std::string moduleId2 = "1536b2ac-213f-463f-8589-a11453cd3729";
  const std::string moduleId3 = "e8b8169a-9f29-4499-a60b-2d126a1beffc";

  ModuleUid module_resource1{kRadioModule, moduleId1};
  ModuleUid module_resource2{kRadioModule, moduleId2};
  ModuleUid module_resource3{kRadioModule, moduleId3};

  ModuleIdConsent module_consent_expired1{
      module_resource1, ModuleConsent::CONSENTED, expired_date_45_days};
  ModuleIdConsent module_consent_expired2{
      module_resource2, ModuleConsent::CONSENTED, expired_date_30_days};

  ModuleIdConsent module_consent_not_expired{
      module_resource3, ModuleConsent::CONSENTED, not_expired_date_29_days};

  ModuleIdConsentVector module_consents{module_consent_expired1,
                                        module_consent_expired2,
                                        module_consent_not_expired};
  rc_consent_manager_->SaveModuleConsents(
      kPolicyApp1Id, kMacAddress1, module_consents);
  SaveStateToFileSystem();

  // All consents, which consent has been saved more than 30 days, will be
  // removed.
  rc_consent_manager_->RemoveExpiredConsents();

  ModuleConsent expired_consent_45_days = rc_consent_manager_->GetModuleConsent(
      kPolicyApp1Id, kMacAddress1, module_resource1);
  EXPECT_EQ(ModuleConsent::NOT_EXISTS, expired_consent_45_days);

  ModuleConsent expired_consent_30_days = rc_consent_manager_->GetModuleConsent(
      kPolicyApp1Id, kMacAddress1, module_resource2);
  EXPECT_EQ(ModuleConsent::NOT_EXISTS, expired_consent_30_days);

  ModuleConsent not_expired_consent = rc_consent_manager_->GetModuleConsent(
      kPolicyApp1Id, kMacAddress1, module_resource3);
  EXPECT_EQ(ModuleConsent::CONSENTED, not_expired_consent);
}

TEST_F(RCConsentManagerImplTest,
       SaveModuleConsents_SameAppID_DifferentDeviceID_NotReplaced) {
  const std::string moduleId = "cef4eae6-e22c-4943-bdc3-60e792414c4c";
  ModuleUid module_resource{kRadioModule, moduleId};

  ModuleIdConsent radio_module_consent_app1_deviceId1{
      module_resource, ModuleConsent::CONSENTED, current_date_};

  ModuleIdConsent radio_module_consent_app1_deviceId2{
      module_resource, ModuleConsent::NOT_CONSENTED, current_date_};

  ModuleIdConsentVector module_consents_app1{
      radio_module_consent_app1_deviceId1};
  ModuleIdConsentVector module_consents_app2{
      radio_module_consent_app1_deviceId2};

  rc_consent_manager_->SaveModuleConsents(
      kPolicyApp1Id, kMacAddress1, module_consents_app1);

  rc_consent_manager_->SaveModuleConsents(
      kPolicyApp1Id, kMacAddress2, module_consents_app2);

  SaveStateToFileSystem();

  // Module consents for same app ids and different device_id (mac_adress) will
  // haven't been replaced each other

  ModuleConsent module_consent_deviceID1 =
      rc_consent_manager_->GetModuleConsent(
          kPolicyApp1Id, kMacAddress1, module_resource);

  ModuleConsent module_consent_deviceID2 =
      rc_consent_manager_->GetModuleConsent(
          kPolicyApp1Id, kMacAddress2, module_resource);

  EXPECT_EQ(ModuleConsent::CONSENTED, module_consent_deviceID1);

  //  // Module consent for app_id2 has different value as has been saved above
  EXPECT_EQ(ModuleConsent::NOT_CONSENTED, module_consent_deviceID2);
}

TEST_F(RCConsentManagerImplTest,
       SaveModuleConsents_SameAppID_SameDeviceID_Replaced) {
  const std::string moduleId = "194aee9e-26cb-4f48-a775-8841a658002d";

  ModuleUid radio_module_resource_consented{kRadioModule, moduleId};
  ModuleUid radio_module_resource_not_consented{kRadioModule, moduleId};

  ModuleIdConsent radio_module_consent_app1_deviceId1{
      radio_module_resource_consented, ModuleConsent::CONSENTED, current_date_};

  ModuleIdConsent radio_module_consent_app1_deviceId2{
      radio_module_resource_not_consented,
      ModuleConsent::NOT_CONSENTED,
      current_date_};

  ModuleIdConsentVector module_consents_app1{
      radio_module_consent_app1_deviceId1};
  ModuleIdConsentVector module_consents_app2{
      radio_module_consent_app1_deviceId2};

  rc_consent_manager_->SaveModuleConsents(
      kPolicyApp1Id, kMacAddress1, module_consents_app1);

  SaveStateToFileSystem();

  rc_consent_manager_->SaveModuleConsents(
      kPolicyApp1Id, kMacAddress1, module_consents_app2);

  SaveStateToFileSystem();

  // Module consents for same app ids and same device_id (mac_adress) will
  // have been replaced each other

  ModuleConsent module_consent = rc_consent_manager_->GetModuleConsent(
      kPolicyApp1Id, kMacAddress1, radio_module_resource_not_consented);

  EXPECT_EQ(ModuleConsent::NOT_CONSENTED, module_consent);
}

TEST_F(RCConsentManagerImplTest, RemoveAllModuleConsents_SUCCESS) {
  const std::string moduleId = "1c5408f2-9766-464c-a7bf-d834b62f43fa";

  ModuleUid module_resource{kRadioModule, moduleId};

  ModuleIdConsent radio_module_consent_app1_deviceId1{
      module_resource, ModuleConsent::CONSENTED, current_date_};

  ModuleIdConsent radio_module_consent_app1_deviceId2{
      module_resource, ModuleConsent::NOT_CONSENTED, current_date_};

  ModuleIdConsentVector module_consents_app1{
      radio_module_consent_app1_deviceId1};
  ModuleIdConsentVector module_consents_app2{
      radio_module_consent_app1_deviceId2};

  rc_consent_manager_->SaveModuleConsents(
      kPolicyApp1Id, kMacAddress1, module_consents_app1);

  rc_consent_manager_->SaveModuleConsents(
      kPolicyApp1Id, kMacAddress2, module_consents_app2);

  SaveStateToFileSystem();

  rc_consent_manager_->RemoveAllConsents();

  ModuleConsent module_consent_deviceID1 =
      rc_consent_manager_->GetModuleConsent(
          kPolicyApp1Id, kMacAddress1, module_resource);

  ModuleConsent module_consent_deviceID2 =
      rc_consent_manager_->GetModuleConsent(
          kPolicyApp1Id, kMacAddress2, module_resource);

  EXPECT_EQ(ModuleConsent::NOT_EXISTS, module_consent_deviceID1);

  EXPECT_EQ(ModuleConsent::NOT_EXISTS, module_consent_deviceID2);
}

}  // namespace rc_consent_manager_test
}  // namespace rc_rpc_plugin_test