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 "FlexibleTypeTest"
16 
17 #include <unistd.h>
18 #include <gtest/gtest.h>
19 #include <string>
20 
21 #include "logger.h"
22 #include "flexible_type.h"
23 
24 using namespace testing::ext;
25 using namespace OHOS::UDMF;
26 using namespace OHOS;
27 namespace OHOS::Test {
28 using namespace std;
29 
30 class FlexibleTypeTest : public testing::Test {
31 public:
32     static void SetUpTestCase();
33     static void TearDownTestCase();
34     void SetUp() override;
35     void TearDown() override;
36 };
37 
SetUpTestCase()38 void FlexibleTypeTest::SetUpTestCase()
39 {
40 }
41 
TearDownTestCase()42 void FlexibleTypeTest::TearDownTestCase()
43 {
44 }
45 
SetUp()46 void FlexibleTypeTest::SetUp()
47 {
48 }
49 
TearDown()50 void FlexibleTypeTest::TearDown()
51 {
52 }
53 
54 /**
55 * @tc.name: EscapeStr001
56 * @tc.desc: Normal testcase of EscapeStr
57 * @tc.type: FUNC
58 */
59 HWTEST_F(FlexibleTypeTest, EscapeStr001, TestSize.Level1)
60 {
61     LOG_INFO(UDMF_TEST, "EscapeStr001 begin.");
62     const std::string chs = "string?";
63     string output = FlexibleType::EscapeStr(chs);
64     EXPECT_EQ(output, "string\\?");
65     LOG_INFO(UDMF_TEST, "EscapeStr001 end.");
66 }
67 
68 /**
69 * @tc.name: EscapeStr002
70 * @tc.desc: Normal testcase of EscapeStr
71 * @tc.type: FUNC
72 */
73 HWTEST_F(FlexibleTypeTest, EscapeStr002, TestSize.Level1)
74 {
75     LOG_INFO(UDMF_TEST, "EscapeStr002 begin.");
76     const std::string chs = "string:";
77     string output = FlexibleType::EscapeStr(chs);
78     EXPECT_EQ(output, "string\\:");
79     LOG_INFO(UDMF_TEST, "EscapeStr002 end.");
80 }
81 
82 /**
83 * @tc.name: EscapeStr003
84 * @tc.desc: Normal testcase of EscapeStr
85 * @tc.type: FUNC
86 */
87 HWTEST_F(FlexibleTypeTest, EscapeStr003, TestSize.Level1)
88 {
89     LOG_INFO(UDMF_TEST, "EscapeStr003 begin.");
90     const std::string chs = "string=";
91     string output = FlexibleType::EscapeStr(chs);
92     EXPECT_EQ(output, "string\\=");
93     LOG_INFO(UDMF_TEST, "EscapeStr003 end.");
94 }
95 
96 /**
97 * @tc.name: EscapeStr004
98 * @tc.desc: Normal testcase of EscapeStr
99 * @tc.type: FUNC
100 */
101 HWTEST_F(FlexibleTypeTest, EscapeStr004, TestSize.Level1)
102 {
103     LOG_INFO(UDMF_TEST, "EscapeStr004 begin.");
104     const std::string chs = "string,";
105     string output = FlexibleType::EscapeStr(chs);
106     EXPECT_EQ(output, "string\\,");
107     LOG_INFO(UDMF_TEST, "EscapeStr004 end.");
108 }
109 
110 /**
111 * @tc.name: EscapeStr005
112 * @tc.desc: Normal testcase of EscapeStr
113 * @tc.type: FUNC
114 */
115 HWTEST_F(FlexibleTypeTest, EscapeStr005, TestSize.Level1)
116 {
117     LOG_INFO(UDMF_TEST, "EscapeStr005 begin.");
118     const std::string chs = "string\\";
119     string output = FlexibleType::EscapeStr(chs);
120     EXPECT_EQ(output, "string\\\\");
121     LOG_INFO(UDMF_TEST, "EscapeStr005 end.");
122 }
123 
124 /**
125 * @tc.name: EscapeStr006
126 * @tc.desc: Abnormal testcase of EscapeStr, because chs doesn't include '?'、 ':'、 '='、',' and '\\'
127 * @tc.type: FUNC
128 */
129 HWTEST_F(FlexibleTypeTest, EscapeStr006, TestSize.Level1)
130 {
131     LOG_INFO(UDMF_TEST, "EscapeStr006 begin.");
132     const std::string chs = "string";
133     string output = FlexibleType::EscapeStr(chs);
134     EXPECT_EQ(output, "string");
135     LOG_INFO(UDMF_TEST, "EscapeStr006 end.");
136 }
137 
138 /**
139 * @tc.name: ParseFlexibleUtd001
140 * @tc.desc: Abnormal testcase of ParseFlexibleUtd, because typeId and FLEXIBLE_TYPE_FLAG are equal
141 * @tc.type: FUNC
142 */
143 HWTEST_F(FlexibleTypeTest, ParseFlexibleUtd001, TestSize.Level1)
144 {
145     LOG_INFO(UDMF_TEST, "ParseFlexibleUtd001 begin.");
146     const std::string typeId = "flex.z";
147     TypeDescriptorCfg flexibleTypeDescriptorCfg;
148     flexibleTypeDescriptorCfg.typeId = "com.example.utdtest.document";
149     flexibleTypeDescriptorCfg.belongingToTypes = {"com.example.utdtest2.document"};
150     flexibleTypeDescriptorCfg.filenameExtensions = {".mydocument"};
151     flexibleTypeDescriptorCfg.mimeTypes = {"application/my-document"};
152     flexibleTypeDescriptorCfg.description = "My document.";
153     flexibleTypeDescriptorCfg.referenceURL = "http://www.mycompany.com/my-document.html";
154     flexibleTypeDescriptorCfg.iconFile = "resources/my-document.png";
155     flexibleTypeDescriptorCfg.ownerBundle = "com.example.utdtest";
156     flexibleTypeDescriptorCfg.installerBundles = {"com.example.utdtest"};
157     FlexibleType flexibleType;
158     bool ret = flexibleType.ParseFlexibleUtd(typeId, flexibleTypeDescriptorCfg);
159     EXPECT_FALSE(ret);
160     LOG_INFO(UDMF_TEST, "ParseFlexibleUtd001 end.");
161 }
162 
163 /**
164 * @tc.name: ParseFlexibleUtd002
165 * @tc.desc: Abnormal testcase of ParseFlexibleUtd, because flexibleUtdDecode[0] != '?'
166 * @tc.type: FUNC
167 */
168 HWTEST_F(FlexibleTypeTest, ParseFlexibleUtd002, TestSize.Level1)
169 {
170     LOG_INFO(UDMF_TEST, "ParseFlexibleUtd002 begin.");
171     const std::string typeId = "com.example.utdtest.document";
172     TypeDescriptorCfg flexibleTypeDescriptorCfg;
173     flexibleTypeDescriptorCfg.typeId = "com.example.utdtest.document";
174     flexibleTypeDescriptorCfg.belongingToTypes = {"com.example.utdtest2.document"};
175     flexibleTypeDescriptorCfg.filenameExtensions = {".mydocument"};
176     flexibleTypeDescriptorCfg.mimeTypes = {"application/my-document"};
177     flexibleTypeDescriptorCfg.description = "My document.";
178     flexibleTypeDescriptorCfg.referenceURL = "http://www.mycompany.com/my-document.html";
179     flexibleTypeDescriptorCfg.iconFile = "resources/my-document.png";
180     flexibleTypeDescriptorCfg.ownerBundle = "com.example.utdtest";
181     flexibleTypeDescriptorCfg.installerBundles = {"com.example.utdtest"};
182     FlexibleType flexibleType;
183     bool ret = flexibleType.ParseFlexibleUtd(typeId, flexibleTypeDescriptorCfg);
184     EXPECT_FALSE(ret);
185     LOG_INFO(UDMF_TEST, "ParseFlexibleUtd002 end.");
186 }
187 
188 /**
189 * @tc.name: ParseFlexibleUtd003
190 * @tc.desc: Abnormal testcase of ParseFlexibleUtd, because flexibleUtdDecode contains "="
191 * @tc.type: FUNC
192 */
193 HWTEST_F(FlexibleTypeTest, ParseFlexibleUtd003, TestSize.Level1)
194 {
195     LOG_INFO(UDMF_TEST, "ParseFlexibleUtd003 begin.");
196     const std::string typeId = "flex.z=";
197     TypeDescriptorCfg flexibleTypeDescriptorCfg;
198     flexibleTypeDescriptorCfg.typeId = "com.example.utdtest.document";
199     flexibleTypeDescriptorCfg.belongingToTypes = {"com.example.utdtest2.document"};
200     flexibleTypeDescriptorCfg.filenameExtensions = {".mydocument"};
201     flexibleTypeDescriptorCfg.mimeTypes = {"application/my-document"};
202     flexibleTypeDescriptorCfg.description = "My document.";
203     flexibleTypeDescriptorCfg.referenceURL = "http://www.mycompany.com/my-document.html";
204     flexibleTypeDescriptorCfg.iconFile = "resources/my-document.png";
205     flexibleTypeDescriptorCfg.ownerBundle = "com.example.utdtest";
206     flexibleTypeDescriptorCfg.installerBundles = {"com.example.utdtest"};
207     FlexibleType flexibleType;
208     bool ret = flexibleType.ParseFlexibleUtd(typeId, flexibleTypeDescriptorCfg);
209     EXPECT_FALSE(ret);
210     LOG_INFO(UDMF_TEST, "ParseFlexibleUtd003 end.");
211 }
212 } // OHOS::Test