1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
|
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-09-16 kurisaw initial commit
*/
#include <rtthread.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <netdev.h>
#include <errno.h>
#include "utest.h"
#include "sal_socket.h"
/**
* @brief Comprehensive test suite for RT-Thread SAL (Socket Abstraction Layer) socket operations
*
* @note This test suite validates:
* - 1.basic socket creation with valid and invalid parameters
* - 2.socket binding functionality with proper address configuration
* - 3.listen operation with various backlog configurations
* - 4.TCP connection establishment and error handling
* - 5.accept operation with timeout and error conditions
* - 6.TCP data transmission (send/receive) with echo verification
* - 7.UDP communication including sendto/recvfrom operations
* - 8.proper socket closure and cleanup procedures
* - 9.socket address information retrieval (getsockname/getpeername)
* - 10.network device status checking and fallback mechanisms
* - 11.thread-safe socket operations using RT-Thread synchronization
* - 12.socket timeout configuration and non-blocking mode handling
* - 13.error handling for invalid parameters and network failures
* - 14.port management to avoid conflicts during concurrent testing
*/
/* Test configuration */
#define TEST_SERVER_BASE_PORT 9000 /* Base port to avoid conflicts */
#define TEST_CLIENT_PORT_OFFSET 100 /* Offset for client ports */
#define TEST_BUFFER_SIZE 1024
#define TEST_TIMEOUT_MS 3000
#define TEST_INVALID_IP "192.168.999.999"
#define TEST_MAX_RETRY_ATTEMPTS 3 /* Max retry attempts for network operations */
#define TEST_CONNECTION_TIMEOUT 8000 /* Increased to 8s for threaded setup */
#define ACCEPT_POLL_TIMEOUT_MS 3000 /* Increased poll timeout for non-blocking accept */
#define CLIENT_DELAY_MS 1000 /* Increased delay after connect before close */
/* Thread synchronization */
#define THREAD_WAIT_TIMEOUT 3000 /* Increased to 3s for thread waits */
#define SOCKET_TIMEOUT_MS 3000 /* 3 seconds for socket operations */
/* Test data */
static char test_send_data[] = "Hello, RT-Thread SAL!";
static char test_recv_buffer[TEST_BUFFER_SIZE];
struct sal_test_ifconf
{
int ifc_len;
union
{
char *ifcu_buf;
struct sal_ifreq *ifcu_req;
} ifc_ifcu;
};
/* Local IP for tests (fallback to loopback) */
static char local_ip[16] = "127.0.0.1";
/* Thread synchronization structures */
static struct rt_event test_event;
static volatile int server_ready = 0;
/* Test helper functions */
static int get_available_port(int base_port)
{
static int port_counter = 0;
int port = base_port + (port_counter++ * 2); /* Increment by 2 to avoid adjacent port conflicts */
LOG_D("Generated available port: %d", port);
return port;
}
static void safe_strcpy(char *dest, const char *src, size_t dest_size)
{
if (dest_size > 0)
{
strncpy(dest, src, dest_size - 1);
dest[dest_size - 1] = '\0';
}
LOG_D("Copied string '%s' to destination with size %u", src, dest_size);
}
static void check_network_status(void)
{
LOG_I("Checking network device status...");
/* Try to create a simple socket to test network availability */
int test_sock = sal_socket(AF_INET, SOCK_STREAM, 0);
if (test_sock < 0)
{
LOG_E("Network test socket creation failed: %d", test_sock);
LOG_E("Network device or protocol family issue detected");
return;
}
LOG_I("Network test socket created successfully: %d", test_sock);
sal_closesocket(test_sock);
/* Fallback to loopback; dynamic IP via netdev may not be available */
safe_strcpy(local_ip, "127.0.0.1", sizeof(local_ip));
LOG_I("Using loopback IP address for testing: %s", local_ip);
LOG_I("Network device appears operational");
}
static int set_socket_timeout(int sock, int timeout_ms)
{
struct timeval timeout;
timeout.tv_sec = timeout_ms / 1000;
timeout.tv_usec = (timeout_ms % 1000) * 1000;
int ret1 = sal_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
int ret2 = sal_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
if (ret1 == 0 && ret2 == 0)
{
LOG_D("Set socket %d timeout to %d ms", sock, timeout_ms);
return 0;
}
else
{
LOG_W("Failed to set socket %d timeout to %d ms", sock, timeout_ms);
return -1;
}
}
static int create_test_socket(int domain, int type, int protocol)
{
int sock = sal_socket(domain, type, protocol);
if (sock >= 0)
{
LOG_I("Created socket %d (domain=%d, type=%d, protocol=%d)", sock, domain, type, protocol);
/* Set timeout for all socket operations to prevent blocking */
if (set_socket_timeout(sock, SOCKET_TIMEOUT_MS) != 0)
{
LOG_W("Failed to set timeout for socket %d", sock);
}
/* Enable SO_REUSEADDR to allow reusing ports and avoid bind failures due to TIME_WAIT */
int opt = 1;
sal_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
LOG_D("Enabled SO_REUSEADDR for socket %d", sock);
}
else
{
LOG_E("Failed to create socket (domain=%d, type=%d, protocol=%d)", domain, type, protocol);
}
return sock;
}
static void close_test_socket(int sock)
{
if (sock >= 0)
{
sal_closesocket(sock);
LOG_I("Closed socket %d", sock);
}
}
static void TC_sal_socket_siocgifconf(void)
{
struct sal_test_ifconf ifconf;
struct sal_ifreq *ifreq;
unsigned char buffer[sizeof(struct sal_ifreq)];
char *original_buffer;
size_t index;
size_t name_length;
int sock;
int ret;
sock = create_test_socket(AF_INET, SOCK_DGRAM, 0);
uassert_true(sock >= 0);
if (sock < 0)
{
return;
}
/* A zero-sized destination must not be written. */
memset(buffer, 0xA5, sizeof(buffer));
ifconf.ifc_len = 0;
ifconf.ifc_ifcu.ifcu_buf = (char *)buffer;
ret = sal_ioctlsocket(sock, SIOCGIFCONF, &ifconf);
uassert_int_equal(ret, 0);
uassert_int_equal(ifconf.ifc_len, 0);
for (index = 0; index < sizeof(buffer); index++)
{
uassert_int_equal(buffer[index], 0xA5);
}
/* A NULL destination queries the required buffer size. */
ifconf.ifc_len = sizeof(buffer);
ifconf.ifc_ifcu.ifcu_buf = RT_NULL;
ret = sal_ioctlsocket(sock, SIOCGIFCONF, &ifconf);
uassert_int_equal(ret, 0);
uassert_true(ifconf.ifc_len >= sizeof(struct sal_ifreq));
uassert_int_equal(ifconf.ifc_len % sizeof(struct sal_ifreq), 0);
/* A negative buffer length must be rejected without writing. */
memset(buffer, 0xA5, sizeof(buffer));
ifconf.ifc_len = -1;
ifconf.ifc_ifcu.ifcu_buf = (char *)buffer;
ret = sal_ioctlsocket(sock, SIOCGIFCONF, &ifconf);
uassert_int_equal(rt_get_errno(), EINVAL);
uassert_int_equal(ret, -1);
for (index = 0; index < sizeof(buffer); index++)
{
uassert_int_equal(buffer[index], 0xA5);
}
/* Returned bytes outside the interface name must be initialized. */
memset(buffer, 0xA5, sizeof(buffer));
original_buffer = (char *)buffer;
ifconf.ifc_len = sizeof(buffer);
ifconf.ifc_ifcu.ifcu_buf = original_buffer;
ret = sal_ioctlsocket(sock, SIOCGIFCONF, &ifconf);
uassert_int_equal(ret, 0);
uassert_int_equal(ifconf.ifc_len, sizeof(struct sal_ifreq));
uassert_true(ifconf.ifc_ifcu.ifcu_buf == original_buffer);
ifreq = (struct sal_ifreq *)buffer;
for (name_length = 0; name_length < IFNAMSIZ; name_length++)
{
if (ifreq->ifr_ifrn.ifrn_name[name_length] == '\0')
{
break;
}
}
uassert_true(name_length > 0 && name_length < IFNAMSIZ);
for (index = name_length + 1; index < sizeof(*ifreq); index++)
{
uassert_int_equal(buffer[index], 0);
}
close_test_socket(sock);
}
/* Server thread function */
static void server_thread_entry(void *parameter)
{
int port = (int)parameter;
int server_sock = -1;
struct sockaddr_in bind_addr;
int ret;
LOG_I("Starting server thread on port %d", port);
/* Create server socket */
server_sock = create_test_socket(AF_INET, SOCK_STREAM, 0);
if (server_sock < 0)
{
LOG_E("Failed to create server socket");
rt_event_send(&test_event, 0x01);
return;
}
/* Setup bind address */
memset(&bind_addr, 0, sizeof(bind_addr));
bind_addr.sin_family = AF_INET;
bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bind_addr.sin_port = htons(port);
LOG_D("Configured server bind address: port %d", port);
/* Bind server socket */
ret = sal_bind(server_sock, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
if (ret != 0)
{
LOG_E("Failed to bind server socket %d: return=%d, errno=%d", server_sock, ret, errno);
close_test_socket(server_sock);
rt_event_send(&test_event, 0x01);
return;
}
LOG_I("Server socket %d bound to port %d", server_sock, port);
/* Listen on server socket */
ret = sal_listen(server_sock, 5);
if (ret != 0)
{
LOG_E("Failed to listen on server socket %d: return=%d, errno=%d", server_sock, ret, errno);
close_test_socket(server_sock);
rt_event_send(&test_event, 0x01);
return;
}
LOG_I("Server socket %d listening with backlog 5", server_sock);
server_ready = 1;
rt_event_send(&test_event, 0x02);
LOG_I("Server ready and signaled on port %d", port);
/* Wait for client connection */
ret = rt_event_recv(&test_event, 0x04, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
RT_WAITING_FOREVER, RT_NULL);
if (ret != RT_EOK)
{
LOG_E("Failed to receive client connect signal: %d", ret);
rt_event_send(&test_event, 0x01);
close_test_socket(server_sock);
return;
}
LOG_D("Received client connect signal");
/* Non-blocking accept loop to avoid blocking on aborted connections */
int mode = 1;
sal_ioctlsocket(server_sock, FIONBIO, &mode);
LOG_D("Set server socket %d to non-blocking mode", server_sock);
int elapsed = 0;
int check_interval = 50;
int accepted_sock = -1;
while (elapsed < ACCEPT_POLL_TIMEOUT_MS)
{
accepted_sock = sal_accept(server_sock, RT_NULL, RT_NULL);
if (accepted_sock >= 0)
{
LOG_I("Accepted connection on socket %d", accepted_sock);
set_socket_timeout(accepted_sock, SOCKET_TIMEOUT_MS);
close_test_socket(accepted_sock);
break;
}
else
{
int saved_errno = errno;
if (saved_errno != EWOULDBLOCK && saved_errno != EAGAIN)
{
LOG_E("Accept failed on socket %d: return=%d, errno=%d", server_sock, accepted_sock, saved_errno);
break;
}
}
rt_thread_mdelay(check_interval);
elapsed += check_interval;
}
if (accepted_sock < 0)
{
LOG_E("Accept poll timeout or failed on socket %d", server_sock);
rt_event_send(&test_event, 0x01);
}
/* Restore blocking mode */
mode = 0;
sal_ioctlsocket(server_sock, FIONBIO, &mode);
LOG_D("Restored server socket %d to blocking mode", server_sock);
/* Cleanup */
close_test_socket(server_sock);
rt_event_send(&test_event, 0x08);
LOG_I("Server thread completed");
}
/* Client thread function */
static void client_thread_entry(void *parameter)
{
int port = (int)parameter;
int client_sock = -1;
struct sockaddr_in connect_addr;
int ret;
LOG_I("Starting client thread for port %d", port);
/* Wait for server to be ready */
ret = rt_event_recv(&test_event, 0x02, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
THREAD_WAIT_TIMEOUT, RT_NULL);
if (ret != RT_EOK)
{
LOG_E("Failed to receive server ready signal: %d", ret);
rt_event_send(&test_event, 0x10);
return;
}
LOG_D("Received server ready signal");
if (!server_ready)
{
LOG_W("Server not ready, skipping connection");
rt_event_send(&test_event, 0x10);
return;
}
/* Create client socket */
client_sock = create_test_socket(AF_INET, SOCK_STREAM, 0);
if (client_sock < 0)
{
LOG_E("Failed to create client socket");
rt_event_send(&test_event, 0x10);
return;
}
/* Setup connect address */
memset(&connect_addr, 0, sizeof(connect_addr));
connect_addr.sin_family = AF_INET;
connect_addr.sin_addr.s_addr = inet_addr(local_ip);
connect_addr.sin_port = htons(port);
LOG_D("Configured client connect address: %s:%d", local_ip, port);
/* Connect to server */
LOG_I("Attempting to connect to server %s:%d", local_ip, port);
ret = sal_connect(client_sock, (struct sockaddr *)&connect_addr, sizeof(connect_addr));
LOG_I("Connect result: %d", ret);
if (ret == 0)
{
LOG_I("Connection to server successful");
rt_thread_mdelay(CLIENT_DELAY_MS);
}
else
{
LOG_E("Connection to server failed: return=%d, errno=%d", ret, errno);
}
/* Signal server that we've attempted connection */
rt_event_send(&test_event, 0x04);
LOG_D("Signaled server of connection attempt");
/* Cleanup */
close_test_socket(client_sock);
rt_event_send(&test_event, 0x10);
LOG_I("Client thread completed");
}
static int setup_test_server_threaded(int *server_sock, int *client_sock, int port)
{
rt_thread_t server_thread = RT_NULL;
rt_thread_t client_thread = RT_NULL;
rt_uint32_t recv_events = 0;
LOG_I("Setting up threaded test server on port %d", port);
/* Initialize event */
rt_event_init(&test_event, "test_event", RT_IPC_FLAG_FIFO);
server_ready = 0;
LOG_D("Initialized test event");
/* Create server thread */
server_thread = rt_thread_create("server", server_thread_entry, (void *)port,
2048, RT_THREAD_PRIORITY_MAX / 2, 20);
if (server_thread == RT_NULL)
{
LOG_E("Failed to create server thread");
rt_event_detach(&test_event);
return -1;
}
LOG_I("Created server thread");
/* Create client thread */
client_thread = rt_thread_create("client", client_thread_entry, (void *)port,
2048, RT_THREAD_PRIORITY_MAX / 2, 20);
if (client_thread == RT_NULL)
{
LOG_E("Failed to create client thread");
rt_thread_delete(server_thread);
rt_event_detach(&test_event);
return -1;
}
LOG_I("Created client thread");
/* Start threads */
rt_thread_startup(server_thread);
rt_thread_startup(client_thread);
LOG_I("Started server and client threads");
/* Wait for both threads to complete */
rt_event_recv(&test_event, 0x18 | 0x01, RT_EVENT_FLAG_OR,
TEST_CONNECTION_TIMEOUT, &recv_events);
LOG_D("Received events: 0x%x", recv_events);
/* Cleanup threads */
rt_thread_delete(server_thread);
rt_thread_delete(client_thread);
rt_event_detach(&test_event);
LOG_I("Cleaned up threads and event");
/* Check if setup partially successful */
if ((recv_events & 0x10) != 0)
{
LOG_I("Test server setup partially successful (connect verified)");
return 0;
}
else
{
LOG_E("Test server setup failed");
return -1;
}
}
static void cleanup_test_connection(int server_sock, int client_sock, int accepted_sock)
{
if (accepted_sock >= 0)
{
close_test_socket(accepted_sock);
}
if (client_sock >= 0)
{
close_test_socket(client_sock);
}
if (server_sock >= 0)
{
close_test_socket(server_sock);
}
LOG_I("Cleaned up test connection sockets");
}
/* Test cases for basic socket operations */
static void TC_sal_socket_create(void)
{
int sock;
LOG_I("Starting TC_sal_socket_create tests...");
/* Test valid socket creation */
LOG_I("Testing TCP socket creation...");
sock = sal_socket(AF_INET, SOCK_STREAM, 0);
LOG_I("TCP socket created: %d", sock);
uassert_true(sock >= 0);
close_test_socket(sock);
LOG_I("Testing UDP socket creation...");
sock = sal_socket(AF_INET, SOCK_DGRAM, 0);
LOG_I("UDP socket created: %d", sock);
uassert_true(sock >= 0);
close_test_socket(sock);
/* Test invalid parameters */
LOG_I("Testing invalid family parameter...");
sock = sal_socket(-1, SOCK_STREAM, 0);
LOG_I("Invalid family result: %d", sock);
uassert_true(sock < 0);
LOG_I("Testing invalid type parameter...");
sock = sal_socket(AF_INET, -1, 0);
LOG_I("Invalid type result: %d", sock);
uassert_true(sock < 0);
LOG_I("Testing invalid protocol parameter...");
sock = sal_socket(AF_INET, SOCK_STREAM, -1);
LOG_I("Invalid protocol result: %d", sock);
uassert_true(sock < 0);
LOG_I("TC_sal_socket_create tests completed");
}
static void TC_sal_socket_bind(void)
{
int sock = -1;
struct sockaddr_in addr;
int ret;
int test_port = get_available_port(TEST_SERVER_BASE_PORT);
LOG_I("Starting TC_sal_socket_bind tests...");
/* Test valid bind */
LOG_I("Creating socket for bind test on port %d...", test_port);
sock = create_test_socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
LOG_E("Failed to create socket for bind test");
return;
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(test_port);
LOG_I("Attempting to bind socket %d to port %d...", sock, test_port);
ret = sal_bind(sock, (struct sockaddr *)&addr, sizeof(addr));
LOG_I("Bind result: %d (expected 0)", ret);
uassert_int_equal(ret, 0);
close_test_socket(sock);
sock = -1;
LOG_I("Skipping NULL address bind test (would cause assertion)");
/* Test bind with invalid socket */
LOG_I("Testing bind with invalid socket...");
ret = sal_bind(-1, (struct sockaddr *)&addr, sizeof(addr));
LOG_I("Invalid socket bind result: %d (expected -1)", ret);
uassert_int_equal(ret, -1);
LOG_I("TC_sal_socket_bind tests completed");
}
static void TC_sal_socket_listen(void)
{
int sock = -1;
struct sockaddr_in addr;
int ret;
int test_port = get_available_port(TEST_SERVER_BASE_PORT);
LOG_I("Starting TC_sal_socket_listen tests...");
LOG_I("Creating socket for listen test...");
sock = create_test_socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
LOG_E("Failed to create socket for listen test");
return;
}
/* Bind socket first */
LOG_I("Binding socket %d for listen test on port %d...", sock, test_port);
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(test_port);
ret = sal_bind(sock, (struct sockaddr *)&addr, sizeof(addr));
if (ret != 0)
{
LOG_E("Failed to bind socket %d for listen test: %d", sock, ret);
close_test_socket(sock);
return;
}
/* Test valid listen */
LOG_I("Testing listen with backlog 5 on socket %d...", sock);
ret = sal_listen(sock, 5);
LOG_I("Listen result: %d (expected 0)", ret);
uassert_int_equal(ret, 0);
/* Test invalid backlog */
LOG_I("Testing listen with invalid backlog (-1)...");
ret = sal_listen(sock, -1);
LOG_I("Invalid backlog listen result: %d", ret);
if (ret != -1 && ret != 0)
{
LOG_W("Unexpected listen result for invalid backlog: %d", ret);
}
close_test_socket(sock);
sock = -1;
/* Test listen on invalid socket */
LOG_I("Testing listen on invalid socket...");
ret = sal_listen(-1, 5);
LOG_I("Invalid socket listen result: %d (expected -1)", ret);
uassert_int_equal(ret, -1);
LOG_I("TC_sal_socket_listen tests completed");
}
static void TC_sal_socket_connect(void)
{
int ret;
int test_port = get_available_port(TEST_SERVER_BASE_PORT);
LOG_I("Starting TC_sal_socket_connect tests...");
/* Setup test server using threaded approach */
LOG_I("Setting up test server on port %d...", test_port);
ret = setup_test_server_threaded(RT_NULL, RT_NULL, test_port);
if (ret != 0)
{
LOG_W("Test server setup failed, may be expected in some environments");
}
else
{
LOG_I("Test server setup completed successfully");
}
/* Test connect to invalid address */
LOG_I("Testing connect to invalid address %s...", TEST_INVALID_IP);
int client_sock = create_test_socket(AF_INET, SOCK_STREAM, 0);
if (client_sock >= 0)
{
struct sockaddr_in connect_addr;
memset(&connect_addr, 0, sizeof(connect_addr));
connect_addr.sin_family = AF_INET;
connect_addr.sin_addr.s_addr = inet_addr(TEST_INVALID_IP);
connect_addr.sin_port = htons(test_port);
ret = sal_connect(client_sock, (struct sockaddr *)&connect_addr, sizeof(connect_addr));
LOG_I("Invalid address connect result: %d (expected -1)", ret);
uassert_int_equal(ret, -1);
close_test_socket(client_sock);
}
LOG_I("TC_sal_socket_connect tests completed");
}
static void TC_sal_socket_accept(void)
{
int server_sock = -1;
struct sockaddr_in bind_addr;
int ret;
int test_port = get_available_port(TEST_SERVER_BASE_PORT);
LOG_I("Starting TC_sal_socket_accept tests...");
/* Setup test server */
LOG_I("Creating server socket...");
server_sock = create_test_socket(AF_INET, SOCK_STREAM, 0);
if (server_sock < 0)
{
LOG_E("Failed to create server socket");
return;
}
memset(&bind_addr, 0, sizeof(bind_addr));
bind_addr.sin_family = AF_INET;
bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bind_addr.sin_port = htons(test_port);
LOG_I("Binding server socket %d to port %d...", server_sock, test_port);
ret = sal_bind(server_sock, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
if (ret != 0)
{
LOG_E("Failed to bind server socket %d: %d", server_sock, ret);
close_test_socket(server_sock);
return;
}
LOG_I("Starting to listen on server socket %d...", server_sock);
ret = sal_listen(server_sock, 5);
if (ret != 0)
{
LOG_E("Failed to listen on server socket %d: %d", server_sock, ret);
close_test_socket(server_sock);
return;
}
/* Test accept on valid socket with timeout */
LOG_I("Testing accept with timeout on socket %d...", server_sock);
struct sockaddr_in client_addr;
socklen_t addr_len = sizeof(client_addr);
int accepted_sock = sal_accept(server_sock, (struct sockaddr*)&client_addr, &addr_len);
if (accepted_sock >= 0)
{
LOG_W("Unexpected connection accepted: %d", accepted_sock);
close_test_socket(accepted_sock);
}
else
{
LOG_I("Accept timed out as expected: %d", accepted_sock);
}
close_test_socket(server_sock);
server_sock = -1;
/* Test accept on invalid socket */
LOG_I("Testing accept on invalid socket...");
accepted_sock = sal_accept(-1, (struct sockaddr *)&client_addr, &addr_len);
LOG_I("Invalid socket accept result: %d (expected -1)", accepted_sock);
uassert_int_equal(accepted_sock, -1);
LOG_I("TC_sal_socket_accept tests completed");
}
static void TC_sal_socket_send_recv(void)
{
int ret;
int test_port = get_available_port(TEST_SERVER_BASE_PORT);
int len = strlen(test_send_data);
LOG_I("Starting TC_sal_socket_send_recv tests...");
/* Setup test connection using threaded approach */
LOG_I("Setting up test server on port %d...", test_port);
ret = setup_test_server_threaded(RT_NULL, RT_NULL, test_port);
if (ret != 0)
{
LOG_W("Failed to setup test server, skipping send/recv tests (may be expected)");
return;
}
/* Simple send/recv test extension */
LOG_I("Testing send/recv with data exchange...");
int server_sock = create_test_socket(AF_INET, SOCK_STREAM, 0);
int client_sock = create_test_socket(AF_INET, SOCK_STREAM, 0);
if (server_sock < 0 || client_sock < 0)
{
LOG_E("Failed to create sockets for send/recv");
goto cleanup_sr;
}
struct sockaddr_in bind_addr, connect_addr;
memset(&bind_addr, 0, sizeof(bind_addr));
bind_addr.sin_family = AF_INET;
bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bind_addr.sin_port = htons(test_port + 1);
ret = sal_bind(server_sock, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
if (ret != 0)
{
LOG_E("Server bind failed: %d", ret);
goto cleanup_sr;
}
LOG_I("Server socket %d bound to port %d", server_sock, test_port + 1);
ret = sal_listen(server_sock, 5);
if (ret != 0)
{
LOG_E("Server listen failed: %d", ret);
goto cleanup_sr;
}
LOG_I("Server socket %d listening", server_sock);
memset(&connect_addr, 0, sizeof(connect_addr));
connect_addr.sin_family = AF_INET;
connect_addr.sin_addr.s_addr = inet_addr(local_ip);
connect_addr.sin_port = bind_addr.sin_port;
ret = sal_connect(client_sock, (struct sockaddr *)&connect_addr, sizeof(connect_addr));
if (ret != 0)
{
LOG_E("Client connect failed: %d", ret);
goto cleanup_sr;
}
LOG_I("Client socket %d connected", client_sock);
/* Server accept */
int accepted_sock = sal_accept(server_sock, NULL, NULL);
if (accepted_sock < 0)
{
LOG_E("Server accept failed: %d", accepted_sock);
goto cleanup_sr;
}
LOG_I("Server accepted connection on socket %d", accepted_sock);
/* Client send */
ret = sal_sendto(client_sock, test_send_data, len, 0, NULL, 0);
if (ret != len)
{
LOG_E("Client send failed: %d bytes sent, expected %d", ret, len);
goto cleanup_sr;
}
LOG_I("Client sent %d bytes", ret);
/* Server recv and echo */
memset(test_recv_buffer, 0, sizeof(test_recv_buffer));
ret = sal_recvfrom(accepted_sock, test_recv_buffer, sizeof(test_recv_buffer), 0, NULL, NULL);
if (ret == len && memcmp(test_recv_buffer, test_send_data, len) == 0)
{
LOG_I("Server received %d bytes, matches sent data", ret);
uassert_int_equal(ret, len);
}
else
{
LOG_E("Server recv failed: expected %d bytes, got %d", len, ret);
}
ret = sal_sendto(accepted_sock, test_recv_buffer, ret, 0, NULL, 0);
if (ret != len)
{
LOG_E("Server echo send failed: %d bytes sent, expected %d", ret, len);
}
else
{
LOG_I("Server sent echo %d bytes", ret);
}
/* Client recv echo */
memset(test_recv_buffer, 0, sizeof(test_recv_buffer));
ret = sal_recvfrom(client_sock, test_recv_buffer, sizeof(test_recv_buffer), 0, NULL, NULL);
if (ret == len && memcmp(test_recv_buffer, test_send_data, len) == 0)
{
LOG_I("Client received echo %d bytes, matches sent data", ret);
uassert_int_equal(ret, len);
}
else
{
LOG_E("Client recv echo failed: expected %d bytes, got %d", len, ret);
}
cleanup_sr:
cleanup_test_connection(server_sock, client_sock, accepted_sock);
LOG_I("TC_sal_socket_send_recv tests completed");
}
static void TC_sal_socket_udp_communication(void)
{
int server_sock = -1, client_sock = -1;
struct sockaddr_in server_bind_addr, client_bind_addr, from_addr;
struct sockaddr_in server_dest_addr, client_dest_addr;
socklen_t addr_len;
int ret;
int len;
int server_port = get_available_port(TEST_SERVER_BASE_PORT);
int client_port = server_port + TEST_CLIENT_PORT_OFFSET;
LOG_I("Starting TC_sal_socket_udp_communication tests...");
/* Create UDP sockets */
server_sock = create_test_socket(AF_INET, SOCK_DGRAM, 0);
if (server_sock < 0)
{
LOG_E("Failed to create server UDP socket");
goto cleanup;
}
client_sock = create_test_socket(AF_INET, SOCK_DGRAM, 0);
if (client_sock < 0)
{
LOG_E("Failed to create client UDP socket");
goto cleanup;
}
/* Setup server bind address */
memset(&server_bind_addr, 0, sizeof(server_bind_addr));
server_bind_addr.sin_family = AF_INET;
server_bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_bind_addr.sin_port = htons(server_port);
/* Bind server socket */
ret = sal_bind(server_sock, (struct sockaddr *)&server_bind_addr, sizeof(server_bind_addr));
if (ret != 0)
{
LOG_W("Server bind failed, may be expected: return=%d, errno=%d", ret, errno);
goto cleanup;
}
LOG_I("Server socket %d bound to port %d", server_sock, server_port);
/* Setup client bind address */
memset(&client_bind_addr, 0, sizeof(client_bind_addr));
client_bind_addr.sin_family = AF_INET;
client_bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
client_bind_addr.sin_port = htons(client_port);
/* Bind client socket */
ret = sal_bind(client_sock, (struct sockaddr *)&client_bind_addr, sizeof(client_bind_addr));
if (ret != 0)
{
LOG_W("Client bind failed, may be expected: return=%d, errno=%d", ret, errno);
goto cleanup;
}
LOG_I("Client socket %d bound to port %d", client_sock, client_port);
/* Setup destination addresses */
memset(&server_dest_addr, 0, sizeof(server_dest_addr));
server_dest_addr.sin_family = AF_INET;
server_dest_addr.sin_addr.s_addr = inet_addr(local_ip);
server_dest_addr.sin_port = htons(server_port);
memset(&client_dest_addr, 0, sizeof(client_dest_addr));
client_dest_addr.sin_family = AF_INET;
client_dest_addr.sin_addr.s_addr = inet_addr(local_ip);
client_dest_addr.sin_port = htons(client_port);
/* Test UDP sendto/recvfrom */
len = strlen(test_send_data);
LOG_I("Sending %d bytes from client to server...", len);
ret = sal_sendto(client_sock, test_send_data, len, 0,
(struct sockaddr *)&server_dest_addr, sizeof(server_dest_addr));
if (ret != len)
{
LOG_W("UDP send failed, may be expected: %d bytes sent, errno=%d", ret, errno);
goto cleanup;
}
LOG_I("Client sent %d bytes to server", ret);
addr_len = sizeof(from_addr);
memset(test_recv_buffer, 0, sizeof(test_recv_buffer));
ret = sal_recvfrom(server_sock, test_recv_buffer, sizeof(test_recv_buffer), 0,
(struct sockaddr *)&from_addr, &addr_len);
if (ret != len)
{
LOG_W("UDP recv failed, may be expected: %d bytes received, errno=%d", ret, errno);
goto cleanup;
}
LOG_I("Server received %d bytes", ret);
uassert_str_equal(test_recv_buffer, test_send_data);
/* Test reverse direction */
LOG_I("Sending %d bytes from server to client...", len);
ret = sal_sendto(server_sock, test_send_data, len, 0,
(struct sockaddr *)&client_dest_addr, sizeof(client_dest_addr));
if (ret != len)
{
LOG_W("Reverse UDP send failed, may be expected: %d bytes sent, errno=%d", ret, errno);
goto cleanup;
}
LOG_I("Server sent %d bytes to client", ret);
addr_len = sizeof(from_addr);
memset(test_recv_buffer, 0, sizeof(test_recv_buffer));
ret = sal_recvfrom(client_sock, test_recv_buffer, sizeof(test_recv_buffer), 0,
(struct sockaddr *)&from_addr, &addr_len);
if (ret != len)
{
LOG_W("Reverse UDP recv failed, may be expected: %d bytes received, errno=%d", ret, errno);
goto cleanup;
}
LOG_I("Client received %d bytes", ret);
uassert_str_equal(test_recv_buffer, test_send_data);
cleanup:
close_test_socket(server_sock);
close_test_socket(client_sock);
LOG_I("TC_sal_socket_udp_communication tests completed");
}
static void TC_sal_socket_close(void)
{
int sock = -1;
LOG_I("Starting TC_sal_socket_close tests...");
/* Test close valid socket */
LOG_I("Testing close valid socket...");
sock = create_test_socket(AF_INET, SOCK_STREAM, 0);
LOG_I("Closing socket %d...", sock);
uassert_int_equal(sal_closesocket(sock), 0);
sock = -1;
/* Test close invalid socket */
LOG_I("Testing close invalid socket...");
uassert_int_equal(sal_closesocket(-1), -1);
/* Test double close */
LOG_I("Testing double close...");
sock = create_test_socket(AF_INET, SOCK_STREAM, 0);
sal_closesocket(sock);
LOG_I("Double closing socket %d (should be safe)", sock);
LOG_I("TC_sal_socket_close tests completed");
}
static void TC_sal_socket_getpeername_getsockname(void)
{
int server_sock = -1;
struct sockaddr_in addr;
socklen_t addr_len;
int ret;
int test_port = get_available_port(TEST_SERVER_BASE_PORT);
LOG_I("Starting TC_sal_socket_getpeername_getsockname tests...");
/* Create and bind a socket for testing */
server_sock = create_test_socket(AF_INET, SOCK_STREAM, 0);
if (server_sock < 0)
{
LOG_E("Failed to create socket for getpeername/getsockname tests");
return;
}
/* Bind socket */
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(test_port);
ret = sal_bind(server_sock, (struct sockaddr *)&addr, sizeof(addr));
if (ret != 0)
{
LOG_W("Bind failed, may be expected: return=%d, errno=%d", ret, errno);
close_test_socket(server_sock);
return;
}
LOG_I("Server socket %d bound to port %d", server_sock, test_port);
/* Test getsockname on valid socket */
LOG_I("Testing getsockname on socket %d...", server_sock);
addr_len = sizeof(addr);
ret = sal_getsockname(server_sock, (struct sockaddr*)&addr, &addr_len);
LOG_I("Getsockname result: %d (expected 0)", ret);
uassert_int_equal(ret, 0);
uassert_int_equal(addr.sin_family, AF_INET);
close_test_socket(server_sock);
server_sock = -1;
/* Test on invalid socket */
LOG_I("Testing getsockname/getpeername on invalid socket...");
addr_len = sizeof(addr);
ret = sal_getsockname(-1, (struct sockaddr*)&addr, &addr_len);
LOG_I("Invalid socket getsockname result: %d (expected -1)", ret);
uassert_int_equal(ret, -1);
ret = sal_getpeername(-1, (struct sockaddr *)&addr, &addr_len);
LOG_I("Invalid socket getpeername result: %d (expected -1)", ret);
uassert_int_equal(ret, -1);
LOG_I("TC_sal_socket_getpeername_getsockname tests completed");
}
static void utest_do_tc(void)
{
LOG_I("===========================================");
LOG_I("Starting SAL Socket Basic API Tests");
LOG_I("===========================================");
/* Check network status before starting tests */
check_network_status();
/* Run test cases - each is independent */
UTEST_UNIT_RUN(TC_sal_socket_create);
UTEST_UNIT_RUN(TC_sal_socket_bind);
UTEST_UNIT_RUN(TC_sal_socket_listen);
UTEST_UNIT_RUN(TC_sal_socket_connect);
UTEST_UNIT_RUN(TC_sal_socket_accept);
UTEST_UNIT_RUN(TC_sal_socket_send_recv);
UTEST_UNIT_RUN(TC_sal_socket_udp_communication);
UTEST_UNIT_RUN(TC_sal_socket_getpeername_getsockname);
UTEST_UNIT_RUN(TC_sal_socket_siocgifconf);
UTEST_UNIT_RUN(TC_sal_socket_close);
LOG_I("===========================================");
LOG_I("SAL Socket Basic API Tests Completed");
LOG_I("===========================================");
}
UTEST_TC_EXPORT(utest_do_tc, "components.net.sal.socket_basic", RT_NULL, RT_NULL, 30);
|