• 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 <gtest/gtest.h>
17 
18 #include "securec.h"
19 #include "want.h"
20 #include "want/include/want.h"
21 
22 using namespace testing::ext;
23 
24 
25 class CWantTest : public testing::Test {
26 public:
SetUpTestCase()27     static void SetUpTestCase(){};
28 
TearDownTestCase()29     static void TearDownTestCase(){};
30 
SetUp()31     void SetUp(){};
32 
TearDown()33     void TearDown(){};
34 };
35 
initElementWithDynamicMemory(AbilityBase_Element & element)36 void initElementWithDynamicMemory(AbilityBase_Element& element)
37 {
38     const char bundle[] = "testBundleName";
39     const char module[] = "testModuleName";
40     const char ability[] = "testAbilityName";
41 
42     element.bundleName = new char[strlen(bundle) + 1];
43     element.moduleName = new char[strlen(module) + 1];
44     element.abilityName = new char[strlen(ability) + 1];
45 
46     strcpy_s(element.bundleName, strlen(bundle) + 1, bundle);
47     strcpy_s(element.moduleName, strlen(module) + 1, module);
48     strcpy_s(element.abilityName, strlen(ability) + 1, ability);
49 }
50 
ReleaseElementMemory(AbilityBase_Element & element)51 void ReleaseElementMemory(AbilityBase_Element& element)
52 {
53     delete[] element.bundleName;
54     delete[] element.moduleName;
55     delete[] element.abilityName;
56 }
57 
58 /**
59  * @tc.number:OH_AbilityBase_SetWantElement_001
60  * @tc.desc: Function test OH_AbilityBase_SetWantElement_001
61  * @tc.type: FUNC
62  */
63 HWTEST_F(CWantTest, OH_AbilityBase_SetWantElement_001, TestSize.Level0)
64 {
65     AbilityBase_Element element;
66     initElementWithDynamicMemory(element);
67 
68     AbilityBase_Want* want = OH_AbilityBase_CreateWant(element);
69     AbilityBase_ErrorCode errCode = OH_AbilityBase_SetWantElement(want, element);
70     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_NO_ERROR);
71     AbilityBase_Element outElement;
72     errCode = OH_AbilityBase_GetWantElement(want, &outElement);
73     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_NO_ERROR);
74     ASSERT_STREQ(outElement.bundleName, "testBundleName");
75     ASSERT_STREQ(outElement.moduleName, "testModuleName");
76     ASSERT_STREQ(outElement.abilityName, "testAbilityName");
77     ReleaseElementMemory(element);
78 }
79 
80 /**
81  * @tc.number:OH_AbilityBase_SetWantElement_002
82  * @tc.desc: Function test OH_AbilityBase_SetWantElement_002
83  * @tc.type: FUNC
84  */
85 HWTEST_F(CWantTest, OH_AbilityBase_SetWantElement_002, TestSize.Level0)
86 {
87     AbilityBase_Element element;
88     initElementWithDynamicMemory(element);
89 
90     AbilityBase_Want* want = nullptr;
91     AbilityBase_ErrorCode errCode = OH_AbilityBase_SetWantElement(want, element);
92     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_PARAM_INVALID);
93 }
94 
95 /**
96  * @tc.number:OH_AbilityBase_SetWantUri_001
97  * @tc.desc: Function test OH_AbilityBase_SetWantUri_001
98  * @tc.type: FUNC
99  */
100 HWTEST_F(CWantTest, OH_AbilityBase_SetWantUri_001, TestSize.Level0)
101 {
102     AbilityBase_Element element;
103     initElementWithDynamicMemory(element);
104 
105     AbilityBase_Want* want = OH_AbilityBase_CreateWant(element);
106     AbilityBase_ErrorCode errCode = OH_AbilityBase_SetWantElement(want, element);
107     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_NO_ERROR);
108 
109     const char * uri = "testUri";
110     errCode = OH_AbilityBase_SetWantUri(want, uri);
111     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_NO_ERROR);
112     errCode = OH_AbilityBase_SetWantUri(want, nullptr);
113     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_PARAM_INVALID);
114     errCode = OH_AbilityBase_SetWantUri(nullptr, nullptr);
115     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_PARAM_INVALID);
116     char outUri[50];
117     errCode = OH_AbilityBase_GetWantUri(nullptr, outUri, sizeof(outUri));
118     errCode = OH_AbilityBase_GetWantUri(want, nullptr, sizeof(outUri));
119     errCode = OH_AbilityBase_GetWantUri(want, outUri, sizeof(outUri));
120     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_NO_ERROR);
121     ASSERT_STREQ(uri, outUri);
122     errCode = OH_AbilityBase_GetWantUri(want, outUri, 1);
123     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_PARAM_INVALID);
124 }
125 
126 /**
127  * @tc.number:OH_AbilityBase_SetWantInt32Param_001
128  * @tc.desc: Function test OH_AbilityBase_SetWantInt32Param_001
129  * @tc.type: FUNC
130  */
131 HWTEST_F(CWantTest, OH_AbilityBase_SetWantInt32Param_001, TestSize.Level0)
132 {
133     AbilityBase_Element element;
134     initElementWithDynamicMemory(element);
135 
136     AbilityBase_Want* want = OH_AbilityBase_CreateWant(element);
137     AbilityBase_ErrorCode errCode = OH_AbilityBase_SetWantElement(want, element);
138     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_NO_ERROR);
139 
140     const char * key = "intKey";
141     int32_t value = 10;
142     errCode = OH_AbilityBase_SetWantInt32Param(want, nullptr, value);
143     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_PARAM_INVALID);
144     errCode = OH_AbilityBase_SetWantInt32Param(want, key, value);
145     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_NO_ERROR);
146     int32_t outValue = 0;
147     errCode = OH_AbilityBase_GetWantInt32Param(nullptr, key, &outValue);
148     errCode = OH_AbilityBase_GetWantInt32Param(want, nullptr, &outValue);
149     errCode = OH_AbilityBase_GetWantInt32Param(want, key, &outValue);
150     ASSERT_EQ(outValue, value);
151     errCode = OH_AbilityBase_GetWantInt32Param(want, "notFoundKey", &outValue);
152     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_PARAM_INVALID);
153 }
154 
155 /**
156  * @tc.number:OH_AbilityBase_SetWantBoolParam_001
157  * @tc.desc: Function test OH_AbilityBase_SetWantBoolParam_001
158  * @tc.type: FUNC
159  */
160 HWTEST_F(CWantTest, OH_AbilityBase_SetWantBoolParam_001, TestSize.Level0)
161 {
162     AbilityBase_Element element;
163     initElementWithDynamicMemory(element);
164 
165     AbilityBase_Want* want = OH_AbilityBase_CreateWant(element);
166     AbilityBase_ErrorCode errCode = OH_AbilityBase_SetWantElement(want, element);
167     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_NO_ERROR);
168 
169     const char * key = "boolKey";
170     bool value = true;
171     errCode = OH_AbilityBase_SetWantBoolParam(want, key, value);
172     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_NO_ERROR);
173 
174     errCode = OH_AbilityBase_SetWantBoolParam(nullptr, key, value);
175     errCode = OH_AbilityBase_SetWantBoolParam(want, nullptr, value);
176     errCode = OH_AbilityBase_SetWantBoolParam(want, key, value);
177     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_NO_ERROR);
178     bool outValue = false;
179     errCode = OH_AbilityBase_GetWantBoolParam(nullptr, key, &outValue);
180     errCode = OH_AbilityBase_GetWantBoolParam(want, nullptr, &outValue);
181     errCode = OH_AbilityBase_GetWantBoolParam(want, key, &outValue);
182     ASSERT_EQ(outValue, value);
183     errCode = OH_AbilityBase_GetWantBoolParam(want, "notFoundKey", &outValue);
184     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_PARAM_INVALID);
185 }
186 
187 /**
188  * @tc.number:OH_AbilityBase_SetWantDoubleParam_001
189  * @tc.desc: Function test OH_AbilityBase_SetWantDoubleParam_001
190  * @tc.type: FUNC
191  */
192 HWTEST_F(CWantTest, OH_AbilityBase_SetWantDoubleParam_001, TestSize.Level0)
193 {
194     AbilityBase_Element element;
195     initElementWithDynamicMemory(element);
196 
197     AbilityBase_Want* want = OH_AbilityBase_CreateWant(element);
198     AbilityBase_ErrorCode errCode = OH_AbilityBase_SetWantElement(want, element);
199     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_NO_ERROR);
200 
201     const char * key = "boolKey";
202     double value = 3.14f;
203     errCode = OH_AbilityBase_SetWantDoubleParam(nullptr, key, value);
204     errCode = OH_AbilityBase_SetWantDoubleParam(want, nullptr, value);
205     errCode = OH_AbilityBase_SetWantDoubleParam(want, key, value);
206     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_NO_ERROR);
207 
208     double outValue = 0.00;
209     errCode = OH_AbilityBase_GetWantDoubleParam(nullptr, key, &outValue);
210     errCode = OH_AbilityBase_GetWantDoubleParam(want, nullptr, &outValue);
211     errCode = OH_AbilityBase_GetWantDoubleParam(want, key, &outValue);
212     ASSERT_EQ(outValue, value);
213     errCode = OH_AbilityBase_GetWantDoubleParam(want, "notFoundKey", &outValue);
214     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_PARAM_INVALID);
215 }
216 
217 /**
218  * @tc.number:OH_AbilityBase_SetWantStrParam_001
219  * @tc.desc: Function test OH_AbilityBase_SetWantStrParam_001
220  * @tc.type: FUNC
221  */
222 HWTEST_F(CWantTest, OH_AbilityBase_SetWantStrParam_001, TestSize.Level0)
223 {
224     AbilityBase_Element element;
225     initElementWithDynamicMemory(element);
226 
227     AbilityBase_Want* want = OH_AbilityBase_CreateWant(element);
228     AbilityBase_ErrorCode errCode = OH_AbilityBase_SetWantElement(want, element);
229     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_NO_ERROR);
230 
231     const char * strKey = "strKey";
232     const char * strValue = "strValue";
233     errCode = OH_AbilityBase_SetWantCharParam(nullptr, strKey, strValue);
234     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_PARAM_INVALID);
235 
236     errCode = OH_AbilityBase_SetWantCharParam(want, nullptr, strValue);
237     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_PARAM_INVALID);
238 
239     errCode = OH_AbilityBase_SetWantCharParam(want, strKey, strValue);
240     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_NO_ERROR);
241 
242     char outStr[50];
243     char outStr1[1];
244     errCode = OH_AbilityBase_GetWantCharParam(nullptr, strKey, outStr, sizeof(outStr));
245     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_PARAM_INVALID);
246 
247     errCode = OH_AbilityBase_GetWantCharParam(want, nullptr, outStr, sizeof(outStr));
248     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_PARAM_INVALID);
249 
250     errCode = OH_AbilityBase_GetWantCharParam(want, strKey, outStr, sizeof(outStr1));
251     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_PARAM_INVALID);
252 
253     errCode = OH_AbilityBase_GetWantCharParam(want, strKey, outStr, sizeof(outStr));
254     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_NO_ERROR);
255 }
256 
257 
258 /**
259  * @tc.number:OH_AbilityBase_SetWantFdParam_001
260  * @tc.desc: Function test OH_AbilityBase_SetWantFdParam_001
261  * @tc.type: FUNC
262  */
263 HWTEST_F(CWantTest, OH_AbilityBase_SetWantFdParam_001, TestSize.Level0)
264 {
265     AbilityBase_Element element;
266     initElementWithDynamicMemory(element);
267 
268     AbilityBase_Want* want = OH_AbilityBase_CreateWant(element);
269     AbilityBase_ErrorCode errCode = OH_AbilityBase_SetWantElement(want, element);
270     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_NO_ERROR);
271 
272     const char * fdKey = "fdKey";
273     int32_t fdvalue = 1;
274     errCode = OH_AbilityBase_AddWantFd(nullptr, fdKey, fdvalue);
275     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_PARAM_INVALID);
276 
277     errCode = OH_AbilityBase_AddWantFd(want, nullptr, fdvalue);
278     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_PARAM_INVALID);
279 
280     errCode = OH_AbilityBase_AddWantFd(want, fdKey, fdvalue);
281     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_NO_ERROR);
282 
283     int32_t fdvalue1;
284     errCode = OH_AbilityBase_GetWantFd(nullptr, fdKey, &fdvalue1);
285     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_PARAM_INVALID);
286 
287     errCode = OH_AbilityBase_GetWantFd(want, nullptr, &fdvalue1);
288     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_PARAM_INVALID);
289 
290     errCode = OH_AbilityBase_GetWantFd(want, fdKey, &fdvalue1);
291     ASSERT_EQ(errCode, ABILITY_BASE_ERROR_CODE_NO_ERROR);
292 }
293 
294