• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 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 "cmsis_os.h"
17 #include <ohos_errno.h>
18 #include "hctest.h"
19 #include "samgr_lite.h"
20 
21 #define SERVICE_NAME "S_iunknown"
22 #define FEATURE_NAME "F_iunknown"
23 
24 typedef struct DemoApi {
25     INHERIT_IUNKNOWN;
26 } DemoApi;
27 
28 typedef struct DemoFeature {
29     INHERIT_FEATURE;
30     INHERIT_IUNKNOWNENTRY(DemoApi);
31     Identity identity;
32 } DemoFeature;
33 
GetName(Service * service)34 static const char *GetName(Service *service)
35 {
36     (void)service;
37     return SERVICE_NAME;
38 }
39 
40 typedef struct ExampleService {
41     INHERIT_SERVICE;
42     Identity identity;
43 } ExampleService;
44 
Initialize(Service * service,Identity identity)45 static BOOL Initialize(Service *service, Identity identity)
46 {
47     ExampleService *example = (ExampleService *)service;
48     example->identity = identity;
49     return TRUE;
50 }
51 
MessageHandle(Service * service,Request * msg)52 static BOOL MessageHandle(Service *service, Request *msg)
53 {
54     (void)service;
55     (void)msg;
56     return FALSE;
57 }
58 
GetTaskConfig(Service * service)59 static TaskConfig GetTaskConfig(Service *service)
60 {
61     (void)service;
62     TaskConfig config = {
63         LEVEL_HIGH,
64         PRI_ABOVE_NORMAL,
65         0x800,
66         20,
67         SHARED_TASK
68         };
69     return config;
70 }
71 
72 static ExampleService g_service = {
73     .GetName = GetName,
74     .Initialize = Initialize,
75     .MessageHandle = MessageHandle,
76     .GetTaskConfig = GetTaskConfig,
77     .identity = {-1, -1, NULL},
78 };
79 
FEATURE_GetName(Feature * feature)80 static const char *FEATURE_GetName(Feature *feature)
81 {
82     (void)feature;
83     return FEATURE_NAME;
84 }
FEATURE_OnInitialize(Feature * feature,Service * parent,Identity identity)85 static void FEATURE_OnInitialize(Feature *feature, Service *parent, Identity identity)
86 {
87     (void)parent;
88     DemoFeature *demoFeature = (DemoFeature *)feature;
89     demoFeature->identity = identity;
90 }
FEATURE_OnStop(Feature * feature,Identity identity)91 static void FEATURE_OnStop(Feature *feature, Identity identity)
92 {
93     (void)feature;
94     (void)identity;
95 }
96 
FEATURE_OnMessage(Feature * feature,Request * request)97 static BOOL FEATURE_OnMessage(Feature *feature, Request *request)
98 {
99     (void)feature;
100     (void)request;
101     return TRUE;
102 }
103 
104 static DemoFeature g_feature = {
105     .GetName = FEATURE_GetName,
106     .OnInitialize = FEATURE_OnInitialize,
107     .OnStop = FEATURE_OnStop,
108     .OnMessage = FEATURE_OnMessage,
109     .ver = DEFAULT_VERSION,
110     .ref = 1,
111     .iUnknown = {
112         DEFAULT_IUNKNOWN_IMPL,
113     },
114     .identity = {-1, -1, NULL},
115 };
116 
117 static DemoApi g_api = {
118     .AddRef = IUNKNOWN_AddRef,
119     .QueryInterface = IUNKNOWN_QueryInterface,
120     .Release = IUNKNOWN_Release,
121 };
122 
123 LITE_TEST_SUIT(test, samgr, IUnknownTestSuite);
124 
IUnknownTestSuiteSetUp(void)125 static BOOL IUnknownTestSuiteSetUp(void)
126 {
127     BOOL result1 = SAMGR_GetInstance()->RegisterService((Service *)&g_service);
128     BOOL result2 = SAMGR_GetInstance()->RegisterFeature(SERVICE_NAME, (Feature *)&g_feature);
129     if (result1 == TRUE && result2 == TRUE) {
130         return TRUE;
131     } else {
132         printf("[hctest]E failed to register service or feature \n");
133         return FALSE;
134     }
135 }
136 
IUnknownTestSuiteTearDown(void)137 static BOOL IUnknownTestSuiteTearDown(void)
138 {
139     SAMGR_GetInstance()->UnregisterFeature(SERVICE_NAME, FEATURE_NAME);
140     SAMGR_GetInstance()->UnregisterService(SERVICE_NAME);
141     return TRUE;
142 }
143 
144 /**
145  * @tc.number    : DMSLite_SAMGR_GetIUnknown_0010
146  * @tc.name      : Use this macro GET_IUNKNOWN user can obtain the IUnknown interface from the subclass object
147  * @tc.desc      : [C- SOFTWARE -0200]
148  */
149 LITE_TEST_CASE(IUnknownTestSuite, testGetIUnknown0010, Function | MediumTest | Level2)
150 {
151     IUnknown *iUnknown = GET_IUNKNOWN(g_feature);
152     TEST_ASSERT_EQUAL_INT(TRUE, iUnknown != NULL);
153 }
154 
155 /**
156  * @tc.number    : DMSLite_SAMGR_GetObject_0010
157  * @tc.name      : Use this macro GET_OBJECT user can obtain an outside object
158  * @tc.desc      : [C- SOFTWARE -0200]
159  */
160 LITE_TEST_CASE(IUnknownTestSuite, testGetObject0010, Function | MediumTest | Level2)
161 {
162     IUnknown *iUnknown = GET_IUNKNOWN(g_feature);
163     IUnknownEntry *entry = GET_OBJECT(iUnknown, IUnknownEntry, iUnknown);
164     TEST_ASSERT_EQUAL_INT(DEFAULT_VERSION, entry->ver);
165     TEST_ASSERT_EQUAL_INT(1, entry->ref);
166     int resultAdd = iUnknown->AddRef(iUnknown);
167     int resultRelease = iUnknown->Release(iUnknown);
168     TEST_ASSERT_EQUAL_INT(1, resultAdd - resultRelease);
169 }
170 
171 /**
172  * @tc.number    : DMSLite_SAMGR_QueryInterface_0010
173  * @tc.name      : Use this api QueryInterface user can convert the object to the required subclass type
174  * @tc.desc      : [C- SOFTWARE -0200]
175  */
176 LITE_TEST_CASE(IUnknownTestSuite, testQueryInterface0010, Function | MediumTest | Level2)
177 {
178     SAMGR_GetInstance()->RegisterFeatureApi(SERVICE_NAME, FEATURE_NAME, GET_IUNKNOWN(g_feature));
179 
180     IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(SERVICE_NAME, FEATURE_NAME);
181     DemoApi *demoApi = NULL;
182     int resultQuery = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&demoApi);
183     TEST_ASSERT_EQUAL_INT(EC_SUCCESS, resultQuery);
184     TEST_ASSERT_EQUAL_INT(TRUE, demoApi != NULL);
185     int resultRelease = iUnknown->Release(iUnknown);
186     TEST_ASSERT_EQUAL_INT(1, resultRelease);
187     SAMGR_GetInstance()->UnregisterFeatureApi(SERVICE_NAME, FEATURE_NAME);
188 }
189 
190 /**
191  * @tc.number    : DMSLite_SAMGR_QueryInterface_0020
192  * @tc.name      : User can not get the required subclass type if the version does not have permission
193  * @tc.desc      : [C- SOFTWARE -0200]
194  */
195 LITE_TEST_CASE(IUnknownTestSuite, testQueryInterface0020, Function | MediumTest | Level2)
196 {
197     SAMGR_GetInstance()->RegisterFeatureApi(SERVICE_NAME, FEATURE_NAME, GET_IUNKNOWN(g_feature));
198 
199     IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(SERVICE_NAME, FEATURE_NAME);
200     DemoApi *demoApi = NULL;
201     int resultQuery = iUnknown->QueryInterface(iUnknown, 0x30, (void **)&demoApi);
202     TEST_ASSERT_EQUAL_INT(EC_INVALID, resultQuery);
203 
204     SAMGR_GetInstance()->UnregisterFeatureApi(SERVICE_NAME, FEATURE_NAME);
205 }
206 
207 /**
208  * @tc.number    : DMSLite_SAMGR_QueryInterface_0030
209  * @tc.name      : User can rewrite this api QueryInterface
210  * @tc.desc      : [C- SOFTWARE -0200]
211  */
212 LITE_TEST_CASE(IUnknownTestSuite, testQueryInterface0030, Function | MediumTest | Level2)
213 {
214     SAMGR_GetInstance()->RegisterFeatureApi(SERVICE_NAME, FEATURE_NAME, (IUnknown *)&g_api);
215     IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(SERVICE_NAME, FEATURE_NAME);
216     DemoApi *demoApi = NULL;
217     int resultQuery = iUnknown->QueryInterface(iUnknown, 0, (void **)&demoApi);
218     TEST_ASSERT_EQUAL_INT(EC_SUCCESS, resultQuery);
219     TEST_ASSERT_EQUAL_INT(TRUE, demoApi != NULL);
220 
221     SAMGR_GetInstance()->UnregisterFeatureApi(SERVICE_NAME, FEATURE_NAME);
222 }
223 
224 RUN_TEST_SUITE(IUnknownTestSuite);