annotate src/libsndfile-1.0.25/tests/win32_test.c @ 85:545efbb81310

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