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