Skip to content

Commit

Permalink
Add set_key_pair_info requester unit_test
Browse files Browse the repository at this point in the history
Signed-off-by: Wenxing Hou <[email protected]>
  • Loading branch information
Wenxing-hou committed Aug 21, 2024
1 parent 3a42925 commit eada6aa
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 0 deletions.
2 changes: 2 additions & 0 deletions unit_test/test_spdm_requester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ target_sources(test_spdm_requester
chunk_send.c
vendor_request.c
get_key_pair_info.c
set_key_pair_info.c
error_test/get_version_err.c
error_test/get_capabilities_err.c
error_test/negotiate_algorithms_err.c
Expand All @@ -53,6 +54,7 @@ target_sources(test_spdm_requester
error_test/get_event_types_err.c
error_test/vendor_request_err.c
error_test/get_key_pair_info_err.c
error_test/set_key_pair_info_err.c
${LIBSPDM_DIR}/unit_test/spdm_unit_test_common/common.c
${LIBSPDM_DIR}/unit_test/spdm_unit_test_common/algo.c
${LIBSPDM_DIR}/unit_test/spdm_unit_test_common/support.c
Expand Down
105 changes: 105 additions & 0 deletions unit_test/test_spdm_requester/error_test/set_key_pair_info_err.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* Copyright Notice:
* Copyright 2024 DMTF. All rights reserved.
* License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
**/

#include "spdm_unit_test.h"
#include "internal/libspdm_requester_lib.h"
#include "internal/libspdm_secured_message_lib.h"

#if LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP

libspdm_return_t libspdm_requester_set_key_pair_info_err_test_send_message(
void *spdm_context, size_t request_size, const void *request,
uint64_t timeout)
{
libspdm_test_context_t *spdm_test_context;

spdm_test_context = libspdm_get_test_context();
switch (spdm_test_context->case_id) {
case 0x1:
return LIBSPDM_STATUS_SEND_FAIL;
default:
return LIBSPDM_STATUS_SEND_FAIL;
}
}

libspdm_return_t libspdm_requester_set_key_pair_info_err_test_receive_message(
void *spdm_context, size_t *response_size,
void **response, uint64_t timeout)
{
libspdm_test_context_t *spdm_test_context;

spdm_test_context = libspdm_get_test_context();
switch (spdm_test_context->case_id) {
case 0x1:
return LIBSPDM_STATUS_RECEIVE_FAIL;

default:
return LIBSPDM_STATUS_RECEIVE_FAIL;
}
}

/**
* Test 1: message could not be sent
* Expected Behavior: get a RETURN_DEVICE_ERROR return code
**/
void libspdm_test_requester_set_key_pair_info_err_case1(void **state)
{
libspdm_return_t status;
libspdm_test_context_t *spdm_test_context;
libspdm_context_t *spdm_context;

uint8_t key_pair_id;
uint8_t operation;
uint16_t desired_key_usage;
uint32_t desired_asym_algo;
uint8_t desired_assoc_cert_slot_mask;

spdm_test_context = *state;
spdm_context = spdm_test_context->spdm_context;
spdm_test_context->case_id = 0x1;
spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
SPDM_VERSION_NUMBER_SHIFT_BIT;

spdm_context->connection_info.connection_state =
LIBSPDM_CONNECTION_STATE_NEGOTIATED;
spdm_context->connection_info.capability.flags |=
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP;

key_pair_id = 1;
operation = SPDM_SET_KEY_PAIR_INFO_ERASE_OPERATION;
desired_key_usage = 0;
desired_asym_algo = 0;
desired_assoc_cert_slot_mask = 0;

status = libspdm_set_key_pair_info(spdm_context, NULL, key_pair_id,
operation, desired_key_usage, desired_asym_algo,
desired_assoc_cert_slot_mask);
assert_int_equal(status, LIBSPDM_STATUS_SEND_FAIL);
}

libspdm_test_context_t m_libspdm_requester_set_key_pair_info_err_test_context = {
LIBSPDM_TEST_CONTEXT_VERSION,
true,
libspdm_requester_set_key_pair_info_err_test_send_message,
libspdm_requester_set_key_pair_info_err_test_receive_message,
};

int libspdm_requester_set_key_pair_info_error_test_main(void)
{
const struct CMUnitTest spdm_requester_set_key_pair_info_err_tests[] = {
/* SendRequest failed*/
cmocka_unit_test(libspdm_test_requester_set_key_pair_info_err_case1),
};

libspdm_setup_test_context(
&m_libspdm_requester_set_key_pair_info_err_test_context);

return cmocka_run_group_tests(spdm_requester_set_key_pair_info_err_tests,
libspdm_unit_test_group_setup,
libspdm_unit_test_group_teardown);
}

#endif /*LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP*/
131 changes: 131 additions & 0 deletions unit_test/test_spdm_requester/set_key_pair_info.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* Copyright Notice:
* Copyright 2024 DMTF. All rights reserved.
* License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
**/

#include "spdm_unit_test.h"
#include "internal/libspdm_requester_lib.h"
#include "internal/libspdm_secured_message_lib.h"

#if LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP

libspdm_return_t libspdm_requester_set_key_pair_info_test_send_message(
void *spdm_context, size_t request_size, const void *request,
uint64_t timeout)
{
libspdm_test_context_t *spdm_test_context;

spdm_test_context = libspdm_get_test_context();
switch (spdm_test_context->case_id) {
case 0x1:
return LIBSPDM_STATUS_SUCCESS;
default:
return LIBSPDM_STATUS_SEND_FAIL;
}
}

libspdm_return_t libspdm_requester_set_key_pair_info_test_receive_message(
void *spdm_context, size_t *response_size,
void **response, uint64_t timeout)
{
libspdm_test_context_t *spdm_test_context;

spdm_test_context = libspdm_get_test_context();
switch (spdm_test_context->case_id) {

case 0x1: {
spdm_key_pair_info_response_t *spdm_response;
size_t spdm_response_size;
size_t transport_header_size;

transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
spdm_response = (void *)((uint8_t *)*response + transport_header_size);
spdm_response_size = sizeof(spdm_key_pair_info_response_t);

spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13;
spdm_response->header.request_response_code = SPDM_SET_KEY_PAIR_INFO_ACK;
spdm_response->header.param1 = 0;
spdm_response->header.param2 = 0;

libspdm_transport_test_encode_message(spdm_context, NULL, false,
false, spdm_response_size,
spdm_response, response_size,
response);
}
return LIBSPDM_STATUS_SUCCESS;
default:
return LIBSPDM_STATUS_RECEIVE_FAIL;
}
}

/**
* Test 1: Successful response to set key pair info
* Expected Behavior: get a RETURN_SUCCESS return code
**/
void libspdm_test_requester_set_key_pair_info_case1(void **state)
{
libspdm_return_t status;
libspdm_test_context_t *spdm_test_context;
libspdm_context_t *spdm_context;

uint8_t key_pair_id;
uint8_t operation;
uint16_t desired_key_usage;
uint32_t desired_asym_algo;
uint8_t desired_assoc_cert_slot_mask;

spdm_test_context = *state;
spdm_context = spdm_test_context->spdm_context;
spdm_test_context->case_id = 0x1;
spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
SPDM_VERSION_NUMBER_SHIFT_BIT;

spdm_context->connection_info.connection_state =
LIBSPDM_CONNECTION_STATE_NEGOTIATED;
spdm_context->connection_info.capability.flags |=
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP;

key_pair_id = 1;
operation = SPDM_SET_KEY_PAIR_INFO_ERASE_OPERATION;
desired_key_usage = 0;
desired_asym_algo = 0;
desired_assoc_cert_slot_mask = 0;
status = libspdm_set_key_pair_info(spdm_context, NULL, key_pair_id,
operation, desired_key_usage, desired_asym_algo,
desired_assoc_cert_slot_mask);

assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);

/*wrong desired_assoc_cert_slot_mask when operation is erase*/
desired_assoc_cert_slot_mask = 1;
status = libspdm_set_key_pair_info(spdm_context, NULL, key_pair_id,
operation, desired_key_usage, desired_asym_algo,
desired_assoc_cert_slot_mask);

assert_int_equal(status, LIBSPDM_STATUS_INVALID_PARAMETER);
}

libspdm_test_context_t m_libspdm_requester_set_key_pair_info_test_context = {
LIBSPDM_TEST_CONTEXT_VERSION,
true,
libspdm_requester_set_key_pair_info_test_send_message,
libspdm_requester_set_key_pair_info_test_receive_message,
};

int libspdm_requester_set_key_pair_info_test_main(void)
{
const struct CMUnitTest spdm_requester_set_key_pair_info_tests[] = {
/* Successful response to set key pair info, key_pair_id is 1*/
cmocka_unit_test(libspdm_test_requester_set_key_pair_info_case1),
};

libspdm_setup_test_context(
&m_libspdm_requester_set_key_pair_info_test_context);

return cmocka_run_group_tests(spdm_requester_set_key_pair_info_tests,
libspdm_unit_test_group_setup,
libspdm_unit_test_group_teardown);
}

#endif /*LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP*/
13 changes: 13 additions & 0 deletions unit_test/test_spdm_requester/test_spdm_requester.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ int libspdm_requester_get_key_pair_info_test_main(void);
int libspdm_requester_get_key_pair_info_error_test_main(void);
#endif /* LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP */

#if LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP
int libspdm_requester_set_key_pair_info_test_main(void);
int libspdm_requester_set_key_pair_info_error_test_main(void);
#endif /* LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP */

int main(void)
{
Expand Down Expand Up @@ -247,6 +251,15 @@ int main(void)
}
#endif /* LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP */

#if LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP
if (libspdm_requester_set_key_pair_info_test_main() != 0) {
return_value = 1;
}
if (libspdm_requester_set_key_pair_info_error_test_main() != 0) {
return_value = 1;
}
#endif /* LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP */

#if LIBSPDM_ENABLE_VENDOR_DEFINED_MESSAGES
if (libspdm_requester_vendor_cmds_test_main() != 0) {
return_value = 1;
Expand Down

0 comments on commit eada6aa

Please sign in to comment.