Skip to content

Commit aab83b7

Browse files
committed
feat:[sal][utest] added test cases for the sal api
1 parent 2d375b2 commit aab83b7

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

components/net/sal/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ if RT_USING_SAL
1212
help
1313
The ability that check internet status is provided by RT-Thread.
1414

15+
config SOCKET_TABLE_STEP_LEN
16+
int "Configure socket table step length"
17+
default 4
18+
1519
menu "Docking with protocol stacks"
1620
config SAL_USING_LWIP
1721
bool "Docking with lwIP stack"

components/net/sal/include/sal_socket.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
#include <stddef.h>
1515
#include <arpa/inet.h>
1616

17+
#include "sal_low_lvl.h"
18+
#include "sal_msg.h"
19+
#include "sal_netdb.h"
20+
#include "sal_tls.h"
21+
1722
#ifdef __cplusplus
1823
extern "C" {
1924
#endif
@@ -112,6 +117,21 @@ typedef uint16_t in_port_t;
112117
#define IPPROTO_UDPLITE 136
113118
#define IPPROTO_RAW 255
114119

120+
#define VALID_PROTOCOL(protocol) ((protocol) >= 0 && (protocol) <= IPPROTO_RAW)
121+
#define VALID_COMBO(domain, type, protocol) \
122+
( \
123+
(((domain) == AF_INET || (domain) == AF_INET6) && \
124+
( \
125+
((type) == SOCK_STREAM && ((protocol) == 0 || (protocol) == IPPROTO_TCP)) || \
126+
((type) == SOCK_DGRAM && ((protocol) == 0 || (protocol) == IPPROTO_UDP)) || \
127+
((type) == SOCK_RAW && ((protocol) == IPPROTO_RAW)) || \
128+
((type) == SOCK_STREAM && ((protocol) == PROTOCOL_TLS || (protocol) == PROTOCOL_DTLS)) /* TLS support */ \
129+
)) || \
130+
((domain) == AF_UNIX && (type) == SOCK_STREAM && (protocol) == 0) || \
131+
((domain) == AF_NETLINK && (type) == SOCK_RAW && (protocol) == 0) \
132+
/* Add more combos for AF_CAN, AF_AT, AF_WIZ if needed */ \
133+
)
134+
115135
/* Flags we can use with send and recv */
116136
#define MSG_PEEK 0x01 /* Peeks at an incoming message */
117137
#define MSG_WAITALL 0x02 /* Unimplemented: Requests that the function block until the full amount of data requested can be returned */

components/net/sal/src/sal_socket.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@
4343
#define DBG_LVL DBG_INFO
4444
#include <rtdbg.h>
4545

46-
#define SOCKET_TABLE_STEP_LEN 4
47-
4846
/* the socket table used to dynamic allocate sockets */
4947
struct sal_socket_table
5048
{
@@ -434,31 +432,51 @@ int sal_netdev_cleanup(struct netdev *netdev)
434432
* -1 : input the wrong family
435433
* -2 : input the wrong socket type
436434
* -3 : get network interface failed
435+
* -4 : invalid protocol or combo
437436
*/
438437
static int socket_init(int family, int type, int protocol, struct sal_socket **res)
439438
{
440-
441439
struct sal_socket *sock;
442440
struct sal_proto_family *pf;
443441
struct netdev *netdv_def = netdev_default;
444442
struct netdev *netdev = RT_NULL;
445443
rt_bool_t flag = RT_FALSE;
446444

445+
/* Existing range checks for family and type */
447446
if (family < 0 || family > AF_MAX)
448447
{
448+
LOG_E("Invalid family: %d (must be 0 ~ %d)", family, AF_MAX);
449449
return -1;
450450
}
451451

452452
if (type < 0 || type > SOCK_MAX)
453453
{
454+
LOG_E("Invalid type: %d (must be 0 ~ %d)", type, SOCK_MAX);
454455
return -2;
455456
}
456457

458+
/* Range check for protocol */
459+
if (!VALID_PROTOCOL(protocol))
460+
{
461+
LOG_E("Invalid protocol: %d (must be 0 ~ %d)", protocol, IPPROTO_RAW);
462+
rt_set_errno(EINVAL);
463+
return -4;
464+
}
465+
457466
sock = *res;
458467
sock->domain = family;
459468
sock->type = type;
460469
sock->protocol = protocol;
461470

471+
/* Combo compatibility check */
472+
if (!VALID_COMBO(family, type, protocol))
473+
{
474+
LOG_E("Invalid combo: domain=%d, type=%d, protocol=%d", family, type, protocol);
475+
rt_set_errno(EINVAL);
476+
return -4;
477+
}
478+
479+
/* Existing netdev selection logic */
462480
if (netdv_def && netdev_is_up(netdv_def))
463481
{
464482
/* check default network interface device protocol family */
@@ -483,6 +501,8 @@ static int socket_init(int family, int type, int protocol, struct sal_socket **r
483501
sock->netdev = netdev;
484502
}
485503

504+
LOG_D("Socket init success: domain=%d, type=%d, protocol=%d, netdev=%s",
505+
family, type, protocol, sock->netdev ? sock->netdev->name : "default");
486506
return 0;
487507
}
488508

@@ -1051,7 +1071,7 @@ int sal_socket(int domain, int type, int protocol)
10511071
{
10521072
LOG_E("SAL socket protocol family input failed, return error %d.", retval);
10531073
socket_delete(socket);
1054-
return -1;
1074+
return retval;
10551075
}
10561076

10571077
/* valid the network interface socket opreation */

0 commit comments

Comments
 (0)