1 /*
2 * Copyright (c) 2024 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 #define LOG_TAG "UnifiedDataHelperTest"
16
17 #include <unistd.h>
18 #include <gtest/gtest.h>
19 #include <string>
20
21 #include "logger.h"
22 #include "udmf_capi_common.h"
23 #include "unified_data_helper.h"
24 #include "file_uri.h"
25
26 using namespace testing::ext;
27 using namespace OHOS::UDMF;
28 using namespace OHOS;
29 namespace OHOS::Test {
30 using namespace std;
31
32 class UnifiedDataHelperTest : public testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36 void SetUp() override;
37 void TearDown() override;
38 };
39
SetUpTestCase()40 void UnifiedDataHelperTest::SetUpTestCase()
41 {
42 }
43
TearDownTestCase()44 void UnifiedDataHelperTest::TearDownTestCase()
45 {
46 }
47
SetUp()48 void UnifiedDataHelperTest::SetUp()
49 {
50 }
51
TearDown()52 void UnifiedDataHelperTest::TearDown()
53 {
54 }
55
56 constexpr mode_t MODE = 0700;
57 constexpr const char *TEMP_UNIFIED_DATA_ROOT_PATH = "data/storage/el2/base/temp/udata";
58
59 /**
60 * @tc.name: CreateDirIfNotExist001
61 * @tc.desc: Normal testcase of CreateDirIfNotExist
62 * @tc.type: FUNC
63 */
64 HWTEST_F(UnifiedDataHelperTest, CreateDirIfNotExist001, TestSize.Level1)
65 {
66 LOG_INFO(UDMF_TEST, "CreateDirIfNotExist001 begin.");
67 const std::string dirPath = "storage/media/100/local/files/Pictures";
68 const mode_t mode = MODE;
69 UnifiedDataHelper unifiedDataHelper;
70 unifiedDataHelper.CreateDirIfNotExist(dirPath, mode);
71 EXPECT_TRUE(true);
72 LOG_INFO(UDMF_TEST, "CreateDirIfNotExist001 end.");
73 }
74
75 /**
76 * @tc.name: CreateDirIfNotExist002
77 * @tc.desc: Abnormal testcase of CreateDirIfNotExist, the storage/el2/base/temp/udata path does not exist
78 * @tc.type: FUNC
79 */
80 HWTEST_F(UnifiedDataHelperTest, CreateDirIfNotExist002, TestSize.Level1)
81 {
82 LOG_INFO(UDMF_TEST, "CreateDirIfNotExist002 begin.");
83 const std::string dirPath = "data/storage/el2/base/temp/udata";
84 const mode_t mode = MODE;
85 UnifiedDataHelper unifiedDataHelper;
86 unifiedDataHelper.CreateDirIfNotExist(dirPath, mode);
87 EXPECT_TRUE(true);
88 LOG_INFO(UDMF_TEST, "CreateDirIfNotExist002 end.");
89 }
90
91 /**
92 * @tc.name: Unpack001
93 * @tc.desc: Abnormal testcase of Unpack, the records_ length is 0
94 * @tc.type: FUNC
95 */
96 HWTEST_F(UnifiedDataHelperTest, Unpack001, TestSize.Level1)
97 {
98 LOG_INFO(UDMF_TEST, "Unpack001 begin.");
99 UnifiedData data;
100 UnifiedDataHelper unifiedDataHelper;
101 data.records_ = std::vector<std::shared_ptr<UnifiedRecord>>();
102 bool ret = unifiedDataHelper.Unpack(data);
103 EXPECT_FALSE(ret);
104 LOG_INFO(UDMF_TEST, "Unpack001 end.");
105 }
106
107 /**
108 * @tc.name: Unpack002
109 * @tc.desc: Abnormal testcase of Unpack, the data is nullptr
110 * @tc.type: FUNC
111 */
112 HWTEST_F(UnifiedDataHelperTest, Unpack002, TestSize.Level1)
113 {
114 LOG_INFO(UDMF_TEST, "Unpack002 begin.");
115 UnifiedData data;
116 UnifiedDataHelper unifiedDataHelper;
117 bool ret = unifiedDataHelper.Unpack(data);
118 EXPECT_FALSE(ret);
119 LOG_INFO(UDMF_TEST, "Unpack002 end.");
120 }
121
122 /**
123 * @tc.name: SaveUDataToFile001
124 * @tc.desc: Abnormal testcase of SaveUDataToFile, the data is nullptr
125 * @tc.type: FUNC
126 */
127 HWTEST_F(UnifiedDataHelperTest, SaveUDataToFile001, TestSize.Level1)
128 {
129 LOG_INFO(UDMF_TEST, "SaveUDataToFile001 begin.");
130 const std::string dataFile = "data/storage/el2/base/temp/udata";
131 UnifiedData data;
132 UnifiedDataHelper unifiedDataHelper;
133 bool ret = unifiedDataHelper.SaveUDataToFile(dataFile, data);
134 EXPECT_FALSE(ret);
135 LOG_INFO(UDMF_TEST, "SaveUDataToFile001 end.");
136 }
137
138 /**
139 * @tc.name: SaveUDataToFile002
140 * @tc.desc: Normal testcase of SaveUDataToFile
141 * @tc.type: FUNC
142 */
143 HWTEST_F(UnifiedDataHelperTest, SaveUDataToFile002, TestSize.Level1)
144 {
145 LOG_INFO(UDMF_TEST, "SaveUDataToFile002 begin.");
146 const std::string dataFile = "data/test";
147 UnifiedData data;
148 UnifiedDataHelper unifiedDataHelper;
149 bool ret = unifiedDataHelper.SaveUDataToFile(dataFile, data);
150 EXPECT_TRUE(ret);
151 LOG_INFO(UDMF_TEST, "SaveUDataToFile002 end.");
152 }
153
154 /**
155 * @tc.name: LoadUDataFromFile001
156 * @tc.desc: Abnormal testcase of LoadUDataFromFile, the data is nullptr
157 * @tc.type: FUNC
158 */
159 HWTEST_F(UnifiedDataHelperTest, LoadUDataFromFile001, TestSize.Level1)
160 {
161 LOG_INFO(UDMF_TEST, "LoadUDataFromFile001 begin.");
162 const std::string dataFile = "data/storage/el2/base/temp/udata";
163 UnifiedData data;
164 UnifiedDataHelper unifiedDataHelper;
165 bool ret = unifiedDataHelper.LoadUDataFromFile(dataFile, data);
166 EXPECT_FALSE(ret);
167 LOG_INFO(UDMF_TEST, "LoadUDataFromFile001 end.");
168 }
169
170 /**
171 * @tc.name: LoadUDataFromFile002
172 * @tc.desc: Abnormal testcase of LoadUDataFromFile, the data is nullptr
173 * @tc.type: FUNC
174 */
175 HWTEST_F(UnifiedDataHelperTest, LoadUDataFromFile002, TestSize.Level1)
176 {
177 LOG_INFO(UDMF_TEST, "LoadUDataFromFile002 begin.");
178 const std::string dataFile = "data/test";
179 UnifiedData data;
180 UnifiedDataHelper unifiedDataHelper;
181 bool ret = unifiedDataHelper.LoadUDataFromFile(dataFile, data);
182 EXPECT_FALSE(ret);
183 LOG_INFO(UDMF_TEST, "LoadUDataFromFile002 end.");
184 }
185
186 /**
187 * @tc.name: GetRootPath001
188 * @tc.desc: Abnormal testcase of GetRootPath, the rootPath_ is nullptr
189 * @tc.type: FUNC
190 */
191 HWTEST_F(UnifiedDataHelperTest, GetRootPath001, TestSize.Level1)
192 {
193 LOG_INFO(UDMF_TEST, "GetRootPath001 begin.");
194 UnifiedDataHelper unifiedDataHelper;
195 unifiedDataHelper.rootPath_ = "";
196 std::string ret = unifiedDataHelper.GetRootPath();
197 EXPECT_EQ(ret, TEMP_UNIFIED_DATA_ROOT_PATH);
198 LOG_INFO(UDMF_TEST, "GetRootPath001 end.");
199 }
200
201 /**
202 * @tc.name: FileClose001
203 * @tc.desc: Abnormal testcase of FileClose, the file is null
204 * @tc.type: FUNC
205 */
206 HWTEST_F(UnifiedDataHelperTest, FileClose001, TestSize.Level1)
207 {
208 LOG_INFO(UDMF_TEST, "FileClose001 begin.");
209 UnifiedDataHelper unifiedDataHelper;
210 const std::string dataFile = "data/test";
211 AppFileService::ModuleFileUri::FileUri fileUri(dataFile);
212 std::string path = fileUri.GetRealPath();
213 std::FILE *file = fopen(path.c_str(), "r");
214 bool status = unifiedDataHelper.FileClose(file, true);
215 EXPECT_TRUE(status);
216 LOG_INFO(UDMF_TEST, "FileClose001 end.");
217 }
218
219 /**
220 * @tc.name: FileClose002
221 * @tc.desc: Abnormal testcase of FileClose, file is nullptr
222 * @tc.type: FUNC
223 */
224 HWTEST_F(UnifiedDataHelperTest, FileClose002, TestSize.Level1)
225 {
226 LOG_INFO(UDMF_TEST, "FileClose002 begin.");
227 UnifiedDataHelper unifiedDataHelper;
228 std::FILE *file = nullptr;
229 bool status = unifiedDataHelper.FileClose(file, true);
230 EXPECT_FALSE(status);
231 LOG_INFO(UDMF_TEST, "FileClose002 end.");
232 }
233 } // OHOS::Test