2 ** Copyright (C) 2001-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
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.
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.
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.
33 #if (HAVE_DECL_S_IRGRP == 0)
34 #include <sf_unistd.h>
37 #if (defined (WIN32) || defined (_WIN32))
46 #define BUFFER_LEN (1<<10)
47 #define LOG_BUFFER_SIZE 1024
49 static void zero_data_test (const char *filename
, int format
) ;
50 static void filesystem_full_test (int format
) ;
51 static void permission_test (const char *filename
, int typemajor
) ;
52 static void wavex_amb_test (const char *filename
) ;
55 main (int argc
, char *argv
[])
60 { printf ("Usage : %s <test>\n", argv
[0]) ;
61 printf (" Where <test> is one of the following:\n") ;
62 printf (" wav - test WAV file peak chunk\n") ;
63 printf (" aiff - test AIFF file PEAK chunk\n") ;
64 printf (" all - perform all tests\n") ;
68 do_all
=!strcmp (argv
[1], "all") ;
70 if (do_all
|| ! strcmp (argv
[1], "wav"))
71 { zero_data_test ("zerolen.wav", SF_FORMAT_WAV
| SF_FORMAT_PCM_16
) ;
72 filesystem_full_test (SF_FORMAT_WAV
| SF_FORMAT_PCM_16
) ;
73 permission_test ("readonly.wav", SF_FORMAT_WAV
) ;
74 wavex_amb_test ("ambisonic.wav") ;
78 if (do_all
|| ! strcmp (argv
[1], "aiff"))
79 { zero_data_test ("zerolen.aiff", SF_FORMAT_AIFF
| SF_FORMAT_PCM_16
) ;
80 filesystem_full_test (SF_FORMAT_AIFF
| SF_FORMAT_PCM_16
) ;
81 permission_test ("readonly.aiff", SF_FORMAT_AIFF
) ;
85 if (do_all
|| ! strcmp (argv
[1], "au"))
86 { zero_data_test ("zerolen.au", SF_FORMAT_AU
| SF_FORMAT_PCM_16
) ;
87 filesystem_full_test (SF_FORMAT_AU
| SF_FORMAT_PCM_16
) ;
88 permission_test ("readonly.au", SF_FORMAT_AU
) ;
92 if (do_all
|| ! strcmp (argv
[1], "caf"))
93 { zero_data_test ("zerolen.caf", SF_FORMAT_CAF
| SF_FORMAT_PCM_16
) ;
94 filesystem_full_test (SF_FORMAT_CAF
| SF_FORMAT_PCM_16
) ;
95 permission_test ("readonly.caf", SF_FORMAT_CAF
) ;
99 if (do_all
|| ! strcmp (argv
[1], "svx"))
100 { zero_data_test ("zerolen.svx", SF_FORMAT_SVX
| SF_FORMAT_PCM_16
) ;
101 filesystem_full_test (SF_FORMAT_SVX
| SF_FORMAT_PCM_16
) ;
102 permission_test ("readonly.svx", SF_FORMAT_SVX
) ;
106 if (do_all
|| ! strcmp (argv
[1], "nist"))
107 { zero_data_test ("zerolen.nist", SF_FORMAT_NIST
| SF_FORMAT_PCM_16
) ;
108 filesystem_full_test (SF_FORMAT_NIST
| SF_FORMAT_PCM_16
) ;
109 permission_test ("readonly.nist", SF_FORMAT_NIST
) ;
113 if (do_all
|| ! strcmp (argv
[1], "paf"))
114 { zero_data_test ("zerolen.paf", SF_FORMAT_PAF
| SF_FORMAT_PCM_16
) ;
115 filesystem_full_test (SF_FORMAT_PAF
| SF_FORMAT_PCM_16
) ;
116 permission_test ("readonly.paf", SF_FORMAT_PAF
) ;
120 if (do_all
|| ! strcmp (argv
[1], "ircam"))
121 { zero_data_test ("zerolen.ircam", SF_FORMAT_IRCAM
| SF_FORMAT_PCM_16
) ;
122 filesystem_full_test (SF_FORMAT_IRCAM
| SF_FORMAT_PCM_16
) ;
123 permission_test ("readonly.ircam", SF_FORMAT_IRCAM
) ;
127 if (do_all
|| ! strcmp (argv
[1], "voc"))
128 { zero_data_test ("zerolen.voc", SF_FORMAT_VOC
| SF_FORMAT_PCM_16
) ;
129 filesystem_full_test (SF_FORMAT_VOC
| SF_FORMAT_PCM_16
) ;
130 permission_test ("readonly.voc", SF_FORMAT_VOC
) ;
134 if (do_all
|| ! strcmp (argv
[1], "w64"))
135 { zero_data_test ("zerolen.w64", SF_FORMAT_W64
| SF_FORMAT_PCM_16
) ;
136 filesystem_full_test (SF_FORMAT_W64
| SF_FORMAT_PCM_16
) ;
137 permission_test ("readonly.w64", SF_FORMAT_W64
) ;
141 if (do_all
|| ! strcmp (argv
[1], "rf64"))
142 { zero_data_test ("zerolen.rf64", SF_FORMAT_W64
| SF_FORMAT_PCM_16
) ;
143 filesystem_full_test (SF_FORMAT_W64
| SF_FORMAT_PCM_16
) ;
144 permission_test ("readonly.rf64", SF_FORMAT_W64
) ;
148 if (do_all
|| ! strcmp (argv
[1], "mat4"))
149 { zero_data_test ("zerolen.mat4", SF_FORMAT_MAT4
| SF_FORMAT_PCM_16
) ;
150 filesystem_full_test (SF_FORMAT_MAT4
| SF_FORMAT_PCM_16
) ;
151 permission_test ("readonly.mat4", SF_FORMAT_MAT4
) ;
155 if (do_all
|| ! strcmp (argv
[1], "mat5"))
156 { zero_data_test ("zerolen.mat5", SF_FORMAT_MAT5
| SF_FORMAT_PCM_16
) ;
157 filesystem_full_test (SF_FORMAT_MAT5
| SF_FORMAT_PCM_16
) ;
158 permission_test ("readonly.mat5", SF_FORMAT_MAT5
) ;
162 if (do_all
|| ! strcmp (argv
[1], "pvf"))
163 { zero_data_test ("zerolen.pvf", SF_FORMAT_PVF
| SF_FORMAT_PCM_16
) ;
164 filesystem_full_test (SF_FORMAT_PVF
| SF_FORMAT_PCM_16
) ;
165 permission_test ("readonly.pvf", SF_FORMAT_PVF
) ;
169 if (do_all
|| ! strcmp (argv
[1], "htk"))
170 { zero_data_test ("zerolen.htk", SF_FORMAT_HTK
| SF_FORMAT_PCM_16
) ;
171 filesystem_full_test (SF_FORMAT_HTK
| SF_FORMAT_PCM_16
) ;
172 permission_test ("readonly.htk", SF_FORMAT_HTK
) ;
176 if (do_all
|| ! strcmp (argv
[1], "avr"))
177 { zero_data_test ("zerolen.avr", SF_FORMAT_AVR
| SF_FORMAT_PCM_16
) ;
178 filesystem_full_test (SF_FORMAT_AVR
| SF_FORMAT_PCM_16
) ;
179 permission_test ("readonly.avr", SF_FORMAT_AVR
) ;
183 if (do_all
|| ! strcmp (argv
[1], "sds"))
184 { zero_data_test ("zerolen.sds", SF_FORMAT_SDS
| SF_FORMAT_PCM_16
) ;
185 filesystem_full_test (SF_FORMAT_SDS
| SF_FORMAT_PCM_16
) ;
186 permission_test ("readonly.sds", SF_FORMAT_SDS
) ;
190 if (do_all
|| ! strcmp (argv
[1], "mpc2k"))
191 { zero_data_test ("zerolen.mpc", SF_FORMAT_MPC2K
| SF_FORMAT_PCM_16
) ;
192 filesystem_full_test (SF_FORMAT_MPC2K
| SF_FORMAT_PCM_16
) ;
193 permission_test ("readonly.mpc", SF_FORMAT_MPC2K
) ;
197 if (do_all
|| ! strcmp (argv
[1], "ogg"))
198 { zero_data_test ("zerolen.oga", SF_FORMAT_OGG
| SF_FORMAT_VORBIS
) ;
199 /*-filesystem_full_test (SF_FORMAT_OGG | SF_FORMAT_VORBIS) ;-*/
200 permission_test ("readonly.oga", SF_FORMAT_OGG
) ;
205 { printf ("Mono : ************************************\n") ;
206 printf ("Mono : * No '%s' test defined.\n", argv
[1]) ;
207 printf ("Mono : ************************************\n") ;
215 /*============================================================================================
216 ** Here are the test functions.
220 zero_data_test (const char *filename
, int format
)
224 switch (format
& SF_FORMAT_TYPEMASK
)
225 { case SF_FORMAT_OGG
:
226 if (HAVE_EXTERNAL_LIBS
== 0)
233 print_test_name ("zero_data_test", filename
) ;
235 sfinfo
.samplerate
= 44100 ;
236 sfinfo
.format
= format
;
237 sfinfo
.channels
= 1 ;
240 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
244 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
246 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
252 } /* zero_data_test */
255 filesystem_full_test (int format
)
260 const char *filename
= "/dev/full", *errorstr
;
262 #if (defined (WIN32) || defined (_WIN32))
263 /* Can't run this test on Win32 so return. */
267 /* Make sure errno is zero before doing anything else. */
270 print_test_name ("filesystem_full_test", filename
) ;
272 if (stat (filename
, &buf
) != 0)
273 { puts ("/dev/full missing") ;
277 if (S_ISCHR (buf
.st_mode
) == 0 && S_ISBLK (buf
.st_mode
) == 0)
278 { puts ("/dev/full is not a device file") ;
282 sfinfo
.samplerate
= 44100 ;
283 sfinfo
.format
= format
;
284 sfinfo
.channels
= 1 ;
287 if ((file
= sf_open (filename
, SFM_WRITE
, &sfinfo
)) != NULL
)
288 { printf ("\n\nLine %d : Error, file should not have openned.\n", __LINE__
- 1) ;
292 errorstr
= sf_strerror (file
) ;
294 if (strstr (errorstr
, " space ") == NULL
|| strstr (errorstr
, "device") == NULL
)
295 { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__
- 1, errorstr
) ;
300 } /* filesystem_full_test */
303 permission_test (const char *filename
, int typemajor
)
306 /* Avoid compiler warnings. */
307 filename
= filename
;
308 typemajor
= typemajor
;
310 /* Can't run this test on Win32 so return. */
317 const char *errorstr
;
319 /* Make sure errno is zero before doing anything else. */
323 { /* If running as root bypass this test.
324 ** Root is allowed to open a readonly file for write.
329 print_test_name ("permission_test", filename
) ;
331 if (access (filename
, F_OK
) == 0)
332 { chmod (filename
, S_IWUSR
) ;
336 if ((textfile
= fopen (filename
, "w")) == NULL
)
337 { printf ("\n\nLine %d : not able to open text file for write.\n", __LINE__
) ;
341 fprintf (textfile
, "This is a read only file.\n") ;
344 if (chmod (filename
, S_IRUSR
| S_IRGRP
))
345 { printf ("\n\nLine %d : chmod failed", __LINE__
) ;
351 sfinfo
.samplerate
= 44100 ;
352 sfinfo
.format
= (typemajor
| SF_FORMAT_PCM_16
) ;
353 sfinfo
.channels
= 1 ;
356 if ((file
= sf_open (filename
, SFM_WRITE
, &sfinfo
)) != NULL
)
357 { printf ("\n\nLine %d : Error, file should not have opened.\n", __LINE__
- 1) ;
361 errorstr
= sf_strerror (file
) ;
363 if (strstr (errorstr
, "ermission denied") == NULL
)
364 { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__
- 1, errorstr
) ;
368 if (chmod (filename
, S_IWUSR
| S_IWGRP
))
369 { printf ("\n\nLine %d : chmod failed", __LINE__
) ;
380 } /* permission_test */
383 wavex_amb_test (const char *filename
)
384 { static short buffer
[800] ;
389 print_test_name (__func__
, filename
) ;
391 sfinfo
.samplerate
= 44100 ;
392 sfinfo
.format
= SF_FORMAT_WAVEX
| SF_FORMAT_PCM_16
;
393 sfinfo
.channels
= 4 ;
395 frames
= ARRAY_LEN (buffer
) / sfinfo
.channels
;
397 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
398 sf_command (file
, SFC_WAVEX_SET_AMBISONIC
, NULL
, SF_AMBISONIC_B_FORMAT
) ;
399 test_writef_short_or_die (file
, 0, buffer
, frames
, __LINE__
) ;
402 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
404 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
407 sf_command (file
, SFC_WAVEX_GET_AMBISONIC
, NULL
, 0) != SF_AMBISONIC_B_FORMAT
,
408 "\n\nLine %d : Error, this file should be in Ambisonic B format.\n", __LINE__
415 } /* wavex_amb_test */