|
| 1 | +/* |
| 2 | + * NASA Docket No. GSC-18,370-1, and identified as "Operating System Abstraction Layer" |
| 3 | + * |
| 4 | + * Copyright (c) 2019 United States Government as represented by |
| 5 | + * the Administrator of the National Aeronautics and Space Administration. |
| 6 | + * All Rights Reserved. |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +#include <stdio.h> |
| 22 | +#include <string.h> |
| 23 | +#include <stdlib.h> |
| 24 | + |
| 25 | +#include "common_types.h" |
| 26 | +#include "osapi.h" |
| 27 | +#include "utassert.h" |
| 28 | +#include "uttest.h" |
| 29 | +#include "utbsp.h" |
| 30 | + |
| 31 | +#define OS_TEST_SHELL_FILENAME "/drive0/shell_test.txt" |
| 32 | + |
| 33 | +/* *************************************** MAIN ************************************** */ |
| 34 | + |
| 35 | +void TestOutputToFile(void) |
| 36 | +{ |
| 37 | + char cmd[33]; |
| 38 | + char filename[OS_MAX_PATH_LEN]; |
| 39 | + char buffer[21]; |
| 40 | + char copyofbuffer[21]; |
| 41 | + size_t size; |
| 42 | + int32 status; |
| 43 | + osal_id_t fd; |
| 44 | + |
| 45 | + OS_mkfs(NULL, "/ramdev0", "RAM", 512, 20); |
| 46 | + OS_mount("/ramdev0", "/drive0"); |
| 47 | + |
| 48 | + strncpy(filename, "/drive0/Filename1", sizeof(filename) - 1); |
| 49 | + filename[sizeof(filename) - 1] = 0; |
| 50 | + |
| 51 | + strcpy(buffer, "ValueToEchoInTheFile"); |
| 52 | + strcpy(copyofbuffer, buffer); /* hold a copy of the buffer */ |
| 53 | + |
| 54 | + /* Open In R/W mode */ |
| 55 | + status = OS_OpenCreate(&fd, OS_TEST_SHELL_FILENAME, OS_FILE_FLAG_CREATE, OS_READ_WRITE); |
| 56 | + UtAssert_True(status >= OS_SUCCESS, "status after creat = %d", (int)status); |
| 57 | + |
| 58 | + /* Write the string */ |
| 59 | + size = strlen(buffer); |
| 60 | + |
| 61 | + snprintf(cmd, sizeof(cmd), "echo -n \"%s\"", buffer); |
| 62 | + |
| 63 | + status = OS_ShellOutputToFile(cmd, fd); |
| 64 | + if (status == OS_ERR_NOT_IMPLEMENTED) |
| 65 | + { |
| 66 | + UtAssert_NA("Shell Commands not implemented"); |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + UtAssert_True(status >= OS_SUCCESS, "status after shell output to file = %d", (int)status); |
| 71 | + |
| 72 | + strcpy(buffer, ""); |
| 73 | + |
| 74 | + /* lseek back to the beginning of the file */ |
| 75 | + status = OS_lseek(fd, 0, 0); |
| 76 | + UtAssert_True(status >= OS_SUCCESS, "status after lseek = %d", (int)status); |
| 77 | + |
| 78 | + /*Read what we wrote to the file */ |
| 79 | + status = OS_read(fd, (void *)buffer, size); |
| 80 | + UtAssert_True(status == size, "status after read = %d size = %lu", (int)status, (unsigned long)size); |
| 81 | + if (status >= OS_SUCCESS) |
| 82 | + { |
| 83 | + UtAssert_True(strcmp(buffer, copyofbuffer) == 0, "Read: %s, Written: %s", buffer, copyofbuffer); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /* close the file */ |
| 88 | + status = OS_close(fd); |
| 89 | + UtAssert_True(status == OS_SUCCESS, "status after close = %d", (int)status); |
| 90 | + |
| 91 | + /* try removing the file from the drive */ |
| 92 | + status = OS_remove(OS_TEST_SHELL_FILENAME); |
| 93 | + UtAssert_True(status == OS_SUCCESS, "status after remove = %d", (int)status); |
| 94 | + |
| 95 | +} /* end TestOutputToFile */ |
| 96 | + |
| 97 | +void UtTest_Setup(void) |
| 98 | +{ |
| 99 | + if (OS_API_Init() != OS_SUCCESS) |
| 100 | + { |
| 101 | + UtAssert_Abort("OS_API_Init() failed"); |
| 102 | + } |
| 103 | + |
| 104 | + /* |
| 105 | + * Register the test setup and check routines in UT assert |
| 106 | + */ |
| 107 | + UtTest_Add(TestOutputToFile, NULL, NULL, "ShellTest"); |
| 108 | +} |
0 commit comments