...
authoralex <[email protected]>
Mon, 28 Mar 2022 00:03:58 +0000 (17:03 -0700)
committeralex <[email protected]>
Mon, 28 Mar 2022 00:03:58 +0000 (17:03 -0700)
src/net/cache.c
src/net/send.c
src/net/wait.c
test/unit/Makefile.am
test/unit/net.tests.c

index 8cb0528490b8d54961ed36fbec7091f85602ee10..a8b27bea27c45a606e3a3df8b1b0ffdb49f16f71 100644 (file)
@@ -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;
index e206987f3d576866ee0d86214584d126dbe91faf..1a34cfe585bb3b569c402876f6611aa7f01d8bf3 100644 (file)
@@ -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))) {
index 636f52c84b2bf414fdd886d2f955d32f6d6f46a3..c5003936ab8a5f8a8097f81eeb0686532f919ca7 100644 (file)
@@ -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);
index ebc7039f2ed40e504cb034be7f78fe678a1f1da7..9a37ff4a8b1d866821cd05a6c23e459b7b2a14c0 100644 (file)
@@ -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 = \
index 934e50551c45998369540c5f26675dda9e09f0df..748983cdff08625a23b693e9f6ef7a713b767316 100644 (file)
@@ -3,16 +3,18 @@
 #include<net.h>
 
 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];
 }