annotate src/libsndfile-1.0.25/tests/win32_test.c @ 83:ae30d91d2ffe

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