Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tf-psa-crypto] Add test_tf_psa_crypto_cmake_shared to components-build-system.sh #181

Open
wants to merge 7 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion framework
29 changes: 20 additions & 9 deletions programs/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
set(executables
benchmark
)
add_dependencies(${programs_target} ${executables})
# Create an empty list of executables which will be filled by all the targets
# that will be built below. This is used at the bottom for the install() command.
set(executables)

foreach(exe IN LISTS executables)
add_executable(${exe} ${exe}.c $<TARGET_OBJECTS:tf_psa_crypto_test>)
function(tfpsacrypto_build_program_common exe)
add_executable(${exe} ${exe}.c)
set_base_compile_options(${exe})
target_link_libraries(${exe} ${tfpsacrypto_target} ${CMAKE_THREAD_LIBS_INIT})
target_include_directories(${exe} PRIVATE ${TF_PSA_CRYPTO_FRAMEWORK_DIR}/framework/tests/include)
endforeach()
add_dependencies(${programs_target} ${exe})
list(APPEND executables exe)
endfunction()

# benchmark
tfpsacrypto_build_program_common(benchmark)
target_sources(benchmark PRIVATE $<TARGET_OBJECTS:tf_psa_crypto_test>)
target_link_libraries(benchmark PRIVATE ${tfpsacrypto_target} ${CMAKE_THREAD_LIBS_INIT})

# dlopen
if(USE_SHARED_TF_PSA_CRYPTO_LIBRARY AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "[Ww][Ii][Nn]")
tfpsacrypto_build_program_common(tfpsacrypto_dlopen)
target_include_directories(tfpsacrypto_dlopen PRIVATE ${TF_PSA_CRYPTO_DIR}/drivers/builtin/include/)
target_link_libraries(tfpsacrypto_dlopen PRIVATE ${CMAKE_DL_LIBS} ${tfpsacrypto_target})
endif()

install(TARGETS ${executables}
DESTINATION "bin"
Expand Down
58 changes: 58 additions & 0 deletions programs/test/tfpsacrypto_dlopen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Test dynamic loading of libtfpsacrypto
*
* Copyright The Mbed TLS Contributors
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
*/

#include "tf-psa-crypto/build_info.h"
#include "psa/crypto.h"
#include "mbedtls/platform.h"

#if defined(__APPLE__)
#define SO_SUFFIX ".dylib"
#else
#define SO_SUFFIX ".so"
#endif

#define CRYPTO_SO_FILENAME "libtfpsacrypto" SO_SUFFIX
#include <stdlib.h>
#include <dlfcn.h>

#define CHECK_DLERROR(function, argument) \
do \
{ \
char *CHECK_DLERROR_error = dlerror(); \
if (CHECK_DLERROR_error != NULL) \
{ \
fprintf(stderr, "Dynamic loading error for %s(%s): %s\n", \
function, argument, CHECK_DLERROR_error); \
mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
} \
} \
while (0)

int main(void)
{
void *crypto_so = dlopen(CRYPTO_SO_FILENAME, RTLD_NOW);
CHECK_DLERROR("dlopen", CRYPTO_SO_FILENAME);

psa_status_t (*psa_crypto_init_ptr)(void) = dlsym(crypto_so, "psa_crypto_init");
CHECK_DLERROR("dlsym", "psa_crypto_init");

psa_status_t status = psa_crypto_init_ptr();
if (status == PSA_SUCCESS)
{
mbedtls_printf("dlopen(%s): Call to psa_crypto_init was successful.\n",
CRYPTO_SO_FILENAME);
}
else
{
mbedtls_printf("dlopen(%s): Call to psa_crypto_init failed.\n",
CRYPTO_SO_FILENAME);
}

dlclose(crypto_so);
CHECK_DLERROR("dlclose", CRYPTO_SO_FILENAME);
return 0;
}
10 changes: 10 additions & 0 deletions tests/scripts/components-build-system.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
#### Build System Testing
################################################################

component_test_tf_psa_crypto_cmake_shared () {
msg "build/test: cmake shared" # ~ 2min
cd $OUT_OF_SOURCE_DIR
cmake -DUSE_SHARED_TF_PSA_CRYPTO_LIBRARY=ON "$TF_PSA_CRYPTO_ROOT_DIR"
make
ldd programs/test/benchmark | grep libtfpsacrypto
make test
$FRAMEWORK/tests/programs/dlopen_demo.sh
}

component_test_tf_psa_crypto_cmake_out_of_source () {
msg "build: cmake tf-psa-crypto 'out-of-source' build"
cd $OUT_OF_SOURCE_DIR
Expand Down