2 ** Copyright (C) 2001-2003 Erik de Castro Lopo <erikd@zip.com.au>
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.
27 #if (defined (WIN32) || defined (_WIN32))
32 #define _IFREG _S_IFREG
34 #define S_ISREG(m) ((m) & _S_IFREG)
36 #define S_IRWXU 0000700 /* rwx, owner */
37 #define S_IRUSR 0000400 /* read permission, owner */
38 #define S_IWUSR 0000200 /* write permission, owner */
39 #define S_IXUSR 0000100 /* execute/search permission, owner */
40 #define S_IRWXG 0000070 /* rwx, group */
41 #define S_IRGRP 0000040 /* read permission, group */
42 #define S_IWGRP 0000020 /* write permission, grougroup */
43 #define S_IXGRP 0000010 /* execute/search permission, group */
44 #define S_IRWXO 0000007 /* rwx, other */
45 #define S_IROTH 0000004 /* read permission, other */
46 #define S_IWOTH 0000002 /* write permission, other */
47 #define S_IXOTH 0000001 /* execute/search permission, other */
55 #define BUFFER_LEN (1<<10)
56 #define LOG_BUFFER_SIZE 1024
58 static void update_header_test (char *filename
, int typemajor
) ;
59 static void zero_data_test (char *filename
, int typemajor
) ;
60 static void filesystem_full_test (int typemajor
) ;
61 static void permission_test (char *filename
, int typemajor
) ;
63 /* Force the start of this buffer to be double aligned. Sparc-solaris will
66 static short data_out
[BUFFER_LEN
] ;
67 static short data_in
[BUFFER_LEN
] ;
70 main (int argc
, char *argv
[])
75 { printf ("Usage : %s <test>\n", argv
[0]) ;
76 printf (" Where <test> is one of the following:\n") ;
77 printf (" wav - test WAV file peak chunk\n") ;
78 printf (" aiff - test AIFF file PEAK chunk\n") ;
79 printf (" all - perform all tests\n") ;
83 do_all
=!strcmp (argv
[1], "all");
85 if (do_all
|| ! strcmp (argv
[1], "wav"))
86 { update_header_test ("header.wav", SF_FORMAT_WAV
) ;
87 zero_data_test ("zerolen.wav", SF_FORMAT_WAV
) ;
88 filesystem_full_test (SF_FORMAT_WAV
) ;
89 permission_test ("readonly.wav", SF_FORMAT_WAV
) ;
93 if (do_all
|| ! strcmp (argv
[1], "aiff"))
94 { update_header_test ("header.aiff", SF_FORMAT_AIFF
) ;
95 zero_data_test ("zerolen.aiff", SF_FORMAT_AIFF
) ;
96 filesystem_full_test (SF_FORMAT_AIFF
) ;
97 permission_test ("readonly.aiff", SF_FORMAT_AIFF
) ;
101 if (do_all
|| ! strcmp (argv
[1], "au"))
102 { update_header_test ("header.au", SF_FORMAT_AU
) ;
103 zero_data_test ("zerolen.au", SF_FORMAT_AU
) ;
104 filesystem_full_test (SF_FORMAT_AU
) ;
105 permission_test ("readonly.au", SF_FORMAT_AU
) ;
109 if (do_all
|| ! strcmp (argv
[1], "svx"))
110 { update_header_test ("header.svx", SF_FORMAT_SVX
) ;
111 zero_data_test ("zerolen.svx", SF_FORMAT_SVX
) ;
112 filesystem_full_test (SF_FORMAT_SVX
) ;
113 permission_test ("readonly.svx", SF_FORMAT_SVX
) ;
117 if (do_all
|| ! strcmp (argv
[1], "nist"))
118 { update_header_test ("header.nist", SF_FORMAT_NIST
) ;
119 zero_data_test ("zerolen.nist", SF_FORMAT_NIST
) ;
120 filesystem_full_test (SF_FORMAT_NIST
) ;
121 permission_test ("readonly.nist", SF_FORMAT_NIST
) ;
125 if (do_all
|| ! strcmp (argv
[1], "paf"))
126 { update_header_test ("header.paf", SF_FORMAT_PAF
) ;
127 zero_data_test ("zerolen.paf", SF_FORMAT_PAF
) ;
128 filesystem_full_test (SF_FORMAT_PAF
) ;
129 permission_test ("readonly.paf", SF_FORMAT_PAF
) ;
133 if (do_all
|| ! strcmp (argv
[1], "ircam"))
134 { update_header_test ("header.ircam", SF_FORMAT_IRCAM
) ;
135 zero_data_test ("zerolen.ircam", SF_FORMAT_IRCAM
) ;
136 filesystem_full_test (SF_FORMAT_IRCAM
) ;
137 permission_test ("readonly.ircam", SF_FORMAT_IRCAM
) ;
141 if (do_all
|| ! strcmp (argv
[1], "voc"))
142 { update_header_test ("header.voc", SF_FORMAT_VOC
) ;
143 zero_data_test ("zerolen.voc", SF_FORMAT_VOC
) ;
144 filesystem_full_test (SF_FORMAT_VOC
) ;
145 permission_test ("readonly.voc", SF_FORMAT_VOC
) ;
149 if (do_all
|| ! strcmp (argv
[1], "w64"))
150 { update_header_test ("header.w64", SF_FORMAT_W64
) ;
151 zero_data_test ("zerolen.w64", SF_FORMAT_W64
) ;
152 filesystem_full_test (SF_FORMAT_W64
) ;
153 permission_test ("readonly.w64", SF_FORMAT_W64
) ;
157 if (do_all
|| ! strcmp (argv
[1], "mat4"))
158 { update_header_test ("header.mat4", SF_FORMAT_MAT4
) ;
159 zero_data_test ("zerolen.mat4", SF_FORMAT_MAT4
) ;
160 filesystem_full_test (SF_FORMAT_MAT4
) ;
161 permission_test ("readonly.mat4", SF_FORMAT_MAT4
) ;
165 if (do_all
|| ! strcmp (argv
[1], "mat5"))
166 { update_header_test ("header.mat5", SF_FORMAT_MAT5
) ;
167 zero_data_test ("zerolen.mat5", SF_FORMAT_MAT5
) ;
168 filesystem_full_test (SF_FORMAT_MAT5
) ;
169 permission_test ("readonly.mat5", SF_FORMAT_MAT5
) ;
173 if (do_all
|| ! strcmp (argv
[1], "pvf"))
174 { update_header_test ("header.pvf", SF_FORMAT_PVF
) ;
175 zero_data_test ("zerolen.pvf", SF_FORMAT_PVF
) ;
176 filesystem_full_test (SF_FORMAT_PVF
) ;
177 permission_test ("readonly.pvf", SF_FORMAT_PVF
) ;
181 if (do_all
|| ! strcmp (argv
[1], "htk"))
182 { update_header_test ("header.htk", SF_FORMAT_HTK
) ;
183 zero_data_test ("zerolen.htk", SF_FORMAT_HTK
) ;
184 filesystem_full_test (SF_FORMAT_HTK
) ;
185 permission_test ("readonly.htk", SF_FORMAT_HTK
) ;
190 { printf ("Mono : ************************************\n") ;
191 printf ("Mono : * No '%s' test defined.\n", argv
[1]) ;
192 printf ("Mono : ************************************\n") ;
200 /*============================================================================================
201 ** Here are the test functions.
205 update_header_test (char *filename
, int typemajor
)
206 { SNDFILE
*outfile
, *infile
;
210 print_test_name ("update_header_test", filename
) ;
212 sfinfo
.samplerate
= 44100 ;
213 sfinfo
.format
= (typemajor
| SF_FORMAT_PCM_16
) ;
214 sfinfo
.channels
= 1 ;
217 frames
= BUFFER_LEN
/ sfinfo
.channels
;
219 outfile
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, __LINE__
) ;
221 for (k
= 0 ; k
< BUFFER_LEN
; k
++)
222 data_out
[k
] = k
+ 1 ;
223 test_write_short_or_die (outfile
, 0, data_out
, BUFFER_LEN
, __LINE__
) ;
225 if (typemajor
!= SF_FORMAT_HTK
)
226 { /* The HTK header is not correct when the file is firstw written. */
227 infile
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, __LINE__
) ;
231 sf_command (outfile
, SFC_UPDATE_HEADER_NOW
, NULL
, 0) ;
233 /* Open file and check log buffer for an error. If header update failed
234 ** the the log buffer will contain errors.
236 infile
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, __LINE__
) ;
237 check_log_buffer_or_die (infile
) ;
239 if (abs (BUFFER_LEN
- sfinfo
.frames
) > 1)
240 { printf ("\n\nLine %d : Incorrect sample count (%ld should be %d)\n", __LINE__
, SF_COUNT_TO_LONG (sfinfo
.frames
), BUFFER_LEN
) ;
241 dump_log_buffer (infile
) ;
245 test_read_short_or_die (infile
, 0, data_in
, BUFFER_LEN
, __LINE__
) ;
246 for (k
= 0 ; k
< BUFFER_LEN
; k
++)
247 if (data_out
[k
] != k
+ 1)
248 printf ("Error : line %d\n", __LINE__
) ;
252 /* Set auto update on. */
253 sf_command (outfile
, SFC_SET_UPDATE_HEADER_AUTO
, NULL
, SF_TRUE
) ;
255 /* Write more data_out. */
256 for (k
= 0 ; k
< BUFFER_LEN
; k
++)
257 data_out
[k
] = k
+ 2 ;
258 test_write_short_or_die (outfile
, 0, data_out
, BUFFER_LEN
, __LINE__
) ;
260 /* Open file again and make sure no errors in log buffer. */
261 infile
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, __LINE__
) ;
262 check_log_buffer_or_die (infile
) ;
264 if (abs (2 * BUFFER_LEN
- sfinfo
.frames
) > 1)
265 { printf ("\n\nLine %d : Incorrect sample count (%ld should be %d)\n", __LINE__
, SF_COUNT_TO_LONG (sfinfo
.frames
), 2 * BUFFER_LEN
) ;
266 dump_log_buffer (infile
) ;
276 } /* update_header_test */
279 zero_data_test (char *filename
, int typemajor
)
284 print_test_name ("zero_data_test", filename
) ;
286 sfinfo
.samplerate
= 44100 ;
287 sfinfo
.format
= (typemajor
| SF_FORMAT_PCM_16
) ;
288 sfinfo
.channels
= 1 ;
291 frames
= BUFFER_LEN
/ sfinfo
.channels
;
293 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, __LINE__
) ;
297 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
299 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, __LINE__
) ;
305 } /* zero_data_test */
308 filesystem_full_test (int typemajor
)
313 const char *filename
= "/dev/full", *errorstr
;
316 #if (defined (WIN32) || defined (_WIN32))
317 /* Can't run this test on Win32 so return. */
321 print_test_name ("filesystem_full_test", filename
) ;
323 if (stat (filename
, &buf
) != 0)
324 { puts ("/dev/full missing") ;
328 if (S_ISCHR (buf
.st_mode
) == 0 && S_ISBLK (buf
.st_mode
) == 0)
329 { puts ("/dev/full is not a device file") ;
333 sfinfo
.samplerate
= 44100 ;
334 sfinfo
.format
= (typemajor
| SF_FORMAT_PCM_16
) ;
335 sfinfo
.channels
= 1 ;
338 frames
= BUFFER_LEN
/ sfinfo
.channels
;
340 if ((file
= sf_open (filename
, SFM_WRITE
, &sfinfo
)) != NULL
)
341 { printf ("\n\nLine %d : Error, file should not have openned.\n", __LINE__
- 1) ;
345 errorstr
= sf_strerror (file
) ;
347 if (strstr (errorstr
, " space ") == NULL
|| strstr (errorstr
, "device") == NULL
)
348 { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__
- 1, errorstr
) ;
353 } /* filesystem_full_test */
356 permission_test (char *filename
, int typemajor
)
360 const char *errorstr
;
364 #if (defined (WIN32) || defined (_WIN32))
365 /* Can't run this test on Win32 so return. */
370 { /* If running as root bypass this test.
371 ** Root is allowed to open a readonly file for write.
376 print_test_name ("permission_test", filename
) ;
378 if ((textfile
= fopen (filename
, "w")) == NULL
)
379 { printf ("\n\nLine %d : not able to open text file for write.\n", __LINE__
) ;
383 fprintf (textfile
, "This is a read only file.\n") ;
386 if (chmod (filename
, S_IRUSR
| S_IRGRP
))
387 { printf ("\n\nLine %d : chmod failed", __LINE__
) ;
393 sfinfo
.samplerate
= 44100 ;
394 sfinfo
.format
= (typemajor
| SF_FORMAT_PCM_16
) ;
395 sfinfo
.channels
= 1 ;
398 frames
= BUFFER_LEN
/ sfinfo
.channels
;
400 if ((file
= sf_open (filename
, SFM_WRITE
, &sfinfo
)) != NULL
)
401 { printf ("\n\nLine %d : Error, file should not have opened.\n", __LINE__
- 1) ;
405 errorstr
= sf_strerror (file
) ;
407 if (strstr (errorstr
, "ould not open file") == NULL
)
408 { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__
- 1, errorstr
) ;
412 if (chmod (filename
, S_IWUSR
| S_IWGRP
))
413 { printf ("\n\nLine %d : chmod failed", __LINE__
) ;
422 } /* permission_test */