Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/tests/test_tio.c
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2008-01-01 21:53:44 +0100
committerArthur de Jong <arthur@arthurdejong.org>2008-01-01 21:53:44 +0100
commit024fde50cc416a089bd1240cd993716557f14bae (patch)
tree38d4d006b232c40629d98af161e5f1030eace3d0 /tests/test_tio.c
parentcbcd0904ad5e15282b1836e452ebeb6d3d03c29e (diff)
add limited implementation of tio_mark() and tio_reset() functions to do limited seeks in the read stream, clean up header file comments and write tests for new code
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-ldapd@547 ef36b2f9-881f-0410-afb5-c4e39611909c
Diffstat (limited to 'tests/test_tio.c')
-rw-r--r--tests/test_tio.c79
1 files changed, 74 insertions, 5 deletions
diff --git a/tests/test_tio.c b/tests/test_tio.c
index 5e8758b..f3bc8f8 100644
--- a/tests/test_tio.c
+++ b/tests/test_tio.c
@@ -2,7 +2,7 @@
test_tio.c - simple test for the tio module
This file is part of the nss-ldapd library.
- Copyright (C) 2007 Arthur de Jong
+ Copyright (C) 2007, 2008 Arthur de Jong
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
@@ -55,7 +55,7 @@ static void *help_tiowriter(void *arg)
timeout.tv_sec=hargs->timeout;
timeout.tv_usec=0;
/* open the file */
- fp = tio_fdopen(hargs->fd,&timeout,&timeout);
+ fp=tio_fdopen(hargs->fd,&timeout,&timeout);
assert(fp!=NULL);
/* write the blocks */
i=0;
@@ -87,7 +87,7 @@ static void *help_tioreader(void *arg)
timeout.tv_sec=hargs->timeout;
timeout.tv_usec=0;
/* open the file */
- fp = tio_fdopen(hargs->fd,&timeout,&timeout);
+ fp=tio_fdopen(hargs->fd,&timeout,&timeout);
assert(fp!=NULL);
/* read the blocks */
i=0;
@@ -115,7 +115,7 @@ static void *help_normwriter(void *arg)
buf=(uint8_t *)malloc(hargs->blocksize);
assert(buf!=NULL);
/* open the file */
- fp = fdopen(hargs->fd,"wb");
+ fp=fdopen(hargs->fd,"wb");
assert(fp!=NULL);
/* write the blocks */
i=0;
@@ -139,7 +139,7 @@ static void *help_normreader(void *arg)
size_t i,j,k;
struct helper_args *hargs=(struct helper_args *)arg;
/* open the file */
- fp = fdopen(hargs->fd,"rb");
+ fp=fdopen(hargs->fd,"rb");
assert(fp!=NULL);
/* read the blocks */
i=0;
@@ -189,6 +189,73 @@ static int test_blocks(size_t wbs, size_t wbl, size_t rbs, size_t rbl)
return 0;
}
+static void test_reset(void)
+{
+ int sp[2];
+ pthread_t wthread;
+ struct helper_args wargs;
+ TFILE *fp;
+ struct timeval timeout;
+ size_t i,j,k,save;
+ uint8_t buf[10];
+ /* set up the socket pair */
+ assert(socketpair(AF_UNIX,SOCK_STREAM,0,sp)==0);
+ /* start the writer thread */
+ wargs.fd=sp[0];
+ wargs.blocksize=4*1024; /* the current TIO_BUFFERSIZE */
+ wargs.blocks=5;
+ wargs.timeout=2;
+ assert(pthread_create(&wthread,NULL,help_tiowriter,&wargs)==0);
+ /* set up read handle */
+ timeout.tv_sec=2;
+ timeout.tv_usec=0;
+ fp=tio_fdopen(sp[1],&timeout,&timeout);
+ assert(fp!=NULL);
+ /* perform 20 reads */
+ i=0;
+ for (k=0;k<20;k++)
+ {
+ assert(tio_read(fp,buf,sizeof(buf))==0);
+ /* check the buffer */
+ for (j=0;j<sizeof(buf);j++)
+ assert(buf[j]==(uint8_t)(i++));
+ }
+ /* mark and perform another 2 reads */
+ tio_mark(fp);
+ save=i;
+ for (k=0;k<2;k++)
+ {
+ assert(tio_read(fp,buf,sizeof(buf))==0);
+ /* check the buffer */
+ for (j=0;j<sizeof(buf);j++)
+ assert(buf[j]==(uint8_t)(i++));
+ }
+ /* reset and perform the same 2 reads again and 500 more */
+ assert(tio_reset(fp)==0);
+ i=save;
+ for (k=0;k<502;k++)
+ {
+ assert(tio_read(fp,buf,sizeof(buf))==0);
+ /* check the buffer */
+ for (j=0;j<sizeof(buf);j++)
+ assert(buf[j]==(uint8_t)(i++));
+ }
+ /* check that reset is no longer possible */
+ assert(tio_reset(fp)!=0);
+ /* read the remainder of the data 1526 reads */
+ for (k=0;k<1526;k++)
+ {
+ assert(tio_read(fp,buf,sizeof(buf))==0);
+ /* check the buffer */
+ for (j=0;j<sizeof(buf);j++)
+ assert(buf[j]==(uint8_t)(i++));
+ }
+ /* close the file */
+ assert(tio_close(fp)==0);
+ /* wait for the writer thread to die */
+ assert(pthread_join(wthread,NULL)==0);
+}
+
/* the main program... */
int main(int UNUSED(argc),char UNUSED(*argv[]))
{
@@ -202,5 +269,7 @@ int main(int UNUSED(argc),char UNUSED(*argv[]))
/* writer closes file sooner */
/* test_blocks(4*1023,20,20*1023,5); */
/* test_blocks(10,9,10,10); */
+ /* set tio_mark() and tio_reset() functions */
+ test_reset();
return 0;
}