From 2d56d4961d10c523d121bb9301263045c8899cd1 Mon Sep 17 00:00:00 2001 From: Yongjoo Ahn Date: Fri, 7 Mar 2025 15:54:21 +0900 Subject: [PATCH 1/2] Add null check in api Add param null checks in hal_ml_create and hal_ml_destroy Signed-off-by: Yongjoo Ahn --- src/hal-api-ml.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/hal-api-ml.c b/src/hal-api-ml.c index b427ee5..5d9e086 100644 --- a/src/hal-api-ml.c +++ b/src/hal-api-ml.c @@ -166,6 +166,16 @@ hal_ml_param_get (hal_ml_param_h param, const char *key, void **value) int hal_ml_create (const char *backend_name, hal_ml_h *handle) { + if (!handle) { + _E ("Got invalid handle"); + return HAL_ML_ERROR_INVALID_PARAMETER; + } + + if (!backend_name) { + _E ("Got invalid backend name"); + return HAL_ML_ERROR_INVALID_PARAMETER; + } + /* Scan backend only once */ static int scanned = 1; if (scanned == 1) { @@ -219,6 +229,12 @@ int hal_ml_destroy (hal_ml_h handle) { hal_ml_s *ml = (hal_ml_s *) handle; + + if (!handle) { + _E ("Got invalid handle"); + return HAL_ML_ERROR_INVALID_PARAMETER; + } + _I ("Deinitializing backend %s", ml->backend_library_name); int ret = ml->funcs->deinit (ml->backend_private); From 5b826d3f6462f1d104b692fcd259b9e27608e22c Mon Sep 17 00:00:00 2001 From: Yongjoo Ahn Date: Fri, 7 Mar 2025 14:48:37 +0900 Subject: [PATCH 2/2] [test] Add haltests package - Add test binary `ml-haltests` in package hal-api-ml-haltests. - Add some unit tests. Signed-off-by: Yongjoo Ahn --- CMakeLists.txt | 21 ++++++++++ packaging/hal-api-ml.spec | 22 +++++++++-- tests/ml-haltests.cc | 82 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 tests/ml-haltests.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ca0921..0a469af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,8 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) PROJECT(hal-api-ml) +option(ENABLE_HALTESTS "Enable HAL tests" ON) + SET(PREFIX ${CMAKE_INSTALL_PREFIX}) SET(EXEC_PREFIX "${CMAKE_INSTALL_PREFIX}/bin") SET(INCLUDEDIR "${CMAKE_INSTALL_PREFIX}/include") @@ -51,3 +53,22 @@ INSTALL(DIRECTORY include/ DESTINATION include/hal PATTERN "include/*.hh") INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.pc DESTINATION ${LIBDIR}/pkgconfig) + +IF(ENABLE_HALTESTS) +SET(HALTESTS_SRCS + tests/ml-haltests.cc +) + +pkg_check_modules(ml_hal_tests_dep_pkgs REQUIRED + gtest +) + +ADD_EXECUTABLE(ml-haltests ${HALTESTS_SRCS}) +TARGET_LINK_LIBRARIES(ml-haltests + ${PROJECT_NAME} + ${pkgs_LDFLAGS} + ${ml_hal_tests_dep_pkgs_LDFLAGS} +) + +INSTALL(TARGETS ml-haltests DESTINATION /usr/bin/hal) +ENDIF() diff --git a/packaging/hal-api-ml.spec b/packaging/hal-api-ml.spec index 6db1be4..61ba539 100644 --- a/packaging/hal-api-ml.spec +++ b/packaging/hal-api-ml.spec @@ -2,7 +2,7 @@ Name: hal-api-ml Summary: hal-api-ml interface Version: 0.0.1 Release: 0 -Group: Development/Libraries +Group: Machine Learning/ML Framework License: Apache-2.0 Source0: hal-api-ml-%{version}.tar.gz Source1: hal-api-ml.manifest @@ -13,6 +13,7 @@ BuildRequires: cmake BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(hal-api-common) +BuildRequires: pkgconfig(gtest) %description hal-api-ml interface @@ -21,25 +22,33 @@ hal-api-ml interface ### devel package ######### %package devel Summary: hal-api-ml interface -Group: Development/Libraries +Group: Machine Learning/ML Framework Requires: hal-api-ml = %{version}-%{release} %description devel hal-api-ml interface development package +### hal test package ######### +%package haltests +Summary: hal-api-ml tests +Requires: hal-api-ml = %{version}-%{release} + +%description haltests +hal-api-ml tests package + ### build and install ######### %prep %setup -q -%cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} -DCMAKE_LIBDIR_PREFIX=%{_libdir} +%cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} -DCMAKE_LIBDIR_PREFIX=%{_libdir} -DENABLE_HALTESTS=ON %build cp %{SOURCE1} . make %{?jobs:-j%jobs} %check -# (cd tests/unittest && LD_LIBRARY_PATH=../../ ctest -V) +LD_LIBRARY_PATH=./ ./ml-haltests %install rm -rf %{buildroot} @@ -69,3 +78,8 @@ rm -rf %{buildroot} %defattr(-,root,root,-) %{_includedir}/hal/*.* %{_libdir}/pkgconfig/*.pc + +%files haltests +%defattr(-,root,root,-) +%manifest hal-api-ml.manifest +%{_bindir}/hal/ml-haltests diff --git a/tests/ml-haltests.cc b/tests/ml-haltests.cc new file mode 100644 index 0000000..e10fec2 --- /dev/null +++ b/tests/ml-haltests.cc @@ -0,0 +1,82 @@ +#include +#include + + +TEST(HAL_ML_PARAM, usecase) +{ + hal_ml_param_h param; + + EXPECT_EQ (hal_ml_param_create (¶m), HAL_ML_ERROR_NONE); + + EXPECT_EQ (hal_ml_param_set (param, "some key", (void *) "some value"), HAL_ML_ERROR_NONE); + + EXPECT_EQ (hal_ml_param_destroy (param), HAL_ML_ERROR_NONE); +} + +TEST (HAL_ML_PARAM, create_n) +{ + EXPECT_EQ (hal_ml_param_create (nullptr), HAL_ML_ERROR_INVALID_PARAMETER); +} + +TEST (HAL_ML_PARAM, destroy_n) +{ + EXPECT_EQ (hal_ml_param_destroy (nullptr), HAL_ML_ERROR_INVALID_PARAMETER); +} + +TEST (HAL_ML_PARAM, set_n) +{ + EXPECT_EQ (hal_ml_param_set (nullptr, "key", (void *) "value"), HAL_ML_ERROR_INVALID_PARAMETER); + + hal_ml_param_h param; + EXPECT_EQ (hal_ml_param_create (¶m), HAL_ML_ERROR_NONE); + + EXPECT_EQ (hal_ml_param_set (param, nullptr, (void *) "value"), HAL_ML_ERROR_INVALID_PARAMETER); + EXPECT_EQ (hal_ml_param_set (param, "key", nullptr), HAL_ML_ERROR_INVALID_PARAMETER); + + EXPECT_EQ (hal_ml_param_destroy (param), HAL_ML_ERROR_NONE); +} + +TEST (HAL_ML, create_n) +{ + EXPECT_EQ (hal_ml_create (nullptr, nullptr), HAL_ML_ERROR_INVALID_PARAMETER); + EXPECT_EQ (hal_ml_create ("there_is_no_available_backend", nullptr), HAL_ML_ERROR_INVALID_PARAMETER); + + hal_ml_h ml; + EXPECT_EQ (hal_ml_create (nullptr, &ml), HAL_ML_ERROR_INVALID_PARAMETER); + EXPECT_EQ (hal_ml_create ("there_is_no_available_backend", &ml), HAL_ML_ERROR_INVALID_PARAMETER); +} + +TEST (HAL_ML, destroy_n) +{ + EXPECT_EQ (hal_ml_destroy (nullptr), HAL_ML_ERROR_INVALID_PARAMETER); +} + +TEST (HAL_ML, request_n) +{ + EXPECT_EQ (hal_ml_request (nullptr, "some_request", nullptr), HAL_ML_ERROR_INVALID_PARAMETER); + + hal_ml_param_h param; + EXPECT_EQ (hal_ml_param_create (¶m), HAL_ML_ERROR_NONE); + EXPECT_EQ (hal_ml_request (nullptr, "some_request", param), HAL_ML_ERROR_INVALID_PARAMETER); + EXPECT_EQ (hal_ml_param_destroy (param), HAL_ML_ERROR_NONE); +} + +int main (int argc, char *argv[]) +{ + int ret = -1; + + try { + testing::InitGoogleTest (&argc, argv); + } catch(...) { + std::cout << "Exception occurred." << std::endl; + } + + try { + ret = RUN_ALL_TESTS (); + } catch (const ::testing::internal::GoogleTestFailureException& e) { + ret = -1; + std::cout << "GoogleTestFailureException was thrown:" << e.what () << std::endl; + } + + return ret; +}