From 1b43a63e5987af4ad0cb973d03de3154ac477791 Mon Sep 17 00:00:00 2001 From: Alex Campbell Date: Thu, 3 Dec 2020 14:03:12 -0500 Subject: [PATCH] fix #377 Add Single Select Test --- src/tests/select-test/select-test.c | 321 ++++++++++++++++++++++++++++ 1 file changed, 321 insertions(+) create mode 100644 src/tests/select-test/select-test.c diff --git a/src/tests/select-test/select-test.c b/src/tests/select-test/select-test.c new file mode 100644 index 000000000..0b3468f09 --- /dev/null +++ b/src/tests/select-test/select-test.c @@ -0,0 +1,321 @@ +/* + * 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. + */ + +/* + * Filename: select-test.c + * + * Purpose: This file contains functional tests for "osapi-select" + * + */ + +#include +#include +#include +#include "common_types.h" +#include "osapi.h" +#include "utassert.h" +#include "uttest.h" +#include "utbsp.h" + +#define UT_EXIT_LOOP_MAX 100 + +osal_id_t task_id; +osal_id_t s_task_id; +osal_id_t s2_task_id; +osal_id_t s3_task_id; +osal_id_t s_socket_id; +osal_id_t s2_socket_id; +osal_id_t s3_socket_id; +osal_id_t c_socket_id; +osal_id_t c2_socket_id; +osal_id_t c3_socket_id; +OS_SockAddr_t s_addr; +OS_SockAddr_t s2_addr; +OS_SockAddr_t s3_addr; +OS_SockAddr_t c_addr; +OS_SockAddr_t c2_addr; +OS_SockAddr_t c3_addr; + +/* *************************************** MAIN ************************************** */ + +char * fsAddrPtr = NULL; +static osal_id_t setup_file(void) +{ + osal_id_t id; + OS_mkfs(fsAddrPtr, "/ramdev3", "RAM3", 512, 20); + OS_mount("/ramdev3", "/drive3"); + OS_OpenCreate(&id, "/drive3/select_test.txt", OS_FILE_FLAG_CREATE | OS_FILE_FLAG_TRUNCATE, OS_READ_ONLY); + return id; +} + +void Setup_Server(void) +{ + int32 expected; + int32 actual; + + /* + * Set up a server + */ + + /* Open a server socket */ + s_socket_id = OS_OBJECT_ID_UNDEFINED; + expected = OS_SUCCESS; + actual = OS_SocketOpen(&s_socket_id, OS_SocketDomain_INET, OS_SocketType_STREAM); + UtAssert_True(actual == expected, "OS_SocketOpen() (%ld) == OS_SUCCESS", (long)actual); + UtAssert_True(OS_ObjectIdDefined(s_socket_id), "s_socket_id (%lu) != 0", OS_ObjectIdToInteger(s_socket_id)); + + /* Initialize server address */ + actual = OS_SocketAddrInit(&s_addr, OS_SocketDomain_INET); + UtAssert_True(actual == expected, "OS_SocketAddrInit() (%ld) == OS_SUCCESS", (long)actual); + + /* Set server port */ + actual = OS_SocketAddrSetPort(&s_addr, 9997); + UtAssert_True(actual == expected, "OS_SocketAddrSetPort() (%ld) == OS_SUCCESS", (long)actual); + + /* Set server address */ + actual = OS_SocketAddrFromString(&s_addr, "127.0.0.1"); + UtAssert_True(actual == expected, "OS_SocketAddrFromString() (%ld) == OS_SUCCESS", (long)actual); + + /* Bind server socket to server address */ + actual = OS_SocketBind(s_socket_id, &s_addr); + UtAssert_True(actual == expected, "OS_SocketBind() (%ld) == OS_SUCCESS", (long)actual); +} + +void Setup_Client(void) +{ + int32 expected; + int32 actual; + + /* + * Set up a client + */ + + /* Open a client socket */ + expected = OS_SUCCESS; + c_socket_id = OS_OBJECT_ID_UNDEFINED; + + actual = OS_SocketOpen(&c_socket_id, OS_SocketDomain_INET, OS_SocketType_STREAM); + UtAssert_True(actual == expected, "OS_SocketOpen() (%ld) == OS_SUCCESS", (long)actual); + UtAssert_True(OS_ObjectIdDefined(c_socket_id), "c_socket_id (%lu) != 0", OS_ObjectIdToInteger(c_socket_id)); + + /* Initialize client address */ + actual = OS_SocketAddrInit(&c_addr, OS_SocketDomain_INET); + UtAssert_True(actual == expected, "OS_SocketAddrInit() (%ld) == OS_SUCCESS", (long)actual); + + /* Set client port */ + actual = OS_SocketAddrSetPort(&c_addr, 9996); + UtAssert_True(actual == expected, "OS_SocketAddrSetPort() (%ld) == OS_SUCCESS", (long)actual); + + /* Set client address */ + actual = OS_SocketAddrFromString(&c_addr, "127.0.0.1"); + UtAssert_True(actual == expected, "OS_SocketAddrFromString() (%ld) == OS_SUCCESS", (long)actual); +} + +void Server_Fn(void) +{ + osal_id_t connsock_id = OS_OBJECT_ID_UNDEFINED; + uint32 iter; + OS_SockAddr_t addr; + char Buf_rcv_s[4] = {0}; + char Buf_trans[8] = {0}; + uint8 Buf_each_char_s[256] = {0}; + + //OS_TaskDelay(1000); + + /* Accept incoming connections */ + OS_SocketAccept(s_socket_id, &connsock_id, &addr, OS_PEND); + + /* Recieve incoming data from client*/ + OS_TimedRead(connsock_id, Buf_rcv_s, sizeof(Buf_rcv_s), 10); + + /* Transform the incoming data and send it back to client */ + strcpy(Buf_trans, "uvw"); + strcat(Buf_trans, Buf_rcv_s); + OS_TimedWrite(connsock_id, Buf_trans, sizeof(Buf_trans), 10); + + /* Send all 256 chars to client */ + for (iter = 0; iter < 256; iter++) + { + Buf_each_char_s[iter] = iter; + } + + OS_TimedWrite(connsock_id, Buf_each_char_s, sizeof(Buf_each_char_s), 10); + + OS_close(s_socket_id); + OS_close(connsock_id); + +} /* end Server_Fn */ + +void TestSelectSingle(void) +{ + /* + * Test Case For: + * int32 OS_SelectSingle(uint32 objid, uint32 *StateFlags, int32 msecs); + */ + int32 expected; + int32 actual; + + + Setup_Server(); + Setup_Client(); + + expected = OS_SUCCESS; + + /* + * Create a server thread, and connect client from + * this thread to server thread and verify connection + */ + + /* Create a server task/thread */ + int32 status = OS_TaskCreate(&s_task_id, "Server", Server_Fn, OSAL_TASK_STACK_ALLOCATE, OSAL_SIZE_C(16384), + OSAL_PRIORITY_C(50), 0); + UtAssert_True(status == OS_SUCCESS, "OS_TaskCreate() (%ld) == OS_SUCCESS", (long)status); + + /* Connect to a server */ + actual = OS_SocketConnect(c_socket_id, &s_addr, 10); + UtAssert_True(actual == expected, "OS_SocketConnect() (%ld) == OS_SUCCESS", (long)actual); + + UtPrintf("Finished all the setup !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); + + // OS_TaskDelay(1000); + + uint32 StateFlags; + StateFlags = OS_STREAM_STATE_READABLE; + actual = OS_SelectSingle(c_socket_id, &StateFlags, 5000); + + /* Verify Outputs */ + UtAssert_True(actual == expected, "OS_SelectSingle() (%ld) == OS_SUCCESS", (long)actual); + UtAssert_True(StateFlags == OS_STREAM_STATE_READABLE, "OS_SelectSingle() (%d) == OS_STREAM_STATE_READABLE", StateFlags); + + + StateFlags = OS_STREAM_STATE_WRITABLE; + actual = OS_SelectSingle(c_socket_id, &StateFlags, 5000); + + /* Verify Outputs */ + UtAssert_True(actual == expected, "OS_SelectSingle() (%ld) == OS_SUCCESS", (long)actual); + UtAssert_True(StateFlags == OS_STREAM_STATE_WRITABLE, "OS_SelectSingle() (%d) == OS_STREAM_STATE_WRITABLE", StateFlags); + + + + + UtPrintf("End of Select stuff @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"); + + + uint32 iter; + uint32 loopcnt; + OS_task_prop_t taskprop; + char Buf_rcv_c[4] = {0}; + char Buf_send_c[4] = {0}; + char Buf_rcv_trans[8] = {0}; + char Buf_expec_trans[8] = {0}; + uint8 Buf_each_expected[256] = {0}; + uint8 Buf_each_char_rcv[256] = {0}; + + /* + * Once connection is made between + * server and client, transfer data + */ + + /* Send data to server to be transformed and sent back */ + strcpy(Buf_send_c, "xyz"); + expected = sizeof(Buf_send_c); + actual = OS_TimedWrite(c_socket_id, Buf_send_c, sizeof(Buf_send_c), 10); + UtAssert_True(actual == expected, "OS_TimedWrite() (%ld) == %ld", (long)actual, (long)expected); + + /* Recieve back transformed data from server*/ + expected = sizeof(Buf_expec_trans); + strcpy(Buf_expec_trans, "uvwxyz"); + + actual = OS_TimedRead(c_socket_id, Buf_rcv_trans, sizeof(Buf_rcv_trans), 10); + UtAssert_True(actual == expected, "OS_TimedRead() (%ld) == %ld", (long)actual, (long)expected); + UtAssert_True(strcmp(Buf_rcv_trans, Buf_expec_trans) == 0, "Buf_rcv_trans (%s) == Buf_expected (%s)", Buf_rcv_trans, + Buf_expec_trans); + + /* Recieve all 256 chars from server one at a time */ + expected = sizeof(Buf_each_char_rcv); + actual = OS_TimedRead(c_socket_id, Buf_each_char_rcv, sizeof(Buf_each_char_rcv), 10); + UtAssert_True(actual == expected, "OS_TimedRead() (%ld) == %ld", (long)actual, (long)expected); + + /* Verify all 256 chars received */ + for (iter = 0; iter < 256; iter++) + { + Buf_each_expected[iter] = iter; + } + + UtAssert_True(memcmp(Buf_each_expected, Buf_each_char_rcv, sizeof(Buf_each_expected)) == 0, "buffer content match"); + + /* Once connection socket is closed, verify that no data is recieved */ + expected = 0; + actual = OS_TimedRead(c_socket_id, Buf_rcv_c, sizeof(Buf_rcv_c), 10); + UtAssert_True(actual == expected, "OS_TimedRead() (%ld) == %ld", (long)actual, (long)expected); + + /* + * NOTE: Tests for invalid and other nominal input parameters + * to some of the network functions being called here are already + * tested in TestDatagramNetworkApi_Setup() + */ + + /* Looping delay in parent task to wait for child task to exit */ + loopcnt = 0; + while ((OS_TaskGetInfo(s_task_id, &taskprop) == OS_SUCCESS) && (loopcnt < UT_EXIT_LOOP_MAX)) + { + OS_TaskDelay(10); + loopcnt++; + } + UtAssert_True(loopcnt < UT_EXIT_LOOP_MAX, "Task exited after %ld iterations", (long)loopcnt); +} + +void TestSelectMultiple(void) +{ + /* + * Test Case For: + * int32 OS_SelectMultiple(OS_FdSet *ReadSet, OS_FdSet *WriteSet, int32 msecs); + */ + OS_FdSet ReadSet; + OS_FdSet WriteSet; + int32 expected = OS_SUCCESS; + int32 actual; + + OS_SelectFdZero(&ReadSet); + OS_SelectFdZero(&WriteSet); + + osal_id_t fd = setup_file(); + + OS_SelectFdAdd(&ReadSet, fd); + actual = OS_SelectMultiple(&ReadSet, &WriteSet, 5000); + + /* Verify Outputs */ + UtAssert_True(actual == expected, "OS_SelectMultiple() (%ld) == OS_SUCCESS", (long)actual); +} + +void UtTest_Setup(void) +{ + if (OS_API_Init() != OS_SUCCESS) + { + UtAssert_Abort("OS_API_Init() failed"); + } + + /* + * Register the test setup and check routines in UT assert + */ + + UtTest_Add(TestSelectSingle, NULL, NULL, "TestSelectSingle"); + UtTest_Add(TestSelectMultiple, NULL, NULL, "TestSelectMultiple"); +} \ No newline at end of file