Skip to content

Commit

Permalink
Fix nasa#795, WIP not full coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
skliper committed Feb 19, 2021
1 parent 9f99c1a commit 5c78d08
Show file tree
Hide file tree
Showing 12 changed files with 289 additions and 40 deletions.
36 changes: 18 additions & 18 deletions src/os/portable/os-impl-bsd-sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@
typedef union
{
char data[OS_SOCKADDR_MAX_LEN];
struct sockaddr sockaddr;
struct sockaddr_in sockaddr_in;
struct sockaddr sa;
struct sockaddr_in sa_in;
#ifdef OS_NETWORK_SUPPORTS_IPV6
struct sockaddr_in6 sockaddr_in6;
struct sockaddr_in6 sa_in6;
#endif
} OS_SockAddr_Accessor_t;

Expand Down Expand Up @@ -566,7 +566,7 @@ int32 OS_SocketAddrInit_Impl(OS_SockAddr_t *Addr, OS_SocketDomain_t Domain)
}

Addr->ActualLength = OSAL_SIZE_C(addrlen);
Accessor->sockaddr.sa_family = sa_family;
Accessor->sa.sa_family = sa_family;

return OS_SUCCESS;
} /* end OS_SocketAddrInit_Impl */
Expand All @@ -586,22 +586,22 @@ int32 OS_SocketAddrToString_Impl(char *buffer, size_t buflen, const OS_SockAddr_

Accessor = (const OS_SockAddr_Accessor_t *)&Addr->AddrData;

switch (Accessor->sockaddr.sa_family)
switch (Accessor->sa.sa_family)
{
case AF_INET:
addrbuffer = &Accessor->sockaddr_in.sin_addr;
addrbuffer = &Accessor->sa_in.sin_addr;
break;
#ifdef OS_NETWORK_SUPPORTS_IPV6
case AF_INET6:
addrbuffer = &Accessor->sockaddr_in6.sin6_addr;
addrbuffer = &Accessor->sa_in6.sin6_addr;
break;
#endif
default:
return OS_ERR_BAD_ADDRESS;
break;
}

if (inet_ntop(Accessor->sockaddr.sa_family, addrbuffer, buffer, buflen) == NULL)
if (inet_ntop(Accessor->sa.sa_family, addrbuffer, buffer, buflen) == NULL)
{
return OS_ERROR;
}
Expand All @@ -624,22 +624,22 @@ int32 OS_SocketAddrFromString_Impl(OS_SockAddr_t *Addr, const char *string)

Accessor = (OS_SockAddr_Accessor_t *)&Addr->AddrData;

switch (Accessor->sockaddr.sa_family)
switch (Accessor->sa.sa_family)
{
case AF_INET:
addrbuffer = &Accessor->sockaddr_in.sin_addr;
addrbuffer = &Accessor->sa_in.sin_addr;
break;
#ifdef OS_NETWORK_SUPPORTS_IPV6
case AF_INET6:
addrbuffer = &Accessor->sockaddr_in6.sin6_addr;
addrbuffer = &Accessor->sa_in6.sin6_addr;
break;
#endif
default:
return OS_ERR_BAD_ADDRESS;
break;
}

if (inet_pton(Accessor->sockaddr.sa_family, string, addrbuffer) < 0)
if (inet_pton(Accessor->sa.sa_family, string, addrbuffer) < 0)
{
return OS_ERROR;
}
Expand All @@ -662,14 +662,14 @@ int32 OS_SocketAddrGetPort_Impl(uint16 *PortNum, const OS_SockAddr_t *Addr)

Accessor = (const OS_SockAddr_Accessor_t *)&Addr->AddrData;

switch (Accessor->sockaddr.sa_family)
switch (Accessor->sa.sa_family)
{
case AF_INET:
sa_port = Accessor->sockaddr_in.sin_port;
sa_port = Accessor->sa_in.sin_port;
break;
#ifdef OS_NETWORK_SUPPORTS_IPV6
case AF_INET6:
sa_port = Accessor->sockaddr_in6.sin6_port;
sa_port = Accessor->sa_in6.sin6_port;
break;
#endif
default:
Expand Down Expand Up @@ -698,14 +698,14 @@ int32 OS_SocketAddrSetPort_Impl(OS_SockAddr_t *Addr, uint16 PortNum)
sa_port = htons(PortNum);
Accessor = (OS_SockAddr_Accessor_t *)&Addr->AddrData;

switch (Accessor->sockaddr.sa_family)
switch (Accessor->sa.sa_family)
{
case AF_INET:
Accessor->sockaddr_in.sin_port = sa_port;
Accessor->sa_in.sin_port = sa_port;
break;
#ifdef OS_NETWORK_SUPPORTS_IPV6
case AF_INET6:
Accessor->sockaddr_in6.sin6_port = sa_port;
Accessor->sa_in6.sin6_port = sa_port;
break;
#endif
default:
Expand Down
32 changes: 30 additions & 2 deletions src/unit-test-coverage/portable/src/coveragetest-bsd-sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,38 @@
*/

#include "os-portable-coveragetest.h"
#include "os-shared-network.h"
#include "os-shared-sockets.h"
#include "os-shared-idmap.h"
#include "os-shared-file.h"

#include "OCS_sys_socket.h"

void Test_OS_SocketOpen_Impl(void)
{
OSAPI_TEST_FUNCTION_RC(OS_SocketOpen_Impl, (NULL), OS_ERR_NOT_IMPLEMENTED);
OS_object_token_t token = {0};

/* Set up token for index 0 */
token.obj_idx = UT_INDEX_0;

/* Invalid socket type */
OS_stream_table[0].socket_type = -1;
OSAPI_TEST_FUNCTION_RC(OS_SocketOpen_Impl, (&token), OS_ERR_NOT_IMPLEMENTED);

/* Invalid domain type */
OS_stream_table[0].socket_type = OS_SocketType_DATAGRAM;
OS_stream_table[0].socket_domain = -1;
OSAPI_TEST_FUNCTION_RC(OS_SocketOpen_Impl, (&token), OS_ERR_NOT_IMPLEMENTED);

/* Socket error */
OS_stream_table[0].socket_domain = OS_SocketDomain_INET;
UT_SetDeferredRetcode(UT_KEY(OCS_socket), 1, -1);
OSAPI_TEST_FUNCTION_RC(OS_SocketOpen_Impl, (&token), OS_ERROR);

/* Success case */
OS_stream_table[0].socket_type = OS_SocketType_STREAM;
OS_stream_table[0].socket_domain = OS_SocketDomain_INET6;
OSAPI_TEST_FUNCTION_RC(OS_SocketOpen_Impl, (&token), OS_SUCCESS);

}

/* ------------------- End of test cases --------------------------------------*/
Expand All @@ -41,6 +68,7 @@ void Test_OS_SocketOpen_Impl(void)
void Osapi_Test_Setup(void)
{
UT_ResetState(0);
memset(OS_stream_table, 0, sizeof(OS_stream_table));
}

/*
Expand Down
5 changes: 4 additions & 1 deletion src/unit-test-coverage/ut-stubs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@
# the OCS functions that are actually used.
#
add_library(ut_libc_stubs STATIC EXCLUDE_FROM_ALL
src/bsd-select-stubs.c
src/arpa-inet-stubs.c
src/libc-ctype-stubs.c
src/libc-stdio-stubs.c
src/libc-stdlib-stubs.c
src/libc-string-stubs.c
src/netinet-in-stubs.c
src/posix-dirent-stubs.c
src/posix-dlfcn-stubs.c
src/posix-errno-stubs.c
Expand All @@ -54,6 +55,8 @@ add_library(ut_libc_stubs STATIC EXCLUDE_FROM_ALL
src/posix-stat-stubs.c
src/posix-time-stubs.c
src/posix-unistd-stubs.c
src/sys-socket-stubs.c
src/sys-select-stubs.c
src/vxworks-ataDrv-stubs.c
src/vxworks-dosFsLib-stubs.c
src/vxworks-errnoLib-stubs.c
Expand Down
33 changes: 32 additions & 1 deletion src/unit-test-coverage/ut-stubs/inc/OCS_sys_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,41 @@
/* types normally defined in sys/socket.h */
/* ----------------------------------------- */
typedef size_t OCS_socklen_t;
typedef unsigned short int OCS_sa_family_t;

struct OCS_sockaddr
{
char sa[4];
OCS_sa_family_t sa_family;
};

struct OCS_sockaddr_in
{
OCS_sa_family_t sa_family;
uint16_t sin_port;
uint32_t sin_addr;
};

struct OCS_sockaddr_in6
{
OCS_sa_family_t sa_family;
uint16_t sin6_port;
uint32_t sin6_addr[4];
};

enum
{
OCS_EINPROGRESS = -2,
OCS_EWOULDBLOCK,
OCS_AF_INET,
OCS_AF_INET6,
OCS_SOCK_DGRAM,
OCS_SOCK_STREAM,
OCS_IPPROTO_UDP,
OCS_IPPROTO_TCP,
OCS_SOL_SOCKET,
OCS_SO_REUSEADDR,
OCS_SO_ERROR,
OCS_MSG_DONTWAIT
};

/* ----------------------------------------- */
Expand Down
15 changes: 8 additions & 7 deletions src/unit-test-coverage/ut-stubs/inc/OCS_sys_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@
/* ----------------------------------------- */
/* types normally defined in sys/types.h */
/* ----------------------------------------- */
typedef ptrdiff_t OCS_ssize_t;
typedef long OCS_off_t;
typedef unsigned int OCS_mode_t;
typedef long OCS_time_t;
typedef int OCS_pid_t;
typedef int OCS_gid_t;
typedef int OCS_uid_t;
typedef ptrdiff_t OCS_ssize_t;
typedef long OCS_off_t;
typedef unsigned int OCS_mode_t;
typedef long OCS_time_t;
typedef int OCS_pid_t;
typedef int OCS_gid_t;
typedef int OCS_uid_t;
typedef unsigned short int OCS_u_short;

/* ----------------------------------------- */
/* prototypes normally declared in sys/types.h */
Expand Down
38 changes: 27 additions & 11 deletions src/unit-test-coverage/ut-stubs/override_inc/sys/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,32 @@
/* ----------------------------------------- */
/* mappings for declarations in sys/socket.h */
/* ----------------------------------------- */
#define socklen_t OCS_socklen_t
#define sockaddr OCS_sockaddr
#define accept OCS_accept
#define bind OCS_bind
#define connect OCS_connect
#define getsockopt OCS_getsockopt
#define listen OCS_listen
#define recvfrom OCS_recvfrom
#define sendto OCS_sendto
#define setsockopt OCS_setsockopt
#define socket OCS_socket
#define socklen_t OCS_socklen_t
#define sockaddr OCS_sockaddr
#define sockaddr_in OCS_sockaddr_in
#define sockaddr_in6 OCS_sockaddr_in6
#define sa_family_t OCS_sa_family_t
#define accept OCS_accept
#define bind OCS_bind
#define connect OCS_connect
#define getsockopt OCS_getsockopt
#define listen OCS_listen
#define recvfrom OCS_recvfrom
#define sendto OCS_sendto
#define setsockopt OCS_setsockopt
#define socket OCS_socket

#define EINPROGRESS OCS_EINPROGRESS
#define EWOULDBLOCK OCS_EWOULDBLOCK
#define AF_INET OCS_AF_INET
#define AF_INET6 OCS_AF_INET6
#define SOCK_DGRAM OCS_SOCK_DGRAM
#define SOCK_STREAM OCS_SOCK_STREAM
#define IPPROTO_UDP OCS_IPPROTO_UDP
#define IPPROTO_TCP OCS_IPPROTO_TCP
#define SOL_SOCKET OCS_SOL_SOCKET
#define SO_REUSEADDR OCS_SO_REUSEADDR
#define SO_ERROR OCS_SO_ERROR
#define MSG_DONTWAIT OCS_MSG_DONTWAIT

#endif /* OSAL_OVERRIDE_SYS_SOCKET_H */
1 change: 1 addition & 0 deletions src/unit-test-coverage/ut-stubs/override_inc/sys/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
#define pid_t OCS_pid_t
#define gid_t OCS_gid_t
#define uid_t OCS_uid_t
#define u_short OCS_u_short

#endif /* OSAL_OVERRIDE_SYS_TYPES_H */
47 changes: 47 additions & 0 deletions src/unit-test-coverage/ut-stubs/src/arpa-inet-stubs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* NASA Docket No. GSC-18,370-1, and identified as "Operating System Abstraction Layer"
*
* Copyright (c) 2019 United States Government as represented by
* the Administrator of the National Aeronautics and Space Administration.
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* \brief Stubs for arpa/inet.h
* \ingroup ut-stubs
*/
#include <arpa/inet.h>
#include "utstubs.h"
#include <OCS_arpa_inet.h>

const char *OCS_inet_ntop(int af, const void *cp, char *buf, size_t len)
{
int32 Status;

Status = UT_DEFAULT_IMPL(OCS_inet_ntop);

if (Status == 0)
{
/* "nominal" response */
return inet_ntop(af, cp, buf, len);
}

return (char *)0;
}

int OCS_inet_pton(int af, const char *cp, void *buf)
{
return UT_DEFAULT_IMPL(OCS_inet_pton);
}
46 changes: 46 additions & 0 deletions src/unit-test-coverage/ut-stubs/src/netinet-in-stubs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* NASA Docket No. GSC-18,370-1, and identified as "Operating System Abstraction Layer"
*
* Copyright (c) 2019 United States Government as represented by
* the Administrator of the National Aeronautics and Space Administration.
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* \brief Stubs for netinet/in.h
* \ingroup ut-stubs
*/
#include "utstubs.h"
#include <OCS_arpa_inet.h>

uint16_t OCS_htons(uint16_t hostshort)
{
return UT_DEFAULT_IMPL(OCS_htons);
}

uint16_t OCS_ntohs(uint16_t netshort)
{
return UT_DEFAULT_IMPL(OCS_ntohs);
}

uint32_t OCS_htonl(uint32_t hostlong)
{
return UT_DEFAULT_IMPL(OCS_htonl);
}

uint32_t OCS_ntohl(uint32_t netlong)
{
return UT_DEFAULT_IMPL(OCS_ntohl);
}
Loading

0 comments on commit 5c78d08

Please sign in to comment.