Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #939, rework shell test #940

Merged
merged 1 commit into from
Apr 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 61 additions & 21 deletions src/tests/shell-test/shell-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,36 @@

#define OS_TEST_SHELL_FILENAME "/drive0/shell_test.txt"

const char OS_TEST_SHELL_COMMAND[] = "echo";
const char OS_TEST_SHELL_STRING[] = "ValueToEchoInTheFile";

/*
* Extra chars to allow for quoting and whitespace.
*
* There needs to be spaces between arguments, and depending
* on the shell config and its behavior it may echo extra whitespace
* and/or other chars, and a newline itself may be multiple chars (CR+LF).
*
* This allows up to this many extra chars surrounding the test string.
*/
#define OS_TEST_SHELL_MAX_EXTRA_CHARS 8

/*
* Sizes for the echo buffer and command buffer
* (both include some extra chars)
*/
#define OS_TEST_SHELL_ECHO_BUFFER_SIZE (sizeof(OS_TEST_SHELL_STRING) + OS_TEST_SHELL_MAX_EXTRA_CHARS)
#define OS_TEST_SHELL_CMD_BUFFER_SIZE (sizeof(OS_TEST_SHELL_COMMAND) + OS_TEST_SHELL_ECHO_BUFFER_SIZE)

/* *************************************** MAIN ************************************** */

void TestOutputToFile(void)
{
char cmd[33];
char cmd[OS_TEST_SHELL_CMD_BUFFER_SIZE + 1]; /* +1 char for term byte */
char filename[OS_MAX_PATH_LEN];
char buffer[21];
char copyofbuffer[21];
char buffer[OS_TEST_SHELL_ECHO_BUFFER_SIZE + 1]; /* +1 char for term byte */
size_t size;
int32 filepos;
int32 status;
osal_id_t fd;

Expand All @@ -48,17 +69,12 @@ void TestOutputToFile(void)
strncpy(filename, "/drive0/Filename1", sizeof(filename) - 1);
filename[sizeof(filename) - 1] = 0;

strcpy(buffer, "ValueToEchoInTheFile");
strcpy(copyofbuffer, buffer); /* hold a copy of the buffer */

/* Open In R/W mode */
status = OS_OpenCreate(&fd, OS_TEST_SHELL_FILENAME, OS_FILE_FLAG_CREATE, OS_READ_WRITE);
UtAssert_True(status >= OS_SUCCESS, "status after creat = %d", (int)status);

/* Write the string */
size = strlen(buffer);

snprintf(cmd, sizeof(cmd), "echo -n \"%s\"", buffer);
/* assemble command */
snprintf(cmd, sizeof(cmd), "%s \"%s\"", OS_TEST_SHELL_COMMAND, OS_TEST_SHELL_STRING);

status = OS_ShellOutputToFile(cmd, fd);
if (status == OS_ERR_NOT_IMPLEMENTED)
Expand All @@ -69,19 +85,43 @@ void TestOutputToFile(void)
{
UtAssert_True(status >= OS_SUCCESS, "status after shell output to file = %d", (int)status);

strcpy(buffer, "");

/* lseek back to the beginning of the file */
status = OS_lseek(fd, 0, 0);
UtAssert_True(status >= OS_SUCCESS, "status after lseek = %d", (int)status);

/*Read what we wrote to the file */
status = OS_read(fd, (void *)buffer, size);
UtAssert_True(status == size, "status after read = %d size = %lu", (int)status, (unsigned long)size);
if (status >= OS_SUCCESS)
/* output content should be at least this size */
size = sizeof(OS_TEST_SHELL_STRING) - 1;

/* Use lseek to determine size of the file */
filepos = OS_lseek(fd, 0, OS_SEEK_END);
UtAssert_True(filepos >= size, "size of output file=%d, echoed string len=%lu", (int)filepos,
(unsigned long)size);

/*
* Different shells vary in how the echoed output actually appears in the file.
*
* Depending on config it may echo the command itself in addition to the output,
* and there may be whitespace/newlines that should be ignored.
*
* Either way the content should appear toward the _end_ of the file.
*/
if (filepos < OS_TEST_SHELL_ECHO_BUFFER_SIZE)
{
UtAssert_True(strcmp(buffer, copyofbuffer) == 0, "Read: %s, Written: %s", buffer, copyofbuffer);
filepos = 0;
}
else
{
filepos -= OS_TEST_SHELL_ECHO_BUFFER_SIZE;
}

status = OS_lseek(fd, filepos, OS_SEEK_SET);
UtAssert_True(status == filepos, "lseek to position %d, result=%d", (int)filepos, (int)status);

/* Read what we wrote to the file */
/* By memset() first and reading one byte less than the buffer, the result is ensured to be terminated */
memset(buffer, 0, sizeof(buffer));
status = OS_read(fd, buffer, sizeof(buffer) - 1);
UtAssert_True(status >= size, "status after read = %d, min valid size = %lu", (int)status, (unsigned long)size);

/* The test should pass if the expected string is anywhere in the buffer */
UtAssert_True(strstr(buffer, OS_TEST_SHELL_STRING) != NULL, "Read: \'%s\', Expected: \'%s\'", buffer,
OS_TEST_SHELL_STRING);
}

/* close the file */
Expand Down