error_test.c 7.8 KB

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