Skip to content

Commit

Permalink
Merge pull request Mbed-TLS#88 from tom-daubney-arm/add_component_tes…
Browse files Browse the repository at this point in the history
…t_cmake_support

Add component_test_cmake_* support
  • Loading branch information
ronald-cron-arm authored Nov 29, 2023
2 parents fbe0e5a + 74235ff commit d3b897d
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/psa/crypto_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@
*/
//#define TF_PSA_CRYPTO_PLATFORM_ZEROIZE

/*
* Platform exit macros
*/

//#define TF_PSA_CRYPTO_PLATFORM_EXIT exit /**< Default exit to use, can be undefined */
//#define TF_PSA_CRYPTO_PLATFORM_EXIT_SUCCESS 0 /**< Default exit value to use, can be undefined */
//#define TF_PSA_CRYPTO_PLATFORM_EXIT_FAILURE 1 /**< Default exit value to use, can be undefined */

/** \} name SECTION: General configuration options */

/**
Expand Down
22 changes: 22 additions & 0 deletions include/tf_psa_crypto/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,28 @@ int tf_psa_crypto_platform_entropy_nv_seed_write(unsigned char *buf,
*/
void tf_psa_crypto_platform_zeroize(void *buf, size_t len);

/*
* Platform exit macros
*/

#if defined(TF_PSA_CRYPTO_PLATFORM_EXIT)
#define tf_psa_crypto_exit TF_PSA_CRYPTO_PLATFORM_EXIT
#else
#define tf_psa_crypto_exit exit
#endif

#if defined(TF_PSA_CRYPTO_PLATFORM_EXIT_SUCCESS)
#define TF_PSA_CRYPTO_EXIT_SUCCESS TF_PSA_CRYPTO_PLATFORM_EXIT_SUCCESS
#else
#define TF_PSA_CRYPTO_EXIT_SUCCESS 0
#endif

#if defined(TF_PSA_CRYPTO_PLATFORM_EXIT_FAILURE)
#define TF_PSA_CRYPTO_EXIT_FAILURE TF_PSA_CRYPTO_PLATFORM_EXIT_FAILURE
#else
#define TF_PSA_CRYPTO_EXIT_FAILURE 1
#endif

#ifdef __cplusplus
}
#endif
Expand Down
1 change: 1 addition & 0 deletions programs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
add_subdirectory(psa)
add_subdirectory(test)
7 changes: 7 additions & 0 deletions programs/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
if(USE_SHARED_TF_PSA_CRYPTO_LIBRARY AND
NOT ${CMAKE_SYSTEM_NAME} MATCHES "[Ww][Ii][Nn]")
add_executable(dlopen "dlopen.c")
target_include_directories(dlopen PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../include)
target_include_directories(dlopen PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../drivers/builtin/include)
target_link_libraries(dlopen ${CMAKE_DL_LIBS})
endif()
59 changes: 59 additions & 0 deletions programs/test/dlopen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Test dynamic loading of libtfpsacrypto
*
* Copyright The Mbed TLS Contributors
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
*/

#include "psa/build_info.h"
#include "psa/crypto.h"
#include "tf_psa_crypto/platform.h"

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

#define CRYPTO_SO_FILENAME "libtfpsacrypto" SO_SUFFIX
#define CRYPTO_SO_PATH "core/" CRYPTO_SO_FILENAME
#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); \
tf_psa_crypto_exit(TF_PSA_CRYPTO_EXIT_FAILURE); \
} \
} \
while (0)

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

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)
{
tf_psa_crypto_printf("dlopen(%s): Call to psa_crypto_init was successful.\n",
CRYPTO_SO_FILENAME);
}
else
{
tf_psa_crypto_printf("dlopen(%s): Call to psa_crypto_init failed.\n",
CRYPTO_SO_FILENAME);
}

dlclose(crypto_so);
CHECK_DLERROR("dlclose", CRYPTO_SO_PATH);
return 0;
}
45 changes: 45 additions & 0 deletions tests/all_sh_components.txt
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,48 @@ component_test_psa_drivers () {
cd "$TF_PSA_CRYPTO_ROOT_DIR"
rm -rf "$OUT_OF_SOURCE_DIR"
}

component_test_cmake_as_subdirectory () {

msg "build: cmake 'as-subdirectory' build"
cd programs/test/cmake_subproject
# Note: Explicitly generate files as these are turned off in releases
cmake -D GEN_FILES=ON .
make
./cmake_subproject
}

component_test_cmake_as_package () {

msg "build: cmake 'as-package' build"
cd programs/test/cmake_package
cmake .
make
./cmake_package
}

component_test_cmake_as_package_install () {

msg "build: cmake 'as-installed-package' build"
cd programs/test/cmake_package_install
cmake .
make
./cmake_package_install
}

component_test_cmake_shared () {
msg "build/test: cmake shared" # ~ 2min

TF_PSA_CRYPTO_ROOT_DIR="$PWD"
mkdir "$OUT_OF_SOURCE_DIR"
cd "$OUT_OF_SOURCE_DIR"

cmake -DUSE_SHARED_TF_PSA_CRYPTO_LIBRARY=On -DUSE_STATIC_TF_PSA_CRYPTO_LIBRARY=Off ..
make
ldd programs/psa/aead_demo | grep libtfpsacrypto
make test
./programs/test/dlopen

cd "$TF_PSA_CRYPTO_ROOT_DIR"
rm -rf "$OUT_OF_SOURCE_DIR"
}

0 comments on commit d3b897d

Please sign in to comment.