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