From: alex Date: Thu, 14 Apr 2022 00:03:22 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=e17282e597566d1a943e862cba18f42829ffd60c;p=seeder ... --- diff --git a/inc/peer.h b/inc/peer.h index 7084bd2..323ea66 100644 --- a/inc/peer.h +++ b/inc/peer.h @@ -76,6 +76,7 @@ enum peer_message { #include #include #include +#include int peer_bitfield(struct peer*); int peer_bitfield_received(struct peer*,ssize_t); diff --git a/inc/piece.h b/inc/piece.h index 3cce33c..a37225b 100644 --- a/inc/piece.h +++ b/inc/piece.h @@ -4,6 +4,8 @@ #include #include +#include + struct piece { const char *path; uint64_t start; @@ -12,10 +14,7 @@ struct piece { size_t data_size; }; -#include - int piece_init(struct piece**); void piece_free(struct piece*); -struct piece* piece_load(const struct torrent*,size_t); #endif diff --git a/inc/torrent.h b/inc/torrent.h index 7623920..3f9478b 100644 --- a/inc/torrent.h +++ b/inc/torrent.h @@ -27,7 +27,7 @@ struct torrent { struct tree *tree; struct torrent_files files; - struct piece **pieces; + unsigned char **pieces; size_t pieces_size; uint8_t infohash[crypto_hash_sha256_BYTES]; @@ -53,6 +53,7 @@ void torrent_free(struct torrent*); int torrent_init(struct torrent**,unsigned long long); int torrent_infohash(struct torrent*); char *torrent_magnet(struct torrent*); +struct piece *torrent_piece_load(const struct torrent*,size_t); int torrent_remove(struct torrent*,const char*); #endif diff --git a/src/add.c b/src/add.c index ec71143..8d29e75 100644 --- a/src/add.c +++ b/src/add.c @@ -46,6 +46,8 @@ int add() { } static int add_find_all(struct torrent *p) { + if(NULL==p) { return -1; } + log_info(ADD_MESSAGE_ADDING_TORRENT,p->root,p->name); current_torrent = p; diff --git a/src/peer/piece.c b/src/peer/piece.c index ef6cc78..0c86b17 100644 --- a/src/peer/piece.c +++ b/src/peer/piece.c @@ -6,7 +6,7 @@ int peer_piece(struct peer *info) { p = info->piece_requests; - piece = piece_load(info->torrent,p->index); + piece = torrent_piece_load(info->torrent,p->index); if(NULL==piece) { return -1; } if( p->begin + p->length > piece->data_size ) { return -1; } diff --git a/src/piece.c b/src/piece.c index 9044829..b0c6cdb 100644 --- a/src/piece.c +++ b/src/piece.c @@ -16,6 +16,3 @@ int piece_cache_remove() { return -1; } -struct piece *piece_load(const struct torrent *torrent, size_t index) { - return NULL; -} diff --git a/src/torrent/piece.c b/src/torrent/piece.c index f5a7747..67b5565 100644 --- a/src/torrent/piece.c +++ b/src/torrent/piece.c @@ -1,9 +1,16 @@ #include -static int piece_layer_sort(const void*, const void*); +static int piece_layers_sort(const void*, const void*); static int torrent_bencode_piece_layers(FILE*,struct file*); static int torrent_piece_layers(struct torrent*); +static int piece_layers_sort(const void *p1, const void *p2) { + if(NULL==p1) { return 1; } + if(NULL==p2) { return -1; } + + return memcmp(*(unsigned char**)p1,*(unsigned char**)p2,crypto_hash_sha256_BYTES); +} + #define PIECE_LAYER_BENCODED_LENGTH 2+1+32+2+1+32 static int torrent_bencode_piece_layers(FILE *fp, struct file *file_p) { @@ -30,55 +37,50 @@ static int torrent_bencode_piece_layers(FILE *fp, struct file *file_p) { return 1; } -static int torrent_piece_layers(struct torrent *p) { +static int torrent_piece_layers(struct torrent *torrent) { struct file *file_p; - struct block *block_p; size_t index, pieces; pieces = 0; - for(size_t i=0;ifiles.roots->size;i++) { - file_p = (struct file*)p->files.roots->map[i]; + for(size_t i=0;ifiles.roots->size;i++) { + file_p = (struct file*)torrent->files.roots->map[i]; if(file_p!=NULL) { - if(file_p->size>p->piece_length) { pieces++; } + if(file_p->size>torrent->piece_length) { pieces++; } } } - // sort hashes first - qsort(hashes,pieces,sizeof(const unsigned char*),&piece_layers_sort); - - torrent->pieces = malloc(sizeof(struct piece)*pieces); + torrent->pieces = malloc(sizeof(unsigned char*)*pieces); if(NULL==torrent->pieces) { return -1; } index = 0; - for(size_t i=0;ifiles.roots->size;i++) { - file_p = (struct file*)p->files.roots->map[i]; + for(size_t i=0;ifiles.roots->size;i++) { + file_p = (struct file*)torrent->files.roots->map[i]; if(file_p!=NULL) { - if(file_p->size>p->piece_length) { - block_p = file_p->piece_layers; - while(block_p!=NULL) { - torrent->pieces[index] = file_p->root->hash; - index++; - } - } + torrent->pieces[index] = file_p->root->hash; + index++; } } + + qsort(torrent->pieces,pieces,sizeof(const unsigned char*),&piece_layers_sort); - return -1; + return 1; } int torrent_file_piece_layers(FILE *fp, struct torrent *p) { + struct file *file_p; + if(torrent_piece_layers(p)<0) { return -1; } for(size_t i=0;ipieces_size;i++) { + file_p = hashmap_find(p->files.roots,p->pieces[i],crypto_hash_sha256_BYTES); + if(NULL==file_p) { return -1; } + if(torrent_bencode_piece_layers(fp,file_p)<0) { return -1; } } return 1; } -static int piece_layers_sort(const void *p1, const void *p2) { - if(NULL==p1) { return 1; } - if(NULL==p2) { return -1; } - - return memcmp(*(unsigned char**)p1,*(unsigned char**)p2,crypto_hash_sha256_BYTES); +struct piece *torrent_piece_load(const struct torrent *torrent, size_t index) { + return NULL; } diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index e1ffe48..e06c8cc 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 net.tests opt.tests peer.tests pqueue.tests rss.tests session.tests torrent.tests tree.tests +check_PROGRAMS = add.tests 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 @@ -39,6 +39,17 @@ torrent_SOURCES = \ $(top_srcdir)/src/torrent/remove.c \ $(top_srcdir)/src/tree.c +add_tests_SOURCES = \ + $(common_SOURCES) \ + $(torrent_SOURCES) \ + add.tests.c \ + $(top_srcdir)/src/fs/filter.c \ + $(top_srcdir)/src/hash.c \ + $(top_srcdir)/src/session.c + +add_tests_CPPFLAGS = $(AM_CPPFLAGS) \ + -DADD_SRC_FILE="$(top_srcdir)/src/add.c" + bencode_tests_SOURCES = \ $(common_SOURCES) \ bencode.tests.c \ diff --git a/test/unit/add.tests.c b/test/unit/add.tests.c new file mode 100644 index 0000000..5615dd7 --- /dev/null +++ b/test/unit/add.tests.c @@ -0,0 +1,50 @@ +#include + +#include + +#include INCLUDE(ADD_SRC_FILE) + +/* dummy functions */ +struct options global_options; +void log_message(enum log_level lvl, FILE *fp, const char *format,...) { return; } +/* end dummy functions */ + +int main(); +static void add_find_all_basic_test(); +static void add_queue_resize_basic_test(); +static void add_to_queue_basic_test(); + +int main() { + setup_env(); + + add_find_all_basic_test(); + add_queue_resize_basic_test(); + add_to_queue_basic_test(); + + clean_env(); + + return EXIT_SUCCESS; +} + +static void add_find_all_basic_test() { + struct torrent *p; + + TORRENT_SETUP_EMPTY_EXAMPLE(&p); + + assert(hashmap_init(&add_queue,ADD_QUEUE_INITIAL_SIZE)==1); + current_torrent = p; + + assert(-1==add_find_all(NULL)); + assert(-1==add_find_all(NULL)); + + assert(1==add_find_all(p)); + + assert(0); +} + +static void add_queue_resize_basic_test() { + assert(0); +} +static void add_to_queue_basic_test() { + assert(0); +} diff --git a/test/unit/net.tests.c b/test/unit/net.tests.c index cf0fa50..5f8382d 100644 --- a/test/unit/net.tests.c +++ b/test/unit/net.tests.c @@ -27,6 +27,7 @@ int peer_interested_received(struct peer *info, ssize_t len) { message_count[PEE int peer_keepalive_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_KEEPALIVE+1]++; return 0; } int peer_not_interested_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_NOT_INTERESTED+1]++; return 0; } int peer_piece_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_PIECE+1]++; return 0; } +int peer_queue_process(struct peer *info) { return 1; } int peer_reject_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_REJECT+1]++; return 0; } int peer_request_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_REQUEST+1]++; return 0; } int peer_unchoke_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_UNCHOKE+1]++; return 0; } diff --git a/test/unit/opt.tests.c b/test/unit/opt.tests.c index 3b8e4da..f639c5e 100644 --- a/test/unit/opt.tests.c +++ b/test/unit/opt.tests.c @@ -74,6 +74,7 @@ static void opt_set_feed_url_basic_test() { static void opt_add_watch_basic_test() { assert(-1==opt_add_watch(NULL)); assert(-1==opt_add_watch("notarealexistingdirectorylksdjflkajsdfklajsklf")); + assert(-1==opt_add_watch("notareal/existingdi/rectorylks/djflkajsd/fklajsklf")); opt_set_log_level(LOG_LEVEL_SILENT); assert(1==session_init()); diff --git a/test/unit/test_macros.h b/test/unit/test_macros.h index fdfe3ea..2ad0ba6 100644 --- a/test/unit/test_macros.h +++ b/test/unit/test_macros.h @@ -80,4 +80,18 @@ assert(torrent_add(*p,file2)==1); \ } +/* Parameters: + * struct torrent **p + */ +#define TORRENT_SETUP_EMPTY_EXAMPLE(p) { \ + assert(torrent_init(p,16384)==1); \ +\ + (*p)->root = strdup(TEST_DIRECTORY); \ + assert((*p)->root!=NULL); \ + (*p)->name = strdup(TEST_DIRECTORY); \ + assert((*p)->name!=NULL); \ + (*p)->feed_url = strdup("https://test.com"); \ + assert((*p)->feed_url!=NULL); \ +} + #endif