1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
/*
NAME
stream-getdelim - test the mu_stream_getdelim function.
DESCRIPTION
This implements a simple memory-based stream and tests the
mu_stream_getdelim function on a predefined stream content with
various combinations of buffering type and buffer size settings.
On success, returns 0. On error, prints diagnostics on stderr and
exits with a non-0 code or aborts.
Before running each test, its short description is printed on stdout.
For obvious reasons, libc functions are used for output.
LICENSE
This file is part of GNU mailutils.
Copyright (C) 2020-2025 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, 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 <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <mailutils/types.h>
#include <mailutils/debug.h>
#include <mailutils/errno.h>
#include <mailutils/stream.h>
#include <mailutils/sys/stream.h>
char content[] =
"AB\n"
"\n"
"\n"
"CDEFG\n"
"H\n"
"IJ\n"
"KLMNOPQRST\n"
"UVWXYZ";
struct test_stream
{
struct _mu_stream stream;
char *ptr;
size_t size;
mu_off_t offset;
};
static int
ts_open (mu_stream_t stream)
{
struct test_stream *ts = (struct test_stream *) stream;
ts->ptr = content;
ts->size = strlen (content);
ts->offset = 0;
return 0;
}
static int
ts_read (mu_stream_t stream, char *optr, size_t osize, size_t *nbytes)
{
struct test_stream *ts = (struct test_stream *) stream;
size_t n = 0;
if (ts->ptr != NULL && ((size_t)ts->offset <= ts->size))
{
n = ts->size - ts->offset;
if (n > osize)
n = osize;
memcpy (optr, ts->ptr + ts->offset, n);
ts->offset += n;
}
if (nbytes)
*nbytes = n;
return 0;
}
static int
ts_seek (mu_stream_t stream, mu_off_t off, mu_off_t *presult)
{
struct test_stream *ts = (struct test_stream *) stream;
if (off < 0)
return ESPIPE;
ts->offset = off;
*presult = off;
return 0;
}
static int
ts_size (mu_stream_t stream, mu_off_t *psize)
{
struct test_stream *ts = (struct test_stream *) stream;
*psize = ts->size;
return 0;
}
int
test_stream_create (mu_stream_t *pstream)
{
int rc;
mu_stream_t stream;
stream = _mu_stream_create (sizeof (struct test_stream), MU_STREAM_READ|MU_STREAM_SEEK);
assert (stream != NULL);
stream->open = ts_open;
stream->read = ts_read;
stream->size = ts_size;
stream->seek = ts_seek;
rc = mu_stream_open (stream);
if (rc)
mu_stream_destroy (&stream);
else
*pstream = stream;
return rc;
}
void
runtest (mu_stream_t str)
{
char *buf = NULL;
size_t size = 0;
mu_off_t off;
size_t n;
int rc;
unsigned long start = 0;
MU_ASSERT (mu_stream_seek (str, 0, MU_SEEK_SET, NULL));
while ((rc = mu_stream_getdelim (str, &buf, &size, '\n', &n)) == 0
&& n > 0)
{
size_t blen = strlen (buf);
size_t len = strcspn (content + start, "\n");
if (content[start+len] == '\n')
len++;
if (len != blen || memcmp (content + start, buf, len))
{
fprintf (stderr, "at %lu: expected:\n", start);
fwrite (content + start, blen, 1, stderr);
fprintf (stderr, "\nfound:\n");
fwrite (buf, blen, 1, stderr);
fputc ('\n', stderr);
exit (1);
}
start += len;
MU_ASSERT (mu_stream_seek (str, 0, MU_SEEK_CUR, &off));
if (off != start)
{
fprintf (stderr, "wrong offset reported (%lu, expected %lu\n",
(unsigned long)off, (unsigned long)start);
exit (2);
}
}
if (rc)
{
mu_diag_funcall (MU_DIAG_ERROR, "mu_stream_getline", NULL, rc);
exit (1);
}
free (buf);
}
#define TESTBUFSIZE 512
static struct test
{
char *name;
enum mu_buffer_type type;
size_t size;
} testtab[] = {
{ "No buffering", mu_buffer_none, 0 },
{ "Linear buffering", mu_buffer_line, TESTBUFSIZE },
{ "Linear buffering (small buffer)", mu_buffer_line, 1 },
{ "Full buffering (big buffer)", mu_buffer_full, TESTBUFSIZE },
{ "Full buffering (moderate buffer)", mu_buffer_full, sizeof(content)/2 },
{ "Full buffering (small buffer)", mu_buffer_full, 1 },
{ NULL }
};
int
main (int argc, char **argv)
{
mu_stream_t str;
int i;
MU_ASSERT (test_stream_create (&str));
for (i = 0; testtab[i].name; i++)
{
printf ("%d: %s\n", i, testtab[i].name);
MU_ASSERT (mu_stream_set_buffer (str, testtab[i].type, testtab[i].size));
runtest (str);
}
mu_stream_destroy (&str);
return 0;
}
|