diff --git a/.github/workflows/unit-test-with-cfg.yml b/.github/workflows/unit-test-with-cfg.yml index 0991ba6519..221d7fb2a4 100644 --- a/.github/workflows/unit-test-with-cfg.yml +++ b/.github/workflows/unit-test-with-cfg.yml @@ -74,6 +74,10 @@ jobs: cmake -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_BUILD_TYPE=${{ inputs.build_type }} ${{ steps.cmake_flags.outputs.compiler }} ${{ inputs.build_args }} ${{ inputs.install_mbedtls && '-DBUILD_MBEDTLS=OFF' || '' }} ${{ inputs.install_tinycbor && '-DBUILD_TINYCBOR=OFF' || '' }} -DBUILD_TESTING=ON .. make oc-unittests + - name: Setup firewall to drop TCP IPV6 with destination port 12345 (for timeout tests) + run: | + sudo ip6tables -I OUTPUT -p tcp --dport 12345 -j DROP + - name: Run unit tests run: | cd build diff --git a/port/linux/tcpsession.c b/port/linux/tcpsession.c index 448a7bf6eb..521156b9b8 100644 --- a/port/linux/tcpsession.c +++ b/port/linux/tcpsession.c @@ -86,6 +86,7 @@ typedef struct tcp_waiting_session_t oc_clock_time_t start; uint8_t count; uint8_t force; + int error; } retry; OC_LIST_STRUCT(messages); on_tcp_connect_t on_tcp_connect; @@ -1115,7 +1116,7 @@ tcp_cleanup_connected_waiting_session_locked(tcp_waiting_session_t *ws, } static bool -tcp_try_connect_waiting_session_locked(tcp_waiting_session_t *ws) +tcp_try_connect_waiting_session_locked(tcp_waiting_session_t *ws, int *err) { assert(ws != NULL && ws->sock != -1); int error = 0; @@ -1125,6 +1126,7 @@ tcp_try_connect_waiting_session_locked(tcp_waiting_session_t *ws) return false; /* Solaris pending error */ } if (error != 0) { + *err = error; OC_ERR("socket error: %d", error); return false; } @@ -1154,6 +1156,7 @@ enum { TCP_WAITING_SESSION_VALID, TCP_WAITING_SESSION_RETRY, TCP_WAITING_SESSION_EXPIRED, + TCP_WAITING_SESSION_ERROR, }; static int @@ -1161,6 +1164,9 @@ tcp_waiting_session_check(const tcp_waiting_session_t *session, oc_clock_time_t now, oc_tcp_connect_retry_t connect_retry) { + if (session->retry.error != 0) { + return TCP_WAITING_SESSION_ERROR; + } bool retry = session->retry.force != 0; if (!retry) { oc_clock_time_t expires_in = connect_retry.timeout * OC_CLOCK_SECOND; @@ -1247,6 +1253,9 @@ tcp_check_expiring_sessions(oc_clock_time_t now) } break; } + case TCP_WAITING_SESSION_ERROR: + free_waiting_session_locked(ws, false, false); + break; case TCP_WAITING_SESSION_EXPIRED: free_waiting_session_locked(ws, true, false); break; @@ -1262,14 +1271,21 @@ tcp_check_expiring_sessions(oc_clock_time_t now) static void tcp_process_waiting_session_locked(tcp_waiting_session_t *ws) { - if (!tcp_try_connect_waiting_session_locked(ws)) { + int error = 0; + if (!tcp_try_connect_waiting_session_locked(ws, &error)) { OC_DBG("failed to connect session(%p, fd=%d)", (void *)ws, ws->sock); if (ws->sock >= 0) { tcp_context_cfds_fd_clr(&ws->dev->tcp, ws->sock); close(ws->sock); ws->sock = -1; } - ws->retry.force = 1; + if (error == 0) { + ws->retry.force = 1; + ws->retry.error = 0; + } else { + // close the connection + ws->retry.error = 1; + } } } diff --git a/port/unittest/connectivitytest.cpp b/port/unittest/connectivitytest.cpp index e07ff8d078..3abda32779 100644 --- a/port/unittest/connectivitytest.cpp +++ b/port/unittest/connectivitytest.cpp @@ -431,7 +431,7 @@ TEST_F(TestConnectivityWithServer, oc_tcp_connect_timeout) oc_endpoint_t ep = oc::endpoint::FromString( "coaps+tcp://[::1]:12345"); // reachable address, but inactive port // enough retries so they will run the whole duration of this test - oc_tcp_set_connect_retry(UINT8_MAX, 5); + oc_tcp_set_connect_retry(0, 5); auto restore_defaults = []() { oc_tcp_set_connect_retry(OC_TCP_CONNECT_RETRY_MAX_COUNT, OC_TCP_CONNECT_RETRY_TIMEOUT); @@ -456,8 +456,7 @@ TEST_F(TestConnectivityWithServer, oc_tcp_connect_timeout) EXPECT_EQ(OC_SEND_MESSAGE_QUEUED, oc_send_buffer2(msg, true)); OC_DBG("oc_tcp_connect_timeout wait"); - oc_tcp_set_connect_retry(0, 2); - oc::TestDevice::PoolEvents(2); + oc::TestDevice::PoolEvents(10); EXPECT_EQ(-1, oc_tcp_connection_state(&ep)); oc_message_unref(msg); diff --git a/port/unittest/dnstest.cpp b/port/unittest/dnstest.cpp index b7fb773d91..d926365261 100644 --- a/port/unittest/dnstest.cpp +++ b/port/unittest/dnstest.cpp @@ -52,6 +52,7 @@ TEST_F(TestDNS, oc_dns_lookup_invalid) static_cast(0))); oc_string_t addr; + memset(&addr, 0, sizeof(oc_string_t)); EXPECT_EQ(-1, oc_dns_lookup(nullptr, &addr, static_cast(0))); EXPECT_NE(0, oc_dns_lookup("openconnectivity", &addr, @@ -61,6 +62,7 @@ TEST_F(TestDNS, oc_dns_lookup_invalid) TEST_F(TestDNS, oc_dns_lookup) { oc_string_t addr; + memset(&addr, 0, sizeof(oc_string_t)); EXPECT_EQ(0, oc_dns_lookup("localhost", &addr, IPV6)); oc_free_string(&addr); } @@ -70,6 +72,7 @@ TEST_F(TestDNS, oc_dns_lookup) TEST_F(TestDNS, oc_dns_lookup_ipv4) { oc_string_t addr; + memset(&addr, 0, sizeof(oc_string_t)); EXPECT_EQ(0, oc_dns_lookup("localhost", &addr, IPV4)); oc_free_string(&addr); }