summaryrefslogtreecommitdiff
path: root/libmailutils/tests
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2020-05-29 08:41:19 +0300
committerSergey Poznyakoff <gray@gnu.org>2020-05-29 11:54:37 +0300
commitf9d6e59d678a540da08c8a7795f45621e9d49a9c (patch)
tree2a4aac70c0d60da5df7a497e96cef513328137f8 /libmailutils/tests
parente863e55881ccfb534cf057e76c95a9ce4a302eab (diff)
downloadmailutils-f9d6e59d678a540da08c8a7795f45621e9d49a9c.tar.gz
mailutils-f9d6e59d678a540da08c8a7795f45621e9d49a9c.tar.bz2
libmailutils: fix error in full and line buffer mode
* libmailutils/stream/stream.c (_stream_buffer_full_p): Fix conditions for buffered streams. The buffer is full if current pointer (not level) equals the size. For line buffering, any newline appearing in the written portion of the buffer causes flushing. (mu_stream_write): Fix the free space calculation. * libmailutils/tests/t0-stream.at: New file. * libmailutils/tests/t0-stream.c: New file. * libmailutils/tests/t1-stream.at: New file. * libmailutils/tests/t1-stream.c: New file. * libmailutils/tests/Makefile.am: Add new testcase. * libmailutils/tests/testsuite.at: Likewise.
Diffstat (limited to 'libmailutils/tests')
-rw-r--r--libmailutils/tests/.gitignore2
-rw-r--r--libmailutils/tests/Makefile.am4
-rw-r--r--libmailutils/tests/t0-stream.at20
-rw-r--r--libmailutils/tests/t0-stream.c74
-rw-r--r--libmailutils/tests/t1-stream.at20
-rw-r--r--libmailutils/tests/t1-stream.c123
-rw-r--r--libmailutils/tests/testsuite.at4
7 files changed, 247 insertions, 0 deletions
diff --git a/libmailutils/tests/.gitignore b/libmailutils/tests/.gitignore
index 3045b1879..4d2380158 100644
--- a/libmailutils/tests/.gitignore
+++ b/libmailutils/tests/.gitignore
@@ -48,3 +48,5 @@ wordwrap
wordsplit-version.h
wsp
xscript
+t0-stream
+t1-stream
diff --git a/libmailutils/tests/Makefile.am b/libmailutils/tests/Makefile.am
index 702d14446..3fda9a4cc 100644
--- a/libmailutils/tests/Makefile.am
+++ b/libmailutils/tests/Makefile.am
@@ -59,6 +59,8 @@ noinst_PROGRAMS = \
strin\
strout\
strtoc\
+ t0-stream\
+ t1-stream\
tempfile\
tcli\
tocrlf\
@@ -167,6 +169,8 @@ TESTSUITE_AT += \
strin.at\
strout.at\
strtoc.at\
+ t0-stream.at\
+ t1-stream.at\
url.at\
url-comp.at\
xml.at\
diff --git a/libmailutils/tests/t0-stream.at b/libmailutils/tests/t0-stream.at
new file mode 100644
index 000000000..1a991dc66
--- /dev/null
+++ b/libmailutils/tests/t0-stream.at
@@ -0,0 +1,20 @@
+# This file is part of GNU Mailutils. -*- Autotest -*-
+# Copyright (C) 2020 Free Software Foundation, Inc.
+#
+# GNU Mailutils 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.
+#
+# GNU Mailutils 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 GNU Mailutils. If not, see <http://www.gnu.org/licenses/>.
+
+AT_SETUP([intermixed rw: full buffering])
+AT_KEYWORDS([stream])
+AT_CHECK([t0-stream])
+AT_CLEANUP \ No newline at end of file
diff --git a/libmailutils/tests/t0-stream.c b/libmailutils/tests/t0-stream.c
new file mode 100644
index 000000000..8de869a63
--- /dev/null
+++ b/libmailutils/tests/t0-stream.c
@@ -0,0 +1,74 @@
+/* This file is part of GNU Mailutils test suite
+ Copyright (C) 2020 Free Software Foundation, Inc.
+
+ GNU Mailutils 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.
+
+ GNU Mailutils 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 GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
+
+/*
+ * Mailutils versions up to release-3.9-11-ge863e55 contained a bug in stream
+ * buffering code, due to which a write occurring after a read caused the
+ * buffer to be flushed and the stream pointer advanced to the next chunk.
+ * As a result, the data were written to the next buffer. This test verifies
+ * that it is no longer the case by
+ * 1. Reading 128 bytes at position 10
+ * 2. Writing 2 bytes at current position.
+ * 3. Reverting to position 138
+ * 4. Reading 2 bytes.
+ * Obviously, the bytes obtained in 4 should be the same as those written in 2.
+ */
+
+#include <config.h>
+#include <mailutils/mailutils.h>
+
+char mem[1024];
+
+int
+main(int argc, char **argv)
+{
+ mu_stream_t str;
+ char buf[128];
+ static char pattern[] = { 'A', 'B' };
+
+ memset (mem, '.', sizeof (mem));
+ MU_ASSERT (mu_fixed_memory_stream_create (&str, mem, sizeof (mem),
+ MU_STREAM_RDWR));
+ MU_ASSERT (mu_stream_set_buffer (str, mu_buffer_full, 512));
+
+ MU_ASSERT (mu_stream_seek (str, 10, MU_SEEK_SET, NULL));
+ MU_ASSERT (mu_stream_read (str, buf, sizeof (buf), NULL));
+ MU_ASSERT (mu_stream_write (str, pattern, sizeof (pattern), NULL));
+
+ MU_ASSERT (mu_stream_seek (str, -2, MU_SEEK_CUR, NULL));
+ MU_ASSERT (mu_stream_read (str, buf, sizeof (pattern), NULL));
+ if (memcmp (buf, pattern, sizeof (pattern)))
+ {
+ int i;
+ static int bs = 64;
+ fprintf (stderr, "FAIL\n");
+ mu_stream_flush (str);
+ for (i = 0; i < sizeof (mem); i++)
+ {
+ if (i % bs == 0)
+ {
+ if (i)
+ putchar ('\n');
+ printf ("%03X: ", i);
+ }
+ putchar (mem[i]);
+ }
+ putchar ('\n');
+ return 1;
+ }
+ return 0;
+}
+
diff --git a/libmailutils/tests/t1-stream.at b/libmailutils/tests/t1-stream.at
new file mode 100644
index 000000000..a930921e1
--- /dev/null
+++ b/libmailutils/tests/t1-stream.at
@@ -0,0 +1,20 @@
+# This file is part of GNU Mailutils. -*- Autotest -*-
+# Copyright (C) 2020 Free Software Foundation, Inc.
+#
+# GNU Mailutils 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.
+#
+# GNU Mailutils 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 GNU Mailutils. If not, see <http://www.gnu.org/licenses/>.
+
+AT_SETUP([intermixed rw: line buffering])
+AT_KEYWORDS([stream])
+AT_CHECK([t1-stream])
+AT_CLEANUP
diff --git a/libmailutils/tests/t1-stream.c b/libmailutils/tests/t1-stream.c
new file mode 100644
index 000000000..7ae9f295f
--- /dev/null
+++ b/libmailutils/tests/t1-stream.c
@@ -0,0 +1,123 @@
+/* This file is part of GNU Mailutils test suite
+ Copyright (C) 2020 Free Software Foundation, Inc.
+
+ GNU Mailutils 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.
+
+ GNU Mailutils 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 GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdio.h>
+#include <mailutils/mailutils.h>
+
+char mem[1024] = "00000000\n"
+ "11111111\n"
+ "22222222\n"
+ "33333333\n";
+
+static char pattern[] = { 'A', 'B' };
+
+void
+test1 (mu_stream_t str)
+{
+ char buf[128];
+ size_t n;
+
+ MU_ASSERT (mu_stream_readline (str, buf, sizeof (buf), &n));
+ MU_ASSERT (mu_stream_seek (str, 2, MU_SEEK_SET, NULL));
+ MU_ASSERT (mu_stream_write (str, pattern, sizeof (pattern), NULL));
+}
+
+void
+test2 (mu_stream_t str)
+{
+ MU_ASSERT (mu_stream_flush (str));
+}
+
+void
+test3 (mu_stream_t str)
+{
+ MU_ASSERT (mu_stream_seek (str, strlen(mem), MU_SEEK_SET, NULL));
+ MU_ASSERT (mu_stream_write (str, pattern, sizeof (pattern), NULL));
+}
+
+void
+test4 (mu_stream_t str)
+{
+ MU_ASSERT (mu_stream_write (str, "ZZ\n", 3, NULL));
+}
+
+static struct test {
+ char *name;
+ void (*fun) (mu_stream_t str);
+ char *expect;
+} test[] = {
+ {
+ "First pattern write",
+ test1,
+ "00000000\n"
+ "11111111\n"
+ "22222222\n"
+ "33333333\n"
+ },
+ {
+ "Flush",
+ test2,
+ "00AB0000\n"
+ "11111111\n"
+ "22222222\n"
+ "33333333\n"
+ },
+ {
+ "Second pattern write",
+ test3,
+ "00AB0000\n"
+ "11111111\n"
+ "22222222\n"
+ "33333333\n"
+ },
+ {
+ "Newline added",
+ test4,
+ "00AB0000\n"
+ "11111111\n"
+ "22222222\n"
+ "33333333\n"
+ "ABZZ\n"
+ },
+
+ { NULL }
+};
+
+int
+main(int argc, char **argv)
+{
+ mu_stream_t str;
+ int i;
+
+ MU_ASSERT (mu_fixed_memory_stream_create (&str, mem, sizeof (mem),
+ MU_STREAM_RDWR));
+ MU_ASSERT (mu_stream_set_buffer (str, mu_buffer_line, 512));
+
+ for (i = 0; test[i].name; i++)
+ {
+ test[i].fun (str);
+ if (memcmp (mem, test[i].expect, strlen (test[i].expect)))
+ {
+ fprintf (stderr, "FAIL: %s\n", test[i].name);
+ fwrite (mem, 1, strlen (mem), stderr);
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
diff --git a/libmailutils/tests/testsuite.at b/libmailutils/tests/testsuite.at
index ab2ce79e9..33c08902a 100644
--- a/libmailutils/tests/testsuite.at
+++ b/libmailutils/tests/testsuite.at
@@ -98,6 +98,10 @@ MU_FILTER_TEST_NIBBLE([$1],[$2],[$3],[write],[$4],[$5],[$6],[$7])
AT_INIT
+AT_BANNER([Basic streams])
+m4_include([t0-stream.at])
+m4_include([t1-stream.at])
+
AT_BANNER([Conversions])
m4_include([strtoc.at])

Return to:

Send suggestions and report system problems to the System administrator.