123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525 |
- /*
- ** Copyright (C) 2001-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
- **
- ** This program is free software; you can redistribute it and/or modify
- ** it under the terms of the GNU General Public License as published by
- ** the Free Software Foundation; either version 2 of the License, or
- ** (at your option) any later version.
- **
- ** This program is distributed in the hope that it will be useful,
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ** GNU General Public License for more details.
- **
- ** You should have received a copy of the GNU General Public License
- ** along with this program; if not, write to the Free Software
- ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- /*==========================================================================
- ** This is a test program which tests reading from and writing to pipes.
- */
- #include "sfconfig.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #if (OS_IS_WIN32 || HAVE_PIPE == 0 || HAVE_WAITPID == 0)
- int
- main (void)
- {
- puts (" pipe_test : this test doesn't work on this OS.") ;
- return 0 ;
- } /* main */
- #else
- #if HAVE_UNISTD_H
- #include <unistd.h>
- #endif
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/wait.h>
- #include <sndfile.h>
- #include "utils.h"
- typedef struct
- { int format ;
- const char *ext ;
- } FILETYPE ;
- static int file_exists (const char *filename) ;
- static void useek_pipe_rw_test (int filetype, const char *ext) ;
- static void pipe_read_test (int filetype, const char *ext) ;
- static void pipe_write_test (const char *ext) ;
- static void pipe_test_others (FILETYPE*, FILETYPE*) ;
- static FILETYPE read_write_types [] =
- { { SF_FORMAT_RAW , "raw" },
- { SF_FORMAT_AU , "au" },
- /* Lite remove start */
- { SF_FORMAT_PAF , "paf" },
- { SF_FORMAT_IRCAM , "ircam" },
- { SF_FORMAT_PVF , "pvf" },
- /* Lite remove end */
- { 0 , NULL }
- } ;
- static FILETYPE read_only_types [] =
- { { SF_FORMAT_RAW , "raw" },
- { SF_FORMAT_AU , "au" },
- { SF_FORMAT_AIFF , "aiff" },
- { SF_FORMAT_WAV , "wav" },
- { SF_FORMAT_W64 , "w64" },
- /* Lite remove start */
- { SF_FORMAT_PAF , "paf" },
- { SF_FORMAT_NIST , "nist" },
- { SF_FORMAT_IRCAM , "ircam" },
- { SF_FORMAT_MAT4 , "mat4" },
- { SF_FORMAT_MAT5 , "mat5" },
- { SF_FORMAT_SVX , "svx" },
- { SF_FORMAT_PVF , "pvf" },
- /* Lite remove end */
- { 0 , NULL }
- } ;
- int
- main (void)
- { int k ;
- if (file_exists ("libsndfile.spec.in"))
- exit_if_true (chdir ("tests") != 0, "\n Error : chdir ('tests') failed.\n") ;
- for (k = 0 ; read_only_types [k].format ; k++)
- pipe_read_test (read_only_types [k].format, read_only_types [k].ext) ;
- for (k = 0 ; read_write_types [k].format ; k++)
- pipe_write_test (read_write_types [k].ext) ;
- for (k = 0 ; read_write_types [k].format ; k++)
- useek_pipe_rw_test (read_write_types [k].format, read_write_types [k].ext) ;
- if (0)
- pipe_test_others (read_write_types, read_only_types) ;
- return 0 ;
- } /* main */
- /*==============================================================================
- */
- static void
- pipe_read_test (int filetype, const char *ext)
- { static short data [PIPE_TEST_LEN] ;
- static char buffer [256] ;
- static char filename [256] ;
- SNDFILE *outfile ;
- SF_INFO sfinfo ;
- int k, retval ;
- snprintf (filename, sizeof (filename), "pipe_in.%s", ext) ;
- print_test_name ("pipe_read_test", filename) ;
- sfinfo.format = filetype | SF_FORMAT_PCM_16 ;
- sfinfo.channels = 1 ;
- sfinfo.samplerate = 44100 ;
- for (k = 0 ; k < PIPE_TEST_LEN ; k++)
- data [k] = PIPE_INDEX (k) ;
- outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
- test_writef_short_or_die (outfile, 0, data, PIPE_TEST_LEN, __LINE__) ;
- sf_close (outfile) ;
- snprintf (buffer, sizeof (buffer), "cat %s | ./stdin_test %s ", filename, ext) ;
- if ((retval = system (buffer)) != 0)
- { retval = WEXITSTATUS (retval) ;
- printf ("\n\n Line %d : pipe test returned error for file type \"%s\".\n\n", __LINE__, ext) ;
- exit (retval) ;
- } ;
- unlink (filename) ;
- puts ("ok") ;
- return ;
- } /* pipe_read_test */
- static void
- pipe_write_test (const char *ext)
- { static char buffer [256] ;
- int retval ;
- print_test_name ("pipe_write_test", ext) ;
- snprintf (buffer, sizeof (buffer), "./stdout_test %s | ./stdin_test %s ", ext, ext) ;
- if ((retval = system (buffer)))
- { retval = WEXITSTATUS (retval) ;
- printf ("\n\n Line %d : pipe test returned error file type \"%s\".\n\n", __LINE__, ext) ;
- exit (retval) ;
- } ;
- puts ("ok") ;
- return ;
- } /* pipe_write_test */
- /*==============================================================================
- */
- static void
- useek_pipe_rw_short (const char * ext, SF_INFO * psfinfo_write, SF_INFO * psfinfo_read)
- { static short buffer [PIPE_TEST_LEN] ;
- static short data [PIPE_TEST_LEN] ;
- SNDFILE *outfile ;
- SNDFILE *infile_piped ;
- int k, status = 0 ;
- int pipefd [2] ;
- pid_t pida ;
- for (k = 0 ; k < PIPE_TEST_LEN ; k++)
- data [k] = PIPE_INDEX (k) ;
- /*
- ** Create the pipe.
- */
- exit_if_true (pipe (pipefd) != 0, "\n\n%s %d : pipe failed : %s\n", __func__, __LINE__, strerror (errno)) ;
- /*
- ** Attach the write end of the pipe to be written to.
- */
- if ((outfile = sf_open_fd (pipefd [1], SFM_WRITE, psfinfo_write, SF_TRUE)) == NULL)
- { printf ("\n\n%s %d : unable to create unseekable pipe for write type \"%s\".\n", __func__, __LINE__, ext) ;
- printf ("\t%s\n\n", sf_strerror (outfile)) ;
- exit (1) ;
- } ;
- if (sf_error (outfile) != SF_ERR_NO_ERROR)
- { printf ("\n\n%s %d : unable to open unseekable pipe for write type \"%s\".\n\n", __func__, __LINE__, ext) ;
- exit (1) ;
- } ;
- /*
- ** Attach the read end of the pipe to be read from.
- */
- if ((infile_piped = sf_open_fd (pipefd [0], SFM_READ, psfinfo_read, SF_TRUE)) == NULL)
- { printf ("\n\n%s %d : unable to create unseekable pipe for read type. \"%s\".\n\n", __func__, __LINE__, ext) ;
- exit (1) ;
- } ;
- if (sf_error (infile_piped) != SF_ERR_NO_ERROR)
- { printf ("\n\n%s %d : unable to open unseekable pipe for read type \"%s\".\n\n", __func__, __LINE__, ext) ;
- exit (1) ;
- } ;
- /* Fork a child process that will write directly into the pipe. */
- if ((pida = fork ()) == 0) /* child process */
- { test_writef_short_or_die (outfile, 0, data, PIPE_TEST_LEN, __LINE__) ;
- exit (0) ;
- } ;
- /* In the parent process, read from the pipe and compare what is read
- ** to what is written, if they match everything went as planned.
- */
- test_readf_short_or_die (infile_piped, 0, buffer, PIPE_TEST_LEN, __LINE__) ;
- if (memcmp (buffer, data, sizeof (buffer)) != 0)
- { printf ("\n\n%s %d : unseekable pipe test failed for file type \"%s\".\n\n", __func__, __LINE__, ext) ;
- exit (1) ;
- } ;
- /* Wait for the child process to return. */
- waitpid (pida, &status, 0) ;
- status = WEXITSTATUS (status) ;
- sf_close (outfile) ;
- sf_close (infile_piped) ;
- if (status != 0)
- { printf ("\n\n%s %d : status of child process is %d for file type %s.\n\n", __func__, __LINE__, status, ext) ;
- exit (1) ;
- } ;
- return ;
- } /* useek_pipe_rw_short */
- static void
- useek_pipe_rw_float (const char * ext, SF_INFO * psfinfo_write, SF_INFO * psfinfo_read)
- { static float buffer [PIPE_TEST_LEN] ;
- static float data [PIPE_TEST_LEN] ;
- SNDFILE *outfile ;
- SNDFILE *infile_piped ;
- int k, status = 0 ;
- int pipefd [2] ;
- pid_t pida ;
- for (k = 0 ; k < PIPE_TEST_LEN ; k++)
- data [k] = PIPE_INDEX (k) ;
- /*
- ** Create the pipe.
- */
- exit_if_true (pipe (pipefd) != 0, "\n\n%s %d : pipe failed : %s\n", __func__, __LINE__, strerror (errno)) ;
- /*
- ** Attach the write end of the pipe to be written to.
- */
- if ((outfile = sf_open_fd (pipefd [1], SFM_WRITE, psfinfo_write, SF_TRUE)) == NULL)
- { printf ("\n\n%s %d : unable to create unseekable pipe for write type \"%s\".\n", __func__, __LINE__, ext) ;
- printf ("\t%s\n\n", sf_strerror (outfile)) ;
- exit (1) ;
- } ;
- if (sf_error (outfile) != SF_ERR_NO_ERROR)
- { printf ("\n\n%s %d : unable to open unseekable pipe for write type \"%s\".\n\n", __func__, __LINE__, ext) ;
- exit (1) ;
- } ;
- /*
- ** Attach the read end of the pipe to be read from.
- */
- if ((infile_piped = sf_open_fd (pipefd [0], SFM_READ, psfinfo_read, SF_TRUE)) == NULL)
- { printf ("\n\n%s %d : unable to create unseekable pipe for read type. \"%s\".\n\n", __func__, __LINE__, ext) ;
- exit (1) ;
- } ;
- if (sf_error (infile_piped) != SF_ERR_NO_ERROR)
- { printf ("\n\n%s %d : unable to open unseekable pipe for read type \"%s\".\n\n", __func__, __LINE__, ext) ;
- exit (1) ;
- } ;
- /* Fork a child process that will write directly into the pipe. */
- if ((pida = fork ()) == 0) /* child process */
- { test_writef_float_or_die (outfile, 0, data, PIPE_TEST_LEN, __LINE__) ;
- exit (0) ;
- } ;
- /* In the parent process, read from the pipe and compare what is read
- ** to what is written, if they match everything went as planned.
- */
- test_readf_float_or_die (infile_piped, 0, buffer, PIPE_TEST_LEN, __LINE__) ;
- if (memcmp (buffer, data, sizeof (buffer)) != 0)
- { printf ("\n\n%s %d : unseekable pipe test failed for file type \"%s\".\n\n", __func__, __LINE__, ext) ;
- exit (1) ;
- } ;
- /* Wait for the child process to return. */
- waitpid (pida, &status, 0) ;
- status = WEXITSTATUS (status) ;
- sf_close (outfile) ;
- sf_close (infile_piped) ;
- if (status != 0)
- { printf ("\n\n%s %d : status of child process is %d for file type %s.\n\n", __func__, __LINE__, status, ext) ;
- exit (1) ;
- } ;
- return ;
- } /* useek_pipe_rw_float */
- static void
- useek_pipe_rw_double (const char * ext, SF_INFO * psfinfo_write, SF_INFO * psfinfo_read)
- { static double buffer [PIPE_TEST_LEN] ;
- static double data [PIPE_TEST_LEN] ;
- SNDFILE *outfile ;
- SNDFILE *infile_piped ;
- int k, status = 0 ;
- int pipefd [2] ;
- pid_t pida ;
- for (k = 0 ; k < PIPE_TEST_LEN ; k++)
- data [k] = PIPE_INDEX (k) ;
- /*
- ** Create the pipe.
- */
- exit_if_true (pipe (pipefd) != 0, "\n\n%s %d : pipe failed : %s\n", __func__, __LINE__, strerror (errno)) ;
- /*
- ** Attach the write end of the pipe to be written to.
- */
- if ((outfile = sf_open_fd (pipefd [1], SFM_WRITE, psfinfo_write, SF_TRUE)) == NULL)
- { printf ("\n\n%s %d : unable to create unseekable pipe for write type \"%s\".\n", __func__, __LINE__, ext) ;
- printf ("\t%s\n\n", sf_strerror (outfile)) ;
- exit (1) ;
- } ;
- if (sf_error (outfile) != SF_ERR_NO_ERROR)
- { printf ("\n\n%s %d : unable to open unseekable pipe for write type \"%s\".\n\n", __func__, __LINE__, ext) ;
- exit (1) ;
- } ;
- /*
- ** Attach the read end of the pipe to be read from.
- */
- if ((infile_piped = sf_open_fd (pipefd [0], SFM_READ, psfinfo_read, SF_TRUE)) == NULL)
- { printf ("\n\n%s %d : unable to create unseekable pipe for read type. \"%s\".\n\n", __func__, __LINE__, ext) ;
- exit (1) ;
- } ;
- if (sf_error (infile_piped) != SF_ERR_NO_ERROR)
- { printf ("\n\n%s %d : unable to open unseekable pipe for read type \"%s\".\n\n", __func__, __LINE__, ext) ;
- exit (1) ;
- } ;
- /* Fork a child process that will write directly into the pipe. */
- if ((pida = fork ()) == 0) /* child process */
- { test_writef_double_or_die (outfile, 0, data, PIPE_TEST_LEN, __LINE__) ;
- exit (0) ;
- } ;
- /* In the parent process, read from the pipe and compare what is read
- ** to what is written, if they match everything went as planned.
- */
- test_readf_double_or_die (infile_piped, 0, buffer, PIPE_TEST_LEN, __LINE__) ;
- if (memcmp (buffer, data, sizeof (buffer)) != 0)
- { printf ("\n\n%s %d : unseekable pipe test failed for file type \"%s\".\n\n", __func__, __LINE__, ext) ;
- exit (1) ;
- } ;
- /* Wait for the child process to return. */
- waitpid (pida, &status, 0) ;
- status = WEXITSTATUS (status) ;
- sf_close (outfile) ;
- sf_close (infile_piped) ;
- if (status != 0)
- { printf ("\n\n%s %d : status of child process is %d for file type %s.\n\n", __func__, __LINE__, status, ext) ;
- exit (1) ;
- } ;
- return ;
- } /* useek_pipe_rw_double */
- static void
- useek_pipe_rw_test (int filetype, const char *ext)
- { SF_INFO sfinfo_write ;
- SF_INFO sfinfo_read ;
- print_test_name ("useek_pipe_rw_test", ext) ;
- /*
- ** Setup the INFO structures for the filetype we will be
- ** working with.
- */
- sfinfo_write.format = filetype | SF_FORMAT_PCM_16 ;
- sfinfo_write.channels = 1 ;
- sfinfo_write.samplerate = 44100 ;
- sfinfo_read.format = 0 ;
- if (filetype == SF_FORMAT_RAW)
- { sfinfo_read.format = filetype | SF_FORMAT_PCM_16 ;
- sfinfo_read.channels = 1 ;
- sfinfo_read.samplerate = 44100 ;
- } ;
- useek_pipe_rw_short (ext, &sfinfo_write, &sfinfo_read) ;
- sfinfo_read.format = sfinfo_write.format = filetype | SF_FORMAT_FLOAT ;
- if (sf_format_check (&sfinfo_read) != 0)
- useek_pipe_rw_float (ext, &sfinfo_write, &sfinfo_read) ;
- sfinfo_read.format = sfinfo_write.format = filetype | SF_FORMAT_DOUBLE ;
- if (sf_format_check (&sfinfo_read) != 0)
- useek_pipe_rw_double (ext, &sfinfo_write, &sfinfo_read) ;
- puts ("ok") ;
- return ;
- } /* useek_pipe_rw_test */
- static void
- pipe_test_others (FILETYPE* list1, FILETYPE* list2)
- { SF_FORMAT_INFO info ;
- int k, m, major_count, in_list ;
- print_test_name ("pipe_test_others", "") ;
- sf_command (NULL, SFC_GET_FORMAT_MAJOR_COUNT, &major_count, sizeof (int)) ;
- for (k = 0 ; k < major_count ; k++)
- { info.format = k ;
- sf_command (NULL, SFC_GET_FORMAT_MAJOR, &info, sizeof (info)) ;
- in_list = SF_FALSE ;
- for (m = 0 ; list1 [m].format ; m++)
- if (info.format == list1 [m].format)
- in_list = SF_TRUE ;
- for (m = 0 ; list2 [m].format ; m++)
- if (info.format == list2 [m].format)
- in_list = SF_TRUE ;
- if (in_list)
- continue ;
- printf ("%s %x\n", info.name, info.format) ;
- if (1)
- { static short data [PIPE_TEST_LEN] ;
- static char buffer [256] ;
- static const char *filename = "pipe_in.dat" ;
- SNDFILE *outfile ;
- SF_INFO sfinfo ;
- int retval ;
- sfinfo.format = info.format | SF_FORMAT_PCM_16 ;
- sfinfo.channels = 1 ;
- sfinfo.samplerate = 44100 ;
- outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
- test_writef_short_or_die (outfile, 0, data, PIPE_TEST_LEN, __LINE__) ;
- sf_close (outfile) ;
- snprintf (buffer, sizeof (buffer), "cat %s | ./stdin_test %s %d ", filename, info.extension, PIPE_TEST_LEN) ;
- if ((retval = system (buffer)) == 0)
- { retval = WEXITSTATUS (retval) ;
- printf ("\n\n Line %d : pipe test should have returned error file type \"%s\" but didn't.\n\n", __LINE__, info.name) ;
- exit (1) ;
- } ;
- unlink (filename) ;
- } ;
- } ;
- puts ("ok") ;
- return ;
- } /* pipe_test_others */
- /*==============================================================================
- */
- static int
- file_exists (const char *filename)
- { struct stat buf ;
- if (stat (filename, &buf))
- return 0 ;
- return 1 ;
- } /* file_exists */
- #endif
|