Skip to content

Commit

Permalink
Update the OTA PAL unit tests for 100% code coverage (#1430)
Browse files Browse the repository at this point in the history
* Add OTA from AFR 202007.00 release

* Fix OTA CMake file, add jsmn and tinycbor

* Delete JSMN files before submodule

* Submodule JSMN on master

* Update JSMN to v1.1.0

* Delete tinycbor before submodule

* Submodule TinyCBOR to master

* Update .gitmodule with the version of TinyCBOR

* OTA Agent Posix : Use POSIX thread instead of FreeRTOS task

* OTA Agent POSIX: Use POSIX semaphore instead of FreeRTOS semaphore

* OTA linux pal

* Add OTA demo

* Update OTA library to use new MQTT library

* OTA LTS Dev: Update include files in OTA Agent File

* OTA LTS Dev: Update OTA Agent context initialization

* OTA LTS Dev: Self-test timer functions to use timer interface

* OTA LTS Dev: Request timer functions implementation to use timers passed to the lib

* OTA LTS Dev: use assert instead of configassert in OTA Agent

* OTA LTS Dev: Update buffer get and free functions by removing protection

* OTA LTS Dev: Add OS interface context to OTA Agent Init functions

* OTA LTS Dev: Add OTA OS implementation file

* OTA LTS Dev: OTA MQTT demo update

* OTA LTS Dev: OTA Agent changes for C-SDK

* OTA LTS Dev: OTA MQTT component changes for C-SDK

* OTA Dev LTS: OTA OS interface for linux

* Add mbedtls

* Remove mock

* tmp fix for OTA

* Remove OTA library

* Add OTA as an submodule

* Update OTA config with minimum request blocks

* Remove ota_os_interface.h from platform folder

* Remove stdbool.h from ota_os_posix.h

* Update OTA naming convention

* Update OTA submodule

* Update OTA demo cmake file

* Fix warnings

* Update OTA submodule

* Remove tinycbor from 3rdparty dir

* Update OTA submodule and demo CMake file

* OTA MQTT demo main and startota functions

* MQTT and OS interface in startotademo

* OTA MQTT interface publish and subscribe implementation

* OTA demo MQTT interface cleanup

* Fix OTA demo due to folder structure update

* Remove hard coded cert from OTA demo

* Use OTA error codes for OTA MQTT interface

* Add sending start event from the OTA demo

* Implement MQTT unscubscribe function for OTA MQTT

* Update OTA submodule and remove jsmn, mbedtls

* Update OTA submodule

* Update .gitmodules URL and ota target commit

* Update OTA submodule

* Update OTA submodule and remove iot_appversion32.h

* Update naming convention in OTA demo and OTA submodule

* Add platform state update in OTA pal for linux

* Update OTA demo loop for receivng notification

* Update OTA submodule

* Update OTA submodule

* Add sleep in the OTA demo loop for all conditions

* Change to use https link for ota submodule

* Add payload size to callback log messages

* Remove OTA OS Posix implementation from Paltforms

* OTA demo directory change

* renaming demos

* Refactor MQTT initialization and connection functions

* OTA over HTTP demo use local mqtt context

* Add http interface to OTA_AgentInit

* Add http utils functions

* Update HTTP utils functions

* OTA over HTTP cmake update for HTTP sources

* Implement OTA http request function

* Implement http deinit function

* Add HTTP port to OTA HTTP demo config

* Update OTA submodule

* Fix OTA platform cmake file

* Remove aws_ota_agent_config.h

* Add ota_config.h for MQTT & HTTP demos

* change OTA logging to csdk logging in demos

* Change OTA logging to Csdk on Linux OTA pal

* Update OTA demo config files and OTA submodule

* Use application buffers in OTA MQTT demo

* Uncrustify demo

* use application memory in OTA HTTP

* remove ota pal file temporarily

* Adding OTA Linux Pal file

* Update with new PAL interface

* Update after PR review

* Update OTA submodule

* Refactor and cleanup OTA MQTT demo

* Format aws_ota_pal.c

* Update log strings in aws_ota_pal.c

* Fix typos in aws_ota_pal.c (#47)

* Fix space formatting in aws_ota_mqtt.c (#48)

* Update OTA submodule

* Added OTA Pal unit tests (#50)

* ota pal unittests addition

* Added more ota pal unittest coverage

* Fix file context check in ota linux pal

* Update OTA submodule

* Formatting changes (#52)

* formatting update (#53)

* Update OTA submodule

* Change OTA demo config and remove pal context checks

* Update OTA submodule

* Update OTA submodule

* Update coreHTTP submodule

* Fix OTA HTTP demo

* Update Thing Name to 128

* Update ota_config.h

* Build and CI checks fixed

* Fixing the CMakeLists.txt

* Coverity MISRA warning fix

* Fix spell check errors

* Fix uncrustify warning

* changes from review

* changes from review

* File, folder name changes.

* formatting and spell check fix

* fix spell check

* Update OTA submodule, demo, and posix pal

* Update HTTP submodule

* Clean up and remove duplicates in demo/lexicon.txt

* Fix OTA demo after merge with c-sdk main

* Temporarily remove ota demos

* Revert a cmake change

* Fix some compiler warnings in ota pal posix

* Update OTA submodule

* Update error codes in OTA pal and unit tests

* Add realpath to the file name. (#57)

* Add realpath to the file name.

* Update OTA submodule

* Fix unused variable warning

* Fix ota pal unit tests

* Remove the private key for ota unit tests

* Update CMake files for OTA test mocks

* Mock fseek and fwrite in OTA PAL tests

* Enhance OTA PAL posix unit test coverage

Get 100% coverage for:
* otaPal_Abort
* otaPal_CreateFileForRx
* otaPal_WriteBlock

* Fix build errors in posix OTA PAL tests

* Mock OpenSSL and stdio func. for OTA unit tests

* Rewrite otaPal_GetPlatformImageState unit tests

* tmp commit for initial attempt

* Add helper function to fail a single mock call

* Update unit tests for otaPal_SetPlatformImageState

* Add mock helper functions to OTA PAL unit tests

* Fix some mistakes in the OTA PAL unit tests

* Fix OTA PAL unit test variable names

* Get OTA PAL unit test coverage over 90%

* Remove some of the alias funcs

* Remove impossible condition from OTA PAL

* Update OTA PAL unit tests for 100% coverage

* Remove unnecessary alias mocks from OTA PAL tests

* Organize OTA PAL unit tests

* Start to remove global usage

* Update OTA PAL unit tests for 100% coverage

* Remove globals from OTA PAL unit tests

* Uncrustify OTA PAL test files

* Fix spelling in OTA PAL unit tests

* Fix mistake in OTA PAL unit tests cmake

* Add mock header include to OTA PAL test

* Fix warnings in OTA PAL unit tests

* Fix incorrect OTA PAL test asserts

* Fix typos in the lexicon files

* Remove unnecessary content from ota.yml

* Remove unnecessary OTA PAL unit test.

* Clarify variable in OTA PAL unit tests

* Remove unnecessary configs from ota.yml

* Update spelling and formatting for OTA PAL

* Address PR feedback for OTA PAL unit tests

* Uncrustify OTA PAL unit test code

Co-authored-by: Tiangang Song <[email protected]>
Co-authored-by: Prasad Vyawahare <[email protected]>
Co-authored-by: Xie <[email protected]>
Co-authored-by: xuelix <[email protected]>
Co-authored-by: xuelix <[email protected]>
Co-authored-by: Shubham Divekar <[email protected]>
  • Loading branch information
7 people authored Dec 8, 2020
1 parent c676cab commit 14f512a
Show file tree
Hide file tree
Showing 12 changed files with 1,156 additions and 586 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ if(${BUILD_TESTS})
# Create a list for each unit test target.
set(utest_targets
openssl_utest sockets_utest
plaintext_utest clock_utest)
plaintext_utest clock_utest ota_pal_posix_utest)

# Add a target for running coverage on tests.
add_custom_target(coverage
Expand Down
2 changes: 2 additions & 0 deletions demos/lexicon.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

accel
ack
acks
Expand Down Expand Up @@ -266,6 +267,7 @@ lastcontrolpacketsent
latestversion
len
leurent
lfilecloseresult
libc
libmosquitto
libpkcs
Expand Down
13 changes: 12 additions & 1 deletion platform/lexicon.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ aws
backoff
backoffdelay
basedefs
bio
bitmasking
bool
bootable
Expand All @@ -29,6 +30,7 @@ const
couldn
coverity
crt
crypto
csdk
cwd
didn
Expand All @@ -40,10 +42,12 @@ endcode
endif
enum
enums
eof
epalstate
errno
errornumber
esavedagentstate
evp
ewouldblock
exe
expectedstatus
Expand All @@ -60,13 +64,16 @@ fopen
fread
freertos
fseek
fseeksuccessreturn
functionname
functionpage
functionspage
functiontofail
fwrite
fwriteerrorreturn
getaddrinfo
getcwd
h
hostnamelength
html
http
Expand All @@ -79,6 +86,7 @@ int
iot
ip
ip
lfilecloseresult
linux
logpath
longjmp
Expand Down Expand Up @@ -113,9 +121,11 @@ otafile
otaimagestateaborted
otaimagestateaccepted
otaimagestaterejected
otaimagestatetesting
otaimagestateinvalid
otaimagestatependingcommit
otaimagestateunknown
otalastimagestate
otapalimagestateinvalid
otapalimagestatependingcommit
otapalimagestateunknown
Expand Down Expand Up @@ -175,6 +185,7 @@ sizeof
sleeptimems
sni
snihostname
snprintf
sockaddr
sockets_invalid_parameter
socketstatus
Expand Down Expand Up @@ -232,4 +243,4 @@ otapalactivatefailed
otapalfileabort
otapalfileclose
pdata
pfilecontext
pfilecontext
2 changes: 1 addition & 1 deletion platform/posix/ota_pal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ target_link_libraries( ota_pal
INTERFACE ${OPENSSL_CRYPTO_LIBRARY}
)

if(BUILD_TESTS)
if(${BUILD_TESTS})
add_subdirectory(utest)
endif()
59 changes: 26 additions & 33 deletions platform/posix/ota_pal/source/ota_pal_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,10 @@ static OtaPalMainStatus_t Openssl_DigestVerifyStart( EVP_MD_CTX * pSigContext,
OtaPalMainStatus_t mainErr = OtaPalSignatureCheckFailed;

assert( pBuf != NULL );
assert( ( pSigContext != NULL ) && ( pPkey != NULL ) );

/* Verify an ECDSA-SHA256 signature. */
if( ( pSigContext != NULL ) &&
( pPkey != NULL ) &&
( pFile != NULL ) &&
if( ( pFile != NULL ) &&
( 1 == EVP_DigestVerifyInit( pSigContext, NULL, EVP_sha256(), NULL, pPkey ) ) )
{
LogDebug( ( "Started signature verification." ) );
Expand Down Expand Up @@ -196,10 +195,12 @@ static bool Openssl_DigestVerifyUpdate( EVP_MD_CTX * pSigContext,
/* coverity[misra_c_2012_rule_21_6_violation] */
bytesRead = fread( pBuf, 1U, OTA_PAL_POSIX_BUF_SIZE, pFile );

assert( bytesRead < OTA_PAL_POSIX_BUF_SIZE );

/* feof returns non-zero if end of file is reached, otherwise it returns 0. When
* bytesRead is not equal to OTA_PAL_POSIX_BUF_SIZE, we should be reading last
* chunk and reach to end of file. */
if( ( bytesRead < OTA_PAL_POSIX_BUF_SIZE ) && ( 0 == feof( pFile ) ) )
if( 0 == feof( pFile ) )
{
break;
}
Expand Down Expand Up @@ -263,44 +264,36 @@ static OtaPalStatus_t otaPal_CheckFileSignature( OtaFileContext_t * const C )
EVP_PKEY * pPkey = NULL;
EVP_MD_CTX * pSigContext = NULL;

if( C != NULL )
{
/* Extract the signer cert from the file */
pPkey = Openssl_GetPkeyFromCertificate( C->pCertFilepath );
assert( C != NULL );

/* Create a new signature context for verification purpose */
pSigContext = EVP_MD_CTX_new();
/* Extract the signer cert from the file. */
pPkey = Openssl_GetPkeyFromCertificate( C->pCertFilepath );

if( ( pPkey != NULL ) && ( pSigContext != NULL ) )
/* Create a new signature context for verification purpose. */
pSigContext = EVP_MD_CTX_new();

if( ( pPkey != NULL ) && ( pSigContext != NULL ) )
{
/* Verify the signature. */
mainErr = Openssl_DigestVerify( pSigContext, pPkey, C->pFile, C->pSignature );
}
else
{
if( pSigContext == NULL )
{
/* Verify an ECDSA-SHA256 signature. */
mainErr = Openssl_DigestVerify( pSigContext, pPkey, C->pFile, C->pSignature );
LogError( ( "File signature check failed at NEW sig context." ) );
}
else
{
if( pSigContext == NULL )
{
LogError( ( "File signature check failed at NEW sig context." ) );
}
else
{
LogError( ( "File signature check failed at EXTRACT pkey from signer certificate." ) );
mainErr = OtaPalBadSignerCert;
}
LogError( ( "File signature check failed at EXTRACT pkey from signer certificate." ) );
mainErr = OtaPalBadSignerCert;
}

/* Free up objects */
EVP_MD_CTX_free( pSigContext );
EVP_PKEY_free( pPkey );
}
else
{
LogError( ( "Failed to check file signature: Paramater check failed: "
" Invalid OTA file context." ) );
/* Invalid OTA context or file pointer. */
mainErr = OtaPalNullFileContext;
}

/* Free up objects */
EVP_MD_CTX_free( pSigContext );
EVP_PKEY_free( pPkey );

return OTA_PAL_COMBINE_ERR( mainErr, 0 );
}

Expand Down
23 changes: 14 additions & 9 deletions platform/posix/ota_pal/utest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ set( project_name "ota_pal" )

#list the files to mock here
list( APPEND mock_list
""
${CMAKE_CURRENT_LIST_DIR}/mocks/stdio_api.h
${CMAKE_CURRENT_LIST_DIR}/mocks/openssl_api.h
)
#list the directories your mocks need
list( APPEND mock_include_list
""
${LOGGING_INCLUDE_DIRS}
${PLATFORM_DIR}/include
)
#list the definitions of your mocks to control what to be included
list( APPEND mock_define_list
Expand All @@ -32,6 +34,7 @@ list( APPEND real_include_directories
"${PLATFORM_DIR}/posix/ota_pal/source/include"
${OPENSSL_INCLUDE_DIR}
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/mocks
)

#== =================== Create UnitTest Code here (edit) =====================
Expand All @@ -41,12 +44,20 @@ list ( APPEND test_include_directories
${CMAKE_CURRENT_BINARY_DIR}/include
${CMAKE_CURRENT_LIST_DIR}
${MODULES_DIR}/aws/ota-for-aws-iot-embedded-sdk/source/include
/usr/include/x86_64-linux-gnu/sys
mocks
)

#== =========================== (end edit) ===================================
set ( mock_name "" )
set ( mock_name "ota_pal_mocks" )
set ( real_name "ota_pal_real" )

create_mock_list(${mock_name}
"${mock_list}"
"${ROOT_DIR}/tools/cmock/ota.yml"
"${mock_include_list}"
"${mock_define_list}"
)

create_real_library ( ${real_name}
"${real_source_files}"
Expand All @@ -56,7 +67,6 @@ create_real_library ( ${real_name}

list(APPEND utest_link_list
lib${real_name}.a
${OPENSSL_LIBRARIES}
)

list ( APPEND utest_dep_list
Expand All @@ -71,8 +81,3 @@ create_test ( ${utest_name}
"${utest_dep_list}"
"${test_include_directories}"
)

# Unit tests configuration
set(OTA_PAL_UTEST_CERT_FILE "${CMAKE_CURRENT_LIST_DIR}/test_files/ecdsa-sha256-signer.crt.pem")
set(OTA_PAL_UTEST_FIRMWARE_FILE "${CMAKE_BINARY_DIR}/bin/tests/dummy.bin")
configure_file(ota_utest_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/include/ota_utest_config.h)
Loading

0 comments on commit 14f512a

Please sign in to comment.