apps/examples/pipe: check POSIX-compliant behavior of NuttX's FIFOs

Adjust the test considering the POSIX-compliant behavior of the
NuttX's FIFO (mkfifo), which should block `open` for read-only
and write-only. This test's result is expected to be the same with
any other POSIX-compliant system.

This commit also:

 * Fix redirect test;
 * Use pthread instead `task_create` to be able to run this test
on POSIX-compliant systems;
 * General fixes regarding formatting and error messages;
This commit is contained in:
Tiago Medicci Serrano
2023-03-26 19:19:48 -03:00
committed by Xiang Xiao
parent f2819b71e5
commit 0a9b983e90
4 changed files with 361 additions and 215 deletions

View File

@@ -33,6 +33,31 @@
#include "pipe.h"
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: open_write_only
****************************************************************************/
static void *open_write_only(pthread_addr_t pvarg)
{
void *fd_addr = (void *)pvarg;
printf("open_write_only: Opening FIFO for write access\n");
((int *)fd_addr)[1] = open(FIFO_PATH1, O_WRONLY);
if (((int *)fd_addr)[1] < 0)
{
fprintf(stderr, \
"open_write_only: Failed to open FIFO %s for writing,"
"errno=%d\n", FIFO_PATH1, errno);
return (void *)(uintptr_t)1;
}
return NULL;
}
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -53,6 +78,15 @@ int main(int argc, FAR char *argv[])
printf("\npipe_main: Performing FIFO test\n");
pthread_t writeonly;
void *status;
/* Open one end of the FIFO for reading and the other end for writing.
* NOTE: This test's result is expected to be the same on any other
* POSIX-compliant system. NuttX FIFOs block for write-only and
* read-only (see interlock_test()).
*/
ret = mkfifo(FIFO_PATH1, 0666);
if (ret < 0)
{
@@ -60,18 +94,12 @@ int main(int argc, FAR char *argv[])
return 1;
}
/* Open one end of the FIFO for reading and the other end for writing.
* NOTE: The following might not work on most FIFO implementations because
* the attempt to open just one end of the FIFO for writing might block.
* The NuttX FIFOs block only on open for read-only (see interlock_test()).
*/
ret = pthread_create(&writeonly, NULL, open_write_only, &fd);
fd[1] = open(FIFO_PATH1, O_WRONLY);
if (fd[1] < 0)
if (ret < 0)
{
fprintf(stderr,
"pipe_main: Failed to open FIFO %s for writing, errno=%d\n",
FIFO_PATH1, errno);
fprintf(stderr, "redirection_test: "
"Failed to create open_write_only task: %d\n", ret);
return 2;
}
@@ -89,9 +117,28 @@ int main(int argc, FAR char *argv[])
return 3;
}
/* Wait for open_write_only thread to complete */
fprintf(stderr, "open_write_only: Waiting for open_write_only thread\n");
ret = pthread_join(writeonly, &status);
if (ret != 0)
{
fprintf(stderr, "pipe_main: pthread_join failed, error=%d\n", ret);
return 4;
}
else
{
fprintf(stderr, "pipe_main: open_write_only returned %p\n", status);
if (status != NULL)
{
return 5;
}
}
/* Then perform the test using those file descriptors */
ret = transfer_test(fd[0], fd[1]);
if (close(fd[0]) != 0)
{
fprintf(stderr, "pipe_main: close failed: %d\n", errno);
@@ -107,9 +154,21 @@ int main(int argc, FAR char *argv[])
if (ret != 0)
{
fprintf(stderr, "pipe_main: FIFO test FAILED (%d)\n", ret);
return 4;
return 6;
}
/* Perform the FIFO interlock test */
printf("\npipe_main: Performing pipe interlock test\n");
ret = interlock_test();
if (ret != 0)
{
fprintf(stderr, "pipe_main: FIFO interlock test FAILED (%d)\n", ret);
return 7;
}
printf("pipe_main: FIFO interlock test PASSED\n");
printf("pipe_main: FIFO test PASSED\n");
#else
@@ -126,16 +185,18 @@ int main(int argc, FAR char *argv[])
if (ret < 0)
{
fprintf(stderr, "pipe_main: pipe failed with errno=%d\n", errno);
return 5;
return 8;
}
/* Then perform the test using those file descriptors */
ret = transfer_test(fd[0], fd[1]);
if (close(fd[0]) != 0)
{
fprintf(stderr, "pipe_main: close failed: %d\n", errno);
}
if (close(fd[1]) != 0)
{
fprintf(stderr, "pipe_main: close failed: %d\n", errno);
@@ -144,35 +205,23 @@ int main(int argc, FAR char *argv[])
if (ret != 0)
{
fprintf(stderr, "pipe_main: PIPE test FAILED (%d)\n", ret);
return 6;
return 9;
}
printf("pipe_main: PIPE test PASSED\n");
/* Perform the FIFO interlock test */
printf("\npipe_main: Performing pipe interlock test\n");
ret = interlock_test();
if (ret != 0)
{
fprintf(stderr, "pipe_main: FIFO interlock test FAILED (%d)\n", ret);
return 7;
}
printf("pipe_main: PIPE interlock test PASSED\n");
/* Perform the pipe redirection test */
printf("\npipe_main: Performing redirection test\n");
ret = redirection_test();
if (ret != 0)
{
fprintf(stderr, "pipe_main: FIFO redirection test FAILED (%d)\n", ret);
return 7;
fprintf(stderr, "pipe_main: PIPE redirection test FAILED (%d)\n", ret);
return 10;
}
printf("pipe_main: PIPE redirection test PASSED\n");
printf("pipe_main: PIPE test PASSED\n");
#else
printf("\npipe_main: Skipping pipe test\n");