Skip to content

Commit d7b04a1

Browse files
Update unit tests to work with asserts enabled (#322)
Description ----------- This PR solves the Issue #321. **Learning:** Unit tests are supposed to be built with asserts enabled during the development process. But when these are built for coverage analysis these should be built with asserts disabled. This is because the lcov tool would consider all the branches in the asserts as uncovered. This PR: 1. Update the unit tests to work with asserts enabled 2. Update the code to have 100% branch coverage. To achieve this, one branch which was logically unreachable, was excluded from the coverage analysis. 3. Updates the CI checks to build the code with asserts to check if all the unit tests are working as expected. Along with this it will also build the code without asserts to check coverage. 4. Updates the command in the README.md that users should use to build the unit tests. This command has asserts disabled, as asserts are only required during the development process of the unit tests. Test Steps ----------- The unit tests are building and running successfully with asserts. They also give 100% coverage without asserts. Checklist: ---------- <!--- Go over all the following points, and put an `x` in all the boxes that apply. --> <!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [x] I have tested my changes. No regression in existing tests. - [x] I have modified and/or added unit-tests to cover the code changes in this Pull Request. Related Issue ----------- #321 <!-- If any, please provide issue ID. --> By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 42d843f commit d7b04a1

File tree

5 files changed

+51
-18
lines changed

5 files changed

+51
-18
lines changed

.github/.cSpellWords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,4 @@ Werror
5252
Wextra
5353
Wsign
5454
Wunused
55+
DCOV

.github/workflows/ci.yml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
uses: actions/checkout@v3
2222

2323
- env:
24-
stepName: Build CoreMQTT
24+
stepName: Build CoreMQTT with asserts
2525
run: |
2626
# ${{ env.stepName }}
2727
echo -e "::group::${{ env.bashInfo }} ${{ env.stepName }} ${{ env.bashEnd }}"
@@ -32,7 +32,29 @@ jobs:
3232
-DCMAKE_BUILD_TYPE=Debug \
3333
-DBUILD_CLONE_SUBMODULES=ON \
3434
-DUNITTEST=1 \
35-
-DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Wsign-compare -Werror -DNDEBUG -DLIBRARY_LOG_LEVEL=LOG_DEBUG'
35+
-DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Wsign-compare -Werror -DLIBRARY_LOG_LEVEL=LOG_DEBUG'
36+
make -C build/ all
37+
echo "::endgroup::"
38+
39+
echo -e "${{ env.bashPass }} ${{env.stepName}} ${{ env.bashEnd }}"
40+
41+
- name: Run System Tests with asserts
42+
run: ctest --test-dir build -E system --output-on-failure
43+
44+
- env:
45+
stepName: Build CoreMQTT without asserts for coverage analysis
46+
run: |
47+
# ${{ env.stepName }}
48+
echo -e "::group::${{ env.bashInfo }} ${{ env.stepName }} ${{ env.bashEnd }}"
49+
50+
sudo apt-get install -y lcov
51+
cmake -S test -B build/ \
52+
-G "Unix Makefiles" \
53+
-DCMAKE_BUILD_TYPE=Debug \
54+
-DBUILD_CLONE_SUBMODULES=ON \
55+
-DUNITTEST=1 \
56+
-DCOV_ANALYSIS=1 \
57+
-DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Wsign-compare -Werror -DLIBRARY_LOG_LEVEL=LOG_DEBUG'
3658
make -C build/ all
3759
echo "::endgroup::"
3860

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ git submodule update --checkout --init --recursive test/unit-test/CMock
176176
-DCMAKE_BUILD_TYPE=Debug \
177177
-DBUILD_CLONE_SUBMODULES=ON \
178178
-DUNITTEST=1 \
179-
-DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Wsign-compare -Werror -DNDEBUG -DLIBRARY_LOG_LEVEL=LOG_DEBUG'
179+
-DCOV_ANALYSIS=1 \
180+
-DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Wsign-compare -Werror -DLIBRARY_LOG_LEVEL=LOG_DEBUG'
180181
```
181182
For Mac machines:
182183
@@ -186,7 +187,8 @@ git submodule update --checkout --init --recursive test/unit-test/CMock
186187
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
187188
-DBUILD_CLONE_SUBMODULES=ON \
188189
-DUNITTEST=1 \
189-
-DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Wsign-compare -Werror -DNDEBUG -DLIBRARY_LOG_LEVEL=LOG_DEBUG' \
190+
-DCOV_ANALYSIS=1 \
191+
-DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Wsign-compare -Werror -DLIBRARY_LOG_LEVEL=LOG_DEBUG' \
190192
-DCMAKE_C_STANDARD=99
191193
```
192194

source/core_mqtt.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,9 +869,14 @@ static int32_t sendMessageVector( MQTTContext_t * pContext,
869869
}
870870

871871
/* Some of the bytes from this vector were sent as well, update the length
872-
* and the pointer to data in this vector. */
872+
* and the pointer to data in this vector. One branch in the following
873+
* condition logically cannot be reached as the iterator would always be
874+
* bounded if the sendResult is positive. If it were not then the assert
875+
* above in the function will be triggered and the flow will never reach
876+
* here. Hence for that sake the branches on this condition are excluded
877+
* from coverage analysis */
873878
if( ( sendResult > 0 ) &&
874-
( pIoVectIterator <= &( pIoVec[ ioVecCount - 1U ] ) ) )
879+
( pIoVectIterator <= &( pIoVec[ ioVecCount - 1U ] ) ) ) /* LCOV_EXCL_BR_LINE */
875880
{
876881
pIoVectIterator->iov_base = ( const void * ) &( ( ( const uint8_t * ) pIoVectIterator->iov_base )[ sendResult ] );
877882
pIoVectIterator->iov_len -= ( size_t ) sendResult;

test/unit-test/core_mqtt_utest.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,7 +1555,7 @@ void test_MQTT_Connect_sendConnect1( void )
15551555
size_t packetSize;
15561556

15571557
setupTransportInterface( &transport );
1558-
transport.writev = transportWritevFail;
1558+
transport.writev = NULL;
15591559
setupNetworkBuffer( &networkBuffer );
15601560

15611561
memset( &mqttContext, 0x0, sizeof( mqttContext ) );
@@ -1674,7 +1674,7 @@ void test_MQTT_Connect_ProperWIllInfo( void )
16741674
MQTTPublishInfo_t willInfo;
16751675

16761676
setupTransportInterface( &transport );
1677-
transport.writev = transportWritevFail;
1677+
transport.writev = NULL;
16781678
setupNetworkBuffer( &networkBuffer );
16791679

16801680
memset( &mqttContext, 0x0, sizeof( mqttContext ) );
@@ -1730,7 +1730,7 @@ void test_MQTT_Connect_ProperWIllInfoWithNoPayload( void )
17301730
MQTTPublishInfo_t willInfo;
17311731

17321732
setupTransportInterface( &transport );
1733-
transport.writev = transportWritevFail;
1733+
transport.writev = NULL;
17341734
setupNetworkBuffer( &networkBuffer );
17351735

17361736
memset( &mqttContext, 0x0, sizeof( mqttContext ) );
@@ -1785,7 +1785,7 @@ void test_MQTT_Connect_ProperWIllInfoWithPayloadButZeroLength( void )
17851785
MQTTPublishInfo_t willInfo;
17861786

17871787
setupTransportInterface( &transport );
1788-
transport.writev = transportWritevFail;
1788+
transport.writev = NULL;
17891789
setupNetworkBuffer( &networkBuffer );
17901790

17911791
memset( &mqttContext, 0x0, sizeof( mqttContext ) );
@@ -3476,7 +3476,8 @@ void test_MQTT_Publish7( void )
34763476

34773477
setupTransportInterface( &transport );
34783478
setupNetworkBuffer( &networkBuffer );
3479-
transport.writev = transportWritevFail;
3479+
transport.writev = NULL;
3480+
transport.send = transportSendFailure;
34803481

34813482
memset( &mqttContext, 0x0, sizeof( mqttContext ) );
34823483
memset( &publishInfo, 0x0, sizeof( publishInfo ) );
@@ -3508,7 +3509,7 @@ void test_MQTT_Publish8( void )
35083509

35093510
setupTransportInterface( &transport );
35103511
setupNetworkBuffer( &networkBuffer );
3511-
transport.writev = transportWritevFail;
3512+
transport.writev = NULL;
35123513

35133514
memset( &mqttContext, 0x0, sizeof( mqttContext ) );
35143515
memset( &publishInfo, 0x0, sizeof( publishInfo ) );
@@ -3794,7 +3795,7 @@ void test_MQTT_Publish_Send_Timeout( void )
37943795

37953796
setupNetworkBuffer( &networkBuffer );
37963797
setupTransportInterface( &transport );
3797-
transport.writev = transportWritevFail;
3798+
transport.writev = NULL;
37983799

37993800
/* Set the transport send function to the mock that always returns zero
38003801
* bytes for the test. */
@@ -5359,8 +5360,9 @@ void test_MQTT_ProcessLoop_handleKeepAlive_Error_Paths3( void )
53595360
context.waitingForPingResp = false;
53605361
/* Set expected return values in the loop. */
53615362
resetProcessLoopParams( &expectParams );
5362-
5363+
size_t packetSize = MQTT_PACKET_PINGREQ_SIZE;
53635364
MQTT_GetPingreqPacketSize_ExpectAnyArgsAndReturn( MQTTSuccess );
5365+
MQTT_GetPingreqPacketSize_ReturnThruPtr_pPacketSize( &packetSize );
53645366
MQTT_SerializePingreq_ExpectAnyArgsAndReturn( MQTTSuccess );
53655367

53665368
mqttStatus = MQTT_ProcessLoop( &context );
@@ -5417,8 +5419,9 @@ void test_MQTT_ProcessLoop_handleKeepAlive_Error_Paths4( void )
54175419

54185420
MQTT_ProcessIncomingPacketTypeAndLength_ExpectAnyArgsAndReturn( MQTTNeedMoreBytes );
54195421
MQTT_ProcessIncomingPacketTypeAndLength_ReturnThruPtr_pIncomingPacket( &incomingPacket );
5420-
5422+
size_t packetSize = MQTT_PACKET_PINGREQ_SIZE;
54215423
MQTT_GetPingreqPacketSize_ExpectAnyArgsAndReturn( MQTTSuccess );
5424+
MQTT_GetPingreqPacketSize_ReturnThruPtr_pPacketSize( &packetSize );
54225425
MQTT_SerializePingreq_ExpectAnyArgsAndReturn( MQTTSuccess );
54235426

54245427
mqttStatus = MQTT_ProcessLoop( &context );
@@ -5975,7 +5978,7 @@ void test_MQTT_Subscribe_error_paths1( void )
59755978
subscribeInfo.qos = MQTTQoS0;
59765979
setupTransportInterface( &transport );
59775980
transport.send = transportSendFailure;
5978-
transport.writev = transportWritevFail;
5981+
transport.writev = NULL;
59795982

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

64516454
transport.send = transportSendFailure;
64526455
transport.recv = transportRecvFailure;
6453-
transport.writev = transportWritevFail;
6456+
transport.writev = NULL;
64546457

64556458
/* Initialize context. */
64566459
mqttStatus = MQTT_Init( &context, &transport, getTime, eventCallback, &networkBuffer );
@@ -6492,7 +6495,7 @@ void test_MQTT_Unsubscribe_error_path2( void )
64926495
setupTransportInterface( &transport );
64936496
transport.send = transportSendFailure;
64946497
transport.recv = transportRecvFailure;
6495-
transport.writev = transportWritevFail;
6498+
transport.writev = NULL;
64966499

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

0 commit comments

Comments
 (0)