@@ -714,6 +714,8 @@ static void test_tcp_recv_cb(struct net_context *context,
714
714
}
715
715
}
716
716
717
+ static struct net_context * accepted_ctx ;
718
+
717
719
static void test_tcp_accept_cb (struct net_context * ctx ,
718
720
struct sockaddr * addr ,
719
721
socklen_t addrlen ,
@@ -726,6 +728,10 @@ static void test_tcp_accept_cb(struct net_context *ctx,
726
728
727
729
/* set callback on newly created context */
728
730
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 );
729
735
730
736
test_sem_give ();
731
737
}
@@ -755,6 +761,8 @@ ZTEST(net_tcp, test_server_ipv4)
755
761
zassert_true (false, "Failed to get net_context" );
756
762
}
757
763
764
+ net_context_ref (ctx );
765
+
758
766
ret = net_context_bind (ctx , (struct sockaddr * )& my_addr_s ,
759
767
sizeof (struct sockaddr_in ));
760
768
if (ret < 0 ) {
@@ -790,7 +798,11 @@ ZTEST(net_tcp, test_server_ipv4)
790
798
/* Trigger the peer to send FIN after timeout */
791
799
k_work_reschedule (& test_server , K_NO_WAIT );
792
800
801
+ /* Let the receiving thread run */
802
+ k_msleep (50 );
803
+
793
804
net_context_put (ctx );
805
+ net_context_put (accepted_ctx );
794
806
}
795
807
796
808
/* Test case scenario IPv4
@@ -818,6 +830,8 @@ ZTEST(net_tcp, test_server_with_options_ipv4)
818
830
zassert_true (false, "Failed to get net_context" );
819
831
}
820
832
833
+ net_context_ref (ctx );
834
+
821
835
ret = net_context_bind (ctx , (struct sockaddr * )& my_addr_s ,
822
836
sizeof (struct sockaddr_in ));
823
837
if (ret < 0 ) {
@@ -853,7 +867,11 @@ ZTEST(net_tcp, test_server_with_options_ipv4)
853
867
/* Trigger the peer to send FIN after timeout */
854
868
k_work_reschedule (& test_server , K_NO_WAIT );
855
869
870
+ /* Let the receiving thread run */
871
+ k_msleep (50 );
872
+
856
873
net_context_put (ctx );
874
+ net_context_put (accepted_ctx );
857
875
}
858
876
859
877
/* Test case scenario IPv6
@@ -881,6 +899,8 @@ ZTEST(net_tcp, test_server_ipv6)
881
899
zassert_true (false, "Failed to get net_context" );
882
900
}
883
901
902
+ net_context_ref (ctx );
903
+
884
904
ret = net_context_bind (ctx , (struct sockaddr * )& my_addr_v6_s ,
885
905
sizeof (struct sockaddr_in6 ));
886
906
if (ret < 0 ) {
@@ -916,7 +936,11 @@ ZTEST(net_tcp, test_server_ipv6)
916
936
/* Trigger the peer to send FIN after timeout */
917
937
k_work_reschedule (& test_server , K_NO_WAIT );
918
938
939
+ /* Let the receiving thread run */
940
+ k_msleep (50 );
941
+
919
942
net_context_put (ctx );
943
+ net_context_put (accepted_ctx );
920
944
}
921
945
922
946
static void handle_syn_resend (void )
@@ -950,6 +974,8 @@ ZTEST(net_tcp, test_client_syn_resend)
950
974
zassert_true (false, "Failed to get net_context" );
951
975
}
952
976
977
+ net_context_ref (ctx );
978
+
953
979
ret = net_context_connect (ctx , (struct sockaddr * )& peer_addr_s ,
954
980
sizeof (struct sockaddr_in ),
955
981
NULL ,
@@ -1075,7 +1101,7 @@ ZTEST(net_tcp, test_client_fin_wait_2_ipv4)
1075
1101
/* Peer will release the semaphore after it sends ACK for data */
1076
1102
test_sem_take (K_MSEC (100 ), __LINE__ );
1077
1103
1078
- net_tcp_put (ctx );
1104
+ net_context_put (ctx );
1079
1105
1080
1106
/* Peer will release the semaphore after it receives
1081
1107
* proper ACK to FIN | ACK
@@ -1125,6 +1151,7 @@ static void handle_client_closing_test(sa_family_t af, struct tcphdr *th)
1125
1151
case T_CLOSING :
1126
1152
test_verify_flags (th , ACK );
1127
1153
t_state = T_FIN_ACK ;
1154
+ seq ++ ;
1128
1155
reply = prepare_ack_packet (af , htons (MY_PORT ), th -> th_sport );
1129
1156
break ;
1130
1157
default :
@@ -1196,7 +1223,7 @@ ZTEST(net_tcp, test_client_closing_ipv6)
1196
1223
/* Peer will release the semaphore after it sends ACK for data */
1197
1224
test_sem_take (K_MSEC (100 ), __LINE__ );
1198
1225
1199
- net_tcp_put (ctx );
1226
+ net_context_put (ctx );
1200
1227
1201
1228
/* Peer will release the semaphore after it receives
1202
1229
* proper ACK to FIN | ACK
@@ -1225,6 +1252,8 @@ static struct net_context *create_server_socket(uint32_t my_seq,
1225
1252
zassert_true (false, "Failed to get net_context" );
1226
1253
}
1227
1254
1255
+ net_context_ref (ctx );
1256
+
1228
1257
ret = net_context_bind (ctx , (struct sockaddr * )& my_addr_v6_s ,
1229
1258
sizeof (struct sockaddr_in6 ));
1230
1259
if (ret < 0 ) {
@@ -1309,7 +1338,8 @@ static void check_rst_succeed(struct net_context *ctx,
1309
1338
"RST packet skipped (before %d, after %d)" ,
1310
1339
rsterr_before , rsterr_after );
1311
1340
1312
- net_tcp_put (ctx );
1341
+ net_context_put (ctx );
1342
+ net_context_put (accepted_ctx );
1313
1343
}
1314
1344
1315
1345
ZTEST (net_tcp , test_client_invalid_rst )
@@ -1332,7 +1362,7 @@ ZTEST(net_tcp, test_client_invalid_rst)
1332
1362
check_rst_succeed (NULL , 0 );
1333
1363
check_rst_succeed (NULL , 1 );
1334
1364
1335
- net_tcp_put ( ctx );
1365
+ /* net_context is released within check_rst_succeed() */
1336
1366
}
1337
1367
1338
1368
#define MAX_DATA 100
@@ -1431,13 +1461,15 @@ static void checklist_based_out_of_order_test(struct out_of_order_check_struct *
1431
1461
}
1432
1462
}
1433
1463
1434
- ZTEST ( net_tcp , test_server_recv_out_of_order_data )
1464
+ static void test_server_recv_out_of_order_data ( void )
1435
1465
{
1436
1466
/* Only run the tests if queueing is enabled */
1437
1467
if (CONFIG_NET_TCP_RECV_QUEUE_TIMEOUT == 0 ) {
1438
1468
return ;
1439
1469
}
1440
1470
1471
+ k_sem_reset (& test_sem );
1472
+
1441
1473
/* Start the sequence numbering so that we will wrap it (just for
1442
1474
* testing purposes)
1443
1475
*/
@@ -1466,7 +1498,7 @@ struct out_of_order_check_struct reorder_timeout_list[] = {
1466
1498
* test_server_recv_out_of_order_data(), so this test must be run after that
1467
1499
* test.
1468
1500
*/
1469
- ZTEST ( net_tcp , test_server_timeout_out_of_order_data )
1501
+ static void test_server_timeout_out_of_order_data ( void )
1470
1502
{
1471
1503
if (CONFIG_NET_TCP_RECV_QUEUE_TIMEOUT == 0 ) {
1472
1504
return ;
@@ -1478,7 +1510,14 @@ ZTEST(net_tcp, test_server_timeout_out_of_order_data)
1478
1510
ARRAY_SIZE (reorder_timeout_list ),
1479
1511
OUT_OF_ORDER_SEQ_INIT + 1 );
1480
1512
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 ();
1482
1521
}
1483
1522
1484
1523
ZTEST_SUITE (net_tcp , NULL , presetup , NULL , NULL , NULL );
0 commit comments