UnitTest/TestSpdmResponder/TestSpdmResponderCertificate.c
/**
  Test 7: request Length at the boundary of maximum integer values, while keeping offset 0
  Expected Behavior: generate correctly formed Certficate messages, including its PortionLength and RemainderLength fields
**/
Test 7: request Length at the boundary of maximum integer values, while keeping offset 0
Expected Behavior: generate correctly formed Certficate messages, including its PortionLength and RemainderLength fields
Test 7 : request Length at the boundary of maximum integer values while keeping offset 0 Expected Behavior : generate correctly formed Certficate messages including its PortionLength and RemainderLength fields
void TestSpdmResponderCertificateCase7(void **state) {
  RETURN_STATUS        Status;
  SPDM_TEST_CONTEXT    *SpdmTestContext;
  SPDM_DEVICE_CONTEXT  *SpdmContext;
  UINTN                ResponseSize;
  UINT8                Response[MAX_SPDM_MESSAGE_BUFFER_SIZE];
  SPDM_CERTIFICATE_RESPONSE *SpdmResponse;
  VOID                 *Data;
  UINTN                DataSize;
  UINT16               TestLenghts[] = {0, MAX_INT8, (UINT16)(MAX_INT8+1), MAX_UINT8, MAX_INT16, (UINT16)(MAX_INT16+1), MAX_UINT16, (UINT16)(-1)};
  UINT16               ExpectedChunkSize;
  SpdmTestContext = *state;
  SpdmContext = SpdmTestContext->SpdmContext;
  SpdmTestContext->CaseId = 0x7;
  SpdmContext->ConnectionInfo.ConnectionState = SpdmConnectionStateAfterDigests;
  SpdmContext->LocalContext.Capability.Flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
  ReadResponderPublicCertificateChain (mUseHashAlgo, mUseAsymAlgo, &Data, &DataSize, NULL, NULL);
  SpdmContext->LocalContext.LocalCertChainProvision[0] = Data;
  SpdmContext->LocalContext.LocalCertChainProvisionSize[0] = DataSize;
  SpdmContext->LocalContext.SlotCount = 1;
  mSpdmGetCertificateRequest3.Offset = 0;
  for(int i=0; i<sizeof(TestLenghts)/sizeof(TestLenghts[0]); i++) {
    TEST_DEBUG_PRINT("i:%d TestLenghts[i]:%u\n",i, TestLenghts[i]);
    mSpdmGetCertificateRequest3.Length = TestLenghts[i];
    ExpectedChunkSize = MIN(mSpdmGetCertificateRequest3.Length, MAX_SPDM_CERT_CHAIN_BLOCK_LEN);
    ResetManagedBuffer (&SpdmContext->Transcript.MessageB);
    ResponseSize = sizeof(Response);
    Status = SpdmGetResponseCertificate (SpdmContext, mSpdmGetCertificateRequest3Size, &mSpdmGetCertificateRequest3, &ResponseSize, Response);
    assert_int_equal (Status, RETURN_SUCCESS);
    assert_int_equal (ResponseSize, sizeof(SPDM_CERTIFICATE_RESPONSE) + ExpectedChunkSize);
    SpdmResponse = (VOID *)Response;
    assert_int_equal (SpdmResponse->Header.RequestResponseCode, SPDM_CERTIFICATE);
    assert_int_equal (SpdmResponse->Header.Param1, 0);
    assert_int_equal (SpdmResponse->PortionLength, ExpectedChunkSize);
    assert_int_equal (SpdmResponse->RemainderLength, DataSize - ExpectedChunkSize);
  }
  free(Data);
}
void TestSpdmResponderCertificateCase7 ( void * * state ) { RETURN_STATUS Status ; SPDM_TEST_CONTEXT * SpdmTestContext ; SPDM_DEVICE_CONTEXT * SpdmContext ; UINTN ResponseSize ; UINT8 Response [ MAX_SPDM_MESSAGE_BUFFER_SIZE ] ; SPDM_CERTIFICATE_RESPONSE * SpdmResponse ; VOID * Data ; UINTN DataSize ; UINT16 TestLenghts [ ] = { 0 , MAX_INT8 , ( UINT16 ) ( MAX_INT8 + 1 ) , MAX_UINT8 , MAX_INT16 , ( UINT16 ) ( MAX_INT16 + 1 ) , MAX_UINT16 , ( UINT16 ) ( -1 ) } ; UINT16 ExpectedChunkSize ; SpdmTestContext = * state ; SpdmContext = SpdmTestContext -> SpdmContext ; SpdmTestContext -> CaseId = 0x7 ; SpdmContext -> ConnectionInfo . ConnectionState = SpdmConnectionStateAfterDigests ; SpdmContext -> LocalContext . Capability . Flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP ; ReadResponderPublicCertificateChain ( mUseHashAlgo , mUseAsymAlgo , & Data , & DataSize , NULL , NULL ) ; SpdmContext -> LocalContext . LocalCertChainProvision [ 0 ] = Data ; SpdmContext -> LocalContext . LocalCertChainProvisionSize [ 0 ] = DataSize ; SpdmContext -> LocalContext . SlotCount = 1 ; mSpdmGetCertificateRequest3 . Offset = 0 ; for ( int i = 0 ; i < sizeof ( TestLenghts ) / sizeof ( TestLenghts [ 0 ] ) ; i ++ ) { TEST_DEBUG_PRINT ( " \n " , i , TestLenghts [ i ] ) ; mSpdmGetCertificateRequest3 . Length = TestLenghts [ i ] ; ExpectedChunkSize = MIN ( mSpdmGetCertificateRequest3 . Length , MAX_SPDM_CERT_CHAIN_BLOCK_LEN ) ; ResetManagedBuffer ( & SpdmContext -> Transcript . MessageB ) ; ResponseSize = sizeof ( Response ) ; Status = SpdmGetResponseCertificate ( SpdmContext , mSpdmGetCertificateRequest3Size , & mSpdmGetCertificateRequest3 , & ResponseSize , Response ) ; assert_int_equal ( Status , RETURN_SUCCESS ) ; assert_int_equal ( ResponseSize , sizeof ( SPDM_CERTIFICATE_RESPONSE ) + ExpectedChunkSize ) ; SpdmResponse = ( VOID * ) Response ; assert_int_equal ( SpdmResponse -> Header . RequestResponseCode , SPDM_CERTIFICATE ) ; assert_int_equal ( SpdmResponse -> Header . Param1 , 0 ) ; assert_int_equal ( SpdmResponse -> PortionLength , ExpectedChunkSize ) ; assert_int_equal ( SpdmResponse -> RemainderLength , DataSize - ExpectedChunkSize ) ; } free ( Data ) ; }
Test 7: request Length at the boundary of maximum integer values, while keeping offset 0
Expected Behavior: generate correctly formed Certficate messages, including its PortionLength and RemainderLength fields
Test 7 : request Length at the boundary of maximum integer values while keeping offset 0 Expected Behavior : generate correctly formed Certficate messages including its PortionLength and RemainderLength fields
// Testing Lengths at the boundary of maximum integer values // Setting up the SpdmContext and loading a sample certificate chain // This tests considers only offset = 0, other tests vary offset value // Expected received length is limited by MAX_SPDM_CERT_CHAIN_BLOCK_LEN (implementation specific?) // reseting an internal buffer to avoid overflow and prevent tests to succeed