• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2025 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 "preprocess_utils.h"
17 #include "gtest/gtest.h"
18 #include "text.h"
19 
20 namespace OHOS::UDMF {
21 using namespace testing::ext;
22 class UdmfPreProcessUtilsTest : public testing::Test {
23 public:
SetUpTestCase(void)24     static void SetUpTestCase(void) {}
TearDownTestCase(void)25     static void TearDownTestCase(void) {}
SetUp()26     void SetUp() {}
TearDown()27     void TearDown() {}
28 };
29 
30 /**
31 * @tc.name: RuntimeDataImputation001
32 * @tc.desc: Abnormal test of FillRuntimeInfo, option is invalid
33 * @tc.type: FUNC
34 * @tc.require:
35 */
36 HWTEST_F(UdmfPreProcessUtilsTest, RuntimeDataImputation001, TestSize.Level1)
37 {
38     UnifiedData data;
39     CustomOption option;
40     PreProcessUtils preProcessUtils;
41     int32_t ret = preProcessUtils.FillRuntimeInfo(data, option);
42     EXPECT_EQ(ret, E_ERROR);
43 }
44 
45 /**
46 * @tc.name: GetHapUidByToken001
47 * @tc.desc: Abnormal test of GetHapUidByToken, tokenId is invalid
48 * @tc.type: FUNC
49 * @tc.require:
50 */
51 HWTEST_F(UdmfPreProcessUtilsTest, GetHapUidByToken001, TestSize.Level1)
52 {
53     uint32_t tokenId = 0;
54     int userId = 0;
55     PreProcessUtils preProcessUtils;
56     int32_t ret = preProcessUtils.GetHapUidByToken(tokenId, userId);
57     EXPECT_EQ(ret, E_ERROR);
58 }
59 
60 /**
61 * @tc.name: SetRemoteData001
62 * @tc.desc: Abnormal test of SetRemoteData, data is null
63 * @tc.type: FUNC
64 * @tc.require:
65 */
66 HWTEST_F(UdmfPreProcessUtilsTest, SetRemoteData001, TestSize.Level1)
67 {
68     UnifiedData data;
69     PreProcessUtils preProcessUtils;
70     EXPECT_NO_FATAL_FAILURE(preProcessUtils.SetRemoteData(data));
71 }
72 
73 /**
74 * @tc.name: SetRemoteData002
75 * @tc.desc: Normal test of SetRemoteData
76 * @tc.type: FUNC
77 * @tc.require:
78 */
79 HWTEST_F(UdmfPreProcessUtilsTest, SetRemoteData002, TestSize.Level1)
80 {
81     UnifiedData data;
82     std::vector<std::shared_ptr<UnifiedRecord>> inputRecords;
83     for (int32_t i = 0; i < 512; ++i) {
84         inputRecords.emplace_back(std::make_shared<Text>());
85     }
86     data.SetRecords(inputRecords);
87     data.runtime_ = std::make_shared<Runtime>();
88     PreProcessUtils preProcessUtils;
89     EXPECT_NO_FATAL_FAILURE(preProcessUtils.SetRemoteData(data));
90 }
91 
92 /**
93 * @tc.name: GetDfsUrisFromLocal001
94 * @tc.desc: Abnormal test of GetDfsUrisFromLocal, uris is null
95 * @tc.type: FUNC
96 * @tc.require:
97 */
98 HWTEST_F(UdmfPreProcessUtilsTest, GetDfsUrisFromLocal001, TestSize.Level1)
99 {
100     const std::vector<std::string> uris;
101     int32_t userId = 0;
102     UnifiedData data;
103     PreProcessUtils preProcessUtils;
104     int32_t ret = preProcessUtils.GetDfsUrisFromLocal(uris, userId, data);
105     EXPECT_EQ(ret, E_FS_ERROR);
106 }
107 
108 /**
109 * @tc.name: CheckUriAuthorization001
110 * @tc.desc: Abnormal test of CheckUriAuthorization, uris is invalid
111 * @tc.type: FUNC
112 * @tc.require:
113 */
114 HWTEST_F(UdmfPreProcessUtilsTest, CheckUriAuthorization001, TestSize.Level1)
115 {
116     const std::vector<std::string> uris = {"test"};
117     uint32_t tokenId = 0;
118     PreProcessUtils preProcessUtils;
119     bool ret = preProcessUtils.CheckUriAuthorization(uris, tokenId);
120     EXPECT_EQ(ret, false);
121 }
122 
123 /**
124 * @tc.name: GetInstIndex001
125 * @tc.desc: Normal test of GetInstIndex
126 * @tc.type: FUNC
127 * @tc.require:
128 */
129 HWTEST_F(UdmfPreProcessUtilsTest, GetInstIndex001, TestSize.Level1)
130 {
131     uint32_t tokenId = 0;
132     int32_t instIndex = 0;
133     PreProcessUtils preProcessUtils;
134     bool ret = preProcessUtils.GetInstIndex(tokenId, instIndex);
135     EXPECT_EQ(instIndex, 0);
136     EXPECT_EQ(ret, true);
137 }
138 
139 /**
140 * @tc.name: ProcessFileType001
141 * @tc.desc: Abnormal test of ProcessFileType, records is nullptr
142 * @tc.type: FUNC
143 * @tc.require:
144 */
145 HWTEST_F(UdmfPreProcessUtilsTest, ProcessFileType001, TestSize.Level1)
146 {
147     std::shared_ptr<UnifiedRecord> record = std::make_shared<UnifiedRecord>();
148     std::shared_ptr<Object> obj = std::make_shared<Object>();
149     obj->value_[UNIFORM_DATA_TYPE] = "general.file-uri";
150     obj->value_[FILE_URI_PARAM] = "http://demo.com.html";
151     obj->value_[FILE_TYPE] = "general.html";
152     record->AddEntry("general.file-uri", obj);
153     std::shared_ptr<Object> obj1 = std::make_shared<Object>();
154     obj1->value_[UNIFORM_DATA_TYPE] = "general.content-form";
155     obj1->value_["title"] = "test";
156     record->AddEntry("general.content-form", obj1);
157 
158     std::shared_ptr<UnifiedRecord> record1 = std::make_shared<UnifiedRecord>();
159     record1->AddEntry("general.file-uri", obj1);
160     std::shared_ptr<UnifiedRecord> record2 = std::make_shared<UnifiedRecord>();
161     record2->AddEntry("general.file-uri", "1111");
162     std::shared_ptr<UnifiedRecord> record3 = std::make_shared<UnifiedRecord>();
163     record3->AddEntry("general.file-uri", 1);
164     std::vector<std::shared_ptr<UnifiedRecord>> records = { record, record1, record2, record3 };
165     std::vector<std::string> uris;
__anonff21d6d20102(std::shared_ptr<Object> obj) 166     PreProcessUtils::ProcessFileType(records, [&uris](std::shared_ptr<Object> obj) {
167         std::string oriUri;
168         obj->GetValue(ORI_URI, oriUri);
169         if (oriUri.empty()) {
170             return false;
171         }
172         uris.push_back(oriUri);
173         return true;
174     });
175     EXPECT_EQ(uris.size(), 1);
176 }
177 
178 /**
179 * @tc.name: GetHtmlFileUris001
180 * @tc.desc: Abnormal test of GetHtmlFileUris, uris is invalid
181 * @tc.type: FUNC
182 * @tc.require:
183 */
184 HWTEST_F(UdmfPreProcessUtilsTest, GetHtmlFileUris001, TestSize.Level1)
185 {
186     uint32_t tokenId = 0;
187     UnifiedData data;
188     bool isLocal = false;
189     std::vector<std::string> uris = {"test"};
190     PreProcessUtils preProcessUtils;
191     EXPECT_NO_FATAL_FAILURE(preProcessUtils.GetHtmlFileUris(tokenId, data, isLocal, uris));
192 }
193 }; // namespace UDMF