1b815c7f3Sopenharmony_ci/* 2b815c7f3Sopenharmony_ci** Copyright (C) 1999-2018 Erik de Castro Lopo <erikd@mega-nerd.com> 3b815c7f3Sopenharmony_ci** 4b815c7f3Sopenharmony_ci** This program is free software; you can redistribute it and/or modify 5b815c7f3Sopenharmony_ci** it under the terms of the GNU General Public License as published by 6b815c7f3Sopenharmony_ci** the Free Software Foundation; either version 2 of the License, or 7b815c7f3Sopenharmony_ci** (at your option) any later version. 8b815c7f3Sopenharmony_ci** 9b815c7f3Sopenharmony_ci** This program is distributed in the hope that it will be useful, 10b815c7f3Sopenharmony_ci** but WITHOUT ANY WARRANTY; without even the implied warranty of 11b815c7f3Sopenharmony_ci** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12b815c7f3Sopenharmony_ci** GNU General Public License for more details. 13b815c7f3Sopenharmony_ci** 14b815c7f3Sopenharmony_ci** You should have received a copy of the GNU General Public License 15b815c7f3Sopenharmony_ci** along with this program; if not, write to the Free Software 16b815c7f3Sopenharmony_ci** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17b815c7f3Sopenharmony_ci*/ 18b815c7f3Sopenharmony_ci 19b815c7f3Sopenharmony_ci#include "sfconfig.h" 20b815c7f3Sopenharmony_ci 21b815c7f3Sopenharmony_ci#include <stdio.h> 22b815c7f3Sopenharmony_ci#include <stdlib.h> 23b815c7f3Sopenharmony_ci#include <string.h> 24b815c7f3Sopenharmony_ci#include <errno.h> 25b815c7f3Sopenharmony_ci 26b815c7f3Sopenharmony_ci#if HAVE_UNISTD_H 27b815c7f3Sopenharmony_ci#include <unistd.h> 28b815c7f3Sopenharmony_ci#else 29b815c7f3Sopenharmony_ci#include "sf_unistd.h" 30b815c7f3Sopenharmony_ci#endif 31b815c7f3Sopenharmony_ci 32b815c7f3Sopenharmony_ci#include <sndfile.h> 33b815c7f3Sopenharmony_ci 34b815c7f3Sopenharmony_ci#include "utils.h" 35b815c7f3Sopenharmony_ci 36b815c7f3Sopenharmony_ci#define BUFFER_SIZE (1 << 15) 37b815c7f3Sopenharmony_ci#define SHORT_BUFFER (256) 38b815c7f3Sopenharmony_ci 39b815c7f3Sopenharmony_cistatic void 40b815c7f3Sopenharmony_cierror_number_test (void) 41b815c7f3Sopenharmony_ci{ const char *noerror, *errstr ; 42b815c7f3Sopenharmony_ci int k ; 43b815c7f3Sopenharmony_ci 44b815c7f3Sopenharmony_ci print_test_name ("error_number_test", "") ; 45b815c7f3Sopenharmony_ci 46b815c7f3Sopenharmony_ci noerror = sf_error_number (0) ; 47b815c7f3Sopenharmony_ci 48b815c7f3Sopenharmony_ci for (k = 1 ; k < 300 ; k++) 49b815c7f3Sopenharmony_ci { errstr = sf_error_number (k) ; 50b815c7f3Sopenharmony_ci 51b815c7f3Sopenharmony_ci /* Test for termination condition. */ 52b815c7f3Sopenharmony_ci if (errstr == noerror) 53b815c7f3Sopenharmony_ci break ; 54b815c7f3Sopenharmony_ci 55b815c7f3Sopenharmony_ci /* Test for error. */ 56b815c7f3Sopenharmony_ci if (strstr (errstr, "This is a bug in libsndfile.")) 57b815c7f3Sopenharmony_ci { printf ("\n\nError : error number %d : %s\n\n\n", k, errstr) ; 58b815c7f3Sopenharmony_ci exit (1) ; 59b815c7f3Sopenharmony_ci } ; 60b815c7f3Sopenharmony_ci } ; 61b815c7f3Sopenharmony_ci 62b815c7f3Sopenharmony_ci 63b815c7f3Sopenharmony_ci puts ("ok") ; 64b815c7f3Sopenharmony_ci return ; 65b815c7f3Sopenharmony_ci} /* error_number_test */ 66b815c7f3Sopenharmony_ci 67b815c7f3Sopenharmony_cistatic void 68b815c7f3Sopenharmony_cierror_value_test (void) 69b815c7f3Sopenharmony_ci{ static unsigned char aiff_data [0x1b0] = 70b815c7f3Sopenharmony_ci { 'F' , 'O' , 'R' , 'M' , 71b815c7f3Sopenharmony_ci 0x00, 0x00, 0x01, 0xA8, /* FORM length */ 72b815c7f3Sopenharmony_ci 73b815c7f3Sopenharmony_ci 'A' , 'I' , 'F' , 'C' , 74b815c7f3Sopenharmony_ci } ; 75b815c7f3Sopenharmony_ci 76b815c7f3Sopenharmony_ci const char *filename = "error.aiff" ; 77b815c7f3Sopenharmony_ci SNDFILE *file ; 78b815c7f3Sopenharmony_ci SF_INFO sfinfo ; 79b815c7f3Sopenharmony_ci int error_num ; 80b815c7f3Sopenharmony_ci 81b815c7f3Sopenharmony_ci print_test_name ("error_value_test", filename) ; 82b815c7f3Sopenharmony_ci 83b815c7f3Sopenharmony_ci dump_data_to_file (filename, aiff_data, sizeof (aiff_data)) ; 84b815c7f3Sopenharmony_ci 85b815c7f3Sopenharmony_ci memset (&sfinfo, 0, sizeof (sfinfo)) ; 86b815c7f3Sopenharmony_ci 87b815c7f3Sopenharmony_ci file = sf_open (filename, SFM_READ, &sfinfo) ; 88b815c7f3Sopenharmony_ci if (file != NULL) 89b815c7f3Sopenharmony_ci { printf ("\n\nLine %d : Should not have been able to open this file.\n\n", __LINE__) ; 90b815c7f3Sopenharmony_ci exit (1) ; 91b815c7f3Sopenharmony_ci } ; 92b815c7f3Sopenharmony_ci 93b815c7f3Sopenharmony_ci if ((error_num = sf_error (NULL)) <= 1 || error_num > 300) 94b815c7f3Sopenharmony_ci { printf ("\n\nLine %d : Should not have had an error number of %d.\n\n", __LINE__, error_num) ; 95b815c7f3Sopenharmony_ci exit (1) ; 96b815c7f3Sopenharmony_ci } ; 97b815c7f3Sopenharmony_ci 98b815c7f3Sopenharmony_ci remove (filename) ; 99b815c7f3Sopenharmony_ci puts ("ok") ; 100b815c7f3Sopenharmony_ci return ; 101b815c7f3Sopenharmony_ci} /* error_value_test */ 102b815c7f3Sopenharmony_ci 103b815c7f3Sopenharmony_cistatic void 104b815c7f3Sopenharmony_cino_file_test (const char * filename) 105b815c7f3Sopenharmony_ci{ SNDFILE *sndfile ; 106b815c7f3Sopenharmony_ci SF_INFO sfinfo ; 107b815c7f3Sopenharmony_ci 108b815c7f3Sopenharmony_ci print_test_name (__func__, filename) ; 109b815c7f3Sopenharmony_ci 110b815c7f3Sopenharmony_ci unlink (filename) ; 111b815c7f3Sopenharmony_ci 112b815c7f3Sopenharmony_ci memset (&sfinfo, 0, sizeof (sfinfo)) ; 113b815c7f3Sopenharmony_ci sndfile = sf_open (filename, SFM_READ, &sfinfo) ; 114b815c7f3Sopenharmony_ci 115b815c7f3Sopenharmony_ci exit_if_true (sndfile != NULL, "\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__) ; 116b815c7f3Sopenharmony_ci 117b815c7f3Sopenharmony_ci unlink (filename) ; 118b815c7f3Sopenharmony_ci puts ("ok") ; 119b815c7f3Sopenharmony_ci} /* no_file_test */ 120b815c7f3Sopenharmony_ci 121b815c7f3Sopenharmony_cistatic void 122b815c7f3Sopenharmony_cizero_length_test (const char *filename) 123b815c7f3Sopenharmony_ci{ SNDFILE *sndfile ; 124b815c7f3Sopenharmony_ci SF_INFO sfinfo ; 125b815c7f3Sopenharmony_ci FILE *file ; 126b815c7f3Sopenharmony_ci 127b815c7f3Sopenharmony_ci print_test_name (__func__, filename) ; 128b815c7f3Sopenharmony_ci 129b815c7f3Sopenharmony_ci /* Create a zero length file. */ 130b815c7f3Sopenharmony_ci file = fopen (filename, "w") ; 131b815c7f3Sopenharmony_ci exit_if_true (file == NULL, "\n\nLine %d : fopen ('%s') failed.\n", __LINE__, filename) ; 132b815c7f3Sopenharmony_ci fclose (file) ; 133b815c7f3Sopenharmony_ci 134b815c7f3Sopenharmony_ci memset (&sfinfo, 0, sizeof (sfinfo)) ; 135b815c7f3Sopenharmony_ci sndfile = sf_open (filename, SFM_READ, &sfinfo) ; 136b815c7f3Sopenharmony_ci 137b815c7f3Sopenharmony_ci exit_if_true (sndfile != NULL, "\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__) ; 138b815c7f3Sopenharmony_ci 139b815c7f3Sopenharmony_ci exit_if_true (0 && sf_error (NULL) != SF_ERR_UNRECOGNISED_FORMAT, 140b815c7f3Sopenharmony_ci "\n\nLine %3d : Error : %s\n should be : %s\n", __LINE__, 141b815c7f3Sopenharmony_ci sf_strerror (NULL), sf_error_number (SF_ERR_UNRECOGNISED_FORMAT)) ; 142b815c7f3Sopenharmony_ci 143b815c7f3Sopenharmony_ci unlink (filename) ; 144b815c7f3Sopenharmony_ci puts ("ok") ; 145b815c7f3Sopenharmony_ci} /* zero_length_test */ 146b815c7f3Sopenharmony_ci 147b815c7f3Sopenharmony_cistatic void 148b815c7f3Sopenharmony_cibad_wav_test (const char * filename) 149b815c7f3Sopenharmony_ci{ SNDFILE *sndfile ; 150b815c7f3Sopenharmony_ci SF_INFO sfinfo ; 151b815c7f3Sopenharmony_ci 152b815c7f3Sopenharmony_ci FILE *file ; 153b815c7f3Sopenharmony_ci const char data [] = "RIFF WAVEfmt " ; 154b815c7f3Sopenharmony_ci 155b815c7f3Sopenharmony_ci print_test_name (__func__, filename) ; 156b815c7f3Sopenharmony_ci 157b815c7f3Sopenharmony_ci if ((file = fopen (filename, "w")) == NULL) 158b815c7f3Sopenharmony_ci { printf ("\n\nLine %d : fopen returned NULL.\n", __LINE__) ; 159b815c7f3Sopenharmony_ci exit (1) ; 160b815c7f3Sopenharmony_ci } ; 161b815c7f3Sopenharmony_ci 162b815c7f3Sopenharmony_ci exit_if_true (fwrite (data, sizeof (data), 1, file) != 1, "\n\nLine %d : fwrite failed.\n", __LINE__) ; 163b815c7f3Sopenharmony_ci fclose (file) ; 164b815c7f3Sopenharmony_ci 165b815c7f3Sopenharmony_ci memset (&sfinfo, 0, sizeof (sfinfo)) ; 166b815c7f3Sopenharmony_ci sndfile = sf_open (filename, SFM_READ, &sfinfo) ; 167b815c7f3Sopenharmony_ci 168b815c7f3Sopenharmony_ci if (sndfile) 169b815c7f3Sopenharmony_ci { printf ("\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__) ; 170b815c7f3Sopenharmony_ci exit (1) ; 171b815c7f3Sopenharmony_ci } ; 172b815c7f3Sopenharmony_ci 173b815c7f3Sopenharmony_ci unlink (filename) ; 174b815c7f3Sopenharmony_ci puts ("ok") ; 175b815c7f3Sopenharmony_ci} /* bad_wav_test */ 176b815c7f3Sopenharmony_ci 177b815c7f3Sopenharmony_cistatic void 178b815c7f3Sopenharmony_ciwav_list_recover_test (const char * filename) 179b815c7f3Sopenharmony_ci{ SNDFILE *sndfile ; 180b815c7f3Sopenharmony_ci SF_INFO sfinfo ; 181b815c7f3Sopenharmony_ci 182b815c7f3Sopenharmony_ci FILE *file ; 183b815c7f3Sopenharmony_ci const char data [] = 184b815c7f3Sopenharmony_ci "RIFF" "J\000\000\000" 185b815c7f3Sopenharmony_ci "WAVE" "fmt " "\020\000\000\000" "\001\000\001\000D\254\000\000\210X\001\000\002\000\020\000" 186b815c7f3Sopenharmony_ci "LIST" "\014\000\000\000" "test" "\010\000\000\000" "1234" /* test is 4 bytes short inside LIST container */ 187b815c7f3Sopenharmony_ci "data" "\004\000\000\000" "abcd" ; 188b815c7f3Sopenharmony_ci 189b815c7f3Sopenharmony_ci print_test_name (__func__, filename) ; 190b815c7f3Sopenharmony_ci 191b815c7f3Sopenharmony_ci if ((file = fopen (filename, "w")) == NULL) 192b815c7f3Sopenharmony_ci { printf ("\n\nLine %d : fopen returned NULL.\n", __LINE__) ; 193b815c7f3Sopenharmony_ci exit (1) ; 194b815c7f3Sopenharmony_ci } ; 195b815c7f3Sopenharmony_ci 196b815c7f3Sopenharmony_ci exit_if_true (fwrite (data, sizeof (data) - 1, 1, file) != 1, "\n\nLine %d : fwrite failed.\n", __LINE__) ; 197b815c7f3Sopenharmony_ci fclose (file) ; 198b815c7f3Sopenharmony_ci 199b815c7f3Sopenharmony_ci memset (&sfinfo, 0, sizeof (sfinfo)) ; 200b815c7f3Sopenharmony_ci sndfile = sf_open (filename, SFM_READ, &sfinfo) ; 201b815c7f3Sopenharmony_ci 202b815c7f3Sopenharmony_ci if (!sndfile) 203b815c7f3Sopenharmony_ci { printf ("\n\nLine %d : expected recovery from bogus LIST content.\n", __LINE__) ; 204b815c7f3Sopenharmony_ci exit (1) ; 205b815c7f3Sopenharmony_ci } ; 206b815c7f3Sopenharmony_ci 207b815c7f3Sopenharmony_ci if (sfinfo.frames != 2) 208b815c7f3Sopenharmony_ci { printf ("\n\nLine %d : Should have read data chunk with 2 stereo frames, got %ld.\n\n", __LINE__, (long)sfinfo.frames) ; 209b815c7f3Sopenharmony_ci exit (1) ; 210b815c7f3Sopenharmony_ci } ; 211b815c7f3Sopenharmony_ci 212b815c7f3Sopenharmony_ci unlink (filename) ; 213b815c7f3Sopenharmony_ci puts ("ok") ; 214b815c7f3Sopenharmony_ci} /* wav_list_recover_test */ 215b815c7f3Sopenharmony_ci 216b815c7f3Sopenharmony_cistatic void 217b815c7f3Sopenharmony_cierror_close_test (void) 218b815c7f3Sopenharmony_ci{ static short buffer [SHORT_BUFFER] ; 219b815c7f3Sopenharmony_ci const char *filename = "error_close.wav" ; 220b815c7f3Sopenharmony_ci SNDFILE *sndfile ; 221b815c7f3Sopenharmony_ci SF_INFO sfinfo ; 222b815c7f3Sopenharmony_ci FILE *file ; 223b815c7f3Sopenharmony_ci 224b815c7f3Sopenharmony_ci print_test_name (__func__, filename) ; 225b815c7f3Sopenharmony_ci 226b815c7f3Sopenharmony_ci /* Open a FILE* from which we will extract a file descriptor. */ 227b815c7f3Sopenharmony_ci if ((file = fopen (filename, "w")) == NULL) 228b815c7f3Sopenharmony_ci { printf ("\n\nLine %d : fopen returned NULL.\n", __LINE__) ; 229b815c7f3Sopenharmony_ci exit (1) ; 230b815c7f3Sopenharmony_ci } ; 231b815c7f3Sopenharmony_ci 232b815c7f3Sopenharmony_ci /* Set parameters for writing the file. */ 233b815c7f3Sopenharmony_ci memset (&sfinfo, 0, sizeof (sfinfo)) ; 234b815c7f3Sopenharmony_ci sfinfo.channels = 1 ; 235b815c7f3Sopenharmony_ci sfinfo.samplerate = 44100 ; 236b815c7f3Sopenharmony_ci sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ; 237b815c7f3Sopenharmony_ci 238b815c7f3Sopenharmony_ci sndfile = sf_open_fd (fileno (file), SFM_WRITE, &sfinfo, SF_TRUE) ; 239b815c7f3Sopenharmony_ci if (sndfile == NULL) 240b815c7f3Sopenharmony_ci { printf ("\n\nLine %d : sf_open_fd failed : %s\n", __LINE__, sf_strerror (NULL)) ; 241b815c7f3Sopenharmony_ci exit (1) ; 242b815c7f3Sopenharmony_ci } ; 243b815c7f3Sopenharmony_ci 244b815c7f3Sopenharmony_ci test_write_short_or_die (sndfile, 0, buffer, ARRAY_LEN (buffer), __LINE__) ; 245b815c7f3Sopenharmony_ci 246b815c7f3Sopenharmony_ci /* Now close the fd associated with file before calling sf_close. */ 247b815c7f3Sopenharmony_ci fclose (file) ; 248b815c7f3Sopenharmony_ci 249b815c7f3Sopenharmony_ci if (sf_close (sndfile) == 0) 250b815c7f3Sopenharmony_ci { 251b815c7f3Sopenharmony_ci printf ("\n\nLine %d : sf_close should not have returned zero.\n", __LINE__) ; 252b815c7f3Sopenharmony_ci#if OS_IS_WIN32 253b815c7f3Sopenharmony_ci printf ("\nHowever, this is a known bug on windows platform so we'll ignore it.\n\n") ; 254b815c7f3Sopenharmony_ci#else 255b815c7f3Sopenharmony_ci exit (1) ; 256b815c7f3Sopenharmony_ci#endif 257b815c7f3Sopenharmony_ci } ; 258b815c7f3Sopenharmony_ci 259b815c7f3Sopenharmony_ci unlink (filename) ; 260b815c7f3Sopenharmony_ci puts ("ok") ; 261b815c7f3Sopenharmony_ci} /* error_close_test */ 262b815c7f3Sopenharmony_ci 263b815c7f3Sopenharmony_cistatic void 264b815c7f3Sopenharmony_ciunrecognised_test (void) 265b815c7f3Sopenharmony_ci{ const char *filename = "unrecognised.bin" ; 266b815c7f3Sopenharmony_ci SNDFILE *sndfile ; 267b815c7f3Sopenharmony_ci SF_INFO sfinfo ; 268b815c7f3Sopenharmony_ci FILE *file ; 269b815c7f3Sopenharmony_ci int k ; 270b815c7f3Sopenharmony_ci 271b815c7f3Sopenharmony_ci print_test_name (__func__, filename) ; 272b815c7f3Sopenharmony_ci 273b815c7f3Sopenharmony_ci file = fopen (filename, "wb") ; 274b815c7f3Sopenharmony_ci exit_if_true (file == NULL, 275b815c7f3Sopenharmony_ci "\n\nLine %d : fopen ('%s') failed : %s\n", __LINE__, filename, strerror (errno) 276b815c7f3Sopenharmony_ci ) ; 277b815c7f3Sopenharmony_ci fputs ("Unrecognised file", file) ; 278b815c7f3Sopenharmony_ci fclose (file) ; 279b815c7f3Sopenharmony_ci 280b815c7f3Sopenharmony_ci memset (&sfinfo, 0, sizeof (sfinfo)) ; 281b815c7f3Sopenharmony_ci sndfile = sf_open (filename, SFM_READ, &sfinfo) ; 282b815c7f3Sopenharmony_ci 283b815c7f3Sopenharmony_ci exit_if_true (sndfile != NULL, 284b815c7f3Sopenharmony_ci "\n\nLine %d : SNDFILE* pointer (%p) should be NULL.\n", __LINE__, (void *) sndfile 285b815c7f3Sopenharmony_ci ) ; 286b815c7f3Sopenharmony_ci 287b815c7f3Sopenharmony_ci k = sf_error (sndfile) ; 288b815c7f3Sopenharmony_ci exit_if_true (k != SF_ERR_UNRECOGNISED_FORMAT, 289b815c7f3Sopenharmony_ci "\n\nLine %d : error (%d) should have been SF_ERR_UNRECOGNISED_FORMAT (%d).\n", 290b815c7f3Sopenharmony_ci __LINE__, k, SF_ERR_UNRECOGNISED_FORMAT 291b815c7f3Sopenharmony_ci ) ; 292b815c7f3Sopenharmony_ci 293b815c7f3Sopenharmony_ci unlink (filename) ; 294b815c7f3Sopenharmony_ci puts ("ok") ; 295b815c7f3Sopenharmony_ci} /* unrecognised_test */ 296b815c7f3Sopenharmony_ci 297b815c7f3Sopenharmony_ciint 298b815c7f3Sopenharmony_cimain (void) 299b815c7f3Sopenharmony_ci{ 300b815c7f3Sopenharmony_ci error_number_test () ; 301b815c7f3Sopenharmony_ci error_value_test () ; 302b815c7f3Sopenharmony_ci 303b815c7f3Sopenharmony_ci error_close_test () ; 304b815c7f3Sopenharmony_ci 305b815c7f3Sopenharmony_ci no_file_test ("no_file.wav") ; 306b815c7f3Sopenharmony_ci zero_length_test ("zero_length.wav") ; 307b815c7f3Sopenharmony_ci bad_wav_test ("bad_wav.wav") ; 308b815c7f3Sopenharmony_ci wav_list_recover_test ("list_recover.wav") ; 309b815c7f3Sopenharmony_ci 310b815c7f3Sopenharmony_ci unrecognised_test () ; 311b815c7f3Sopenharmony_ci 312b815c7f3Sopenharmony_ci return 0 ; 313b815c7f3Sopenharmony_ci} /* main */ 314b815c7f3Sopenharmony_ci 315