Skip to content

Update unit tests to work with asserts enabled #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/.cSpellWords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ Werror
Wextra
Wsign
Wunused
DCOV
26 changes: 24 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
uses: actions/checkout@v3

- env:
stepName: Build CoreMQTT
stepName: Build CoreMQTT with asserts
run: |
# ${{ env.stepName }}
echo -e "::group::${{ env.bashInfo }} ${{ env.stepName }} ${{ env.bashEnd }}"
Expand All @@ -32,7 +32,29 @@ jobs:
-DCMAKE_BUILD_TYPE=Debug \
-DBUILD_CLONE_SUBMODULES=ON \
-DUNITTEST=1 \
-DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Wsign-compare -Werror -DNDEBUG -DLIBRARY_LOG_LEVEL=LOG_DEBUG'
-DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Wsign-compare -Werror -DLIBRARY_LOG_LEVEL=LOG_DEBUG'
make -C build/ all
echo "::endgroup::"

echo -e "${{ env.bashPass }} ${{env.stepName}} ${{ env.bashEnd }}"

- name: Run System Tests with asserts
run: ctest --test-dir build -E system --output-on-failure

- env:
stepName: Build CoreMQTT without asserts for coverage analysis
run: |
# ${{ env.stepName }}
echo -e "::group::${{ env.bashInfo }} ${{ env.stepName }} ${{ env.bashEnd }}"

sudo apt-get install -y lcov
cmake -S test -B build/ \
-G "Unix Makefiles" \
-DCMAKE_BUILD_TYPE=Debug \
-DBUILD_CLONE_SUBMODULES=ON \
-DUNITTEST=1 \
-DCOV_ANALYSIS=1 \
-DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Wsign-compare -Werror -DLIBRARY_LOG_LEVEL=LOG_DEBUG'
make -C build/ all
echo "::endgroup::"

Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ git submodule update --checkout --init --recursive test/unit-test/CMock
-DCMAKE_BUILD_TYPE=Debug \
-DBUILD_CLONE_SUBMODULES=ON \
-DUNITTEST=1 \
-DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Wsign-compare -Werror -DNDEBUG -DLIBRARY_LOG_LEVEL=LOG_DEBUG'
-DCOV_ANALYSIS=1 \
-DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Wsign-compare -Werror -DLIBRARY_LOG_LEVEL=LOG_DEBUG'
```
For Mac machines:

Expand All @@ -186,7 +187,8 @@ git submodule update --checkout --init --recursive test/unit-test/CMock
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DBUILD_CLONE_SUBMODULES=ON \
-DUNITTEST=1 \
-DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Wsign-compare -Werror -DNDEBUG -DLIBRARY_LOG_LEVEL=LOG_DEBUG' \
-DCOV_ANALYSIS=1 \
-DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Wsign-compare -Werror -DLIBRARY_LOG_LEVEL=LOG_DEBUG' \
-DCMAKE_C_STANDARD=99
```

Expand Down
9 changes: 7 additions & 2 deletions source/core_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -869,9 +869,14 @@ static int32_t sendMessageVector( MQTTContext_t * pContext,
}

/* Some of the bytes from this vector were sent as well, update the length
* and the pointer to data in this vector. */
* and the pointer to data in this vector. One branch in the following
* condition logically cannot be reached as the iterator would always be
* bounded if the sendResult is positive. If it were not then the assert
* above in the function will be triggered and the flow will never reach
* here. Hence for that sake the branches on this condition are excluded
* from coverage analysis */
if( ( sendResult > 0 ) &&
( pIoVectIterator <= &( pIoVec[ ioVecCount - 1U ] ) ) )
( pIoVectIterator <= &( pIoVec[ ioVecCount - 1U ] ) ) ) /* LCOV_EXCL_BR_LINE */
{
pIoVectIterator->iov_base = ( const void * ) &( ( ( const uint8_t * ) pIoVectIterator->iov_base )[ sendResult ] );
pIoVectIterator->iov_len -= ( size_t ) sendResult;
Expand Down
27 changes: 15 additions & 12 deletions test/unit-test/core_mqtt_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,7 @@ void test_MQTT_Connect_sendConnect1( void )
size_t packetSize;

setupTransportInterface( &transport );
transport.writev = transportWritevFail;
transport.writev = NULL;
setupNetworkBuffer( &networkBuffer );

memset( &mqttContext, 0x0, sizeof( mqttContext ) );
Expand Down Expand Up @@ -1674,7 +1674,7 @@ void test_MQTT_Connect_ProperWIllInfo( void )
MQTTPublishInfo_t willInfo;

setupTransportInterface( &transport );
transport.writev = transportWritevFail;
transport.writev = NULL;
setupNetworkBuffer( &networkBuffer );

memset( &mqttContext, 0x0, sizeof( mqttContext ) );
Expand Down Expand Up @@ -1730,7 +1730,7 @@ void test_MQTT_Connect_ProperWIllInfoWithNoPayload( void )
MQTTPublishInfo_t willInfo;

setupTransportInterface( &transport );
transport.writev = transportWritevFail;
transport.writev = NULL;
setupNetworkBuffer( &networkBuffer );

memset( &mqttContext, 0x0, sizeof( mqttContext ) );
Expand Down Expand Up @@ -1785,7 +1785,7 @@ void test_MQTT_Connect_ProperWIllInfoWithPayloadButZeroLength( void )
MQTTPublishInfo_t willInfo;

setupTransportInterface( &transport );
transport.writev = transportWritevFail;
transport.writev = NULL;
setupNetworkBuffer( &networkBuffer );

memset( &mqttContext, 0x0, sizeof( mqttContext ) );
Expand Down Expand Up @@ -3476,7 +3476,8 @@ void test_MQTT_Publish7( void )

setupTransportInterface( &transport );
setupNetworkBuffer( &networkBuffer );
transport.writev = transportWritevFail;
transport.writev = NULL;
transport.send = transportSendFailure;

memset( &mqttContext, 0x0, sizeof( mqttContext ) );
memset( &publishInfo, 0x0, sizeof( publishInfo ) );
Expand Down Expand Up @@ -3508,7 +3509,7 @@ void test_MQTT_Publish8( void )

setupTransportInterface( &transport );
setupNetworkBuffer( &networkBuffer );
transport.writev = transportWritevFail;
transport.writev = NULL;

memset( &mqttContext, 0x0, sizeof( mqttContext ) );
memset( &publishInfo, 0x0, sizeof( publishInfo ) );
Expand Down Expand Up @@ -3794,7 +3795,7 @@ void test_MQTT_Publish_Send_Timeout( void )

setupNetworkBuffer( &networkBuffer );
setupTransportInterface( &transport );
transport.writev = transportWritevFail;
transport.writev = NULL;

/* Set the transport send function to the mock that always returns zero
* bytes for the test. */
Expand Down Expand Up @@ -5359,8 +5360,9 @@ void test_MQTT_ProcessLoop_handleKeepAlive_Error_Paths3( void )
context.waitingForPingResp = false;
/* Set expected return values in the loop. */
resetProcessLoopParams( &expectParams );

size_t packetSize = MQTT_PACKET_PINGREQ_SIZE;
MQTT_GetPingreqPacketSize_ExpectAnyArgsAndReturn( MQTTSuccess );
MQTT_GetPingreqPacketSize_ReturnThruPtr_pPacketSize( &packetSize );
MQTT_SerializePingreq_ExpectAnyArgsAndReturn( MQTTSuccess );

mqttStatus = MQTT_ProcessLoop( &context );
Expand Down Expand Up @@ -5417,8 +5419,9 @@ void test_MQTT_ProcessLoop_handleKeepAlive_Error_Paths4( void )

MQTT_ProcessIncomingPacketTypeAndLength_ExpectAnyArgsAndReturn( MQTTNeedMoreBytes );
MQTT_ProcessIncomingPacketTypeAndLength_ReturnThruPtr_pIncomingPacket( &incomingPacket );

size_t packetSize = MQTT_PACKET_PINGREQ_SIZE;
MQTT_GetPingreqPacketSize_ExpectAnyArgsAndReturn( MQTTSuccess );
MQTT_GetPingreqPacketSize_ReturnThruPtr_pPacketSize( &packetSize );
MQTT_SerializePingreq_ExpectAnyArgsAndReturn( MQTTSuccess );

mqttStatus = MQTT_ProcessLoop( &context );
Expand Down Expand Up @@ -5975,7 +5978,7 @@ void test_MQTT_Subscribe_error_paths1( void )
subscribeInfo.qos = MQTTQoS0;
setupTransportInterface( &transport );
transport.send = transportSendFailure;
transport.writev = transportWritevFail;
transport.writev = NULL;

/* Initialize context. */
mqttStatus = MQTT_Init( &context, &transport, getTime, eventCallback, &networkBuffer );
Expand Down Expand Up @@ -6450,7 +6453,7 @@ void test_MQTT_Unsubscribe_error_path1( void )

transport.send = transportSendFailure;
transport.recv = transportRecvFailure;
transport.writev = transportWritevFail;
transport.writev = NULL;

/* Initialize context. */
mqttStatus = MQTT_Init( &context, &transport, getTime, eventCallback, &networkBuffer );
Expand Down Expand Up @@ -6492,7 +6495,7 @@ void test_MQTT_Unsubscribe_error_path2( void )
setupTransportInterface( &transport );
transport.send = transportSendFailure;
transport.recv = transportRecvFailure;
transport.writev = transportWritevFail;
transport.writev = NULL;

/* Initialize context. */
mqttStatus = MQTT_Init( &context, &transport, getTime, eventCallback, &networkBuffer );
Expand Down