The long-standing bug with ftello on mingw and 32-bit Solaris OpenIndiana
could only go undetected for so long because we had no unit test for it.
Admittedly, a unit test that creates a 3 GB file could be considered crazy
a couple of years ago. But gnulib-tool now has mechanisms to enable such tests
only selectively: off by default, enabled through --with-longrunnings-tests.


2024-11-14  Bruno Haible  <br...@clisp.org>

        ftello: Add tests for large files.
        * tests/test-ftello-largefile.c: New file.
        * modules/ftello-extra-tests: New file.
        * modules/ftello-tests (Depends-on): Add ftello-extra-tests.

2024-11-14  Bruno Haible  <br...@clisp.org>

        fseeko: Add tests for large files.
        * tests/test-fseeko-largefile.c: New file.
        * modules/fseeko-extra-tests: New file.
        * modules/fseeko-tests (Depends-on): Add fseeko-extra-tests.

>From b18f628def8ff2187ae2912bac003a2549299fb8 Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Thu, 14 Nov 2024 05:20:33 +0100
Subject: [PATCH 1/2] fseeko: Add tests for large files.

* tests/test-fseeko-largefile.c: New file.
* modules/fseeko-extra-tests: New file.
* modules/fseeko-tests (Depends-on): Add fseeko-extra-tests.
---
 ChangeLog                     |  7 +++
 modules/fseeko-extra-tests    | 17 +++++++
 modules/fseeko-tests          |  1 +
 tests/test-fseeko-largefile.c | 86 +++++++++++++++++++++++++++++++++++
 4 files changed, 111 insertions(+)
 create mode 100644 modules/fseeko-extra-tests
 create mode 100644 tests/test-fseeko-largefile.c

diff --git a/ChangeLog b/ChangeLog
index 671d025e30..421c2b277f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2024-11-14  Bruno Haible  <br...@clisp.org>
+
+	fseeko: Add tests for large files.
+	* tests/test-fseeko-largefile.c: New file.
+	* modules/fseeko-extra-tests: New file.
+	* modules/fseeko-tests (Depends-on): Add fseeko-extra-tests.
+
 2024-11-14  Bruno Haible  <br...@clisp.org>
 
 	ftello: Fix override on mingw.
diff --git a/modules/fseeko-extra-tests b/modules/fseeko-extra-tests
new file mode 100644
index 0000000000..e59b800e66
--- /dev/null
+++ b/modules/fseeko-extra-tests
@@ -0,0 +1,17 @@
+Status:
+longrunning-test
+
+Files:
+tests/test-fseeko-largefile.c
+tests/macros.h
+
+Depends-on:
+fsync
+ftruncate
+write
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-fseeko-largefile
+check_PROGRAMS += test-fseeko-largefile
diff --git a/modules/fseeko-tests b/modules/fseeko-tests
index 22e1e2b472..4fc539364a 100644
--- a/modules/fseeko-tests
+++ b/modules/fseeko-tests
@@ -12,6 +12,7 @@ m4/ungetc.m4
 
 Depends-on:
 fdopen
+fseeko-extra-tests
 
 configure.ac:
 gl_FUNC_UNGETC_WORKS
diff --git a/tests/test-fseeko-largefile.c b/tests/test-fseeko-largefile.c
new file mode 100644
index 0000000000..7cb7edc264
--- /dev/null
+++ b/tests/test-fseeko-largefile.c
@@ -0,0 +1,86 @@
+/* Test of fseeko() function on large files.
+   Copyright (C) 2024 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 <br...@clisp.org>, 2024.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <stdio.h>
+
+#include <fcntl.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "macros.h"
+
+#define TESTFILE "test-fseeko.data"
+#define TESTFILE_ZEROES 3000000000LL
+#define TESTFILE_DATA "GNU"
+#define TESTFILE_DATA_LEN 3
+
+int
+main (void)
+{
+  remove (TESTFILE);
+
+  if (sizeof (off_t) <= 4)
+    {
+      fprintf (stderr, "off_t is only 32 bits.\n");
+      return 77;
+    }
+  /* off_t is larger than 32-bit.  */
+
+  /* Create a file that is larger than 2 GiB.
+     On file systems such as ext2, the file will have "holes" and thus
+     not consume much disk space.  */
+  {
+    int fd = open (TESTFILE, O_CREAT | O_TRUNC | O_RDWR, 0600);
+    ASSERT (fd >= 0);
+    if (ftruncate (fd, TESTFILE_ZEROES) < 0
+        || lseek (fd, TESTFILE_ZEROES, SEEK_SET) < 0
+        || write (fd, TESTFILE_DATA, TESTFILE_DATA_LEN) < 0
+        || fsync (fd) < 0)
+      {
+        close (fd);
+        remove (TESTFILE);
+        fprintf (stderr, "Could not create 3 GB large file.\n");
+        return 77;
+      }
+    close (fd);
+  }
+
+  /* Check that fseeko() works.  */
+  {
+    FILE *fp = fopen (TESTFILE, "r");
+    ASSERT (fp != NULL);
+
+    int ret;
+    ret = fseeko (fp, TESTFILE_ZEROES, SEEK_SET);
+    ASSERT (ret == 0);
+
+    char buf[TESTFILE_DATA_LEN];
+    ASSERT (fread (buf, 1, TESTFILE_DATA_LEN, fp) == TESTFILE_DATA_LEN);
+    ASSERT (memcmp (buf, TESTFILE_DATA, TESTFILE_DATA_LEN) == 0);
+
+    ret = fclose (fp);
+    ASSERT (ret == 0);
+  }
+
+  remove (TESTFILE);
+
+  return test_exit_status;
+}
-- 
2.34.1

>From 21c9d397bdbcb10413513be40374e605f8a185b1 Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Thu, 14 Nov 2024 05:22:06 +0100
Subject: [PATCH 2/2] ftello: Add tests for large files.

* tests/test-ftello-largefile.c: New file.
* modules/ftello-extra-tests: New file.
* modules/ftello-tests (Depends-on): Add ftello-extra-tests.
---
 ChangeLog                     |  7 +++
 modules/ftello-extra-tests    | 18 ++++++++
 modules/ftello-tests          |  1 +
 tests/test-ftello-largefile.c | 83 +++++++++++++++++++++++++++++++++++
 4 files changed, 109 insertions(+)
 create mode 100644 modules/ftello-extra-tests
 create mode 100644 tests/test-ftello-largefile.c

diff --git a/ChangeLog b/ChangeLog
index 421c2b277f..6e44c14648 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2024-11-14  Bruno Haible  <br...@clisp.org>
+
+	ftello: Add tests for large files.
+	* tests/test-ftello-largefile.c: New file.
+	* modules/ftello-extra-tests: New file.
+	* modules/ftello-tests (Depends-on): Add ftello-extra-tests.
+
 2024-11-14  Bruno Haible  <br...@clisp.org>
 
 	fseeko: Add tests for large files.
diff --git a/modules/ftello-extra-tests b/modules/ftello-extra-tests
new file mode 100644
index 0000000000..0f980f2518
--- /dev/null
+++ b/modules/ftello-extra-tests
@@ -0,0 +1,18 @@
+Status:
+longrunning-test
+
+Files:
+tests/test-ftello-largefile.c
+tests/macros.h
+
+Depends-on:
+fsync
+fseeko
+ftruncate
+write
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-ftello-largefile
+check_PROGRAMS += test-ftello-largefile
diff --git a/modules/ftello-tests b/modules/ftello-tests
index 90d269eae7..d709a6ebe5 100644
--- a/modules/ftello-tests
+++ b/modules/ftello-tests
@@ -12,6 +12,7 @@ m4/ungetc.m4
 Depends-on:
 binary-io
 fdopen
+ftello-extra-tests
 
 configure.ac:
 gl_FUNC_UNGETC_WORKS
diff --git a/tests/test-ftello-largefile.c b/tests/test-ftello-largefile.c
new file mode 100644
index 0000000000..4dd778b366
--- /dev/null
+++ b/tests/test-ftello-largefile.c
@@ -0,0 +1,83 @@
+/* Test of ftello() function on large files.
+   Copyright (C) 2024 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 <br...@clisp.org>, 2024.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <stdio.h>
+
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "macros.h"
+
+#define TESTFILE "test-ftello.data"
+#define TESTFILE_ZEROES 3000000000LL
+#define TESTFILE_DATA "GNU"
+#define TESTFILE_DATA_LEN 3
+
+int
+main (void)
+{
+  remove (TESTFILE);
+
+  if (sizeof (off_t) <= 4)
+    {
+      fprintf (stderr, "off_t is only 32 bits.\n");
+      return 77;
+    }
+  /* off_t is larger than 32-bit.  */
+
+  /* Create a file that is larger than 2 GiB.
+     On file systems such as ext2, the file will have "holes" and thus
+     not consume much disk space.  */
+  {
+    int fd = open (TESTFILE, O_CREAT | O_TRUNC | O_RDWR, 0600);
+    ASSERT (fd >= 0);
+    if (ftruncate (fd, TESTFILE_ZEROES) < 0
+        || lseek (fd, TESTFILE_ZEROES, SEEK_SET) < 0
+        || write (fd, TESTFILE_DATA, TESTFILE_DATA_LEN) < 0
+        || fsync (fd) < 0)
+      {
+        close (fd);
+        remove (TESTFILE);
+        fprintf (stderr, "Could not create 3 GB large file.\n");
+        return 77;
+      }
+    close (fd);
+  }
+
+  /* Check the value of ftello().  */
+  {
+    FILE *fp = fopen (TESTFILE, "r");
+    ASSERT (fp != NULL);
+
+    ASSERT (fseeko (fp, 0, SEEK_END) == 0);
+
+    off_t length = ftello (fp);
+    ASSERT (length > 0);
+    ASSERT (length == TESTFILE_ZEROES + TESTFILE_DATA_LEN);
+
+    int ret = fclose (fp);
+    ASSERT (ret == 0);
+  }
+
+  remove (TESTFILE);
+
+  return test_exit_status;
+}
-- 
2.34.1

Reply via email to