From: alex Date: Mon, 28 Mar 2022 00:03:58 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=7c54314cbd8e9f3d425fc82ccb1d9d5a1cbb61ab;p=seeder ... --- diff --git a/src/net/cache.c b/src/net/cache.c index 8cb0528..a8b27be 100644 --- a/src/net/cache.c +++ b/src/net/cache.c @@ -4,14 +4,22 @@ int net_cache(void **prev, size_t *prev_size, void *p, size_t size) { void *next; size_t next_size; + if(NULL==prev) { return -1; } + if(NULL==prev_size) { return -1; } + if(NULL==p) { return -1; } + + if(size==0) { return 1; } + next_size = ((*prev)!=NULL) ? size + (*prev_size) : size; next = malloc(next_size); if(NULL==next) { return -1; } if((*prev)!=NULL) { memcpy(next,(*prev),(*prev_size)); - memcpy(&(((uint8_t*)next)[(*prev_size)]),p,size); + memcpy(&(((unsigned char*)next)[(*prev_size)]),p,size); free((*prev)); + } else { + memcpy(next,p,size); } (*prev) = next; diff --git a/src/net/send.c b/src/net/send.c index e206987..1a34cfe 100644 --- a/src/net/send.c +++ b/src/net/send.c @@ -3,6 +3,10 @@ int net_send(int sock, struct peer *info, void *p, size_t size) { ssize_t i; + if(sock<0) { return -1; } + if(NULL==info) { return -1; } + if(NULL==p) { return -1; } + i = send(sock,p,size,0); if(-1==i) { if(!((errno==EAGAIN)||(errno==EWOULDBLOCK))) { diff --git a/src/net/wait.c b/src/net/wait.c index 636f52c..c500393 100644 --- a/src/net/wait.c +++ b/src/net/wait.c @@ -6,6 +6,11 @@ int net_wait(int sock, struct peer *info, void *buf, size_t buf_size) { void *p; size_t size; ssize_t i; + + if(sock<0) { return -1; } + if(NULL==info) { return -1; } + if(NULL==buf) { return -1; } + if(0==buf_size) { return 1; } size = buf_size; p = use_cached(info,buf,&size); diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index ebc7039..9a37ff4 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -122,6 +122,7 @@ net_tests_SOURCES = \ $(top_srcdir)/src/net/cache.c \ $(top_srcdir)/src/net/send.c \ $(top_srcdir)/src/net/wait.c \ + $(top_srcdir)/src/peer/free.c \ $(top_srcdir)/src/peer/init.c opt_tests_SOURCES = \ diff --git a/test/unit/net.tests.c b/test/unit/net.tests.c index 934e505..748983c 100644 --- a/test/unit/net.tests.c +++ b/test/unit/net.tests.c @@ -3,16 +3,18 @@ #include int main(); -static int setup_socket(); +static void setup_sockets(); +static void net_cache_basic_test(); static void net_send_basic_test(); static void net_wait_basic_test(); -static int read_socket; -static int write_socket; +static int read_sock; +static int write_sock; int main() { setup_env(); + net_cache_basic_test(); net_send_basic_test(); net_wait_basic_test(); @@ -21,13 +23,55 @@ int main() { return EXIT_SUCCESS; } +static void net_cache_basic_test() { + unsigned char buf1[100], buf2[100]; + void *p1, *p2; + size_t i; + + memset(buf1,1,100); + memset(buf2,2,100); + + p1 = NULL; + p2 = NULL; + + assert(-1==net_cache(NULL,NULL,NULL,0)); + assert(-1==net_cache(&p1,NULL,NULL,0)); + + i = 0; + assert(-1==net_cache(&p1,&i,NULL,0)); + assert(-1==net_cache(&p1,&i,p2,0)); + + p2 = buf2; + assert(1==net_cache(&p1,&i,p2,0)); + + assert(NULL==p1); + + assert(1==net_cache(&p1,&i,p2,50)); + assert(memcmp(buf2,p1,50)==0); + assert(50==i); + + p2 = buf1; + assert(1==net_cache(&p1,&i,p2,50)); + + assert(100==i); + assert(memcmp(buf2,p1,50)==0); + assert(memcmp(buf1,&(((unsigned char*)p1)[50]),50)==0); + + assert(1==net_cache(&p1,&i,p2,100)); + assert(200==i); + assert(memcmp(buf2,p1,50)==0); + assert(memcmp(buf1,&(((unsigned char*)p1)[50]),100)==0); + assert(memcmp(buf1,&(((unsigned char*)p1)[150]),50)==0); + + free(p1); +} + static void net_send_basic_test() { struct peer *info; - int sock; char buf[100]; char expected[100]; - sock = setup_socket(); + setup_sockets(); memset(buf,1,100); memset(expected,0,100); @@ -35,27 +79,29 @@ static void net_send_basic_test() { assert(1==peer_init(&info)); assert(-1==net_send(-1,NULL,NULL,0)); - assert(-1==net_send(sock,info,NULL,0)); + assert(-1==net_send(write_sock,info,NULL,0)); - assert(1==net_send(sock,info,buf,0)); - assert(0==read(sock,expected,100)); + assert(1==net_send(write_sock,info,buf,0)); + assert(-1==read(read_sock,expected,100)); - assert(1==net_send(sock,info,buf,50)); - assert(50==read(sock,expected,100)); + assert(1==net_send(write_sock,info,buf,50)); + assert(50==read(read_sock,expected,100)); - assert(1==net_send(sock,info,buf,100)); - assert(100==read(sock,expected,100)); + assert(1==net_send(write_sock,info,buf,100)); + assert(100==read(read_sock,expected,100)); + + close(read_sock); + close(write_sock); - close(sock); + peer_free(info); } static void net_wait_basic_test() { struct peer *info; - int sock; char buf[100]; char expected[100]; - sock = setup_socket(); + setup_sockets(); memset(buf,1,100); memset(expected,0,100); @@ -63,40 +109,43 @@ static void net_wait_basic_test() { assert(1==peer_init(&info)); assert(-1==net_wait(-1,NULL,NULL,0)); - assert(-1==net_wait(sock,NULL,NULL,0)); - assert(-1==net_wait(sock,info,NULL,0)); - assert(-1==net_wait(sock,info,expected,0)); + assert(-1==net_wait(read_sock,NULL,NULL,0)); + assert(-1==net_wait(read_sock,info,NULL,0)); + assert(1==net_wait(read_sock,info,expected,0)); - assert(0==net_wait(sock,info,buf,100)); + assert(0==net_wait(read_sock,info,buf,100)); - assert(50==write(sock,expected,50)); + assert(50==write(write_sock,expected,50)); - assert(0==net_wait(sock,info,buf,100)); + assert(0==net_wait(read_sock,info,buf,100)); assert(info->in!=NULL); assert(info->in_size==50); assert(memcmp(buf,expected,100)!=0); - assert(25==write(sock,expected,25)); - assert(0==net_wait(sock,info,buf,100)); + assert(25==write(write_sock,expected,25)); + assert(0==net_wait(read_sock,info,buf,100)); assert(info->in!=NULL); assert(info->in_size==75); assert(memcmp(buf,expected,100)!=0); - assert(25==write(sock,expected,25)); + assert(25==write(write_sock,expected,25)); - assert(1==net_wait(sock,info,buf,100)); + assert(1==net_wait(read_sock,info,buf,100)); assert(info->in==NULL); assert(info->in_size==0); assert(memcmp(buf,expected,100)==0); - close(sock); + close(read_sock); + close(write_sock); + + peer_free(info); } -static int setup_sockets() { - int sock = socket(AF_UNIX,SOCK_STREAM,0); - assert(sock!=-1); +static void setup_sockets() { + int fds[2]; - assert(0==bind(sock, + assert(0==socketpair(AF_UNIX,SOCK_STREAM | SOCK_NONBLOCK, 0 ,fds)); - return sock; + read_sock = fds[0]; + write_sock = fds[1]; }