• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "platform_manager_test.h"
10 #include "osal_mem.h"
11 #include "platform_assert.h"
12 #include "platform_manager.h"
13 
14 #define HDF_LOG_TAG platform_manager_test
15 
16 #define PLAT_MGR_TEST_DEV_NUM           3
17 #define PLAT_MGR_TEST_DEV_NUM_START     0x5A
18 #define PLAT_DEV_NUMBER_0               0
19 #define PLAT_DEV_NUMBER_1               1
20 #define PLAT_DEV_NUMBER_2               2
21 
22 static struct PlatformDevice *g_platDevices[PLAT_MGR_TEST_DEV_NUM];
23 
PlatformManagerTestCreateDevices(void)24 static int32_t PlatformManagerTestCreateDevices(void)
25 {
26     int i;
27     int j;
28     int32_t ret;
29 
30     for (i = 0; i < PLAT_MGR_TEST_DEV_NUM; i++) {
31         g_platDevices[i] = (struct PlatformDevice *)OsalMemCalloc(sizeof(struct PlatformDevice));
32         if (g_platDevices[i] == NULL) {
33             ret = HDF_ERR_MALLOC_FAIL;
34             PLAT_LOGE("PlatformManagerTestCreateDevices: memcalloc platform device fail, ret: %d!", ret);
35             goto CREATE_ERR__;
36         }
37         g_platDevices[i]->number = i + PLAT_MGR_TEST_DEV_NUM_START;
38         ret = PlatformDeviceInit(g_platDevices[i]);
39         if (ret != HDF_SUCCESS) {
40             OsalMemFree(g_platDevices[i]);
41             g_platDevices[i] = NULL;
42             PLAT_LOGE("PlatformManagerTestCreateDevices: platform device init fail, ret: %d!", ret);
43             goto CREATE_ERR__;
44         }
45     }
46 
47     return HDF_SUCCESS;
48 
49 CREATE_ERR__:
50     for (j = 0; j < i; j++) {
51         PlatformDeviceUninit(g_platDevices[j]);
52         OsalMemFree(g_platDevices[j]);
53         g_platDevices[j] = 0;
54     }
55 
56     return ret;
57 }
58 
PlatformManagerTestDestroyDevices(void)59 static void PlatformManagerTestDestroyDevices(void)
60 {
61     int i;
62 
63     for (i = 0; i < PLAT_MGR_TEST_DEV_NUM; i++) {
64         PlatformDeviceUninit(g_platDevices[i]);
65         OsalMemFree(g_platDevices[i]);
66         g_platDevices[i] = NULL;
67     }
68 }
69 
PlatformManagerTestAddAndDel(struct PlatformManager * manager)70 static int32_t PlatformManagerTestAddAndDel(struct PlatformManager *manager)
71 {
72     int32_t ret;
73     struct PlatformDevice *device0 = g_platDevices[PLAT_DEV_NUMBER_0];
74     struct PlatformDevice *device1 = g_platDevices[PLAT_DEV_NUMBER_1];
75     struct PlatformDevice *device2 = g_platDevices[PLAT_DEV_NUMBER_2];
76     struct PlatformDevice *device0Get = NULL;
77     struct PlatformDevice *device1Get = NULL;
78     int32_t refCntBeforeAdd;
79     int32_t refCntAfterAdd;
80     int32_t refCntBeforeDel;
81     int32_t refCntAfterDel;
82     int32_t number;
83 
84     PLAT_LOGD("PlatformManagerTestAddAndDel: enter!");
85     // should add first device success
86     device0->name = "platform_device0";
87     refCntBeforeAdd = PlatformDeviceRefCount(device0);
88     ret = PlatformManagerAddDevice(manager, device0);
89     CHECK_EQ_RETURN(ret, HDF_SUCCESS, ret);
90     refCntAfterAdd = PlatformDeviceRefCount(device0);
91     // ref count should increase by 1 after add
92     CHECK_EQ_RETURN(refCntAfterAdd, refCntBeforeAdd + 1, HDF_FAILURE);
93 
94     // should add another success
95     device1->name = "platform_device1";
96     ret = PlatformManagerAddDevice(manager, device1);
97     CHECK_EQ_RETURN(ret, HDF_SUCCESS, ret);
98 
99     // should find the device added
100     device0Get = PlatformManagerGetDeviceByNumber(manager, device0->number);
101     CHECK_EQ_RETURN(device0Get, device0, HDF_FAILURE);
102     PlatformDevicePut(device0Get);
103 
104     // should find another device added
105     device1Get = PlatformManagerGetDeviceByNumber(manager, device1->number);
106     CHECK_EQ_RETURN(device1Get, device1, HDF_FAILURE);
107     PlatformDevicePut(device1Get);
108 
109     // should add fail on repeated number
110     number = device2->number;
111     device2->number = device0->number;
112     device2->name = "platform_device2";
113     ret = PlatformManagerAddDevice(manager, device2);
114     CHECK_NE_RETURN(ret, HDF_SUCCESS, ret);
115 
116     // should add fail on repeated name
117     device2->number = number; // give it a right number
118     device2->name = device0->name;
119     ret = PlatformManagerAddDevice(manager, device2);
120     CHECK_NE_RETURN(ret, HDF_SUCCESS, ret);
121 
122     refCntBeforeDel = PlatformDeviceRefCount(device0);
123     (void)PlatformManagerDelDevice(manager, device0);
124     refCntAfterDel = PlatformDeviceRefCount(device0);
125     // ref count should decrease by 1 after del
126     CHECK_EQ_RETURN(refCntAfterDel + 1, refCntBeforeDel, HDF_FAILURE);
127 
128     // should not get the device removed
129     device0Get = PlatformManagerGetDeviceByNumber(manager, device0->number);
130     CHECK_EQ_RETURN(device0Get, NULL, HDF_FAILURE);
131 
132     PLAT_LOGD("PlatformManagerTestAddAndDel: exit!");
133     return HDF_SUCCESS;
134 }
135 
PlatformManagerTestGetDevice(struct PlatformManager * manager)136 static int32_t PlatformManagerTestGetDevice(struct PlatformManager *manager)
137 {
138     int32_t ret;
139     struct PlatformDevice *device0 = g_platDevices[PLAT_DEV_NUMBER_0];
140     struct PlatformDevice *device1 = g_platDevices[PLAT_DEV_NUMBER_1];
141     struct PlatformDevice *device2 = g_platDevices[PLAT_DEV_NUMBER_2];
142     struct PlatformDevice *device0Get = NULL;
143     struct PlatformDevice *device1Get = NULL;
144     int32_t refCntBeforeGet;
145     int32_t refCntAfterGet;
146     int32_t refCntBeforePut;
147     int32_t refCntAfterPut;
148 
149     PLAT_LOGD("PlatformManagerTestGetDevice: enter!");
150     // we add some devices first
151     device0->name = "platform_device0";
152     ret = PlatformManagerAddDevice(manager, device0);
153     CHECK_EQ_RETURN(ret, HDF_SUCCESS, ret);
154     device1->name = "platform_device1";
155     ret = PlatformManagerAddDevice(manager, device1);
156     CHECK_EQ_RETURN(ret, HDF_SUCCESS, ret);
157     device2->name = "platform_device2";
158     ret = PlatformManagerAddDevice(manager, device2);
159     CHECK_EQ_RETURN(ret, HDF_SUCCESS, ret);
160 
161     refCntBeforeGet = PlatformDeviceRefCount(device0);
162     // should get the device by number
163     device0Get = PlatformManagerGetDeviceByNumber(manager, device0->number);
164     CHECK_EQ_RETURN(device0Get, device0, HDF_FAILURE);
165 
166     refCntAfterGet = PlatformDeviceRefCount(device0);
167     // should increase refcount after get
168     CHECK_EQ_RETURN(refCntBeforeGet + 1, refCntAfterGet, HDF_FAILURE);
169 
170     refCntBeforePut = PlatformDeviceRefCount(device0);
171     PlatformDevicePut(device0Get);
172     refCntAfterPut = PlatformDeviceRefCount(device0);
173     // should dcrease refcount after put
174     CHECK_EQ_RETURN(refCntBeforePut, refCntAfterPut + 1, HDF_FAILURE);
175 
176     refCntBeforeGet = PlatformDeviceRefCount(device1);
177     // should get the device by name
178     device1Get = PlatformManagerGetDeviceByName(manager, "platform_device1");
179 
180     CHECK_EQ_RETURN(device1Get, device1, HDF_FAILURE);
181     refCntAfterGet = PlatformDeviceRefCount(device1);
182     // should increase refcount after get
183     CHECK_EQ_RETURN(refCntBeforeGet + 1, refCntAfterGet, HDF_FAILURE);
184 
185     refCntBeforePut = PlatformDeviceRefCount(device1);
186     PlatformDevicePut(device1Get);
187     refCntAfterPut = PlatformDeviceRefCount(device1);
188     // should dcrease refcount after put
189     CHECK_EQ_RETURN(refCntBeforePut, refCntAfterPut + 1, HDF_FAILURE);
190 
191     PLAT_LOGD("PlatformManagerTestGetDevice: exit!");
192     return HDF_SUCCESS;
193 }
194 
PlatformManagerTestReliability(struct PlatformManager * manager)195 static int32_t PlatformManagerTestReliability(struct PlatformManager *manager)
196 {
197     int32_t ret;
198     struct PlatformDevice *device = g_platDevices[0];
199     int32_t number = device->number;
200 
201     PLAT_LOGD("PlatformManagerTestReliability: enter!");
202     // should not add success when manager is NULL
203     ret = PlatformManagerAddDevice(NULL, device);
204     CHECK_NE_RETURN(ret, HDF_SUCCESS, HDF_FAILURE);
205 
206     // should not add success when device is NULL
207     ret = PlatformManagerAddDevice(manager, NULL);
208     CHECK_NE_RETURN(ret, HDF_SUCCESS, HDF_FAILURE);
209 
210     // will not crash ...
211     PlatformManagerAddDevice(manager, NULL);
212     PlatformManagerAddDevice(NULL, device);
213 
214     // should not get device by wrong parms
215     device = PlatformManagerGetDeviceByNumber(NULL, number);
216     CHECK_EQ_RETURN(device, NULL, HDF_FAILURE);
217     device = PlatformManagerGetDeviceByName(NULL, "platform_device0");
218     CHECK_EQ_RETURN(device, NULL, HDF_FAILURE);
219 
220     PLAT_LOGD("PlatformManagerTestReliability: exit!");
221     return HDF_SUCCESS;
222 }
223 
224 struct PlatformManagerTestEntry {
225     int cmd;
226     int32_t (*func)(struct PlatformManager *manager);
227     const char *name;
228 };
229 
230 static struct PlatformManagerTestEntry g_entry[] = {
231     { PLAT_MANAGER_TEST_ADD_DEVICE, PlatformManagerTestAddAndDel, "PlatformManagerTestAddAndDel" },
232     { PLAT_MANAGER_TEST_GET_DEVICE, PlatformManagerTestGetDevice, "PlatformManagerTestGetDevice" },
233     { PLAT_MANAGER_TEST_RELIABILITY, PlatformManagerTestReliability, "PlatformManagerTestReliability" },
234 };
235 
PlatformManagerTestExecute(int cmd)236 int PlatformManagerTestExecute(int cmd)
237 {
238     uint32_t i;
239     int32_t ret = HDF_ERR_NOT_SUPPORT;
240     struct PlatformManager *manager = NULL;
241     struct PlatformManagerTestEntry *entry = NULL;
242 
243     if (cmd > PLAT_MANAGER_TEST_CMD_MAX) {
244         PLAT_LOGE("PlatformManagerTestExecute: invalid cmd:%d", cmd);
245         ret = HDF_ERR_NOT_SUPPORT;
246         PLAT_LOGE("[PlatformManagerTestExecute][======cmd:%d====ret:%d======]", cmd, ret);
247         return ret;
248     }
249 
250     for (i = 0; i < sizeof(g_entry) / sizeof(g_entry[0]); i++) {
251         if (g_entry[i].cmd != cmd || g_entry[i].func == NULL) {
252             continue;
253         }
254         entry = &g_entry[i];
255         break;
256     }
257 
258     if (entry == NULL) {
259         PLAT_LOGE("PlatformManagerTestExecute: no entry matched, cmd = %d!", cmd);
260         return HDF_ERR_NOT_SUPPORT;
261     }
262 
263     ret = PlatformManagerCreate("platform_test_manager", &manager);
264     if (manager == NULL) {
265         PLAT_LOGE("PlatformManagerTestExecute: create manager fail, ret: %d!", ret);
266         return HDF_FAILURE;
267     }
268 
269     ret = PlatformManagerTestCreateDevices();
270     if (ret != HDF_SUCCESS) {
271         PLAT_LOGE("PlatformManagerTestExecute: create devices fail, ret: %d!", ret);
272         PlatformManagerDestroy(manager);
273         return HDF_FAILURE;
274     }
275 
276     ret = entry->func(manager);
277     PlatformManagerDestroy(manager);
278     PlatformManagerTestDestroyDevices();
279 
280     PLAT_LOGE("[PlatformManagerTestExecute][======cmd:%d====ret:%d======]", cmd, ret);
281     return ret;
282 }
283 
PlatformManagerTestExecuteAll(void)284 void PlatformManagerTestExecuteAll(void)
285 {
286     int32_t i;
287     int32_t ret;
288     int32_t fails = 0;
289 
290     for (i = 0; i < PLAT_MANAGER_TEST_CMD_MAX; i++) {
291         ret = PlatformManagerTestExecute(i);
292         fails += (ret != HDF_SUCCESS) ? 1 : 0;
293     }
294 
295     PLAT_LOGE("PlatformManagerTestExecuteALL: **********PASS:%d  FAIL:%d************\n\n",
296         PLAT_MANAGER_TEST_CMD_MAX - fails, fails);
297 }
298