From 6d00a8be61cde0221ad5bf488c839bf5db227bfe Mon Sep 17 00:00:00 2001 From: Tiangang Song Date: Wed, 2 Dec 2020 22:38:30 -0800 Subject: [PATCH] Update error codes in OTA pal and unit tests --- .../ota_pal/source/include/ota_pal_posix.h | 109 +++--- platform/posix/ota_pal/source/ota_pal_posix.c | 149 ++++----- .../posix/ota_pal/utest/ota_pal_posix_utest.c | 315 +++++++++--------- 3 files changed, 287 insertions(+), 286 deletions(-) diff --git a/platform/posix/ota_pal/source/include/ota_pal_posix.h b/platform/posix/ota_pal/source/include/ota_pal_posix.h index 0cde475371..e1d4e5baa6 100644 --- a/platform/posix/ota_pal/source/include/ota_pal_posix.h +++ b/platform/posix/ota_pal/source/include/ota_pal_posix.h @@ -1,5 +1,5 @@ /* - * FreeRTOS OTA V1.2.0 + * FreeRTOS OTA V2.0.0 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy of @@ -46,94 +46,95 @@ static const char signingcredentialSIGNING_CERTIFICATE_PEM[] = "Paste code signi /** * @brief Abort an OTA transfer. * - * Aborts access to an existing open file represented by the OTA file context C. This is only valid - * for jobs that started successfully. + * Aborts access to an existing open file represented by the OTA file context pFileContext. This is + * only valid for jobs that started successfully. * - * @note The input OtaFileContext_t C is checked for NULL by the OTA agent before this + * @note The input OtaFileContext_t pFileContext is checked for NULL by the OTA agent before this * function is called. - * This function may be called before the file is opened, so the file pointer C->fileHandle may be NULL - * when this function is called. + * This function may be called before the file is opened, so the file pointer pFileContext->fileHandle + * may be NULL when this function is called. * - * @param[in] C OTA file context information. + * @param[in] pFileContext OTA file context information. * * @return The OTA PAL layer error code combined with the MCU specific error code. See OTA Agent * error codes information in ota.h. * * The file pointer will be set to NULL after this function returns. - * OTA_ERR_NONE is returned when aborting access to the open file was successful. - * OTA_ERR_FILE_ABORT is returned when aborting access to the open file context was unsuccessful. + * OtaPalSuccess is returned when aborting access to the open file was successful. + * OtaPalFileAbort is returned when aborting access to the open file context was unsuccessful. */ -OtaErr_t otaPal_Abort( OtaFileContext_t * const C ); +OtaPalStatus_t otaPal_Abort( OtaFileContext_t * const pFileContext ); /** * @brief Create a new receive file for the data chunks as they come in. * * @note Opens the file indicated in the OTA file context in the MCU file system. * - * @note The previous image may be present in the designated image download partition or file, so the partition or file - * must be completely erased or overwritten in this routine. + * @note The previous image may be present in the designated image download partition or file, so the + * partition or file must be completely erased or overwritten in this routine. * - * @note The input OtaFileContext_t C is checked for NULL by the OTA agent before this + * @note The input OtaFileContext_t pFileContext is checked for NULL by the OTA agent before this * function is called. - * The device file path is a required field in the OTA job document, so C->pFilePath is + * The device file path is a required field in the OTA job document, so pFileContext->pFilePath is * checked for NULL by the OTA agent before this function is called. * - * @param[in] C OTA file context information. + * @param[in] pFileContext OTA file context information. * * @return The OTA PAL layer error code combined with the MCU specific error code. See OTA Agent * error codes information in ota.h. * - * OTA_ERR_NONE is returned when file creation is successful. - * OTA_ERR_RX_FILE_TOO_LARGE is returned if the file to be created exceeds the device's non-volatile memory size constraints. - * OTA_ERR_BOOT_INFO_CREATE_FAILED is returned if the bootloader information file creation fails. - * OTA_ERR_RX_FILE_CREATE_FAILED is returned for other errors creating the file in the device's non-volatile memory. + * OtaPalSuccess is returned when file creation is successful. + * OtaPalRxFileTooLarge is returned if the file to be created exceeds the device's non-volatile memory size constraints. + * OtaPalBootInfoCreateFailed is returned if the bootloader information file creation fails. + * OtaPalRxFileCreateFailed is returned for other errors creating the file in the device's non-volatile memory. */ -OtaErr_t otaPal_CreateFileForRx( OtaFileContext_t * const C ); +OtaPalStatus_t otaPal_CreateFileForRx( OtaFileContext_t * const pFileContext ); /* @brief Authenticate and close the underlying receive file in the specified OTA context. * - * @note The input OtaFileContext_t C is checked for NULL by the OTA agent before this + * @note The input OtaFileContext_t pFileContext is checked for NULL by the OTA agent before this * function is called. This function is called only at the end of block ingestion. * otaPal_CreateFileForRx() must succeed before this function is reached, so - * C->fileHandle(or C->pFile) is never NULL. + * pFileContext->fileHandle(or pFileContext->pFile) is never NULL. * The certificate path on the device is a required job document field in the OTA Agent, - * so C->pCertFilepath is never NULL. - * The file signature key is required job document field in the OTA Agent, so C->pSignature will + * so pFileContext->pCertFilepath is never NULL. + * The file signature key is required job document field in the OTA Agent, so pFileContext->pSignature will * never be NULL. * * If the signature verification fails, file close should still be attempted. * - * @param[in] C OTA file context information. + * @param[in] pFileContext OTA file context information. * * @return The OTA PAL layer error code combined with the MCU specific error code. See OTA Agent * error codes information in ota.h. * - * OTA_ERR_NONE is returned on success. - * OTA_ERR_SIGNATURE_CHECK_FAILED is returned when cryptographic signature verification fails. - * OTA_ERR_BAD_SIGNER_CERT is returned for errors in the certificate itself. - * OTA_ERR_FILE_CLOSE is returned when closing the file fails. + * OtaPalSuccess is returned on success. + * OtaPalSignatureCheckFailed is returned when cryptographic signature verification fails. + * OtaPalBadSignerCert is returned for errors in the certificate itself. + * OtaPalFileClose is returned when closing the file fails. */ -OtaErr_t otaPal_CloseFile( OtaFileContext_t * const C ); +OtaPalStatus_t otaPal_CloseFile( OtaFileContext_t * const pFileContext ); /** * @brief Write a block of data to the specified file at the given offset. * - * @note The input OtaFileContext_t C is checked for NULL by the OTA agent before this + * @note The input OtaFileContext_t pFileContext is checked for NULL by the OTA agent before this * function is called. - * The file pointer/handle C->pFile, is checked for NULL by the OTA agent before this + * The file pointer/handle pFileContext->pFile, is checked for NULL by the OTA agent before this * function is called. - * pcData is checked for NULL by the OTA agent before this function is called. - * ulBlockSize is validated for range by the OTA agent before this function is called. - * ulBlockIndex is validated by the OTA agent before this function is called. + * pData is checked for NULL by the OTA agent before this function is called. + * blockSize is validated for range by the OTA agent before this function is called. + * offset is validated by the OTA agent before this function is called. * - * @param[in] C OTA file context information. - * @param[in] ulOffset Byte offset to write to from the beginning of the file. - * @param[in] pcData Pointer to the byte array of data to write. - * @param[in] ulBlockSize The number of bytes to write. + * @param[in] pFileContext OTA file context information. + * @param[in] offset Byte offset to write to from the beginning of the file. + * @param[in] pData Pointer to the byte array of data to write. + * @param[in] blockSize The number of bytes to write. * - * @return The number of bytes written on a success, or a negative error code from the platform abstraction layer. + * @return The number of bytes written on a success, or a negative error code from the platform + * abstraction layer. */ -int16_t otaPal_WriteBlock( OtaFileContext_t * const C, +int16_t otaPal_WriteBlock( OtaFileContext_t * const pFileContext, uint32_t ulOffset, uint8_t * const pcData, uint32_t ulBlockSize ); @@ -150,7 +151,7 @@ int16_t otaPal_WriteBlock( OtaFileContext_t * const C, * @return The OTA PAL layer error code combined with the MCU specific error code. See OTA Agent * error codes information in ota.h. */ -OtaErr_t otaPal_ActivateNewImage( OtaFileContext_t * const C ); +OtaPalStatus_t otaPal_ActivateNewImage( OtaFileContext_t * const pFileContext ); /** * @brief Reset the device. @@ -164,7 +165,7 @@ OtaErr_t otaPal_ActivateNewImage( OtaFileContext_t * const C ); * error codes information in ota.h. */ -OtaErr_t otaPal_ResetDevice( OtaFileContext_t * const C ); +OtaPalStatus_t otaPal_ResetDevice( OtaFileContext_t * const pFileContext ); /** * @brief Attempt to set the state of the OTA update image. @@ -172,21 +173,23 @@ OtaErr_t otaPal_ResetDevice( OtaFileContext_t * const C ); * Do whatever is required by the platform to Accept/Reject the OTA update image (or bundle). * Refer to the PAL implementation to determine what happens on your platform. * + * @param[in] pFileContext File context of type OtaFileContext_t. + * * @param[in] eState The desired state of the OTA update image. * - * @return The OtaErr_t error code combined with the MCU specific error code. See ota.h for + * @return The OtaPalStatus_t error code combined with the MCU specific error code. See ota.h for * OTA major error codes and your specific PAL implementation for the sub error code. * * Major error codes returned are: * - * OTA_ERR_NONE on success. - * OTA_ERR_BAD_IMAGE_STATE: if you specify an invalid OtaImageState_t. No sub error code. - * OTA_ERR_ABORT_FAILED: failed to roll back the update image as requested by OtaImageStateAborted. - * OTA_ERR_REJECT_FAILED: failed to roll back the update image as requested by OtaImageStateRejected. - * OTA_ERR_COMMIT_FAILED: failed to make the update image permanent as requested by OtaImageStateAccepted. + * OtaPalSuccess on success. + * OtaPalBadImageState: if you specify an invalid OtaImageState_t. No sub error code. + * OtaPalAbortFailed: failed to roll back the update image as requested by OtaImageStateAborted. + * OtaPalRejectFailed: failed to roll back the update image as requested by OtaImageStateRejected. + * OtaPalCommitFailed: failed to make the update image permanent as requested by OtaImageStateAccepted. */ -OtaErr_t otaPal_SetPlatformImageState( OtaFileContext_t * const C, - OtaImageState_t eState ); +OtaPalStatus_t otaPal_SetPlatformImageState( OtaFileContext_t * const pFileContext, + OtaImageState_t eState ); /** * @brief Get the state of the OTA update image. @@ -202,6 +205,8 @@ OtaErr_t otaPal_SetPlatformImageState( OtaFileContext_t * const C, * If the update image state is not in "pending commit," the self test timer is * not started. * + * @param[in] pFileContext File context of type OtaFileContext_t. + * * @return An OtaPalImageState_t. One of the following: * OtaPalImageStatePendingCommit (the new firmware image is in the self test phase) * OtaPalImageStateValid (the new firmware image is already committed) @@ -209,6 +214,6 @@ OtaErr_t otaPal_SetPlatformImageState( OtaFileContext_t * const C, * * NOTE: OtaPalImageStateUnknown should NEVER be returned and indicates an implementation error. */ -OtaPalImageState_t otaPal_GetPlatformImageState( OtaFileContext_t * const C ); +OtaPalImageState_t otaPal_GetPlatformImageState( OtaFileContext_t * const pFileContext ); #endif /* ifndef _OTA_PAL_H_ */ diff --git a/platform/posix/ota_pal/source/ota_pal_posix.c b/platform/posix/ota_pal/source/ota_pal_posix.c index a8cd138a48..97c512376b 100644 --- a/platform/posix/ota_pal/source/ota_pal_posix.c +++ b/platform/posix/ota_pal/source/ota_pal_posix.c @@ -44,14 +44,10 @@ /* Specify the OTA signature algorithm we support on this platform. */ const char OTA_JsonFileSignatureKey[ OTA_FILE_SIG_KEY_STR_MAX_LENGTH ] = "sig-sha256-ecdsa"; -static OtaErr_t otaPal_CheckFileSignature( OtaFileContext_t * const C ); - - +static OtaPalStatus_t otaPal_CheckFileSignature( OtaFileContext_t * const C ); /* Read the specified signer certificate from the filesystem into a local buffer. The allocated - * memory becomes the property of the caller who is responsible for freeing it. - */ - + * memory becomes the property of the caller who is responsible for freeing it. */ static EVP_PKEY * Openssl_GetPkeyFromCertificate( uint8_t * pCertFilePath ) { BIO * pBio = NULL; @@ -132,10 +128,9 @@ static EVP_PKEY * Openssl_GetPkeyFromCertificate( uint8_t * pCertFilePath ) /* Attempt to create a new receive file for the file chunks as they come in. */ - -OtaErr_t otaPal_CreateFileForRx( OtaFileContext_t * const C ) +OtaPalStatus_t otaPal_CreateFileForRx( OtaFileContext_t * const C ) { - OtaErr_t result = OTA_ERR_UNINITIALIZED; /* For MISRA mandatory. */ + OtaPalStatus_t result = OTA_PAL_COMBINE_ERR( OtaPalUninitialized, 0 ); if( C != NULL ) { @@ -148,40 +143,38 @@ OtaErr_t otaPal_CreateFileForRx( OtaFileContext_t * const C ) if( C->pFile != NULL ) { - result = OTA_ERR_NONE; + result = OTA_PAL_COMBINE_ERR( OtaPalSuccess, 0 ); LogInfo( ( "Receive file created." ) ); } else { - result = ( OTA_ERR_RX_FILE_CREATE_FAILED | ( ( uint32_t ) errno & ( uint32_t ) OTA_PAL_ERR_MASK ) ); /*lint !e40 !e737 !e9027 !e9029 - * Errno is being used in accordance with host API documentation. - * Bitmasking is being used to preserve host API error with library status code. */ + result = OTA_PAL_COMBINE_ERR( OtaPalRxFileCreateFailed, errno ); LogError( ( "Failed to start operation: Operation already started." ) ); } } else { - result = OTA_ERR_RX_FILE_CREATE_FAILED; + result = OTA_PAL_COMBINE_ERR( OtaPalRxFileCreateFailed, 0 ); LogError( ( "Invalid file path provided." ) ); } } else { - result = OTA_ERR_RX_FILE_CREATE_FAILED; + result = OTA_PAL_COMBINE_ERR( OtaPalRxFileCreateFailed, 0 ); LogError( ( "Invalid context provided." ) ); } - return result; /*lint !e480 !e481 Exiting function without calling fclose. - * Context file handle state is managed by this API. */ + /* Exiting function without calling fclose. Context file handle state is managed by this API. */ + return result; } /* Abort receiving the specified OTA update by closing the file. */ - -OtaErr_t otaPal_Abort( OtaFileContext_t * const C ) +OtaPalStatus_t otaPal_Abort( OtaFileContext_t * const C ) { /* Set default return status to uninitialized. */ - OtaErr_t result = OTA_ERR_UNINITIALIZED; + OtaPalMainStatus_t mainErr = OtaPalUninitialized; + OtaPalSubStatus_t subErr = 0; int32_t lFileClosresult; if( NULL != C ) @@ -198,29 +191,28 @@ OtaErr_t otaPal_Abort( OtaFileContext_t * const C ) if( 0 == lFileClosresult ) { LogInfo( ( "Closed file." ) ); - result = OTA_ERR_NONE; + mainErr = OtaPalSuccess; } else /* Failed to close file. */ { LogError( ( "Failed to close file." ) ); - result = ( OTA_ERR_FILE_ABORT | ( ( uint32_t ) errno & ( uint32_t ) OTA_PAL_ERR_MASK ) ); /*lint !e40 !e737 !e9027 !e9029 - * Errno is being used in accordance with host API documentation. - * Bitmasking is being used to preserve host API error with library status code. */ + mainErr = OtaPalFileAbort; + subErr = errno; } } else { /* Nothing to do. No open file associated with this context. */ - result = OTA_ERR_NONE; + mainErr = OtaPalSuccess; } } else /* Context was not valid. */ { LogError( ( "Parameter check failed: Input is NULL." ) ); - result = OTA_ERR_FILE_ABORT; + mainErr = OtaPalFileAbort; } - return result; + return OTA_PAL_COMBINE_ERR( mainErr, subErr ); } /* Write a block of data to the specified file. */ @@ -277,11 +269,12 @@ int16_t otaPal_WriteBlock( OtaFileContext_t * const C, } /* Close the specified file. This shall authenticate the file if it is marked as secure. */ - -OtaErr_t otaPal_CloseFile( OtaFileContext_t * const C ) +OtaPalStatus_t otaPal_CloseFile( OtaFileContext_t * const C ) { - OtaErr_t result = OTA_ERR_NONE; int32_t filerc = 0; + OtaPalMainStatus_t mainErr = OtaPalSuccess; + OtaPalSubStatus_t subErr = 0; + OtaPalStatus_t result; if( C != NULL ) { @@ -289,11 +282,13 @@ OtaErr_t otaPal_CloseFile( OtaFileContext_t * const C ) { /* Verify the file signature, close the file and return the signature verification result. */ result = otaPal_CheckFileSignature( C ); + mainErr = OTA_PAL_MAIN_ERR( result ); + subErr = OTA_PAL_SUB_ERR( result ); } else { LogError( ( "Parameter check failed: OTA signature structure is NULL." ) ); - result = OTA_ERR_SIGNATURE_CHECK_FAILED; + mainErr = OtaPalSignatureCheckFailed; } /* Close the file. */ @@ -306,12 +301,11 @@ OtaErr_t otaPal_CloseFile( OtaFileContext_t * const C ) if( filerc != 0 ) { LogError( ( "Failed to close OTA update file." ) ); - result = ( OTA_ERR_FILE_CLOSE | ( ( uint32_t ) errno & ( uint32_t ) OTA_PAL_ERR_MASK ) ); /*lint !e40 !e737 !e9027 !e9029 - * Errno is being used in accordance with host API documentation. - * Bitmasking is being used to preserve host API error with library status code. */ + mainErr = OtaPalFileClose; + subErr = errno; } - if( result == OTA_ERR_NONE ) + if( mainErr == OtaPalSuccess ) { LogInfo( ( "%s signature verification passed.", OTA_JsonFileSignatureKey ) ); @@ -320,7 +314,7 @@ OtaErr_t otaPal_CloseFile( OtaFileContext_t * const C ) else { LogError( ( "Failed to pass %s signature verification: %d.", - OTA_JsonFileSignatureKey, result ) ); + OTA_JsonFileSignatureKey, OTA_PAL_COMBINE_ERR( mainErr, subErr ) ) ); /* If we fail to verify the file signature that means the image is not valid. We need to set the image state to aborted. */ ( void ) otaPal_SetPlatformImageState( C, OtaImageStateAborted ); @@ -332,19 +326,19 @@ OtaErr_t otaPal_CloseFile( OtaFileContext_t * const C ) LogError( ( "Failed to close file: " "Parameter check failed: " "Invalid context." ) ); - result = OTA_ERR_FILE_CLOSE; + mainErr = OtaPalFileClose; } - return result; + return OTA_PAL_COMBINE_ERR( mainErr, subErr ); } - -static OtaErr_t Openssl_DigestVerify( EVP_MD_CTX * pSigContext, - EVP_PKEY * pPkey, - FILE * pFile, - Sig256_t * pSignature ) +/* Verify the signautre of the input content with OpenSSL. */ +static OtaPalMainStatus_t Openssl_DigestVerify( EVP_MD_CTX * pSigContext, + EVP_PKEY * pPkey, + FILE * pFile, + Sig256_t * pSignature ) { - OtaErr_t result = OTA_ERR_NONE; + OtaPalMainStatus_t mainErr = OtaPalSuccess; size_t bytesRead; uint8_t * pBuf; @@ -381,7 +375,7 @@ static OtaErr_t Openssl_DigestVerify( EVP_MD_CTX * pSigContext, pSignature->size ) ) /*lint !e732 !e9034 Allow comparison in this context. */ { LogError( ( "File signature check failed at FINAL" ) ); - result = OTA_ERR_SIGNATURE_CHECK_FAILED; + mainErr = OtaPalSignatureCheckFailed; } } @@ -391,23 +385,22 @@ static OtaErr_t Openssl_DigestVerify( EVP_MD_CTX * pSigContext, else { LogError( ( "Failed to allocate buffer memory." ) ); - result = OTA_ERR_OUT_OF_MEMORY; + mainErr = OtaPalOutOfMemory; } } else { LogError( ( "File signature check failed at INIT." ) ); - result = OTA_ERR_SIGNATURE_CHECK_FAILED; + mainErr = OtaPalSignatureCheckFailed; } - return result; + return mainErr; } /* Verify the signature of the specified file using OpenSSL. */ - -static OtaErr_t otaPal_CheckFileSignature( OtaFileContext_t * const C ) +static OtaPalStatus_t otaPal_CheckFileSignature( OtaFileContext_t * const C ) { - OtaErr_t result = OTA_ERR_NONE; + OtaPalMainStatus_t mainErr = OtaPalSuccess; EVP_PKEY * pPkey = NULL; EVP_MD_CTX * pSigContext = NULL; @@ -422,19 +415,19 @@ static OtaErr_t otaPal_CheckFileSignature( OtaFileContext_t * const C ) if( ( pPkey != NULL ) && ( pSigContext != NULL ) ) { /* Verify an ECDSA-SHA256 signature. */ - result = Openssl_DigestVerify( pSigContext, pPkey, C->pFile, C->pSignature ); + mainErr = Openssl_DigestVerify( pSigContext, pPkey, C->pFile, C->pSignature ); } else { if( pSigContext == NULL ) { LogError( ( "File signature check failed at NEW sig context." ) ); - result = OTA_ERR_SIGNATURE_CHECK_FAILED; + mainErr = OtaPalSignatureCheckFailed; } else { LogError( ( "File signature check failed at EXTRACT pkey from signer certificate." ) ); - result = OTA_ERR_BAD_SIGNER_CERT; + mainErr = OtaPalBadSignerCert; } } @@ -448,42 +441,40 @@ static OtaErr_t otaPal_CheckFileSignature( OtaFileContext_t * const C ) LogError( ( "Failed to check file signature: Paramater check failed: " " Invalid OTA file context." ) ); /* Invalid OTA context or file pointer. */ - result = OTA_ERR_NULL_FILE_PTR; + mainErr = OtaPalNullFileContext; } - return result; + return OTA_PAL_COMBINE_ERR( mainErr, 0 ); } /*-----------------------------------------------------------*/ -OtaErr_t otaPal_ResetDevice( OtaFileContext_t * const C ) +OtaPalStatus_t otaPal_ResetDevice( OtaFileContext_t * const C ) { ( void ) C; /* Return no error. linux implementation does not reset device. */ - return OTA_ERR_NONE; + return OTA_PAL_COMBINE_ERR( OtaPalSuccess, 0 ); } /*-----------------------------------------------------------*/ -OtaErr_t otaPal_ActivateNewImage( OtaFileContext_t * const C ) +OtaPalStatus_t otaPal_ActivateNewImage( OtaFileContext_t * const C ) { ( void ) C; /* Return no error. linux implementation simply does nothing on activate. * To run the new firmware image, double click the newly downloaded exe */ - return OTA_ERR_NONE; + return OTA_PAL_COMBINE_ERR( OtaPalSuccess, 0 ); } -/* - * Set the final state of the last transferred (final) OTA file (or bundle). - * On linux, the state of the OTA image is stored in PlaformImageState.txt. - */ - -OtaErr_t otaPal_SetPlatformImageState( OtaFileContext_t * const C, - OtaImageState_t eState ) +/* Set the final state of the last transferred (final) OTA file (or bundle). + * On linux, the state of the OTA image is stored in PlaformImageState.txt. */ +OtaPalStatus_t otaPal_SetPlatformImageState( OtaFileContext_t * const C, + OtaImageState_t eState ) { - OtaErr_t result = OTA_ERR_NONE; + OtaPalMainStatus_t mainErr = OtaPalSuccess; + OtaPalSubStatus_t subErr = 0; FILE * pPlatformImageState = NULL; ( void ) C; @@ -504,9 +495,8 @@ OtaErr_t otaPal_SetPlatformImageState( OtaFileContext_t * const C, * C standard library call is being used for portability. */ { LogError( ( "Unable to write to image state file." ) ); - result = ( OTA_ERR_BAD_IMAGE_STATE | ( ( uint32_t ) errno & ( uint32_t ) OTA_PAL_ERR_MASK ) ); /*lint !e40 !e737 !e9027 !e9029 - * Errno is being used in accordance with host API documentation. - * Bitmasking is being used to preserve host API error with library status code. */ + mainErr = OtaPalBadImageState; + subErr = errno; } /* Close PlatformImageState.txt. */ @@ -515,26 +505,25 @@ OtaErr_t otaPal_SetPlatformImageState( OtaFileContext_t * const C, if( 0 != fclose( pPlatformImageState ) ) /*lint !e586 Allow call in this context. */ { LogError( ( "Unable to close image state file." ) ); - result = ( OTA_ERR_BAD_IMAGE_STATE | ( ( uint32_t ) errno & ( uint32_t ) OTA_PAL_ERR_MASK ) ); /*lint !e40 !e737 !e9027 !e9029 - * Errno is being used in accordance with host API documentation. - * Bitmasking is being used to preserve host API error with library status code. */ + mainErr = OtaPalBadImageState; + subErr = errno; } } else { LogError( ( "Unable to open image state file." ) ); - result = ( OTA_ERR_BAD_IMAGE_STATE | ( ( uint32_t ) errno & ( uint32_t ) OTA_PAL_ERR_MASK ) ); /*lint !e40 !e737 !e9027 !e9029 - * Errno is being used in accordance with host API documentation. - * Bitmasking is being used to preserve host API error with library status code. */ + mainErr = OtaPalBadImageState; + subErr = errno; } - } /*lint !e481 Allow fopen and fclose calls in this context. */ + } /*lint !e481 Allow fopen and fclose calls in this context. */ else /* Image state invalid. */ { LogError( ( "Invalid image state provided." ) ); - result = OTA_ERR_BAD_IMAGE_STATE; + mainErr = OtaPalBadImageState; } - return result; /*lint !e480 !e481 Allow calls to fopen and fclose in this context. */ + /* Allow calls to fopen and fclose in this context. */ + return OTA_PAL_COMBINE_ERR( mainErr, subErr ); } /* Get the state of the currently running image. diff --git a/platform/posix/ota_pal/utest/ota_pal_posix_utest.c b/platform/posix/ota_pal/utest/ota_pal_posix_utest.c index 4c7b76c0cc..49256ad456 100644 --- a/platform/posix/ota_pal/utest/ota_pal_posix_utest.c +++ b/platform/posix/ota_pal/utest/ota_pal_posix_utest.c @@ -31,6 +31,8 @@ #include #include + +#include #include #include "unity.h" @@ -112,7 +114,7 @@ static OtaFileContext_t otaFile; * * #define otatestpalCERTIFICATE_FILE "..\\..\\..\\..\\..\\libraries\\freertos_plus\\aws\\ota\\test\\test_files\\ecdsa-sha256-signer.crt.pem" */ -#define otatestpalCERTIFICATE_FILE "../../../platform/ota/utest/test_files/ecdsa-sha256-signer.crt.pem" +#define otatestpalCERTIFICATE_FILE "../../../platform/posix/ota_pal/utest/test_files/ecdsa-sha256-signer.crt.pem" /** * @brief Some devices have a hard-coded name for the firmware image to boot. @@ -130,15 +132,11 @@ void setUp( void ) void tearDown( void ) { - OtaErr_t result; + OtaPalMainStatus_t result; /* We want to abort the OTA file after every test. This closes the OtaFile. */ result = otaPal_Abort( &otaFile ); - - if( OTA_ERR_NONE != result ) - { - LogError( ( "Error aborting otaFile with code: %d", result ) ); - } + TEST_ASSERT_EQUAL( OtaPalSuccess, result ); unlink( "PlatformImageState.txt" ); } @@ -150,11 +148,11 @@ void tearDown( void ) */ void test_OTAPAL_Abort_NullFileContext( void ) { - OtaErr_t result = OTA_ERR_UNINITIALIZED; + OtaPalMainStatus_t result; - result = otaPal_Abort( NULL ); + result = OTA_PAL_MAIN_ERR( otaPal_Abort( NULL ) ); - TEST_ASSERT_EQUAL( OTA_ERR_FILE_ABORT, result ); + TEST_ASSERT_EQUAL( OtaPalFileAbort, result ); } @@ -163,12 +161,12 @@ void test_OTAPAL_Abort_NullFileContext( void ) */ void test_OTAPAL_Abort_NullFileHandle( void ) { - OtaErr_t result = OTA_ERR_UNINITIALIZED; + OtaPalMainStatus_t result; otaFile.pFile = NULL; - result = otaPal_Abort( &otaFile ); + result = OTA_PAL_MAIN_ERR( otaPal_Abort( &otaFile ) ); - TEST_ASSERT_EQUAL( OTA_ERR_NONE, result ); + TEST_ASSERT_EQUAL( OtaPalSuccess, result ); } /** @@ -176,14 +174,14 @@ void test_OTAPAL_Abort_NullFileHandle( void ) */ void test_OTAPAL_Abort_ValidFileHandle( void ) { - OtaErr_t result = OTA_ERR_UNINITIALIZED; + OtaPalMainStatus_t result; otaFile.pFilePath = ( uint8_t * ) otatestpalFIRMWARE_FILE; - result = otaPal_CreateFileForRx( &otaFile ); - TEST_ASSERT_EQUAL( OTA_ERR_NONE, result ); + result = OTA_PAL_MAIN_ERR( otaPal_CreateFileForRx( &otaFile ) ); + TEST_ASSERT_EQUAL( OtaPalSuccess, result ); - result = otaPal_Abort( &otaFile ); - TEST_ASSERT_EQUAL( OTA_ERR_NONE, result ); + result = OTA_PAL_MAIN_ERR( otaPal_Abort( &otaFile ) ); + TEST_ASSERT_EQUAL( OtaPalSuccess, result ); } /** @@ -191,11 +189,11 @@ void test_OTAPAL_Abort_ValidFileHandle( void ) */ void test_OTAPAL_Abort_NonExistentFile( void ) { - OtaErr_t result = OTA_ERR_UNINITIALIZED; + OtaPalMainStatus_t result; otaFile.pFilePath = ( uint8_t * ) ( "nonexistingfile.bin" ); - result = otaPal_Abort( &otaFile ); - TEST_ASSERT_EQUAL( OTA_ERR_NONE, ( result & OTA_MAIN_ERR_MASK ) ); + result = OTA_PAL_MAIN_ERR( otaPal_Abort( &otaFile ) ); + TEST_ASSERT_EQUAL( OtaPalSuccess, result ); } /** @@ -203,10 +201,10 @@ void test_OTAPAL_Abort_NonExistentFile( void ) */ void test_OTAPAL_CreateFileForRx_NullFileContext( void ) { - OtaErr_t result = OTA_ERR_UNINITIALIZED; + OtaPalMainStatus_t result; - result = otaPal_CreateFileForRx( NULL ); - TEST_ASSERT_EQUAL( OTA_ERR_RX_FILE_CREATE_FAILED, result ); + result = OTA_PAL_MAIN_ERR( otaPal_CreateFileForRx( NULL ) ); + TEST_ASSERT_EQUAL( OtaPalRxFileCreateFailed, result ); } @@ -215,12 +213,12 @@ void test_OTAPAL_CreateFileForRx_NullFileContext( void ) */ void test_OTAPAL_CreateFileForRx_NullFilePath( void ) { - OtaErr_t result = OTA_ERR_UNINITIALIZED; + OtaPalMainStatus_t result; otaFile.pFilePath = NULL; - result = otaPal_CreateFileForRx( &otaFile ); + result = OTA_PAL_MAIN_ERR( otaPal_CreateFileForRx( &otaFile ) ); - TEST_ASSERT_EQUAL( OTA_ERR_RX_FILE_CREATE_FAILED, result ); + TEST_ASSERT_EQUAL( OtaPalRxFileCreateFailed, result ); } /** @@ -228,20 +226,20 @@ void test_OTAPAL_CreateFileForRx_NullFilePath( void ) */ void test_OTAPAL_CreateFileForRx_FailedToCreateFile( void ) { - OtaErr_t result = OTA_ERR_UNINITIALIZED; + OtaPalMainStatus_t result; - FILE * existingFile = fopen( otatestpalFIRMWARE_FILE, "r" ); + fopen( otatestpalFIRMWARE_FILE, "r" ); chmod( otatestpalFIRMWARE_FILE, S_IRUSR ); otaFile.pFilePath = ( uint8_t * ) otatestpalFIRMWARE_FILE; /* Create a file that exists with w+b mode */ - result = otaPal_CreateFileForRx( &otaFile ); - TEST_ASSERT_EQUAL( OTA_ERR_RX_FILE_CREATE_FAILED, ( result & OTA_MAIN_ERR_MASK ) ); + result = OTA_PAL_MAIN_ERR( otaPal_CreateFileForRx( &otaFile ) ); + TEST_ASSERT_EQUAL( OtaPalRxFileCreateFailed, result ); chmod( otatestpalFIRMWARE_FILE, S_IRWXU ); - result = otaPal_CreateFileForRx( &otaFile ); - TEST_ASSERT_EQUAL( OTA_ERR_NONE, ( result & OTA_MAIN_ERR_MASK ) ); + result = OTA_PAL_MAIN_ERR( otaPal_CreateFileForRx( &otaFile ) ); + TEST_ASSERT_EQUAL( OtaPalSuccess, result ); } /** @@ -249,11 +247,11 @@ void test_OTAPAL_CreateFileForRx_FailedToCreateFile( void ) */ void test_OTAPAL_CreateFileForRx_ValidFileHandle( void ) { - OtaErr_t result = OTA_ERR_UNINITIALIZED; + OtaPalMainStatus_t result; otaFile.pFilePath = ( uint8_t * ) otatestpalFIRMWARE_FILE; - result = otaPal_CreateFileForRx( &otaFile ); - TEST_ASSERT_EQUAL( OTA_ERR_NONE, result ); + result = OTA_PAL_MAIN_ERR( otaPal_CreateFileForRx( &otaFile ) ); + TEST_ASSERT_EQUAL( OtaPalSuccess, result ); } @@ -262,11 +260,11 @@ void test_OTAPAL_CreateFileForRx_ValidFileHandle( void ) */ void test_OTAPAL_WriteBlock_NullFileContext( void ) { - OtaErr_t result = OTA_ERR_UNINITIALIZED; + int16_t bytes_written = 0; uint8_t data = 0xAA; - result = otaPal_WriteBlock( NULL, 0, &data, 1 ); - TEST_ASSERT_EQUAL( OTA_ERR_NONE, result + 1 ); + bytes_written = otaPal_WriteBlock( NULL, 0, &data, 1 ); + TEST_ASSERT_EQUAL( OtaPalSuccess, bytes_written + 1 ); } @@ -275,14 +273,14 @@ void test_OTAPAL_WriteBlock_NullFileContext( void ) */ void test_OTAPAL_WriteBlock_WriteSingleByte( void ) { - OtaErr_t result; + OtaPalMainStatus_t result; int16_t numBytesWritten; uint8_t data = 0xAA; /* TEST: Write a byte of data. */ otaFile.pFilePath = ( uint8_t * ) otatestpalFIRMWARE_FILE; - result = otaPal_CreateFileForRx( &otaFile ); - TEST_ASSERT_EQUAL( OTA_ERR_NONE, result ); + result = OTA_PAL_MAIN_ERR( otaPal_CreateFileForRx( &otaFile ) ); + TEST_ASSERT_EQUAL( OtaPalSuccess, result ); if( TEST_PROTECT() ) { @@ -296,7 +294,7 @@ void test_OTAPAL_WriteBlock_WriteSingleByte( void ) */ void test_OTAPAL_WriteBlock_WriteManyBlocks( void ) { - OtaErr_t result; + OtaPalMainStatus_t result; int16_t numBytesWritten; otaFile.pFilePath = ( uint8_t * ) otatestpalFIRMWARE_FILE; @@ -304,8 +302,8 @@ void test_OTAPAL_WriteBlock_WriteManyBlocks( void ) /* TEST: Write many bytes of data. */ otaFile.pFilePath = ( uint8_t * ) otatestpalFIRMWARE_FILE; - result = otaPal_CreateFileForRx( &otaFile ); - TEST_ASSERT_EQUAL( OTA_ERR_NONE, result ); + result = OTA_PAL_MAIN_ERR( otaPal_CreateFileForRx( &otaFile ) ); + TEST_ASSERT_EQUAL( OtaPalSuccess, result ); if( TEST_PROTECT() ) { @@ -338,33 +336,34 @@ void test_OTAPAL_WriteBlock_FwriteError( void ) void test_OTAPAL_CloseFile_ValidSignature( void ) { - OtaErr_t result; + OtaPalMainStatus_t result; + int16_t bytes_written = 0; Sig256_t sig = { 0 }; /* We use a dummy file name here because closing the system designated bootable * image with content that is not runnable may cause issues. */ otaFile.pFilePath = ( uint8_t * ) ( "test_happy_path_image.bin" ); otaFile.fileSize = sizeof( dummyData ); - result = otaPal_CreateFileForRx( &otaFile ); - TEST_ASSERT_EQUAL( OTA_ERR_NONE, result ); + result = OTA_PAL_MAIN_ERR( otaPal_CreateFileForRx( &otaFile ) ); + TEST_ASSERT_EQUAL( OtaPalSuccess, result ); /* We still want to close the file if the test fails somewhere here. */ if( TEST_PROTECT() ) { /* Write data to the file. */ - result = otaPal_WriteBlock( &otaFile, - 0, - dummyData, - sizeof( dummyData ) ); - TEST_ASSERT_EQUAL( sizeof( dummyData ), result ); + bytes_written = otaPal_WriteBlock( &otaFile, + 0, + dummyData, + sizeof( dummyData ) ); + TEST_ASSERT_EQUAL( sizeof( dummyData ), bytes_written ); otaFile.pSignature = &sig; otaFile.pSignature->size = ucValidSignatureLength; memcpy( otaFile.pSignature->data, ucValidSignature, ucValidSignatureLength ); otaFile.pCertFilepath = ( uint8_t * ) otatestpalCERTIFICATE_FILE; - result = otaPal_CloseFile( &otaFile ); - TEST_ASSERT_EQUAL_INT( OTA_ERR_NONE, result ); + result = OTA_PAL_MAIN_ERR( otaPal_CloseFile( &otaFile ) ); + TEST_ASSERT_EQUAL_INT( OtaPalSuccess, result ); } } @@ -376,25 +375,26 @@ void test_OTAPAL_CloseFile_ValidSignature( void ) */ void test_OTAPAL_CloseFile_InvalidSignatureBlockWritten( void ) { - OtaErr_t result; + OtaPalMainStatus_t result; + int16_t bytes_written = 0; Sig256_t sig = { 0 }; /* Create a local file using the PAL. */ otaFile.pFilePath = ( uint8_t * ) otatestpalFIRMWARE_FILE; otaFile.fileSize = sizeof( dummyData ); - result = otaPal_CreateFileForRx( &otaFile ); - TEST_ASSERT_EQUAL( OTA_ERR_NONE, result ); + result = OTA_PAL_MAIN_ERR( otaPal_CreateFileForRx( &otaFile ) ); + TEST_ASSERT_EQUAL( OtaPalSuccess, result ); /* We still want to close the file if the test fails somewhere here. */ if( TEST_PROTECT() ) { /* Write data to the file. */ - result = otaPal_WriteBlock( &otaFile, - 0, - dummyData, - sizeof( dummyData ) ); - TEST_ASSERT_EQUAL( sizeof( dummyData ), result ); + bytes_written = otaPal_WriteBlock( &otaFile, + 0, + dummyData, + sizeof( dummyData ) ); + TEST_ASSERT_EQUAL( sizeof( dummyData ), bytes_written ); /* Fill out an incorrect signature. */ otaFile.pSignature = &sig; @@ -403,11 +403,11 @@ void test_OTAPAL_CloseFile_InvalidSignatureBlockWritten( void ) otaFile.pCertFilepath = ( uint8_t * ) otatestpalCERTIFICATE_FILE; /* Try to close the file. */ - result = otaPal_CloseFile( &otaFile ); + result = OTA_PAL_MAIN_ERR( otaPal_CloseFile( &otaFile ) ); - if( ( OTA_ERR_BAD_SIGNER_CERT != ( result & OTA_MAIN_ERR_MASK ) ) && - ( OTA_ERR_SIGNATURE_CHECK_FAILED != ( result & OTA_MAIN_ERR_MASK ) ) && - ( OTA_ERR_FILE_CLOSE != ( result & OTA_MAIN_ERR_MASK ) ) ) + if( ( OtaPalBadSignerCert != result ) && + ( OtaPalSignatureCheckFailed != result ) && + ( OtaPalFileClose != result ) ) { TEST_ASSERT_TRUE( 0 ); } @@ -421,13 +421,13 @@ void test_OTAPAL_CloseFile_InvalidSignatureBlockWritten( void ) */ void test_OTAPAL_CloseFile_InvalidSignatureNoBlockWritten( void ) { - OtaErr_t result; + OtaPalMainStatus_t result; Sig256_t sig = { 0 }; /* Create a local file using the PAL. */ otaFile.pFilePath = ( uint8_t * ) otatestpalFIRMWARE_FILE; - result = otaPal_CreateFileForRx( &otaFile ); - TEST_ASSERT_EQUAL( OTA_ERR_NONE, result ); + result = OTA_PAL_MAIN_ERR( otaPal_CreateFileForRx( &otaFile ) ); + TEST_ASSERT_EQUAL( OtaPalSuccess, result ); /* Fill out an incorrect signature. */ otaFile.pSignature = &sig; @@ -439,11 +439,11 @@ void test_OTAPAL_CloseFile_InvalidSignatureNoBlockWritten( void ) if( TEST_PROTECT() ) { /* Try to close the file. */ - result = otaPal_CloseFile( &otaFile ); + result = OTA_PAL_MAIN_ERR( otaPal_CloseFile( &otaFile ) ); - if( ( OTA_ERR_BAD_SIGNER_CERT != ( result & OTA_MAIN_ERR_MASK ) ) && - ( OTA_ERR_SIGNATURE_CHECK_FAILED != ( result & OTA_MAIN_ERR_MASK ) ) && - ( OTA_ERR_FILE_CLOSE != ( result & OTA_MAIN_ERR_MASK ) ) ) + if( ( OtaPalBadSignerCert != result ) && + ( OtaPalSignatureCheckFailed != result ) && + ( OtaPalFileClose != result ) ) { TEST_ASSERT_TRUE( 0 ); } @@ -461,7 +461,8 @@ void test_OTAPAL_CloseFile_InvalidSignatureNoBlockWritten( void ) */ void test_OTAPAL_CloseFile_NonexistingCodeSignerCertificate( void ) { - OtaErr_t result; + OtaPalMainStatus_t result; + int16_t bytes_written = 0; Sig256_t sig = { 0 }; memset( &otaFile, 0, sizeof( otaFile ) ); @@ -470,18 +471,18 @@ void test_OTAPAL_CloseFile_NonexistingCodeSignerCertificate( void ) otaFile.pFilePath = ( uint8_t * ) otatestpalFIRMWARE_FILE; otaFile.fileSize = sizeof( dummyData ); - result = otaPal_CreateFileForRx( &otaFile ); - TEST_ASSERT_EQUAL( OTA_ERR_NONE, result ); + result = OTA_PAL_MAIN_ERR( otaPal_CreateFileForRx( &otaFile ) ); + TEST_ASSERT_EQUAL( OtaPalSuccess, result ); /* We still want to close the file if the test fails somewhere here. */ if( TEST_PROTECT() ) { /* Write data to the file. */ - result = otaPal_WriteBlock( &otaFile, - 0, - dummyData, - sizeof( dummyData ) ); - TEST_ASSERT_EQUAL( sizeof( dummyData ), result ); + bytes_written = otaPal_WriteBlock( &otaFile, + 0, + dummyData, + sizeof( dummyData ) ); + TEST_ASSERT_EQUAL( sizeof( dummyData ), bytes_written ); /* Check the signature (not expected to be valid in this case). */ otaFile.pSignature = &sig; @@ -489,11 +490,11 @@ void test_OTAPAL_CloseFile_NonexistingCodeSignerCertificate( void ) memcpy( otaFile.pSignature->data, ucValidSignature, ucValidSignatureLength ); otaFile.pCertFilepath = ( uint8_t * ) ( "nonexistingfile.crt" ); - result = otaPal_CloseFile( &otaFile ); + result = OTA_PAL_MAIN_ERR( otaPal_CloseFile( &otaFile ) ); - if( ( OTA_ERR_BAD_SIGNER_CERT != ( result & OTA_MAIN_ERR_MASK ) ) && - ( OTA_ERR_SIGNATURE_CHECK_FAILED != ( result & OTA_MAIN_ERR_MASK ) ) && - ( OTA_ERR_FILE_CLOSE != ( result & OTA_MAIN_ERR_MASK ) ) ) + if( ( OtaPalBadSignerCert != result ) && + ( OtaPalSignatureCheckFailed != result ) && + ( OtaPalFileClose != result ) ) { TEST_ASSERT_TRUE( 0 ); } @@ -505,11 +506,11 @@ void test_OTAPAL_CloseFile_NonexistingCodeSignerCertificate( void ) */ void test_OTAPAL_ResetDevice_NullFileContext( void ) { - OtaErr_t result = OTA_ERR_UNINITIALIZED; + OtaPalMainStatus_t result; /* Currently there is nothing done inside the function. It's a placeholder. */ - result = otaPal_ResetDevice( NULL ); - TEST_ASSERT_EQUAL( OTA_ERR_NONE, result ); + result = OTA_PAL_MAIN_ERR( otaPal_ResetDevice( NULL ) ); + TEST_ASSERT_EQUAL( OtaPalSuccess, result ); } /** @@ -517,10 +518,10 @@ void test_OTAPAL_ResetDevice_NullFileContext( void ) */ void test_OTAPAL_ActivateNewImage_NullFileContext( void ) { - OtaErr_t result = OTA_ERR_UNINITIALIZED; + OtaPalMainStatus_t result; - result = otaPal_ActivateNewImage( NULL ); - TEST_ASSERT_EQUAL( OTA_ERR_NONE, result ); + result = OTA_PAL_MAIN_ERR( otaPal_ActivateNewImage( NULL ) ); + TEST_ASSERT_EQUAL( OtaPalSuccess, result ); } /** @@ -528,7 +529,8 @@ void test_OTAPAL_ActivateNewImage_NullFileContext( void ) */ void test_OTAPAL_SetPlatformImageState_SelfTestImageState( void ) { - OtaErr_t result; + OtaPalMainStatus_t result; + int16_t bytes_written = 0; OtaImageState_t eImageState = OtaImageStateUnknown; OtaPalImageState_t palImageState = OtaPalImageStateUnknown; @@ -537,23 +539,23 @@ void test_OTAPAL_SetPlatformImageState_SelfTestImageState( void ) otaFile.pFilePath = ( uint8_t * ) otatestpalFIRMWARE_FILE; otaFile.fileSize = sizeof( dummyData ); - result = otaPal_CreateFileForRx( &otaFile ); - TEST_ASSERT_EQUAL( OTA_ERR_NONE, result ); + result = OTA_PAL_MAIN_ERR( otaPal_CreateFileForRx( &otaFile ) ); + TEST_ASSERT_EQUAL( OtaPalSuccess, result ); /* We still want to close the file if the test fails. */ if( TEST_PROTECT() ) { /* Write data to the file. */ - result = otaPal_WriteBlock( &otaFile, - 0, - dummyData, - sizeof( dummyData ) ); - TEST_ASSERT_EQUAL( sizeof( dummyData ), result ); + bytes_written = otaPal_WriteBlock( &otaFile, + 0, + dummyData, + sizeof( dummyData ) ); + TEST_ASSERT_EQUAL( sizeof( dummyData ), bytes_written ); /* Set the image state. */ eImageState = OtaImageStateTesting; - result = otaPal_SetPlatformImageState( &otaFile, eImageState ); - TEST_ASSERT_EQUAL_INT( OTA_ERR_NONE, result ); + result = OTA_PAL_MAIN_ERR( otaPal_SetPlatformImageState( &otaFile, eImageState ) ); + TEST_ASSERT_EQUAL_INT( OtaPalSuccess, result ); /* Verify that image state was saved correctly. */ @@ -571,7 +573,8 @@ void test_OTAPAL_SetPlatformImageState_SelfTestImageState( void ) */ void test_OTAPAL_SetPlatformImageState_InvalidImageState( void ) { - OtaErr_t result; + OtaPalMainStatus_t result; + int16_t bytes_written = 0; OtaImageState_t eImageState = OtaImageStateUnknown; OtaPalImageState_t palImageState = OtaPalImageStateUnknown; @@ -579,23 +582,23 @@ void test_OTAPAL_SetPlatformImageState_InvalidImageState( void ) otaFile.pFilePath = ( uint8_t * ) otatestpalFIRMWARE_FILE; otaFile.fileSize = sizeof( dummyData ); - result = otaPal_CreateFileForRx( &otaFile ); - TEST_ASSERT_EQUAL( OTA_ERR_NONE, result ); + result = OTA_PAL_MAIN_ERR( otaPal_CreateFileForRx( &otaFile ) ); + TEST_ASSERT_EQUAL( OtaPalSuccess, result ); /* We still want to close the file if the test fails. */ if( TEST_PROTECT() ) { /* Write data to the file. */ - result = otaPal_WriteBlock( &otaFile, - 0, - dummyData, - sizeof( dummyData ) ); - TEST_ASSERT_EQUAL( sizeof( dummyData ), result ); + bytes_written = otaPal_WriteBlock( &otaFile, + 0, + dummyData, + sizeof( dummyData ) ); + TEST_ASSERT_EQUAL( sizeof( dummyData ), bytes_written ); /* Try to set an invalid image state. */ eImageState = ( OtaImageState_t ) ( OtaLastImageState + 1 ); - result = otaPal_SetPlatformImageState( &otaFile, eImageState ); - TEST_ASSERT_EQUAL( OTA_ERR_BAD_IMAGE_STATE, ( result & OTA_MAIN_ERR_MASK ) ); + result = OTA_PAL_MAIN_ERR( otaPal_SetPlatformImageState( &otaFile, eImageState ) ); + TEST_ASSERT_EQUAL( OtaPalBadImageState, result ); /* Read the platform image state to verify */ /* Nothing wrote to the image state file. Ota Pal Image state remain valid */ @@ -609,7 +612,8 @@ void test_OTAPAL_SetPlatformImageState_InvalidImageState( void ) */ void test_OTAPAL_SetPlatformImageState_UnknownImageState( void ) { - OtaErr_t result; + OtaPalMainStatus_t result; + int16_t bytes_written = 0; OtaImageState_t eImageState = OtaImageStateUnknown; OtaPalImageState_t palImageState = OtaPalImageStateUnknown; @@ -617,23 +621,23 @@ void test_OTAPAL_SetPlatformImageState_UnknownImageState( void ) otaFile.pFilePath = ( uint8_t * ) otatestpalFIRMWARE_FILE; otaFile.fileSize = sizeof( dummyData ); - result = otaPal_CreateFileForRx( &otaFile ); - TEST_ASSERT_EQUAL( OTA_ERR_NONE, result ); + result = OTA_PAL_MAIN_ERR( otaPal_CreateFileForRx( &otaFile ) ); + TEST_ASSERT_EQUAL( OtaPalSuccess, result ); /* We still want to close the file if the test fails. */ if( TEST_PROTECT() ) { /* Write data to the file. */ - result = otaPal_WriteBlock( &otaFile, - 0, - dummyData, - sizeof( dummyData ) ); - TEST_ASSERT_EQUAL( sizeof( dummyData ), result ); + bytes_written = otaPal_WriteBlock( &otaFile, + 0, + dummyData, + sizeof( dummyData ) ); + TEST_ASSERT_EQUAL( sizeof( dummyData ), bytes_written ); /* Try to set an invalid image state. */ eImageState = OtaImageStateUnknown; - result = otaPal_SetPlatformImageState( &otaFile, eImageState ); - TEST_ASSERT_EQUAL( OTA_ERR_BAD_IMAGE_STATE, ( result & OTA_MAIN_ERR_MASK ) ); + result = OTA_PAL_MAIN_ERR( otaPal_SetPlatformImageState( &otaFile, eImageState ) ); + TEST_ASSERT_EQUAL( OtaPalBadImageState, result ); /* Read the platform image state to verify */ /* Nothing wrote to the image state file. Ota Pal Image state remain valid */ @@ -649,7 +653,8 @@ void test_OTAPAL_SetPlatformImageState_UnknownImageState( void ) */ void test_OTAPAL_SetPlatformImageState_RejectImageState( void ) { - OtaErr_t result; + OtaPalMainStatus_t result; + int16_t bytes_written = 0; OtaImageState_t eImageState = OtaImageStateUnknown; OtaPalImageState_t palImageState = OtaPalImageStateUnknown; @@ -657,22 +662,22 @@ void test_OTAPAL_SetPlatformImageState_RejectImageState( void ) otaFile.pFilePath = ( uint8_t * ) otatestpalFIRMWARE_FILE; otaFile.fileSize = sizeof( dummyData ); - result = otaPal_CreateFileForRx( &otaFile ); - TEST_ASSERT_EQUAL( OTA_ERR_NONE, result ); + result = OTA_PAL_MAIN_ERR( otaPal_CreateFileForRx( &otaFile ) ); + TEST_ASSERT_EQUAL( OtaPalSuccess, result ); /* We still want to close the file if the test fails. */ if( TEST_PROTECT() ) { /* Write data to the file. */ - result = otaPal_WriteBlock( &otaFile, - 0, - dummyData, - sizeof( dummyData ) ); - TEST_ASSERT_EQUAL( sizeof( dummyData ), result ); + bytes_written = otaPal_WriteBlock( &otaFile, + 0, + dummyData, + sizeof( dummyData ) ); + TEST_ASSERT_EQUAL( sizeof( dummyData ), bytes_written ); eImageState = OtaImageStateRejected; - result = otaPal_SetPlatformImageState( &otaFile, eImageState ); - TEST_ASSERT_EQUAL_INT( OTA_ERR_NONE, ( result & OTA_MAIN_ERR_MASK ) ); + result = OTA_PAL_MAIN_ERR( otaPal_SetPlatformImageState( &otaFile, eImageState ) ); + TEST_ASSERT_EQUAL_INT( OtaPalSuccess, result ); /* Read the platform image state to verify */ palImageState = otaPal_GetPlatformImageState( &otaFile ); @@ -686,7 +691,8 @@ void test_OTAPAL_SetPlatformImageState_RejectImageState( void ) */ void test_OTAPAL_SetPlatformImageState_AbortImageState( void ) { - OtaErr_t result; + OtaPalMainStatus_t result; + int16_t bytes_written = 0; OtaImageState_t eImageState = OtaImageStateUnknown; OtaPalImageState_t palImageState = OtaPalImageStateUnknown; @@ -694,22 +700,22 @@ void test_OTAPAL_SetPlatformImageState_AbortImageState( void ) otaFile.pFilePath = ( uint8_t * ) otatestpalFIRMWARE_FILE; otaFile.fileSize = sizeof( dummyData ); - result = otaPal_CreateFileForRx( &otaFile ); - TEST_ASSERT_EQUAL( OTA_ERR_NONE, result ); + result = OTA_PAL_MAIN_ERR( otaPal_CreateFileForRx( &otaFile ) ); + TEST_ASSERT_EQUAL( OtaPalSuccess, result ); /* We still want to close the file if the test fails. */ if( TEST_PROTECT() ) { /* Write data to the file. */ - result = otaPal_WriteBlock( &otaFile, - 0, - dummyData, - sizeof( dummyData ) ); - TEST_ASSERT_EQUAL( sizeof( dummyData ), result ); + bytes_written = otaPal_WriteBlock( &otaFile, + 0, + dummyData, + sizeof( dummyData ) ); + TEST_ASSERT_EQUAL( sizeof( dummyData ), bytes_written ); eImageState = OtaImageStateAborted; - result = otaPal_SetPlatformImageState( &otaFile, eImageState ); - TEST_ASSERT_EQUAL_INT( OTA_ERR_NONE, ( result & OTA_MAIN_ERR_MASK ) ); + result = OTA_PAL_MAIN_ERR( otaPal_SetPlatformImageState( &otaFile, eImageState ) ); + TEST_ASSERT_EQUAL_INT( OtaPalSuccess, result ); /* Read the platform image state to verify */ palImageState = otaPal_GetPlatformImageState( &otaFile ); @@ -723,7 +729,8 @@ void test_OTAPAL_SetPlatformImageState_AbortImageState( void ) */ void test_OTAPAL_GetPlatformImageState_InvalidImageStateFromFileCloseFailure( void ) { - OtaErr_t result; + OtaPalMainStatus_t result; + int16_t bytes_written = 0; Sig256_t sig = { 0 }; OtaPalImageState_t ePalImageState = OtaPalImageStateUnknown; @@ -732,18 +739,18 @@ void test_OTAPAL_GetPlatformImageState_InvalidImageStateFromFileCloseFailure( vo otaFile.pFilePath = ( uint8_t * ) otatestpalFIRMWARE_FILE; otaFile.fileSize = sizeof( dummyData ); - result = otaPal_CreateFileForRx( &otaFile ); - TEST_ASSERT_EQUAL( OTA_ERR_NONE, result ); + result = OTA_PAL_MAIN_ERR( otaPal_CreateFileForRx( &otaFile ) ); + TEST_ASSERT_EQUAL( OtaPalSuccess, result ); /* We still want to close the file if the test fails. */ if( TEST_PROTECT() ) { /* Write data to the file. */ - result = otaPal_WriteBlock( &otaFile, - 0, - dummyData, - sizeof( dummyData ) ); - TEST_ASSERT_EQUAL( sizeof( dummyData ), result ); + bytes_written = otaPal_WriteBlock( &otaFile, + 0, + dummyData, + sizeof( dummyData ) ); + TEST_ASSERT_EQUAL( sizeof( dummyData ), bytes_written ); /* Check the signature. */ otaFile.pSignature = &sig; @@ -751,11 +758,11 @@ void test_OTAPAL_GetPlatformImageState_InvalidImageStateFromFileCloseFailure( vo memcpy( otaFile.pSignature->data, ucInvalidSignature, ucInvalidSignatureLength ); otaFile.pCertFilepath = ( uint8_t * ) otatestpalCERTIFICATE_FILE; - result = otaPal_CloseFile( &otaFile ); + result = OTA_PAL_MAIN_ERR( otaPal_CloseFile( &otaFile ) ); - if( ( OTA_ERR_BAD_SIGNER_CERT != ( result & OTA_MAIN_ERR_MASK ) ) && - ( OTA_ERR_SIGNATURE_CHECK_FAILED != ( result & OTA_MAIN_ERR_MASK ) ) && - ( OTA_ERR_FILE_CLOSE != ( result & OTA_MAIN_ERR_MASK ) ) ) + if( ( OtaPalBadSignerCert != result ) && + ( OtaPalSignatureCheckFailed != result ) && + ( OtaPalFileClose != result ) ) { TEST_ASSERT_TRUE( 0 ); }