LCOV - code coverage report
Current view: top level - unit_test/test_spdm_requester - set_key_pair_info.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 96.7 % 121 117
Test Date: 2025-10-12 08:10:56 Functions: 100.0 % 7 7

            Line data    Source code
       1              : /**
       2              :  *  Copyright Notice:
       3              :  *  Copyright 2024-2025 DMTF. All rights reserved.
       4              :  *  License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
       5              :  **/
       6              : 
       7              : #include "spdm_unit_test.h"
       8              : #include "internal/libspdm_requester_lib.h"
       9              : #include "internal/libspdm_secured_message_lib.h"
      10              : 
      11              : #if LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP
      12              : 
      13            4 : static libspdm_return_t send_message(
      14              :     void *spdm_context, size_t request_size, const void *request, uint64_t timeout)
      15              : {
      16              :     libspdm_test_context_t *spdm_test_context;
      17              : 
      18            4 :     spdm_test_context = libspdm_get_test_context();
      19            4 :     switch (spdm_test_context->case_id) {
      20            4 :     case 0x1:
      21              :     case 0x2:
      22              :     case 0x3:
      23              :     case 0x4:
      24            4 :         return LIBSPDM_STATUS_SUCCESS;
      25            0 :     default:
      26            0 :         return LIBSPDM_STATUS_SEND_FAIL;
      27              :     }
      28              : }
      29              : 
      30            4 : static libspdm_return_t receive_message(
      31              :     void *spdm_context, size_t *response_size, void **response, uint64_t timeout)
      32              : {
      33              :     libspdm_test_context_t *spdm_test_context;
      34              : 
      35            4 :     spdm_test_context = libspdm_get_test_context();
      36            4 :     switch (spdm_test_context->case_id) {
      37              : 
      38            1 :     case 0x1: {
      39              :         spdm_key_pair_info_response_t *spdm_response;
      40              :         size_t spdm_response_size;
      41              :         size_t transport_header_size;
      42              : 
      43            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
      44            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
      45            1 :         spdm_response_size = sizeof(spdm_key_pair_info_response_t);
      46              : 
      47            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13;
      48            1 :         spdm_response->header.request_response_code = SPDM_SET_KEY_PAIR_INFO_ACK;
      49            1 :         spdm_response->header.param1 = 0;
      50            1 :         spdm_response->header.param2 = 0;
      51              : 
      52            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
      53              :                                               false, spdm_response_size,
      54              :                                               spdm_response, response_size,
      55              :                                               response);
      56              :     }
      57            1 :         return LIBSPDM_STATUS_SUCCESS;
      58            1 :     case 0x2: {
      59              :         spdm_key_pair_info_response_t *spdm_response;
      60              :         size_t spdm_response_size;
      61              :         size_t transport_header_size;
      62              : 
      63            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
      64            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
      65            1 :         spdm_response_size = sizeof(spdm_key_pair_info_response_t);
      66              : 
      67            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_12;
      68            1 :         spdm_response->header.request_response_code = SPDM_SET_KEY_PAIR_INFO_ACK;
      69            1 :         spdm_response->header.param1 = 0;
      70            1 :         spdm_response->header.param2 = 0;
      71              : 
      72            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
      73              :                                               false, spdm_response_size,
      74              :                                               spdm_response, response_size,
      75              :                                               response);
      76              :     }
      77            1 :         return LIBSPDM_STATUS_SUCCESS;
      78              : 
      79            1 :     case 0x3: {
      80              :         spdm_key_pair_info_response_t *spdm_response;
      81              :         size_t spdm_response_size;
      82              :         size_t transport_header_size;
      83              : 
      84            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
      85            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
      86            1 :         spdm_response_size = sizeof(spdm_key_pair_info_response_t);
      87              : 
      88            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13;
      89            1 :         spdm_response->header.request_response_code = SPDM_KEY_PAIR_INFO;
      90            1 :         spdm_response->header.param1 = 0;
      91            1 :         spdm_response->header.param2 = 0;
      92              : 
      93            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
      94              :                                               false, spdm_response_size,
      95              :                                               spdm_response, response_size,
      96              :                                               response);
      97              :     }
      98            1 :         return LIBSPDM_STATUS_SUCCESS;
      99            1 :     case 0x4: {
     100              :         spdm_error_response_t *spdm_response;
     101              :         size_t spdm_response_size;
     102              :         size_t transport_header_size;
     103              : 
     104            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     105            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     106            1 :         spdm_response_size = sizeof(spdm_error_response_t);
     107              : 
     108            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_14;
     109            1 :         spdm_response->header.request_response_code = SPDM_ERROR;
     110            1 :         spdm_response->header.param1 = SPDM_ERROR_CODE_RESET_REQUIRED;
     111            1 :         spdm_response->header.param2 = 0;
     112              : 
     113            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     114              :                                               false, spdm_response_size,
     115              :                                               spdm_response, response_size,
     116              :                                               response);
     117              :     }
     118            1 :         return LIBSPDM_STATUS_SUCCESS;
     119            0 :     default:
     120            0 :         return LIBSPDM_STATUS_RECEIVE_FAIL;
     121              :     }
     122              : }
     123              : 
     124              : /**
     125              :  * Test 1: Successful response to set key pair info
     126              :  * Expected Behavior: get a LIBSPDM_STATUS_SUCCESS return code
     127              :  **/
     128            1 : static void req_set_key_pair_info_case1(void **state)
     129              : {
     130              :     libspdm_return_t status;
     131              :     libspdm_test_context_t *spdm_test_context;
     132              :     libspdm_context_t *spdm_context;
     133              : 
     134              :     uint8_t key_pair_id;
     135              :     uint8_t operation;
     136              :     uint16_t desired_key_usage;
     137              :     uint32_t desired_asym_algo;
     138              :     uint8_t desired_assoc_cert_slot_mask;
     139              : 
     140            1 :     spdm_test_context = *state;
     141            1 :     spdm_context = spdm_test_context->spdm_context;
     142            1 :     spdm_test_context->case_id = 0x1;
     143            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
     144              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     145              : 
     146            1 :     spdm_context->connection_info.connection_state =
     147              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
     148            1 :     spdm_context->connection_info.capability.flags |=
     149              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP;
     150              : 
     151            1 :     key_pair_id = 1;
     152            1 :     operation = SPDM_SET_KEY_PAIR_INFO_ERASE_OPERATION;
     153            1 :     desired_key_usage = 0;
     154            1 :     desired_asym_algo = 0;
     155            1 :     desired_assoc_cert_slot_mask = 0;
     156            1 :     status = libspdm_set_key_pair_info(spdm_context, NULL, key_pair_id,
     157              :                                        operation, desired_key_usage, desired_asym_algo,
     158              :                                        desired_assoc_cert_slot_mask);
     159              : 
     160            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     161              : 
     162              :     /*wrong desired_assoc_cert_slot_mask when operation is erase*/
     163            1 :     desired_assoc_cert_slot_mask = 1;
     164            1 :     status = libspdm_set_key_pair_info(spdm_context, NULL, key_pair_id,
     165              :                                        operation, desired_key_usage, desired_asym_algo,
     166              :                                        desired_assoc_cert_slot_mask);
     167              : 
     168            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_PARAMETER);
     169            1 : }
     170              : 
     171              : /**
     172              :  * Test 2: Fail case, The response version is incorrect .
     173              :  * Expected Behavior: returns a status of LIBSPDM_STATUS_INVALID_MSG_FIELD.
     174              :  **/
     175            1 : static void req_set_key_pair_info_case2(void **state)
     176              : {
     177              :     libspdm_return_t status;
     178              :     libspdm_test_context_t *spdm_test_context;
     179              :     libspdm_context_t *spdm_context;
     180              : 
     181              :     uint8_t key_pair_id;
     182              :     uint8_t operation;
     183              :     uint16_t desired_key_usage;
     184              :     uint32_t desired_asym_algo;
     185              :     uint8_t desired_assoc_cert_slot_mask;
     186              : 
     187            1 :     spdm_test_context = *state;
     188            1 :     spdm_context = spdm_test_context->spdm_context;
     189            1 :     spdm_test_context->case_id = 0x2;
     190            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
     191              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     192              : 
     193            1 :     spdm_context->connection_info.connection_state =
     194              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
     195            1 :     spdm_context->connection_info.capability.flags |=
     196              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP;
     197              : 
     198            1 :     key_pair_id = 1;
     199            1 :     operation = SPDM_SET_KEY_PAIR_INFO_ERASE_OPERATION;
     200            1 :     desired_key_usage = 0;
     201            1 :     desired_asym_algo = 0;
     202            1 :     desired_assoc_cert_slot_mask = 0;
     203            1 :     status = libspdm_set_key_pair_info(spdm_context, NULL, key_pair_id,
     204              :                                        operation, desired_key_usage, desired_asym_algo,
     205              :                                        desired_assoc_cert_slot_mask);
     206              : 
     207            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
     208            1 : }
     209              : 
     210              : /**
     211              :  * Test 3: Fail case, The response code is incorrect
     212              :  * Expected Behavior: returns a status of LIBSPDM_STATUS_INVALID_MSG_FIELD.
     213              :  **/
     214            1 : static void req_set_key_pair_info_case3(void **state)
     215              : {
     216              :     libspdm_return_t status;
     217              :     libspdm_test_context_t *spdm_test_context;
     218              :     libspdm_context_t *spdm_context;
     219              : 
     220              :     uint8_t key_pair_id;
     221              :     uint8_t operation;
     222              :     uint16_t desired_key_usage;
     223              :     uint32_t desired_asym_algo;
     224              :     uint8_t desired_assoc_cert_slot_mask;
     225              : 
     226            1 :     spdm_test_context = *state;
     227            1 :     spdm_context = spdm_test_context->spdm_context;
     228            1 :     spdm_test_context->case_id = 0x3;
     229            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
     230              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     231              : 
     232            1 :     spdm_context->connection_info.connection_state =
     233              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
     234            1 :     spdm_context->connection_info.capability.flags |=
     235              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP;
     236              : 
     237            1 :     key_pair_id = 1;
     238            1 :     operation = SPDM_SET_KEY_PAIR_INFO_ERASE_OPERATION;
     239            1 :     desired_key_usage = 0;
     240            1 :     desired_asym_algo = 0;
     241            1 :     desired_assoc_cert_slot_mask = 0;
     242            1 :     status = libspdm_set_key_pair_info(spdm_context, NULL, key_pair_id,
     243              :                                        operation, desired_key_usage, desired_asym_algo,
     244              :                                        desired_assoc_cert_slot_mask);
     245              : 
     246            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
     247            1 : }
     248              : 
     249              : /**
     250              :  * Test 4: Successful reset required error code
     251              :  * Expected Behavior: get a RESET_REQURED_PEER return code
     252              :  **/
     253            1 : static void req_set_key_pair_info_case4(void **state)
     254              : {
     255              :     libspdm_return_t status;
     256              :     libspdm_test_context_t *spdm_test_context;
     257              :     libspdm_context_t *spdm_context;
     258              : 
     259              :     uint8_t key_pair_id;
     260              :     uint8_t operation;
     261              :     uint16_t desired_key_usage;
     262              :     uint32_t desired_asym_algo;
     263              :     uint8_t desired_assoc_cert_slot_mask;
     264              : 
     265            1 :     spdm_test_context = *state;
     266            1 :     spdm_context = spdm_test_context->spdm_context;
     267            1 :     spdm_test_context->case_id = 0x4;
     268            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_14 <<
     269              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     270              : 
     271            1 :     spdm_context->connection_info.connection_state =
     272              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
     273            1 :     spdm_context->connection_info.capability.flags |=
     274              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP;
     275            1 :     spdm_context->connection_info.capability.flags |=
     276              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_RESET_CAP;
     277              : 
     278            1 :     key_pair_id = 1;
     279            1 :     operation = SPDM_SET_KEY_PAIR_INFO_ERASE_OPERATION;
     280            1 :     desired_key_usage = 0;
     281            1 :     desired_asym_algo = 0;
     282            1 :     desired_assoc_cert_slot_mask = 0;
     283            1 :     status = libspdm_set_key_pair_info(spdm_context, NULL, key_pair_id,
     284              :                                        operation, desired_key_usage, desired_asym_algo,
     285              :                                        desired_assoc_cert_slot_mask);
     286              : 
     287            1 :     assert_int_equal(status, LIBSPDM_STATUS_RESET_REQUIRED_PEER);
     288            1 : }
     289              : 
     290            1 : int libspdm_req_set_key_pair_info_test(void)
     291              : {
     292            1 :     const struct CMUnitTest test_cases[] = {
     293              :         /* Successful response to set key pair info, key_pair_id is 1*/
     294              :         cmocka_unit_test(req_set_key_pair_info_case1),
     295              :         /* The response version is incorrect */
     296              :         cmocka_unit_test(req_set_key_pair_info_case2),
     297              :         /* The response code is incorrect */
     298              :         cmocka_unit_test(req_set_key_pair_info_case3),
     299              :         /* Successful response with reset required error code */
     300              :         cmocka_unit_test(req_set_key_pair_info_case4),
     301              :     };
     302              : 
     303            1 :     libspdm_test_context_t test_context = {
     304              :         LIBSPDM_TEST_CONTEXT_VERSION,
     305              :         true,
     306              :         send_message,
     307              :         receive_message,
     308              :     };
     309              : 
     310            1 :     libspdm_setup_test_context(&test_context);
     311              : 
     312            1 :     return cmocka_run_group_tests(test_cases,
     313              :                                   libspdm_unit_test_group_setup,
     314              :                                   libspdm_unit_test_group_teardown);
     315              : }
     316              : 
     317              : #endif /*LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP*/
        

Generated by: LCOV version 2.0-1