1
0
mirror of https://git.savannah.gnu.org/git/gnulib.git synced 2025-09-11 11:50:52 +03:00

fopen-gnu: Add tests.

* tests/test-fopen-gnu.c: New file.
* modules/fopen-gnu-tests: New file.
This commit is contained in:
Bruno Haible
2020-05-24 20:40:01 +02:00
parent 10cb4be2a2
commit 48888b2562
3 changed files with 87 additions and 0 deletions

View File

@@ -1,5 +1,9 @@
2020-05-24 Bruno Haible <bruno@clisp.org>
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 <tim.ruehsen@gmx.de> in
<https://lists.gnu.org/archive/html/bug-gnulib/2020-05/msg00119.html>.

12
modules/fopen-gnu-tests Normal file
View File

@@ -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

71
tests/test-fopen-gnu.c Normal file
View File

@@ -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 <https://www.gnu.org/licenses/>. */
/* Written by Bruno Haible <bruno@clisp.org>, 2020. */
#include <config.h>
/* Specification. */
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#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;
}