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