From 492fa39c76fca2772b67d7cf80339a57aa19837a Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 24 Mar 2022 11:21:37 -0700 Subject: [PATCH] ... --- test/unit/Makefile.am | 8 +++- test/unit/net.tests.c | 93 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 test/unit/net.tests.c diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index 70f810d..2605ba1 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -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 index 0000000..55399d0 --- /dev/null +++ b/test/unit/net.tests.c @@ -0,0 +1,93 @@ +#include + +#include + +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; +} -- 2.39.5