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 <sys/stat.h>
21 #include <unistd.h>
22 #include "utils_log.h"
23 
24 #define USERID 100
25 namespace OHOS::FileManagement::CloudSync::Test {
26 using namespace testing;
27 using namespace testing::ext;
28 using namespace std;
29 
30 class CloudPrefImplTest : public testing::Test {
31 public:
32     static void SetUpTestCase(void);
33     static void TearDownTestCase(void);
34     void SetUp();
35     void TearDown();
36     int32_t userId_;
37     std::string fileName_;
38     std::shared_ptr<CloudPrefImpl> cloudPtr_;
39 };
40 
SetUpTestCase(void)41 void CloudPrefImplTest::SetUpTestCase(void)
42 {
43     GTEST_LOG_(INFO) << "SetUpTestCase";
44 }
45 
TearDownTestCase(void)46 void CloudPrefImplTest::TearDownTestCase(void)
47 {
48     GTEST_LOG_(INFO) << "TearDownTestCase";
49 }
50 
SetUp(void)51 void CloudPrefImplTest::SetUp(void)
52 {
53     userId_ = USERID;
54     fileName_ = "test";
55     cloudPtr_ = std::make_shared<CloudPrefImpl>(fileName_);
56     if (cloudPtr_ == nullptr) {
57         GTEST_LOG_(INFO) << "cloudPtr_ == nullptr";
58     }
59     GTEST_LOG_(INFO) << "SetUp";
60 }
61 
TearDown(void)62 void CloudPrefImplTest::TearDown(void)
63 {
64     if (cloudPtr_ != nullptr) {
65         cloudPtr_ = nullptr;
66     }
67     GTEST_LOG_(INFO) << "TearDown";
68 }
69 
70 /**
71  * @tc.name: CloudPrefImplTest001
72  * @tc.desc: Verify the CloudPrefImpl function
73  * @tc.type: FUNC
74  * @tc.require: I6H5MH
75  */
76 HWTEST_F(CloudPrefImplTest, CloudPrefImpTest001, TestSize.Level1)
77 {
78     GTEST_LOG_(INFO) << "CloudPrefImpTest001 Start";
79     try {
80         CloudPrefImpl cloudPreImpl("");
81         EXPECT_EQ(cloudPreImpl.pref_, nullptr);
82     } catch (...) {
83         EXPECT_TRUE(false);
84         GTEST_LOG_(INFO) << " CloudPrefImpTest001 ERROR";
85     }
86     GTEST_LOG_(INFO) << "CloudPrefImpTest001 End";
87 }
88 
89 /**
90  * @tc.name: CloudPrefImplTest002
91  * @tc.desc: Verify the CloudPrefImpl function
92  * @tc.type: FUNC
93  * @tc.require: I6H5MH
94  */
95 HWTEST_F(CloudPrefImplTest, CloudPrefImpTest002, TestSize.Level1)
96 {
97     GTEST_LOG_(INFO) << "CloudPrefImpTest002 Start";
98     try {
99         CloudPrefImpl cloudPreImpl(fileName_);
100         EXPECT_NE(cloudPreImpl.pref_, nullptr);
101     } catch (...) {
102         EXPECT_TRUE(false);
103         GTEST_LOG_(INFO) << " CloudPrefImpTest002 ERROR";
104     }
105     GTEST_LOG_(INFO) << "CloudPrefImpTest002 End";
106 }
107 
108 /**
109  * @tc.name: CloudPrefImplTest003
110  * @tc.desc: Verify the CloudPrefImpl function
111  * @tc.type: FUNC
112  * @tc.require: I6H5MH
113  */
114 HWTEST_F(CloudPrefImplTest, CloudPrefImplTest003, TestSize.Level1)
115 {
116     GTEST_LOG_(INFO) << "CloudPrefImplTest003 Start";
117     try {
118         const std::string bundleName = "";
119         std::string fileDir = "/data/service/el1/public/cloudfile/";
120         std::string tableName = "testTable";
121         EXPECT_EQ(access(fileDir.c_str(), F_OK), 0);
122         CloudPrefImpl cloudPreImpl(userId_, bundleName, tableName);
123         EXPECT_NE(cloudPreImpl.pref_, nullptr);
124     } catch (...) {
125         EXPECT_TRUE(false);
126         GTEST_LOG_(INFO) << " CloudPrefImplTest003 ERROR";
127     }
128     GTEST_LOG_(INFO) << "CloudPrefImplTest003 End";
129 }
130 
131 /**
132  * @tc.name: SetStringTest
133  * @tc.desc: Verify the SetString function
134  * @tc.type: FUNC
135  * @tc.require: I6H5MH
136  */
137 HWTEST_F(CloudPrefImplTest, SetStringTest, TestSize.Level1)
138 {
139     GTEST_LOG_(INFO) << "SetStringTest Start";
140     try {
141         EXPECT_NE(cloudPtr_->pref_, nullptr);
142         std::string key;
143         std::string value;
144         cloudPtr_->SetString(key, value);
145         EXPECT_TRUE(true);
146     } catch (...) {
147         EXPECT_TRUE(false);
148         GTEST_LOG_(INFO) << " SetStringTest ERROR";
149     }
150     GTEST_LOG_(INFO) << "SetStringTest End";
151 }
152 
153 /**
154  * @tc.name: GetStringTest
155  * @tc.desc: Verify the GetString function
156  * @tc.type: FUNC
157  * @tc.require: I6H5MH
158  */
159 HWTEST_F(CloudPrefImplTest, GetStringTest, TestSize.Level1)
160 {
161     GTEST_LOG_(INFO) << "GetStringTest Start";
162     try {
163         EXPECT_NE(cloudPtr_->pref_, nullptr);
164         std::string key;
165         std::string value;
166         cloudPtr_->GetString(key, value);
167         EXPECT_TRUE(true);
168     } catch (...) {
169         EXPECT_TRUE(false);
170         GTEST_LOG_(INFO) << " GetStringTest ERROR";
171     }
172     GTEST_LOG_(INFO) << "GetStringTest End";
173 }
174 
175 /**
176  * @tc.name: SetIntTest
177  * @tc.desc: Verify the SetInt function
178  * @tc.type: FUNC
179  * @tc.require: I6H5MH
180  */
181 HWTEST_F(CloudPrefImplTest, SetIntTest, TestSize.Level1)
182 {
183     GTEST_LOG_(INFO) << "SetIntTest Start";
184     try {
185         EXPECT_NE(cloudPtr_->pref_, nullptr);
186         std::string key;
187         int value = 0;
188         cloudPtr_->SetInt(key, value);
189         EXPECT_TRUE(true);
190     } catch (...) {
191         EXPECT_TRUE(false);
192         GTEST_LOG_(INFO) << " SetIntTest ERROR";
193     }
194     GTEST_LOG_(INFO) << "SetIntTest End";
195 }
196 
197 /**
198  * @tc.name: GetIntTest
199  * @tc.desc: Verify the GetInt function
200  * @tc.type: FUNC
201  * @tc.require: I6H5MH
202  */
203 HWTEST_F(CloudPrefImplTest, GetIntTest, TestSize.Level1)
204 {
205     GTEST_LOG_(INFO) << "GetIntTest Start";
206     try {
207         EXPECT_NE(cloudPtr_->pref_, nullptr);
208         std::string key;
209         int32_t value = 0;
210         cloudPtr_->GetInt(key, value);
211         EXPECT_TRUE(true);
212     } catch (...) {
213         EXPECT_TRUE(false);
214         GTEST_LOG_(INFO) << " GetIntTest ERROR";
215     }
216     GTEST_LOG_(INFO) << "GetIntTest End";
217 }
218 
219 /**
220  * @tc.name: ClearTest
221  * @tc.desc: Verify the Clear function
222  * @tc.type: FUNC
223  * @tc.require: I6H5MH
224  */
225 HWTEST_F(CloudPrefImplTest, ClearTest, TestSize.Level1)
226 {
227     GTEST_LOG_(INFO) << "ClearTest Start";
228     try {
229         EXPECT_NE(cloudPtr_->pref_, nullptr);
230         cloudPtr_->Clear();
231         EXPECT_TRUE(true);
232     } catch (...) {
233         EXPECT_TRUE(false);
234         GTEST_LOG_(INFO) << " ClearTest ERROR";
235     }
236     GTEST_LOG_(INFO) << "ClearTest End";
237 }
238 
239 /**
240  * @tc.name: DeleteTest
241  * @tc.desc: Verify the Delete function
242  * @tc.type: FUNC
243  * @tc.require: I6H5MH
244  */
245 HWTEST_F(CloudPrefImplTest, DeleteTest, TestSize.Level1)
246 {
247     GTEST_LOG_(INFO) << "DeleteTest Start";
248     try {
249         EXPECT_NE(cloudPtr_->pref_, nullptr);
250         std::string key;
251         cloudPtr_->Delete(key);
252         EXPECT_TRUE(true);
253     } catch (...) {
254         EXPECT_TRUE(false);
255         GTEST_LOG_(INFO) << " DeleteTest ERROR";
256     }
257     GTEST_LOG_(INFO) << "DeleteTest End";
258 }
259 
260 } // namespace OHOS::FileManagement::CloudSync::Test