tests: unittest: Add hook function for handle hal backend library 66/276566/2
authorChanwoo Choi <cw00.choi@samsung.com>
Thu, 16 Jun 2022 05:36:55 +0000 (14:36 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Wed, 22 Jun 2022 03:14:42 +0000 (12:14 +0900)
The hal-api-common uses the dlopen/dlsym/dlclose to load/unload
the shared library of hal backend and find symbol from hal backend library.
For testing hal-api-common functions when building time, re-implement
dlopen/dlsym/dlclose for hooking.By implementing the hooking functions,
be able to verify the internal logic of hal-api-common functions.

Following fucntions are re-implemented for hooking:
- void *dlopen(const char *filename, int flags)
- int dlclose(void *handle)
- void *dlsym(void *handle, const char *symbol)
- int access(const char *pathname, int mode)

And hal-backend-[module] (e.g., tbm/tdm/audio/camera/devcie/power etc)
have to contain the their own hal_backend structure to implement
the h/w device-dependent hal backend. Instead of adding the each hal_backend
structure for all hal modules, use the common 'hal_backend_module_data'
regardless of hal module name.

Change-Id: Icbcba49a654f9ed1a287233c9cc0e3efcca508df
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
tests/unittest/hal-api-common-hook.c [new file with mode: 0644]

diff --git a/tests/unittest/hal-api-common-hook.c b/tests/unittest/hal-api-common-hook.c
new file mode 100644 (file)
index 0000000..c55b1cc
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+#include <dlfcn.h>
+#include <unistd.h>
+
+#include "hal-common-interface.h"
+
+/*
+ * All hal-backend-[module] (e.g., tbm/tdm/audio/camera/devcie/power etc)
+ * have to contain the their own hal_backend structure to implement
+ * the h/w device-dependent hal backend. Instead of adding the each hal_backend
+ * structure for all hal modules, use the common hal_backend_module_data
+ * regardless of hal module name. Below dlsym hooking function doesn't
+ * check the symbol name and then return the common hal_backend_module_data.
+ * */
+static int hal_backend_init(void **data) { return 0; }
+static int hal_backend_exit(void *data) { return 0; }
+
+hal_backend hal_backend_module_data = {
+       .name = "hal-backend-module",
+       .vendor = "hal-backend-vendor",
+       .abi_version = HAL_ABI_VERSION_TIZEN_7_0,
+       .init = hal_backend_init,
+       .exit = hal_backend_exit,
+};
+
+/*
+ * hal-api-common uses the dlopen/dlsym/dlclose/access to load/unload
+ * the shared library of hal backend and find symbol from hal backend library.
+ * For testing hal-api-common functions when building time, re-implement
+ * dlopen/dlsym/dlclose/access for hooking.By implementing the hooking functions,
+ * be able to verify the internal logic of hal-api-common functions.
+ */
+void *dlopen(const char *filename, int flags) {
+       return calloc(1, sizeof(void *));
+}
+
+int dlclose(void *handle) {
+       free(handle);
+       return 0;
+}
+
+void *dlsym(void *handle, const char *symbol) {
+       return (void *)&hal_backend_module_data;
+}
+
+int access(const char *pathname, int mode) {
+       return 0;
+}