UnitTest/TestSpdmResponder/TestSpdmResponderCertificate.c
/**
  Test 8: request Offset at the boundary of maximum integer values, while keeping Length 0
  Expected Behavior: generate correctly formed Certficate messages, including its PortionLength and RemainderLength fields
**/
Test 8: request Offset at the boundary of maximum integer values, while keeping Length 0
Expected Behavior: generate correctly formed Certficate messages, including its PortionLength and RemainderLength fields
Test 8 : request Offset at the boundary of maximum integer values while keeping Length 0 Expected Behavior : generate correctly formed Certficate messages including its PortionLength and RemainderLength fields
void TestSpdmResponderCertificateCase8(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;
  SPDM_ERROR_RESPONSE  *SpdmResponseError;
  VOID                 *Data;
  UINTN                DataSize;
  UINT16               TestOffsets[] = {(UINT16)(-1), 0, +1, 0, MAX_INT8, (UINT16)(MAX_INT8+1), MAX_UINT8, MAX_INT16, (UINT16)(MAX_INT16+1), MAX_UINT16, (UINT16)(-1)};
  SpdmTestContext = *state;
  SpdmContext = SpdmTestContext->SpdmContext;
  SpdmTestContext->CaseId = 0x8;
  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.Length = 0;
  TestOffsets[0] = (UINT16)(TestOffsets[0] + DataSize);
  TestOffsets[1] = (UINT16)(TestOffsets[1] + DataSize);
  TestOffsets[2] = (UINT16)(TestOffsets[2] + DataSize);
  for(int i=0; i<sizeof(TestOffsets)/sizeof(TestOffsets[0]); i++) {
    TEST_DEBUG_PRINT("i:%d TestOffsets[i]:%u\n",i, TestOffsets[i]);
    mSpdmGetCertificateRequest3.Offset = TestOffsets[i];
    ResetManagedBuffer (&SpdmContext->Transcript.MessageB);
    ResponseSize = sizeof(Response);
    Status = SpdmGetResponseCertificate (SpdmContext, mSpdmGetCertificateRequest3Size, &mSpdmGetCertificateRequest3, &ResponseSize, Response);
    assert_int_equal (Status, RETURN_SUCCESS);
    if(mSpdmGetCertificateRequest3.Offset >= DataSize) {
      SpdmResponseError = (VOID *)Response;
      assert_int_equal (SpdmResponseError->Header.RequestResponseCode, SPDM_ERROR);
      assert_int_equal (SpdmResponseError->Header.Param1, SPDM_ERROR_CODE_INVALID_REQUEST);
    } else {
      assert_int_equal (ResponseSize, sizeof(SPDM_CERTIFICATE_RESPONSE));
      SpdmResponse = (VOID *)Response;
      assert_int_equal (SpdmResponse->Header.RequestResponseCode, SPDM_CERTIFICATE);
      assert_int_equal (SpdmResponse->Header.Param1, 0);
      assert_int_equal (SpdmResponse->PortionLength, 0);
      assert_int_equal (SpdmResponse->RemainderLength, (UINT16)(DataSize - mSpdmGetCertificateRequest3.Offset));
    }
  }
  free(Data);
}
void TestSpdmResponderCertificateCase8 ( 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 ; SPDM_ERROR_RESPONSE * SpdmResponseError ; VOID * Data ; UINTN DataSize ; UINT16 TestOffsets [ ] = { ( UINT16 ) ( -1 ) , 0 , +1 , 0 , MAX_INT8 , ( UINT16 ) ( MAX_INT8 + 1 ) , MAX_UINT8 , MAX_INT16 , ( UINT16 ) ( MAX_INT16 + 1 ) , MAX_UINT16 , ( UINT16 ) ( -1 ) } ; SpdmTestContext = * state ; SpdmContext = SpdmTestContext -> SpdmContext ; SpdmTestContext -> CaseId = 0x8 ; 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 . Length = 0 ; TestOffsets [ 0 ] = ( UINT16 ) ( TestOffsets [ 0 ] + DataSize ) ; TestOffsets [ 1 ] = ( UINT16 ) ( TestOffsets [ 1 ] + DataSize ) ; TestOffsets [ 2 ] = ( UINT16 ) ( TestOffsets [ 2 ] + DataSize ) ; for ( int i = 0 ; i < sizeof ( TestOffsets ) / sizeof ( TestOffsets [ 0 ] ) ; i ++ ) { TEST_DEBUG_PRINT ( " \n " , i , TestOffsets [ i ] ) ; mSpdmGetCertificateRequest3 . Offset = TestOffsets [ i ] ; ResetManagedBuffer ( & SpdmContext -> Transcript . MessageB ) ; ResponseSize = sizeof ( Response ) ; Status = SpdmGetResponseCertificate ( SpdmContext , mSpdmGetCertificateRequest3Size , & mSpdmGetCertificateRequest3 , & ResponseSize , Response ) ; assert_int_equal ( Status , RETURN_SUCCESS ) ; if ( mSpdmGetCertificateRequest3 . Offset >= DataSize ) { SpdmResponseError = ( VOID * ) Response ; assert_int_equal ( SpdmResponseError -> Header . RequestResponseCode , SPDM_ERROR ) ; assert_int_equal ( SpdmResponseError -> Header . Param1 , SPDM_ERROR_CODE_INVALID_REQUEST ) ; } else { assert_int_equal ( ResponseSize , sizeof ( SPDM_CERTIFICATE_RESPONSE ) ) ; SpdmResponse = ( VOID * ) Response ; assert_int_equal ( SpdmResponse -> Header . RequestResponseCode , SPDM_CERTIFICATE ) ; assert_int_equal ( SpdmResponse -> Header . Param1 , 0 ) ; assert_int_equal ( SpdmResponse -> PortionLength , 0 ) ; assert_int_equal ( SpdmResponse -> RemainderLength , ( UINT16 ) ( DataSize - mSpdmGetCertificateRequest3 . Offset ) ) ; } } free ( Data ) ; }
Test 8: request Offset at the boundary of maximum integer values, while keeping Length 0
Expected Behavior: generate correctly formed Certficate messages, including its PortionLength and RemainderLength fields
Test 8 : request Offset at the boundary of maximum integer values while keeping Length 0 Expected Behavior : generate correctly formed Certficate messages including its PortionLength and RemainderLength fields
// Testing Offsets at the boundary of maximum integer values and at the boundary of certificate length (first three positions) // Setting up the SpdmContext and loading a sample certificate chain // This tests considers only length = 0, other tests vary length value // Setting up offset values at the boundary of certificate length // reseting an internal buffer to avoid overflow and prevent tests to succeed // A too long of an offset should return an error // Otherwise it should work properly, considering Length = 0