2
2
test_tio.c - simple test for the tio module
3
3
This file is part of the nss-pam-ldapd library.
5
Copyright (C) 2007, 2008 Arthur de Jong
5
Copyright (C) 2007, 2008, 2011 Arthur de Jong
7
7
This library is free software; you can redistribute it and/or
8
8
modify it under the terms of the GNU Lesser General Public
33
33
#endif /* HAVE_STDINT_H */
34
34
#include <stdlib.h>
37
41
#include "common/tio.h"
39
#ifndef __ASSERT_FUNCTION
40
#define __ASSERT_FUNCTION ""
41
#endif /* not __ASSERT_FUNCTION */
43
#define assertok(expr) \
46
: __assertok_fail(__STRING(expr),__FILE__,__LINE__,__ASSERT_FUNCTION))
48
static void __assertok_fail(const char *expr,const char *file,
49
int line,const char *function)
52
snprintf(msg,sizeof(msg),"%s (errno=\"%s\")",expr,strerror(errno));
53
__assert_fail(msg,file,line,function);
56
43
/* structure for passing arguments to helper (is a thread) */
57
44
struct helper_args {
187
174
/* set up the socket pair */
188
175
assertok(socketpair(AF_UNIX,SOCK_STREAM,0,sp)==0);
190
printf("test_tio: writing %d blocks of %d bytes (%d total)\n",wbl,wbs,wbl*wbs);
191
printf("test_tio: reading %d blocks of %d bytes (%d total)\n",rbl,rbs,rbl*rbs);
177
printf("test_tio: writing %d blocks of %d bytes (%d total)\n",(int)wbl,(int)wbs,(int)(wbl*wbs));
178
printf("test_tio: reading %d blocks of %d bytes (%d total)\n",(int)rbl,(int)rbs,(int)(rbl*rbs));
192
179
/* start the writer thread */
194
181
wargs.blocksize=wbs;
288
275
assertok(pthread_join(wthread,NULL)==0);
278
/* this test starts a reader and writer and does not write for a while */
279
static void test_timeout_reader(void)
284
struct timeval timeout;
287
/* set up the socket pair */
288
assertok(socketpair(AF_UNIX,SOCK_STREAM,0,sp)==0);
289
/* open the writer */
290
assertok((wfp=fdopen(sp[0],"wb"))!=NULL);
291
/* open the reader */
293
timeout.tv_usec=100000;
294
assertok((rfp=tio_fdopen(sp[1],&timeout,&timeout,2*1024,4*1024,2*1024,4*1024))!=NULL);
297
assertok(tio_read(rfp,buf,sizeof(buf))!=0);
300
/* close the files */
301
assertok(tio_close(rfp)==0);
302
assertok(fclose(wfp)==0);
305
/* this test starts a writer and an idle reader */
306
static void test_timeout_writer(void)
312
struct timeval timeout;
315
/* set up the socket pair */
316
assertok(socketpair(AF_UNIX,SOCK_STREAM,0,sp)==0);
317
/* open the reader */
318
assertok((rfp=fdopen(sp[0],"rb"))!=NULL);
319
/* open the writer */
321
timeout.tv_usec=100000;
322
assertok((wfp=tio_fdopen(sp[1],&timeout,&timeout,2*1024,4*1024,2*20,4*20+1))!=NULL);
323
/* perform a few write (these should be OK because they fill the buffer) */
324
assertok(tio_write(wfp,buf,sizeof(buf))==0);
325
assertok(tio_write(wfp,buf,sizeof(buf))==0);
326
assertok(tio_write(wfp,buf,sizeof(buf))==0);
327
assertok(tio_write(wfp,buf,sizeof(buf))==0);
328
/* one of these should fail but it depends on OS buffers */
330
for (i=0;(i<10000)&&(tio_write(wfp,buf,sizeof(buf))==0);i++);
334
/* close the files */
335
assertok(tio_close(wfp)!=0); /* fails because of bufferred data */
336
assertok(fclose(rfp)==0);
291
339
/* the main program... */
292
340
int main(int UNUSED(argc),char UNUSED(*argv[]))