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.8 % 125 121
Test Date: 2026-03-15 08:15:47 Functions: 100.0 % 7 7

            Line data    Source code
       1              : /**
       2              :  *  Copyright Notice:
       3              :  *  Copyright 2024-2026 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              :     uint32_t desired_pqc_asym_algo;
     139              :     uint8_t desired_assoc_cert_slot_mask;
     140              : 
     141            1 :     spdm_test_context = *state;
     142            1 :     spdm_context = spdm_test_context->spdm_context;
     143            1 :     spdm_test_context->case_id = 0x1;
     144            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
     145              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     146              : 
     147            1 :     spdm_context->connection_info.connection_state = 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_pqc_asym_algo = 0;
     156            1 :     desired_assoc_cert_slot_mask = 0;
     157            1 :     status = libspdm_set_key_pair_info(spdm_context, NULL, key_pair_id,
     158              :                                        operation, desired_key_usage, desired_asym_algo,
     159              :                                        desired_pqc_asym_algo, desired_assoc_cert_slot_mask);
     160              : 
     161            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     162              : 
     163              :     /*wrong desired_assoc_cert_slot_mask when operation is erase*/
     164            1 :     desired_assoc_cert_slot_mask = 1;
     165            1 :     status = libspdm_set_key_pair_info(spdm_context, NULL, key_pair_id,
     166              :                                        operation, desired_key_usage, desired_asym_algo,
     167              :                                        desired_pqc_asym_algo, desired_assoc_cert_slot_mask);
     168              : 
     169            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_PARAMETER);
     170            1 : }
     171              : 
     172              : /**
     173              :  * Test 2: Fail case, The response version is incorrect .
     174              :  * Expected Behavior: returns a status of LIBSPDM_STATUS_INVALID_MSG_FIELD.
     175              :  **/
     176            1 : static void req_set_key_pair_info_case2(void **state)
     177              : {
     178              :     libspdm_return_t status;
     179              :     libspdm_test_context_t *spdm_test_context;
     180              :     libspdm_context_t *spdm_context;
     181              : 
     182              :     uint8_t key_pair_id;
     183              :     uint8_t operation;
     184              :     uint16_t desired_key_usage;
     185              :     uint32_t desired_asym_algo;
     186              :     uint32_t desired_pqc_asym_algo;
     187              :     uint8_t desired_assoc_cert_slot_mask;
     188              : 
     189            1 :     spdm_test_context = *state;
     190            1 :     spdm_context = spdm_test_context->spdm_context;
     191            1 :     spdm_test_context->case_id = 0x2;
     192            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
     193              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     194              : 
     195            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
     196            1 :     spdm_context->connection_info.capability.flags |=
     197              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP;
     198              : 
     199            1 :     key_pair_id = 1;
     200            1 :     operation = SPDM_SET_KEY_PAIR_INFO_ERASE_OPERATION;
     201            1 :     desired_key_usage = 0;
     202            1 :     desired_asym_algo = 0;
     203            1 :     desired_pqc_asym_algo = 0;
     204            1 :     desired_assoc_cert_slot_mask = 0;
     205            1 :     status = libspdm_set_key_pair_info(spdm_context, NULL, key_pair_id,
     206              :                                        operation, desired_key_usage, desired_asym_algo,
     207              :                                        desired_pqc_asym_algo, desired_assoc_cert_slot_mask);
     208              : 
     209            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
     210            1 : }
     211              : 
     212              : /**
     213              :  * Test 3: Fail case, The response code is incorrect
     214              :  * Expected Behavior: returns a status of LIBSPDM_STATUS_INVALID_MSG_FIELD.
     215              :  **/
     216            1 : static void req_set_key_pair_info_case3(void **state)
     217              : {
     218              :     libspdm_return_t status;
     219              :     libspdm_test_context_t *spdm_test_context;
     220              :     libspdm_context_t *spdm_context;
     221              : 
     222              :     uint8_t key_pair_id;
     223              :     uint8_t operation;
     224              :     uint16_t desired_key_usage;
     225              :     uint32_t desired_asym_algo;
     226              :     uint32_t desired_pqc_asym_algo;
     227              :     uint8_t desired_assoc_cert_slot_mask;
     228              : 
     229            1 :     spdm_test_context = *state;
     230            1 :     spdm_context = spdm_test_context->spdm_context;
     231            1 :     spdm_test_context->case_id = 0x3;
     232            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
     233              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     234              : 
     235            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
     236            1 :     spdm_context->connection_info.capability.flags |=
     237              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP;
     238              : 
     239            1 :     key_pair_id = 1;
     240            1 :     operation = SPDM_SET_KEY_PAIR_INFO_ERASE_OPERATION;
     241            1 :     desired_key_usage = 0;
     242            1 :     desired_asym_algo = 0;
     243            1 :     desired_pqc_asym_algo = 0;
     244            1 :     desired_assoc_cert_slot_mask = 0;
     245            1 :     status = libspdm_set_key_pair_info(spdm_context, NULL, key_pair_id,
     246              :                                        operation, desired_key_usage, desired_asym_algo,
     247              :                                        desired_pqc_asym_algo, desired_assoc_cert_slot_mask);
     248              : 
     249            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
     250            1 : }
     251              : 
     252              : /**
     253              :  * Test 4: Successful reset required error code
     254              :  * Expected Behavior: get a RESET_REQURED_PEER return code
     255              :  **/
     256            1 : static void req_set_key_pair_info_case4(void **state)
     257              : {
     258              :     libspdm_return_t status;
     259              :     libspdm_test_context_t *spdm_test_context;
     260              :     libspdm_context_t *spdm_context;
     261              : 
     262              :     uint8_t key_pair_id;
     263              :     uint8_t operation;
     264              :     uint16_t desired_key_usage;
     265              :     uint32_t desired_asym_algo;
     266              :     uint32_t desired_pqc_asym_algo;
     267              :     uint8_t desired_assoc_cert_slot_mask;
     268              : 
     269            1 :     spdm_test_context = *state;
     270            1 :     spdm_context = spdm_test_context->spdm_context;
     271            1 :     spdm_test_context->case_id = 0x4;
     272            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_14 <<
     273              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     274              : 
     275            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
     276            1 :     spdm_context->connection_info.capability.flags |=
     277              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP;
     278            1 :     spdm_context->connection_info.capability.flags |=
     279              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_RESET_CAP;
     280              : 
     281            1 :     key_pair_id = 1;
     282            1 :     operation = SPDM_SET_KEY_PAIR_INFO_ERASE_OPERATION;
     283            1 :     desired_key_usage = 0;
     284            1 :     desired_asym_algo = 0;
     285            1 :     desired_pqc_asym_algo = 0;
     286            1 :     desired_assoc_cert_slot_mask = 0;
     287            1 :     status = libspdm_set_key_pair_info(spdm_context, NULL, key_pair_id,
     288              :                                        operation, desired_key_usage, desired_asym_algo,
     289              :                                        desired_pqc_asym_algo, desired_assoc_cert_slot_mask);
     290              : 
     291            1 :     assert_int_equal(status, LIBSPDM_STATUS_RESET_REQUIRED_PEER);
     292            1 : }
     293              : 
     294            1 : int libspdm_req_set_key_pair_info_test(void)
     295              : {
     296            1 :     const struct CMUnitTest test_cases[] = {
     297              :         /* Successful response to set key pair info, key_pair_id is 1*/
     298              :         cmocka_unit_test(req_set_key_pair_info_case1),
     299              :         /* The response version is incorrect */
     300              :         cmocka_unit_test(req_set_key_pair_info_case2),
     301              :         /* The response code is incorrect */
     302              :         cmocka_unit_test(req_set_key_pair_info_case3),
     303              :         /* Successful response with reset required error code */
     304              :         cmocka_unit_test(req_set_key_pair_info_case4),
     305              :     };
     306              : 
     307            1 :     libspdm_test_context_t test_context = {
     308              :         LIBSPDM_TEST_CONTEXT_VERSION,
     309              :         true,
     310              :         send_message,
     311              :         receive_message,
     312              :     };
     313              : 
     314            1 :     libspdm_setup_test_context(&test_context);
     315              : 
     316            1 :     return cmocka_run_group_tests(test_cases,
     317              :                                   libspdm_unit_test_group_setup,
     318              :                                   libspdm_unit_test_group_teardown);
     319              : }
     320              : 
     321              : #endif /*LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP*/
        

Generated by: LCOV version 2.0-1