• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "CustomUtdStoreTest"
16 #include <gtest/gtest.h>
17 
18 #include <unistd.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 
22 #include "logger.h"
23 #include "custom_utd_store.h"
24 #include "utd_common.h"
25 
26 using namespace testing::ext;
27 using namespace OHOS::UDMF;
28 using namespace OHOS;
29 namespace OHOS::Test {
30 constexpr const int32_t USERID = 1000000;
31 constexpr const char* TEST_CFG_FILE = "/data/1000000/utd-adt.json";
32 constexpr const char* TEST_CFG_DIR = "/data/1000000/";
33 
34 constexpr const char* TEST_DATA2 = "{\
35     \"CustomUTDs\": [{\
36     \"typeId\": \"com.example.utdtest.document\",\
37     \"belongingToTypes\": [\"com.example.utdtest2.document\"],\
38     \"FilenameExtensions\": [\".mydocument\", \".mydoc\"],\
39     \"mimeTypes\": [\"application/my-document\", \"application/my-doc\"],\
40     \"description\": \"My document.\",\
41     \"referenceURL\": \"http://www.mycompany.com/my-document.html\",\
42     \"iconFile\": \"resources/my-document.png\",\
43     \"installerBundles\":[\"com.example.utdtest\"],\
44     \"ownerBundle\":\"com.example.utdtest\"\
45     }, {\
46     \"typeId\": \"com.example.utdtest2.document\",\
47     \"belongingToTypes\": [\"general.object\"],\
48     \"FilenameExtensions\": [\".mydocument2\", \".mydoc2\"],\
49     \"mimeTypes\": [\"application/my-document2\", \"application/my-doc2\"],\
50     \"description\": \"My document 2.\",\
51     \"referenceURL\": \"hhttp://www.mycompany.com/my-document2.html\",\
52     \"iconFile\": \"resources/my-document2.png\",\
53     \"installerBundles\":[\"com.example.utdtest2\", \"com.example.utdtest\"],\
54     \"ownerBundle\":\"com.example.utdtest2\"\
55     }]}";
56 
57 class CustomUtdStoreTest : public testing::Test {
58 public:
59     static void SetUpTestCase();
60     static void TearDownTestCase();
61     void SetUp() override;
62     void TearDown() override;
63 };
64 
SetUpTestCase()65 void CustomUtdStoreTest::SetUpTestCase()
66 {
67 }
68 
TearDownTestCase()69 void CustomUtdStoreTest::TearDownTestCase()
70 {
71 }
72 
SetUp()73 void CustomUtdStoreTest::SetUp()
74 {
75 }
76 
TearDown()77 void CustomUtdStoreTest::TearDown()
78 {
79     if (remove(TEST_CFG_FILE) == 0) {
80         rmdir(TEST_CFG_DIR);
81         LOG_INFO(UDMF_TEST, "Removed file success, %{public}s.", TEST_CFG_FILE);
82     } else {
83         LOG_INFO(UDMF_TEST, "Failed to remove the file., %{public}s.", TEST_CFG_FILE);
84     }
85 }
86 
87 /**
88 * @tc.name: SaveTypeCfgs001
89 * @tc.desc: SaveTypeCfgs
90 * @tc.type: FUNC
91 */
92 HWTEST_F(CustomUtdStoreTest, SaveTypeCfgs001, TestSize.Level1)
93 {
94     LOG_INFO(UDMF_TEST, "SaveTypeCfgs001 begin.");
95     std::vector<TypeDescriptorCfg> typesCfg;
96     CustomUtdJsonParser parser;
97     parser.ParseStoredCustomUtdJson(TEST_DATA2, typesCfg);
98     auto status = CustomUtdStore::GetInstance().SaveTypeCfgs(typesCfg, USERID);
99     EXPECT_EQ(status, E_OK);
100 
101     typesCfg.clear();
102     typesCfg = CustomUtdStore::GetInstance().GetCustomUtd(false, USERID);
103     TypeDescriptorCfg type1 = *(typesCfg.begin());
104     EXPECT_EQ(type1.typeId, "com.example.utdtest.document");
105     EXPECT_EQ(*(type1.belongingToTypes.begin()), "com.example.utdtest2.document");
106     EXPECT_EQ(*(type1.filenameExtensions.begin()), ".mydocument");
107     EXPECT_EQ(*(type1.mimeTypes.begin()), "application/my-document");
108     EXPECT_EQ(type1.description, "My document.");
109     EXPECT_EQ(type1.referenceURL, "http://www.mycompany.com/my-document.html");
110     EXPECT_EQ(type1.iconFile, "resources/my-document.png");
111     EXPECT_EQ(*(type1.installerBundles.begin()), "com.example.utdtest");
112     EXPECT_EQ(type1.ownerBundle, "com.example.utdtest");
113 
114     LOG_INFO(UDMF_TEST, "SaveTypeCfgs001 end.");
115 }
116 
117 /**
118 * @tc.name: GetCustomUtdPathTest001
119 * @tc.desc: GetCustomUtdPath
120 * @tc.type: FUNC
121 */
122 HWTEST_F(CustomUtdStoreTest, GetCustomUtdPathTest001, TestSize.Level1)
123 {
124     LOG_INFO(UDMF_TEST, "GetCustomUtdPathTest001 begin.");
125 
126     std::string path = CustomUtdStore::GetInstance().GetCustomUtdPath(false, USERID);
127     EXPECT_EQ(path, "/data/service/el1/1000000/utdtypes/utd/utd-adt.json");
128 
129     LOG_INFO(UDMF_TEST, "GetCustomUtdPathTest001 end.");
130 }
131 
132 /**
133 * @tc.name: GetCustomUtdPathTest002
134 * @tc.desc: GetCustomUtdPath
135 * @tc.type: FUNC
136 */
137 HWTEST_F(CustomUtdStoreTest, GetCustomUtdPathTest002, TestSize.Level1)
138 {
139     LOG_INFO(UDMF_TEST, "GetCustomUtdPathTest002 begin.");
140 
141     std::string path = CustomUtdStore::GetInstance().GetCustomUtdPath(true, USERID);
142     EXPECT_EQ(path, "/data/utd/utd-adt.json");
143 
144     LOG_INFO(UDMF_TEST, "GetCustomUtdPathTest002 end.");
145 }
146 
147 /**
148 * @tc.name: GetCustomUtdInfoTest001
149 * @tc.desc: GetCustomUtdInfo
150 * @tc.type: FUNC
151 */
152 HWTEST_F(CustomUtdStoreTest, GetCustomUtdInfoTest001, TestSize.Level1)
153 {
154     LOG_INFO(UDMF_TEST, "GetCustomUtdInfoTest001 begin.");
155 
156     UtdFileInfo info = CustomUtdStore::GetInstance().GetCustomUtdInfo(true, USERID);
157     EXPECT_EQ(info.size, 0);
158 
159     LOG_INFO(UDMF_TEST, "GetCustomUtdInfoTest001 end.");
160 }
161 
162 /**
163 * @tc.name: GetCustomUtdInfoTest002
164 * @tc.desc: GetCustomUtdInfo
165 * @tc.type: FUNC
166 */
167 HWTEST_F(CustomUtdStoreTest, GetCustomUtdInfoTest002, TestSize.Level1)
168 {
169     LOG_INFO(UDMF_TEST, "GetCustomUtdInfoTest002 begin.");
170 
171     UtdFileInfo info = CustomUtdStore::GetInstance().GetCustomUtdInfo(false, USERID);
172     EXPECT_EQ(info.size, strlen(TEST_DATA2) + 2);
173     auto customUtds = CustomUtdStore::GetInstance().GetCustomUtd(false, USERID);
174     EXPECT_EQ(customUtds.size(), 2);
175 
176     LOG_INFO(UDMF_TEST, "GetCustomUtdInfoTest002 end.");
177 }
178 
179 /**
180 * @tc.name: ParseStoredCustomUtdJson001
181 * @tc.desc: empty json
182 * @tc.type: FUNC
183 */
184 HWTEST_F(CustomUtdStoreTest, ParseStoredCustomUtdJson001, TestSize.Level1) {
185     LOG_INFO(UDMF_TEST, "ParseStoredCustomUtdJson001 begin.");
186     CustomUtdJsonParser parser;
187     std::vector<TypeDescriptorCfg> typesCfg;
188     bool ret = parser.ParseStoredCustomUtdJson("", typesCfg);
189 
190     EXPECT_FALSE(ret);
191     EXPECT_TRUE(typesCfg.empty());
192     LOG_INFO(UDMF_TEST, "ParseStoredCustomUtdJson001 end.");
193 }
194 
195 /**
196 * @tc.name: ParseStoredCustomUtdJson002
197 * @tc.desc: Invalid Format
198 * @tc.type: FUNC
199 */
200 HWTEST_F(CustomUtdStoreTest, ParseStoredCustomUtdJson002, TestSize.Level1)
201 {
202     LOG_INFO(UDMF_TEST, "ParseStoredCustomUtdJson002 begin.");
203     const std::string invalidJson = R"({
204         "UniformDataTypeDeclarations": []
205     )"; // syntax error
206 
207     CustomUtdJsonParser parser;
208     std::vector<TypeDescriptorCfg> typesCfg;
209     bool ret = parser.ParseStoredCustomUtdJson(invalidJson, typesCfg);
210 
211     EXPECT_FALSE(ret);
212     EXPECT_TRUE(typesCfg.empty());
213     LOG_INFO(UDMF_TEST, "ParseStoredCustomUtdJson002 end.");
214 }
215 
216 /**
217 * @tc.name: ParseStoredCustomUtdJson003
218 * @tc.desc: invalid json
219 * @tc.type: FUNC
220 */
221 HWTEST_F(CustomUtdStoreTest, ParseStoredCustomUtdJson003, TestSize.Level1) {
222     LOG_INFO(UDMF_TEST, "ParseStoredCustomUtdJson003 begin.");
223     CustomUtdJsonParser parser;
224     std::vector<TypeDescriptorCfg> typesCfg;
225     std::string invalidJson = "{invalid_json}";
226     EXPECT_FALSE(parser.ParseStoredCustomUtdJson(invalidJson, typesCfg));
227     EXPECT_TRUE(typesCfg.empty());
228     LOG_INFO(UDMF_TEST, "ParseStoredCustomUtdJson003 end.");
229 }
230 
231 /**
232 * @tc.name: ParseStoredCustomUtdJson004
233 * @tc.desc: root not object
234 * @tc.type: FUNC
235 */
236 HWTEST_F(CustomUtdStoreTest, ParseStoredCustomUtdJson004, TestSize.Level1) {
237     LOG_INFO(UDMF_TEST, "ParseStoredCustomUtdJson004 begin.");
238     const std::string rootArrayJson = R"([
239         { "TypeId": "com.example.custom.image" }
240     ])";
241 
242     CustomUtdJsonParser parser;
243     std::vector<TypeDescriptorCfg> typesCfg;
244     bool ret = parser.ParseStoredCustomUtdJson(rootArrayJson, typesCfg);
245 
246     EXPECT_FALSE(ret);
247     EXPECT_TRUE(typesCfg.empty());
248     LOG_INFO(UDMF_TEST, "ParseStoredCustomUtdJson004 end.");
249 }
250 
251 /**
252 * @tc.name: ParseStoredCustomUtdJson005
253 * @tc.desc: valid json
254 * @tc.type: FUNC
255 */
256 HWTEST_F(CustomUtdStoreTest, ParseStoredCustomUtdJson005, TestSize.Level1) {
257     LOG_INFO(UDMF_TEST, "ParseStoredCustomUtdJson005 begin.");
258 
259     const std::string validJson = R"({
260         "CustomUTDs": [
261             {
262                 "TypeId": "com.example.custom.image",
263                 "BelongingToTypes": ["general.image"],
264                 "FilenameExtensions": [".img", ".pic"],
265                 "MIMETypes": ["application/img", "application/pic"],
266                 "Description": "Custom image type",
267                 "ReferenceURL": ""
268             }
269         ]
270     })";
271 
272     CustomUtdJsonParser parser;
273     std::vector<TypeDescriptorCfg> typesCfg;
274     bool ret = parser.ParseStoredCustomUtdJson(validJson, typesCfg);
275 
276     EXPECT_TRUE(ret);
277     ASSERT_EQ(typesCfg.size(), 1u);
278     EXPECT_EQ(typesCfg[0].typeId, "com.example.custom.image");
279     EXPECT_EQ(typesCfg[0].belongingToTypes.size(), 1u);
280     EXPECT_EQ(typesCfg[0].belongingToTypes[0], "general.image");
281 
282     LOG_INFO(UDMF_TEST, "ParseStoredCustomUtdJson005 end.");
283 }
284 
285 /**
286  * @tc.name: ParseUserCustomUtdJsonTest001
287  * @tc.desc: parse a valid JSON containing declarations and references.
288  * @tc.type: FUNC
289  */
290 HWTEST_F(CustomUtdStoreTest, ParseUserCustomUtdJsonTest001, TestSize.Level1)
291 {
292     LOG_INFO(UDMF_TEST, "ParseUserCustomUtdJsonTest001 begin.");
293     const char *testJson = R"({
294         "UniformDataTypeDeclarations": [
295             {
296                 "TypeId": "com.example.thread.image",
297                 "BelongingToTypes": ["general.image"],
298                 "FilenameExtensions": [".myImage", ".khImage"],
299                 "MIMETypes": ["application/myImage", "application/khImage"],
300                 "Description": "My Image.",
301                 "ReferenceURL": ""
302             }
303         ],
304         "ReferenceUniformDataTypeDeclarations": [
305             {
306                 "TypeId": "com.example.thread.audio",
307                 "BelongingToTypes": ["general.audio"],
308                 "FilenameExtensions": [".myAudio", ".khAudio"],
309                 "MIMETypes": ["application/myAudio", "application/khAudio"],
310                 "Description": "My audio.",
311                 "ReferenceURL": ""
312             }
313         ]
314     })";
315 
316     std::vector<TypeDescriptorCfg> declarations;
317     std::vector<TypeDescriptorCfg> references;
318 
319     CustomUtdJsonParser parser;
320     bool result = parser.ParseUserCustomUtdJson(testJson, declarations, references);
321 
322     EXPECT_TRUE(result);
323     EXPECT_EQ(declarations.size(), 1);
324     EXPECT_EQ(references.size(), 1);
325 
326     EXPECT_EQ(declarations[0].typeId, "com.example.thread.image");
327     EXPECT_EQ(references[0].typeId, "com.example.thread.audio");
328     LOG_INFO(UDMF_TEST, "ParseUserCustomUtdJsonTest001 end.");
329 }
330 
331 /**
332  * @tc.name: ParseUserCustomUtdJsonTest002
333  * @tc.desc: Verify ParseUserCustomUtdJson returns false when given empty JSON string.
334  * @tc.type: FUNC
335  */
336 HWTEST_F(CustomUtdStoreTest, ParseUserCustomUtdJsonTest002, TestSize.Level1)
337 {
338     LOG_INFO(UDMF_TEST, "ParseUserCustomUtdJsonTest002 begin.");
339     std::vector<TypeDescriptorCfg> declarations;
340     std::vector<TypeDescriptorCfg> references;
341 
342     CustomUtdJsonParser parser;
343     bool result = parser.ParseUserCustomUtdJson("", declarations, references);
344 
345     EXPECT_FALSE(result);
346     EXPECT_TRUE(declarations.empty());
347     EXPECT_TRUE(references.empty());
348     LOG_INFO(UDMF_TEST, "ParseUserCustomUtdJsonTest002 end.");
349 }
350 
351 /**
352  * @tc.name: ParseUserCustomUtdJsonTest003
353  * @tc.desc: Verify ParseUserCustomUtdJson returns false for invalid JSON format.
354  * @tc.type: FUNC
355  */
356 HWTEST_F(CustomUtdStoreTest, ParseUserCustomUtdJsonTest003, TestSize.Level1)
357 {
358     LOG_INFO(UDMF_TEST, "ParseUserCustomUtdJsonTest003 begin.");
359     const char *invalidJson = R"({ "UniformDataTypeDeclarations": [ )";
360 
361     std::vector<TypeDescriptorCfg> declarations;
362     std::vector<TypeDescriptorCfg> references;
363 
364     CustomUtdJsonParser parser;
365     bool result = parser.ParseUserCustomUtdJson(invalidJson, declarations, references);
366 
367     EXPECT_FALSE(result);
368     EXPECT_TRUE(declarations.empty());
369     EXPECT_TRUE(references.empty());
370     LOG_INFO(UDMF_TEST, "ParseUserCustomUtdJsonTest003 end.");
371 }
372 
373 /**
374  * @tc.name: ParseUserCustomUtdJsonTest004
375  * @tc.desc: Verify ParseUserCustomUtdJson returns false for invalid JSON format.
376  * @tc.type: FUNC
377  */
378 HWTEST_F(CustomUtdStoreTest, ParseUserCustomUtdJsonTest004, TestSize.Level1)
379 {
380     LOG_INFO(UDMF_TEST, "ParseUserCustomUtdJsonTest004 begin.");
381     const char *invalidJson = R"([
382         { "TypeId": "com.example.custom.image" }
383     ])";
384 
385     std::vector<TypeDescriptorCfg> declarations;
386     std::vector<TypeDescriptorCfg> references;
387 
388     CustomUtdJsonParser parser;
389     bool result = parser.ParseUserCustomUtdJson(invalidJson, declarations, references);
390 
391     EXPECT_FALSE(result);
392     EXPECT_TRUE(declarations.empty());
393     EXPECT_TRUE(references.empty());
394     LOG_INFO(UDMF_TEST, "ParseUserCustomUtdJsonTest004 end.");
395 }
396 } // OHOS::Test