]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Thu, 24 Mar 2022 18:21:37 +0000 (11:21 -0700)
committeralex <[email protected]>
Thu, 24 Mar 2022 18:21:37 +0000 (11:21 -0700)
test/unit/Makefile.am
test/unit/net.tests.c [new file with mode: 0644]

index 70f810d6810392e78605920f5d0a17ead30db0c4..2605ba15c2bf4fd84debf45dac67b4e5f332ed24 100644 (file)
@@ -13,7 +13,7 @@ AM_CPPFLAGS += \
        -DNDEBUG
 endif
 
-check_PROGRAMS = bencode.tests block.tests feed.tests file.tests fs.concat.tests fs.filter.tests hash.tests hashmap.tests meta.tests opt.tests peer.tests pqueue.tests rss.tests session.tests torrent.tests tree.tests
+check_PROGRAMS = bencode.tests block.tests feed.tests file.tests fs.concat.tests fs.filter.tests hash.tests hashmap.tests meta.tests net.tests opt.tests peer.tests pqueue.tests rss.tests session.tests torrent.tests tree.tests
 TESTS = $(check_PROGRAMS)
 
 if ENABLE_MEMCHECK
@@ -116,6 +116,12 @@ meta_tests_SOURCES = \
 meta_tests_CPPFLAGS = $(AM_CPPFLAGS) \
        -DMETA_SRC_FILE="$(top_srcdir)/src/meta.c"
 
+net_tests_SOURCES = \
+       $(common_SOURCES) \
+       net.tests.c \
+       $(top_srcdir)/src/net/send.c \
+       $(top_srcdir)/src/net/wait.c
+       
 opt_tests_SOURCES = \
        $(common_SOURCES) \
        $(torrent_SOURCES) \
diff --git a/test/unit/net.tests.c b/test/unit/net.tests.c
new file mode 100644 (file)
index 0000000..55399d0
--- /dev/null
@@ -0,0 +1,93 @@
+#include<test_utils.h>
+
+#include<net.h>
+
+int main();
+static int setup_socket();
+static void net_send_basic_test();
+static void net_wait_basic_test();
+
+int main() {
+       setup_env();
+
+       net_send_basic_test();
+
+       clean_env();
+
+       return EXIT_SUCCESS;
+}
+
+static void net_send_basic_test() {
+       struct peer *info;
+       int sock;
+       char buf[100];
+       char expected[100];
+
+       sock = setup_socket();
+
+       memset(buf,1,100);
+       memset(expected,0,100);
+
+       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(sock,info,buf,0));
+       assert(0==read(sock,expected,100));
+       
+       assert(1==net_send(sock,info,buf,50));
+       assert(50==read(sock,expected,100));
+       
+       assert(1==net_send(sock,info,buf,100));
+       assert(100==read(sock,expected,100));
+
+       close(sock);
+}
+
+static void net_wait_basic_test() {
+       struct peer *info;
+       int sock;
+       char buf[100];
+       char expected[100];
+
+       sock = setup_socket();
+
+       memset(buf,1,100);
+       memset(expected,0,100);
+
+       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(0==net_wait(sock,info,buf,100));
+
+       assert(50==write(sock,expected,50));
+       
+       assert(0==net_wait(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(info->in!=NULL);
+       assert(info->in_size==75);
+       assert(memcmp(buf,expected,100)!=0);
+       
+       assert(25==write(sock,expected,25));
+
+       assert(1==net_wait(sock,info,buf,100));
+       assert(info->in==NULL);
+       assert(info->in_size==0);
+       assert(memcmp(buf,expected,100)==0);
+       
+       close(sock);
+}
+
+static int setup_socket() {
+       return -1;
+}