From b1a3392f14e9161e3364bef1a7fd459a3adff3e9 Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 23 Jul 2022 12:22:43 -0700 Subject: [PATCH] ... --- inc/peer.h | 1 + src/peer/bitfield.c | 6 ++++-- test/integration/Makefile.am | 6 +++++- test/integration/qbittorrent.tests.c | 16 +++++++++++++++- test/integration/test_utils.h | 2 ++ 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/inc/peer.h b/inc/peer.h index b2b7b8d..c2537ca 100644 --- a/inc/peer.h +++ b/inc/peer.h @@ -77,6 +77,7 @@ enum peer_message { #include int peer_bitfield(struct peer*); +int peer_bitfield_init(struct peer*,size_t); int peer_bitfield_received(struct peer*,ssize_t); int peer_bitfield_toggle(struct peer*,size_t); int peer_cancel(struct peer*); diff --git a/src/peer/bitfield.c b/src/peer/bitfield.c index cb79765..c7d0cf3 100644 --- a/src/peer/bitfield.c +++ b/src/peer/bitfield.c @@ -5,8 +5,6 @@ int peer_bitfield(struct peer *info) { buf = malloc(info->bitfield_size); if(NULL==buf) { return -1; } - memset(buf,0xff,info->bitfield_size); - if(net_send(info,buf,info->bitfield_size)<0) { return -1; } free(buf); @@ -17,6 +15,10 @@ int peer_bitfield(struct peer *info) { int peer_bitfield_init(struct peer *info, size_t pieces) { info->bitfield_size = pieces/8; + if((info->bitfield_size*8)bitfield_size++; + } + info->bitfield = calloc(pieces,sizeof(uint8_t)); if(NULL==info->bitfield) { return -1; } diff --git a/test/integration/Makefile.am b/test/integration/Makefile.am index 4706590..9550eeb 100644 --- a/test/integration/Makefile.am +++ b/test/integration/Makefile.am @@ -16,7 +16,8 @@ AM_CPPFLAGS += \ -DNDEBUG endif -check_PROGRAMS = basic.tests qbittorrent.tests +#check_PROGRAMS = basic.tests qbittorrent.tests +check_PROGRAMS = qbittorrent.tests TESTS = $(check_PROGRAMS) #if ENABLE_MEMCHECK @@ -35,8 +36,11 @@ qbittorrent_tests_SOURCES = \ $(common_SOURCES) \ qbittorrent.tests.c \ $(top_srcdir)/src/net/cache.c \ + $(top_srcdir)/src/net/expected.c \ + $(top_srcdir)/src/net/peek.c \ $(top_srcdir)/src/net/send.c \ $(top_srcdir)/src/net/wait.c \ + $(top_srcdir)/src/peer/bitfield.c \ $(top_srcdir)/src/peer/free.c \ $(top_srcdir)/src/peer/handshake.c \ $(top_srcdir)/src/peer/id.c \ diff --git a/test/integration/qbittorrent.tests.c b/test/integration/qbittorrent.tests.c index a51fe54..f1fef6f 100644 --- a/test/integration/qbittorrent.tests.c +++ b/test/integration/qbittorrent.tests.c @@ -23,7 +23,12 @@ int main() { void qbittorrent_integration_test() { struct addrinfo hints, *res; struct peer *info; + enum peer_message type; + ssize_t i; + + // test torrent: debian-live-11.4.0-amd64-xfce.iso const char infohash[] = {0x25, 0x11, 0xdf, 0x9f, 0x74, 0xdc, 0x6f, 0xc1, 0x8e, 0xc9, 0x46, 0x6c, 0xdd, 0x52, 0xf5, 0xa1, 0x83, 0x27, 0xe2, 0x6e}; + const size_t pieces = 2526; assert(1==peer_init(&info)); @@ -37,15 +42,24 @@ void qbittorrent_integration_test() { assert(info->sock>0); if(0!=connect(info->sock,res->ai_addr,res->ai_addrlen)) { - exit(77); + exit(EXIT_SKIP); } assert(1==session_setup()); memcpy(info->infohash,infohash,PEER_INFOHASH_SIZE); + assert(1==peer_bitfield_init(info,pieces)); assert(1==peer_handshake(info)); assert(1==peer_handshake_received(info)); + assert(1==peer_bitfield(info)); + + i = net_expected(info,&type); + assert(i>=0); + assert(PEER_MESSAGE_BITFIELD==type); + + assert(1==peer_bitfield_received(info,i)); + close(info->sock); peer_free(info); } diff --git a/test/integration/test_utils.h b/test/integration/test_utils.h index 1d9ee1b..726e541 100644 --- a/test/integration/test_utils.h +++ b/test/integration/test_utils.h @@ -15,6 +15,8 @@ #include #include +#define EXIT_SKIP 77 + void clean_env(); void reset_env(); pid_t run(char*const[]); -- 2.30.2