1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gmock/gmock.h>
17 #include <gtest/gtest.h>
18
19 #include "cloud_pref_impl.h"
20 #include "utils_log.h"
21
22 namespace OHOS::FileManagement::CloudSync::Test {
23 using namespace testing;
24 using namespace testing::ext;
25 using namespace std;
26 using namespace NativePreferences;
27 constexpr int32_t CURRENT_USER = 100;
28 class CloudPrefImplTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31 static void TearDownTestCase(void);
32 void SetUp();
33 void TearDown();
34 static inline std::shared_ptr<CloudPrefImpl> cloudPtr_{nullptr};
35 };
36
SetUpTestCase(void)37 void CloudPrefImplTest::SetUpTestCase(void)
38 {
39 GTEST_LOG_(INFO) << "SetUpTestCase";
40 }
41
TearDownTestCase(void)42 void CloudPrefImplTest::TearDownTestCase(void)
43 {
44 GTEST_LOG_(INFO) << "TearDownTestCase";
45 }
46
SetUp(void)47 void CloudPrefImplTest::SetUp(void)
48 {
49 auto preference = std::make_shared<Preferences>();
50 auto preferencesHelper = NativePreferences::PreferencesHelper::GetInstance();
51 EXPECT_CALL(*preferencesHelper, GetPreferences(_, _)).WillOnce(Return(preference));
52 std::string fileName = "test";
53 cloudPtr_ = std::make_shared<CloudPrefImpl>(fileName);
54 if (cloudPtr_ == nullptr) {
55 GTEST_LOG_(INFO) << "cloudPtr_ == nullptr";
56 }
57 if (cloudPtr_->pref_ == nullptr) {
58 GTEST_LOG_(INFO) << "cloudPtr_->pref == nullptr";
59 }
60 GTEST_LOG_(INFO) << "SetUp";
61 }
62
TearDown(void)63 void CloudPrefImplTest::TearDown(void)
64 {
65 NativePreferences::PreferencesHelper::DeleteInstance();
66 cloudPtr_.reset();
67 cloudPtr_ = nullptr;
68 GTEST_LOG_(INFO) << "TearDown";
69 }
70
71 /**
72 * @tc.name: CloudPrefImplTest001
73 * @tc.desc: Verify the CloudPrefImpl function
74 * @tc.type: FUNC
75 * @tc.require: I6H5MH
76 */
77 HWTEST_F(CloudPrefImplTest, CloudPrefImpTest001, TestSize.Level1)
78 {
79 GTEST_LOG_(INFO) << "CloudPrefImpTest001 Start";
80 try {
81 NativePreferences::PreferencesHelper::DeleteInstance();
82 CloudPrefImpl cloudPreImpl("");
83 EXPECT_EQ(cloudPreImpl.pref_, nullptr);
84 } catch (...) {
85 EXPECT_TRUE(false);
86 GTEST_LOG_(INFO) << " CloudPrefImpTest001 ERROR";
87 }
88 GTEST_LOG_(INFO) << "CloudPrefImpTest001 End";
89 }
90
91 /**
92 * @tc.name: CloudPrefImplTest002
93 * @tc.desc: Verify the CloudPrefImpl function
94 * @tc.type: FUNC
95 * @tc.require: I6H5MH
96 */
97 HWTEST_F(CloudPrefImplTest, CloudPrefImpTest002, TestSize.Level1)
98 {
99 GTEST_LOG_(INFO) << "CloudPrefImpTest002 Start";
100 try {
101 auto preferences = std::make_shared<Preferences>();
102 auto preferencesHelper = NativePreferences::PreferencesHelper::GetInstance();
103 EXPECT_CALL(*preferencesHelper, GetPreferences(_, _)).WillOnce(Return(preferences));
104 const std::string bundleName = "";
105 std::string tableName = "testTable";
106 CloudPrefImpl cloudPreImpl(CURRENT_USER, bundleName, tableName);
107 EXPECT_NE(cloudPreImpl.pref_, nullptr);
108 } catch (...) {
109 EXPECT_TRUE(false);
110 GTEST_LOG_(INFO) << " CloudPrefImpTest002 ERROR";
111 }
112 GTEST_LOG_(INFO) << "CloudPrefImpTest002 End";
113 }
114
115 /**
116 * @tc.name: CloudPrefImplTest003
117 * @tc.desc: Verify the CloudPrefImpl function
118 * @tc.type: FUNC
119 * @tc.require: I6H5MH
120 */
121 HWTEST_F(CloudPrefImplTest, CloudPrefImplTest003, TestSize.Level1)
122 {
123 GTEST_LOG_(INFO) << "CloudPrefImplTest003 Start";
124 try {
125 NativePreferences::PreferencesHelper::DeleteInstance();
126 const std::string bundleName = "";
127 std::string tableName = "testTable";
128 CloudPrefImpl cloudPrefImpl(CURRENT_USER, bundleName, tableName);
129 EXPECT_EQ(cloudPrefImpl.pref_, nullptr);
130 } catch (...) {
131 EXPECT_TRUE(false);
132 GTEST_LOG_(INFO) << " CloudPrefImplTest003 ERROR";
133 }
134 GTEST_LOG_(INFO) << "CloudPrefImplTest003 End";
135 }
136
137 /**
138 * @tc.name: SetStringTest001
139 * @tc.desc: Verify the SetString function
140 * @tc.type: FUNC
141 * @tc.require: I6H5MH
142 */
143 HWTEST_F(CloudPrefImplTest, SetStringTest001, TestSize.Level1)
144 {
145 GTEST_LOG_(INFO) << "SetStringTest001 Start";
146 try {
147 EXPECT_CALL(*(cloudPtr_->pref_), PutString(_, _)).WillOnce(Return(0));
148 EXPECT_CALL(*(cloudPtr_->pref_), FlushSync()).WillOnce(Return(-1));
149 std::string key;
150 std::string value;
151 cloudPtr_->SetString(key, value);
152 EXPECT_TRUE(true);
153 } catch (...) {
154 EXPECT_TRUE(false);
155 GTEST_LOG_(INFO) << " SetStringTest001 ERROR";
156 }
157 GTEST_LOG_(INFO) << "SetStringTest001 End";
158 }
159
160 /**
161 * @tc.name: SetStringTest002
162 * @tc.desc: Verify the SetString function
163 * @tc.type: FUNC
164 * @tc.require: I6H5MH
165 */
166 HWTEST_F(CloudPrefImplTest, SetStringTest002, TestSize.Level1)
167 {
168 GTEST_LOG_(INFO) << "SetStringTest002 Start";
169 try {
170 NativePreferences::PreferencesHelper::DeleteInstance();
171 CloudPrefImpl cloudPrefImpl("");
172 std::string key;
173 std::string value;
174 cloudPrefImpl.SetString(key, value);
175 EXPECT_EQ(cloudPrefImpl.pref_, nullptr);
176 } catch (...) {
177 EXPECT_TRUE(false);
178 GTEST_LOG_(INFO) << " SetStringTest002 ERROR";
179 }
180 GTEST_LOG_(INFO) << "SetStringTest002 End";
181 }
182
183 /**
184 * @tc.name: GetStringTest001
185 * @tc.desc: Verify the GetString function
186 * @tc.type: FUNC
187 * @tc.require: I6H5MH
188 */
189 HWTEST_F(CloudPrefImplTest, GetStringTest001, TestSize.Level1)
190 {
191 GTEST_LOG_(INFO) << "GetStringTest001 Start";
192 try {
193 EXPECT_NE(cloudPtr_->pref_, nullptr);
194 std::string key;
195 std::string value = "value";
196 std::string ret;
197 EXPECT_CALL(*(cloudPtr_->pref_), GetString(_, _)).WillOnce(Return(value));
198 cloudPtr_->GetString(key, ret);
199 EXPECT_EQ(value, ret);
200 } catch (...) {
201 EXPECT_TRUE(false);
202 GTEST_LOG_(INFO) << " GetStringTest001 ERROR";
203 }
204 GTEST_LOG_(INFO) << "GetStringTest001 End";
205 }
206
207 /**
208 * @tc.name: GetStringTest002
209 * @tc.desc: Verify the GetString function
210 * @tc.type: FUNC
211 * @tc.require: I6H5MH
212 */
213 HWTEST_F(CloudPrefImplTest, GetStringTest002, TestSize.Level1)
214 {
215 GTEST_LOG_(INFO) << "GetStringTest002 Start";
216 try {
217 NativePreferences::PreferencesHelper::DeleteInstance();
218 CloudPrefImpl cloudPrefImpl("");
219 std::string key;
220 std::string value = "value";
221 cloudPrefImpl.GetString(key, value);
222 EXPECT_EQ(cloudPrefImpl.pref_, nullptr);
223 } catch (...) {
224 EXPECT_TRUE(false);
225 GTEST_LOG_(INFO) << " GetStringTest002 ERROR";
226 }
227 GTEST_LOG_(INFO) << "GetStringTest002 End";
228 }
229
230 /**
231 * @tc.name: SetIntTest001
232 * @tc.desc: Verify the SetInt function
233 * @tc.type: FUNC
234 * @tc.require: I6H5MH
235 */
236 HWTEST_F(CloudPrefImplTest, SetIntTest001, TestSize.Level1)
237 {
238 GTEST_LOG_(INFO) << "SetIntTest001 Start";
239 try {
240 EXPECT_NE(cloudPtr_->pref_, nullptr);
241 EXPECT_CALL(*(cloudPtr_->pref_), PutInt(_, _)).WillOnce(Return(0));
242 EXPECT_CALL(*(cloudPtr_->pref_), FlushSync()).WillOnce(Return(-1));
243 std::string key;
244 int value = 0;
245 cloudPtr_->SetInt(key, value);
246 EXPECT_TRUE(true);
247 } catch (...) {
248 EXPECT_TRUE(false);
249 GTEST_LOG_(INFO) << " SetIntTest001 ERROR";
250 }
251 GTEST_LOG_(INFO) << "SetIntTest001 End";
252 }
253
254 /**
255 * @tc.name: SetIntTest002
256 * @tc.desc: Verify the SetInt function
257 * @tc.type: FUNC
258 * @tc.require: I6H5MH
259 */
260 HWTEST_F(CloudPrefImplTest, SetIntTest002, TestSize.Level1)
261 {
262 GTEST_LOG_(INFO) << "SetIntTest002 Start";
263 try {
264 NativePreferences::PreferencesHelper::DeleteInstance();
265 CloudPrefImpl cloudPrefImpl("");
266 std::string key;
267 int value = 0;
268 cloudPrefImpl.SetInt(key, value);
269 EXPECT_EQ(cloudPrefImpl.pref_, nullptr);
270 } catch (...) {
271 EXPECT_TRUE(false);
272 GTEST_LOG_(INFO) << " SetIntTest002 ERROR";
273 }
274 GTEST_LOG_(INFO) << "SetIntTest002 End";
275 }
276
277 /**
278 * @tc.name: GetIntTest001
279 * @tc.desc: Verify the GetInt function
280 * @tc.type: FUNC
281 * @tc.require: I6H5MH
282 */
283 HWTEST_F(CloudPrefImplTest, GetIntTest001, TestSize.Level1)
284 {
285 GTEST_LOG_(INFO) << "GetIntTest001 Start";
286 try {
287 EXPECT_NE(cloudPtr_->pref_, nullptr);
288 std::string key;
289 int32_t value = 0;
290 int32_t ret = -1;
291 EXPECT_CALL(*(cloudPtr_->pref_), GetInt(_, _)).WillOnce(Return(value));
292 cloudPtr_->GetInt(key, ret);
293 EXPECT_EQ(value, ret);
294 } catch (...) {
295 EXPECT_TRUE(false);
296 GTEST_LOG_(INFO) << " GetIntTest001 ERROR";
297 }
298 GTEST_LOG_(INFO) << "GetIntTest001 End";
299 }
300
301 /**
302 * @tc.name: GetIntTest002
303 * @tc.desc: Verify the GetInt function
304 * @tc.type: FUNC
305 * @tc.require: I6H5MH
306 */
307 HWTEST_F(CloudPrefImplTest, GetIntTest002, TestSize.Level1)
308 {
309 GTEST_LOG_(INFO) << "GetIntTest002 Start";
310 try {
311 NativePreferences::PreferencesHelper::DeleteInstance();
312 CloudPrefImpl cloudPrefImpl("");
313 std::string key;
314 int32_t value = 0;
315 cloudPrefImpl.GetInt(key, value);
316 EXPECT_EQ(cloudPrefImpl.pref_, nullptr);
317 } catch (...) {
318 EXPECT_TRUE(false);
319 GTEST_LOG_(INFO) << " GetIntTest002 ERROR";
320 }
321 GTEST_LOG_(INFO) << "GetIntTest002 End";
322 }
323
324 /**
325 * @tc.name: ClearTest001
326 * @tc.desc: Verify the Clear function
327 * @tc.type: FUNC
328 * @tc.require: I6H5MH
329 */
330 HWTEST_F(CloudPrefImplTest, ClearTest001, TestSize.Level1)
331 {
332 GTEST_LOG_(INFO) << "ClearTest001 Start";
333 try {
334 EXPECT_NE(cloudPtr_->pref_, nullptr);
335 auto preferencesHelper = NativePreferences::PreferencesHelper::GetInstance();
336 EXPECT_CALL(*preferencesHelper, DeletePreferences(_)).WillOnce(Return());
337 EXPECT_CALL(*(cloudPtr_->pref_), Clear()).WillOnce(Return(0));
338 cloudPtr_->Clear();
339 EXPECT_TRUE(true);
340 } catch (...) {
341 EXPECT_TRUE(false);
342 GTEST_LOG_(INFO) << " ClearTest001 ERROR";
343 }
344 GTEST_LOG_(INFO) << "ClearTest001 End";
345 }
346
347 /**
348 * @tc.name: ClearTest002
349 * @tc.desc: Verify the Clear function
350 * @tc.type: FUNC
351 * @tc.require: I6H5MH
352 */
353 HWTEST_F(CloudPrefImplTest, ClearTest002, TestSize.Level1)
354 {
355 GTEST_LOG_(INFO) << "ClearTest002 Start";
356 try {
357 NativePreferences::PreferencesHelper::DeleteInstance();
358 CloudPrefImpl cloudPrefImpl("");
359 cloudPrefImpl.Clear();
360 EXPECT_EQ(cloudPrefImpl.pref_, nullptr);
361 } catch (...) {
362 EXPECT_TRUE(false);
363 GTEST_LOG_(INFO) << " ClearTest002 ERROR";
364 }
365 GTEST_LOG_(INFO) << "ClearTest002 End";
366 }
367
368 /**
369 * @tc.name: DeleteTest001
370 * @tc.desc: Verify the Delete function
371 * @tc.type: FUNC
372 * @tc.require: I6H5MH
373 */
374 HWTEST_F(CloudPrefImplTest, DeleteTest001, TestSize.Level1)
375 {
376 GTEST_LOG_(INFO) << "DeleteTest001 Start";
377 try {
378 EXPECT_NE(cloudPtr_->pref_, nullptr);
379 EXPECT_CALL(*(cloudPtr_->pref_), Delete(_)).WillOnce(Return(0));
380 EXPECT_CALL(*(cloudPtr_->pref_), FlushSync()).WillOnce(Return(-1));
381 std::string key;
382 cloudPtr_->Delete(key);
383 EXPECT_TRUE(true);
384 } catch (...) {
385 EXPECT_TRUE(false);
386 GTEST_LOG_(INFO) << " DeleteTest001 ERROR";
387 }
388 GTEST_LOG_(INFO) << "DeleteTest001 End";
389 }
390
391 /**
392 * @tc.name: DeleteTest002
393 * @tc.desc: Verify the Delete function
394 * @tc.type: FUNC
395 * @tc.require: I6H5MH
396 */
397 HWTEST_F(CloudPrefImplTest, DeleteTest002, TestSize.Level1)
398 {
399 GTEST_LOG_(INFO) << "DeleteTest002 Start";
400 try {
401 NativePreferences::PreferencesHelper::DeleteInstance();
402 CloudPrefImpl cloudPrefImpl("");
403 std::string key;
404 cloudPrefImpl.Delete(key);
405 EXPECT_EQ(cloudPrefImpl.pref_, nullptr);
406 } catch (...) {
407 EXPECT_TRUE(false);
408 GTEST_LOG_(INFO) << " DeleteTest002 ERROR";
409 }
410 GTEST_LOG_(INFO) << "DeleteTest002 End";
411 }
412
413 /**
414 * @tc.name: SetLongTest
415 * @tc.desc: Verify the SetLong function
416 * @tc.type: FUNC
417 * @tc.require: I6H5MH
418 */
419 HWTEST_F(CloudPrefImplTest, SetLong_001, TestSize.Level0)
420 {
421 GTEST_LOG_(INFO) << "SetLong_001 Start";
422 try {
423 NativePreferences::PreferencesHelper::DeleteInstance();
424 CloudPrefImpl cloudPrefImpl("");
425 string key = "test_key";
426 int64_t value = -1;
427 ;
428 cloudPrefImpl.SetLong(key, value);
429 EXPECT_EQ(cloudPrefImpl.pref_, nullptr);
430 } catch (...) {
431 EXPECT_TRUE(false);
432 GTEST_LOG_(INFO) << " SetLong_001 ERROR";
433 }
434 GTEST_LOG_(INFO) << "SetLong_001 End";
435 }
436
437 /**
438 * @tc.name: SetLongTest
439 * @tc.desc: Verify the SetLong function
440 * @tc.type: FUNC
441 * @tc.require: I6H5MH
442 */
443 HWTEST_F(CloudPrefImplTest, SetLong_002, TestSize.Level0)
444 {
445 GTEST_LOG_(INFO) << "SetLong_002 Start";
446 try {
447 EXPECT_NE(cloudPtr_->pref_, nullptr);
448 EXPECT_CALL(*(cloudPtr_->pref_), PutLong(_, _)).WillOnce(Return(0));
449 EXPECT_CALL(*(cloudPtr_->pref_), FlushSync()).WillOnce(Return(-1));
450 string key = "test_key";
451 int64_t value = 89;
452 cloudPtr_->SetLong(key, value);
453 EXPECT_TRUE(true);
454 } catch (...) {
455 EXPECT_TRUE(false);
456 GTEST_LOG_(INFO) << " SetLong_002 ERROR";
457 }
458 GTEST_LOG_(INFO) << "SetLong_002 End";
459 }
460
461 /**
462 * @tc.name: GetLongTest001
463 * @tc.desc: Verify the GetLong function
464 * @tc.type: FUNC
465 * @tc.require: I6H5MH
466 */
467 HWTEST_F(CloudPrefImplTest, GetLongTest001, TestSize.Level1)
468 {
469 GTEST_LOG_(INFO) << "GetLongTest001 Start";
470 NativePreferences::PreferencesHelper::DeleteInstance();
471 CloudPrefImpl cloudPrefImpl("");
472 std::string key;
473 int64_t value = 0;
474 cloudPrefImpl.GetLong(key, value);
475 EXPECT_EQ(cloudPrefImpl.pref_, nullptr);
476 GTEST_LOG_(INFO) << "GetLongTest001 End";
477 }
478
479 /**
480 * @tc.name: GetLongTest002
481 * @tc.desc: Verify the GetLong function
482 * @tc.type: FUNC
483 * @tc.require: I6H5MH
484 */
485 HWTEST_F(CloudPrefImplTest, GetLongTest002, TestSize.Level1)
486 {
487 GTEST_LOG_(INFO) << "GetLongTest002 Start";
488 EXPECT_NE(cloudPtr_->pref_, nullptr);
489 std::string key;
490 int64_t value = 0;
491 int64_t ret = -1;
492 EXPECT_CALL(*(cloudPtr_->pref_), GetLong(_, _)).WillOnce(Return(value));
493 cloudPtr_->GetLong(key, ret);
494 EXPECT_EQ(value, ret);
495 GTEST_LOG_(INFO) << "GetLongTest002 End";
496 }
497
498 /**
499 * @tc.name: SetBoolTest
500 * @tc.desc: Verify the SetLong function
501 * @tc.type: FUNC
502 * @tc.require: I6H5MH
503 */
504 HWTEST_F(CloudPrefImplTest, SetBool_001, TestSize.Level0)
505 {
506 GTEST_LOG_(INFO) << "SetBool_001 Start";
507 try {
508 NativePreferences::PreferencesHelper::DeleteInstance();
509 int32_t userId = 123;
510 string bundleName = "";
511 string tableName = "";
512 CloudPrefImpl cloudPrefImpl(userId, bundleName, tableName);
513
514 string key = "test_key";
515 bool value = true;
516 cloudPrefImpl.SetBool(key, value);
517 EXPECT_EQ(cloudPrefImpl.pref_, nullptr);
518 } catch (...) {
519 EXPECT_TRUE(false);
520 GTEST_LOG_(INFO) << " SetBool_001 ERROR";
521 }
522 GTEST_LOG_(INFO) << "SetBool_001 End";
523 }
524
525 /**
526 * @tc.name: SetBoolTest
527 * @tc.desc: Verify the SetLong function
528 * @tc.type: FUNC
529 * @tc.require: I6H5MH
530 */
531 HWTEST_F(CloudPrefImplTest, SetBool_002, TestSize.Level0)
532 {
533 GTEST_LOG_(INFO) << "SetBool_002 Start";
534 try {
535 EXPECT_NE(cloudPtr_->pref_, nullptr);
536 EXPECT_CALL(*(cloudPtr_->pref_), PutBool(_, _)).WillOnce(Return(0));
537 EXPECT_CALL(*(cloudPtr_->pref_), FlushSync()).WillOnce(Return(-1));
538 string key = "test_key1";
539 bool value = false;
540 cloudPtr_->SetBool(key, value);
541 EXPECT_TRUE(true);
542 } catch (...) {
543 EXPECT_TRUE(false);
544 GTEST_LOG_(INFO) << " SetBool_002 ERROR";
545 }
546 GTEST_LOG_(INFO) << "SetBool_002 End";
547 }
548
549 /**
550 * @tc.name: GetBoolTest
551 * @tc.desc: Verify the GetBool function
552 * @tc.type: FUNC
553 * @tc.require: I6H5MH
554 */
555 HWTEST_F(CloudPrefImplTest, GetBoolTest001, TestSize.Level0)
556 {
557 GTEST_LOG_(INFO) << "GetBoolTest001 Start";
558 try {
559 NativePreferences::PreferencesHelper::DeleteInstance();
560 int32_t userId = 123;
561 string bundleName = "";
562 string tableName = "";
563 CloudPrefImpl cloudPrefImpl(userId, bundleName, tableName);
564
565 string key = "test_key";
566 bool value = true;
567 cloudPrefImpl.GetBool(key, value);
568 EXPECT_EQ(cloudPrefImpl.pref_, nullptr);
569 } catch (...) {
570 EXPECT_TRUE(false);
571 GTEST_LOG_(INFO) << " GetBoolTest001 ERROR";
572 }
573 GTEST_LOG_(INFO) << "GetBoolTest001 End";
574 }
575
576 /**
577 * @tc.name: GetBoolTest002
578 * @tc.desc: Verify the GetBool function
579 * @tc.type: FUNC
580 * @tc.require: I6H5MH
581 */
582 HWTEST_F(CloudPrefImplTest, GetBoolTest002, TestSize.Level1)
583 {
584 GTEST_LOG_(INFO) << "GetBoolTest002 Start";
585 EXPECT_NE(cloudPtr_->pref_, nullptr);
586 std::string key;
587 bool value = true;
588 bool ret = false;
589 EXPECT_CALL(*(cloudPtr_->pref_), GetBool(_, _)).WillOnce(Return(value));
590 cloudPtr_->GetBool(key, ret);
591 EXPECT_EQ(value, ret);
592
593 GTEST_LOG_(INFO) << "GetBoolTest002 End";
594 }
595 } // namespace OHOS::FileManagement::CloudSync::Test