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 
16 #define LOG_TAG "UtdCfgsCheckerTest"
17 
18 #include "utd_cfgs_checker.h"
19 #include <gtest/gtest.h>
20 #include "logger.h"
21 
22 using namespace testing::ext;
23 using namespace OHOS::UDMF;
24 using namespace OHOS;
25 
26 namespace OHOS::Test {
27 class UtdCfgsCheckerTest : public testing::Test {
28 public:
29     static void SetUpTestCase(void);
30     static void TearDownTestCase(void);
31     void SetUp();
32     void TearDown();
33 };
34 
SetUpTestCase(void)35 void UtdCfgsCheckerTest::SetUpTestCase(void) {}
TearDownTestCase(void)36 void UtdCfgsCheckerTest::TearDownTestCase(void) {}
SetUp(void)37 void UtdCfgsCheckerTest::SetUp(void) {}
TearDown(void)38 void UtdCfgsCheckerTest::TearDown(void) {}
39 
40 /**
41  * @tc.name: CheckTypeDescriptors_001
42  * @tc.desc: Normal testcase of CheckTypeDescriptors
43  * @tc.type: FUNC
44  */
45 HWTEST_F(UtdCfgsCheckerTest, CheckTypeDescriptors_001, TestSize.Level1)
46 {
47     LOG_INFO(UDMF_TEST, "CheckTypeDescriptors_001 begin.");
48     CustomUtdCfgs typeCfgs;
49     TypeDescriptorCfg tdc1;
50     tdc1.typeId = "com.demo.test.type";
51     tdc1.filenameExtensions = {".abc"};
52     tdc1.belongingToTypes = {"com.demo.test.parent"};
53     tdc1.mimeTypes = {"parent/abc"};
54     std::vector<TypeDescriptorCfg> first = {tdc1};
55     TypeDescriptorCfg tdc2;
56     tdc2.typeId = "com.demo.test.type2";
57     tdc2.filenameExtensions = {".abc2"};
58     tdc2.belongingToTypes = {"com.demo.test.parent2"};
59     tdc2.mimeTypes = {"parent2/abc"};
60     std::vector<TypeDescriptorCfg> second = {tdc2};
61     CustomUtdCfgs customUtdCfgs = {first, second};
62 
63     TypeDescriptorCfg preTdc1;
64     preTdc1.typeId = "com.demo.test.parent";
65     TypeDescriptorCfg preTdc2;
66     preTdc2.typeId = "com.demo.test.parent2";
67     std::vector<TypeDescriptorCfg> presetCfgs = {preTdc1, preTdc2};
68 
69     std::vector<TypeDescriptorCfg> customCfgs = {};
70     std::string bundleName("com.demo.test");
71 
72     bool result = UtdCfgsChecker::GetInstance().CheckTypeDescriptors(customUtdCfgs, presetCfgs, customCfgs, bundleName);
73     EXPECT_TRUE(result);
74     LOG_INFO(UDMF_TEST, "CheckTypeDescriptors_001 end.");
75 }
76 
77 /**
78  * @tc.name: CheckTypeDescriptors_002
79  * @tc.desc: testcase of CheckTypeDescriptors: failed to match the regular expression
80  * @tc.type: FUNC
81  */
82 HWTEST_F(UtdCfgsCheckerTest, CheckTypeDescriptors_002, TestSize.Level1)
83 {
84     LOG_INFO(UDMF_TEST, "CheckTypeDescriptors_002 begin.");
85     CustomUtdCfgs typeCfgs;
86     TypeDescriptorCfg tdc1;
87     tdc1.typeId = "com.demo.test.type";
88     std::vector<TypeDescriptorCfg> first = {tdc1};
89     TypeDescriptorCfg tdc2;
90     tdc2.typeId = "errorTypeId";
91     std::vector<TypeDescriptorCfg> second = {tdc2};
92     CustomUtdCfgs customUtdCfgs1 = {first, second};
93 
94     TypeDescriptorCfg preTdc1;
95     preTdc1.typeId = "com.demo.test.parent";
96     TypeDescriptorCfg preTdc2;
97     preTdc2.typeId = "com.demo.test.parent2";
98     std::vector<TypeDescriptorCfg> presetCfgs = {preTdc1, preTdc2};
99 
100     std::vector<TypeDescriptorCfg> customCfgs = {};
101     std::string bundleName("com.demo.test");
102 
103     bool result = UtdCfgsChecker::GetInstance().CheckTypeDescriptors(customUtdCfgs1, presetCfgs, customCfgs,
104         bundleName);
105     EXPECT_FALSE(result);
106 
107     CustomUtdCfgs customUtdCfgs2 = {second, first};
108     result = UtdCfgsChecker::GetInstance().CheckTypeDescriptors(customUtdCfgs2, presetCfgs, customCfgs, bundleName);
109     EXPECT_FALSE(result);
110     LOG_INFO(UDMF_TEST, "CheckTypeDescriptors_002 end.");
111 }
112 
113 /**
114  * @tc.name: CheckTypeDescriptors_003
115  * @tc.desc: testcase of CheckTypeDescriptors: empty testcase
116  * @tc.type: FUNC
117  */
118 HWTEST_F(UtdCfgsCheckerTest, CheckTypeDescriptors_003, TestSize.Level1)
119 {
120     LOG_INFO(UDMF_TEST, "CheckTypeDescriptors_003 begin.");
121     CustomUtdCfgs typeCfgs;
122     std::vector<TypeDescriptorCfg> first = {};
123     std::vector<TypeDescriptorCfg> second = {};
124     CustomUtdCfgs customUtdCfgs = {first, second};
125     std::vector<TypeDescriptorCfg> presetCfgs = {};
126     std::vector<TypeDescriptorCfg> customCfgs = {};
127     std::string bundleName("com.demo.test");
128 
129     bool result = UtdCfgsChecker::GetInstance().CheckTypeDescriptors(customUtdCfgs, presetCfgs, customCfgs, bundleName);
130     EXPECT_FALSE(result);
131     LOG_INFO(UDMF_TEST, "CheckTypeDescriptors_003 end.");
132 }
133 
134 /**
135  * @tc.name: CheckTypeDescriptors_004
136  * @tc.desc: testcase of CheckTypeDescriptors: filename, belongingToType or mimeType not match
137  * @tc.type: FUNC
138  */
139 HWTEST_F(UtdCfgsCheckerTest, CheckTypeDescriptors_004, TestSize.Level1)
140 {
141     LOG_INFO(UDMF_TEST, "CheckTypeDescriptors_004 begin.");
142     CustomUtdCfgs typeCfgs;
143     TypeDescriptorCfg tdc1;
144     tdc1.typeId = "com.demo.test.type";
145     std::vector<TypeDescriptorCfg> first = {tdc1};
146     TypeDescriptorCfg tdc2;
147     tdc2.typeId = "com.demo.test.type2";
148     tdc2.filenameExtensions = {".abc2"};
149     tdc2.belongingToTypes = {"com.demo.test.parent2"};
150     tdc2.mimeTypes = {"parent2/abc"};
151     std::vector<TypeDescriptorCfg> second = {tdc2};
152     CustomUtdCfgs customUtdCfgs = {first, second};
153 
154     TypeDescriptorCfg preTdc1;
155     preTdc1.typeId = "com.demo.test.parent";
156     TypeDescriptorCfg preTdc2;
157     preTdc2.typeId = "com.demo.test.parent2";
158     std::vector<TypeDescriptorCfg> presetCfgs = {preTdc1, preTdc2};
159 
160     std::vector<TypeDescriptorCfg> customCfgs = {};
161     std::string bundleName("com.demo.test");
162 
163     bool result = UtdCfgsChecker::GetInstance().CheckTypeDescriptors(customUtdCfgs, presetCfgs, customCfgs, bundleName);
164     EXPECT_FALSE(result);
165 
166     tdc1.filenameExtensions = {".abc"};
167     first = {tdc1};
168     customUtdCfgs = {first, second};
169     result = UtdCfgsChecker::GetInstance().CheckTypeDescriptors(customUtdCfgs, presetCfgs, customCfgs, bundleName);
170     EXPECT_FALSE(result);
171 
172     tdc1.belongingToTypes = {"com.demo.test.parent"};
173     tdc1.mimeTypes = {""};
174     first = {tdc1};
175     customUtdCfgs = {first, second};
176     result = UtdCfgsChecker::GetInstance().CheckTypeDescriptors(customUtdCfgs, presetCfgs, customCfgs, bundleName);
177     EXPECT_FALSE(result);
178 
179     tdc1.mimeTypes = {"parent/abc"};
180     first = {tdc1};
181     customUtdCfgs = {first, second};
182     result = UtdCfgsChecker::GetInstance().CheckTypeDescriptors(customUtdCfgs, presetCfgs, customCfgs, bundleName);
183     EXPECT_TRUE(result);
184     LOG_INFO(UDMF_TEST, "CheckTypeDescriptors_004 end.");
185 }
186 
187 /**
188  * @tc.name: CheckBelongingToTypes_001
189  * @tc.desc: Normal testcase of CheckTypeDescriptors
190  * @tc.type: FUNC
191  */
192 HWTEST_F(UtdCfgsCheckerTest, CheckBelongingToTypes_001, TestSize.Level1)
193 {
194     LOG_INFO(UDMF_TEST, "CheckBelongingToTypes_001 begin.");
195     TypeDescriptorCfg tdc1;
196     tdc1.typeId = "com.demo.test.type";
197     tdc1.filenameExtensions = {".abc"};
198     tdc1.belongingToTypes = {""};
199     tdc1.mimeTypes = {"parent/abc"};
200     std::vector<TypeDescriptorCfg> typeCfgs = {tdc1};
201 
202     TypeDescriptorCfg preTdc1;
203     preTdc1.typeId = "com.demo.test.parent";
204     TypeDescriptorCfg preTdc2;
205     preTdc2.typeId = "com.demo.test.parent2";
206     std::vector<TypeDescriptorCfg> presetCfgs = {preTdc1, preTdc2};
207 
208     bool result = UtdCfgsChecker::GetInstance().CheckBelongingToTypes(typeCfgs, presetCfgs);
209     EXPECT_FALSE(result);
210 
211     tdc1.belongingToTypes = {"com.demo.test.type"};
212     typeCfgs = {tdc1};
213     result = UtdCfgsChecker::GetInstance().CheckBelongingToTypes(typeCfgs, presetCfgs);
214     EXPECT_FALSE(result);
215 
216     tdc1.belongingToTypes = {"com.demo.test.parent3"};
217     typeCfgs = {tdc1};
218     result = UtdCfgsChecker::GetInstance().CheckBelongingToTypes(typeCfgs, presetCfgs);
219     EXPECT_FALSE(result);
220 
221     tdc1.belongingToTypes = {"com.demo.test.parent"};
222     typeCfgs = {tdc1};
223     result = UtdCfgsChecker::GetInstance().CheckBelongingToTypes(typeCfgs, presetCfgs);
224     EXPECT_TRUE(result);
225 
226     LOG_INFO(UDMF_TEST, "CheckBelongingToTypes_001 end.");
227 }
228 
229 /**
230  * @tc.name: CheckTypeDescriptors_005
231  * @tc.desc: Normal testcase of CheckTypeDescriptors
232  * @tc.type: FUNC
233  */
234 HWTEST_F(UtdCfgsCheckerTest, CheckTypeDescriptors_005, TestSize.Level1)
235 {
236     LOG_INFO(UDMF_TEST, "CheckTypeDescriptors_005 begin.");
237     TypeDescriptorCfg tdc1;
238     tdc1.typeId = "com.demo.test.type";
239     tdc1.filenameExtensions = {".abc"};
240     tdc1.belongingToTypes = {"com.demo.test.parent"};
241     tdc1.mimeTypes = {"parent/abc"};
242     TypeDescriptorCfg tdc2;
243     tdc2.typeId = "com.demo.test.type2";
244     tdc2.filenameExtensions = {".abc2"};
245     tdc2.belongingToTypes = {"com.demo.test.parent2"};
246     tdc2.mimeTypes = {"parent2/abc"};
247     std::vector<TypeDescriptorCfg> first = {tdc1, tdc2};
248     TypeDescriptorCfg tdc3;
249     tdc3.typeId = "com.demo.test.parent";
250     tdc3.filenameExtensions = {".abc3"};
251     tdc3.belongingToTypes = {"com.demo.test.parent3"};
252     tdc3.mimeTypes = {"parent3/abc"};
253     TypeDescriptorCfg tdc4;
254     tdc4.typeId = "com.demo.test.parent2";
255     tdc4.filenameExtensions = {".abc4"};
256     tdc4.belongingToTypes = {"com.demo.test.parent4"};
257     tdc4.mimeTypes = {"parent4/abc"};
258     std::vector<TypeDescriptorCfg> second = {tdc3, tdc4};
259     CustomUtdCfgs customUtdCfgs = {first, second};
260 
261     TypeDescriptorCfg presetCfg1;
262     presetCfg1.typeId = "com.demo.test.parent3";
263     presetCfg1.filenameExtensions = {".abca3"};
264     presetCfg1.belongingToTypes = {"com.demo.test.parent5"};
265     presetCfg1.mimeTypes = {"parent3/abc"};
266     TypeDescriptorCfg presetCfg2;
267     presetCfg2.typeId = "com.demo.test.parent4";
268     presetCfg2.filenameExtensions = {".abca3"};
269     presetCfg2.belongingToTypes = {"com.demo.test.parent6"};
270     presetCfg2.mimeTypes = {"parent3/abc"};
271     std::vector<TypeDescriptorCfg> presetCfgs = {presetCfg1, presetCfg2};
272 
273     std::vector<TypeDescriptorCfg> customCfgs = {tdc1, tdc3};
274 
275     std::string bundleName("com.demo.test");
276     bool result = UtdCfgsChecker::GetInstance().CheckTypeDescriptors(customUtdCfgs, presetCfgs, customCfgs, bundleName);
277     EXPECT_TRUE(result);
278     LOG_INFO(UDMF_TEST, "CheckTypeDescriptors_005 end.");
279 }
280 
281 }
282