Comprehensive test suite for the CheckEncapsulationInactivity() function from source/src/ports/generic_networkhandler.c. This function monitors TCP socket encapsulation activity and closes inactive connections that exceed a configured timeout threshold.
void CheckEncapsulationInactivity(int socket_handle)
Purpose: Checks if a socket has exceeded the encapsulation inactivity timeout and closes the connection if necessary.
Parameters:
socket_handle: The socket to check for inactivityDependencies:
g_tcpip.encapsulation_inactivity_timeout: Global timeout configuration (in seconds)g_actual_time: Current time in millisecondsg_timestamps[]: Array of socket timers tracking last update timesGetSessionFromSocket(): Maps socket to session handleCloseClass3ConnectionBasedOnSession(): Closes Class 3 connectionsCloseTcpSocket(): Closes the TCP socketRemoveSession(): Removes the session from trackingsource/tests/ports/check_encapsulation_inactivity_tests.cpp
12 comprehensive tests
TimeoutDisabledDoesNothingPurpose: Verify that when encapsulation inactivity timeout is disabled (value = 0), no connection cleanup occurs regardless of elapsed time.
Setup:
Expected: No mock calls (no cleanup)
InactivityBelowThresholdDoesNothingPurpose: Verify that connections remain open when elapsed time is below the timeout threshold.
Setup:
Expected: No cleanup (elapsed < threshold)
InactivityThresholdExceededClosesConnectionPurpose: Verify complete cleanup sequence when inactivity timeout is exceeded.
Setup:
Expected Behavior:
GetSessionFromSocket(3) called → returns session 2CloseClass3ConnectionBasedOnSession(2) calledCloseTcpSocket(3) calledRemoveSession(3) calledInvalidSocketTimerDoesNothingPurpose: Verify graceful handling when socket has no timer allocated.
Setup:
Expected: Function returns early, no cleanup (socket_timer is NULL)
ExactTimeoutBoundaryClosesConnectionPurpose: Verify connection closes at exact timeout boundary (>= comparison).
Setup:
Expected: Connection closed (diff >= threshold)
JustBelowTimeoutBoundaryDoesNotClosePurpose: Verify off-by-one handling - no close when just below threshold.
Setup:
Expected: No cleanup (diff < threshold)
MultipleSocketsIndependentEvaluationPurpose: Verify that multiple sockets are evaluated independently with correct timeout semantics.
Setup:
Expected:
ZeroTimeoutDisablesFeaturePurpose: Verify timeout = 0 disables the feature.
Setup:
Expected: Feature disabled, no cleanup
LargeTimeoutValuePurpose: Verify handling of very large timeout values (e.g., 1 hour).
Setup:
Expected: Connection remains open (30 min < 1 hour)
MinimumOneSecondTimeoutPurpose: Verify behavior with minimum practical timeout value.
Setup:
Expected: Connection closed (1500ms > 1000ms)
SocketAtArrayBoundaryPurpose: Verify correct behavior for sockets at the boundary of the timer array.
Setup:
Expected: Works correctly at array edge
OffByOneBoundaryTestPurpose: Comprehensive boundary testing with two threshold checks.
Setup:
Sequence:
Expected: Demonstrates exact boundary behavior
The tests use CppUTest mocking framework to verify the function's interaction with dependencies:
GetSessionFromSocket(int socket_handle)
CloseClass3ConnectionBasedOnSession(CipSessionHandle session)
CloseTcpSocket(int socket)
RemoveSession(int socket)
OK (246 tests, 12 ran, 24 checks, 0 ignored, 234 filtered out, 1 ms)
All 12 tests PASSED
Timeout Semantics: The function uses >= comparison (delta >= threshold), closing connections at or after the threshold.
Feature Enablement: Timeout must be > 0 to enable the feature. A value of 0 disables all inactivity checking.
Time Unit: Timeout is in seconds, converted to milliseconds internally (1000 * g_tcpip.encapsulation_inactivity_timeout).
Graceful Degradation: If socket timer lookup fails (socket not found), the function safely returns without attempting cleanup.
Call Sequence: When timeout is exceeded, the function always calls in this order:
Run all CheckEncapsulationInactivity tests:
cd /home/mmm/OpENer/bin/posix
./tests/OpENer_Tests -v -g CheckEncapsulationInactivity
Run with detailed output:
./tests/OpENer_Tests -v -c -g CheckEncapsulationInactivity
Run all tests:
./tests/OpENer_Tests
The test suite covers:
Tests are designed to be deterministic and independent, with each test's setup and teardown managed through the TEST_GROUP setup/teardown methods.