LCOV - code coverage report
Current view: top level - unit_test/test_spdm_requester - heartbeat.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 95.2 % 799 761
Test Date: 2025-06-29 08:09:00 Functions: 100.0 % 18 18

            Line data    Source code
       1              : /**
       2              :  *  Copyright Notice:
       3              :  *  Copyright 2021-2022 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_KEY_EX_CAP) || (LIBSPDM_ENABLE_CAPABILITY_PSK_CAP)
      12              : 
      13              : static uint8_t m_libspdm_dummy_key_buffer[LIBSPDM_MAX_AEAD_KEY_SIZE];
      14              : static uint8_t m_libspdm_dummy_salt_buffer[LIBSPDM_MAX_AEAD_IV_SIZE];
      15              : 
      16           29 : void libspdm_secured_message_set_response_data_encryption_key(
      17              :     void *spdm_secured_message_context, const void *key, size_t key_size)
      18              : {
      19              :     libspdm_secured_message_context_t *secured_message_context;
      20              : 
      21           29 :     secured_message_context = spdm_secured_message_context;
      22           29 :     LIBSPDM_ASSERT(key_size == secured_message_context->aead_key_size);
      23           29 :     libspdm_copy_mem(secured_message_context->application_secret.response_data_encryption_key,
      24              :                      sizeof(secured_message_context->application_secret.response_data_encryption_key),
      25              :                      key, secured_message_context->aead_key_size);
      26           29 : }
      27              : 
      28           29 : void libspdm_secured_message_set_response_data_salt(
      29              :     void *spdm_secured_message_context, const void *salt,
      30              :     size_t salt_size)
      31              : {
      32              :     libspdm_secured_message_context_t *secured_message_context;
      33              : 
      34           29 :     secured_message_context = spdm_secured_message_context;
      35           29 :     LIBSPDM_ASSERT(salt_size == secured_message_context->aead_iv_size);
      36           29 :     libspdm_copy_mem(secured_message_context->application_secret.response_data_salt,
      37              :                      sizeof(secured_message_context->application_secret.response_data_salt),
      38              :                      salt, secured_message_context->aead_iv_size);
      39           29 : }
      40              : 
      41           31 : libspdm_return_t libspdm_requester_heartbeat_test_send_message(void *spdm_context,
      42              :                                                                size_t request_size,
      43              :                                                                const void *request,
      44              :                                                                uint64_t timeout)
      45              : {
      46              :     libspdm_test_context_t *spdm_test_context;
      47              : 
      48           31 :     spdm_test_context = libspdm_get_test_context();
      49           31 :     switch (spdm_test_context->case_id) {
      50            1 :     case 0x1:
      51            1 :         return LIBSPDM_STATUS_SEND_FAIL;
      52            1 :     case 0x2:
      53            1 :         return LIBSPDM_STATUS_SUCCESS;
      54            0 :     case 0x3:
      55            0 :         return LIBSPDM_STATUS_SUCCESS;
      56            1 :     case 0x4:
      57            1 :         return LIBSPDM_STATUS_SUCCESS;
      58            1 :     case 0x5:
      59            1 :         return LIBSPDM_STATUS_SUCCESS;
      60            2 :     case 0x6:
      61            2 :         return LIBSPDM_STATUS_SUCCESS;
      62            1 :     case 0x7:
      63            1 :         return LIBSPDM_STATUS_SUCCESS;
      64            2 :     case 0x8:
      65            2 :         return LIBSPDM_STATUS_SUCCESS;
      66            2 :     case 0x9:
      67            2 :         return LIBSPDM_STATUS_SUCCESS;
      68           18 :     case 0xA:
      69           18 :         return LIBSPDM_STATUS_SUCCESS;
      70            1 :     case 0xB:
      71            1 :         return LIBSPDM_STATUS_SUCCESS;
      72            1 :     case 0xC:
      73            1 :         return LIBSPDM_STATUS_SUCCESS;
      74            0 :     default:
      75            0 :         return LIBSPDM_STATUS_SEND_FAIL;
      76              :     }
      77              : }
      78              : 
      79           30 : libspdm_return_t libspdm_requester_heartbeat_test_receive_message(
      80              :     void *spdm_context, size_t *response_size,
      81              :     void **response, uint64_t timeout)
      82              : {
      83              :     libspdm_test_context_t *spdm_test_context;
      84              : 
      85           30 :     spdm_test_context = libspdm_get_test_context();
      86           30 :     switch (spdm_test_context->case_id) {
      87            0 :     case 0x1:
      88            0 :         return LIBSPDM_STATUS_RECEIVE_FAIL;
      89              : 
      90            1 :     case 0x2: {
      91              :         spdm_heartbeat_response_t *spdm_response;
      92              :         size_t spdm_response_size;
      93              :         size_t transport_header_size;
      94              :         uint32_t session_id;
      95              :         libspdm_session_info_t *session_info;
      96              :         uint8_t *scratch_buffer;
      97              :         size_t scratch_buffer_size;
      98              : 
      99            1 :         session_id = 0xFFFFFFFF;
     100            1 :         spdm_response_size = sizeof(spdm_heartbeat_response_t);
     101            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     102            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     103              : 
     104            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     105            1 :         spdm_response->header.request_response_code =
     106              :             SPDM_HEARTBEAT_ACK;
     107            1 :         spdm_response->header.param1 = 0;
     108            1 :         spdm_response->header.param2 = 0;
     109              : 
     110              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     111              :          * transport_message is always in sender buffer. */
     112            1 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
     113            1 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
     114              :                           scratch_buffer_size - transport_header_size,
     115              :                           spdm_response, spdm_response_size);
     116            1 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
     117            1 :         libspdm_transport_test_encode_message(spdm_context, &session_id,
     118              :                                               false, false, spdm_response_size,
     119              :                                               spdm_response, response_size,
     120              :                                               response);
     121            1 :         session_info = libspdm_get_session_info_via_session_id(
     122              :             spdm_context, session_id);
     123            1 :         if (session_info == NULL) {
     124            0 :             return LIBSPDM_STATUS_RECEIVE_FAIL;
     125              :         }
     126              :         /* WALKAROUND: If just use single context to encode message and then decode message */
     127              :         ((libspdm_secured_message_context_t
     128            1 :           *)(session_info->secured_message_context))
     129            1 :         ->application_secret.response_data_sequence_number--;
     130              :     }
     131            1 :         return LIBSPDM_STATUS_SUCCESS;
     132              : 
     133            0 :     case 0x3: {
     134              :         spdm_heartbeat_response_t *spdm_response;
     135              :         size_t spdm_response_size;
     136              :         size_t transport_header_size;
     137              :         uint32_t session_id;
     138              :         libspdm_session_info_t *session_info;
     139              :         uint8_t *scratch_buffer;
     140              :         size_t scratch_buffer_size;
     141              : 
     142            0 :         session_id = 0xFFFFFFFF;
     143            0 :         spdm_response_size = sizeof(spdm_heartbeat_response_t);
     144            0 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     145            0 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     146              : 
     147            0 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     148            0 :         spdm_response->header.request_response_code =
     149              :             SPDM_HEARTBEAT_ACK;
     150            0 :         spdm_response->header.param1 = 0;
     151            0 :         spdm_response->header.param2 = 0;
     152              : 
     153              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     154              :          * transport_message is always in sender buffer. */
     155            0 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
     156            0 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
     157              :                           scratch_buffer_size - transport_header_size,
     158              :                           spdm_response, spdm_response_size);
     159            0 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
     160            0 :         libspdm_transport_test_encode_message(spdm_context, &session_id,
     161              :                                               false, false, spdm_response_size,
     162              :                                               spdm_response, response_size,
     163              :                                               response);
     164            0 :         session_info = libspdm_get_session_info_via_session_id(
     165              :             spdm_context, session_id);
     166            0 :         if (session_info == NULL) {
     167            0 :             return LIBSPDM_STATUS_RECEIVE_FAIL;
     168              :         }
     169              :         ((libspdm_secured_message_context_t
     170            0 :           *)(session_info->secured_message_context))
     171            0 :         ->application_secret.response_data_sequence_number--;
     172              :     }
     173            0 :         return LIBSPDM_STATUS_SUCCESS;
     174              : 
     175            1 :     case 0x4: {
     176              :         spdm_error_response_t *spdm_response;
     177              :         size_t spdm_response_size;
     178              :         size_t transport_header_size;
     179              :         uint32_t session_id;
     180              :         libspdm_session_info_t *session_info;
     181              :         uint8_t *scratch_buffer;
     182              :         size_t scratch_buffer_size;
     183              : 
     184            1 :         spdm_response_size = sizeof(spdm_error_response_t);
     185            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     186            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     187              : 
     188            1 :         session_id = 0xFFFFFFFF;
     189            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     190            1 :         spdm_response->header.request_response_code = SPDM_ERROR;
     191            1 :         spdm_response->header.param1 = SPDM_ERROR_CODE_INVALID_REQUEST;
     192            1 :         spdm_response->header.param2 = 0;
     193              : 
     194              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     195              :          * transport_message is always in sender buffer. */
     196            1 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
     197            1 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
     198              :                           scratch_buffer_size - transport_header_size,
     199              :                           spdm_response, spdm_response_size);
     200            1 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
     201            1 :         libspdm_transport_test_encode_message(spdm_context, &session_id,
     202              :                                               false, false,
     203              :                                               spdm_response_size,
     204              :                                               spdm_response,
     205              :                                               response_size, response);
     206            1 :         session_info = libspdm_get_session_info_via_session_id(
     207              :             spdm_context, session_id);
     208            1 :         if (session_info == NULL) {
     209            0 :             return LIBSPDM_STATUS_RECEIVE_FAIL;
     210              :         }
     211              :         ((libspdm_secured_message_context_t
     212            1 :           *)(session_info->secured_message_context))
     213            1 :         ->application_secret.response_data_sequence_number--;
     214              :     }
     215            1 :         return LIBSPDM_STATUS_SUCCESS;
     216              : 
     217            1 :     case 0x5: {
     218              :         spdm_error_response_t *spdm_response;
     219              :         size_t spdm_response_size;
     220              :         size_t transport_header_size;
     221              :         uint32_t session_id;
     222              :         libspdm_session_info_t *session_info;
     223              :         uint8_t *scratch_buffer;
     224              :         size_t scratch_buffer_size;
     225              : 
     226            1 :         spdm_response_size = sizeof(spdm_error_response_t);
     227            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     228            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     229              : 
     230            1 :         session_id = 0xFFFFFFFF;
     231            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     232            1 :         spdm_response->header.request_response_code = SPDM_ERROR;
     233            1 :         spdm_response->header.param1 = SPDM_ERROR_CODE_BUSY;
     234            1 :         spdm_response->header.param2 = 0;
     235              : 
     236              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     237              :          * transport_message is always in sender buffer. */
     238            1 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
     239            1 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
     240              :                           scratch_buffer_size - transport_header_size,
     241              :                           spdm_response, spdm_response_size);
     242            1 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
     243            1 :         libspdm_transport_test_encode_message(spdm_context, &session_id,
     244              :                                               false, false,
     245              :                                               spdm_response_size,
     246              :                                               spdm_response,
     247              :                                               response_size, response);
     248            1 :         session_info = libspdm_get_session_info_via_session_id(
     249              :             spdm_context, session_id);
     250            1 :         if (session_info == NULL) {
     251            0 :             return LIBSPDM_STATUS_RECEIVE_FAIL;
     252              :         }
     253              :         ((libspdm_secured_message_context_t
     254            1 :           *)(session_info->secured_message_context))
     255            1 :         ->application_secret.response_data_sequence_number--;
     256              :     }
     257            1 :         return LIBSPDM_STATUS_SUCCESS;
     258              : 
     259            2 :     case 0x6: {
     260              :         static size_t sub_index1 = 0;
     261            2 :         if (sub_index1 == 0) {
     262              :             spdm_error_response_t *spdm_response;
     263              :             size_t spdm_response_size;
     264              :             size_t transport_header_size;
     265              :             uint32_t session_id;
     266              :             libspdm_session_info_t *session_info;
     267              :             uint8_t *scratch_buffer;
     268              :             size_t scratch_buffer_size;
     269              : 
     270            1 :             spdm_response_size = sizeof(spdm_error_response_t);
     271            1 :             transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     272            1 :             spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     273              : 
     274            1 :             session_id = 0xFFFFFFFF;
     275            1 :             spdm_response->header.spdm_version =
     276              :                 SPDM_MESSAGE_VERSION_11;
     277            1 :             spdm_response->header.request_response_code = SPDM_ERROR;
     278            1 :             spdm_response->header.param1 = SPDM_ERROR_CODE_BUSY;
     279            1 :             spdm_response->header.param2 = 0;
     280              : 
     281              :             /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     282              :              * transport_message is always in sender buffer. */
     283            1 :             libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer,
     284              :                                         &scratch_buffer_size);
     285            1 :             libspdm_copy_mem (scratch_buffer + transport_header_size,
     286              :                               scratch_buffer_size - transport_header_size,
     287              :                               spdm_response, spdm_response_size);
     288            1 :             spdm_response = (void *)(scratch_buffer + transport_header_size);
     289            1 :             libspdm_transport_test_encode_message(
     290              :                 spdm_context, &session_id, false, false,
     291              :                 spdm_response_size, spdm_response,
     292              :                 response_size, response);
     293            1 :             sub_index1++;
     294            1 :             session_info = libspdm_get_session_info_via_session_id(
     295              :                 spdm_context, session_id);
     296            1 :             if (session_info == NULL) {
     297            0 :                 return LIBSPDM_STATUS_RECEIVE_FAIL;
     298              :             }
     299              :             ((libspdm_secured_message_context_t
     300            1 :               *)(session_info->secured_message_context))
     301              :             ->application_secret
     302            1 :             .response_data_sequence_number--;
     303            1 :         } else if (sub_index1 == 1) {
     304              :             spdm_heartbeat_response_t *spdm_response;
     305              :             size_t spdm_response_size;
     306              :             size_t transport_header_size;
     307              :             uint32_t session_id;
     308              :             libspdm_session_info_t *session_info;
     309              :             uint8_t *scratch_buffer;
     310              :             size_t scratch_buffer_size;
     311              : 
     312            1 :             session_id = 0xFFFFFFFF;
     313            1 :             spdm_response_size = sizeof(spdm_heartbeat_response_t);
     314            1 :             transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     315            1 :             spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     316              : 
     317            1 :             spdm_response->header.spdm_version =
     318              :                 SPDM_MESSAGE_VERSION_11;
     319            1 :             spdm_response->header.request_response_code =
     320              :                 SPDM_HEARTBEAT_ACK;
     321            1 :             spdm_response->header.param1 = 0;
     322            1 :             spdm_response->header.param2 = 0;
     323              : 
     324              :             /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     325              :              * transport_message is always in sender buffer. */
     326            1 :             libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer,
     327              :                                         &scratch_buffer_size);
     328            1 :             libspdm_copy_mem (scratch_buffer + transport_header_size,
     329              :                               scratch_buffer_size - transport_header_size,
     330              :                               spdm_response, spdm_response_size);
     331            1 :             spdm_response = (void *)(scratch_buffer + transport_header_size);
     332            1 :             libspdm_transport_test_encode_message(
     333              :                 spdm_context, &session_id, false, false,
     334              :                 spdm_response_size, spdm_response, response_size,
     335              :                 response);
     336            1 :             session_info = libspdm_get_session_info_via_session_id(
     337              :                 spdm_context, session_id);
     338            1 :             if (session_info == NULL) {
     339            0 :                 return LIBSPDM_STATUS_RECEIVE_FAIL;
     340              :             }
     341              :             ((libspdm_secured_message_context_t
     342            1 :               *)(session_info->secured_message_context))
     343              :             ->application_secret
     344            1 :             .response_data_sequence_number--;
     345              :         }
     346              :     }
     347            2 :         return LIBSPDM_STATUS_SUCCESS;
     348              : 
     349            1 :     case 0x7: {
     350              :         spdm_error_response_t *spdm_response;
     351              :         size_t spdm_response_size;
     352              :         size_t transport_header_size;
     353              :         uint32_t session_id;
     354              :         libspdm_session_info_t *session_info;
     355              :         uint8_t *scratch_buffer;
     356              :         size_t scratch_buffer_size;
     357              : 
     358            1 :         spdm_response_size = sizeof(spdm_error_response_t);
     359            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     360            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     361              : 
     362            1 :         session_id = 0xFFFFFFFF;
     363            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     364            1 :         spdm_response->header.request_response_code = SPDM_ERROR;
     365            1 :         spdm_response->header.param1 = SPDM_ERROR_CODE_REQUEST_RESYNCH;
     366            1 :         spdm_response->header.param2 = 0;
     367              : 
     368              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     369              :          * transport_message is always in sender buffer. */
     370            1 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
     371            1 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
     372              :                           scratch_buffer_size - transport_header_size,
     373              :                           spdm_response, spdm_response_size);
     374            1 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
     375            1 :         libspdm_transport_test_encode_message(spdm_context, &session_id,
     376              :                                               false, false,
     377              :                                               spdm_response_size,
     378              :                                               spdm_response,
     379              :                                               response_size, response);
     380            1 :         session_info = libspdm_get_session_info_via_session_id(
     381              :             spdm_context, session_id);
     382            1 :         if (session_info == NULL) {
     383            0 :             return LIBSPDM_STATUS_RECEIVE_FAIL;
     384              :         }
     385              :         ((libspdm_secured_message_context_t
     386            1 :           *)(session_info->secured_message_context))
     387            1 :         ->application_secret.response_data_sequence_number--;
     388              :     }
     389            1 :         return LIBSPDM_STATUS_SUCCESS;
     390              : 
     391            2 :     case 0x8: {
     392              :         spdm_error_response_data_response_not_ready_t *spdm_response;
     393              :         size_t spdm_response_size;
     394              :         size_t transport_header_size;
     395              :         uint32_t session_id;
     396              :         libspdm_session_info_t *session_info;
     397              :         uint8_t *scratch_buffer;
     398              :         size_t scratch_buffer_size;
     399              : 
     400            2 :         spdm_response_size = sizeof(spdm_error_response_data_response_not_ready_t);
     401            2 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     402            2 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     403              : 
     404            2 :         session_id = 0xFFFFFFFF;
     405            2 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     406            2 :         spdm_response->header.request_response_code = SPDM_ERROR;
     407            2 :         spdm_response->header.param1 =
     408              :             SPDM_ERROR_CODE_RESPONSE_NOT_READY;
     409            2 :         spdm_response->header.param2 = 0;
     410            2 :         spdm_response->extend_error_data.rd_exponent = 1;
     411            2 :         spdm_response->extend_error_data.rd_tm = 2;
     412            2 :         spdm_response->extend_error_data.request_code = SPDM_HEARTBEAT;
     413            2 :         spdm_response->extend_error_data.token = 0;
     414              : 
     415              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     416              :          * transport_message is always in sender buffer. */
     417            2 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
     418            2 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
     419              :                           scratch_buffer_size - transport_header_size,
     420              :                           spdm_response, spdm_response_size);
     421            2 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
     422            2 :         libspdm_transport_test_encode_message(spdm_context, &session_id,
     423              :                                               false, false,
     424              :                                               spdm_response_size,
     425              :                                               spdm_response,
     426              :                                               response_size, response);
     427            2 :         session_info = libspdm_get_session_info_via_session_id(
     428              :             spdm_context, session_id);
     429            2 :         if (session_info == NULL) {
     430            0 :             return LIBSPDM_STATUS_RECEIVE_FAIL;
     431              :         }
     432              :         ((libspdm_secured_message_context_t
     433            2 :           *)(session_info->secured_message_context))
     434            2 :         ->application_secret.response_data_sequence_number--;
     435              :     }
     436            2 :         return LIBSPDM_STATUS_SUCCESS;
     437              : 
     438            2 :     case 0x9: {
     439              :         static size_t sub_index2 = 0;
     440            2 :         if (sub_index2 == 0) {
     441              :             spdm_error_response_data_response_not_ready_t
     442              :             *spdm_response;
     443              :             size_t spdm_response_size;
     444              :             size_t transport_header_size;
     445              :             uint32_t session_id;
     446              :             libspdm_session_info_t *session_info;
     447              :             uint8_t *scratch_buffer;
     448              :             size_t scratch_buffer_size;
     449              : 
     450            1 :             spdm_response_size = sizeof(spdm_error_response_data_response_not_ready_t);
     451            1 :             transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     452            1 :             spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     453              : 
     454            1 :             session_id = 0xFFFFFFFF;
     455            1 :             spdm_response->header.spdm_version =
     456              :                 SPDM_MESSAGE_VERSION_11;
     457            1 :             spdm_response->header.request_response_code = SPDM_ERROR;
     458            1 :             spdm_response->header.param1 =
     459              :                 SPDM_ERROR_CODE_RESPONSE_NOT_READY;
     460            1 :             spdm_response->header.param2 = 0;
     461            1 :             spdm_response->extend_error_data.rd_exponent = 1;
     462            1 :             spdm_response->extend_error_data.rd_tm = 2;
     463            1 :             spdm_response->extend_error_data.request_code =
     464              :                 SPDM_HEARTBEAT;
     465            1 :             spdm_response->extend_error_data.token = 1;
     466              : 
     467              :             /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     468              :              * transport_message is always in sender buffer. */
     469            1 :             libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer,
     470              :                                         &scratch_buffer_size);
     471            1 :             libspdm_copy_mem (scratch_buffer + transport_header_size,
     472              :                               scratch_buffer_size - transport_header_size,
     473              :                               spdm_response, spdm_response_size);
     474            1 :             spdm_response = (void *)(scratch_buffer + transport_header_size);
     475            1 :             libspdm_transport_test_encode_message(
     476              :                 spdm_context, &session_id, false, false,
     477              :                 spdm_response_size, spdm_response,
     478              :                 response_size, response);
     479            1 :             sub_index2++;
     480            1 :             session_info = libspdm_get_session_info_via_session_id(
     481              :                 spdm_context, session_id);
     482            1 :             if (session_info == NULL) {
     483            0 :                 return LIBSPDM_STATUS_RECEIVE_FAIL;
     484              :             }
     485              :             ((libspdm_secured_message_context_t
     486            1 :               *)(session_info->secured_message_context))
     487              :             ->application_secret
     488            1 :             .response_data_sequence_number--;
     489            1 :         } else if (sub_index2 == 1) {
     490              :             spdm_heartbeat_response_t *spdm_response;
     491              :             size_t spdm_response_size;
     492              :             size_t transport_header_size;
     493              :             uint32_t session_id;
     494              :             libspdm_session_info_t *session_info;
     495              :             uint8_t *scratch_buffer;
     496              :             size_t scratch_buffer_size;
     497              : 
     498            1 :             session_id = 0xFFFFFFFF;
     499            1 :             spdm_response_size = sizeof(spdm_heartbeat_response_t);
     500            1 :             transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     501            1 :             spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     502              : 
     503            1 :             spdm_response->header.spdm_version =
     504              :                 SPDM_MESSAGE_VERSION_11;
     505            1 :             spdm_response->header.request_response_code =
     506              :                 SPDM_HEARTBEAT_ACK;
     507            1 :             spdm_response->header.param1 = 0;
     508            1 :             spdm_response->header.param2 = 0;
     509              : 
     510              :             /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     511              :              * transport_message is always in sender buffer. */
     512            1 :             libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer,
     513              :                                         &scratch_buffer_size);
     514            1 :             libspdm_copy_mem (scratch_buffer + transport_header_size,
     515              :                               scratch_buffer_size - transport_header_size,
     516              :                               spdm_response, spdm_response_size);
     517            1 :             spdm_response = (void *)(scratch_buffer + transport_header_size);
     518            1 :             libspdm_transport_test_encode_message(
     519              :                 spdm_context, &session_id, false, false,
     520              :                 spdm_response_size, spdm_response, response_size,
     521              :                 response);
     522            1 :             session_info = libspdm_get_session_info_via_session_id(
     523              :                 spdm_context, session_id);
     524            1 :             if (session_info == NULL) {
     525            0 :                 return LIBSPDM_STATUS_RECEIVE_FAIL;
     526              :             }
     527              :             ((libspdm_secured_message_context_t
     528            1 :               *)(session_info->secured_message_context))
     529              :             ->application_secret
     530            1 :             .response_data_sequence_number--;
     531              :         }
     532              :     }
     533            2 :         return LIBSPDM_STATUS_SUCCESS;
     534              : 
     535           18 :     case 0xA:
     536              :     {
     537              :         static uint16_t error_code = LIBSPDM_ERROR_CODE_RESERVED_00;
     538              : 
     539              :         spdm_error_response_t *spdm_response;
     540              :         size_t spdm_response_size;
     541              :         size_t transport_header_size;
     542              :         uint32_t session_id;
     543              :         libspdm_session_info_t      *session_info;
     544              :         uint8_t *scratch_buffer;
     545              :         size_t scratch_buffer_size;
     546              : 
     547           18 :         session_id = 0xFFFFFFFF;
     548              : 
     549           18 :         spdm_response_size = sizeof(spdm_error_response_t);
     550           18 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     551           18 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     552              : 
     553           18 :         if(error_code <= 0xff) {
     554           18 :             libspdm_zero_mem (spdm_response, spdm_response_size);
     555           18 :             spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     556           18 :             spdm_response->header.request_response_code = SPDM_ERROR;
     557           18 :             spdm_response->header.param1 = (uint8_t) error_code;
     558           18 :             spdm_response->header.param2 = 0;
     559              : 
     560              :             /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     561              :              * transport_message is always in sender buffer. */
     562           18 :             libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer,
     563              :                                         &scratch_buffer_size);
     564           18 :             libspdm_copy_mem (scratch_buffer + transport_header_size,
     565              :                               scratch_buffer_size - transport_header_size,
     566              :                               spdm_response, spdm_response_size);
     567           18 :             spdm_response = (void *)(scratch_buffer + transport_header_size);
     568           18 :             libspdm_transport_test_encode_message (spdm_context, &session_id, false, false,
     569              :                                                    spdm_response_size, spdm_response,
     570              :                                                    response_size, response);
     571           18 :             session_info = libspdm_get_session_info_via_session_id (spdm_context, session_id);
     572           18 :             ((libspdm_secured_message_context_t*)(session_info->secured_message_context))->
     573           18 :             application_secret.response_data_sequence_number--;
     574              :         }
     575              : 
     576           18 :         error_code++;
     577           18 :         if(error_code == SPDM_ERROR_CODE_BUSY) { /*busy is treated in cases 5 and 6*/
     578            1 :             error_code = SPDM_ERROR_CODE_UNEXPECTED_REQUEST;
     579              :         }
     580           18 :         if(error_code == LIBSPDM_ERROR_CODE_RESERVED_0D) { /*skip some reserved error codes (0d to 3e)*/
     581            1 :             error_code = LIBSPDM_ERROR_CODE_RESERVED_3F;
     582              :         }
     583           18 :         if(error_code == SPDM_ERROR_CODE_RESPONSE_NOT_READY) { /*skip response not ready, request resync, and some reserved codes (44 to fc)*/
     584            1 :             error_code = LIBSPDM_ERROR_CODE_RESERVED_FD;
     585              :         }
     586              :     }
     587           18 :         return LIBSPDM_STATUS_SUCCESS;
     588            1 :     case 0xB: {
     589              :         spdm_heartbeat_response_t *spdm_response;
     590              :         size_t spdm_response_size;
     591              :         size_t transport_header_size;
     592              :         uint32_t session_id;
     593              :         libspdm_session_info_t *session_info;
     594              :         uint8_t *scratch_buffer;
     595              :         size_t scratch_buffer_size;
     596              : 
     597            1 :         session_id = 0xFFFFFFFF;
     598            1 :         spdm_response_size = sizeof(spdm_heartbeat_response_t);
     599            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     600            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     601              : 
     602            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     603            1 :         spdm_response->header.request_response_code =
     604              :             SPDM_HEARTBEAT_ACK;
     605            1 :         spdm_response->header.param1 = 0;
     606            1 :         spdm_response->header.param2 = 0;
     607              : 
     608              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     609              :          * transport_message is always in sender buffer. */
     610            1 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
     611            1 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
     612              :                           scratch_buffer_size - transport_header_size,
     613              :                           spdm_response, spdm_response_size);
     614            1 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
     615            1 :         libspdm_transport_test_encode_message(spdm_context, &session_id,
     616              :                                               false, false, spdm_response_size,
     617              :                                               spdm_response, response_size,
     618              :                                               response);
     619            1 :         session_info = libspdm_get_session_info_via_session_id(
     620              :             spdm_context, session_id);
     621            1 :         if (session_info == NULL) {
     622            0 :             return LIBSPDM_STATUS_RECEIVE_FAIL;
     623              :         }
     624              :         /* WALKAROUND: If just use single context to encode message and then decode message */
     625              :         ((libspdm_secured_message_context_t
     626            1 :           *)(session_info->secured_message_context))
     627            1 :         ->application_secret.response_data_sequence_number--;
     628              :     }
     629            1 :         return LIBSPDM_STATUS_SUCCESS;
     630              : 
     631            1 :     case 0xC: {
     632              :         spdm_error_response_t *spdm_response;
     633              :         size_t spdm_response_size;
     634              :         size_t transport_header_size;
     635              :         uint32_t session_id;
     636              :         libspdm_session_info_t *session_info;
     637              :         uint8_t *scratch_buffer;
     638              :         size_t scratch_buffer_size;
     639              : 
     640            1 :         spdm_response_size = sizeof(spdm_error_response_t);
     641            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     642            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     643              : 
     644            1 :         session_id = 0xFFFFFFFF;
     645            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     646            1 :         spdm_response->header.request_response_code = SPDM_ERROR;
     647            1 :         spdm_response->header.param1 = SPDM_ERROR_CODE_DECRYPT_ERROR;
     648            1 :         spdm_response->header.param2 = 0;
     649              : 
     650              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     651              :          * transport_message is always in sender buffer. */
     652            1 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
     653            1 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
     654              :                           scratch_buffer_size - transport_header_size,
     655              :                           spdm_response, spdm_response_size);
     656            1 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
     657            1 :         libspdm_transport_test_encode_message(spdm_context, &session_id,
     658              :                                               false, false,
     659              :                                               spdm_response_size,
     660              :                                               spdm_response,
     661              :                                               response_size, response);
     662            1 :         session_info = libspdm_get_session_info_via_session_id(
     663              :             spdm_context, session_id);
     664            1 :         if (session_info == NULL) {
     665            0 :             return LIBSPDM_STATUS_RECEIVE_FAIL;
     666              :         }
     667              :         ((libspdm_secured_message_context_t
     668            1 :           *)(session_info->secured_message_context))
     669            1 :         ->application_secret.response_data_sequence_number--;
     670              :     }
     671            1 :         return LIBSPDM_STATUS_SUCCESS;
     672              : 
     673            0 :     default:
     674            0 :         return LIBSPDM_STATUS_RECEIVE_FAIL;
     675              :     }
     676              : }
     677              : 
     678            1 : void libspdm_test_requester_heartbeat_case1(void **state)
     679              : {
     680              :     libspdm_return_t status;
     681              :     libspdm_test_context_t *spdm_test_context;
     682              :     libspdm_context_t *spdm_context;
     683              :     uint32_t session_id;
     684              :     void *data;
     685              :     size_t data_size;
     686              :     void *hash;
     687              :     size_t hash_size;
     688              :     libspdm_session_info_t *session_info;
     689              : 
     690            1 :     spdm_test_context = *state;
     691            1 :     spdm_context = spdm_test_context->spdm_context;
     692            1 :     spdm_test_context->case_id = 0x1;
     693            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
     694              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     695            1 :     spdm_context->connection_info.connection_state =
     696              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
     697            1 :     spdm_context->connection_info.capability.flags |=
     698              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP;
     699            1 :     spdm_context->connection_info.capability.flags |=
     700              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
     701            1 :     spdm_context->connection_info.capability.flags |=
     702              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
     703            1 :     spdm_context->local_context.capability.flags |=
     704              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP;
     705            1 :     spdm_context->local_context.capability.flags |=
     706              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
     707            1 :     spdm_context->local_context.capability.flags |=
     708              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
     709            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     710              :                                                     m_libspdm_use_asym_algo, &data,
     711              :                                                     &data_size, &hash, &hash_size);
     712            1 :     libspdm_reset_message_a(spdm_context);
     713            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
     714              :         m_libspdm_use_hash_algo;
     715            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
     716              :         m_libspdm_use_asym_algo;
     717            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
     718              :         m_libspdm_use_dhe_algo;
     719            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
     720              :         m_libspdm_use_aead_algo;
     721              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
     722              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
     723              :         data_size;
     724              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
     725              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
     726              :                      data, data_size);
     727              : #endif
     728              : 
     729            1 :     session_id = 0xFFFFFFFF;
     730            1 :     session_info = &spdm_context->session_info[0];
     731            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, true);
     732            1 :     libspdm_secured_message_set_session_state(
     733              :         session_info->secured_message_context,
     734              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
     735            1 :     session_info->heartbeat_period = 1;
     736              : 
     737            1 :     status = libspdm_heartbeat(spdm_context, session_id);
     738            1 :     assert_int_equal(status, LIBSPDM_STATUS_SEND_FAIL);
     739            1 :     free(data);
     740            1 : }
     741              : 
     742            1 : void libspdm_test_requester_heartbeat_case2(void **state)
     743              : {
     744              :     libspdm_return_t status;
     745              :     libspdm_test_context_t *spdm_test_context;
     746              :     libspdm_context_t *spdm_context;
     747              :     uint32_t session_id;
     748              :     void *data;
     749              :     size_t data_size;
     750              :     void *hash;
     751              :     size_t hash_size;
     752              :     libspdm_session_info_t *session_info;
     753              : 
     754            1 :     spdm_test_context = *state;
     755            1 :     spdm_context = spdm_test_context->spdm_context;
     756            1 :     spdm_test_context->case_id = 0x2;
     757            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
     758              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     759            1 :     spdm_context->connection_info.connection_state =
     760              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
     761            1 :     spdm_context->connection_info.capability.flags |=
     762              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP;
     763            1 :     spdm_context->connection_info.capability.flags |=
     764              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
     765            1 :     spdm_context->connection_info.capability.flags |=
     766              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
     767            1 :     spdm_context->local_context.capability.flags |=
     768              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP;
     769            1 :     spdm_context->local_context.capability.flags |=
     770              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
     771            1 :     spdm_context->local_context.capability.flags |=
     772              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
     773            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     774              :                                                     m_libspdm_use_asym_algo, &data,
     775              :                                                     &data_size, &hash, &hash_size);
     776            1 :     libspdm_reset_message_a(spdm_context);
     777            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
     778              :         m_libspdm_use_hash_algo;
     779            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
     780              :         m_libspdm_use_asym_algo;
     781            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
     782              :         m_libspdm_use_dhe_algo;
     783            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
     784              :         m_libspdm_use_aead_algo;
     785              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
     786              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
     787              :         data_size;
     788              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
     789              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
     790              :                      data, data_size);
     791              : #endif
     792              : 
     793            1 :     session_id = 0xFFFFFFFF;
     794            1 :     session_info = &spdm_context->session_info[0];
     795            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, true);
     796            1 :     libspdm_secured_message_set_session_state(
     797              :         session_info->secured_message_context,
     798              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
     799            1 :     session_info->heartbeat_period = 1;
     800            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
     801              :                     ((libspdm_secured_message_context_t
     802            1 :                       *)(session_info->secured_message_context))
     803              :                     ->aead_key_size,
     804              :                     (uint8_t)(0xFF));
     805            1 :     libspdm_secured_message_set_response_data_encryption_key(
     806              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
     807              :         ((libspdm_secured_message_context_t
     808            1 :           *)(session_info->secured_message_context))
     809              :         ->aead_key_size);
     810            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
     811              :                     ((libspdm_secured_message_context_t
     812            1 :                       *)(session_info->secured_message_context))
     813              :                     ->aead_iv_size,
     814              :                     (uint8_t)(0xFF));
     815            1 :     libspdm_secured_message_set_response_data_salt(
     816              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
     817              :         ((libspdm_secured_message_context_t
     818            1 :           *)(session_info->secured_message_context))
     819              :         ->aead_iv_size);
     820              :     ((libspdm_secured_message_context_t *)(session_info
     821            1 :                                            ->secured_message_context))
     822            1 :     ->application_secret.response_data_sequence_number = 0;
     823              : 
     824            1 :     status = libspdm_heartbeat(spdm_context, session_id);
     825            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     826            1 :     free(data);
     827            1 : }
     828              : 
     829            1 : void libspdm_test_requester_heartbeat_case3(void **state)
     830              : {
     831              :     libspdm_return_t status;
     832              :     libspdm_test_context_t *spdm_test_context;
     833              :     libspdm_context_t *spdm_context;
     834              :     uint32_t session_id;
     835              :     void *data;
     836              :     size_t data_size;
     837              :     void *hash;
     838              :     size_t hash_size;
     839              :     libspdm_session_info_t *session_info;
     840              : 
     841            1 :     spdm_test_context = *state;
     842            1 :     spdm_context = spdm_test_context->spdm_context;
     843            1 :     spdm_test_context->case_id = 0x3;
     844            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
     845              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     846            1 :     spdm_context->connection_info.connection_state =
     847              :         LIBSPDM_CONNECTION_STATE_NOT_STARTED;
     848            1 :     spdm_context->connection_info.capability.flags |=
     849              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP;
     850            1 :     spdm_context->connection_info.capability.flags |=
     851              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
     852            1 :     spdm_context->connection_info.capability.flags |=
     853              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
     854            1 :     spdm_context->local_context.capability.flags |=
     855              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP;
     856            1 :     spdm_context->local_context.capability.flags |=
     857              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
     858            1 :     spdm_context->local_context.capability.flags |=
     859              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
     860            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     861              :                                                     m_libspdm_use_asym_algo, &data,
     862              :                                                     &data_size, &hash, &hash_size);
     863            1 :     libspdm_reset_message_a(spdm_context);
     864            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
     865              :         m_libspdm_use_hash_algo;
     866            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
     867              :         m_libspdm_use_asym_algo;
     868            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
     869              :         m_libspdm_use_dhe_algo;
     870            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
     871              :         m_libspdm_use_aead_algo;
     872              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
     873              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
     874              :         data_size;
     875              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
     876              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
     877              :                      data, data_size);
     878              : #endif
     879              : 
     880            1 :     session_id = 0xFFFFFFFF;
     881            1 :     session_info = &spdm_context->session_info[0];
     882            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, true);
     883            1 :     libspdm_secured_message_set_session_state(
     884              :         session_info->secured_message_context,
     885              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
     886            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
     887              :                     ((libspdm_secured_message_context_t
     888            1 :                       *)(session_info->secured_message_context))
     889              :                     ->aead_key_size,
     890              :                     (uint8_t)(0xFF));
     891            1 :     libspdm_secured_message_set_response_data_encryption_key(
     892              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
     893              :         ((libspdm_secured_message_context_t
     894            1 :           *)(session_info->secured_message_context))
     895              :         ->aead_key_size);
     896            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
     897              :                     ((libspdm_secured_message_context_t
     898            1 :                       *)(session_info->secured_message_context))
     899              :                     ->aead_iv_size,
     900              :                     (uint8_t)(0xFF));
     901            1 :     libspdm_secured_message_set_response_data_salt(
     902              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
     903              :         ((libspdm_secured_message_context_t
     904            1 :           *)(session_info->secured_message_context))
     905              :         ->aead_iv_size);
     906              :     ((libspdm_secured_message_context_t *)(session_info
     907            1 :                                            ->secured_message_context))
     908            1 :     ->application_secret.response_data_sequence_number = 0;
     909              : 
     910            1 :     status = libspdm_heartbeat(spdm_context, session_id);
     911            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_STATE_LOCAL);
     912            1 :     free(data);
     913            1 : }
     914              : 
     915            1 : void libspdm_test_requester_heartbeat_case4(void **state)
     916              : {
     917              :     libspdm_return_t status;
     918              :     libspdm_test_context_t *spdm_test_context;
     919              :     libspdm_context_t *spdm_context;
     920              :     uint32_t session_id;
     921              :     void *data;
     922              :     size_t data_size;
     923              :     void *hash;
     924              :     size_t hash_size;
     925              :     libspdm_session_info_t *session_info;
     926              : 
     927            1 :     spdm_test_context = *state;
     928            1 :     spdm_context = spdm_test_context->spdm_context;
     929            1 :     spdm_test_context->case_id = 0x4;
     930            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
     931              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     932            1 :     spdm_context->connection_info.connection_state =
     933              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
     934            1 :     spdm_context->connection_info.capability.flags |=
     935              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP;
     936            1 :     spdm_context->connection_info.capability.flags |=
     937              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
     938            1 :     spdm_context->connection_info.capability.flags |=
     939              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
     940            1 :     spdm_context->local_context.capability.flags |=
     941              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP;
     942            1 :     spdm_context->local_context.capability.flags |=
     943              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
     944            1 :     spdm_context->local_context.capability.flags |=
     945              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
     946            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     947              :                                                     m_libspdm_use_asym_algo, &data,
     948              :                                                     &data_size, &hash, &hash_size);
     949            1 :     libspdm_reset_message_a(spdm_context);
     950            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
     951              :         m_libspdm_use_hash_algo;
     952            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
     953              :         m_libspdm_use_asym_algo;
     954            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
     955              :         m_libspdm_use_dhe_algo;
     956            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
     957              :         m_libspdm_use_aead_algo;
     958              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
     959              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
     960              :         data_size;
     961              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
     962              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
     963              :                      data, data_size);
     964              : #endif
     965              : 
     966            1 :     session_id = 0xFFFFFFFF;
     967            1 :     session_info = &spdm_context->session_info[0];
     968            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, true);
     969            1 :     libspdm_secured_message_set_session_state(
     970              :         session_info->secured_message_context,
     971              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
     972            1 :     session_info->heartbeat_period = 1;
     973            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
     974              :                     ((libspdm_secured_message_context_t
     975            1 :                       *)(session_info->secured_message_context))
     976              :                     ->aead_key_size,
     977              :                     (uint8_t)(0xFF));
     978            1 :     libspdm_secured_message_set_response_data_encryption_key(
     979              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
     980              :         ((libspdm_secured_message_context_t
     981            1 :           *)(session_info->secured_message_context))
     982              :         ->aead_key_size);
     983            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
     984              :                     ((libspdm_secured_message_context_t
     985            1 :                       *)(session_info->secured_message_context))
     986              :                     ->aead_iv_size,
     987              :                     (uint8_t)(0xFF));
     988            1 :     libspdm_secured_message_set_response_data_salt(
     989              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
     990              :         ((libspdm_secured_message_context_t
     991            1 :           *)(session_info->secured_message_context))
     992              :         ->aead_iv_size);
     993              :     ((libspdm_secured_message_context_t *)(session_info
     994            1 :                                            ->secured_message_context))
     995            1 :     ->application_secret.response_data_sequence_number = 0;
     996              : 
     997            1 :     status = libspdm_heartbeat(spdm_context, session_id);
     998            1 :     assert_int_equal(status, LIBSPDM_STATUS_ERROR_PEER);
     999            1 :     free(data);
    1000            1 : }
    1001              : 
    1002            1 : void libspdm_test_requester_heartbeat_case5(void **state)
    1003              : {
    1004              :     libspdm_return_t status;
    1005              :     libspdm_test_context_t *spdm_test_context;
    1006              :     libspdm_context_t *spdm_context;
    1007              :     uint32_t session_id;
    1008              :     void *data;
    1009              :     size_t data_size;
    1010              :     void *hash;
    1011              :     size_t hash_size;
    1012              :     libspdm_session_info_t *session_info;
    1013              : 
    1014            1 :     spdm_test_context = *state;
    1015            1 :     spdm_context = spdm_test_context->spdm_context;
    1016            1 :     spdm_test_context->case_id = 0x5;
    1017            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1018              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1019            1 :     spdm_context->connection_info.connection_state =
    1020              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1021            1 :     spdm_context->connection_info.capability.flags |=
    1022              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP;
    1023            1 :     spdm_context->connection_info.capability.flags |=
    1024              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1025            1 :     spdm_context->connection_info.capability.flags |=
    1026              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1027            1 :     spdm_context->local_context.capability.flags |=
    1028              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP;
    1029            1 :     spdm_context->local_context.capability.flags |=
    1030              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1031            1 :     spdm_context->local_context.capability.flags |=
    1032              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1033            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1034              :                                                     m_libspdm_use_asym_algo, &data,
    1035              :                                                     &data_size, &hash, &hash_size);
    1036            1 :     libspdm_reset_message_a(spdm_context);
    1037            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1038              :         m_libspdm_use_hash_algo;
    1039            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1040              :         m_libspdm_use_asym_algo;
    1041            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1042              :         m_libspdm_use_dhe_algo;
    1043            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1044              :         m_libspdm_use_aead_algo;
    1045              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1046              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1047              :         data_size;
    1048              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1049              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1050              :                      data, data_size);
    1051              : #endif
    1052              : 
    1053            1 :     session_id = 0xFFFFFFFF;
    1054            1 :     session_info = &spdm_context->session_info[0];
    1055            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, true);
    1056            1 :     libspdm_secured_message_set_session_state(
    1057              :         session_info->secured_message_context,
    1058              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    1059            1 :     session_info->heartbeat_period = 1;
    1060            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    1061              :                     ((libspdm_secured_message_context_t
    1062            1 :                       *)(session_info->secured_message_context))
    1063              :                     ->aead_key_size,
    1064              :                     (uint8_t)(0xFF));
    1065            1 :     libspdm_secured_message_set_response_data_encryption_key(
    1066              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    1067              :         ((libspdm_secured_message_context_t
    1068            1 :           *)(session_info->secured_message_context))
    1069              :         ->aead_key_size);
    1070            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    1071              :                     ((libspdm_secured_message_context_t
    1072            1 :                       *)(session_info->secured_message_context))
    1073              :                     ->aead_iv_size,
    1074              :                     (uint8_t)(0xFF));
    1075            1 :     libspdm_secured_message_set_response_data_salt(
    1076              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    1077              :         ((libspdm_secured_message_context_t
    1078            1 :           *)(session_info->secured_message_context))
    1079              :         ->aead_iv_size);
    1080              :     ((libspdm_secured_message_context_t *)(session_info
    1081            1 :                                            ->secured_message_context))
    1082            1 :     ->application_secret.response_data_sequence_number = 0;
    1083              : 
    1084            1 :     status = libspdm_heartbeat(spdm_context, session_id);
    1085            1 :     assert_int_equal(status, LIBSPDM_STATUS_BUSY_PEER);
    1086            1 :     free(data);
    1087            1 : }
    1088              : 
    1089            1 : void libspdm_test_requester_heartbeat_case6(void **state)
    1090              : {
    1091              :     libspdm_return_t status;
    1092              :     libspdm_test_context_t *spdm_test_context;
    1093              :     libspdm_context_t *spdm_context;
    1094              :     uint32_t session_id;
    1095              :     void *data;
    1096              :     size_t data_size;
    1097              :     void *hash;
    1098              :     size_t hash_size;
    1099              :     libspdm_session_info_t *session_info;
    1100              : 
    1101            1 :     spdm_test_context = *state;
    1102            1 :     spdm_context = spdm_test_context->spdm_context;
    1103            1 :     spdm_test_context->case_id = 0x6;
    1104            1 :     spdm_context->retry_times = 3;
    1105            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1106              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1107            1 :     spdm_context->connection_info.connection_state =
    1108              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1109            1 :     spdm_context->connection_info.capability.flags |=
    1110              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP;
    1111            1 :     spdm_context->connection_info.capability.flags |=
    1112              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1113            1 :     spdm_context->connection_info.capability.flags |=
    1114              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1115            1 :     spdm_context->local_context.capability.flags |=
    1116              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP;
    1117            1 :     spdm_context->local_context.capability.flags |=
    1118              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1119            1 :     spdm_context->local_context.capability.flags |=
    1120              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1121            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1122              :                                                     m_libspdm_use_asym_algo, &data,
    1123              :                                                     &data_size, &hash, &hash_size);
    1124            1 :     libspdm_reset_message_a(spdm_context);
    1125            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1126              :         m_libspdm_use_hash_algo;
    1127            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1128              :         m_libspdm_use_asym_algo;
    1129            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1130              :         m_libspdm_use_dhe_algo;
    1131            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1132              :         m_libspdm_use_aead_algo;
    1133              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1134              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1135              :         data_size;
    1136              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1137              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1138              :                      data, data_size);
    1139              : #endif
    1140              : 
    1141            1 :     session_id = 0xFFFFFFFF;
    1142            1 :     session_info = &spdm_context->session_info[0];
    1143            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, true);
    1144            1 :     libspdm_secured_message_set_session_state(
    1145              :         session_info->secured_message_context,
    1146              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    1147            1 :     session_info->heartbeat_period = 1;
    1148            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    1149              :                     ((libspdm_secured_message_context_t
    1150            1 :                       *)(session_info->secured_message_context))
    1151              :                     ->aead_key_size,
    1152              :                     (uint8_t)(0xFF));
    1153            1 :     libspdm_secured_message_set_response_data_encryption_key(
    1154              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    1155              :         ((libspdm_secured_message_context_t
    1156            1 :           *)(session_info->secured_message_context))
    1157              :         ->aead_key_size);
    1158            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    1159              :                     ((libspdm_secured_message_context_t
    1160            1 :                       *)(session_info->secured_message_context))
    1161              :                     ->aead_iv_size,
    1162              :                     (uint8_t)(0xFF));
    1163            1 :     libspdm_secured_message_set_response_data_salt(
    1164              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    1165              :         ((libspdm_secured_message_context_t
    1166            1 :           *)(session_info->secured_message_context))
    1167              :         ->aead_iv_size);
    1168              :     ((libspdm_secured_message_context_t *)(session_info
    1169            1 :                                            ->secured_message_context))
    1170            1 :     ->application_secret.response_data_sequence_number = 0;
    1171              : 
    1172            1 :     status = libspdm_heartbeat(spdm_context, session_id);
    1173            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    1174            1 :     free(data);
    1175            1 : }
    1176              : 
    1177            1 : void libspdm_test_requester_heartbeat_case7(void **state)
    1178              : {
    1179              :     libspdm_return_t status;
    1180              :     libspdm_test_context_t *spdm_test_context;
    1181              :     libspdm_context_t *spdm_context;
    1182              :     uint32_t session_id;
    1183              :     void *data;
    1184              :     size_t data_size;
    1185              :     void *hash;
    1186              :     size_t hash_size;
    1187              :     libspdm_session_info_t *session_info;
    1188              : 
    1189            1 :     spdm_test_context = *state;
    1190            1 :     spdm_context = spdm_test_context->spdm_context;
    1191            1 :     spdm_test_context->case_id = 0x7;
    1192            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1193              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1194            1 :     spdm_context->connection_info.connection_state =
    1195              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1196            1 :     spdm_context->connection_info.capability.flags |=
    1197              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP;
    1198            1 :     spdm_context->connection_info.capability.flags |=
    1199              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1200            1 :     spdm_context->connection_info.capability.flags |=
    1201              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1202            1 :     spdm_context->local_context.capability.flags |=
    1203              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP;
    1204            1 :     spdm_context->local_context.capability.flags |=
    1205              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1206            1 :     spdm_context->local_context.capability.flags |=
    1207              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1208            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1209              :                                                     m_libspdm_use_asym_algo, &data,
    1210              :                                                     &data_size, &hash, &hash_size);
    1211            1 :     libspdm_reset_message_a(spdm_context);
    1212            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1213              :         m_libspdm_use_hash_algo;
    1214            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1215              :         m_libspdm_use_asym_algo;
    1216            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1217              :         m_libspdm_use_dhe_algo;
    1218            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1219              :         m_libspdm_use_aead_algo;
    1220              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1221              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1222              :         data_size;
    1223              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1224              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1225              :                      data, data_size);
    1226              : #endif
    1227              : 
    1228            1 :     session_id = 0xFFFFFFFF;
    1229            1 :     session_info = &spdm_context->session_info[0];
    1230            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, true);
    1231            1 :     libspdm_secured_message_set_session_state(
    1232              :         session_info->secured_message_context,
    1233              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    1234            1 :     session_info->heartbeat_period = 1;
    1235            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    1236              :                     ((libspdm_secured_message_context_t
    1237            1 :                       *)(session_info->secured_message_context))
    1238              :                     ->aead_key_size,
    1239              :                     (uint8_t)(0xFF));
    1240            1 :     libspdm_secured_message_set_response_data_encryption_key(
    1241              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    1242              :         ((libspdm_secured_message_context_t
    1243            1 :           *)(session_info->secured_message_context))
    1244              :         ->aead_key_size);
    1245            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    1246              :                     ((libspdm_secured_message_context_t
    1247            1 :                       *)(session_info->secured_message_context))
    1248              :                     ->aead_iv_size,
    1249              :                     (uint8_t)(0xFF));
    1250            1 :     libspdm_secured_message_set_response_data_salt(
    1251              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    1252              :         ((libspdm_secured_message_context_t
    1253            1 :           *)(session_info->secured_message_context))
    1254              :         ->aead_iv_size);
    1255              :     ((libspdm_secured_message_context_t *)(session_info
    1256            1 :                                            ->secured_message_context))
    1257            1 :     ->application_secret.response_data_sequence_number = 0;
    1258              : 
    1259            1 :     status = libspdm_heartbeat(spdm_context, session_id);
    1260            1 :     assert_int_equal(status, LIBSPDM_STATUS_RESYNCH_PEER);
    1261            1 :     assert_int_equal(spdm_context->connection_info.connection_state,
    1262              :                      LIBSPDM_CONNECTION_STATE_NOT_STARTED);
    1263            1 :     free(data);
    1264            1 : }
    1265              : 
    1266            1 : void libspdm_test_requester_heartbeat_case8(void **state)
    1267              : {
    1268              :     libspdm_return_t status;
    1269              :     libspdm_test_context_t *spdm_test_context;
    1270              :     libspdm_context_t *spdm_context;
    1271              :     uint32_t session_id;
    1272              :     void *data;
    1273              :     size_t data_size;
    1274              :     void *hash;
    1275              :     size_t hash_size;
    1276              :     libspdm_session_info_t *session_info;
    1277              : 
    1278            1 :     spdm_test_context = *state;
    1279            1 :     spdm_context = spdm_test_context->spdm_context;
    1280            1 :     spdm_test_context->case_id = 0x8;
    1281            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1282              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1283            1 :     spdm_context->connection_info.connection_state =
    1284              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1285            1 :     spdm_context->connection_info.capability.flags |=
    1286              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP;
    1287            1 :     spdm_context->connection_info.capability.flags |=
    1288              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1289            1 :     spdm_context->connection_info.capability.flags |=
    1290              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1291            1 :     spdm_context->local_context.capability.flags |=
    1292              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP;
    1293            1 :     spdm_context->local_context.capability.flags |=
    1294              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1295            1 :     spdm_context->local_context.capability.flags |=
    1296              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1297            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1298              :                                                     m_libspdm_use_asym_algo, &data,
    1299              :                                                     &data_size, &hash, &hash_size);
    1300            1 :     libspdm_reset_message_a(spdm_context);
    1301            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1302              :         m_libspdm_use_hash_algo;
    1303            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1304              :         m_libspdm_use_asym_algo;
    1305            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1306              :         m_libspdm_use_dhe_algo;
    1307            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1308              :         m_libspdm_use_aead_algo;
    1309              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1310              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1311              :         data_size;
    1312              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1313              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1314              :                      data, data_size);
    1315              : #endif
    1316              : 
    1317            1 :     session_id = 0xFFFFFFFF;
    1318            1 :     session_info = &spdm_context->session_info[0];
    1319            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, true);
    1320            1 :     libspdm_secured_message_set_session_state(
    1321              :         session_info->secured_message_context,
    1322              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    1323            1 :     session_info->heartbeat_period = 1;
    1324            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    1325              :                     ((libspdm_secured_message_context_t
    1326            1 :                       *)(session_info->secured_message_context))
    1327              :                     ->aead_key_size,
    1328              :                     (uint8_t)(0xFF));
    1329            1 :     libspdm_secured_message_set_response_data_encryption_key(
    1330              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    1331              :         ((libspdm_secured_message_context_t
    1332            1 :           *)(session_info->secured_message_context))
    1333              :         ->aead_key_size);
    1334            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    1335              :                     ((libspdm_secured_message_context_t
    1336            1 :                       *)(session_info->secured_message_context))
    1337              :                     ->aead_iv_size,
    1338              :                     (uint8_t)(0xFF));
    1339            1 :     libspdm_secured_message_set_response_data_salt(
    1340              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    1341              :         ((libspdm_secured_message_context_t
    1342            1 :           *)(session_info->secured_message_context))
    1343              :         ->aead_iv_size);
    1344              :     ((libspdm_secured_message_context_t *)(session_info
    1345            1 :                                            ->secured_message_context))
    1346            1 :     ->application_secret.response_data_sequence_number = 0;
    1347              : 
    1348            1 :     status = libspdm_heartbeat(spdm_context, session_id);
    1349            1 :     assert_int_equal(status, LIBSPDM_STATUS_NOT_READY_PEER);
    1350            1 :     free(data);
    1351            1 : }
    1352              : 
    1353            1 : void libspdm_test_requester_heartbeat_case9(void **state)
    1354              : {
    1355              :     libspdm_return_t status;
    1356              :     libspdm_test_context_t *spdm_test_context;
    1357              :     libspdm_context_t *spdm_context;
    1358              :     uint32_t session_id;
    1359              :     void *data;
    1360              :     size_t data_size;
    1361              :     void *hash;
    1362              :     size_t hash_size;
    1363              :     libspdm_session_info_t *session_info;
    1364              : 
    1365            1 :     spdm_test_context = *state;
    1366            1 :     spdm_context = spdm_test_context->spdm_context;
    1367            1 :     spdm_test_context->case_id = 0x9;
    1368            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1369              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1370            1 :     spdm_context->connection_info.connection_state =
    1371              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1372            1 :     spdm_context->connection_info.capability.flags |=
    1373              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP;
    1374            1 :     spdm_context->connection_info.capability.flags |=
    1375              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1376            1 :     spdm_context->connection_info.capability.flags |=
    1377              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1378            1 :     spdm_context->local_context.capability.flags |=
    1379              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP;
    1380            1 :     spdm_context->local_context.capability.flags |=
    1381              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1382            1 :     spdm_context->local_context.capability.flags |=
    1383              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1384            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1385              :                                                     m_libspdm_use_asym_algo, &data,
    1386              :                                                     &data_size, &hash, &hash_size);
    1387            1 :     libspdm_reset_message_a(spdm_context);
    1388            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1389              :         m_libspdm_use_hash_algo;
    1390            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1391              :         m_libspdm_use_asym_algo;
    1392            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1393              :         m_libspdm_use_dhe_algo;
    1394            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1395              :         m_libspdm_use_aead_algo;
    1396              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1397              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1398              :         data_size;
    1399              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1400              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1401              :                      data, data_size);
    1402              : #endif
    1403              : 
    1404            1 :     session_id = 0xFFFFFFFF;
    1405            1 :     session_info = &spdm_context->session_info[0];
    1406            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, true);
    1407            1 :     libspdm_secured_message_set_session_state(
    1408              :         session_info->secured_message_context,
    1409              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    1410            1 :     session_info->heartbeat_period = 1;
    1411            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    1412              :                     ((libspdm_secured_message_context_t
    1413            1 :                       *)(session_info->secured_message_context))
    1414              :                     ->aead_key_size,
    1415              :                     (uint8_t)(0xFF));
    1416            1 :     libspdm_secured_message_set_response_data_encryption_key(
    1417              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    1418              :         ((libspdm_secured_message_context_t
    1419            1 :           *)(session_info->secured_message_context))
    1420              :         ->aead_key_size);
    1421            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    1422              :                     ((libspdm_secured_message_context_t
    1423            1 :                       *)(session_info->secured_message_context))
    1424              :                     ->aead_iv_size,
    1425              :                     (uint8_t)(0xFF));
    1426            1 :     libspdm_secured_message_set_response_data_salt(
    1427              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    1428              :         ((libspdm_secured_message_context_t
    1429            1 :           *)(session_info->secured_message_context))
    1430              :         ->aead_iv_size);
    1431              :     ((libspdm_secured_message_context_t *)(session_info
    1432            1 :                                            ->secured_message_context))
    1433            1 :     ->application_secret.response_data_sequence_number = 0;
    1434              : 
    1435            1 :     status = libspdm_heartbeat(spdm_context, session_id);
    1436              :     if (LIBSPDM_RESPOND_IF_READY_SUPPORT) {
    1437            1 :         assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    1438              :     } else {
    1439              :         assert_int_equal(status, LIBSPDM_STATUS_NOT_READY_PEER);
    1440              :     }
    1441            1 :     free(data);
    1442            1 : }
    1443              : 
    1444            1 : void libspdm_test_requester_heartbeat_case10(void **state) {
    1445              :     libspdm_return_t status;
    1446              :     libspdm_test_context_t    *spdm_test_context;
    1447              :     libspdm_context_t  *spdm_context;
    1448              :     uint32_t session_id;
    1449              :     void                 *data;
    1450              :     size_t data_size;
    1451              :     void                 *hash;
    1452              :     size_t hash_size;
    1453              :     libspdm_session_info_t    *session_info;
    1454              :     uint16_t error_code;
    1455              : 
    1456            1 :     spdm_test_context = *state;
    1457            1 :     spdm_context = spdm_test_context->spdm_context;
    1458            1 :     spdm_test_context->case_id = 0xA;
    1459            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1460              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1461            1 :     spdm_context->connection_info.capability.flags |=
    1462              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP;
    1463            1 :     spdm_context->connection_info.capability.flags |=
    1464              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1465            1 :     spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1466            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP;
    1467            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1468            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1469            1 :     libspdm_read_responder_public_certificate_chain (m_libspdm_use_hash_algo,
    1470              :                                                      m_libspdm_use_asym_algo,
    1471              :                                                      &data, &data_size,
    1472              :                                                      &hash, &hash_size);
    1473            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    1474            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    1475            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    1476            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    1477              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1478              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1479              :         data_size;
    1480              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1481              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1482              :                      data, data_size);
    1483              : #endif
    1484              : 
    1485            1 :     error_code = LIBSPDM_ERROR_CODE_RESERVED_00;
    1486           19 :     while(error_code <= 0xff) {
    1487           18 :         spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1488           18 :         libspdm_reset_message_a(spdm_context);
    1489              : 
    1490           18 :         session_id = 0xFFFFFFFF;
    1491           18 :         session_info = &spdm_context->session_info[0];
    1492           18 :         libspdm_session_info_init (spdm_context, session_info, session_id, true);
    1493           18 :         libspdm_secured_message_set_session_state (session_info->secured_message_context,
    1494              :                                                    LIBSPDM_SESSION_STATE_ESTABLISHED);
    1495           18 :         session_info->heartbeat_period = 1;
    1496           18 :         libspdm_set_mem (m_libspdm_dummy_key_buffer,
    1497           18 :                          ((libspdm_secured_message_context_t*)(session_info->secured_message_context))->aead_key_size,
    1498              :                          (uint8_t)(0xFF));
    1499           18 :         libspdm_secured_message_set_response_data_encryption_key (
    1500              :             session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    1501           18 :             ((libspdm_secured_message_context_t*)(session_info->secured_message_context))->aead_key_size);
    1502           18 :         libspdm_set_mem (m_libspdm_dummy_salt_buffer,
    1503           18 :                          ((libspdm_secured_message_context_t*)(session_info->secured_message_context))->aead_iv_size,
    1504              :                          (uint8_t)(0xFF));
    1505           18 :         libspdm_secured_message_set_response_data_salt (session_info->secured_message_context,
    1506              :                                                         m_libspdm_dummy_salt_buffer,
    1507              :                                                         ((libspdm_secured_message_context_t*)(
    1508              :                                                              session_info
    1509           18 :                                                              ->
    1510              :                                                              secured_message_context))->aead_iv_size);
    1511           18 :         ((libspdm_secured_message_context_t*)(session_info->secured_message_context))->
    1512           18 :         application_secret.response_data_sequence_number = 0;
    1513              : 
    1514           18 :         status = libspdm_heartbeat (spdm_context, session_id);
    1515           18 :         if(error_code != SPDM_ERROR_CODE_DECRYPT_ERROR) {
    1516           17 :             LIBSPDM_ASSERT_INT_EQUAL_CASE (status, LIBSPDM_STATUS_ERROR_PEER, error_code);
    1517              :         } else {
    1518            1 :             LIBSPDM_ASSERT_INT_EQUAL_CASE (status, LIBSPDM_STATUS_SESSION_MSG_ERROR, error_code);
    1519              :         }
    1520              : 
    1521           18 :         error_code++;
    1522           18 :         if(error_code == SPDM_ERROR_CODE_BUSY) { /*busy is treated in cases 5 and 6*/
    1523            1 :             error_code = SPDM_ERROR_CODE_UNEXPECTED_REQUEST;
    1524              :         }
    1525           18 :         if(error_code == LIBSPDM_ERROR_CODE_RESERVED_0D) { /*skip some reserved error codes (0d to 3e)*/
    1526            1 :             error_code = LIBSPDM_ERROR_CODE_RESERVED_3F;
    1527              :         }
    1528           18 :         if(error_code == SPDM_ERROR_CODE_RESPONSE_NOT_READY) { /*skip response not ready, request resync, and some reserved codes (44 to fc)*/
    1529            1 :             error_code = LIBSPDM_ERROR_CODE_RESERVED_FD;
    1530              :         }
    1531              :     }
    1532              : 
    1533            1 :     free(data);
    1534            1 : }
    1535              : 
    1536            1 : void libspdm_test_requester_heartbeat_case11(void **state)
    1537              : {
    1538              :     libspdm_return_t status;
    1539              :     libspdm_test_context_t *spdm_test_context;
    1540              :     libspdm_context_t *spdm_context;
    1541              :     uint32_t session_id;
    1542              :     void *data;
    1543              :     size_t data_size;
    1544              :     void *hash;
    1545              :     size_t hash_size;
    1546              :     libspdm_session_info_t *session_info;
    1547              : 
    1548            1 :     spdm_test_context = *state;
    1549            1 :     spdm_context = spdm_test_context->spdm_context;
    1550            1 :     spdm_test_context->case_id = 0xB;
    1551            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1552              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1553            1 :     spdm_context->connection_info.connection_state =
    1554              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1555            1 :     spdm_context->connection_info.capability.flags |=
    1556              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP;
    1557            1 :     spdm_context->connection_info.capability.flags |=
    1558              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1559            1 :     spdm_context->connection_info.capability.flags |=
    1560              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1561            1 :     spdm_context->local_context.capability.flags |=
    1562              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP;
    1563            1 :     spdm_context->local_context.capability.flags |=
    1564              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1565            1 :     spdm_context->local_context.capability.flags |=
    1566              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1567            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1568              :                                                     m_libspdm_use_asym_algo, &data,
    1569              :                                                     &data_size, &hash, &hash_size);
    1570            1 :     libspdm_reset_message_a(spdm_context);
    1571            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1572              :         m_libspdm_use_hash_algo;
    1573            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1574              :         m_libspdm_use_asym_algo;
    1575            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1576              :         m_libspdm_use_dhe_algo;
    1577            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1578              :         m_libspdm_use_aead_algo;
    1579              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1580              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1581              :         data_size;
    1582              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1583              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1584              :                      data, data_size);
    1585              : #endif
    1586              : 
    1587            1 :     session_id = 0xFFFFFFFF;
    1588            1 :     session_info = &spdm_context->session_info[0];
    1589            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, true);
    1590            1 :     libspdm_secured_message_set_session_state(
    1591              :         session_info->secured_message_context,
    1592              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    1593            1 :     session_info->heartbeat_period = 1;
    1594            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    1595              :                     ((libspdm_secured_message_context_t
    1596            1 :                       *)(session_info->secured_message_context))
    1597              :                     ->aead_key_size,
    1598              :                     (uint8_t)(0xFF));
    1599            1 :     libspdm_secured_message_set_response_data_encryption_key(
    1600              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    1601              :         ((libspdm_secured_message_context_t
    1602            1 :           *)(session_info->secured_message_context))
    1603              :         ->aead_key_size);
    1604            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    1605              :                     ((libspdm_secured_message_context_t
    1606            1 :                       *)(session_info->secured_message_context))
    1607              :                     ->aead_iv_size,
    1608              :                     (uint8_t)(0xFF));
    1609            1 :     libspdm_secured_message_set_response_data_salt(
    1610              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    1611              :         ((libspdm_secured_message_context_t
    1612            1 :           *)(session_info->secured_message_context))
    1613              :         ->aead_iv_size);
    1614              :     ((libspdm_secured_message_context_t *)(session_info
    1615            1 :                                            ->secured_message_context))
    1616            1 :     ->application_secret.response_data_sequence_number = 0;
    1617              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1618              :     session_info->session_transcript.message_m.buffer_size =
    1619              :         session_info->session_transcript.message_m.max_buffer_size;
    1620              :     spdm_context->transcript.message_b.buffer_size =
    1621              :         spdm_context->transcript.message_b.max_buffer_size;
    1622              :     spdm_context->transcript.message_c.buffer_size =
    1623              :         spdm_context->transcript.message_c.max_buffer_size;
    1624              :     spdm_context->transcript.message_mut_b.buffer_size =
    1625              :         spdm_context->transcript.message_mut_b.max_buffer_size;
    1626              :     spdm_context->transcript.message_mut_c.buffer_size =
    1627              :         spdm_context->transcript.message_mut_c.max_buffer_size;
    1628              : #endif
    1629              : 
    1630            1 :     status = libspdm_heartbeat(spdm_context, session_id);
    1631            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    1632              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1633              :     assert_int_equal(session_info->session_transcript.message_m.buffer_size, 0);
    1634              :     assert_int_equal(spdm_context->transcript.message_b.buffer_size, 0);
    1635              :     assert_int_equal(spdm_context->transcript.message_c.buffer_size, 0);
    1636              :     assert_int_equal(spdm_context->transcript.message_mut_b.buffer_size, 0);
    1637              :     assert_int_equal(spdm_context->transcript.message_mut_c.buffer_size, 0);
    1638              : #endif
    1639            1 :     free(data);
    1640            1 : }
    1641              : 
    1642              : /**
    1643              :  * Test 12: the requester is setup correctly, but receives an ERROR with SPDM_ERROR_CODE_DECRYPT_ERROR.
    1644              :  * Expected behavior: client returns a Status of INVALID_SESSION_ID  and free the session ID.
    1645              :  **/
    1646            1 : void libspdm_test_requester_heartbeat_case12(void **state)
    1647              : {
    1648              :     libspdm_return_t status;
    1649              :     libspdm_test_context_t *spdm_test_context;
    1650              :     libspdm_context_t *spdm_context;
    1651              :     uint32_t session_id;
    1652              :     void *data;
    1653              :     size_t data_size;
    1654              :     void *hash;
    1655              :     size_t hash_size;
    1656              :     libspdm_session_info_t *session_info;
    1657              : 
    1658            1 :     spdm_test_context = *state;
    1659            1 :     spdm_context = spdm_test_context->spdm_context;
    1660            1 :     spdm_test_context->case_id = 0xC;
    1661            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1662              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1663            1 :     spdm_context->connection_info.connection_state =
    1664              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1665            1 :     spdm_context->connection_info.capability.flags |=
    1666              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP;
    1667            1 :     spdm_context->connection_info.capability.flags |=
    1668              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1669            1 :     spdm_context->connection_info.capability.flags |=
    1670              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1671            1 :     spdm_context->local_context.capability.flags |=
    1672              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP;
    1673            1 :     spdm_context->local_context.capability.flags |=
    1674              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1675            1 :     spdm_context->local_context.capability.flags |=
    1676              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1677            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1678              :                                                     m_libspdm_use_asym_algo, &data,
    1679              :                                                     &data_size, &hash, &hash_size);
    1680            1 :     libspdm_reset_message_a(spdm_context);
    1681            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1682              :         m_libspdm_use_hash_algo;
    1683            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1684              :         m_libspdm_use_asym_algo;
    1685            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1686              :         m_libspdm_use_dhe_algo;
    1687            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1688              :         m_libspdm_use_aead_algo;
    1689              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1690              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1691              :         data_size;
    1692              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1693              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1694              :                      data, data_size);
    1695              : #endif
    1696              : 
    1697            1 :     session_id = 0xFFFFFFFF;
    1698            1 :     session_info = &spdm_context->session_info[0];
    1699            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, true);
    1700            1 :     libspdm_secured_message_set_session_state(
    1701              :         session_info->secured_message_context,
    1702              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    1703            1 :     session_info->heartbeat_period = 1;
    1704            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    1705              :                     ((libspdm_secured_message_context_t
    1706            1 :                       *)(session_info->secured_message_context))
    1707              :                     ->aead_key_size,
    1708              :                     (uint8_t)(0xFF));
    1709            1 :     libspdm_secured_message_set_response_data_encryption_key(
    1710              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    1711              :         ((libspdm_secured_message_context_t
    1712            1 :           *)(session_info->secured_message_context))
    1713              :         ->aead_key_size);
    1714            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    1715              :                     ((libspdm_secured_message_context_t
    1716            1 :                       *)(session_info->secured_message_context))
    1717              :                     ->aead_iv_size,
    1718              :                     (uint8_t)(0xFF));
    1719            1 :     libspdm_secured_message_set_response_data_salt(
    1720              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    1721              :         ((libspdm_secured_message_context_t
    1722            1 :           *)(session_info->secured_message_context))
    1723              :         ->aead_iv_size);
    1724              :     ((libspdm_secured_message_context_t *)(session_info
    1725            1 :                                            ->secured_message_context))
    1726            1 :     ->application_secret.response_data_sequence_number = 0;
    1727              : 
    1728            1 :     status = libspdm_heartbeat(spdm_context, session_id);
    1729            1 :     assert_int_equal(status, LIBSPDM_STATUS_SESSION_MSG_ERROR);
    1730            1 :     assert_int_equal(spdm_context->session_info->session_id, INVALID_SESSION_ID);
    1731              : 
    1732            1 :     free(data);
    1733            1 : }
    1734              : 
    1735            1 : void libspdm_test_requester_heartbeat_case13(void **state)
    1736              : {
    1737              :     libspdm_return_t status;
    1738              :     libspdm_test_context_t *spdm_test_context;
    1739              :     libspdm_context_t *spdm_context;
    1740              :     uint32_t session_id;
    1741              :     void *data;
    1742              :     size_t data_size;
    1743              :     void *hash;
    1744              :     size_t hash_size;
    1745              :     libspdm_session_info_t *session_info;
    1746              : 
    1747            1 :     spdm_test_context = *state;
    1748            1 :     spdm_context = spdm_test_context->spdm_context;
    1749            1 :     spdm_test_context->case_id = 0x3;
    1750            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1751              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1752            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_AUTHENTICATED;
    1753            1 :     spdm_context->connection_info.capability.flags |=
    1754              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP;
    1755            1 :     spdm_context->connection_info.capability.flags |=
    1756              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1757            1 :     spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1758            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP;
    1759            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1760            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1761            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1762              :                                                     m_libspdm_use_asym_algo, &data,
    1763              :                                                     &data_size, &hash, &hash_size);
    1764            1 :     libspdm_reset_message_a(spdm_context);
    1765            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    1766            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    1767            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    1768            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    1769              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1770              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
    1771              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1772              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1773              :                      data, data_size);
    1774              : #endif
    1775              : 
    1776            1 :     session_id = 0xFFFFFFFF;
    1777            1 :     session_info = &spdm_context->session_info[0];
    1778            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, true);
    1779            1 :     libspdm_secured_message_set_session_state(
    1780              :         session_info->secured_message_context,
    1781              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    1782            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    1783              :                     ((libspdm_secured_message_context_t
    1784            1 :                       *)(session_info->secured_message_context))
    1785              :                     ->aead_key_size, (uint8_t)(0xFF));
    1786            1 :     libspdm_secured_message_set_response_data_encryption_key(
    1787              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    1788              :         ((libspdm_secured_message_context_t
    1789            1 :           *)(session_info->secured_message_context))->aead_key_size);
    1790            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    1791              :                     ((libspdm_secured_message_context_t
    1792            1 :                       *)(session_info->secured_message_context))
    1793              :                     ->aead_iv_size, (uint8_t)(0xFF));
    1794            1 :     libspdm_secured_message_set_response_data_salt(
    1795              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    1796              :         ((libspdm_secured_message_context_t
    1797            1 :           *)(session_info->secured_message_context))->aead_iv_size);
    1798              :     ((libspdm_secured_message_context_t *)(session_info
    1799            1 :                                            ->secured_message_context))
    1800            1 :     ->application_secret.response_data_sequence_number = 0;
    1801              : 
    1802            1 :     session_info->heartbeat_period = 0;
    1803              : 
    1804            1 :     status = libspdm_heartbeat(spdm_context, session_id);
    1805            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_STATE_LOCAL);
    1806            1 :     free(data);
    1807            1 : }
    1808              : 
    1809            1 : int libspdm_requester_heartbeat_test_main(void)
    1810              : {
    1811            1 :     const struct CMUnitTest spdm_requester_heartbeat_tests[] = {
    1812              :         /* SendRequest failed*/
    1813              :         cmocka_unit_test(libspdm_test_requester_heartbeat_case1),
    1814              :         /* Successful response*/
    1815              :         cmocka_unit_test(libspdm_test_requester_heartbeat_case2),
    1816              :         /* connection_state check failed*/
    1817              :         cmocka_unit_test(libspdm_test_requester_heartbeat_case3),
    1818              :         /* Error response: SPDM_ERROR_CODE_INVALID_REQUEST*/
    1819              :         cmocka_unit_test(libspdm_test_requester_heartbeat_case4),
    1820              :         /* Always SPDM_ERROR_CODE_BUSY*/
    1821              :         cmocka_unit_test(libspdm_test_requester_heartbeat_case5),
    1822              :         /* SPDM_ERROR_CODE_BUSY + Successful response*/
    1823              :         cmocka_unit_test(libspdm_test_requester_heartbeat_case6),
    1824              :         /* Error response: SPDM_ERROR_CODE_REQUEST_RESYNCH*/
    1825              :         cmocka_unit_test(libspdm_test_requester_heartbeat_case7),
    1826              :         /* Always SPDM_ERROR_CODE_RESPONSE_NOT_READY*/
    1827              :         cmocka_unit_test(libspdm_test_requester_heartbeat_case8),
    1828              :         /* SPDM_ERROR_CODE_RESPONSE_NOT_READY + Successful response*/
    1829              :         cmocka_unit_test(libspdm_test_requester_heartbeat_case9),
    1830              :         /* Unexpected errors*/
    1831              :         cmocka_unit_test(libspdm_test_requester_heartbeat_case10),
    1832              :         /* Buffer reset*/
    1833              :         cmocka_unit_test(libspdm_test_requester_heartbeat_case11),
    1834              :         /* Error response: SPDM_ERROR_CODE_DECRYPT_ERROR*/
    1835              :         cmocka_unit_test(libspdm_test_requester_heartbeat_case12),
    1836              :         cmocka_unit_test(libspdm_test_requester_heartbeat_case13),
    1837              :     };
    1838              : 
    1839            1 :     libspdm_test_context_t test_context = {
    1840              :         LIBSPDM_TEST_CONTEXT_VERSION,
    1841              :         true,
    1842              :         libspdm_requester_heartbeat_test_send_message,
    1843              :         libspdm_requester_heartbeat_test_receive_message,
    1844              :     };
    1845              : 
    1846            1 :     libspdm_setup_test_context(&test_context);
    1847              : 
    1848            1 :     return cmocka_run_group_tests(spdm_requester_heartbeat_tests,
    1849              :                                   libspdm_unit_test_group_setup,
    1850              :                                   libspdm_unit_test_group_teardown);
    1851              : }
    1852              : 
    1853              : #endif /* (LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP) || (LIBSPDM_ENABLE_CAPABILITY_PSK_CAP) */
        

Generated by: LCOV version 2.0-1