]> Savannah Git Hosting - gnulib.git/commitdiff
fopen-gnu: Add tests.
authorBruno Haible <bruno@clisp.org>
Sun, 24 May 2020 18:40:01 +0000 (20:40 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 24 May 2020 19:20:51 +0000 (21:20 +0200)
* tests/test-fopen-gnu.c: New file.
* modules/fopen-gnu-tests: New file.

ChangeLog
modules/fopen-gnu-tests [new file with mode: 0644]
tests/test-fopen-gnu.c [new file with mode: 0644]

index fab0d875a50ddf0e8eef5f826cad25a50617425e..d7c1e8d8a05bee0b254246a027ed7b1c2b00a1b5 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -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>.
diff --git a/modules/fopen-gnu-tests b/modules/fopen-gnu-tests
new file mode 100644 (file)
index 0000000..9e4c4ca
--- /dev/null
@@ -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 (file)
index 0000000..cae4042
--- /dev/null
@@ -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;
+}