• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "unifiedKeyTest"
16 
17 #include <unistd.h>
18 #include <gtest/gtest.h>
19 #include <string>
20 #include "unified_key.h"
21 
22 using namespace testing::ext;
23 using namespace OHOS::UDMF;
24 using namespace OHOS;
25 namespace OHOS::Test {
26 using namespace std;
27 
28 class UnifiedKeyTest : public testing::Test {
29 public:
30     static void SetUpTestCase();
31     static void TearDownTestCase();
32     void SetUp() override;
33     void TearDown() override;
34 };
35 
SetUpTestCase()36 void UnifiedKeyTest::SetUpTestCase()
37 {
38 }
39 
TearDownTestCase()40 void UnifiedKeyTest::TearDownTestCase()
41 {
42 }
43 
SetUp()44 void UnifiedKeyTest::SetUp()
45 {
46 }
47 
TearDown()48 void UnifiedKeyTest::TearDown()
49 {
50 }
51 
52 /**
53 * @tc.name: UnifiedKey001
54 * @tc.desc: Normal testcase of UnifiedKey
55 * @tc.type: FUNC
56 */
57 HWTEST_F(UnifiedKeyTest, UnifiedKey001, TestSize.Level1)
58 {
59     std::string key = "general.text";
60     UnifiedKey unifiedKey(key);
61     EXPECT_FALSE(unifiedKey.key.empty());
62 }
63 
64 /**
65 * @tc.name: UnifiedKey002
66 * @tc.desc: Normal testcase of UnifiedKey
67 * @tc.type: FUNC
68 */
69 HWTEST_F(UnifiedKeyTest, UnifiedKey002, TestSize.Level1)
70 {
71     std::string intention = "general.text";
72     std::string bundle = "com.ohos.test";
73     std::string groupId = "test";
74 
75     UnifiedKey unifiedKey(intention, bundle, groupId);
76     EXPECT_FALSE(unifiedKey.intention.empty());
77     EXPECT_FALSE(unifiedKey.bundleName.empty());
78     EXPECT_FALSE(unifiedKey.groupId.empty());
79 }
80 
81 /**
82 * @tc.name: GetUnifiedKey001
83 * @tc.desc: Normal testcase of GetUnifiedKey, this->key is empty
84 * @tc.type: FUNC
85 */
86 HWTEST_F(UnifiedKeyTest, GetUnifiedKey001, TestSize.Level1)
87 {
88     std::string intention = "general.text";
89     std::string bundle = "com.ohos.test";
90     std::string groupId = "test";
91 
92     UnifiedKey unifiedKey(intention, bundle, groupId);
93     EXPECT_NO_FATAL_FAILURE(unifiedKey.GetUnifiedKey());
94 }
95 
96 /**
97 * @tc.name: GetUnifiedKey002
98 * @tc.desc: Normal testcase of GetUnifiedKey
99 * @tc.type: FUNC
100 */
101 HWTEST_F(UnifiedKeyTest, GetUnifiedKey002, TestSize.Level1)
102 {
103     std::string key = "general.text";
104     UnifiedKey unifiedKey(key);
105     std::string ret = unifiedKey.GetUnifiedKey();
106     EXPECT_EQ(ret, key);
107 }
108 
109 /**
110 * @tc.name: GetUnifiedKey003
111 * @tc.desc: Abnormal testcase of GetUnifiedKey, this->intention is empty
112 * @tc.type: FUNC
113 */
114 HWTEST_F(UnifiedKeyTest, GetUnifiedKey003, TestSize.Level1)
115 {
116     std::string intention;
117     std::string bundle = "com.ohos.test";
118     std::string groupId = "group1";
119 
120     UnifiedKey unifiedKey(intention, bundle, groupId);
121     std::string ret = unifiedKey.GetUnifiedKey();
122     EXPECT_EQ(ret, "");
123 }
124 
125 /**
126 * @tc.name: GetUnifiedKey004
127 * @tc.desc: Abnormal testcase of GetUnifiedKey, this->groupId is empty
128 * @tc.type: FUNC
129 */
130 HWTEST_F(UnifiedKeyTest, GetUnifiedKey004, TestSize.Level1)
131 {
132     std::string intention = "intention1";
133     std::string bundle;
134     std::string groupId;
135 
136     UnifiedKey unifiedKey(intention, bundle, groupId);
137     std::string ret = unifiedKey.GetUnifiedKey();
138     EXPECT_EQ(ret, "");
139 }
140 
141 /**
142 * @tc.name: GetUnifiedKey005
143 * @tc.desc: Abnormal testcase of GetUnifiedKey, this->groupId and this->intention is empty
144 * @tc.type: FUNC
145 */
146 HWTEST_F(UnifiedKeyTest, GetUnifiedKey005, TestSize.Level1)
147 {
148     std::string intention;
149     std::string bundle;
150     std::string groupId;
151 
152     UnifiedKey unifiedKey(intention, bundle, groupId);
153     std::string ret = unifiedKey.GetUnifiedKey();
154     EXPECT_EQ(ret, "");
155 }
156 
157 /**
158 * @tc.name: GetKeyCommonPrefix001
159 * @tc.desc: Abnormal testcase of GetKeyCommonPrefix, size of this->key is 33
160 * @tc.type: FUNC
161 */
162 HWTEST_F(UnifiedKeyTest, GetKeyCommonPrefix001, TestSize.Level1)
163 {
164     std::string key('A', 33);
165     UnifiedKey unifiedKey(key);
166 
167     std::string ret = unifiedKey.GetKeyCommonPrefix();
168     EXPECT_FALSE(ret.empty());
169 }
170 
171 /**
172 * @tc.name: GetKeyCommonPrefix002
173 * @tc.desc: Abnormal testcase of GetKeyCommonPrefix, size of this->key is 31, this->intention is empty
174 * @tc.type: FUNC
175 */
176 HWTEST_F(UnifiedKeyTest, GetKeyCommonPrefix002, TestSize.Level1)
177 {
178     std::string intention;
179     std::string bundle = "com.ohos.test";
180     std::string groupId('A', 31);
181     UnifiedKey unifiedKey(intention, bundle, groupId);
182 
183     std::string ret = unifiedKey.GetKeyCommonPrefix();
184     EXPECT_EQ(ret, "");
185 }
186 
187 /**
188 * @tc.name: GetKeyCommonPrefix003
189 * @tc.desc: Abnormal testcase of GetKeyCommonPrefix, this->groupId size < INDEX_LEN
190 * @tc.type: FUNC
191 */
192 HWTEST_F(UnifiedKeyTest, GetKeyCommonPrefix003, TestSize.Level1)
193 {
194     std::string intention = "general.text";
195     std::string bundle = "com.ohos.test";
196     std::string groupId = "test";
197 
198     UnifiedKey unifiedKey(intention, bundle, groupId);
199     std::string ret = unifiedKey.GetKeyCommonPrefix();
200     EXPECT_EQ(ret, "");
201 }
202 
203 /**
204 * @tc.name: GetKeyCommonPrefix004
205 * @tc.desc: Abnormal testcase of GetKeyCommonPrefix, this->groupId size < INDEX_LEN
206 * @tc.type: FUNC
207 */
208 HWTEST_F(UnifiedKeyTest, GetKeyCommonPrefix004, TestSize.Level1)
209 {
210     std::string intention = "general.text";
211     std::string bundle = "com.ohos.test";
212     std::string groupId('A', 33);
213 
214     UnifiedKey unifiedKey(intention, bundle, groupId);
215     std::string ret = unifiedKey.GetKeyCommonPrefix();
216     EXPECT_FALSE(ret.empty());
217 }
218 
219 /**
220 * @tc.name: IsValid001
221 * @tc.desc: Normal testcase of IsValid, key is invalid
222 * @tc.type: FUNC
223 */
224 HWTEST_F(UnifiedKeyTest, IsValid001, TestSize.Level1)
225 {
226     std::string intention = "general.text";
227     std::string bundle = "com.ohos.test";
228     std::string groupId('A', 33);
229 
230     UnifiedKey unifiedKey(intention, bundle, groupId);
231     bool ret = unifiedKey.IsValid();
232     EXPECT_FALSE(ret);
233 }
234 
235 /**
236 * @tc.name: IsValid002
237 * @tc.desc: Abnormal testcase of IsValid, key is invalid
238 * @tc.type: FUNC
239 */
240 HWTEST_F(UnifiedKeyTest, IsValid002, TestSize.Level1)
241 {
242     std::string key('A', 33);
243     UnifiedKey unifiedKey(key);
244 
245     bool ret = unifiedKey.IsValid();
246     EXPECT_FALSE(ret);
247 }
248 
249 /**
250 * @tc.name: IsValid003
251 * @tc.desc: Abnormal testcase of IsValid, key is invalid
252 * @tc.type: FUNC
253 */
254 HWTEST_F(UnifiedKeyTest, IsValid003, TestSize.Level1)
255 {
256     std::string key = "udmf://";
257     UnifiedKey unifiedKey(key);
258 
259     bool ret = unifiedKey.IsValid();
260     EXPECT_FALSE(ret);
261 }
262 
263 /**
264 * @tc.name: PreliminaryWork002
265 * @tc.desc: Normal testcase of PreliminaryWork
266 * @tc.type: FUNC
267 */
268 HWTEST_F(UnifiedKeyTest, PreliminaryWork002, TestSize.Level1)
269 {
270     std::string key = "udmf://";
271     UnifiedKey unifiedKey(key);
272 
273     EXPECT_NO_FATAL_FAILURE(unifiedKey.PreliminaryWork());
274 }
275 
276 /**
277 * @tc.name: CheckCharacter001
278 * @tc.desc: Abnormal testcase of CheckCharacter, data is invalid
279 * @tc.type: FUNC
280 */
281 HWTEST_F(UnifiedKeyTest, CheckCharacter001, TestSize.Level1)
282 {
283     std::string key = "udmf://";
284     UnifiedKey unifiedKey(key);
285     std::string_view data;
286     std::bitset<MAX_BIT_SIZE> rule;
287 
288     EXPECT_FALSE(unifiedKey.CheckCharacter(data, rule));
289 }
290 
291 /**
292 * @tc.name: CheckCharacter002
293 * @tc.desc: Abnormal testcase of CheckCharacter, data is invalid
294 * @tc.type: FUNC
295 */
296 HWTEST_F(UnifiedKeyTest, CheckCharacter002, TestSize.Level1)
297 {
298     std::string key = "udmf://";
299     UnifiedKey unifiedKey(key);
300     std::string_view data = "data";
301     std::bitset<MAX_BIT_SIZE> rule;
302 
303     EXPECT_FALSE(unifiedKey.CheckCharacter(data, rule));
304 }
305 
306 /**
307 * @tc.name: ExtractAndValidateSegment001
308 * @tc.desc: Abnormal testcase of ExtractAndValidateSegment, data is invalid
309 * @tc.type: FUNC
310 */
311 HWTEST_F(UnifiedKeyTest, ExtractAndValidateSegment001, TestSize.Level1)
312 {
313     std::string key = "udmf://";
314     UnifiedKey unifiedKey(key);
315     std::string_view data = "data";
316     std::string field;
317     std::bitset<MAX_BIT_SIZE> rule;
318     std::string name;
319 
320     EXPECT_FALSE(unifiedKey.ExtractAndValidateSegment(data, field, rule, name));
321 }
322 
323 /**
324 * @tc.name: ExtractAndValidateSegment002
325 * @tc.desc: Abnormal testcase of ExtractAndValidateSegment, data is invalid
326 * @tc.type: FUNC
327 */
328 HWTEST_F(UnifiedKeyTest, ExtractAndValidateSegment002, TestSize.Level1)
329 {
330     std::string key = "udmf://";
331     UnifiedKey unifiedKey(key);
332     std::string_view data = "udmf://";
333     std::string field = "udmf://";
334     std::bitset<MAX_BIT_SIZE> rule;
335     std::string name;
336 
337     EXPECT_FALSE(unifiedKey.ExtractAndValidateSegment(data, field, rule, name));
338 }
339 } // OHOS::Test