From ccb6c9b9b63b6cedecfa532b8790246f1c395c43 Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 4 May 2022 18:42:40 -0700 Subject: [PATCH 01/16] ... --- src/net/loop.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/net/loop.c b/src/net/loop.c index 0ee81bf..9770436 100644 --- a/src/net/loop.c +++ b/src/net/loop.c @@ -13,6 +13,7 @@ void net_loop(struct net_info *info) { static void net_await_epoll_event(struct net_info *info) { struct epoll_event ev, events[MAX_EVENTS]; + struct peer *peer; int nfds, conn_sock; nfds = epoll_wait(info->epoll_fd,events,MAX_EVENTS,-1); @@ -34,11 +35,12 @@ static void net_await_epoll_event(struct net_info *info) { ev.events = EPOLLIN | EPOLLET; ev.data.fd = conn_sock; - if(peer_init((struct peer**)ev.data.ptr)<0) { + if(peer_init(&peer)<0) { return; } - ((struct peer*)ev.data.ptr)->sock = conn_sock; + peer->sock = conn_sock; + ev.data.ptr = peer; if(-1==epoll_ctl(info->epoll_fd,EPOLL_CTL_ADD,conn_sock,&ev)) { peer_free(ev.data.ptr); -- 2.30.2 From b64bfaff0a6981108c03942eecce6f9659620d2b Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 19 Jul 2022 15:15:13 -0700 Subject: [PATCH 02/16] ... --- .gitignore | 3 --- Makefile.am | 14 -------------- src/default.c | 1 - src/init.c | 4 ---- src/main.c | 1 - src/opt/env.c | 1 - src/opt/set.c | 1 - 7 files changed, 25 deletions(-) diff --git a/.gitignore b/.gitignore index 109ba99..f6bc33f 100644 --- a/.gitignore +++ b/.gitignore @@ -18,9 +18,6 @@ configure.scan **/Makefile.in stamp-h1 -# tags -tags - # build objects *.o *.log diff --git a/Makefile.am b/Makefile.am index 84b6457..f54c93c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -17,11 +17,6 @@ seederd_SOURCES = \ src/bencode/encode.c \ src/block.c \ src/default.c \ - src/feed/entries.c \ - src/feed/feeds.c \ - src/feed/gen.c \ - src/feed/info.c \ - src/feed/path.c \ src/file.c \ src/fs/concat.c \ src/fs/dir.c \ @@ -48,7 +43,6 @@ seederd_SOURCES = \ src/net/wait.c \ src/opt/config.c \ src/opt/env.c \ - src/opt/feed.c \ src/opt/filter.c \ src/opt/loglevel.c \ src/opt/out.c \ @@ -74,12 +68,6 @@ seederd_SOURCES = \ src/peer/request.c \ src/piece.c \ src/pqueue.c \ - src/rss/entry.c \ - src/rss/footer.c \ - src/rss/free.c \ - src/rss/header.c \ - src/rss/info.c \ - src/rss/init.c \ src/session.c \ src/sighandler.c \ src/torrent/add.c \ @@ -101,7 +89,6 @@ seederd_SOURCES += \ inc/bencode.h \ inc/block.h \ inc/default.h \ - inc/feed.h \ inc/file.h \ inc/fs.h \ inc/hash.h \ @@ -114,7 +101,6 @@ seederd_SOURCES += \ inc/opt.h \ inc/piece.h \ inc/pqueue.h \ - inc/rss.h \ inc/session.h \ inc/sighandler.h \ inc/torrent.h \ diff --git a/src/default.c b/src/default.c index 3d839e0..a31fcf9 100644 --- a/src/default.c +++ b/src/default.c @@ -15,7 +15,6 @@ int defaults() { // suppress output temporarily opt_set_log_level(LOG_LEVEL_SILENT); - if(opt_set_feed_url("")<0) { return -1; } if(opt_set_file_filter("default")<0) { return -1; } if(opt_set_piece_length("16384")<0) { return -1; } if(opt_set_port("5150")<0) { return -1; } diff --git a/src/init.c b/src/init.c index 86e6a2f..10cf191 100644 --- a/src/init.c +++ b/src/init.c @@ -7,7 +7,6 @@ struct option long_options[] = { {"config-file", required_argument, 0, 'c'}, {"daemon", no_argument, 0, 'd'}, - {"feed-url", required_argument, 0, 'u'}, {"file-filter", required_argument, 0, 'f'}, {"help", no_argument, 0, 'h'}, {"log-file", required_argument, 0, 'l'}, @@ -68,9 +67,6 @@ int init(int argc, char **argv) { case 'q': opt_set_log_level(LOG_LEVEL_SILENT); break; - case 'u': - if(opt_set_feed_url(optarg)<0) { return -1; } - break; case 'v': opt_set_log_level(LOG_LEVEL_VERBOSE); break; diff --git a/src/main.c b/src/main.c index 7aaf5fe..0c6526e 100644 --- a/src/main.c +++ b/src/main.c @@ -4,7 +4,6 @@ int main(int argc, char **argv) { if(init(argc,argv)<0) { return EXIT_FAILURE; } if(add()<0) { return EXIT_FAILURE; } - if(feeds()<0) { return EXIT_FAILURE; } if(watch()<0) { return EXIT_FAILURE; } if(net_start()<0) { return EXIT_FAILURE; } diff --git a/src/opt/env.c b/src/opt/env.c index f3fcbf6..2374f31 100644 --- a/src/opt/env.c +++ b/src/opt/env.c @@ -15,7 +15,6 @@ int opt_load_from_env() { log_info(OPT_MESSAGE_ENV_LOADING); CHECK_ENV("CONFIG",opt_load_config_file); - CHECK_ENV("FEED_URL",opt_set_feed_url); CHECK_ENV("FILE_FILTER",opt_set_file_filter); CHECK_ENV("LOG_FILE",opt_set_out_stream); CHECK_ENV("PIECE_LENGTH",opt_set_piece_length); diff --git a/src/opt/set.c b/src/opt/set.c index 35fe0cb..680ce0d 100644 --- a/src/opt/set.c +++ b/src/opt/set.c @@ -9,7 +9,6 @@ struct option_lookup_table_entry { // see src/opt/env.c for environmental variables struct option_lookup_table_entry option_lookup_table[] = { - {"feed_url",&opt_set_feed_url}, {"file_filter",&opt_set_file_filter}, {"piece_length",&opt_set_piece_length}, {"port",&opt_set_port}, -- 2.30.2 From 114062f958a0a3d58913138bbca298aeff6cecf6 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 22 Jul 2022 23:24:35 -0700 Subject: [PATCH 03/16] ... --- test/integration/Makefile.am | 6 ++++- test/integration/qbittorrent.tests.c | 34 ++++++++++++++++++++++++++++ test/integration/test_utils.h | 2 ++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 test/integration/qbittorrent.tests.c diff --git a/test/integration/Makefile.am b/test/integration/Makefile.am index 5cccaf1..a9e83d4 100644 --- a/test/integration/Makefile.am +++ b/test/integration/Makefile.am @@ -16,7 +16,7 @@ AM_CPPFLAGS += \ -DNDEBUG endif -check_PROGRAMS = basic.tests +check_PROGRAMS = basic.tests qbittorrent.tests TESTS = $(check_PROGRAMS) #if ENABLE_MEMCHECK @@ -31,5 +31,9 @@ basic_tests_SOURCES = \ $(common_SOURCES) \ basic.tests.c +qbittorrent_tests_SOURCES = \ + $(common_SOURCES) \ + qbittorrent.tests.c + clean-local: -rm -rf $(TEST_DIRECTORY) diff --git a/test/integration/qbittorrent.tests.c b/test/integration/qbittorrent.tests.c new file mode 100644 index 0000000..4924fdb --- /dev/null +++ b/test/integration/qbittorrent.tests.c @@ -0,0 +1,34 @@ +#include + +int main(); +void qbittorrent_integration_test(); + +int main() { + setup_env(); + + qbittorrent_integration_test(); + + reset_env(); + + return EXIT_SUCCESS; +} + +#define QBITTORRENT_PORT "9634" + +void qbittorrent_integration_test() { + struct addrinfo hints, *res; + int sock_fd; + + memset(&hints,0,sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + + getaddrinfo("127.0.0.1",QBITTORRENT_PORT,&hints,&res); + + sock_fd = socket(res->ai_family,res->ai_socktype,res->ai_protocol); + assert(sock_fd>0); + + assert(0==connect(sock_fd,res->ai_addr,res->ai_addrlen)); + + close(sock_fd); +} diff --git a/test/integration/test_utils.h b/test/integration/test_utils.h index 5df4ace..1d9ee1b 100644 --- a/test/integration/test_utils.h +++ b/test/integration/test_utils.h @@ -4,9 +4,11 @@ #include #include +#include #include #include #include +#include #include #include #include -- 2.30.2 From beb575f69ddbe89c32e3432e58bb34e31ef6fda2 Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 23 Jul 2022 10:15:02 -0700 Subject: [PATCH 04/16] ... --- Makefile.am | 2 +- inc/peer.h | 4 +-- inc/session.h | 3 ++- src/peer/handshake.c | 15 ++++------- src/peer/id.c | 7 +++++ src/session.c | 2 ++ test/integration/Makefile.am | 9 ++++++- test/integration/qbittorrent.tests.c | 40 +++++++++++++++++++++++----- 8 files changed, 61 insertions(+), 21 deletions(-) create mode 100644 src/peer/id.c diff --git a/Makefile.am b/Makefile.am index f54c93c..6273299 100644 --- a/Makefile.am +++ b/Makefile.am @@ -112,7 +112,7 @@ SUBDIRS = . test/unit test/integration bench # recipes for integration/unit testing # NOTE: integration requires all target -integration: all +integration: $(MAKE) -C test/integration check unit: $(MAKE) -C test/unit check diff --git a/inc/peer.h b/inc/peer.h index 323ea66..b2b7b8d 100644 --- a/inc/peer.h +++ b/inc/peer.h @@ -41,8 +41,6 @@ struct peer { unsigned char peer_id[PEER_PEER_ID_SIZE]; uint8_t infohash[PEER_INFOHASH_SIZE]; - struct torrent *torrent; - uint8_t *bitfield; size_t bitfield_size; @@ -87,6 +85,7 @@ int peer_choke(struct peer*); int peer_choke_received(struct peer*,ssize_t); void peer_free(struct peer*); int peer_handshake(struct peer*); +int peer_handshake_received(struct peer*); int peer_hash_reject(struct peer*); int peer_hash_reject_received(struct peer*,ssize_t); int peer_hash_request(struct peer*); @@ -95,6 +94,7 @@ int peer_hashes(struct peer*); int peer_hashes_received(struct peer*,ssize_t); int peer_have(struct peer*,size_t); int peer_have_received(struct peer*,ssize_t); +void peer_id(void*,size_t); int peer_init(struct peer**); int peer_interested(struct peer*); int peer_interested_received(struct peer*,ssize_t); diff --git a/inc/session.h b/inc/session.h index 2b3b404..43be8c6 100644 --- a/inc/session.h +++ b/inc/session.h @@ -5,6 +5,7 @@ #include #include +#include #include struct session_torrents { @@ -14,7 +15,7 @@ struct session_torrents { struct session { struct session_torrents torrents; - unsigned char peer_id[20]; + unsigned char peer_id[PEER_PEER_ID_SIZE]; }; extern struct session session; diff --git a/src/peer/handshake.c b/src/peer/handshake.c index 98d44f7..a85567f 100644 --- a/src/peer/handshake.c +++ b/src/peer/handshake.c @@ -5,13 +5,11 @@ #define HEADER_LENGTH HANDSHAKE_STRING_LENGTH+PEER_INFOHASH_SIZE+PEER_PEER_ID_SIZE -static int peer_handshake_receive(struct peer*); - int peer_handshake(struct peer *info) { char header[HEADER_LENGTH] = HANDSHAKE_STRING; - if(peer_handshake_receive(info)<0) { return -1; } - if(!info->handshake) { return 0; } +// if(peer_handshake_receive(info)<0) { return -1; } +// if(!info->handshake) { return 0; } // copy infohash to buffer memcpy( @@ -19,10 +17,10 @@ int peer_handshake(struct peer *info) { info->infohash, PEER_INFOHASH_SIZE); - // copy peer id to buffer + // copy session->peer_id to buffer memcpy( &(header[HANDSHAKE_STRING_LENGTH+PEER_INFOHASH_SIZE]), - info->peer_id, + session.peer_id, PEER_PEER_ID_SIZE); if(net_send(info,header,HEADER_LENGTH)<0) { return -1; } @@ -30,7 +28,7 @@ int peer_handshake(struct peer *info) { return 1; } -static int peer_handshake_receive(struct peer *info) { +int peer_handshake_received(struct peer *info) { char header[HEADER_LENGTH]; int i; @@ -48,9 +46,6 @@ static int peer_handshake_receive(struct peer *info) { &(header[HANDSHAKE_STRING_LENGTH+PEER_INFOHASH_SIZE]), PEER_INFOHASH_SIZE); - info->torrent = session_find_torrent(info->infohash,PEER_INFOHASH_SIZE); - if(NULL==info->torrent) { return -1; } - info->handshake = 1; return 1; diff --git a/src/peer/id.c b/src/peer/id.c new file mode 100644 index 0000000..7df5682 --- /dev/null +++ b/src/peer/id.c @@ -0,0 +1,7 @@ +#include + +void peer_id(void *buf, size_t size) { + assert(PEER_PEER_ID_SIZE==size); + + randombytes_buf(buf,PEER_PEER_ID_SIZE); +} diff --git a/src/session.c b/src/session.c index a9659b6..f1fec05 100644 --- a/src/session.c +++ b/src/session.c @@ -38,6 +38,8 @@ int session_init() { int session_setup() { if(session_init(&session)<0) { return -1; } + peer_id(session.peer_id,PEER_PEER_ID_SIZE); + if(0!=atexit(&session_clean)) { perror("atexit"); return -1; diff --git a/test/integration/Makefile.am b/test/integration/Makefile.am index a9e83d4..4706590 100644 --- a/test/integration/Makefile.am +++ b/test/integration/Makefile.am @@ -33,7 +33,14 @@ basic_tests_SOURCES = \ qbittorrent_tests_SOURCES = \ $(common_SOURCES) \ - qbittorrent.tests.c + qbittorrent.tests.c \ + $(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/handshake.c \ + $(top_srcdir)/src/peer/id.c \ + $(top_srcdir)/src/peer/init.c clean-local: -rm -rf $(TEST_DIRECTORY) diff --git a/test/integration/qbittorrent.tests.c b/test/integration/qbittorrent.tests.c index 4924fdb..0e1b340 100644 --- a/test/integration/qbittorrent.tests.c +++ b/test/integration/qbittorrent.tests.c @@ -1,13 +1,18 @@ #include +#include + int main(); void qbittorrent_integration_test(); +/* mocks and structs */ +struct session session; +int session_setup(); + int main() { setup_env(); qbittorrent_integration_test(); - reset_env(); return EXIT_SUCCESS; @@ -17,7 +22,10 @@ int main() { void qbittorrent_integration_test() { struct addrinfo hints, *res; - int sock_fd; + struct peer *info; + const char infohash[] = {0x25, 0x11, 0xdf, 0x9f, 0x74, 0xdc, 0x6f, 0xc1, 0x8e, 0xc9, 0x46, 0x6c, 0xdd, 0x52, 0xf5, 0xa1, 0x83, 0x27, 0xe2, 0x6e}; + + assert(1==peer_init(&info)); memset(&hints,0,sizeof(hints)); hints.ai_family = AF_UNSPEC; @@ -25,10 +33,30 @@ void qbittorrent_integration_test() { getaddrinfo("127.0.0.1",QBITTORRENT_PORT,&hints,&res); - sock_fd = socket(res->ai_family,res->ai_socktype,res->ai_protocol); - assert(sock_fd>0); + info->sock = socket(res->ai_family,res->ai_socktype,res->ai_protocol); + assert(info->sock>0); + + assert(0==connect(info->sock,res->ai_addr,res->ai_addrlen)); + + assert(1==session_setup()); + memcpy(info->infohash,infohash,PEER_INFOHASH_SIZE); + + assert(1==peer_handshake(info)); + assert(1==peer_handshake_received(info)); + + assert(1==peer_bitfield(info)); + + close(info->sock); + peer_free(info); +} + +int session_init() { + return 1; +} - assert(0==connect(sock_fd,res->ai_addr,res->ai_addrlen)); +int session_setup() { + assert(1==session_init(&session)); + peer_id(session.peer_id,PEER_PEER_ID_SIZE); - close(sock_fd); + return 1; } -- 2.30.2 From debbfd803a94ff3458a9ae98e24c74ddf6ce0f33 Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 23 Jul 2022 10:19:02 -0700 Subject: [PATCH 05/16] ... --- test/integration/qbittorrent.tests.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/integration/qbittorrent.tests.c b/test/integration/qbittorrent.tests.c index 0e1b340..a51fe54 100644 --- a/test/integration/qbittorrent.tests.c +++ b/test/integration/qbittorrent.tests.c @@ -36,7 +36,9 @@ void qbittorrent_integration_test() { info->sock = socket(res->ai_family,res->ai_socktype,res->ai_protocol); assert(info->sock>0); - assert(0==connect(info->sock,res->ai_addr,res->ai_addrlen)); + if(0!=connect(info->sock,res->ai_addr,res->ai_addrlen)) { + exit(77); + } assert(1==session_setup()); memcpy(info->infohash,infohash,PEER_INFOHASH_SIZE); @@ -44,8 +46,6 @@ void qbittorrent_integration_test() { assert(1==peer_handshake(info)); assert(1==peer_handshake_received(info)); - assert(1==peer_bitfield(info)); - close(info->sock); peer_free(info); } -- 2.30.2 From b1a3392f14e9161e3364bef1a7fd459a3adff3e9 Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 23 Jul 2022 12:22:43 -0700 Subject: [PATCH 06/16] ... --- 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 From 0e904b482c684cf7e1b00af1041bf2add743919f Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 23 Jul 2022 12:23:49 -0700 Subject: [PATCH 07/16] ... --- test/integration/Makefile.am | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/integration/Makefile.am b/test/integration/Makefile.am index 9550eeb..22d58d7 100644 --- a/test/integration/Makefile.am +++ b/test/integration/Makefile.am @@ -16,8 +16,7 @@ AM_CPPFLAGS += \ -DNDEBUG endif -#check_PROGRAMS = basic.tests qbittorrent.tests -check_PROGRAMS = qbittorrent.tests +check_PROGRAMS = basic.tests qbittorrent.tests TESTS = $(check_PROGRAMS) #if ENABLE_MEMCHECK -- 2.30.2 From 48eded37712ecce0411800aeb358ffaf806a82a0 Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 28 Jul 2022 11:47:28 -0700 Subject: [PATCH 08/16] ... --- inc/net.h | 6 ++++++ src/peer/hash.c | 7 ++++++- test/integration/Makefile.am | 4 +++- test/integration/qbittorrent.tests.c | 10 ++++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/inc/net.h b/inc/net.h index 1bee9dd..95ac067 100644 --- a/inc/net.h +++ b/inc/net.h @@ -43,6 +43,12 @@ struct net_info { (x[2]<<8) + \ x[3] +#define UINT32_T_TO_BYTES(x,buf) \ + buf[0] = (uint8_t) (x >> 24); \ + buf[1] = (uint8_t) (x >> 16 & 0xff); \ + buf[2] = (uint8_t) (x >> 8 & 0xff); \ + buf[3] = (uint8_t) (x & 0xff); + int net_cache(void**,size_t*,void*,size_t); ssize_t net_expected(struct peer*,enum peer_message*); void net_handler(int,struct peer*); diff --git a/src/peer/hash.c b/src/peer/hash.c index 88574d3..efe1b43 100644 --- a/src/peer/hash.c +++ b/src/peer/hash.c @@ -1,6 +1,11 @@ #include -int peer_hash_request(struct peer *info) { +#define HASH_REQUEST_LENGTH crypto_hash_sha256_BYTES+4+4+4+4 +int peer_hash_request(struct peer *info,const struct hash_request *request) { + unsigned char buf[HASH_REQUEST_LENGTH]; + + + return -1; } diff --git a/test/integration/Makefile.am b/test/integration/Makefile.am index 22d58d7..94b93aa 100644 --- a/test/integration/Makefile.am +++ b/test/integration/Makefile.am @@ -40,10 +40,12 @@ qbittorrent_tests_SOURCES = \ $(top_srcdir)/src/net/send.c \ $(top_srcdir)/src/net/wait.c \ $(top_srcdir)/src/peer/bitfield.c \ + $(top_srcdir)/src/peer/choke.c \ $(top_srcdir)/src/peer/free.c \ $(top_srcdir)/src/peer/handshake.c \ $(top_srcdir)/src/peer/id.c \ - $(top_srcdir)/src/peer/init.c + $(top_srcdir)/src/peer/init.c \ + $(top_srcdir)/src/peer/interest.c clean-local: -rm -rf $(TEST_DIRECTORY) diff --git a/test/integration/qbittorrent.tests.c b/test/integration/qbittorrent.tests.c index f1fef6f..28543b7 100644 --- a/test/integration/qbittorrent.tests.c +++ b/test/integration/qbittorrent.tests.c @@ -60,6 +60,16 @@ void qbittorrent_integration_test() { assert(1==peer_bitfield_received(info,i)); + assert(1==peer_choke(info)); + assert(1==peer_unchoke(info)); + + assert(1==peer_not_interested(info)); + assert(1==peer_interested(info)); + + assert(1==peer_keepalive(info)); + + assert(1==peer_hash_request(info)); + close(info->sock); peer_free(info); } -- 2.30.2 From 97f678b9405478da31cc42eae6aefb6e99ef22b3 Mon Sep 17 00:00:00 2001 From: alex Date: Sun, 31 Jul 2022 00:35:49 -0700 Subject: [PATCH 09/16] ... --- inc/peer.h | 2 +- src/peer/hash.c | 12 ++++++++++-- test/integration/Makefile.am | 6 +++++- test/integration/qbittorrent.tests.c | 3 ++- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/inc/peer.h b/inc/peer.h index c2537ca..7eae50f 100644 --- a/inc/peer.h +++ b/inc/peer.h @@ -89,7 +89,7 @@ int peer_handshake(struct peer*); int peer_handshake_received(struct peer*); int peer_hash_reject(struct peer*); int peer_hash_reject_received(struct peer*,ssize_t); -int peer_hash_request(struct peer*); +int peer_hash_request(struct peer*, const struct hash_request*); int peer_hash_request_received(struct peer*,ssize_t); int peer_hashes(struct peer*); int peer_hashes_received(struct peer*,ssize_t); diff --git a/src/peer/hash.c b/src/peer/hash.c index efe1b43..ff92cba 100644 --- a/src/peer/hash.c +++ b/src/peer/hash.c @@ -1,12 +1,20 @@ #include #define HASH_REQUEST_LENGTH crypto_hash_sha256_BYTES+4+4+4+4 + int peer_hash_request(struct peer *info,const struct hash_request *request) { unsigned char buf[HASH_REQUEST_LENGTH]; - + memcpy(buf,request->pieces_root,crypto_hash_sha256_BYTES); - return -1; + UINT32_T_TO_BYTES(request->base_layer,(&(buf[crypto_hash_sha256_BYTES]))); + UINT32_T_TO_BYTES(request->index,(&(buf[crypto_hash_sha256_BYTES+4]))); + UINT32_T_TO_BYTES(request->length,(&(buf[crypto_hash_sha256_BYTES+8]))); + UINT32_T_TO_BYTES(request->proof_layers,(&(buf[crypto_hash_sha256_BYTES+12]))); + + if(net_send(info,buf,HASH_REQUEST_LENGTH)<0) { return -1; } + + return 1; } int peer_hash_request_received(struct peer *info, ssize_t len) { diff --git a/test/integration/Makefile.am b/test/integration/Makefile.am index 94b93aa..d41168f 100644 --- a/test/integration/Makefile.am +++ b/test/integration/Makefile.am @@ -43,9 +43,13 @@ qbittorrent_tests_SOURCES = \ $(top_srcdir)/src/peer/choke.c \ $(top_srcdir)/src/peer/free.c \ $(top_srcdir)/src/peer/handshake.c \ + $(top_srcdir)/src/peer/hash.c \ $(top_srcdir)/src/peer/id.c \ $(top_srcdir)/src/peer/init.c \ - $(top_srcdir)/src/peer/interest.c + $(top_srcdir)/src/peer/interest.c \ + $(top_srcdir)/src/peer/keepalive.c \ + $(top_srcdir)/src/peer/piece.c \ + $(top_srcdir)/src/peer/request.c clean-local: -rm -rf $(TEST_DIRECTORY) diff --git a/test/integration/qbittorrent.tests.c b/test/integration/qbittorrent.tests.c index 28543b7..cba1e42 100644 --- a/test/integration/qbittorrent.tests.c +++ b/test/integration/qbittorrent.tests.c @@ -23,6 +23,7 @@ int main() { void qbittorrent_integration_test() { struct addrinfo hints, *res; struct peer *info; + struct hash_request req; enum peer_message type; ssize_t i; @@ -68,7 +69,7 @@ void qbittorrent_integration_test() { assert(1==peer_keepalive(info)); - assert(1==peer_hash_request(info)); + assert(1==peer_hash_request(info,&req)); close(info->sock); peer_free(info); -- 2.30.2 From 9005ddfc574c569ca3bc8e6a01ab9d17f7cc2043 Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 1 Aug 2022 18:13:55 -0700 Subject: [PATCH 10/16] ... --- test/integration/Makefile.am | 8 +++- test/integration/qbittorrent.tests.c | 63 ++++++++++++++++++++++------ 2 files changed, 57 insertions(+), 14 deletions(-) diff --git a/test/integration/Makefile.am b/test/integration/Makefile.am index d41168f..4966466 100644 --- a/test/integration/Makefile.am +++ b/test/integration/Makefile.am @@ -34,6 +34,10 @@ basic_tests_SOURCES = \ qbittorrent_tests_SOURCES = \ $(common_SOURCES) \ qbittorrent.tests.c \ + $(top_srcdir)/src/block.c \ + $(top_srcdir)/src/bencode/encode.c \ + $(top_srcdir)/src/hash.c \ + $(top_srcdir)/src/hashmap.c \ $(top_srcdir)/src/net/cache.c \ $(top_srcdir)/src/net/expected.c \ $(top_srcdir)/src/net/peek.c \ @@ -44,12 +48,14 @@ qbittorrent_tests_SOURCES = \ $(top_srcdir)/src/peer/free.c \ $(top_srcdir)/src/peer/handshake.c \ $(top_srcdir)/src/peer/hash.c \ + $(top_srcdir)/src/peer/hashes.c \ $(top_srcdir)/src/peer/id.c \ $(top_srcdir)/src/peer/init.c \ $(top_srcdir)/src/peer/interest.c \ $(top_srcdir)/src/peer/keepalive.c \ $(top_srcdir)/src/peer/piece.c \ - $(top_srcdir)/src/peer/request.c + $(top_srcdir)/src/peer/request.c \ + $(top_srcdir)/src/torrent/piece.c clean-local: -rm -rf $(TEST_DIRECTORY) diff --git a/test/integration/qbittorrent.tests.c b/test/integration/qbittorrent.tests.c index cba1e42..b6da3ea 100644 --- a/test/integration/qbittorrent.tests.c +++ b/test/integration/qbittorrent.tests.c @@ -23,13 +23,32 @@ int main() { void qbittorrent_integration_test() { struct addrinfo hints, *res; struct peer *info; - struct hash_request req; enum peer_message type; + struct hash_request req; 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; + /* + * Test uses the following torrent: + * name: bittorrent-v2-test + * infov2: caf1e1c30e81cb361b9ee167c4aa64228a7fa4fa9f6105232b28ad099f3a302e + * + * Files: + * '13.Popsy Team - ViP 2.vob.mp4' + * 'cncd_fairlight-ceasefire_(all_falls_down)-1080p.mp4' + * elevated_4000.avi + * 'Struct by Outracks (FullHD 1080p HQ demoscene).mov' + * asd-rupture.mp4 + * 'crionics & silents - hardwired (1991, hpad, divx5).avi' + * 'luma - mercury _ 64k _ Final.mp4' + * tbl-starstruck-2006.avi + * 'Chameleon by ASD (female voice).mov' + * 'Darkroom (Stellar, 1994, Amiga ECS) HQ.mp4' + * readme.txt + */ + const unsigned char infohash[] = {0xca, 0xf1, 0xe1, 0xc3, 0x0e, 0x81, 0xcb, 0x36, 0x1b, 0x9e, 0xe1, 0x67, 0xc4, 0xaa, 0x64, 0x22, 0x8a, 0x7f, 0xa4, 0xfa, 0x9f, 0x61, 0x05, 0x23, 0x2b, 0x28, 0xad, 0x09, 0x9f, 0x3a, 0x30, 0x2e}; +// const unsigned char readme_txt_pieces_root[] = { 0x3b, 0x3c, 0x50, 0xa1, 0x2e, 0x27, 0xa6, 0xb3, 0x42, 0x1b, 0x81, 0x7a, 0xfd, 0x49, 0xdf, 0xa0, 0xd5, 0x4f, 0x69, 0xa0, 0x86, 0xcf, 0x99, 0x14, 0x16, 0x4a, 0x9f, 0x51, 0x6e, 0x74, 0x16, 0xe4}; + const unsigned char tbl_starstruck_2006_avi_pieces_root[] = {0xeb, 0x3f, 0xde, 0x5f, 0xce, 0xb8, 0xed, 0xa2, 0x55, 0xdb, 0x2f, 0x21, 0x8a, 0x0c, 0xec, 0xac, 0x95, 0x71, 0x75, 0xcd, 0x54, 0x4a, 0x83, 0xb0, 0x28, 0x65, 0x0f, 0xd4, 0x44, 0x7a, 0x17, 0x91}; + const size_t pieces = 371; assert(1==peer_init(&info)); @@ -52,24 +71,42 @@ void qbittorrent_integration_test() { assert(1==peer_handshake(info)); assert(1==peer_handshake_received(info)); + assert(1==peer_interested(info)); + assert(1==peer_unchoke(info)); + + /* + * skipping (un)choke, (not)interested, keepalive, etc. + */ assert(1==peer_bitfield(info)); i = net_expected(info,&type); - assert(i>=0); + assert(i==52); assert(PEER_MESSAGE_BITFIELD==type); - assert(1==peer_bitfield_received(info,i)); - assert(1==peer_choke(info)); - assert(1==peer_unchoke(info)); - - assert(1==peer_not_interested(info)); - assert(1==peer_interested(info)); + i = net_expected(info,&type); + assert(i==5); + assert(PEER_MESSAGE_UNCHOKE==type); + assert(1==peer_unchoke_received(info,i)); + + memset(&req,0,sizeof(req)); + memcpy(req.pieces_root,tbl_starstruck_2006_avi_pieces_root,32); + req.base_layer = 0; + req.index = 1; + req.length = 1; + req.proof_layers = 1; + assert(1==peer_hash_request(info,&req)); - assert(1==peer_keepalive(info)); + i = net_expected(info,&type); + assert(i==5); + assert(PEER_MESSAGE_UNCHOKE==type); + assert(1==peer_unchoke_received(info,i)); - assert(1==peer_hash_request(info,&req)); + i = net_expected(info,&type); + assert(i>0); + assert(PEER_MESSAGE_HASHES==type); + assert(1==peer_hashes_received(info,i)); close(info->sock); peer_free(info); -- 2.30.2 From 266c03aa5bd7e3c8b56caa2e072b11f84ede670a Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 9 Aug 2022 17:08:18 -0700 Subject: [PATCH 11/16] ... --- test/integration/qbittorrent.tests.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/qbittorrent.tests.c b/test/integration/qbittorrent.tests.c index b6da3ea..0cd39d3 100644 --- a/test/integration/qbittorrent.tests.c +++ b/test/integration/qbittorrent.tests.c @@ -94,7 +94,7 @@ void qbittorrent_integration_test() { memcpy(req.pieces_root,tbl_starstruck_2006_avi_pieces_root,32); req.base_layer = 0; req.index = 1; - req.length = 1; + req.length = 2; req.proof_layers = 1; assert(1==peer_hash_request(info,&req)); -- 2.30.2 From 5f12be2d30b03c1b756ce0ce01c49d2f17baab5a Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 28 Oct 2022 15:33:32 -0700 Subject: [PATCH 12/16] ... --- manifest.scm | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 manifest.scm diff --git a/manifest.scm b/manifest.scm new file mode 100644 index 0000000..bf78772 --- /dev/null +++ b/manifest.scm @@ -0,0 +1,4 @@ +(specifications->manifest + (list "autoconf" + "automake" + "libsodium")) -- 2.30.2 From dffd77faf61c76262fd0f6ac3cfa0af3f0198477 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 28 Oct 2022 16:19:30 -0700 Subject: [PATCH 13/16] ... --- guix.scm | 26 ++++++++++++++++++++++++++ manifest.scm | 4 ---- 2 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 guix.scm delete mode 100644 manifest.scm diff --git a/guix.scm b/guix.scm new file mode 100644 index 0000000..7283fca --- /dev/null +++ b/guix.scm @@ -0,0 +1,26 @@ +(use-modules (guix packages) + (guix build-system gnu) + (guix git-download) + (gnu packages autotools)) + +(define %source-dir (dirname (current-filename))) + +(define seeder + (package + (name "seeder") + (version "git") + (source (local-file %source-dir + #:recursive? #t + #:select? (git-predicate %source-dir))) + (build-system gnu-build-system) + (native-inputs + `(("autoconf", autoconf) + ("automake", automake))) + (inputs + `(("libsodium", libsodium))) + (home-page "https://git.infiniteadaptability.org/seeder") + (synopsis "bittorrent client") + (description "v2 bittorrent client") + (license public-domain))) + +seeder diff --git a/manifest.scm b/manifest.scm deleted file mode 100644 index bf78772..0000000 --- a/manifest.scm +++ /dev/null @@ -1,4 +0,0 @@ -(specifications->manifest - (list "autoconf" - "automake" - "libsodium")) -- 2.30.2 From 9cc7895b111f6eeb32c85af6b324a47df266db95 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 28 Oct 2022 16:20:39 -0700 Subject: [PATCH 14/16] ... --- guix.scm | 1 + 1 file changed, 1 insertion(+) diff --git a/guix.scm b/guix.scm index 7283fca..eb91e21 100644 --- a/guix.scm +++ b/guix.scm @@ -1,6 +1,7 @@ (use-modules (guix packages) (guix build-system gnu) (guix git-download) + (guix licenses) (gnu packages autotools)) (define %source-dir (dirname (current-filename))) -- 2.30.2 From cbbc5caea63f63eb90a7f960682cca76f24e2612 Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 29 Oct 2022 01:52:07 -0700 Subject: [PATCH 15/16] ... --- guix.scm | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/guix.scm b/guix.scm index eb91e21..806b4f5 100644 --- a/guix.scm +++ b/guix.scm @@ -1,8 +1,10 @@ -(use-modules (guix packages) +(use-modules (gnu packages autotools) + (gnu packages libsodium) + (guix packages) (guix build-system gnu) (guix git-download) - (guix licenses) - (gnu packages autotools)) + (guix gexp) + (guix licenses)) (define %source-dir (dirname (current-filename))) @@ -16,9 +18,9 @@ (build-system gnu-build-system) (native-inputs `(("autoconf", autoconf) - ("automake", automake))) + ("automake", automake))) (inputs - `(("libsodium", libsodium))) + `("libsodium", libsodium)) (home-page "https://git.infiniteadaptability.org/seeder") (synopsis "bittorrent client") (description "v2 bittorrent client") -- 2.30.2 From fddcbfe3d2b2a22b2d614cfb6f6c3966a8765ef5 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 1 Nov 2022 22:10:59 -0700 Subject: [PATCH 16/16] ... --- inc/bencode.h | 30 ------- src/bencode/decode.c | 67 -------------- src/bencode/encode.c | 95 -------------------- test/unit/bencode.tests.c | 179 -------------------------------------- 4 files changed, 371 deletions(-) delete mode 100644 inc/bencode.h delete mode 100644 src/bencode/decode.c delete mode 100644 src/bencode/encode.c delete mode 100644 test/unit/bencode.tests.c diff --git a/inc/bencode.h b/inc/bencode.h deleted file mode 100644 index e5b1008..0000000 --- a/inc/bencode.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef __BENCODE_H_ -#define __BENCODE_H_ - -#include -#include -#include -#include -#include -#include - -enum bencode_type { - BENCODE_DICTIONARY, - BENCODE_INTEGER, - BENCODE_LIST, - BENCODE_STRING -}; - -ssize_t bdecode_int(const uint8_t*,size_t,long long int*); -ssize_t bdecode_string(const uint8_t*,size_t,uint8_t*,size_t); -enum bencode_type bdecode_type(const uint8_t*,size_t); -ssize_t bencode_dict_end(uint8_t*,size_t); -ssize_t bencode_dict_start(uint8_t*,size_t); -ssize_t bencode_int(long long int,uint8_t*,size_t); -ssize_t bencode_list_end(uint8_t*,size_t); -ssize_t bencode_list_start(uint8_t*,size_t); -ssize_t bencode_size(size_t,uint8_t*,size_t); -size_t bencode_size_int(intmax_t); -ssize_t bencode_string(const uint8_t*,size_t,uint8_t*,size_t); - -#endif diff --git a/src/bencode/decode.c b/src/bencode/decode.c deleted file mode 100644 index 8963eee..0000000 --- a/src/bencode/decode.c +++ /dev/null @@ -1,67 +0,0 @@ -#include - -ssize_t bdecode_int(const uint8_t *buf, size_t buf_len, long long int *output) { - if(NULL==buf) { return -1; } - if(buf_len<=0) { return -1; } - if(NULL==output) { return -1; } - - const uint8_t *p = NULL; - for(size_t i=0;i3)) { return -1; } - if(buf[i]=='e') { - p = &(buf[i]); - } - } - - if(NULL==p) { return -1; } - - ssize_t len = p - (buf+1); - uint8_t *num_string = (uint8_t*) strndup((const char*)&(buf[1]),len); - *output = atoll((char *)num_string); - free(num_string); - - len += 2; - - return len; -} - -ssize_t bdecode_string(const uint8_t *buf, size_t buf_len, uint8_t *output, size_t output_size) { - if(NULL==buf) { return -1; } - if(buf_len<=0) { return -1; } - if(NULL==output) { return -1; } - - const uint8_t *p = NULL; - for(size_t i=0;ioutput_size)) { return -1; } - - p++; - memcpy(output,p,str_len); - - return len; -} - -enum bencode_type bdecode_type(const uint8_t *buf, size_t len) { - switch(buf[0]) { - case 'i': - return BENCODE_INTEGER; - case 'l': - return BENCODE_LIST; - case 'd': - return BENCODE_DICTIONARY; - default: - return BENCODE_STRING; - } -} diff --git a/src/bencode/encode.c b/src/bencode/encode.c deleted file mode 100644 index ef1ccd1..0000000 --- a/src/bencode/encode.c +++ /dev/null @@ -1,95 +0,0 @@ -#include - -ssize_t bencode_dict_end(uint8_t *output, size_t output_size) { - if(NULL==output) { return -1; } - if(output_size<1) { return -1; } - - output[0] = 'e'; - - return 1; -} - -ssize_t bencode_dict_start(uint8_t *output, size_t output_size) { - if(NULL==output) { return -1; } - if(output_size<1) { return -1; } - - output[0] = 'd'; - - return 1; -} - -ssize_t bencode_int(long long int to_encode, uint8_t *output, size_t output_size) { - size_t i; - int ret; - - if(NULL==output) { return -1; } - - i = bencode_size_int(to_encode); - i += 2; - - if((ret = snprintf((char*)output,output_size,"i%llue",to_encode))<0) { return -1; } - if(ret!=i) { return -1; } - - return i; -} - -ssize_t bencode_list_end(uint8_t *output, size_t output_size) { - if(NULL==output) { return -1; } - if(output_size<=1) { return -1; } - - output[0] = 'e'; - - return 1; -} - -ssize_t bencode_list_start(uint8_t *output, size_t output_size) { - if(NULL==output) { return -1; } - if(output_size<=1) { return -1; } - - output[0] = 'l'; - - return 1; -} - -ssize_t bencode_size(size_t len, uint8_t *output, size_t output_size) { - size_t i; - - if(len>INTMAX_MAX) { return -1; } - - i = bencode_size_int(len); - i++; // account for ':' - if(i>output_size) { return -1; } - - // snprintf requires space for '\0' otherwise will truncate - if(snprintf((char*)output,i+1,"%lu:",len)!=i) { return -1; } - - return i; -} - -size_t bencode_size_int(intmax_t i) { - size_t j = (i<0)?1:0; - - i = imaxabs(i); - if(0==i) { return 1; } - - while(i>0) { - i /= 10; - j++; - } - return j; -} - -ssize_t bencode_string(const uint8_t *str, size_t len, uint8_t *output, size_t output_size) { - size_t i; - - if(NULL==str) { return -1; } - if(NULL==output) { return -1; } - - if((i = bencode_size(len,output,output_size))<0) { return -1; } - - if(i+len>output_size) { return -1; } - - memcpy(&(output[i]),str,len); - - return i+len; -} diff --git a/test/unit/bencode.tests.c b/test/unit/bencode.tests.c deleted file mode 100644 index d8723d0..0000000 --- a/test/unit/bencode.tests.c +++ /dev/null @@ -1,179 +0,0 @@ -#include - -#include - -int main(); -static void bdecode_integer_basic_test(); -static void bdecode_string_basic_test(); -static void bdecode_type_basic_test(); -static void bencode_dict_basic_test(); -static void bencode_list_basic_test(); -static void bencode_size_basic_test(); -static void bencode_size_int_basic_test(); -static void bencode_string_basic_test(); - -int main() { - setup_env(); - - bdecode_integer_basic_test(); - bdecode_string_basic_test(); - bdecode_type_basic_test(); - bencode_dict_basic_test(); - bencode_list_basic_test(); - bencode_size_basic_test(); - bencode_size_int_basic_test(); - bencode_string_basic_test(); - - clean_env(); - - return EXIT_SUCCESS; -} - -static void bdecode_integer_basic_test() { - uint8_t str1[] = "i10e"; - uint8_t str2[] = "i0e"; - uint8_t str3[] = "i0023423e"; - uint8_t str4[] = "i10928390128301e"; - long long int i; - - assert(bdecode_int(NULL,sizeof(str1)-1,&i)==-1); - assert(bdecode_int(str1,0,&i)==-1); - assert(bdecode_int(str1,sizeof(str1)-1,NULL)==-1); - assert(bdecode_int(str1,sizeof(str1)-1,&i)==4); - assert(i==10); - - assert(bdecode_int(str2,sizeof(str2)-1,&i)==3); - assert(i==0); - - assert(bdecode_int(str3,sizeof(str3)-1,&i)==-1); - - assert(bdecode_int(str4,sizeof(str4)-1,&i)==16); - assert(i==10928390128301); -} - -static void bdecode_string_basic_test() { - uint8_t buf[1024]; - uint8_t str1[] = "4:spam"; - - assert(bdecode_string(NULL,sizeof(str1)-1,buf,1024)==-1); - assert(bdecode_string(str1,0,buf,1024)==-1); - assert(bdecode_string(str1,sizeof(str1)-1,NULL,1024)==-1); - assert(bdecode_string(str1,sizeof(str1)-1,buf,0)==-1); - assert(bdecode_string(str1,sizeof(str1)-1,buf,3)==-1); - assert(bdecode_string(str1,sizeof(str1)-1,buf,4)==4); - assert(bdecode_string(str1,sizeof(str1)-1,buf,1024)==4); - assert(memcmp(buf,"spam",4)==0); -} - -static void bdecode_type_basic_test() { - uint8_t str1[] = "4:spam"; - uint8_t str2[] = "i12093e"; - uint8_t str3[] = "l4:spam4:eggse"; - uint8_t str4[] = "d3:cow3:moo4:spam4:eggse"; - - assert(BENCODE_STRING==bdecode_type(str1,sizeof(str1)-1)); - assert(BENCODE_INTEGER==bdecode_type(str2,sizeof(str2)-1)); - assert(BENCODE_LIST==bdecode_type(str3,sizeof(str3)-1)); - assert(BENCODE_DICTIONARY==bdecode_type(str4,sizeof(str4)-1)); -} - -static void bencode_dict_basic_test() { - uint8_t buf[1024]; - uint8_t *p; - - memset(buf,0,1024); - - p = buf; - assert(bencode_dict_start(NULL,1024)==-1); - assert(bencode_dict_start(p,0)==-1); - assert(bencode_dict_start(p,1024)==1); - p++; - - uint8_t str1[] = "cow"; - assert(bencode_string(str1,3,p,1023)==5); - p += 5; - - uint8_t str2[] = "moo"; - assert(bencode_string(str2,3,p,1018)==5); - p += 5; - - uint8_t str3[] = "spam"; - assert(bencode_string(str3,4,p,1013)==6); - p += 6; - - uint8_t str4[] = "eggs"; - assert(bencode_string(str4,4,p,1007)==6); - p += 6; - - assert(bencode_dict_end(NULL,1001)==-1); - assert(bencode_dict_end(p,0)==-1); - assert(bencode_dict_end(p,1001)==1); - - assert(memcmp(buf,"d3:cow3:moo4:spam4:eggse",24)==0); -} - -static void bencode_list_basic_test() { - uint8_t buf[1024]; - uint8_t *p; - - memset(buf,0,1024); - - p = buf; - assert(bencode_list_start(NULL,1024)==-1); - assert(bencode_list_start(p,0)==-1); - assert(bencode_list_start(p,1024)==1); - p++; - - uint8_t str1[] = "spam"; - assert(bencode_string(str1,4,p,1023)==6); - p += 6; - - uint8_t str2[] = "eggs"; - assert(bencode_string(str2,4,p,1017)==6); - p += 6; - - assert(bencode_list_end(NULL,1011)==-1); - assert(bencode_list_end(p,0)==-1); - assert(bencode_list_end(p,1011)==1); - - assert(memcmp(buf,"l4:spam4:eggse",14)==0); -} - -static void bencode_size_basic_test() { - uint8_t buf[1024]; - - assert(bencode_size(0,buf,1)==-1); - assert(bencode_size(0,buf,2)==2); - - assert(bencode_size(1234567890,buf,1024)==11); - - assert(bencode_size(SIZE_MAX,buf,1024)==-1); - assert(bencode_size(INTMAX_MAX,buf,1024)>0); -} - -static void bencode_size_int_basic_test() { - assert(bencode_size_int(0)==1); - assert(bencode_size_int(-1)==2); - assert(bencode_size_int(109)==3); - assert(bencode_size_int(23984723984)==11); - assert(bencode_size_int(922337203685477580)==18); - assert(bencode_size_int(INTMAX_MIN)>0); - assert(bencode_size_int(INTMAX_MAX)>0); -} - -static void bencode_string_basic_test() { - uint8_t buf[1024]; - - memset(buf,0,1024); - - uint8_t str1[] = "testlkajslfkdjasdfl test string"; - assert(bencode_string(NULL,31,buf,1024)==-1); - assert(bencode_string(str1,31,NULL,1024)==-1); - assert(bencode_string(str1,31,buf,0)==-1); - - assert(bencode_string(str1,0,buf,1024)==2); - assert(memcmp("0:",buf,2)==0); - - assert(bencode_string(str1,31,buf,1024)==34); - assert(memcmp("31:testlkajslfkdjasdfl test string",buf,34)==0); -} -- 2.30.2