annotate src/libsndfile-1.0.27/tests/win32_test.c @ 84:08ae793730bd

Add null config files
author Chris Cannam
date Mon, 02 Mar 2020 14:03:47 +0000
parents 1df64224f5ac
children
rev   line source
Chris@40 1 /*
Chris@40 2 ** Copyright (C) 2001-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
Chris@40 3 **
Chris@40 4 ** This program is free software; you can redistribute it and/or modify
Chris@40 5 ** it under the terms of the GNU General Public License as published by
Chris@40 6 ** the Free Software Foundation; either version 2 of the License, or
Chris@40 7 ** (at your option) any later version.
Chris@40 8 **
Chris@40 9 ** This program is distributed in the hope that it will be useful,
Chris@40 10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@40 11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@40 12 ** GNU General Public License for more details.
Chris@40 13 **
Chris@40 14 ** You should have received a copy of the GNU General Public License
Chris@40 15 ** along with this program; if not, write to the Free Software
Chris@40 16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Chris@40 17 */
Chris@40 18
Chris@40 19 #include "sfconfig.h"
Chris@40 20 #include "sndfile.h"
Chris@40 21
Chris@40 22 #include <stdio.h>
Chris@40 23 #include <stdlib.h>
Chris@40 24 #include <assert.h>
Chris@40 25
Chris@40 26 #if HAVE_UNISTD_H
Chris@40 27 #include <unistd.h>
Chris@40 28 #endif
Chris@40 29
Chris@40 30 #if (HAVE_DECL_S_IRGRP == 0)
Chris@40 31 #include <sf_unistd.h>
Chris@40 32 #endif
Chris@40 33
Chris@40 34 #include <string.h>
Chris@40 35 #include <fcntl.h>
Chris@40 36 #include <errno.h>
Chris@40 37 #include <sys/types.h>
Chris@40 38 #include <sys/stat.h>
Chris@40 39
Chris@40 40 #define SIGNED_SIZEOF(x) ((int) sizeof (x))
Chris@40 41
Chris@40 42 /* EMX is OS/2. */
Chris@40 43 #if defined (__CYGWIN__) || defined (__EMX__)
Chris@40 44
Chris@40 45 #define LSEEK lseek
Chris@40 46 #define FSTAT fstat
Chris@40 47
Chris@40 48 typedef struct stat STATBUF ;
Chris@40 49 typedef off_t INT64 ;
Chris@40 50
Chris@40 51 static char dir_cmd [] = "ls -l" ;
Chris@40 52
Chris@40 53 #elif (defined (WIN32) || defined (_WIN32))
Chris@40 54
Chris@40 55 #define LSEEK _lseeki64
Chris@40 56 #define FSTAT _fstati64
Chris@40 57
Chris@40 58 typedef struct _stati64 STATBUF ;
Chris@40 59 typedef __int64 INT64 ;
Chris@40 60
Chris@40 61 static char dir_cmd [] = "dir" ;
Chris@40 62
Chris@40 63 #else
Chris@40 64
Chris@40 65 #define LSEEK lseek
Chris@40 66 #define FSTAT fstat
Chris@40 67
Chris@40 68 typedef struct stat STATBUF ;
Chris@40 69 typedef sf_count_t INT64 ;
Chris@40 70
Chris@40 71 #define O_BINARY 0
Chris@40 72 static char dir_cmd [] = "ls -l" ;
Chris@40 73
Chris@40 74 #endif
Chris@40 75
Chris@40 76 static void show_fstat_error (void) ;
Chris@40 77 static void show_lseek_error (void) ;
Chris@40 78 static void show_stat_fstat_error (void) ;
Chris@40 79 static void write_to_closed_file (void) ;
Chris@40 80
Chris@40 81 int
Chris@40 82 main (void)
Chris@40 83 {
Chris@40 84 puts ("\n\n\n\n"
Chris@40 85 "This program shows up errors in the Win32 implementation of\n"
Chris@40 86 "a couple of POSIX API functions on some versions of windoze.\n"
Chris@40 87 "It can also be compiled on Linux (which works correctly) and\n"
Chris@40 88 "other OSes just to provide a sanity check.\n"
Chris@40 89 ) ;
Chris@40 90
Chris@40 91 show_fstat_error () ;
Chris@40 92 show_lseek_error () ;
Chris@40 93 show_stat_fstat_error () ;
Chris@40 94 write_to_closed_file () ;
Chris@40 95
Chris@40 96 puts ("\n\n") ;
Chris@40 97
Chris@40 98 return 0 ;
Chris@40 99 } /* main */
Chris@40 100
Chris@40 101 static void
Chris@40 102 show_fstat_error (void)
Chris@40 103 { static const char *filename = "fstat.dat" ;
Chris@40 104 static char data [256] ;
Chris@40 105
Chris@40 106 STATBUF statbuf ;
Chris@40 107 int fd, mode, flags ;
Chris@40 108
Chris@40 109 if (sizeof (statbuf.st_size) != sizeof (INT64))
Chris@40 110 { printf ("\n\nLine %d: Error, sizeof (statbuf.st_size) != 8.\n\n", __LINE__) ;
Chris@40 111 return ;
Chris@40 112 } ;
Chris@40 113
Chris@40 114 puts ("\n64 bit fstat() test.\n--------------------") ;
Chris@40 115
Chris@40 116 printf ("0) Create a file, write %d bytes and close it.\n", SIGNED_SIZEOF (data)) ;
Chris@40 117 mode = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY ;
Chris@40 118 flags = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ;
Chris@40 119 if ((fd = open (filename, mode, flags)) < 0)
Chris@40 120 { printf ("\n\nLine %d: open() failed : %s\n\n", __LINE__, strerror (errno)) ;
Chris@40 121 return ;
Chris@40 122 } ;
Chris@40 123 assert (write (fd, data, sizeof (data)) > 0) ;
Chris@40 124 close (fd) ;
Chris@40 125
Chris@40 126 printf ("1) Re-open file in read/write mode and write another %d bytes at the end.\n", SIGNED_SIZEOF (data)) ;
Chris@40 127 mode = O_RDWR | O_BINARY ;
Chris@40 128 flags = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ;
Chris@40 129 if ((fd = open (filename, mode, flags)) < 0)
Chris@40 130 { printf ("\n\nLine %d: open() failed : %s\n\n", __LINE__, strerror (errno)) ;
Chris@40 131 return ;
Chris@40 132 } ;
Chris@40 133 LSEEK (fd, 0, SEEK_END) ;
Chris@40 134 assert (write (fd, data, sizeof (data)) > 0) ;
Chris@40 135
Chris@40 136 printf ("2) Now use system (\"%s %s\") to show the file length.\n\n", dir_cmd, filename) ;
Chris@40 137
Chris@40 138 /* Would use snprintf, but thats not really available on windows. */
Chris@40 139 memset (data, 0, sizeof (data)) ;
Chris@40 140 strncpy (data, dir_cmd, sizeof (data) - 1) ;
Chris@40 141 strncat (data, " ", sizeof (data) - 1 - strlen (data)) ;
Chris@40 142 strncat (data, filename, sizeof (data) - 1 - strlen (data)) ;
Chris@40 143
Chris@40 144 assert (system (data) >= 0) ;
Chris@40 145 puts ("") ;
Chris@40 146
Chris@40 147 printf ("3) Now use fstat() to get the file length.\n") ;
Chris@40 148 if (FSTAT (fd, &statbuf) != 0)
Chris@40 149 { printf ("\n\nLine %d: fstat() returned error : %s\n", __LINE__, strerror (errno)) ;
Chris@40 150 return ;
Chris@40 151 } ;
Chris@40 152
Chris@40 153 printf ("4) According to fstat(), the file length is %ld, ", (long) statbuf.st_size) ;
Chris@40 154
Chris@40 155 close (fd) ;
Chris@40 156
Chris@40 157 if (statbuf.st_size != 2 * sizeof (data))
Chris@40 158 printf ("but thats just plain ***WRONG***.\n\n") ;
Chris@40 159 else
Chris@40 160 { printf ("which is correct.\n\n") ;
Chris@40 161 unlink (filename) ;
Chris@40 162 } ;
Chris@40 163
Chris@40 164 } /* show_fstat_error */
Chris@40 165
Chris@40 166 static void
Chris@40 167 show_lseek_error (void)
Chris@40 168 { static const char *filename = "fstat.dat" ;
Chris@40 169 static char data [256] ;
Chris@40 170
Chris@40 171 INT64 retval ;
Chris@40 172 int fd, mode, flags ;
Chris@40 173
Chris@40 174 puts ("\n64 bit lseek() test.\n--------------------") ;
Chris@40 175
Chris@40 176 printf ("0) Create a file, write %d bytes and close it.\n", SIGNED_SIZEOF (data)) ;
Chris@40 177 mode = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY ;
Chris@40 178 flags = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ;
Chris@40 179 if ((fd = open (filename, mode, flags)) < 0)
Chris@40 180 { printf ("\n\nLine %d: open() failed : %s\n\n", __LINE__, strerror (errno)) ;
Chris@40 181 return ;
Chris@40 182 } ;
Chris@40 183 assert (write (fd, data, sizeof (data)) > 0) ;
Chris@40 184 close (fd) ;
Chris@40 185
Chris@40 186 printf ("1) Re-open file in read/write mode and write another %d bytes at the end.\n", SIGNED_SIZEOF (data)) ;
Chris@40 187 mode = O_RDWR | O_BINARY ;
Chris@40 188 flags = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ;
Chris@40 189 if ((fd = open (filename, mode, flags)) < 0)
Chris@40 190 { printf ("\n\nLine %d: open() failed : %s\n\n", __LINE__, strerror (errno)) ;
Chris@40 191 return ;
Chris@40 192 } ;
Chris@40 193
Chris@40 194 LSEEK (fd, 0, SEEK_END) ;
Chris@40 195 assert (write (fd, data, sizeof (data)) > 0) ;
Chris@40 196
Chris@40 197 printf ("2) Now use system (\"%s %s\") to show the file length.\n\n", dir_cmd, filename) ;
Chris@40 198
Chris@40 199 /* Would use snprintf, but thats not really available on windows. */
Chris@40 200 memset (data, 0, sizeof (data)) ;
Chris@40 201 strncpy (data, dir_cmd, sizeof (data) - 1) ;
Chris@40 202 strncat (data, " ", sizeof (data) - 1 - strlen (data)) ;
Chris@40 203 strncat (data, filename, sizeof (data) - 1 - strlen (data)) ;
Chris@40 204
Chris@40 205 assert (system (data) >= 0) ;
Chris@40 206 puts ("") ;
Chris@40 207
Chris@40 208 printf ("3) Now use lseek() to go to the end of the file.\n") ;
Chris@40 209 retval = LSEEK (fd, 0, SEEK_END) ;
Chris@40 210
Chris@40 211 printf ("4) We are now at position %ld, ", (long) retval) ;
Chris@40 212
Chris@40 213 close (fd) ;
Chris@40 214
Chris@40 215 if (retval != 2 * sizeof (data))
Chris@40 216 printf ("but thats just plain ***WRONG***.\n\n") ;
Chris@40 217 else
Chris@40 218 { printf ("which is correct.\n\n") ;
Chris@40 219 unlink (filename) ;
Chris@40 220 } ;
Chris@40 221
Chris@40 222 } /* show_lseek_error */
Chris@40 223
Chris@40 224 static void
Chris@40 225 show_stat_fstat_error (void)
Chris@40 226 { static const char *filename = "stat_fstat.dat" ;
Chris@40 227 static char data [256] ;
Chris@40 228
Chris@40 229 int fd, mode, flags ;
Chris@40 230 int stat_size, fstat_size ;
Chris@40 231 struct stat buf ;
Chris@40 232
Chris@40 233 /* Known to fail on WinXP. */
Chris@40 234 puts ("\nstat/fstat test.\n----------------") ;
Chris@40 235
Chris@40 236 printf ("0) Create a file and write %d bytes.\n", SIGNED_SIZEOF (data)) ;
Chris@40 237
Chris@40 238 mode = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY ;
Chris@40 239 flags = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ;
Chris@40 240 if ((fd = open (filename, mode, flags)) < 0)
Chris@40 241 { printf ("\n\nLine %d: open() failed : %s\n\n", __LINE__, strerror (errno)) ;
Chris@40 242 return ;
Chris@40 243 } ;
Chris@40 244
Chris@40 245 assert (write (fd, data, sizeof (data)) > 0) ;
Chris@40 246
Chris@40 247 printf ("1) Now call stat and fstat on the file and retreive the file lengths.\n") ;
Chris@40 248
Chris@40 249 if (stat (filename, &buf) != 0)
Chris@40 250 { printf ("\n\nLine %d: stat() failed : %s\n\n", __LINE__, strerror (errno)) ;
Chris@40 251 goto error_exit ;
Chris@40 252 } ;
Chris@40 253 stat_size = buf.st_size ;
Chris@40 254
Chris@40 255 if (fstat (fd, &buf) != 0)
Chris@40 256 { printf ("\n\nLine %d: fstat() failed : %s\n\n", __LINE__, strerror (errno)) ;
Chris@40 257 goto error_exit ;
Chris@40 258 } ;
Chris@40 259 fstat_size = buf.st_size ;
Chris@40 260
Chris@40 261 printf ("2) Size returned by stat and fstat is %d and %d, ", stat_size, fstat_size) ;
Chris@40 262
Chris@40 263
Chris@40 264 if (stat_size == 0 || stat_size != fstat_size)
Chris@40 265 printf ("but thats just plain ***WRONG***.\n\n") ;
Chris@40 266 else
Chris@40 267 printf ("which is correct.\n\n") ;
Chris@40 268
Chris@40 269 error_exit :
Chris@40 270
Chris@40 271 close (fd) ;
Chris@40 272 unlink (filename) ;
Chris@40 273
Chris@40 274 return ;
Chris@40 275 } /* show_stat_fstat_error */
Chris@40 276
Chris@40 277
Chris@40 278 static void
Chris@40 279 write_to_closed_file (void)
Chris@40 280 { const char * filename = "closed_write_test.txt" ;
Chris@40 281 struct stat buf ;
Chris@40 282 FILE * file ;
Chris@40 283 int fd ;
Chris@40 284
Chris@40 285 puts ("\nWrite to closed file test.\n--------------------------") ;
Chris@40 286
Chris@40 287 printf ("0) First we open file for write using fopen().\n") ;
Chris@40 288 if ((file = fopen (filename, "w")) == NULL)
Chris@40 289 { printf ("\n\nLine %d: fopen() failed : %s\n\n", __LINE__, strerror (errno)) ;
Chris@40 290 return ;
Chris@40 291 } ;
Chris@40 292
Chris@40 293 printf ("1) Now we grab the file descriptor fileno().\n") ;
Chris@40 294 fd = fileno (file) ;
Chris@40 295
Chris@40 296 printf ("2) Write some text via the file descriptor.\n") ;
Chris@40 297 assert (write (fd, "a\n", 2) > 0) ;
Chris@40 298
Chris@40 299 printf ("3) Now we close the file using fclose().\n") ;
Chris@40 300 fclose (file) ;
Chris@40 301
Chris@40 302 stat (filename, &buf) ;
Chris@40 303 printf (" File size is %d bytes.\n", (int) buf.st_size) ;
Chris@40 304
Chris@40 305 printf ("4) Now write more data to the file descriptor which should fail.\n") ;
Chris@40 306 if (write (fd, "b\n", 2) < 0)
Chris@40 307 printf ("5) Good, write returned an error code as it should have.\n") ;
Chris@40 308 else
Chris@40 309 { printf ("5) Attempting to write to a closed file should have failed but didn't! *** WRONG ***\n") ;
Chris@40 310
Chris@40 311 stat (filename, &buf) ;
Chris@40 312 printf (" File size is %d bytes.\n", (int) buf.st_size) ;
Chris@40 313 } ;
Chris@40 314
Chris@40 315 unlink (filename) ;
Chris@40 316
Chris@40 317 return ;
Chris@40 318 } /* write_to_closed_file */