LCOV - code coverage report
Current view: top level - unit_test/test_spdm_requester - chunk_send.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 96.3 % 270 260
Test Date: 2025-06-29 08:09:00 Functions: 100.0 % 19 19

            Line data    Source code
       1              : /**
       2              :  *  Copyright Notice:
       3              :  *  Copyright 2021-2025 DMTF. All rights reserved.
       4              :  *  License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
       5              :  **/
       6              : 
       7              : #include "spdm_unit_test.h"
       8              : #include "internal/libspdm_requester_lib.h"
       9              : #include "internal/libspdm_secured_message_lib.h"
      10              : 
      11              : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
      12              : 
      13              : static bool m_libspdm_chunk_send_last_chunk = false;
      14              : static uint8_t m_libspdm_chunk_send_chunk_handle = 0;
      15              : static uint16_t m_libspdm_chunk_send_chunk_seq_no = 0;
      16              : 
      17              : /* Override the LIBSPDM_DATA_TRANSFER_SIZE just for the unit tests in this file.
      18              :  * All other unit tests have the default data transfer size due to the specific
      19              :  * unit tests requests and responses hardcode for each test case. */
      20              : #define CHUNK_SEND_REQUESTER_UNIT_TEST_DATA_TRANSFER_SIZE (42)
      21              : 
      22            3 : void libspdm_requester_chunk_send_test_case1_build_algorithms_response(
      23              :     void* context, void* response, size_t* response_size)
      24              : {
      25              :     spdm_algorithms_response_t* spdm_response;
      26              : 
      27            3 :     *response_size = sizeof(spdm_algorithms_response_t);
      28            3 :     spdm_response = (spdm_algorithms_response_t*) response;
      29              : 
      30            3 :     libspdm_zero_mem(spdm_response, *response_size);
      31            3 :     spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_12;
      32            3 :     spdm_response->header.request_response_code = SPDM_ALGORITHMS;
      33            3 :     spdm_response->header.param1 = 0;
      34            3 :     spdm_response->header.param2 = 0;
      35            3 :     spdm_response->length = sizeof(spdm_algorithms_response_t);
      36            3 :     spdm_response->measurement_specification_sel =
      37              :         SPDM_MEASUREMENT_SPECIFICATION_DMTF;
      38            3 :     spdm_response->measurement_hash_algo =
      39              :         m_libspdm_use_measurement_hash_algo;
      40            3 :     spdm_response->base_asym_sel = m_libspdm_use_asym_algo;
      41            3 :     spdm_response->base_hash_sel = m_libspdm_use_hash_algo;
      42            3 :     spdm_response->ext_asym_sel_count = 0;
      43            3 :     spdm_response->ext_hash_sel_count = 0;
      44            3 : }
      45              : 
      46              : 
      47           13 : libspdm_return_t libspdm_requester_chunk_send_test_send_message(
      48              :     void* spdm_context, size_t request_size, const void* request,
      49              :     uint64_t timeout)
      50              : {
      51              :     libspdm_test_context_t* spdm_test_context;
      52              :     const spdm_chunk_send_request_t* chunk_send;
      53              : 
      54           13 :     spdm_test_context = libspdm_get_test_context();
      55              : 
      56           13 :     chunk_send = (const spdm_chunk_send_request_t*)
      57              :                  ((const uint8_t*) request + sizeof(libspdm_test_message_header_t));
      58              : 
      59           13 :     m_libspdm_chunk_send_chunk_handle = chunk_send->header.param2;
      60           13 :     m_libspdm_chunk_send_chunk_seq_no = chunk_send->chunk_seq_no;
      61              : 
      62           13 :     if (chunk_send->header.param1 & SPDM_CHUNK_SEND_REQUEST_ATTRIBUTE_LAST_CHUNK) {
      63            2 :         m_libspdm_chunk_send_last_chunk = true;
      64              :     }
      65              : 
      66           13 :     if (spdm_test_context->case_id == 1) {
      67            2 :         return LIBSPDM_STATUS_SUCCESS;
      68              :     }
      69           11 :     if (spdm_test_context->case_id == 2) {
      70            1 :         return LIBSPDM_STATUS_SEND_FAIL;
      71              :     }
      72           10 :     if (spdm_test_context->case_id == 3) {
      73            1 :         return LIBSPDM_STATUS_SUCCESS;
      74              :     }
      75            9 :     if (spdm_test_context->case_id == 4) {
      76            1 :         return LIBSPDM_STATUS_SUCCESS;
      77              :     }
      78            8 :     if (spdm_test_context->case_id == 5) {
      79            1 :         return LIBSPDM_STATUS_SUCCESS;
      80              :     }
      81            7 :     if (spdm_test_context->case_id == 6) {
      82            1 :         return LIBSPDM_STATUS_SUCCESS;
      83              :     }
      84            6 :     if (spdm_test_context->case_id == 7) {
      85            1 :         return LIBSPDM_STATUS_SUCCESS;
      86              :     }
      87            5 :     if (spdm_test_context->case_id == 8) {
      88            1 :         return LIBSPDM_STATUS_SUCCESS;
      89              :     }
      90            4 :     if (spdm_test_context->case_id == 9) {
      91            1 :         return LIBSPDM_STATUS_SUCCESS;
      92              :     }
      93            3 :     if (spdm_test_context->case_id == 10) {
      94            2 :         if (chunk_send->header.request_response_code == SPDM_CHUNK_SEND) {
      95            2 :             return LIBSPDM_STATUS_SUCCESS;
      96              :         }
      97            0 :         return LIBSPDM_STATUS_SEND_FAIL;
      98              :     }
      99            1 :     if (spdm_test_context->case_id == 11) {
     100            0 :         return LIBSPDM_STATUS_SUCCESS;
     101              :     }
     102            1 :     if (spdm_test_context->case_id == 12) {
     103              :         /* Here the send request message should always be SPDM_CHUNK_SEND,
     104              :          * if not then something is wrong. */
     105            1 :         LIBSPDM_ASSERT(chunk_send->header.request_response_code == SPDM_CHUNK_SEND);
     106            1 :         return LIBSPDM_STATUS_SUCCESS;
     107              :     }
     108            0 :     if (spdm_test_context->case_id == 13) {
     109              :         /* Should never reach here since the test case is meant to fail before send */
     110            0 :         LIBSPDM_ASSERT(0);
     111              :     }
     112            0 :     return LIBSPDM_STATUS_SEND_FAIL;
     113              : }
     114              : 
     115           12 : libspdm_return_t libspdm_requester_chunk_send_test_receive_message(
     116              :     void *context, size_t *response_size,
     117              :     void **response, uint64_t timeout)
     118              : {
     119              :     libspdm_test_context_t *spdm_test_context;
     120              :     libspdm_context_t *spdm_context;
     121              :     spdm_chunk_send_ack_response_t *chunk_send_ack_rsp;
     122              :     spdm_error_response_t *error_response;
     123              :     size_t chunk_rsp_size;
     124              :     uint8_t *chunk_copy_to;
     125              :     size_t chunk_size;
     126              : 
     127           12 :     spdm_test_context = libspdm_get_test_context();
     128           12 :     spdm_context = context;
     129              : 
     130           12 :     if ((spdm_test_context->case_id == 1) || (spdm_test_context->case_id == 10) ||
     131            8 :         (spdm_test_context->case_id == 11)) {
     132              :         /* Successful chunk send of algorithms request */
     133              :         chunk_send_ack_rsp
     134            4 :             = (void*) ((uint8_t*) *response + sizeof(libspdm_test_message_header_t));
     135              : 
     136            4 :         chunk_send_ack_rsp->header.spdm_version = SPDM_MESSAGE_VERSION_12;
     137            4 :         chunk_send_ack_rsp->header.request_response_code = SPDM_CHUNK_SEND_ACK;
     138            4 :         chunk_send_ack_rsp->header.param1 = 0;
     139            4 :         chunk_send_ack_rsp->header.param2 = m_libspdm_chunk_send_chunk_handle;
     140            4 :         chunk_send_ack_rsp->chunk_seq_no = m_libspdm_chunk_send_chunk_seq_no;
     141              : 
     142            4 :         if (m_libspdm_chunk_send_last_chunk) {
     143              : 
     144            3 :             chunk_copy_to = (uint8_t*) (chunk_send_ack_rsp + 1);
     145            3 :             chunk_size = *response_size - (chunk_copy_to - (uint8_t*) *response);
     146              : 
     147            3 :             libspdm_requester_chunk_send_test_case1_build_algorithms_response(
     148              :                 spdm_context, chunk_copy_to, &chunk_size);
     149            3 :             chunk_rsp_size = sizeof(spdm_chunk_send_ack_response_t) + chunk_size;
     150              : 
     151            3 :             libspdm_transport_test_encode_message(
     152              :                 spdm_context, NULL, false, false,
     153              :                 chunk_rsp_size, chunk_send_ack_rsp,
     154              :                 response_size, response);
     155              :         } else {
     156            1 :             chunk_rsp_size = sizeof(spdm_chunk_send_ack_response_t);
     157            1 :             libspdm_transport_test_encode_message(
     158              :                 spdm_context, NULL, false, false,
     159              :                 chunk_rsp_size, chunk_send_ack_rsp,
     160              :                 response_size, response);
     161              :         }
     162            4 :         return LIBSPDM_STATUS_SUCCESS;
     163              :     }
     164            8 :     if (spdm_test_context->case_id == 2) {
     165              :         /* Request fail send
     166              :          * Should never reach here since the test case is meant to fail at send */
     167            0 :         LIBSPDM_ASSERT(0);
     168            0 :         return LIBSPDM_STATUS_RECEIVE_FAIL;
     169              :     }
     170            8 :     if (spdm_test_context->case_id == 3) {
     171              :         /* Response fail receive */
     172            1 :         return LIBSPDM_STATUS_RECEIVE_FAIL;
     173              :     }
     174            7 :     if (spdm_test_context->case_id == 4) {
     175              :         /* Response has bad SPDM version */
     176              :         chunk_send_ack_rsp
     177            1 :             = (void*) ((uint8_t*) *response + sizeof(libspdm_test_message_header_t));
     178              : 
     179            1 :         chunk_send_ack_rsp->header.spdm_version = SPDM_MESSAGE_VERSION_11; /* Bad SPDM version */
     180            1 :         chunk_send_ack_rsp->header.request_response_code = SPDM_CHUNK_SEND_ACK;
     181            1 :         chunk_send_ack_rsp->header.param1 = 0;
     182            1 :         chunk_send_ack_rsp->header.param2 = m_libspdm_chunk_send_chunk_handle;
     183            1 :         chunk_send_ack_rsp->chunk_seq_no = m_libspdm_chunk_send_chunk_seq_no;
     184              : 
     185            1 :         chunk_rsp_size = sizeof(spdm_chunk_send_ack_response_t);
     186            1 :         libspdm_transport_test_encode_message(
     187              :             spdm_context, NULL, false, false,
     188              :             chunk_rsp_size, chunk_send_ack_rsp,
     189              :             response_size, response);
     190              : 
     191            1 :         return LIBSPDM_STATUS_SUCCESS;
     192              :     }
     193            6 :     if (spdm_test_context->case_id == 5) {
     194              :         /* Response has bad request response code */
     195              :         chunk_send_ack_rsp
     196            1 :             = (void*) ((uint8_t*) *response + sizeof(libspdm_test_message_header_t));
     197              : 
     198            1 :         chunk_send_ack_rsp->header.spdm_version = SPDM_MESSAGE_VERSION_12;
     199            1 :         chunk_send_ack_rsp->header.request_response_code = SPDM_ERROR; /* Bad response code */
     200            1 :         chunk_send_ack_rsp->header.param1 = 0;
     201            1 :         chunk_send_ack_rsp->header.param2 = 0;
     202              : 
     203            1 :         chunk_rsp_size = sizeof(spdm_chunk_send_ack_response_t);
     204            1 :         libspdm_transport_test_encode_message(
     205              :             spdm_context, NULL, false, false,
     206              :             chunk_rsp_size, chunk_send_ack_rsp,
     207              :             response_size, response);
     208              : 
     209            1 :         return LIBSPDM_STATUS_SUCCESS;
     210              :     }
     211            5 :     if (spdm_test_context->case_id == 6) {
     212              :         /* Response has bad response size */
     213              :         chunk_send_ack_rsp
     214            1 :             = (void*) ((uint8_t*) *response + sizeof(libspdm_test_message_header_t));
     215              : 
     216            1 :         chunk_send_ack_rsp->header.spdm_version = SPDM_MESSAGE_VERSION_12;
     217            1 :         chunk_send_ack_rsp->header.request_response_code = SPDM_CHUNK_SEND_ACK;
     218            1 :         chunk_send_ack_rsp->header.param1 = 0;
     219            1 :         chunk_send_ack_rsp->header.param2 = m_libspdm_chunk_send_chunk_handle;
     220            1 :         chunk_send_ack_rsp->chunk_seq_no = m_libspdm_chunk_send_chunk_seq_no;
     221              : 
     222            1 :         chunk_rsp_size = 4; /* Bad response size */
     223              : 
     224            1 :         libspdm_transport_test_encode_message(
     225              :             spdm_context, NULL, false, false,
     226              :             chunk_rsp_size, chunk_send_ack_rsp,
     227              :             response_size, response);
     228              : 
     229              : 
     230            1 :         return LIBSPDM_STATUS_SUCCESS;
     231              :     }
     232            4 :     if (spdm_test_context->case_id == 7) {
     233              :         /* Response has early error detected */
     234              :         chunk_send_ack_rsp
     235            1 :             = (void*) ((uint8_t*) *response + sizeof(libspdm_test_message_header_t));
     236              : 
     237            1 :         chunk_send_ack_rsp->header.spdm_version = SPDM_MESSAGE_VERSION_12;
     238            1 :         chunk_send_ack_rsp->header.request_response_code = SPDM_CHUNK_SEND_ACK;
     239              :         chunk_send_ack_rsp->header.param1
     240            1 :             = SPDM_CHUNK_SEND_ACK_RESPONSE_ATTRIBUTE_EARLY_ERROR_DETECTED;
     241            1 :         chunk_send_ack_rsp->header.param2 = m_libspdm_chunk_send_chunk_handle;
     242            1 :         chunk_send_ack_rsp->chunk_seq_no = m_libspdm_chunk_send_chunk_seq_no;
     243              : 
     244            1 :         error_response = (void*) (chunk_send_ack_rsp + 1);
     245            1 :         error_response->header.spdm_version = SPDM_MESSAGE_VERSION_12;
     246            1 :         error_response->header.request_response_code = SPDM_ERROR;
     247            1 :         error_response->header.param1 = SPDM_ERROR_CODE_UNSPECIFIED;
     248            1 :         error_response->header.param2 = 0;
     249              : 
     250            1 :         chunk_rsp_size = sizeof(spdm_chunk_send_ack_response_t) + sizeof(spdm_error_response_t);
     251            1 :         libspdm_transport_test_encode_message(
     252              :             spdm_context, NULL, false, false,
     253              :             chunk_rsp_size, chunk_send_ack_rsp,
     254              :             response_size, response);
     255              : 
     256            1 :         return LIBSPDM_STATUS_SUCCESS;
     257              :     }
     258            3 :     if (spdm_test_context->case_id == 8) {
     259              :         /* Response has bad chunk handle */
     260              :         chunk_send_ack_rsp
     261            1 :             = (void*) ((uint8_t*) *response + sizeof(libspdm_test_message_header_t));
     262              : 
     263            1 :         chunk_send_ack_rsp->header.spdm_version = SPDM_MESSAGE_VERSION_12;
     264            1 :         chunk_send_ack_rsp->header.request_response_code = SPDM_CHUNK_SEND_ACK;
     265            1 :         chunk_send_ack_rsp->header.param1 = 0;
     266              : 
     267              :         /* Bad chunk handle */
     268            1 :         chunk_send_ack_rsp->header.param2 = m_libspdm_chunk_send_chunk_handle - 1;
     269            1 :         chunk_send_ack_rsp->chunk_seq_no = m_libspdm_chunk_send_chunk_seq_no;
     270              : 
     271            1 :         chunk_rsp_size = sizeof(spdm_chunk_send_ack_response_t);
     272            1 :         libspdm_transport_test_encode_message(
     273              :             spdm_context, NULL, false, false,
     274              :             chunk_rsp_size, chunk_send_ack_rsp,
     275              :             response_size, response);
     276              : 
     277            1 :         return LIBSPDM_STATUS_SUCCESS;
     278              :     }
     279            2 :     if (spdm_test_context->case_id == 9) {
     280              :         /* Response has bad chunk seq no */
     281              :         chunk_send_ack_rsp
     282            1 :             = (void*) ((uint8_t*) *response + sizeof(libspdm_test_message_header_t));
     283              : 
     284            1 :         chunk_send_ack_rsp->header.spdm_version = SPDM_MESSAGE_VERSION_12;
     285            1 :         chunk_send_ack_rsp->header.request_response_code = SPDM_CHUNK_SEND_ACK;
     286            1 :         chunk_send_ack_rsp->header.param1 = 0;
     287            1 :         chunk_send_ack_rsp->header.param2 = m_libspdm_chunk_send_chunk_handle;
     288              : 
     289              :         /* Bad Chunk Seq No */
     290            1 :         chunk_send_ack_rsp->chunk_seq_no = m_libspdm_chunk_send_chunk_seq_no - 1;
     291              : 
     292            1 :         chunk_rsp_size = sizeof(spdm_chunk_send_ack_response_t);
     293            1 :         libspdm_transport_test_encode_message(
     294              :             spdm_context, NULL, false, false,
     295              :             chunk_rsp_size, chunk_send_ack_rsp,
     296              :             response_size, response);
     297              : 
     298            1 :         return LIBSPDM_STATUS_SUCCESS;
     299              :     }
     300            1 :     if (spdm_test_context->case_id == 12) {
     301              :         /* ErrorCode == LargeResponse shall not be allowed in ResponseToLargeRequest */
     302              :         chunk_send_ack_rsp
     303            1 :             = (void*) ((uint8_t*) *response + sizeof(libspdm_test_message_header_t));
     304              : 
     305            1 :         chunk_send_ack_rsp->header.spdm_version = SPDM_MESSAGE_VERSION_12;
     306            1 :         chunk_send_ack_rsp->header.request_response_code = SPDM_CHUNK_SEND_ACK;
     307              :         chunk_send_ack_rsp->header.param1
     308            1 :             = SPDM_CHUNK_SEND_ACK_RESPONSE_ATTRIBUTE_EARLY_ERROR_DETECTED;
     309            1 :         chunk_send_ack_rsp->header.param2 = m_libspdm_chunk_send_chunk_handle;
     310            1 :         chunk_send_ack_rsp->chunk_seq_no = m_libspdm_chunk_send_chunk_seq_no;
     311              : 
     312            1 :         error_response = (void*) (chunk_send_ack_rsp + 1);
     313            1 :         error_response->header.spdm_version = SPDM_MESSAGE_VERSION_12;
     314            1 :         error_response->header.request_response_code = SPDM_ERROR;
     315              : 
     316              :         /* ErrorCode == LargeResponse in ResponseToLargeRequest */
     317            1 :         error_response->header.param1 = SPDM_ERROR_CODE_LARGE_RESPONSE;
     318            1 :         error_response->header.param2 = 0;
     319            1 :         *((uint8_t*) (error_response + 1)) = 0;
     320              : 
     321            1 :         chunk_rsp_size = sizeof(spdm_chunk_send_ack_response_t) + sizeof(spdm_error_response_t) +
     322              :                          sizeof(uint8_t);
     323            1 :         libspdm_transport_test_encode_message(
     324              :             spdm_context, NULL, false, false,
     325              :             chunk_rsp_size, chunk_send_ack_rsp,
     326              :             response_size, response);
     327              : 
     328            1 :         return LIBSPDM_STATUS_SUCCESS;
     329              :     }
     330            0 :     if (spdm_test_context->case_id == 13) {
     331              :         /* Should never reach here since the test case is meant to fail before send */
     332            0 :         LIBSPDM_ASSERT(0);
     333              :     }
     334            0 :     return LIBSPDM_STATUS_RECEIVE_FAIL;
     335              : }
     336              : 
     337           12 : libspdm_return_t libspdm_test_requester_chunk_send_generic_test_case(
     338              :     void** state, uint32_t case_id)
     339              : {
     340              :     /* Copied from Neg. Algorithms test case 2 */
     341              :     libspdm_return_t status;
     342              :     libspdm_test_context_t* spdm_test_context;
     343              :     libspdm_context_t* spdm_context;
     344              : 
     345           12 :     spdm_test_context = *state;
     346           12 :     spdm_context = spdm_test_context->spdm_context;
     347           12 :     spdm_test_context->case_id = case_id;
     348           12 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_12 <<
     349              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     350           12 :     spdm_context->connection_info.capability.max_spdm_msg_size = LIBSPDM_MAX_SPDM_MSG_SIZE;
     351           12 :     spdm_context->connection_info.connection_state =
     352              :         LIBSPDM_CONNECTION_STATE_AFTER_CAPABILITIES;
     353           12 :     spdm_context->connection_info.capability.flags |=
     354              :         (SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP
     355              :          | SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHUNK_CAP);
     356           12 :     if (case_id != 10) {
     357              :         spdm_context->connection_info.capability.data_transfer_size
     358           11 :             = CHUNK_SEND_REQUESTER_UNIT_TEST_DATA_TRANSFER_SIZE;
     359              :         spdm_context->local_context.capability.sender_data_transfer_size
     360           11 :             = LIBSPDM_DATA_TRANSFER_SIZE;
     361              :     } else {
     362              :         spdm_context->connection_info.capability.data_transfer_size
     363            1 :             = LIBSPDM_DATA_TRANSFER_SIZE;
     364              :         spdm_context->local_context.capability.sender_data_transfer_size
     365            1 :             = CHUNK_SEND_REQUESTER_UNIT_TEST_DATA_TRANSFER_SIZE;
     366              :     }
     367              : 
     368           12 :     if (case_id == 11) {
     369            1 :         spdm_context->connection_info.capability.max_spdm_msg_size = 42;
     370              :     }
     371              : 
     372           12 :     spdm_context->local_context.capability.flags |=
     373              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CHUNK_CAP;
     374           12 :     spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP;
     375           12 :     spdm_context->local_context.algorithm.measurement_spec = SPDM_MEASUREMENT_SPECIFICATION_DMTF;
     376              : 
     377           12 :     spdm_context->local_context.algorithm.measurement_hash_algo =
     378              :         m_libspdm_use_measurement_hash_algo;
     379           12 :     spdm_context->local_context.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
     380           12 :     spdm_context->local_context.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
     381           12 :     spdm_context->local_context.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
     382           12 :     spdm_context->local_context.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
     383           12 :     spdm_context->local_context.algorithm.req_base_asym_alg = m_libspdm_use_req_asym_algo;
     384           12 :     spdm_context->local_context.algorithm.key_schedule = m_libspdm_use_key_schedule_algo;
     385           12 :     libspdm_reset_message_a(spdm_context);
     386              : 
     387           12 :     status = libspdm_negotiate_algorithms(spdm_context);
     388           12 :     return status;
     389              : }
     390              : 
     391            1 : libspdm_return_t libspdm_test_requester_chunk_send_vendor_specific_test_case(
     392              :     void** state, uint32_t case_id)
     393              : {
     394              :     /* Use vendor specific request to generate a large request. */
     395              :     libspdm_return_t status;
     396              :     libspdm_test_context_t* spdm_test_context;
     397              :     libspdm_context_t* spdm_context;
     398              : 
     399            1 :     uint16_t standard_id = 6;
     400            1 :     uint8_t vendor_id_len = 2;
     401            1 :     uint8_t vendor_id[SPDM_MAX_VENDOR_ID_LENGTH] = {0xAA, 0xAA};
     402            1 :     uint16_t data_len = 65535;
     403            1 :     uint8_t data[65535] = {0};
     404              : 
     405            1 :     spdm_test_context = *state;
     406            1 :     spdm_context = spdm_test_context->spdm_context;
     407            1 :     spdm_test_context->case_id = case_id;
     408            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_12 <<
     409              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     410              :     /* Large request need a large scratch buffer. */
     411            1 :     spdm_context->connection_info.capability.max_spdm_msg_size = 0x12000;
     412            1 :     spdm_context->local_context.capability.max_spdm_msg_size = 0x12000;
     413            1 :     spdm_context->connection_info.connection_state =
     414              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
     415            1 :     spdm_context->connection_info.capability.flags |=
     416              :         (SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP
     417              :          | SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHUNK_CAP);
     418              :     spdm_context->connection_info.capability.data_transfer_size
     419            1 :         = sizeof(spdm_chunk_send_request_t) + 1;
     420              :     spdm_context->local_context.capability.sender_data_transfer_size
     421            1 :         = CHUNK_SEND_REQUESTER_UNIT_TEST_DATA_TRANSFER_SIZE;
     422            1 :     spdm_context->local_context.is_requester = true;
     423              : 
     424            1 :     spdm_test_context->scratch_buffer_size =
     425            1 :         libspdm_get_sizeof_required_scratch_buffer(spdm_context);
     426            1 :     spdm_test_context->scratch_buffer = (void *)malloc(spdm_test_context->scratch_buffer_size);
     427            1 :     libspdm_set_scratch_buffer (spdm_context,
     428              :                                 spdm_test_context->scratch_buffer,
     429              :                                 spdm_test_context->scratch_buffer_size);
     430              : 
     431            1 :     libspdm_reset_message_a(spdm_context);
     432              : 
     433            1 :     status = libspdm_vendor_send_request_receive_response(spdm_context, NULL,
     434              :                                                           standard_id, vendor_id_len, vendor_id,
     435              :                                                           data_len, data,
     436              :                                                           &standard_id, &vendor_id_len, vendor_id,
     437              :                                                           &data_len, data);
     438            1 :     return status;
     439              : }
     440              : 
     441            1 : void libspdm_test_requester_chunk_send_case1(void** state)
     442              : {
     443              :     libspdm_return_t status;
     444              : 
     445            1 :     status = libspdm_test_requester_chunk_send_generic_test_case(state, 1);
     446            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     447            1 : }
     448              : 
     449            1 : void libspdm_test_requester_chunk_send_case2(void** state)
     450              : {
     451              :     libspdm_return_t status;
     452            1 :     status = libspdm_test_requester_chunk_send_generic_test_case(state, 2);
     453            1 :     assert_int_equal(status, LIBSPDM_STATUS_SEND_FAIL);
     454            1 : }
     455              : 
     456            1 : void libspdm_test_requester_chunk_send_case3(void** state)
     457              : {
     458              :     libspdm_return_t status;
     459            1 :     status = libspdm_test_requester_chunk_send_generic_test_case(state, 3);
     460            1 :     assert_int_equal(status, LIBSPDM_STATUS_RECEIVE_FAIL);
     461            1 : }
     462              : 
     463            1 : void libspdm_test_requester_chunk_send_case4(void** state)
     464              : {
     465              :     libspdm_return_t status;
     466            1 :     status = libspdm_test_requester_chunk_send_generic_test_case(state, 4);
     467            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
     468            1 : }
     469              : 
     470            1 : void libspdm_test_requester_chunk_send_case5(void** state)
     471              : {
     472              :     libspdm_return_t status;
     473            1 :     status = libspdm_test_requester_chunk_send_generic_test_case(state, 5);
     474            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
     475            1 : }
     476              : 
     477            1 : void libspdm_test_requester_chunk_send_case6(void** state)
     478              : {
     479              :     libspdm_return_t status;
     480            1 :     status = libspdm_test_requester_chunk_send_generic_test_case(state, 6);
     481            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_SIZE);
     482            1 : }
     483              : 
     484            1 : void libspdm_test_requester_chunk_send_case7(void** state)
     485              : {
     486              :     libspdm_return_t status;
     487            1 :     status = libspdm_test_requester_chunk_send_generic_test_case(state, 7);
     488            1 :     assert_int_equal(status, LIBSPDM_STATUS_ERROR_PEER);
     489            1 : }
     490              : 
     491            1 : void libspdm_test_requester_chunk_send_case8(void** state)
     492              : {
     493              :     libspdm_return_t status;
     494            1 :     status = libspdm_test_requester_chunk_send_generic_test_case(state, 8);
     495            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
     496            1 : }
     497              : 
     498            1 : void libspdm_test_requester_chunk_send_case9(void** state)
     499              : {
     500              :     libspdm_return_t status;
     501            1 :     status = libspdm_test_requester_chunk_send_generic_test_case(state, 9);
     502            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
     503            1 : }
     504              : 
     505            1 : void libspdm_test_requester_chunk_send_case10(void** state)
     506              : {
     507              :     libspdm_return_t status;
     508            1 :     status = libspdm_test_requester_chunk_send_generic_test_case(state, 10);
     509            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     510            1 : }
     511              : 
     512            1 : void libspdm_test_requester_chunk_send_case11(void** state)
     513              : {
     514              :     libspdm_return_t status;
     515              : 
     516            1 :     status = libspdm_test_requester_chunk_send_generic_test_case(state, 11);
     517            1 :     assert_int_equal(status, LIBSPDM_STATUS_PEER_BUFFER_TOO_SMALL);
     518            1 : }
     519              : 
     520              : /**
     521              :  * Test 12: ErrorCode == LargeResponse shall not be allowed in ResponseToLargeRequest.
     522              :  * Expected behavior: returns a status of LIBSPDM_STATUS_ERROR_PEER,
     523              :  * Received an unexpected error message.
     524              :  **/
     525            1 : void libspdm_test_requester_chunk_send_case12(void** state)
     526              : {
     527              :     libspdm_return_t status;
     528              : 
     529            1 :     status = libspdm_test_requester_chunk_send_generic_test_case(state, 12);
     530            1 :     assert_int_equal(status, LIBSPDM_STATUS_ERROR_PEER);
     531            1 : }
     532              : 
     533              : #if LIBSPDM_ENABLE_VENDOR_DEFINED_MESSAGES
     534              : /**
     535              :  * Test 13: Request size shall not exceed max supported transfer size.
     536              :  * Expected behavior: returns a status of LIBSPDM_STATUS_SEND_FAIL,
     537              :  **/
     538            1 : void libspdm_test_requester_chunk_send_case13(void** state)
     539              : {
     540              :     libspdm_return_t status;
     541              : 
     542            1 :     status = libspdm_test_requester_chunk_send_vendor_specific_test_case(state, 13);
     543            1 :     assert_int_equal(status, LIBSPDM_STATUS_SEND_FAIL);
     544            1 : }
     545              : #endif
     546              : 
     547            1 : int libspdm_requester_chunk_send_test_main(void)
     548              : {
     549              :     /* Test the CHUNK_SEND handlers in various requester handlers */
     550            1 :     const struct CMUnitTest spdm_requester_chunk_send_tests[] = {
     551              :         /* Request Algorithms successfully sent in chunks. */
     552              :         cmocka_unit_test(libspdm_test_requester_chunk_send_case1),
     553              :         /* Chunk Request fail send */
     554              :         cmocka_unit_test(libspdm_test_requester_chunk_send_case2),
     555              :         /* Chunk Response fail receive */
     556              :         cmocka_unit_test(libspdm_test_requester_chunk_send_case3),
     557              :         /* Chunk Response has bad SPDM version */
     558              :         cmocka_unit_test(libspdm_test_requester_chunk_send_case4),
     559              :         /* Chunk Response has bad request response code */
     560              :         cmocka_unit_test(libspdm_test_requester_chunk_send_case5),
     561              :         /* Chunk Response has bad response size */
     562              :         cmocka_unit_test(libspdm_test_requester_chunk_send_case6),
     563              :         /* Chunk Response has early error detected */
     564              :         cmocka_unit_test(libspdm_test_requester_chunk_send_case7),
     565              :         /* Chunk Response has bad chunk handle */
     566              :         cmocka_unit_test(libspdm_test_requester_chunk_send_case8),
     567              :         /* Chunk Response has bad chunk seq no */
     568              :         cmocka_unit_test(libspdm_test_requester_chunk_send_case9),
     569              :         /* sent in chunks due to greater than the sending transmit buffer size. */
     570              :         cmocka_unit_test(libspdm_test_requester_chunk_send_case10),
     571              :         /* requester message size greater than the responder max_spdm_msg_size, return LIBSPDM_STATUS_PEER_BUFFER_TOO_SMALL */
     572              :         cmocka_unit_test(libspdm_test_requester_chunk_send_case11),
     573              :         /* ErrorCode == LargeResponse shall not be allowed in ResponseToLargeRequest */
     574              :         cmocka_unit_test(libspdm_test_requester_chunk_send_case12),
     575              : #if LIBSPDM_ENABLE_VENDOR_DEFINED_MESSAGES
     576              :         /* Request size exceed max chunks */
     577              :         cmocka_unit_test(libspdm_test_requester_chunk_send_case13),
     578              : #endif
     579              :     };
     580              : 
     581            1 :     libspdm_test_context_t test_context = {
     582              :         LIBSPDM_TEST_CONTEXT_VERSION,
     583              :         true,
     584              :         libspdm_requester_chunk_send_test_send_message,
     585              :         libspdm_requester_chunk_send_test_receive_message,
     586              :     };
     587              : 
     588            1 :     libspdm_setup_test_context(&test_context);
     589              : 
     590            1 :     return cmocka_run_group_tests(spdm_requester_chunk_send_tests,
     591              :                                   libspdm_unit_test_group_setup,
     592              :                                   libspdm_unit_test_group_teardown);
     593              : }
     594              : 
     595              : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP*/
        

Generated by: LCOV version 2.0-1