1 /*
2 ** Copyright (C) 1999-2018 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
26 #if HAVE_UNISTD_H
27 #include <unistd.h>
28 #else
29 #include "sf_unistd.h"
30 #endif
31
32 #if OS_IS_WIN32
33 #include <windows.h>
34 #endif
35
36 #include <sndfile.h>
37
38 #include "utils.h"
39
40 #define BUFFER_SIZE (1 << 15)
41 #define SHORT_BUFFER (256)
42
43 static void
error_number_test(void)44 error_number_test (void)
45 { const char *noerror, *errstr ;
46 int k ;
47
48 print_test_name ("error_number_test", "") ;
49
50 noerror = sf_error_number (0) ;
51
52 for (k = 1 ; k < 300 ; k++)
53 { errstr = sf_error_number (k) ;
54
55 /* Test for termination condition. */
56 if (errstr == noerror)
57 break ;
58
59 /* Test for error. */
60 if (strstr (errstr, "This is a bug in libsndfile."))
61 { printf ("\n\nError : error number %d : %s\n\n\n", k, errstr) ;
62 exit (1) ;
63 } ;
64 } ;
65
66
67 puts ("ok") ;
68 return ;
69 } /* error_number_test */
70
71 static void
error_value_test(void)72 error_value_test (void)
73 { static unsigned char aiff_data [0x1b0] =
74 { 'F' , 'O' , 'R' , 'M' ,
75 0x00, 0x00, 0x01, 0xA8, /* FORM length */
76
77 'A' , 'I' , 'F' , 'C' ,
78 } ;
79
80 const char *filename = "error.aiff" ;
81 SNDFILE *file ;
82 SF_INFO sfinfo ;
83 int error_num ;
84
85 print_test_name ("error_value_test", filename) ;
86
87 dump_data_to_file (filename, aiff_data, sizeof (aiff_data)) ;
88
89 memset (&sfinfo, 0, sizeof (sfinfo)) ;
90
91 file = sf_open (filename, SFM_READ, &sfinfo) ;
92 if (file != NULL)
93 { printf ("\n\nLine %d : Should not have been able to open this file.\n\n", __LINE__) ;
94 exit (1) ;
95 } ;
96
97 if ((error_num = sf_error (NULL)) <= 1 || error_num > 300)
98 { printf ("\n\nLine %d : Should not have had an error number of %d.\n\n", __LINE__, error_num) ;
99 exit (1) ;
100 } ;
101
102 remove (filename) ;
103 puts ("ok") ;
104 return ;
105 } /* error_value_test */
106
107 static void
no_file_test(const char * filename)108 no_file_test (const char * filename)
109 { SNDFILE *sndfile ;
110 SF_INFO sfinfo ;
111
112 print_test_name (__func__, filename) ;
113
114 unlink (filename) ;
115
116 memset (&sfinfo, 0, sizeof (sfinfo)) ;
117 sndfile = sf_open (filename, SFM_READ, &sfinfo) ;
118
119 exit_if_true (sndfile != NULL, "\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__) ;
120
121 unlink (filename) ;
122 puts ("ok") ;
123 } /* no_file_test */
124
125 static void
zero_length_test(const char * filename)126 zero_length_test (const char *filename)
127 { SNDFILE *sndfile ;
128 SF_INFO sfinfo ;
129 FILE *file ;
130
131 print_test_name (__func__, filename) ;
132
133 /* Create a zero length file. */
134 file = fopen (filename, "w") ;
135 exit_if_true (file == NULL, "\n\nLine %d : fopen ('%s') failed.\n", __LINE__, filename) ;
136 fclose (file) ;
137
138 memset (&sfinfo, 0, sizeof (sfinfo)) ;
139 sndfile = sf_open (filename, SFM_READ, &sfinfo) ;
140
141 exit_if_true (sndfile != NULL, "\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__) ;
142
143 exit_if_true (0 && sf_error (NULL) != SF_ERR_UNRECOGNISED_FORMAT,
144 "\n\nLine %3d : Error : %s\n should be : %s\n", __LINE__,
145 sf_strerror (NULL), sf_error_number (SF_ERR_UNRECOGNISED_FORMAT)) ;
146
147 unlink (filename) ;
148 puts ("ok") ;
149 } /* zero_length_test */
150
151 static void
bad_wav_test(const char * filename)152 bad_wav_test (const char * filename)
153 { SNDFILE *sndfile ;
154 SF_INFO sfinfo ;
155
156 FILE *file ;
157 const char data [] = "RIFF WAVEfmt " ;
158
159 print_test_name (__func__, filename) ;
160
161 if ((file = fopen (filename, "w")) == NULL)
162 { printf ("\n\nLine %d : fopen returned NULL.\n", __LINE__) ;
163 exit (1) ;
164 } ;
165
166 exit_if_true (fwrite (data, sizeof (data), 1, file) != 1, "\n\nLine %d : fwrite failed.\n", __LINE__) ;
167 fclose (file) ;
168
169 memset (&sfinfo, 0, sizeof (sfinfo)) ;
170 sndfile = sf_open (filename, SFM_READ, &sfinfo) ;
171
172 if (sndfile)
173 { printf ("\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__) ;
174 exit (1) ;
175 } ;
176
177 unlink (filename) ;
178 puts ("ok") ;
179 } /* bad_wav_test */
180
181 static void
error_close_test(void)182 error_close_test (void)
183 { static short buffer [SHORT_BUFFER] ;
184 const char *filename = "error_close.wav" ;
185 SNDFILE *sndfile ;
186 SF_INFO sfinfo ;
187 FILE *file ;
188
189 print_test_name (__func__, filename) ;
190
191 /* Open a FILE* from which we will extract a file descriptor. */
192 if ((file = fopen (filename, "w")) == NULL)
193 { printf ("\n\nLine %d : fopen returned NULL.\n", __LINE__) ;
194 exit (1) ;
195 } ;
196
197 /* Set parameters for writing the file. */
198 memset (&sfinfo, 0, sizeof (sfinfo)) ;
199 sfinfo.channels = 1 ;
200 sfinfo.samplerate = 44100 ;
201 sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
202
203 sndfile = sf_open_fd (fileno (file), SFM_WRITE, &sfinfo, SF_TRUE) ;
204 if (sndfile == NULL)
205 { printf ("\n\nLine %d : sf_open_fd failed : %s\n", __LINE__, sf_strerror (NULL)) ;
206 exit (1) ;
207 } ;
208
209 test_write_short_or_die (sndfile, 0, buffer, ARRAY_LEN (buffer), __LINE__) ;
210
211 /* Now close the fd associated with file before calling sf_close. */
212 fclose (file) ;
213
214 if (sf_close (sndfile) == 0)
215 {
216 #if OS_IS_WIN32
217 OSVERSIONINFOEX osvi ;
218
219 memset (&osvi, 0, sizeof (OSVERSIONINFOEX)) ;
220 osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX) ;
221
222 if (GetVersionEx ((OSVERSIONINFO *) &osvi))
223 { printf ("\n\nLine %d : sf_close should not have returned zero.\n", __LINE__) ;
224 printf ("\nHowever, this is a known bug in version %d.%d of windows so we'll ignore it.\n\n",
225 (int) osvi.dwMajorVersion, (int) osvi.dwMinorVersion) ;
226 } ;
227 #else
228 printf ("\n\nLine %d : sf_close should not have returned zero.\n", __LINE__) ;
229 exit (1) ;
230 #endif
231 } ;
232
233 unlink (filename) ;
234 puts ("ok") ;
235 } /* error_close_test */
236
237 static void
unrecognised_test(void)238 unrecognised_test (void)
239 { const char *filename = "unrecognised.bin" ;
240 SNDFILE *sndfile ;
241 SF_INFO sfinfo ;
242 FILE *file ;
243 int k ;
244
245 print_test_name (__func__, filename) ;
246
247 file = fopen (filename, "wb") ;
248 exit_if_true (file == NULL,
249 "\n\nLine %d : fopen ('%s') failed : %s\n", __LINE__, filename, strerror (errno)
250 ) ;
251 fputs ("Unrecognised file", file) ;
252 fclose (file) ;
253
254 memset (&sfinfo, 0, sizeof (sfinfo)) ;
255 sndfile = sf_open (filename, SFM_READ, &sfinfo) ;
256
257 exit_if_true (sndfile != NULL,
258 "\n\nLine %d : SNDFILE* pointer (%p) should ne NULL.\n", __LINE__, sndfile
259 ) ;
260
261 k = sf_error (sndfile) ;
262 exit_if_true (k != SF_ERR_UNRECOGNISED_FORMAT,
263 "\n\nLine %d : error (%d) should have been SF_ERR_UNRECOGNISED_FORMAT (%d).\n",
264 __LINE__, k, SF_ERR_UNRECOGNISED_FORMAT
265 ) ;
266
267 unlink (filename) ;
268 puts ("ok") ;
269 } /* unrecognised_test */
270
271 int
main(void)272 main (void)
273 {
274 error_number_test () ;
275 error_value_test () ;
276
277 error_close_test () ;
278
279 no_file_test ("no_file.wav") ;
280 zero_length_test ("zero_length.wav") ;
281 bad_wav_test ("bad_wav.wav") ;
282
283 unrecognised_test () ;
284
285 return 0 ;
286 } /* main */
287
288