LCOV - code coverage report
Current view: top level - unit_test/test_spdm_responder - key_pair_info.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 79 79
Test Date: 2025-06-29 08:09:00 Functions: 100.0 % 5 5

            Line data    Source code
       1              : /**
       2              :  *  Copyright Notice:
       3              :  *  Copyright 2024 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_responder_lib.h"
       9              : #include "internal/libspdm_requester_lib.h"
      10              : 
      11              : #if LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP
      12              : 
      13              : spdm_get_key_pair_info_request_t m_libspdm_get_key_pair_info_request1 = {
      14              :     { SPDM_MESSAGE_VERSION_13, SPDM_GET_KEY_PAIR_INFO, 0, 0 },
      15              :     4
      16              : };
      17              : size_t m_libspdm_get_key_pair_info_request1_size = sizeof(m_libspdm_get_key_pair_info_request1);
      18              : 
      19              : /**
      20              :  * Test 1: Successful response to get key pair info with key pair id 4
      21              :  * Expected Behavior: get a LIBSPDM_STATUS_SUCCESS return code, and correct response message size and fields
      22              :  **/
      23            1 : void libspdm_test_responder_key_pair_info_case1(void **state)
      24              : {
      25              :     libspdm_return_t status;
      26              :     libspdm_test_context_t *spdm_test_context;
      27              :     libspdm_context_t *spdm_context;
      28              :     size_t response_size;
      29              :     uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
      30              :     spdm_key_pair_info_response_t *spdm_response;
      31              :     uint8_t key_pair_id;
      32              :     uint16_t public_key_info_len;
      33            1 :     uint8_t public_key_info_ecp256[] = {0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
      34              :                                         0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
      35              :                                         0x03, 0x01, 0x07};
      36              : 
      37            1 :     spdm_test_context = *state;
      38            1 :     spdm_context = spdm_test_context->spdm_context;
      39            1 :     spdm_test_context->case_id = 0x1;
      40            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
      41              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
      42            1 :     spdm_context->connection_info.connection_state =
      43              :         LIBSPDM_CONNECTION_STATE_AUTHENTICATED;
      44            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
      45              :         m_libspdm_use_asym_algo;
      46            1 :     spdm_context->local_context.capability.flags |=
      47              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP;
      48            1 :     spdm_context->local_context.total_key_pairs = libspdm_read_total_key_pairs();
      49              : 
      50            1 :     key_pair_id = 4;
      51            1 :     public_key_info_len = sizeof(public_key_info_ecp256);
      52              : 
      53            1 :     response_size = sizeof(response);
      54              : 
      55            1 :     status = libspdm_get_response_key_pair_info(
      56              :         spdm_context, m_libspdm_get_key_pair_info_request1_size,
      57              :         &m_libspdm_get_key_pair_info_request1, &response_size, response);
      58            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
      59            1 :     assert_int_equal(response_size,
      60              :                      sizeof(spdm_key_pair_info_response_t) + public_key_info_len);
      61            1 :     spdm_response = (void *)response;
      62            1 :     assert_int_equal(spdm_response->header.request_response_code,
      63              :                      SPDM_KEY_PAIR_INFO);
      64            1 :     assert_int_equal(spdm_response->key_pair_id,
      65              :                      key_pair_id);
      66            1 : }
      67              : 
      68              : /**
      69              :  * Test 2:
      70              :  * An SPDM endpoint shall contain KeyPairID s starting from 1 to TotalKeyPairs inclusive and without gaps.
      71              :  * KeyPairID is set to 0.
      72              :  * Expected Behavior:  Generate error response message
      73              :  **/
      74            1 : void libspdm_test_responder_key_pair_info_case2(void **state)
      75              : {
      76              :     libspdm_return_t status;
      77              :     libspdm_test_context_t *spdm_test_context;
      78              :     libspdm_context_t *spdm_context;
      79              :     size_t response_size;
      80              :     uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
      81              :     spdm_key_pair_info_response_t *spdm_response;
      82              :     uint8_t key_pair_id;
      83              : 
      84            1 :     spdm_test_context = *state;
      85            1 :     spdm_context = spdm_test_context->spdm_context;
      86            1 :     spdm_test_context->case_id = 0x2;
      87            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
      88              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
      89            1 :     spdm_context->connection_info.connection_state =
      90              :         LIBSPDM_CONNECTION_STATE_AUTHENTICATED;
      91            1 :     spdm_context->local_context.capability.flags |=
      92              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP;
      93              : 
      94              :     /* An SPDM endpoint shall contain KeyPairID s starting from 1 to TotalKeyPairs inclusive and without gaps.
      95              :      * KeyPairID is set to 0.*/
      96            1 :     key_pair_id = 0;
      97            1 :     m_libspdm_get_key_pair_info_request1.key_pair_id = key_pair_id;
      98              : 
      99            1 :     spdm_context->local_context.total_key_pairs = libspdm_read_total_key_pairs();
     100              : 
     101            1 :     response_size = sizeof(response);
     102              : 
     103            1 :     status = libspdm_get_response_key_pair_info(
     104              :         spdm_context, m_libspdm_get_key_pair_info_request1_size,
     105              :         &m_libspdm_get_key_pair_info_request1, &response_size, response);
     106            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     107            1 :     spdm_response = (void *)response;
     108            1 :     assert_int_equal(spdm_response->header.request_response_code,
     109              :                      SPDM_ERROR);
     110            1 :     assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_INVALID_REQUEST);
     111            1 :     assert_int_equal(spdm_response->header.param2, 0);
     112            1 : }
     113              : 
     114              : /**
     115              :  * Test 3: The key_pair_id is greater than the total key pairs
     116              :  * Expected Behavior:  Generate error response message
     117              :  **/
     118            1 : void libspdm_test_responder_key_pair_info_case3(void **state)
     119              : {
     120              :     libspdm_return_t status;
     121              :     libspdm_test_context_t *spdm_test_context;
     122              :     libspdm_context_t *spdm_context;
     123              :     size_t response_size;
     124              :     uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
     125              :     spdm_key_pair_info_response_t *spdm_response;
     126              :     uint8_t key_pair_id;
     127              : 
     128            1 :     spdm_test_context = *state;
     129            1 :     spdm_context = spdm_test_context->spdm_context;
     130            1 :     spdm_test_context->case_id = 0x3;
     131            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
     132              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     133            1 :     spdm_context->connection_info.connection_state =
     134              :         LIBSPDM_CONNECTION_STATE_AUTHENTICATED;
     135            1 :     spdm_context->local_context.capability.flags |=
     136              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP;
     137              : 
     138              :     /* key_pair_id > total_key_pairs*/
     139            1 :     key_pair_id = libspdm_read_total_key_pairs() + 1;
     140            1 :     m_libspdm_get_key_pair_info_request1.key_pair_id = key_pair_id;
     141            1 :     spdm_context->local_context.total_key_pairs = libspdm_read_total_key_pairs();
     142              : 
     143            1 :     response_size = sizeof(response);
     144              : 
     145            1 :     status = libspdm_get_response_key_pair_info(
     146              :         spdm_context, m_libspdm_get_key_pair_info_request1_size,
     147              :         &m_libspdm_get_key_pair_info_request1, &response_size, response);
     148            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     149            1 :     spdm_response = (void *)response;
     150            1 :     assert_int_equal(spdm_response->header.request_response_code,
     151              :                      SPDM_ERROR);
     152            1 :     assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_INVALID_REQUEST);
     153            1 :     assert_int_equal(spdm_response->header.param2, 0);
     154            1 : }
     155              : 
     156              : /**
     157              :  * Test 4: not set KEY_PAIR_INFO
     158              :  * Expected Behavior:  Generate error response message
     159              :  **/
     160            1 : void libspdm_test_responder_key_pair_info_case4(void **state)
     161              : {
     162              :     libspdm_return_t status;
     163              :     libspdm_test_context_t *spdm_test_context;
     164              :     libspdm_context_t *spdm_context;
     165              :     size_t response_size;
     166              :     uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
     167              :     spdm_key_pair_info_response_t *spdm_response;
     168              :     uint8_t key_pair_id;
     169              : 
     170            1 :     spdm_test_context = *state;
     171            1 :     spdm_context = spdm_test_context->spdm_context;
     172            1 :     spdm_test_context->case_id = 0x4;
     173            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
     174              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     175            1 :     spdm_context->connection_info.connection_state =
     176              :         LIBSPDM_CONNECTION_STATE_AUTHENTICATED;
     177              :     /* not set KEY_PAIR_INFO*/
     178            1 :     spdm_context->local_context.capability.flags &=
     179              :         ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP;
     180              : 
     181            1 :     key_pair_id = 1;
     182            1 :     m_libspdm_get_key_pair_info_request1.key_pair_id = key_pair_id;
     183              : 
     184            1 :     spdm_context->local_context.total_key_pairs = libspdm_read_total_key_pairs();
     185              : 
     186            1 :     response_size = sizeof(response);
     187              : 
     188            1 :     status = libspdm_get_response_key_pair_info(
     189              :         spdm_context, m_libspdm_get_key_pair_info_request1_size,
     190              :         &m_libspdm_get_key_pair_info_request1, &response_size, response);
     191            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     192            1 :     spdm_response = (void *)response;
     193            1 :     assert_int_equal(spdm_response->header.request_response_code,
     194              :                      SPDM_ERROR);
     195            1 :     assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_UNSUPPORTED_REQUEST);
     196            1 :     assert_int_equal(spdm_response->header.param2, SPDM_GET_KEY_PAIR_INFO);
     197            1 : }
     198              : 
     199            1 : int libspdm_responder_key_pair_info_test_main(void)
     200              : {
     201            1 :     const struct CMUnitTest spdm_responder_key_pair_info_tests[] = {
     202              :         /* Success Case to get key pair info*/
     203              :         cmocka_unit_test(libspdm_test_responder_key_pair_info_case1),
     204              :         /* The KeyPairID is at least 1 , KeyPairID is set to 0.*/
     205              :         cmocka_unit_test(libspdm_test_responder_key_pair_info_case2),
     206              :         /* KeyPairID > total_key_pairs*/
     207              :         cmocka_unit_test(libspdm_test_responder_key_pair_info_case3),
     208              :         /* capability not set KEY_PAIR_INFO*/
     209              :         cmocka_unit_test(libspdm_test_responder_key_pair_info_case4),
     210              :     };
     211              : 
     212            1 :     libspdm_test_context_t test_context = {
     213              :         LIBSPDM_TEST_CONTEXT_VERSION,
     214              :         false,
     215              :     };
     216            1 :     libspdm_setup_test_context(&test_context);
     217              : 
     218            1 :     return cmocka_run_group_tests(spdm_responder_key_pair_info_tests,
     219              :                                   libspdm_unit_test_group_setup,
     220              :                                   libspdm_unit_test_group_teardown);
     221              : }
     222              : 
     223              : #endif /* LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP*/
        

Generated by: LCOV version 2.0-1