misc_test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. ** Copyright (C) 2001-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
  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. #include <sys/stat.h>
  24. #include <math.h>
  25. #if HAVE_UNISTD_H
  26. #include <unistd.h>
  27. #endif
  28. #if (HAVE_DECL_S_IRGRP == 0)
  29. #include <sf_unistd.h>
  30. #endif
  31. #if (defined (WIN32) || defined (_WIN32))
  32. #include <io.h>
  33. #include <direct.h>
  34. #endif
  35. #include <sndfile.h>
  36. #include "utils.h"
  37. #define BUFFER_LEN (1 << 10)
  38. #define LOG_BUFFER_SIZE 1024
  39. static void zero_data_test (const char *filename, int format) ;
  40. static void filesystem_full_test (int format) ;
  41. static void permission_test (const char *filename, int typemajor) ;
  42. static void wavex_amb_test (const char *filename) ;
  43. int
  44. main (int argc, char *argv [])
  45. { int do_all = 0 ;
  46. int test_count = 0 ;
  47. if (argc != 2)
  48. { printf ("Usage : %s <test>\n", argv [0]) ;
  49. printf (" Where <test> is one of the following:\n") ;
  50. printf (" wav - test WAV file peak chunk\n") ;
  51. printf (" aiff - test AIFF file PEAK chunk\n") ;
  52. printf (" all - perform all tests\n") ;
  53. exit (1) ;
  54. } ;
  55. do_all = ! strcmp (argv [1], "all") ;
  56. if (do_all || ! strcmp (argv [1], "wav"))
  57. { zero_data_test ("zerolen.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
  58. filesystem_full_test (SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
  59. permission_test ("readonly.wav", SF_FORMAT_WAV) ;
  60. wavex_amb_test ("ambisonic.wav") ;
  61. test_count++ ;
  62. } ;
  63. if (do_all || ! strcmp (argv [1], "aiff"))
  64. { zero_data_test ("zerolen.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_16) ;
  65. filesystem_full_test (SF_FORMAT_AIFF | SF_FORMAT_PCM_16) ;
  66. permission_test ("readonly.aiff", SF_FORMAT_AIFF) ;
  67. test_count++ ;
  68. } ;
  69. if (do_all || ! strcmp (argv [1], "au"))
  70. { zero_data_test ("zerolen.au", SF_FORMAT_AU | SF_FORMAT_PCM_16) ;
  71. filesystem_full_test (SF_FORMAT_AU | SF_FORMAT_PCM_16) ;
  72. permission_test ("readonly.au", SF_FORMAT_AU) ;
  73. test_count++ ;
  74. } ;
  75. if (do_all || ! strcmp (argv [1], "caf"))
  76. { zero_data_test ("zerolen.caf", SF_FORMAT_CAF | SF_FORMAT_PCM_16) ;
  77. filesystem_full_test (SF_FORMAT_CAF | SF_FORMAT_PCM_16) ;
  78. permission_test ("readonly.caf", SF_FORMAT_CAF) ;
  79. test_count++ ;
  80. } ;
  81. if (do_all || ! strcmp (argv [1], "svx"))
  82. { zero_data_test ("zerolen.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_16) ;
  83. filesystem_full_test (SF_FORMAT_SVX | SF_FORMAT_PCM_16) ;
  84. permission_test ("readonly.svx", SF_FORMAT_SVX) ;
  85. test_count++ ;
  86. } ;
  87. if (do_all || ! strcmp (argv [1], "nist"))
  88. { zero_data_test ("zerolen.nist", SF_FORMAT_NIST | SF_FORMAT_PCM_16) ;
  89. filesystem_full_test (SF_FORMAT_NIST | SF_FORMAT_PCM_16) ;
  90. permission_test ("readonly.nist", SF_FORMAT_NIST) ;
  91. test_count++ ;
  92. } ;
  93. if (do_all || ! strcmp (argv [1], "paf"))
  94. { zero_data_test ("zerolen.paf", SF_FORMAT_PAF | SF_FORMAT_PCM_16) ;
  95. filesystem_full_test (SF_FORMAT_PAF | SF_FORMAT_PCM_16) ;
  96. permission_test ("readonly.paf", SF_FORMAT_PAF) ;
  97. test_count++ ;
  98. } ;
  99. if (do_all || ! strcmp (argv [1], "ircam"))
  100. { zero_data_test ("zerolen.ircam", SF_FORMAT_IRCAM | SF_FORMAT_PCM_16) ;
  101. filesystem_full_test (SF_FORMAT_IRCAM | SF_FORMAT_PCM_16) ;
  102. permission_test ("readonly.ircam", SF_FORMAT_IRCAM) ;
  103. test_count++ ;
  104. } ;
  105. if (do_all || ! strcmp (argv [1], "voc"))
  106. { zero_data_test ("zerolen.voc", SF_FORMAT_VOC | SF_FORMAT_PCM_16) ;
  107. filesystem_full_test (SF_FORMAT_VOC | SF_FORMAT_PCM_16) ;
  108. permission_test ("readonly.voc", SF_FORMAT_VOC) ;
  109. test_count++ ;
  110. } ;
  111. if (do_all || ! strcmp (argv [1], "w64"))
  112. { zero_data_test ("zerolen.w64", SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
  113. filesystem_full_test (SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
  114. permission_test ("readonly.w64", SF_FORMAT_W64) ;
  115. test_count++ ;
  116. } ;
  117. if (do_all || ! strcmp (argv [1], "rf64"))
  118. { zero_data_test ("zerolen.rf64", SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
  119. filesystem_full_test (SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
  120. permission_test ("readonly.rf64", SF_FORMAT_W64) ;
  121. test_count++ ;
  122. } ;
  123. if (do_all || ! strcmp (argv [1], "mat4"))
  124. { zero_data_test ("zerolen.mat4", SF_FORMAT_MAT4 | SF_FORMAT_PCM_16) ;
  125. filesystem_full_test (SF_FORMAT_MAT4 | SF_FORMAT_PCM_16) ;
  126. permission_test ("readonly.mat4", SF_FORMAT_MAT4) ;
  127. test_count++ ;
  128. } ;
  129. if (do_all || ! strcmp (argv [1], "mat5"))
  130. { zero_data_test ("zerolen.mat5", SF_FORMAT_MAT5 | SF_FORMAT_PCM_16) ;
  131. filesystem_full_test (SF_FORMAT_MAT5 | SF_FORMAT_PCM_16) ;
  132. permission_test ("readonly.mat5", SF_FORMAT_MAT5) ;
  133. test_count++ ;
  134. } ;
  135. if (do_all || ! strcmp (argv [1], "pvf"))
  136. { zero_data_test ("zerolen.pvf", SF_FORMAT_PVF | SF_FORMAT_PCM_16) ;
  137. filesystem_full_test (SF_FORMAT_PVF | SF_FORMAT_PCM_16) ;
  138. permission_test ("readonly.pvf", SF_FORMAT_PVF) ;
  139. test_count++ ;
  140. } ;
  141. if (do_all || ! strcmp (argv [1], "htk"))
  142. { zero_data_test ("zerolen.htk", SF_FORMAT_HTK | SF_FORMAT_PCM_16) ;
  143. filesystem_full_test (SF_FORMAT_HTK | SF_FORMAT_PCM_16) ;
  144. permission_test ("readonly.htk", SF_FORMAT_HTK) ;
  145. test_count++ ;
  146. } ;
  147. if (do_all || ! strcmp (argv [1], "avr"))
  148. { zero_data_test ("zerolen.avr", SF_FORMAT_AVR | SF_FORMAT_PCM_16) ;
  149. filesystem_full_test (SF_FORMAT_AVR | SF_FORMAT_PCM_16) ;
  150. permission_test ("readonly.avr", SF_FORMAT_AVR) ;
  151. test_count++ ;
  152. } ;
  153. if (do_all || ! strcmp (argv [1], "sds"))
  154. { zero_data_test ("zerolen.sds", SF_FORMAT_SDS | SF_FORMAT_PCM_16) ;
  155. filesystem_full_test (SF_FORMAT_SDS | SF_FORMAT_PCM_16) ;
  156. permission_test ("readonly.sds", SF_FORMAT_SDS) ;
  157. test_count++ ;
  158. } ;
  159. if (do_all || ! strcmp (argv [1], "mpc2k"))
  160. { zero_data_test ("zerolen.mpc", SF_FORMAT_MPC2K | SF_FORMAT_PCM_16) ;
  161. filesystem_full_test (SF_FORMAT_MPC2K | SF_FORMAT_PCM_16) ;
  162. permission_test ("readonly.mpc", SF_FORMAT_MPC2K) ;
  163. test_count++ ;
  164. } ;
  165. if (do_all || ! strcmp (argv [1], "ogg"))
  166. { zero_data_test ("zerolen.oga", SF_FORMAT_OGG | SF_FORMAT_VORBIS) ;
  167. /*-filesystem_full_test (SF_FORMAT_OGG | SF_FORMAT_VORBIS) ;-*/
  168. permission_test ("readonly.oga", SF_FORMAT_OGG) ;
  169. test_count++ ;
  170. } ;
  171. if (test_count == 0)
  172. { printf ("Mono : ************************************\n") ;
  173. printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
  174. printf ("Mono : ************************************\n") ;
  175. return 1 ;
  176. } ;
  177. return 0 ;
  178. } /* main */
  179. /*============================================================================================
  180. ** Here are the test functions.
  181. */
  182. static void
  183. zero_data_test (const char *filename, int format)
  184. { SNDFILE *file ;
  185. SF_INFO sfinfo ;
  186. switch (format & SF_FORMAT_TYPEMASK)
  187. { case SF_FORMAT_OGG :
  188. if (HAVE_EXTERNAL_LIBS == 0)
  189. return ;
  190. break ;
  191. default :
  192. break ;
  193. } ;
  194. print_test_name ("zero_data_test", filename) ;
  195. sfinfo.samplerate = 44100 ;
  196. sfinfo.format = format ;
  197. sfinfo.channels = 1 ;
  198. sfinfo.frames = 0 ;
  199. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  200. sf_close (file) ;
  201. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  202. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  203. sf_close (file) ;
  204. unlink (filename) ;
  205. puts ("ok") ;
  206. } /* zero_data_test */
  207. static void
  208. filesystem_full_test (int format)
  209. { SNDFILE *file ;
  210. SF_INFO sfinfo ;
  211. struct stat buf ;
  212. const char *filename = "/dev/full", *errorstr ;
  213. #if (defined (WIN32) || defined (_WIN32))
  214. /* Can't run this test on Win32 so return. */
  215. return ;
  216. #endif
  217. /* Make sure errno is zero before doing anything else. */
  218. errno = 0 ;
  219. print_test_name ("filesystem_full_test", filename) ;
  220. if (stat (filename, &buf) != 0)
  221. { puts ("/dev/full missing") ;
  222. return ;
  223. } ;
  224. if (S_ISCHR (buf.st_mode) == 0 && S_ISBLK (buf.st_mode) == 0)
  225. { puts ("/dev/full is not a device file") ;
  226. return ;
  227. } ;
  228. sfinfo.samplerate = 44100 ;
  229. sfinfo.format = format ;
  230. sfinfo.channels = 1 ;
  231. sfinfo.frames = 0 ;
  232. if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) != NULL)
  233. { printf ("\n\nLine %d : Error, file should not have openned.\n", __LINE__ - 1) ;
  234. exit (1) ;
  235. } ;
  236. errorstr = sf_strerror (file) ;
  237. if (strstr (errorstr, " space ") == NULL || strstr (errorstr, "device") == NULL)
  238. { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__ - 1, errorstr) ;
  239. exit (1) ;
  240. } ;
  241. puts ("ok") ;
  242. } /* filesystem_full_test */
  243. static void
  244. permission_test (const char *filename, int typemajor)
  245. {
  246. #if (OS_IS_WIN32)
  247. /* Avoid compiler warnings. */
  248. filename = filename ;
  249. typemajor = typemajor ;
  250. /* Can't run this test on Win32 so return. */
  251. return ;
  252. #else
  253. FILE *textfile ;
  254. SNDFILE *file ;
  255. SF_INFO sfinfo ;
  256. const char *errorstr ;
  257. /* Make sure errno is zero before doing anything else. */
  258. errno = 0 ;
  259. if (getuid () == 0)
  260. { /* If running as root bypass this test.
  261. ** Root is allowed to open a readonly file for write.
  262. */
  263. return ;
  264. } ;
  265. print_test_name ("permission_test", filename) ;
  266. if (access (filename, F_OK) == 0)
  267. { chmod (filename, S_IWUSR) ;
  268. unlink (filename) ;
  269. } ;
  270. if ((textfile = fopen (filename, "w")) == NULL)
  271. { printf ("\n\nLine %d : not able to open text file for write.\n", __LINE__) ;
  272. exit (1) ;
  273. } ;
  274. fprintf (textfile, "This is a read only file.\n") ;
  275. fclose (textfile) ;
  276. if (chmod (filename, S_IRUSR | S_IRGRP))
  277. { printf ("\n\nLine %d : chmod failed", __LINE__) ;
  278. fflush (stdout) ;
  279. perror ("") ;
  280. exit (1) ;
  281. } ;
  282. sfinfo.samplerate = 44100 ;
  283. sfinfo.format = (typemajor | SF_FORMAT_PCM_16) ;
  284. sfinfo.channels = 1 ;
  285. sfinfo.frames = 0 ;
  286. if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) != NULL)
  287. { printf ("\n\nLine %d : Error, file should not have opened.\n", __LINE__ - 1) ;
  288. exit (1) ;
  289. } ;
  290. errorstr = sf_strerror (file) ;
  291. if (strstr (errorstr, "ermission denied") == NULL)
  292. { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__ - 1, errorstr) ;
  293. exit (1) ;
  294. } ;
  295. if (chmod (filename, S_IWUSR | S_IWGRP))
  296. { printf ("\n\nLine %d : chmod failed", __LINE__) ;
  297. fflush (stdout) ;
  298. perror ("") ;
  299. exit (1) ;
  300. } ;
  301. unlink (filename) ;
  302. puts ("ok") ;
  303. #endif
  304. } /* permission_test */
  305. static void
  306. wavex_amb_test (const char *filename)
  307. { static short buffer [800] ;
  308. SNDFILE *file ;
  309. SF_INFO sfinfo ;
  310. sf_count_t frames ;
  311. print_test_name (__func__, filename) ;
  312. sfinfo.samplerate = 44100 ;
  313. sfinfo.format = SF_FORMAT_WAVEX | SF_FORMAT_PCM_16 ;
  314. sfinfo.channels = 4 ;
  315. frames = ARRAY_LEN (buffer) / sfinfo.channels ;
  316. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  317. sf_command (file, SFC_WAVEX_SET_AMBISONIC, NULL, SF_AMBISONIC_B_FORMAT) ;
  318. test_writef_short_or_die (file, 0, buffer, frames, __LINE__) ;
  319. sf_close (file) ;
  320. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  321. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  322. exit_if_true (
  323. sf_command (file, SFC_WAVEX_GET_AMBISONIC, NULL, 0) != SF_AMBISONIC_B_FORMAT,
  324. "\n\nLine %d : Error, this file should be in Ambisonic B format.\n", __LINE__
  325. ) ;
  326. sf_close (file) ;
  327. unlink (filename) ;
  328. puts ("ok") ;
  329. } /* wavex_amb_test */