From 900936108a3b2eeee860ffd148f6db5b593bb9ef Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 9 Dec 2021 17:30:56 -0800 Subject: [PATCH] ... --- src/torrent/file.c | 4 ++-- src/torrent/info.c | 2 ++ test/unit/torrent.tests.c | 31 ++++++++++++++++++++++++++++--- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/torrent/file.c b/src/torrent/file.c index aef04bd..3666f2b 100644 --- a/src/torrent/file.c +++ b/src/torrent/file.c @@ -78,7 +78,7 @@ char *torrent_file_path(unsigned char *infohash, size_t len) { // this always returns hex, unnecessary to return sodium_bin2hex(hex,(crypto_hash_sha256_BYTES<<1)+1,infohash,crypto_hash_sha256_BYTES); - p = concat(PREFIX TORRENT_FILE_DIRECTORY,hex); + p = concat(TORRENT_FILE_DIRECTORY,hex); if(NULL==p) { return NULL; } path = malloc(strlen(p)+strlen(".torrent")+1); @@ -87,7 +87,7 @@ char *torrent_file_path(unsigned char *infohash, size_t len) { return NULL; } - if(sprintf("%s" TORRENT_FILE_EXTENSION,p)<0) { + if(sprintf(path,"%s" TORRENT_FILE_EXTENSION,p)<0) { free(p); free(path); return NULL; diff --git a/src/torrent/info.c b/src/torrent/info.c index 8d014a8..99347e7 100644 --- a/src/torrent/info.c +++ b/src/torrent/info.c @@ -7,6 +7,8 @@ ssize_t torrent_file_info(struct torrent *torrent_p, uint8_t **buf) { ssize_t i, len; size_t buf_len; + if(NULL==buf) { return -1; } + p = malloc(INITIAL_TORRENT_INFO_SIZE); if(NULL==p) { return -1; } diff --git a/test/unit/torrent.tests.c b/test/unit/torrent.tests.c index a6d924e..8b8300e 100644 --- a/test/unit/torrent.tests.c +++ b/test/unit/torrent.tests.c @@ -76,18 +76,43 @@ static void torrent_add_basic_test() { static void torrent_file_info_basic_test() { struct torrent *torrent; + uint8_t *buf; torrent_setup_example(&torrent); + assert(-1==torrent_file_info(NULL,NULL)); + assert(-1==torrent_file_info(torrent,NULL)); + + assert(212==torrent_file_info(torrent,&buf)); + + uint8_t expected[] = "d9:file treed4:testd0:d6:lengthi0e11:pieces root32:" "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01" "ee5:test2d0:d6:lengthi0e11:pieces root32:" "\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02" "eee12:meta versioni2e4:name5:.test12:piece lengthi16384e"; + assert(memcmp(buf,expected,212)==0); + + free(buf); + torrent_free(torrent); } static void torrent_file_path_basic_test() { - struct torrent *torrent; + unsigned char infohash[crypto_hash_sha256_BYTES]; + char *p; - torrent_setup_example(&torrent); + memset(infohash,0,crypto_hash_sha256_BYTES); - torrent_free(torrent); + + p = torrent_file_path(NULL,0); + assert(NULL==p); + + p = torrent_file_path(infohash,0); + assert(NULL==p); + + p = torrent_file_path(infohash,23); + assert(NULL==p); + + p = torrent_file_path(infohash,crypto_hash_sha256_BYTES); + assert(strcmp(p,"/torrents/0000000000000000000000000000000000000000000000000000000000000000.torrent")==0); + + free(p); } static void torrent_init_basic_test() { -- 2.30.2