1 /*
2 ** Copyright (C) 2001-2017 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
19 #include "sfconfig.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <inttypes.h>
26
27 #include <sys/stat.h>
28 #include <math.h>
29
30 #if HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33
34 #if (HAVE_DECL_S_IRGRP == 0)
35 #include <sf_unistd.h>
36 #endif
37
38 #if (defined (WIN32) || defined (_WIN32))
39 #include <io.h>
40 #include <direct.h>
41 #endif
42
43 #include <sndfile.h>
44
45 #include "utils.h"
46
47 #define BUFFER_LEN (1 << 10)
48 #define LOG_BUFFER_SIZE 1024
49
50 static void zero_data_test (const char *filename, int format) ;
51 static void filesystem_full_test (int format) ;
52 static void permission_test (const char *filename, int typemajor) ;
53 static void wavex_amb_test (const char *filename) ;
54 static void rf64_downgrade_test (const char *filename) ;
55 static void rf64_long_file_downgrade_test (const char *filename) ;
56
57 int
main(int argc,char * argv[])58 main (int argc, char *argv [])
59 { int do_all = 0 ;
60 int test_count = 0 ;
61
62 if (argc != 2)
63 { printf ("Usage : %s <test>\n", argv [0]) ;
64 printf (" Where <test> is one of the following:\n") ;
65 printf (" wav - test WAV file peak chunk\n") ;
66 printf (" aiff - test AIFF file PEAK chunk\n") ;
67 printf (" all - perform all tests\n") ;
68 exit (1) ;
69 } ;
70
71 do_all = ! strcmp (argv [1], "all") ;
72
73 if (do_all || ! strcmp (argv [1], "wav"))
74 { zero_data_test ("zerolen.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
75 filesystem_full_test (SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
76 permission_test ("readonly.wav", SF_FORMAT_WAV) ;
77 wavex_amb_test ("ambisonic.wav") ;
78 test_count++ ;
79 } ;
80
81 if (do_all || ! strcmp (argv [1], "aiff"))
82 { zero_data_test ("zerolen.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_16) ;
83 filesystem_full_test (SF_FORMAT_AIFF | SF_FORMAT_PCM_16) ;
84 permission_test ("readonly.aiff", SF_FORMAT_AIFF) ;
85 test_count++ ;
86 } ;
87
88 if (do_all || ! strcmp (argv [1], "au"))
89 { zero_data_test ("zerolen.au", SF_FORMAT_AU | SF_FORMAT_PCM_16) ;
90 filesystem_full_test (SF_FORMAT_AU | SF_FORMAT_PCM_16) ;
91 permission_test ("readonly.au", SF_FORMAT_AU) ;
92 test_count++ ;
93 } ;
94
95 if (do_all || ! strcmp (argv [1], "caf"))
96 { zero_data_test ("zerolen.caf", SF_FORMAT_CAF | SF_FORMAT_PCM_16) ;
97 filesystem_full_test (SF_FORMAT_CAF | SF_FORMAT_PCM_16) ;
98 permission_test ("readonly.caf", SF_FORMAT_CAF) ;
99 test_count++ ;
100 } ;
101
102 if (do_all || ! strcmp (argv [1], "svx"))
103 { zero_data_test ("zerolen.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_16) ;
104 filesystem_full_test (SF_FORMAT_SVX | SF_FORMAT_PCM_16) ;
105 permission_test ("readonly.svx", SF_FORMAT_SVX) ;
106 test_count++ ;
107 } ;
108
109 if (do_all || ! strcmp (argv [1], "nist"))
110 { zero_data_test ("zerolen.nist", SF_FORMAT_NIST | SF_FORMAT_PCM_16) ;
111 filesystem_full_test (SF_FORMAT_NIST | SF_FORMAT_PCM_16) ;
112 permission_test ("readonly.nist", SF_FORMAT_NIST) ;
113 test_count++ ;
114 } ;
115
116 if (do_all || ! strcmp (argv [1], "paf"))
117 { zero_data_test ("zerolen.paf", SF_FORMAT_PAF | SF_FORMAT_PCM_16) ;
118 filesystem_full_test (SF_FORMAT_PAF | SF_FORMAT_PCM_16) ;
119 permission_test ("readonly.paf", SF_FORMAT_PAF) ;
120 test_count++ ;
121 } ;
122
123 if (do_all || ! strcmp (argv [1], "ircam"))
124 { zero_data_test ("zerolen.ircam", SF_FORMAT_IRCAM | SF_FORMAT_PCM_16) ;
125 filesystem_full_test (SF_FORMAT_IRCAM | SF_FORMAT_PCM_16) ;
126 permission_test ("readonly.ircam", SF_FORMAT_IRCAM) ;
127 test_count++ ;
128 } ;
129
130 if (do_all || ! strcmp (argv [1], "voc"))
131 { zero_data_test ("zerolen.voc", SF_FORMAT_VOC | SF_FORMAT_PCM_16) ;
132 filesystem_full_test (SF_FORMAT_VOC | SF_FORMAT_PCM_16) ;
133 permission_test ("readonly.voc", SF_FORMAT_VOC) ;
134 test_count++ ;
135 } ;
136
137 if (do_all || ! strcmp (argv [1], "w64"))
138 { zero_data_test ("zerolen.w64", SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
139 filesystem_full_test (SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
140 permission_test ("readonly.w64", SF_FORMAT_W64) ;
141 test_count++ ;
142 } ;
143
144 if (do_all || ! strcmp (argv [1], "rf64"))
145 { zero_data_test ("zerolen.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ;
146 filesystem_full_test (SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ;
147 permission_test ("readonly.rf64", SF_FORMAT_RF64) ;
148 rf64_downgrade_test ("downgrade.wav") ;
149 /* Disable this by default, because it needs to write 4 gigabytes of data. */
150 if (SF_FALSE)
151 rf64_long_file_downgrade_test ("no-downgrade.rf64") ;
152 test_count++ ;
153 } ;
154
155 if (do_all || ! strcmp (argv [1], "mat4"))
156 { zero_data_test ("zerolen.mat4", SF_FORMAT_MAT4 | SF_FORMAT_PCM_16) ;
157 filesystem_full_test (SF_FORMAT_MAT4 | SF_FORMAT_PCM_16) ;
158 permission_test ("readonly.mat4", SF_FORMAT_MAT4) ;
159 test_count++ ;
160 } ;
161
162 if (do_all || ! strcmp (argv [1], "mat5"))
163 { zero_data_test ("zerolen.mat5", SF_FORMAT_MAT5 | SF_FORMAT_PCM_16) ;
164 filesystem_full_test (SF_FORMAT_MAT5 | SF_FORMAT_PCM_16) ;
165 permission_test ("readonly.mat5", SF_FORMAT_MAT5) ;
166 test_count++ ;
167 } ;
168
169 if (do_all || ! strcmp (argv [1], "pvf"))
170 { zero_data_test ("zerolen.pvf", SF_FORMAT_PVF | SF_FORMAT_PCM_16) ;
171 filesystem_full_test (SF_FORMAT_PVF | SF_FORMAT_PCM_16) ;
172 permission_test ("readonly.pvf", SF_FORMAT_PVF) ;
173 test_count++ ;
174 } ;
175
176 if (do_all || ! strcmp (argv [1], "htk"))
177 { zero_data_test ("zerolen.htk", SF_FORMAT_HTK | SF_FORMAT_PCM_16) ;
178 filesystem_full_test (SF_FORMAT_HTK | SF_FORMAT_PCM_16) ;
179 permission_test ("readonly.htk", SF_FORMAT_HTK) ;
180 test_count++ ;
181 } ;
182
183 if (do_all || ! strcmp (argv [1], "avr"))
184 { zero_data_test ("zerolen.avr", SF_FORMAT_AVR | SF_FORMAT_PCM_16) ;
185 filesystem_full_test (SF_FORMAT_AVR | SF_FORMAT_PCM_16) ;
186 permission_test ("readonly.avr", SF_FORMAT_AVR) ;
187 test_count++ ;
188 } ;
189
190 if (do_all || ! strcmp (argv [1], "sds"))
191 { zero_data_test ("zerolen.sds", SF_FORMAT_SDS | SF_FORMAT_PCM_16) ;
192 filesystem_full_test (SF_FORMAT_SDS | SF_FORMAT_PCM_16) ;
193 permission_test ("readonly.sds", SF_FORMAT_SDS) ;
194 test_count++ ;
195 } ;
196
197 if (do_all || ! strcmp (argv [1], "mpc2k"))
198 { zero_data_test ("zerolen.mpc", SF_FORMAT_MPC2K | SF_FORMAT_PCM_16) ;
199 filesystem_full_test (SF_FORMAT_MPC2K | SF_FORMAT_PCM_16) ;
200 permission_test ("readonly.mpc", SF_FORMAT_MPC2K) ;
201 test_count++ ;
202 } ;
203
204 if (do_all || ! strcmp (argv [1], "ogg"))
205 { zero_data_test ("zerolen.oga", SF_FORMAT_OGG | SF_FORMAT_VORBIS) ;
206 /*-filesystem_full_test (SF_FORMAT_OGG | SF_FORMAT_VORBIS) ;-*/
207 permission_test ("readonly.oga", SF_FORMAT_OGG) ;
208 test_count++ ;
209 } ;
210
211 if (test_count == 0)
212 { printf ("Mono : ************************************\n") ;
213 printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
214 printf ("Mono : ************************************\n") ;
215 return 1 ;
216 } ;
217
218 return 0 ;
219 } /* main */
220
221
222 /*============================================================================================
223 ** Here are the test functions.
224 */
225
226 static void
zero_data_test(const char * filename,int format)227 zero_data_test (const char *filename, int format)
228 { SNDFILE *file ;
229 SF_INFO sfinfo ;
230
231 switch (format & SF_FORMAT_TYPEMASK)
232 { case SF_FORMAT_OGG :
233 if (HAVE_EXTERNAL_XIPH_LIBS == 0)
234 return ;
235 break ;
236 default :
237 break ;
238 } ;
239
240 print_test_name ("zero_data_test", filename) ;
241
242 sfinfo.samplerate = 44100 ;
243 sfinfo.format = format ;
244 sfinfo.channels = 1 ;
245 sfinfo.frames = 0 ;
246
247 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
248
249 sf_close (file) ;
250
251 memset (&sfinfo, 0, sizeof (sfinfo)) ;
252
253 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
254
255 sf_close (file) ;
256
257 unlink (filename) ;
258 puts ("ok") ;
259 } /* zero_data_test */
260
261 static void
filesystem_full_test(int format)262 filesystem_full_test (int format)
263 {
264 #if (defined (WIN32) || defined (_WIN32))
265 (void) format ;
266 /* Can't run this test on Win32 so return. */
267 return ;
268 #else
269 SNDFILE *file ;
270 SF_INFO sfinfo ;
271 struct stat buf ;
272
273 const char *filename = "/dev/full", *errorstr ;
274
275 /* Make sure errno is zero before doing anything else. */
276 errno = 0 ;
277
278 print_test_name ("filesystem_full_test", filename) ;
279
280 if (stat (filename, &buf) != 0)
281 { puts ("/dev/full missing") ;
282 return ;
283 } ;
284
285 if (S_ISCHR (buf.st_mode) == 0 && S_ISBLK (buf.st_mode) == 0)
286 { puts ("/dev/full is not a device file") ;
287 return ;
288 } ;
289
290 sfinfo.samplerate = 44100 ;
291 sfinfo.format = format ;
292 sfinfo.channels = 1 ;
293 sfinfo.frames = 0 ;
294
295 if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) != NULL)
296 { printf ("\n\nLine %d : Error, file should not have openned.\n", __LINE__ - 1) ;
297 exit (1) ;
298 } ;
299
300 errorstr = sf_strerror (file) ;
301
302 if (strstr (errorstr, " space ") == NULL || strstr (errorstr, "device") == NULL)
303 { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__ - 1, errorstr) ;
304 exit (1) ;
305 } ;
306
307 puts ("ok") ;
308 #endif
309 } /* filesystem_full_test */
310
311 static void
permission_test(const char * filename,int typemajor)312 permission_test (const char *filename, int typemajor)
313 {
314 #if (OS_IS_WIN32)
315 /* Avoid compiler warnings. */
316 (void) filename ;
317 (void) typemajor ;
318
319 /* Can't run this test on Win32 so return. */
320 return ;
321 #else
322
323 FILE *textfile ;
324 SNDFILE *file ;
325 SF_INFO sfinfo ;
326 const char *errorstr ;
327
328 /* Make sure errno is zero before doing anything else. */
329 errno = 0 ;
330
331 if (getuid () == 0)
332 { /* If running as root bypass this test.
333 ** Root is allowed to open a readonly file for write.
334 */
335 return ;
336 } ;
337
338 print_test_name ("permission_test", filename) ;
339
340 if (access (filename, F_OK) == 0)
341 { chmod (filename, S_IWUSR) ;
342 unlink (filename) ;
343 } ;
344
345 if ((textfile = fopen (filename, "w")) == NULL)
346 { printf ("\n\nLine %d : not able to open text file for write.\n", __LINE__) ;
347 exit (1) ;
348 } ;
349
350 fprintf (textfile, "This is a read only file.\n") ;
351 fclose (textfile) ;
352
353 if (chmod (filename, S_IRUSR | S_IRGRP))
354 { printf ("\n\nLine %d : chmod failed", __LINE__) ;
355 fflush (stdout) ;
356 perror ("") ;
357 exit (1) ;
358 } ;
359
360 sfinfo.samplerate = 44100 ;
361 sfinfo.format = (typemajor | SF_FORMAT_PCM_16) ;
362 sfinfo.channels = 1 ;
363 sfinfo.frames = 0 ;
364
365 if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) != NULL)
366 { printf ("\n\nLine %d : Error, file should not have opened.\n", __LINE__ - 1) ;
367 exit (1) ;
368 } ;
369
370 errorstr = sf_strerror (file) ;
371
372 if (strstr (errorstr, "ermission denied") == NULL)
373 { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__ - 1, errorstr) ;
374 exit (1) ;
375 } ;
376
377 if (chmod (filename, S_IWUSR | S_IWGRP))
378 { printf ("\n\nLine %d : chmod failed", __LINE__) ;
379 fflush (stdout) ;
380 perror ("") ;
381 exit (1) ;
382 } ;
383
384 unlink (filename) ;
385
386 puts ("ok") ;
387
388 #endif
389 } /* permission_test */
390
391 static void
wavex_amb_test(const char * filename)392 wavex_amb_test (const char *filename)
393 { static short buffer [800] ;
394 SNDFILE *file ;
395 SF_INFO sfinfo ;
396 sf_count_t frames ;
397
398 print_test_name (__func__, filename) ;
399
400 sfinfo.samplerate = 44100 ;
401 sfinfo.format = SF_FORMAT_WAVEX | SF_FORMAT_PCM_16 ;
402 sfinfo.channels = 4 ;
403
404 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
405
406 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
407 sf_command (file, SFC_WAVEX_SET_AMBISONIC, NULL, SF_AMBISONIC_B_FORMAT) ;
408 test_writef_short_or_die (file, 0, buffer, frames, __LINE__) ;
409 sf_close (file) ;
410
411 memset (&sfinfo, 0, sizeof (sfinfo)) ;
412
413 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
414
415 exit_if_true (
416 sf_command (file, SFC_WAVEX_GET_AMBISONIC, NULL, 0) != SF_AMBISONIC_B_FORMAT,
417 "\n\nLine %d : Error, this file should be in Ambisonic B format.\n", __LINE__
418 ) ;
419
420 sf_close (file) ;
421
422 unlink (filename) ;
423 puts ("ok") ;
424 } /* wavex_amb_test */
425
426 static void
rf64_downgrade_test(const char * filename)427 rf64_downgrade_test (const char *filename)
428 { static short output [BUFFER_LEN] ;
429 static short input [BUFFER_LEN] ;
430
431 SNDFILE *file ;
432 SF_INFO sfinfo ;
433 unsigned k ;
434
435 print_test_name (__func__, filename) ;
436
437 sf_info_clear (&sfinfo) ;
438
439 sfinfo.samplerate = 44100 ;
440 sfinfo.frames = ARRAY_LEN (output) ;
441 sfinfo.channels = 1 ;
442 sfinfo.format = SF_FORMAT_RF64 | SF_FORMAT_PCM_16 ;
443
444 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
445
446 exit_if_true (sf_command (file, SFC_RF64_AUTO_DOWNGRADE, NULL, SF_FALSE) != SF_FALSE, "\n\nLine %d: sf_command failed.\n", __LINE__) ;
447 exit_if_true (sf_command (file, SFC_RF64_AUTO_DOWNGRADE, NULL, SF_TRUE) != SF_TRUE, "\n\nLine %d: sf_command failed.\n", __LINE__) ;
448
449 test_write_short_or_die (file, 0, output, ARRAY_LEN (output), __LINE__) ;
450
451 exit_if_true (sf_command (file, SFC_RF64_AUTO_DOWNGRADE, NULL, SF_FALSE) != SF_TRUE, "\n\nLine %d: sf_command failed.\n", __LINE__) ;
452 exit_if_true (sf_command (file, SFC_RF64_AUTO_DOWNGRADE, NULL, SF_TRUE) != SF_TRUE, "\n\nLine %d: sf_command failed.\n", __LINE__) ;
453
454 sf_close (file) ;
455
456 memset (input, 0, sizeof (input)) ;
457 sf_info_clear (&sfinfo) ;
458
459 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
460
461 exit_if_true (sfinfo.format != (SF_FORMAT_WAVEX | SF_FORMAT_PCM_16), "\n\nLine %d: RF64 to WAV downgrade failed.\n", __LINE__) ;
462 exit_if_true (sfinfo.frames != ARRAY_LEN (output), "\n\nLine %d: Incorrect number of frames in file (too short). (%d should be %d)\n", __LINE__, (int) sfinfo.frames, (int) ARRAY_LEN (output)) ;
463 exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
464
465 check_log_buffer_or_die (file, __LINE__) ;
466
467 test_read_short_or_die (file, 0, input, ARRAY_LEN (input), __LINE__) ;
468
469 sf_close (file) ;
470
471 for (k = 0 ; k < ARRAY_LEN (input) ; k++)
472 exit_if_true (input [k] != output [k],
473 "\n\nLine: %d: Error on input %d, expected %d, got %d\n", __LINE__, k, output [k], input [k]) ;
474
475 puts ("ok") ;
476 unlink (filename) ;
477
478 return ;
479 } /* rf64_downgrade_test */
480
481 static void
rf64_long_file_downgrade_test(const char * filename)482 rf64_long_file_downgrade_test (const char *filename)
483 { static int output [BUFFER_LEN] ;
484 static int input [1] = { 0 } ;
485
486 SNDFILE *file ;
487 SF_INFO sfinfo ;
488 sf_count_t output_frames = 0 ;
489
490 print_test_name (__func__, filename) ;
491
492 sf_info_clear (&sfinfo) ;
493
494 memset (output, 0, sizeof (output)) ;
495 output [0] = 0x1020304 ;
496
497 sfinfo.samplerate = 44100 ;
498 sfinfo.frames = ARRAY_LEN (output) ;
499 sfinfo.channels = 1 ;
500 sfinfo.format = SF_FORMAT_RF64 | SF_FORMAT_PCM_32 ;
501
502 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
503
504 exit_if_true (sf_command (file, SFC_RF64_AUTO_DOWNGRADE, NULL, SF_TRUE) != SF_TRUE, "\n\nLine %d: sf_command failed.\n", __LINE__) ;
505
506 while (output_frames * sizeof (output [0]) < 0x100000000)
507 { test_write_int_or_die (file, 0, output, ARRAY_LEN (output), __LINE__) ;
508 output_frames += ARRAY_LEN (output) ;
509 } ;
510
511 sf_close (file) ;
512
513 sf_info_clear (&sfinfo) ;
514
515 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
516
517 exit_if_true (sfinfo.format != (SF_FORMAT_RF64 | SF_FORMAT_PCM_32), "\n\nLine %d: RF64 to WAV downgrade should have failed.\n", __LINE__) ;
518 exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
519 exit_if_true (sfinfo.frames != output_frames, "\n\nLine %d: Incorrect number of frames in file (%d should be %d).\n", __LINE__, (int) sfinfo.frames, (int) output_frames) ;
520
521 /* Check that the first sample read is the same as the first written. */
522 test_read_int_or_die (file, 0, input, ARRAY_LEN (input), __LINE__) ;
523 exit_if_true (input [0] != output [0], "\n\nLine %d: Bad first sample (0x%08x).\n", __LINE__, input [0]) ;
524
525 check_log_buffer_or_die (file, __LINE__) ;
526
527 sf_close (file) ;
528
529 puts ("ok") ;
530 unlink (filename) ;
531
532 return ;
533 } /* rf64_long_file_downgrade_test */
534