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
25 using namespace testing::ext;
26 using namespace OHOS::UDMF;
27 using namespace OHOS;
28 namespace OHOS::Test {
29 using namespace std;
30
31 class UnifiedDataHelperTest : public testing::Test {
32 public:
33 static void SetUpTestCase();
34 static void TearDownTestCase();
35 void SetUp() override;
36 void TearDown() override;
37 };
38
SetUpTestCase()39 void UnifiedDataHelperTest::SetUpTestCase()
40 {
41 }
42
TearDownTestCase()43 void UnifiedDataHelperTest::TearDownTestCase()
44 {
45 }
46
SetUp()47 void UnifiedDataHelperTest::SetUp()
48 {
49 }
50
TearDown()51 void UnifiedDataHelperTest::TearDown()
52 {
53 }
54
55 constexpr mode_t MODE = 0700;
56 constexpr const char *TEMP_UNIFIED_DATA_ROOT_PATH = "data/storage/el2/base/temp/udata";
57
58 /**
59 * @tc.name: CreateDirIfNotExist001
60 * @tc.desc: Normal testcase of CreateDirIfNotExist
61 * @tc.type: FUNC
62 */
HWTEST_F(UnifiedDataHelperTest, CreateDirIfNotExist001, TestSize.Level1)63 HWTEST_F(UnifiedDataHelperTest, CreateDirIfNotExist001, TestSize.Level1)
64 {
65 LOG_INFO(UDMF_TEST, "CreateDirIfNotExist001 begin.");
66 const std::string dirPath = "storage/media/100/local/files/Pictures";
67 const mode_t mode = MODE;
68 UnifiedDataHelper unifiedDataHelper;
69 unifiedDataHelper.CreateDirIfNotExist(dirPath, mode);
70 EXPECT_TRUE(true);
71 LOG_INFO(UDMF_TEST, "CreateDirIfNotExist001 end.");
72 }
73
74 /**
75 * @tc.name: CreateDirIfNotExist002
76 * @tc.desc: Abnormal testcase of CreateDirIfNotExist, the storage/el2/base/temp/udata path does not exist
77 * @tc.type: FUNC
78 */
HWTEST_F(UnifiedDataHelperTest, CreateDirIfNotExist002, TestSize.Level1)79 HWTEST_F(UnifiedDataHelperTest, CreateDirIfNotExist002, TestSize.Level1)
80 {
81 LOG_INFO(UDMF_TEST, "CreateDirIfNotExist002 begin.");
82 const std::string dirPath = "data/storage/el2/base/temp/udata";
83 const mode_t mode = MODE;
84 UnifiedDataHelper unifiedDataHelper;
85 unifiedDataHelper.CreateDirIfNotExist(dirPath, mode);
86 EXPECT_TRUE(true);
87 LOG_INFO(UDMF_TEST, "CreateDirIfNotExist002 end.");
88 }
89
90 /**
91 * @tc.name: Unpack001
92 * @tc.desc: Abnormal testcase of Unpack, the records_ length is 0
93 * @tc.type: FUNC
94 */
HWTEST_F(UnifiedDataHelperTest, Unpack001, TestSize.Level1)95 HWTEST_F(UnifiedDataHelperTest, Unpack001, TestSize.Level1)
96 {
97 LOG_INFO(UDMF_TEST, "Unpack001 begin.");
98 UnifiedData data;
99 UnifiedDataHelper unifiedDataHelper;
100 data.records_ = std::vector<std::shared_ptr<UnifiedRecord>>();
101 bool ret = unifiedDataHelper.Unpack(data);
102 EXPECT_FALSE(ret);
103 LOG_INFO(UDMF_TEST, "Unpack001 end.");
104 }
105
106 /**
107 * @tc.name: Unpack002
108 * @tc.desc: Abnormal testcase of Unpack, the data is nullptr
109 * @tc.type: FUNC
110 */
HWTEST_F(UnifiedDataHelperTest, Unpack002, TestSize.Level1)111 HWTEST_F(UnifiedDataHelperTest, Unpack002, TestSize.Level1)
112 {
113 LOG_INFO(UDMF_TEST, "Unpack002 begin.");
114 UnifiedData data;
115 UnifiedDataHelper unifiedDataHelper;
116 bool ret = unifiedDataHelper.Unpack(data);
117 EXPECT_FALSE(ret);
118 LOG_INFO(UDMF_TEST, "Unpack002 end.");
119 }
120
121 /**
122 * @tc.name: SaveUDataToFile001
123 * @tc.desc: Abnormal testcase of SaveUDataToFile, the data is nullptr
124 * @tc.type: FUNC
125 */
HWTEST_F(UnifiedDataHelperTest, SaveUDataToFile001, TestSize.Level1)126 HWTEST_F(UnifiedDataHelperTest, SaveUDataToFile001, TestSize.Level1)
127 {
128 LOG_INFO(UDMF_TEST, "SaveUDataToFile001 begin.");
129 const std::string dataFile = "data/storage/el2/base/temp/udata";
130 UnifiedData data;
131 UnifiedDataHelper unifiedDataHelper;
132 bool ret = unifiedDataHelper.SaveUDataToFile(dataFile, data);
133 EXPECT_FALSE(ret);
134 LOG_INFO(UDMF_TEST, "SaveUDataToFile001 end.");
135 }
136
137 /**
138 * @tc.name: LoadUDataFromFile001
139 * @tc.desc: Abnormal testcase of LoadUDataFromFile, the data is nullptr
140 * @tc.type: FUNC
141 */
HWTEST_F(UnifiedDataHelperTest, LoadUDataFromFile001, TestSize.Level1)142 HWTEST_F(UnifiedDataHelperTest, LoadUDataFromFile001, TestSize.Level1)
143 {
144 LOG_INFO(UDMF_TEST, "LoadUDataFromFile001 begin.");
145 const std::string dataFile = "data/storage/el2/base/temp/udata";
146 UnifiedData data;
147 UnifiedDataHelper unifiedDataHelper;
148 bool ret = unifiedDataHelper.LoadUDataFromFile(dataFile, data);
149 EXPECT_FALSE(ret);
150 LOG_INFO(UDMF_TEST, "LoadUDataFromFile001 end.");
151 }
152
153 /**
154 * @tc.name: GetRootPath001
155 * @tc.desc: Abnormal testcase of GetRootPath, the rootPath_ is nullptr
156 * @tc.type: FUNC
157 */
HWTEST_F(UnifiedDataHelperTest, GetRootPath001, TestSize.Level1)158 HWTEST_F(UnifiedDataHelperTest, GetRootPath001, TestSize.Level1)
159 {
160 LOG_INFO(UDMF_TEST, "GetRootPath001 begin.");
161 UnifiedDataHelper unifiedDataHelper;
162 unifiedDataHelper.rootPath_ = "";
163 std::string ret = unifiedDataHelper.GetRootPath();
164 EXPECT_EQ(ret, TEMP_UNIFIED_DATA_ROOT_PATH);
165 LOG_INFO(UDMF_TEST, "GetRootPath001 end.");
166 }
167 } // OHOS::Test