Skip to content

Commit c79a25b

Browse files
rlubosfabiobaltieri
authored andcommitted
tests: net: tcp: Fix a few issues with TCP tests
The net_context-based TCP tests required some adjustments/fixes: * seq number was not incremented for the final ACK after FIN * accepted net_context was not dereferenced (with net_context_put()) * net_context_put() should be used instead of net_tcp_put(), as it's the net_context that is allocated in the test (with net_context_get()) * out of order tests depend on each other, therefore it must be assured they're executed in the correct order Signed-off-by: Robert Lubos <[email protected]>
1 parent 690140b commit c79a25b

File tree

1 file changed

+46
-7
lines changed

1 file changed

+46
-7
lines changed

tests/net/tcp/src/main.c

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,8 @@ static void test_tcp_recv_cb(struct net_context *context,
714714
}
715715
}
716716

717+
static struct net_context *accepted_ctx;
718+
717719
static void test_tcp_accept_cb(struct net_context *ctx,
718720
struct sockaddr *addr,
719721
socklen_t addrlen,
@@ -726,6 +728,10 @@ static void test_tcp_accept_cb(struct net_context *ctx,
726728

727729
/* set callback on newly created context */
728730
ctx->recv_cb = test_tcp_recv_cb;
731+
accepted_ctx = ctx;
732+
733+
/* Ref the context on the app behalf. */
734+
net_context_ref(ctx);
729735

730736
test_sem_give();
731737
}
@@ -755,6 +761,8 @@ ZTEST(net_tcp, test_server_ipv4)
755761
zassert_true(false, "Failed to get net_context");
756762
}
757763

764+
net_context_ref(ctx);
765+
758766
ret = net_context_bind(ctx, (struct sockaddr *)&my_addr_s,
759767
sizeof(struct sockaddr_in));
760768
if (ret < 0) {
@@ -790,7 +798,11 @@ ZTEST(net_tcp, test_server_ipv4)
790798
/* Trigger the peer to send FIN after timeout */
791799
k_work_reschedule(&test_server, K_NO_WAIT);
792800

801+
/* Let the receiving thread run */
802+
k_msleep(50);
803+
793804
net_context_put(ctx);
805+
net_context_put(accepted_ctx);
794806
}
795807

796808
/* Test case scenario IPv4
@@ -818,6 +830,8 @@ ZTEST(net_tcp, test_server_with_options_ipv4)
818830
zassert_true(false, "Failed to get net_context");
819831
}
820832

833+
net_context_ref(ctx);
834+
821835
ret = net_context_bind(ctx, (struct sockaddr *)&my_addr_s,
822836
sizeof(struct sockaddr_in));
823837
if (ret < 0) {
@@ -853,7 +867,11 @@ ZTEST(net_tcp, test_server_with_options_ipv4)
853867
/* Trigger the peer to send FIN after timeout */
854868
k_work_reschedule(&test_server, K_NO_WAIT);
855869

870+
/* Let the receiving thread run */
871+
k_msleep(50);
872+
856873
net_context_put(ctx);
874+
net_context_put(accepted_ctx);
857875
}
858876

859877
/* Test case scenario IPv6
@@ -881,6 +899,8 @@ ZTEST(net_tcp, test_server_ipv6)
881899
zassert_true(false, "Failed to get net_context");
882900
}
883901

902+
net_context_ref(ctx);
903+
884904
ret = net_context_bind(ctx, (struct sockaddr *)&my_addr_v6_s,
885905
sizeof(struct sockaddr_in6));
886906
if (ret < 0) {
@@ -916,7 +936,11 @@ ZTEST(net_tcp, test_server_ipv6)
916936
/* Trigger the peer to send FIN after timeout */
917937
k_work_reschedule(&test_server, K_NO_WAIT);
918938

939+
/* Let the receiving thread run */
940+
k_msleep(50);
941+
919942
net_context_put(ctx);
943+
net_context_put(accepted_ctx);
920944
}
921945

922946
static void handle_syn_resend(void)
@@ -950,6 +974,8 @@ ZTEST(net_tcp, test_client_syn_resend)
950974
zassert_true(false, "Failed to get net_context");
951975
}
952976

977+
net_context_ref(ctx);
978+
953979
ret = net_context_connect(ctx, (struct sockaddr *)&peer_addr_s,
954980
sizeof(struct sockaddr_in),
955981
NULL,
@@ -1075,7 +1101,7 @@ ZTEST(net_tcp, test_client_fin_wait_2_ipv4)
10751101
/* Peer will release the semaphore after it sends ACK for data */
10761102
test_sem_take(K_MSEC(100), __LINE__);
10771103

1078-
net_tcp_put(ctx);
1104+
net_context_put(ctx);
10791105

10801106
/* Peer will release the semaphore after it receives
10811107
* proper ACK to FIN | ACK
@@ -1125,6 +1151,7 @@ static void handle_client_closing_test(sa_family_t af, struct tcphdr *th)
11251151
case T_CLOSING:
11261152
test_verify_flags(th, ACK);
11271153
t_state = T_FIN_ACK;
1154+
seq++;
11281155
reply = prepare_ack_packet(af, htons(MY_PORT), th->th_sport);
11291156
break;
11301157
default:
@@ -1196,7 +1223,7 @@ ZTEST(net_tcp, test_client_closing_ipv6)
11961223
/* Peer will release the semaphore after it sends ACK for data */
11971224
test_sem_take(K_MSEC(100), __LINE__);
11981225

1199-
net_tcp_put(ctx);
1226+
net_context_put(ctx);
12001227

12011228
/* Peer will release the semaphore after it receives
12021229
* proper ACK to FIN | ACK
@@ -1225,6 +1252,8 @@ static struct net_context *create_server_socket(uint32_t my_seq,
12251252
zassert_true(false, "Failed to get net_context");
12261253
}
12271254

1255+
net_context_ref(ctx);
1256+
12281257
ret = net_context_bind(ctx, (struct sockaddr *)&my_addr_v6_s,
12291258
sizeof(struct sockaddr_in6));
12301259
if (ret < 0) {
@@ -1309,7 +1338,8 @@ static void check_rst_succeed(struct net_context *ctx,
13091338
"RST packet skipped (before %d, after %d)",
13101339
rsterr_before, rsterr_after);
13111340

1312-
net_tcp_put(ctx);
1341+
net_context_put(ctx);
1342+
net_context_put(accepted_ctx);
13131343
}
13141344

13151345
ZTEST(net_tcp, test_client_invalid_rst)
@@ -1332,7 +1362,7 @@ ZTEST(net_tcp, test_client_invalid_rst)
13321362
check_rst_succeed(NULL, 0);
13331363
check_rst_succeed(NULL, 1);
13341364

1335-
net_tcp_put(ctx);
1365+
/* net_context is released within check_rst_succeed() */
13361366
}
13371367

13381368
#define MAX_DATA 100
@@ -1431,13 +1461,15 @@ static void checklist_based_out_of_order_test(struct out_of_order_check_struct *
14311461
}
14321462
}
14331463

1434-
ZTEST(net_tcp, test_server_recv_out_of_order_data)
1464+
static void test_server_recv_out_of_order_data(void)
14351465
{
14361466
/* Only run the tests if queueing is enabled */
14371467
if (CONFIG_NET_TCP_RECV_QUEUE_TIMEOUT == 0) {
14381468
return;
14391469
}
14401470

1471+
k_sem_reset(&test_sem);
1472+
14411473
/* Start the sequence numbering so that we will wrap it (just for
14421474
* testing purposes)
14431475
*/
@@ -1466,7 +1498,7 @@ struct out_of_order_check_struct reorder_timeout_list[] = {
14661498
* test_server_recv_out_of_order_data(), so this test must be run after that
14671499
* test.
14681500
*/
1469-
ZTEST(net_tcp, test_server_timeout_out_of_order_data)
1501+
static void test_server_timeout_out_of_order_data(void)
14701502
{
14711503
if (CONFIG_NET_TCP_RECV_QUEUE_TIMEOUT == 0) {
14721504
return;
@@ -1478,7 +1510,14 @@ ZTEST(net_tcp, test_server_timeout_out_of_order_data)
14781510
ARRAY_SIZE(reorder_timeout_list),
14791511
OUT_OF_ORDER_SEQ_INIT + 1);
14801512

1481-
net_tcp_put(ooo_ctx);
1513+
net_context_put(ooo_ctx);
1514+
net_context_put(accepted_ctx);
1515+
}
1516+
1517+
ZTEST(net_tcp, test_server_out_of_order_data)
1518+
{
1519+
test_server_recv_out_of_order_data();
1520+
test_server_timeout_out_of_order_data();
14821521
}
14831522

14841523
ZTEST_SUITE(net_tcp, NULL, presetup, NULL, NULL, NULL);

0 commit comments

Comments
 (0)