From caddad4b95b774f2358e37fab443249d80bc9d17 Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 28 Mar 2022 10:13:23 -0700 Subject: [PATCH] ... --- src/net/queue.c | 4 ++++ src/net/wait.c | 10 ++++++---- test/unit/Makefile.am | 1 + test/unit/net.tests.c | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/net/queue.c b/src/net/queue.c index eafc7ec..f4f08f0 100644 --- a/src/net/queue.c +++ b/src/net/queue.c @@ -3,6 +3,10 @@ int net_queue(int epoll_fd, int sock, struct peer *info) { struct epoll_event ev; + if(epoll_fd<0) { return -1; } + if(sock<0) { return -1; } + if(NULL==info) { return -1; } + ev.events = EPOLLIN | EPOLLOUT | EPOLLET; ev.data.ptr = info; ev.data.fd = sock; diff --git a/src/net/wait.c b/src/net/wait.c index beb4af3..8a96276 100644 --- a/src/net/wait.c +++ b/src/net/wait.c @@ -17,9 +17,11 @@ int net_wait(int sock, struct peer *info, void *buf, size_t buf_size) { i = recv(sock,p,size,0); if(-1==i) { - if((errno==EAGAIN)||(errno==EWOULDBLOCK)) { return 0; } - perror("recv"); - return -1; + if(!((errno==EAGAIN)||(errno==EWOULDBLOCK))) { + perror("recv"); + return -1; + } + i = 0; } if(i!=size) { goto cached; } @@ -51,7 +53,7 @@ static void *use_cached(struct peer *info,void *p,size_t *size) { free(info->in); info->in = NULL; - *size -= info->in_size; + (*size) -= info->in_size; p += info->in_size; } } diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index 9a37ff4..ea1d39f 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -120,6 +120,7 @@ net_tests_SOURCES = \ $(common_SOURCES) \ net.tests.c \ $(top_srcdir)/src/net/cache.c \ + $(top_srcdir)/src/net/queue.c \ $(top_srcdir)/src/net/send.c \ $(top_srcdir)/src/net/wait.c \ $(top_srcdir)/src/peer/free.c \ diff --git a/test/unit/net.tests.c b/test/unit/net.tests.c index 0fabe6d..eeb6014 100644 --- a/test/unit/net.tests.c +++ b/test/unit/net.tests.c @@ -5,6 +5,7 @@ int main(); static void setup_sockets(); static void net_cache_basic_test(); +static void net_queue_basic_test(); static void net_send_basic_test(); static void net_wait_basic_test(); @@ -15,6 +16,7 @@ int main() { setup_env(); net_cache_basic_test(); + net_queue_basic_test(); net_send_basic_test(); net_wait_basic_test(); @@ -66,6 +68,28 @@ static void net_cache_basic_test() { free(p1); } +static void net_queue_basic_test() { + int epoll_fd; + int sock; + struct peer *info; + + assert(1==peer_init(&info)); + + epoll_fd = epoll_create1(0); + assert(epoll_fd!=-1); + + sock = 0; + + assert(-1==net_queue(-1,-1,NULL)); + assert(-1==net_queue(epoll_fd,-1,NULL)); + assert(-1==net_queue(epoll_fd,sock,NULL)); + + assert(1==net_queue(epoll_fd,sock,info)); + + peer_free(info); + close(epoll_fd); +} + static void net_send_basic_test() { struct peer *info; char buf[100]; @@ -100,6 +124,7 @@ static void net_wait_basic_test() { struct peer *info; char buf[100]; char expected[100]; + int i, increment; setup_sockets(); @@ -133,6 +158,22 @@ static void net_wait_basic_test() { assert(1==net_wait(read_sock,info,buf,100)); assert(info->in==NULL); assert(memcmp(buf,expected,100)==0); + + i = 0; + info->in_size = 0; + while(i<99) { + assert(0==net_wait(read_sock,info,buf,100)); + increment = rand()%2; + i += increment; + assert(((increment>0)?1:0)==write(write_sock,expected,increment)); + assert(0==net_wait(read_sock,info,buf,100)); + assert(info->in_size==i); + } + + assert(1==write(write_sock,expected,1)); + assert(1==net_wait(read_sock,info,buf,100)); + assert(info->in==NULL); + assert(memcmp(buf,expected,100)==0); close(read_sock); close(write_sock); -- 2.39.5