Skip to content

Commit

Permalink
transport: storage: add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Wilfred Mallawa <[email protected]>
  • Loading branch information
twilfredo committed Nov 29, 2024
1 parent 2f0be54 commit bade1db
Show file tree
Hide file tree
Showing 5 changed files with 569 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,8 @@ else()
add_subdirectory(unit_test/fuzzing/test_transport/test_spdm_transport_mctp_decode_message)
add_subdirectory(unit_test/fuzzing/test_transport/test_spdm_transport_pci_doe_encode_message)
add_subdirectory(unit_test/fuzzing/test_transport/test_spdm_transport_pci_doe_decode_message)
add_subdirectory(unit_test/fuzzing/test_transport/test_spdm_transport_storage_encode_message)
add_subdirectory(unit_test/fuzzing/test_transport/test_spdm_transport_storage_decode_message)
add_subdirectory(unit_test/fuzzing/test_secured_message/test_spdm_decode_secured_message)
add_subdirectory(unit_test/fuzzing/test_secured_message/test_spdm_encode_secured_message)
add_subdirectory(unit_test/fuzzing/test_spdm_crypt/test_x509_certificate_check)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
cmake_minimum_required(VERSION 3.5)

add_executable(test_spdm_transport_storage_decode_message)

target_include_directories(test_spdm_transport_storage_decode_message
PRIVATE
${LIBSPDM_DIR}/unit_test/fuzzing/test_transport/test_spdm_transport_storage_decode_message
${LIBSPDM_DIR}/include
${LIBSPDM_DIR}/include/library
${LIBSPDM_DIR}/unit_test/include
${LIBSPDM_DIR}/unit_test/fuzzing/spdm_unit_fuzzing_common
${LIBSPDM_DIR}/os_stub/include
)

if(TOOLCHAIN STREQUAL "KLEE")
target_include_directories(test_spdm_transport_storage_decode_message
PRIVATE
$ENV{KLEE_SRC_PATH}/include
)
endif()

target_sources(test_spdm_transport_storage_decode_message
PRIVATE
spdm_transport_storage_decode_message.c
${PROJECT_SOURCE_DIR}/unit_test/fuzzing/spdm_unit_fuzzing_common/common.c
${PROJECT_SOURCE_DIR}/unit_test/fuzzing/spdm_unit_fuzzing_common/toolchain_harness.c
${PROJECT_SOURCE_DIR}/unit_test/fuzzing/spdm_unit_fuzzing_common/algo.c
)

if((TOOLCHAIN STREQUAL "KLEE") OR (TOOLCHAIN STREQUAL "CBMC"))
target_link_libraries(test_spdm_transport_storage_decode_message
PRIVATE
$<TARGET_OBJECTS:memlib>
$<TARGET_OBJECTS:debuglib>
$<TARGET_OBJECTS:spdm_transport_storage_lib>
$<TARGET_OBJECTS:spdm_common_lib>
$<TARGET_OBJECTS:${CRYPTO_LIB_PATHS}>
$<TARGET_OBJECTS:rnglib>
$<TARGET_OBJECTS:cryptlib_${CRYPTO}>
$<TARGET_OBJECTS:malloclib>
$<TARGET_OBJECTS:spdm_crypt_lib>
$<TARGET_OBJECTS:spdm_secured_message_lib>
$<TARGET_OBJECTS:spdm_transport_test_lib>
$<TARGET_OBJECTS:spdm_device_secret_lib_null>
)
else()
target_link_libraries(test_spdm_transport_storage_decode_message
PRIVATE
memlib
debuglib
spdm_transport_storage_lib
spdm_common_lib
${CRYPTO_LIB_PATHS}
rnglib
cryptlib_${CRYPTO}
malloclib
spdm_crypt_lib
spdm_secured_message_lib
spdm_transport_test_lib
spdm_device_secret_lib_null
)
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/**
* Copyright Notice:
* Copyright 2021-2022 DMTF. All rights reserved.
* License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
**/

#include "internal/libspdm_responder_lib.h"
#include "spdm_transport_storage_lib.h"
#include "industry_standard/spdm_storage_binding.h"
#include "industry_standard/pcidoe.h"
#include "spdm_unit_fuzzing.h"
#include "toolchain_harness.h"

libspdm_test_context_t m_libspdm_transport_storage_test_context = {
LIBSPDM_TEST_CONTEXT_VERSION,
false,
};

size_t libspdm_get_max_buffer_size(void)
{
return LIBSPDM_MAX_SPDM_MSG_SIZE;
}

void libspdm_test_transport_storage_decode_message(void **state)
{
libspdm_test_context_t *spdm_test_context = *state;
void *transport_message, *message, *dec_message;
size_t transport_message_size, message_size, dec_message_size;
uint32_t *session_id;
bool is_app_message, dec_is_app_message, is_request_message;
libspdm_return_t ret;

if (m_libspdm_transport_storage_test_context.test_buffer_size < sizeof(storage_spdm_transport_header)) {
LIBSPDM_ASSERT(false);
}

/* Encode an SPDM Storage Message First (Test Setup) */
transport_message_size = LIBSPDM_MAX_SENDER_RECEIVER_BUFFER_SIZE;
transport_message = spdm_test_context->test_buffer;
is_app_message = false;
is_request_message = true;
message_size = 12;
message = (uint8_t *)transport_message + sizeof(storage_spdm_transport_header);

ret = libspdm_transport_storage_encode_message(
state,
NULL,
is_app_message,
is_request_message,
message_size,
message,
&transport_message_size,
&transport_message);
LIBSPDM_ASSERT(ret == LIBSPDM_STATUS_SUCCESS);

ret = libspdm_transport_storage_decode_message(
state,
&session_id,
&dec_is_app_message,
is_request_message,
transport_message_size,
transport_message,
&dec_message_size,
&dec_message
);
/* Trivial Assertions */
LIBSPDM_ASSERT(ret == LIBSPDM_STATUS_SUCCESS);
LIBSPDM_ASSERT(dec_is_app_message == false);
LIBSPDM_ASSERT(dec_message_size == message_size);
}

void libspdm_test_transport_storage_decode_management_cmd(void **state)
{
libspdm_test_context_t *spdm_test_context = *state;
void *transport_message;
size_t transport_message_size;
size_t alloc_len;
uint32_t decoded_alloc_len;
uint8_t cmd_direction;
uint8_t transport_operation, transport_command;
libspdm_return_t ret;

if (m_libspdm_transport_storage_test_context.test_buffer_size < sizeof(storage_spdm_transport_header)) {
LIBSPDM_ASSERT(false);
}

transport_message_size = LIBSPDM_MAX_SENDER_RECEIVER_BUFFER_SIZE;
transport_message = spdm_test_context->test_buffer;
cmd_direction = LIBSPDM_STORAGE_CMD_DIRECTION_IF_RECV;
transport_operation = SPDM_STORAGE_OPERATION_CODE_DISCOVERY;
alloc_len = 0xFF;

/* Encode a management command first (Test Setup) */
ret = libspdm_transport_storage_encode_management_cmd(
cmd_direction,
transport_operation,
0,
&transport_message_size,
&alloc_len,
transport_message);
LIBSPDM_ASSERT(ret == LIBSPDM_STATUS_SUCCESS);

/* Attempt to decode with trivial assertions */
ret = libspdm_transport_storage_decode_management_cmd(
transport_message_size,
transport_message,
&transport_command,
&decoded_alloc_len
);
LIBSPDM_ASSERT(ret == LIBSPDM_STATUS_SUCCESS);
LIBSPDM_ASSERT(transport_command == SPDM_STORAGE_OPERATION_CODE_DISCOVERY);
LIBSPDM_ASSERT(decoded_alloc_len == sizeof(storage_spdm_transport_header));
}

void libspdm_run_test_harness(void *test_buffer, size_t test_buffer_size)
{
void *state;

libspdm_setup_test_context(&m_libspdm_transport_storage_test_context);

m_libspdm_transport_storage_test_context.test_buffer = test_buffer;
m_libspdm_transport_storage_test_context.test_buffer_size = test_buffer_size;

libspdm_unit_test_group_setup(&state);

libspdm_test_transport_storage_decode_management_cmd(&state);
libspdm_test_transport_storage_decode_message(&state);

libspdm_unit_test_group_teardown(&state);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
cmake_minimum_required(VERSION 3.5)

add_executable(test_spdm_transport_storage_encode_message)

target_include_directories(test_spdm_transport_storage_encode_message
PRIVATE
${LIBSPDM_DIR}/unit_test/fuzzing/test_transport/test_spdm_transport_storage_encode_message
${LIBSPDM_DIR}/include
${LIBSPDM_DIR}/include/library
${LIBSPDM_DIR}/unit_test/include
${LIBSPDM_DIR}/unit_test/fuzzing/spdm_unit_fuzzing_common
${LIBSPDM_DIR}/os_stub/include
)

if(TOOLCHAIN STREQUAL "KLEE")
target_include_directories(test_spdm_transport_storage_encode_message
PRIVATE
$ENV{KLEE_SRC_PATH}/include
)
endif()

target_sources(test_spdm_transport_storage_encode_message
PRIVATE
spdm_transport_storage_encode_message.c
${PROJECT_SOURCE_DIR}/unit_test/fuzzing/spdm_unit_fuzzing_common/common.c
${PROJECT_SOURCE_DIR}/unit_test/fuzzing/spdm_unit_fuzzing_common/toolchain_harness.c
${PROJECT_SOURCE_DIR}/unit_test/fuzzing/spdm_unit_fuzzing_common/algo.c
)

if((TOOLCHAIN STREQUAL "KLEE") OR (TOOLCHAIN STREQUAL "CBMC"))
target_link_libraries(test_spdm_transport_storage_encode_message
PRIVATE
$<TARGET_OBJECTS:memlib>
$<TARGET_OBJECTS:debuglib>
$<TARGET_OBJECTS:spdm_transport_storage_lib>
$<TARGET_OBJECTS:spdm_common_lib>
$<TARGET_OBJECTS:${CRYPTO_LIB_PATHS}>
$<TARGET_OBJECTS:rnglib>
$<TARGET_OBJECTS:cryptlib_${CRYPTO}>
$<TARGET_OBJECTS:malloclib>
$<TARGET_OBJECTS:spdm_crypt_lib>
$<TARGET_OBJECTS:spdm_secured_message_lib>
$<TARGET_OBJECTS:spdm_transport_test_lib>
$<TARGET_OBJECTS:spdm_device_secret_lib_null>
)
else()
target_link_libraries(test_spdm_transport_storage_encode_message
PRIVATE
memlib
debuglib
spdm_transport_storage_lib
spdm_common_lib
${CRYPTO_LIB_PATHS}
rnglib
cryptlib_${CRYPTO}
malloclib
spdm_crypt_lib
spdm_secured_message_lib
spdm_transport_test_lib
spdm_device_secret_lib_null
)
endif()
Loading

0 comments on commit bade1db

Please sign in to comment.