diff options
author | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2011-10-03 18:13:11 +0200 |
---|---|---|
committer | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2011-10-03 18:13:11 +0200 |
commit | c75a4dfa301eb5aafe519a92e48598d1e94470b0 (patch) | |
tree | 93de9eb424fe0722975450c22e046970871b3633 /gl/tests | |
parent | 1f86b82c7a33d37f85ba3bb372bd45d249bed2a3 (diff) | |
download | gnutls-c75a4dfa301eb5aafe519a92e48598d1e94470b0.tar.gz |
Added new gnulib.
Diffstat (limited to 'gl/tests')
-rw-r--r-- | gl/tests/Makefile.am | 79 | ||||
-rw-r--r-- | gl/tests/close.c | 69 | ||||
-rw-r--r-- | gl/tests/dup2.c | 84 | ||||
-rw-r--r-- | gl/tests/fdopen.c | 69 | ||||
-rw-r--r-- | gl/tests/msvc-inval.c | 69 | ||||
-rw-r--r-- | gl/tests/msvc-inval.h | 167 | ||||
-rw-r--r-- | gl/tests/test-close.c | 44 | ||||
-rw-r--r-- | gl/tests/test-dup2.c | 2 | ||||
-rw-r--r-- | gl/tests/test-fgetc.c | 12 | ||||
-rw-r--r-- | gl/tests/test-fputc.c | 90 | ||||
-rw-r--r-- | gl/tests/test-fread.c | 99 | ||||
-rw-r--r-- | gl/tests/test-fwrite.c | 93 |
12 files changed, 586 insertions, 291 deletions
diff --git a/gl/tests/Makefile.am b/gl/tests/Makefile.am index ba41456a89..71cfb3f41c 100644 --- a/gl/tests/Makefile.am +++ b/gl/tests/Makefile.am @@ -2,10 +2,22 @@ ## Process this file with automake to produce Makefile.in. # Copyright (C) 2002-2011 Free Software Foundation, Inc. # -# This file is free software, distributed under the terms of the GNU -# General Public License. As a special exception to the GNU General -# Public License, this file may be distributed as part of a program -# that contains a configuration script generated by Autoconf, under +# This file is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This file is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this file. If not, see <http://www.gnu.org/licenses/>. +# +# As a special exception to the GNU General Public License, +# this file may be distributed as part of a program that +# contains a configuration script generated by Autoconf, under # the same distribution terms as the rest of that program. # # Generated by gnulib-tool. @@ -96,6 +108,23 @@ EXTRA_DIST += test-c-ctype.c macros.h ## end gnulib module c-ctype-tests +## begin gnulib module close + + +EXTRA_DIST += close.c + +EXTRA_libtests_a_SOURCES += close.c + +## end gnulib module close + +## begin gnulib module close-tests + +TESTS += test-close +check_PROGRAMS += test-close +EXTRA_DIST += test-close.c signature.h macros.h + +## end gnulib module close-tests + ## begin gnulib module crypto/hmac-md5-tests TESTS += test-hmac-md5 @@ -198,6 +227,15 @@ EXTRA_DIST += test-fcntl-h.c ## end gnulib module fcntl-h-tests +## begin gnulib module fdopen + + +EXTRA_DIST += fdopen.c + +EXTRA_libtests_a_SOURCES += fdopen.c + +## end gnulib module fdopen + ## begin gnulib module fdopen-tests TESTS += test-fdopen @@ -222,6 +260,22 @@ EXTRA_DIST += test-float.c macros.h ## end gnulib module float-tests +## begin gnulib module fputc-tests + +TESTS += test-fputc +check_PROGRAMS += test-fputc +EXTRA_DIST += test-fputc.c signature.h macros.h + +## end gnulib module fputc-tests + +## begin gnulib module fread-tests + +TESTS += test-fread +check_PROGRAMS += test-fread +EXTRA_DIST += test-fread.c signature.h macros.h + +## end gnulib module fread-tests + ## begin gnulib module frexp-nolibm-tests TESTS += test-frexp-nolibm @@ -299,6 +353,14 @@ EXTRA_DIST += test-func.c macros.h ## end gnulib module func-tests +## begin gnulib module fwrite-tests + +TESTS += test-fwrite +check_PROGRAMS += test-fwrite +EXTRA_DIST += test-fwrite.c signature.h macros.h + +## end gnulib module fwrite-tests + ## begin gnulib module getcwd-lgpl @@ -519,15 +581,6 @@ EXTRA_DIST += test-memchr.c zerosize-ptr.h signature.h macros.h ## end gnulib module memchr-tests -## begin gnulib module msvc-inval - - -EXTRA_DIST += msvc-inval.c msvc-inval.h - -EXTRA_libtests_a_SOURCES += msvc-inval.c - -## end gnulib module msvc-inval - ## begin gnulib module netdb-tests TESTS += test-netdb diff --git a/gl/tests/close.c b/gl/tests/close.c new file mode 100644 index 0000000000..c2197fe0a6 --- /dev/null +++ b/gl/tests/close.c @@ -0,0 +1,69 @@ +/* close replacement. + Copyright (C) 2008-2011 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include <config.h> + +/* Specification. */ +#include <unistd.h> + +#include <errno.h> + +#include "fd-hook.h" +#include "msvc-inval.h" + +#undef close + +#if HAVE_MSVC_INVALID_PARAMETER_HANDLER +static int +close_nothrow (int fd) +{ + int result; + + TRY_MSVC_INVAL + { + result = close (fd); + } + CATCH_MSVC_INVAL + { + result = -1; + errno = EBADF; + } + DONE_MSVC_INVAL; + + return result; +} +#else +# define close_nothrow close +#endif + +/* Override close() to call into other gnulib modules. */ + +int +rpl_close (int fd) +{ +#if WINDOWS_SOCKETS + int retval = execute_all_close_hooks (close_nothrow, fd); +#else + int retval = close_nothrow (fd); +#endif + +#if REPLACE_FCHDIR + if (retval >= 0) + _gl_unregister_fd (fd); +#endif + + return retval; +} diff --git a/gl/tests/dup2.c b/gl/tests/dup2.c index 234bdfe50b..790c98a2e8 100644 --- a/gl/tests/dup2.c +++ b/gl/tests/dup2.c @@ -25,47 +25,39 @@ #include <errno.h> #include <fcntl.h> -#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ -/* Get declarations of the Win32 API functions. */ -# define WIN32_LEAN_AND_MEAN -# include <windows.h> -#endif - -#include "msvc-inval.h" - #if HAVE_DUP2 # undef dup2 -int -rpl_dup2 (int fd, int desired_fd) +# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ + +/* Get declarations of the Win32 API functions. */ +# define WIN32_LEAN_AND_MEAN +# include <windows.h> + +# include "msvc-inval.h" + +/* Get _get_osfhandle. */ +# include "msvc-nothrow.h" + +static int +ms_windows_dup2 (int fd, int desired_fd) { int result; -# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ + /* If fd is closed, mingw hangs on dup2 (fd, fd). If fd is open, dup2 (fd, fd) returns 0, but all further attempts to use fd in future dup2 calls will hang. */ if (fd == desired_fd) { - HANDLE handle; - - TRY_MSVC_INVAL - { - handle = (HANDLE) _get_osfhandle (fd); - } - CATCH_MSVC_INVAL - { - handle = INVALID_HANDLE_VALUE; - } - DONE_MSVC_INVAL; - - if (handle == INVALID_HANDLE_VALUE) + if ((HANDLE) _get_osfhandle (fd) == INVALID_HANDLE_VALUE) { errno = EBADF; return -1; } return fd; } + /* Wine 1.0.1 return 0 when desired_fd is negative but not -1: http://bugs.winehq.org/show_bug.cgi?id=21289 */ if (desired_fd < 0) @@ -73,11 +65,6 @@ rpl_dup2 (int fd, int desired_fd) errno = EBADF; return -1; } -# elif !defined __linux__ - /* On Haiku, dup2 (fd, fd) mistakenly clears FD_CLOEXEC. */ - if (fd == desired_fd) - return fcntl (fd, F_GETFL) == -1 ? -1 : fd; -# endif TRY_MSVC_INVAL { @@ -85,25 +72,38 @@ rpl_dup2 (int fd, int desired_fd) } CATCH_MSVC_INVAL { - result = -1; errno = EBADF; + result = -1; } DONE_MSVC_INVAL; -# ifdef __linux__ - /* Correct a Linux return value. - <http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.30.y.git;a=commitdiff;h=2b79bc4f7ebbd5af3c8b867968f9f15602d5f802> - */ - if (fd == desired_fd && result == (unsigned int) -EBADF) - { - errno = EBADF; - result = -1; - } -# endif if (result == 0) result = desired_fd; - /* Correct a cygwin 1.5.x errno value. */ - else if (result == -1 && errno == EMFILE) + + return result; +} + +# define dup2 ms_windows_dup2 + +# endif + +int +rpl_dup2 (int fd, int desired_fd) +{ + int result; + +# ifdef F_GETFL + /* On Linux kernels 2.6.26-2.6.29, dup2 (fd, fd) returns -EBADF. + On Cygwin 1.5.x, dup2 (1, 1) returns 0. + On Haiku, dup2 (fd, fd) mistakenly clears FD_CLOEXEC. */ + if (fd == desired_fd) + return fcntl (fd, F_GETFL) == -1 ? -1 : fd; +# endif + + result = dup2 (fd, desired_fd); + + /* Correct an errno value on FreeBSD 6.1 and Cygwin 1.5.x. */ + if (result == -1 && errno == EMFILE) errno = EBADF; # if REPLACE_FCHDIR if (fd != desired_fd && result != -1) diff --git a/gl/tests/fdopen.c b/gl/tests/fdopen.c new file mode 100644 index 0000000000..50c889b17a --- /dev/null +++ b/gl/tests/fdopen.c @@ -0,0 +1,69 @@ +/* Open a stream with a given file descriptor. + Copyright (C) 2011 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include <config.h> + +/* Specification. */ +#include <stdio.h> + +#include <errno.h> + +#if HAVE_MSVC_INVALID_PARAMETER_HANDLER +# include "msvc-inval.h" +#endif + +#undef fdopen + +#if HAVE_MSVC_INVALID_PARAMETER_HANDLER +static FILE * +fdopen_nothrow (int fd, const char *mode) +{ + FILE *result; + + TRY_MSVC_INVAL + { + result = fdopen (fd, mode); + } + CATCH_MSVC_INVAL + { + result = NULL; + } + DONE_MSVC_INVAL; + + return result; +} +#else +# define fdopen_nothrow fdopen +#endif + +FILE * +rpl_fdopen (int fd, const char *mode) +{ + int saved_errno = errno; + FILE *fp; + + errno = 0; + fp = fdopen_nothrow (fd, mode); + if (fp == NULL) + { + if (errno == 0) + errno = EBADF; + } + else + errno = saved_errno; + + return fp; +} diff --git a/gl/tests/msvc-inval.c b/gl/tests/msvc-inval.c deleted file mode 100644 index cff07960de..0000000000 --- a/gl/tests/msvc-inval.c +++ /dev/null @@ -1,69 +0,0 @@ -/* Invalid parameter handler for MSVC runtime libraries. - Copyright (C) 2011 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3, or (at your option) - any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, - Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - -#include <config.h> - -/* Specification. */ -#include "msvc-inval.h" - -#if HAVE_MSVC_INVALID_PARAMETER_HANDLER - -# ifdef STATUS_GNULIB_INVALID_PARAMETER - -/* Get declarations of the Win32 API functions. */ -# define WIN32_LEAN_AND_MEAN -# include <windows.h> - -static void cdecl -gl_msvc_invalid_parameter_handler (const wchar_t *expression, - const wchar_t *function, - const wchar_t *file, - unsigned int line, - uintptr_t dummy) -{ - RaiseException (STATUS_GNULIB_INVALID_PARAMETER, 0, 0, NULL); -} - -static int gl_msvc_inval_initialized /* = 0 */; - -void -gl_msvc_inval_ensure_handler (void) -{ - if (gl_msvc_inval_initialized == 0) - { - _set_invalid_parameter_handler (gl_msvc_invalid_parameter_handler); - gl_msvc_inval_initialized = 1; - } -} - -# else - -jmp_buf gl_msvc_inval_restart; - -void cdecl -gl_msvc_invalid_parameter_handler (const wchar_t *expression, - const wchar_t *function, - const wchar_t *file, - unsigned int line, - uintptr_t dummy) -{ - longjmp (gl_msvc_inval_restart, 1); -} - -# endif - -#endif diff --git a/gl/tests/msvc-inval.h b/gl/tests/msvc-inval.h deleted file mode 100644 index 00d3a1e96c..0000000000 --- a/gl/tests/msvc-inval.h +++ /dev/null @@ -1,167 +0,0 @@ -/* Invalid parameter handler for MSVC runtime libraries. - Copyright (C) 2011 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3, or (at your option) - any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, - Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - -#ifndef _MSVC_INVAL_H -#define _MSVC_INVAL_H - -/* With MSVC runtime libraries with the "invalid parameter handler" concept, - functions like fprintf(), dup2(), or close() crash when the caller passes - an invalid argument. But POSIX wants error codes (such as EINVAL or EBADF) - instead. - This file defines macros that turn such an invalid parameter notification - into a non-local exit. An error code can then be produced at the target - of this exit. You can thus write code like - - TRY_MSVC_INVAL - { - <Code that can trigger an invalid parameter notification - but does not do 'return', 'break', 'continue', nor 'goto'.> - } - CATCH_MSVC_INVAL - { - <Code that handles an invalid parameter notification - but does not do 'return', 'break', 'continue', nor 'goto'.> - } - DONE_MSVC_INVAL; - - This entire block expands to a single statement. - */ - -#if HAVE_MSVC_INVALID_PARAMETER_HANDLER -/* A native Windows platform with the "invalid parameter handler" concept. */ - -/* Get _invalid_parameter_handler type and _set_invalid_parameter_handler - declaration. */ -#include <stdlib.h> - -# if defined _MSC_VER -/* A compiler that supports __try/__except, as described in the page - "try-except statement" on microsoft.com - <http://msdn.microsoft.com/en-us/library/s58ftw19.aspx>. - With __try/__except, we can use the multithread-safe exception handling. */ - -/* Gnulib can define its own status codes, as described in the page - "Raising Software Exceptions" on microsoft.com - <http://msdn.microsoft.com/en-us/library/het71c37.aspx>. - Our status codes are composed of - - 0xE0000000, mandatory for all user-defined status codes, - - 0x474E550, a API identifier ("GNU"), - - 0, 1, 2, ..., used to distinguish different status codes from the - same API. */ -# define STATUS_GNULIB_INVALID_PARAMETER (0xE0000000 + 0x474E550 + 0) - -# ifdef __cplusplus -extern "C" { -# endif - -/* Ensure that the invalid parameter handler in installed that raises a - software exception with code STATUS_GNULIB_INVALID_PARAMETER. - Because we assume no other part of the program installs a different - invalid parameter handler, this solution is multithread-safe. */ -extern void gl_msvc_inval_ensure_handler (void); - -# ifdef __cplusplus -} -# endif - -# define TRY_MSVC_INVAL \ - do \ - { \ - gl_msvc_inval_ensure_handler (); \ - __try -# define CATCH_MSVC_INVAL \ - __except (GetExceptionCode () == STATUS_GNULIB_INVALID_PARAMETER \ - ? EXCEPTION_EXECUTE_HANDLER \ - : EXCEPTION_CONTINUE_SEARCH) -# define DONE_MSVC_INVAL \ - } \ - while (0) - -# else -/* Any compiler. - We can only use setjmp/longjmp. - Unfortunately, this is *not* multithread-safe. */ - -# include <setjmp.h> - -# ifdef __cplusplus -extern "C" { -# endif - -/* The restart that will resume execution at the code between - CATCH_MSVC_INVAL and DONE_MSVC_INVAL. It is enabled only between - TRY_MSVC_INVAL and CATCH_MSVC_INVAL. */ -extern jmp_buf gl_msvc_inval_restart; - -/* The invalid parameter handler that unwinds the stack up to the - gl_msvc_inval_restart. It is enabled only between TRY_MSVC_INVAL - and CATCH_MSVC_INVAL. */ -extern void cdecl gl_msvc_invalid_parameter_handler (const wchar_t *expression, - const wchar_t *function, - const wchar_t *file, - unsigned int line, - uintptr_t dummy); - -# ifdef __cplusplus -} -# endif - -# define TRY_MSVC_INVAL \ - do \ - { \ - _invalid_parameter_handler orig_handler; \ - /* First, initialize gl_msvc_inval_restart. */ \ - if (setjmp (gl_msvc_inval_restart) == 0) \ - { \ - /* Then, enable gl_msvc_invalid_parameter_handler. */ \ - orig_handler = \ - _set_invalid_parameter_handler (gl_msvc_invalid_parameter_handler); -# define CATCH_MSVC_INVAL \ - /* Execution completed. \ - Disable gl_msvc_invalid_parameter_handler. */ \ - _set_invalid_parameter_handler (orig_handler); \ - } \ - else \ - { \ - /* Execution triggered an invalid parameter notification. \ - Disable gl_msvc_invalid_parameter_handler. */ \ - _set_invalid_parameter_handler (orig_handler); -# define DONE_MSVC_INVAL \ - } \ - } \ - while (0) - -# endif - -#else -/* A platform that does not need to the invalid parameter handler. */ - -/* The braces here avoid GCC warnings like - "warning: suggest explicit braces to avoid ambiguous `else'". */ -# define TRY_MSVC_INVAL \ - do \ - { \ - if (1) -# define CATCH_MSVC_INVAL \ - else -# define DONE_MSVC_INVAL \ - } \ - while (0) - -#endif - -#endif /* _MSVC_INVAL_H */ diff --git a/gl/tests/test-close.c b/gl/tests/test-close.c new file mode 100644 index 0000000000..2b87db1d0f --- /dev/null +++ b/gl/tests/test-close.c @@ -0,0 +1,44 @@ +/* Test closing a file or socket. + Copyright (C) 2011 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include <config.h> + +#include <unistd.h> + +#include "signature.h" +SIGNATURE_CHECK (close, int, (int)); + +#include <errno.h> + +#include "macros.h" + +int +main (void) +{ + /* Test behaviour for invalid file descriptors. */ + { + errno = 0; + ASSERT (close (-1) == -1); + ASSERT (errno == EBADF); + } + { + errno = 0; + ASSERT (close (99) == -1); + ASSERT (errno == EBADF); + } + + return 0; +} diff --git a/gl/tests/test-dup2.c b/gl/tests/test-dup2.c index 37454e965c..a14807a36e 100644 --- a/gl/tests/test-dup2.c +++ b/gl/tests/test-dup2.c @@ -36,6 +36,8 @@ SIGNATURE_CHECK (dup2, int, (int, int)); /* Get declarations of the Win32 API functions. */ # define WIN32_LEAN_AND_MEAN # include <windows.h> +/* Get _get_osfhandle. */ +# include "msvc-nothrow.h" #endif #include "macros.h" diff --git a/gl/tests/test-fgetc.c b/gl/tests/test-fgetc.c index a4e1953eec..4df46fb6e3 100644 --- a/gl/tests/test-fgetc.c +++ b/gl/tests/test-fgetc.c @@ -26,6 +26,8 @@ SIGNATURE_CHECK (fgetc, int, (FILE *)); #include <fcntl.h> #include <unistd.h> +#include "msvc-inval.h" + #include "macros.h" int @@ -33,6 +35,13 @@ main (int argc, char **argv) { const char *filename = "test-fgetc.txt"; + /* We don't have an fgetc() function that installs an invalid parameter + handler so far. So install that handler here, explicitly. */ +#if HAVE_MSVC_INVALID_PARAMETER_HANDLER \ + && MSVC_INVALID_PARAMETER_HANDLING == DEFAULT_HANDLING + gl_msvc_inval_ensure_handler (); +#endif + /* Prepare a file. */ { const char text[] = "hello world"; @@ -51,6 +60,7 @@ main (int argc, char **argv) errno = 0; ASSERT (fgetc (fp) == EOF); ASSERT (errno == EBADF); + ASSERT (ferror (fp)); fclose (fp); } @@ -63,6 +73,7 @@ main (int argc, char **argv) errno = 0; ASSERT (fgetc (fp) == EOF); ASSERT (errno == EBADF); + ASSERT (ferror (fp)); fclose (fp); } } @@ -73,6 +84,7 @@ main (int argc, char **argv) errno = 0; ASSERT (fgetc (fp) == EOF); ASSERT (errno == EBADF); + ASSERT (ferror (fp)); fclose (fp); } } diff --git a/gl/tests/test-fputc.c b/gl/tests/test-fputc.c new file mode 100644 index 0000000000..03a371627c --- /dev/null +++ b/gl/tests/test-fputc.c @@ -0,0 +1,90 @@ +/* Test of fputc() function. + Copyright (C) 2011 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#include <config.h> + +#include <stdio.h> + +#include "signature.h" +SIGNATURE_CHECK (fputc, int, (int, FILE *)); + +#include <errno.h> +#include <fcntl.h> +#include <unistd.h> + +#include "msvc-inval.h" + +#include "macros.h" + +int +main (int argc, char **argv) +{ + const char *filename = "test-fputc.txt"; + + /* We don't have an fputc() function that installs an invalid parameter + handler so far. So install that handler here, explicitly. */ +#if HAVE_MSVC_INVALID_PARAMETER_HANDLER \ + && MSVC_INVALID_PARAMETER_HANDLING == DEFAULT_HANDLING + gl_msvc_inval_ensure_handler (); +#endif + + /* Test that fputc() on an unbuffered stream sets errno if someone else + closes the stream fd behind the back of stdio. */ + { + FILE *fp = fopen (filename, "w"); + ASSERT (fp != NULL); + setvbuf (fp, NULL, _IONBF, 0); + ASSERT (close (fileno (fp)) == 0); + errno = 0; + ASSERT (fputc ('x', fp) == EOF); + ASSERT (errno == EBADF); + ASSERT (ferror (fp)); + fclose (fp); + } + + /* Test that fputc() on an unbuffered stream sets errno if the stream + was constructed with an invalid file descriptor. */ + { + FILE *fp = fdopen (-1, "w"); + if (fp != NULL) + { + setvbuf (fp, NULL, _IONBF, 0); + errno = 0; + ASSERT (fputc ('x', fp) == EOF); + ASSERT (errno == EBADF); + ASSERT (ferror (fp)); + fclose (fp); + } + } + { + FILE *fp = fdopen (99, "w"); + if (fp != NULL) + { + setvbuf (fp, NULL, _IONBF, 0); + errno = 0; + ASSERT (fputc ('x', fp) == EOF); + ASSERT (errno == EBADF); + ASSERT (ferror (fp)); + fclose (fp); + } + } + + /* Clean up. */ + unlink (filename); + + return 0; +} diff --git a/gl/tests/test-fread.c b/gl/tests/test-fread.c new file mode 100644 index 0000000000..e205c9e6ec --- /dev/null +++ b/gl/tests/test-fread.c @@ -0,0 +1,99 @@ +/* Test of fread() function. + Copyright (C) 2011 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#include <config.h> + +#include <stdio.h> + +#include "signature.h" +SIGNATURE_CHECK (fread, size_t, (void *, size_t, size_t, FILE *)); + +#include <errno.h> +#include <fcntl.h> +#include <unistd.h> + +#include "msvc-inval.h" + +#include "macros.h" + +int +main (int argc, char **argv) +{ + const char *filename = "test-fread.txt"; + + /* We don't have an fread() function that installs an invalid parameter + handler so far. So install that handler here, explicitly. */ +#if HAVE_MSVC_INVALID_PARAMETER_HANDLER \ + && MSVC_INVALID_PARAMETER_HANDLING == DEFAULT_HANDLING + gl_msvc_inval_ensure_handler (); +#endif + + /* Prepare a file. */ + { + const char text[] = "hello world"; + int fd = open (filename, O_RDWR | O_CREAT | O_TRUNC, 0600); + ASSERT (fd >= 0); + ASSERT (write (fd, text, sizeof (text)) == sizeof (text)); + ASSERT (close (fd) == 0); + } + + /* Test that fread() sets errno if someone else closes the stream + fd behind the back of stdio. */ + { + FILE *fp = fopen (filename, "r"); + char buf[5]; + ASSERT (fp != NULL); + ASSERT (close (fileno (fp)) == 0); + errno = 0; + ASSERT (fread (buf, 1, sizeof (buf), fp) == 0); + ASSERT (errno == EBADF); + ASSERT (ferror (fp)); + fclose (fp); + } + + /* Test that fread() sets errno if the stream was constructed with + an invalid file descriptor. */ + { + FILE *fp = fdopen (-1, "r"); + if (fp != NULL) + { + char buf[1]; + errno = 0; + ASSERT (fread (buf, 1, 1, fp) == 0); + ASSERT (errno == EBADF); + ASSERT (ferror (fp)); + fclose (fp); + } + } + { + FILE *fp = fdopen (99, "r"); + if (fp != NULL) + { + char buf[1]; + errno = 0; + ASSERT (fread (buf, 1, 1, fp) == 0); + ASSERT (errno == EBADF); + ASSERT (ferror (fp)); + fclose (fp); + } + } + + /* Clean up. */ + unlink (filename); + + return 0; +} diff --git a/gl/tests/test-fwrite.c b/gl/tests/test-fwrite.c new file mode 100644 index 0000000000..76ed05c5ef --- /dev/null +++ b/gl/tests/test-fwrite.c @@ -0,0 +1,93 @@ +/* Test of fwrite() function. + Copyright (C) 2011 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#include <config.h> + +#include <stdio.h> + +#include "signature.h" +SIGNATURE_CHECK (fwrite, size_t, (const void *, size_t, size_t, FILE *)); + +#include <errno.h> +#include <fcntl.h> +#include <unistd.h> + +#include "msvc-inval.h" + +#include "macros.h" + +int +main (int argc, char **argv) +{ + const char *filename = "test-fwrite.txt"; + + /* We don't have an fwrite() function that installs an invalid parameter + handler so far. So install that handler here, explicitly. */ +#if HAVE_MSVC_INVALID_PARAMETER_HANDLER \ + && MSVC_INVALID_PARAMETER_HANDLING == DEFAULT_HANDLING + gl_msvc_inval_ensure_handler (); +#endif + + /* Test that fwrite() on an unbuffered stream sets errno if someone else + closes the stream fd behind the back of stdio. */ + { + FILE *fp = fopen (filename, "w"); + char buf[5] = "world"; + ASSERT (fp != NULL); + setvbuf (fp, NULL, _IONBF, 0); + ASSERT (close (fileno (fp)) == 0); + errno = 0; + ASSERT (fwrite (buf, 1, sizeof (buf), fp) == 0); + ASSERT (errno == EBADF); + ASSERT (ferror (fp)); + fclose (fp); + } + + /* Test that fwrite() on an unbuffered stream sets errno if the stream + was constructed with an invalid file descriptor. */ + { + FILE *fp = fdopen (-1, "w"); + if (fp != NULL) + { + char buf[5] = "world"; + setvbuf (fp, NULL, _IONBF, 0); + errno = 0; + ASSERT (fwrite (buf, 1, sizeof (buf), fp) == 0); + ASSERT (errno == EBADF); + ASSERT (ferror (fp)); + fclose (fp); + } + } + { + FILE *fp = fdopen (99, "w"); + if (fp != NULL) + { + char buf[5] = "world"; + setvbuf (fp, NULL, _IONBF, 0); + errno = 0; + ASSERT (fwrite (buf, 1, sizeof (buf), fp) == 0); + ASSERT (errno == EBADF); + ASSERT (ferror (fp)); + fclose (fp); + } + } + + /* Clean up. */ + unlink (filename); + + return 0; +} |