From 48888b25625ddcdcc296e4cc1b0cd13e417e54f0 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 24 May 2020 20:40:01 +0200 Subject: [PATCH 2/2] fopen-gnu: Add tests. * tests/test-fopen-gnu.c: New file. * modules/fopen-gnu-tests: New file. --- ChangeLog | 4 +++ modules/fopen-gnu-tests | 12 +++++++++ tests/test-fopen-gnu.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 modules/fopen-gnu-tests create mode 100644 tests/test-fopen-gnu.c diff --git a/ChangeLog b/ChangeLog index fab0d87..d7c1e8d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2020-05-24 Bruno Haible + fopen-gnu: Add tests. + * tests/test-fopen-gnu.c: New file. + * modules/fopen-gnu-tests: New file. + fopen-gnu: New module. Suggested by Tim Rühsen in . diff --git a/modules/fopen-gnu-tests b/modules/fopen-gnu-tests new file mode 100644 index 0000000..9e4c4ca --- /dev/null +++ b/modules/fopen-gnu-tests @@ -0,0 +1,12 @@ +Files: +tests/test-fopen-gnu.c +tests/macros.h + +Depends-on: +fcntl + +configure.ac: + +Makefile.am: +TESTS += test-fopen-gnu +check_PROGRAMS += test-fopen-gnu diff --git a/tests/test-fopen-gnu.c b/tests/test-fopen-gnu.c new file mode 100644 index 0000000..cae4042 --- /dev/null +++ b/tests/test-fopen-gnu.c @@ -0,0 +1,71 @@ +/* Test of opening a file stream. + Copyright (C) 2020 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 . */ + +/* Written by Bruno Haible , 2020. */ + +#include + +/* Specification. */ +#include + +#include +#include +#include + +#include "macros.h" + +#define BASE "test-fopen-gnu.t" + +int +main (void) +{ + FILE *f; + int fd; + int flags; + + /* Remove anything from prior partial run. */ + unlink (BASE "file"); + + /* Create the file. */ + f = fopen (BASE "file", "w"); + ASSERT (f); + fd = fileno (f); + ASSERT (fd >= 0); + flags = fcntl (fd, F_GETFD); + ASSERT (flags >= 0); + ASSERT ((flags & FD_CLOEXEC) == 0); + ASSERT (fclose (f) == 0); + + /* Create the file and check the 'e' mode. */ + f = fopen (BASE "file", "we"); + ASSERT (f); + fd = fileno (f); + ASSERT (fd >= 0); + flags = fcntl (fd, F_GETFD); + ASSERT (flags >= 0); + ASSERT ((flags & FD_CLOEXEC) != 0); + ASSERT (fclose (f) == 0); + + /* Open the file and check the 'x' mode. */ + f = fopen (BASE "file", "ax"); + ASSERT (f == NULL); + ASSERT (errno == EEXIST); + + /* Cleanup. */ + ASSERT (unlink (BASE "file") == 0); + + return 0; +} -- 2.7.4