diff options
author | Neal H. Walfield <[email protected]> | 2015-08-12 09:44:59 +0000 |
---|---|---|
committer | Neal H. Walfield <[email protected]> | 2015-08-20 12:16:19 +0000 |
commit | e76c75d8726558dc9084710253f0f6780e06fad3 (patch) | |
tree | af4f94877afbff1fceeeb3958c20046f9acc1eed | |
parent | common/iobuf.c: Better respect boundary conditions in iobuf_read_line. (diff) | |
download | gnupg-e76c75d8726558dc9084710253f0f6780e06fad3.tar.gz gnupg-e76c75d8726558dc9084710253f0f6780e06fad3.zip |
common/t-iobuf.c: Add a test case for multiple EOFs.
common/t-iobuf.c (main): Add a test case for multiple EOFs in an INPUT
pipeline.
--
Signed-off-by: Neal H. Walfield <[email protected]>.
-rw-r--r-- | common/t-iobuf.c | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/common/t-iobuf.c b/common/t-iobuf.c index e3d749995..0f21c351e 100644 --- a/common/t-iobuf.c +++ b/common/t-iobuf.c @@ -42,6 +42,58 @@ every_other_filter (void *opaque, int control, return 0; } +struct content_filter_state +{ + int pos; + int len; + const char *buffer; +}; + +static struct content_filter_state * +content_filter_new (const char *buffer) +{ + struct content_filter_state *state + = malloc (sizeof (struct content_filter_state)); + + state->pos = 0; + state->len = strlen (buffer); + state->buffer = buffer; + + return state; +} + +static int +content_filter (void *opaque, int control, + iobuf_t chain, byte *buf, size_t *len) +{ + struct content_filter_state *state = opaque; + + (void) chain; + + if (control == IOBUFCTRL_UNDERFLOW) + { + int remaining = state->len - state->pos; + int toread = *len; + assert (toread > 0); + + if (toread > remaining) + toread = remaining; + + if (toread == 0) + return -1; + + memcpy (buf, &state->buffer[state->pos], toread); + + state->pos += toread; + + *len = toread; + + return 0; + } + + return 0; +} + int main (int argc, char *argv[]) { @@ -184,5 +236,46 @@ main (int argc, char *argv[]) free (buffer); } + { + /* - 3 characters plus new line + - 4 characters plus new line + - 5 characters plus new line + - 5 characters, no new line + */ + char *content = "abcdefghijklmnopq"; + char *content2 = "0123456789"; + iobuf_t iobuf; + int rc; + int c; + int n; + int lastc = 0; + + iobuf = iobuf_temp_with_content (content, strlen(content)); + rc = iobuf_push_filter (iobuf, + content_filter, content_filter_new (content2)); + assert (rc == 0); + + n = 0; + while (1) + { + c = iobuf_readbyte (iobuf); + if (c == -1 && lastc == -1) + { + printf("Two EOFs in a row. Done.\n"); + break; + } + + lastc = c; + + if (c == -1) + printf("After %d bytes, got EOF.\n", n); + else + { + n ++; + printf ("%d: '%c' (%d)\n", n, c, c); + } + } + } + return 0; } |