annotate src/libsndfile-1.0.27/tests/win32_test.c @ 168:ceec0dd9ec9c

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