From: alex Date: Sat, 30 Oct 2021 20:47:03 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=4f1c29be7884e89a3474f6e8a975624d64e4e346;p=seeder ... --- diff --git a/inc/add.h b/inc/add.h index a9dc464..05c0ff2 100644 --- a/inc/add.h +++ b/inc/add.h @@ -10,6 +10,7 @@ #define ADD_MESSAGE_ADDING_TORRENT "adding all files in %s to torrent named %s\n" #define ADD_MESSAGE_ADDED_FILE "added file %s\n" +#define ADD_MESSAGE_HASH_FILE_FAILED "failed to hash %s\n" #define ADD_QUEUE_INITIAL_SIZE 8 diff --git a/inc/file.h b/inc/file.h index 5322409..2e029ec 100644 --- a/inc/file.h +++ b/inc/file.h @@ -9,9 +9,7 @@ #include #include -#include - -#define FILE_MESSAGE_FREAD_FAILED "failed to read file: %s\n" +#include struct file { char *name; diff --git a/inc/util.h b/inc/util.h index 7a8310d..ebaf9cd 100644 --- a/inc/util.h +++ b/inc/util.h @@ -3,11 +3,10 @@ #include #include +#include #include #include -#include - char *concat(const char*,const char*); int file_filter_all(const char*); int file_filter_ignore_dotfiles(const char*); diff --git a/src/add.c b/src/add.c index ea41ddc..8990296 100644 --- a/src/add.c +++ b/src/add.c @@ -63,7 +63,10 @@ static void *add_hash(void *unused) { if(NULL==p) { return NULL; } - if(file_hash(p,global_options.piece_length)<0) { return NULL; } + if(file_hash(p,global_options.piece_length)<0) { + log_err(ADD_MESSAGE_HASH_FILE_FAILED,p->path); + return NULL; + } log_info(ADD_MESSAGE_ADDED_FILE,p->path); } diff --git a/src/block.c b/src/block.c index 1219125..61adb04 100644 --- a/src/block.c +++ b/src/block.c @@ -13,16 +13,19 @@ int block_append_blank(struct block *p) { return 1; } -int block_duplicate(struct block **p, struct block *to_dup) { - if(NULL==p) { return -1; } +int block_duplicate(struct block **root, struct block *to_dup) { + struct block *p; + if(NULL==root) { return -1; } if(NULL==to_dup) { return -1; } + p = (*root); + while(to_dup!=NULL) { - if(block_init(p)<0) { return -1; } + if(block_init(&p)<0) { return -1; } - memcpy((*p)->hash,to_dup->hash,crypto_hash_sha256_BYTES); + memcpy(p->hash,to_dup->hash,crypto_hash_sha256_BYTES); - (*p) = (*p)->next; + p = p->next; to_dup = to_dup->next; } @@ -53,6 +56,7 @@ int block_init(struct block **p) { } (*p)->data = NULL; + memset((*p)->hash,0,crypto_hash_sha256_BYTES); (*p)->next = NULL; return 1; diff --git a/src/file.c b/src/file.c index 5262ebf..fe63072 100644 --- a/src/file.c +++ b/src/file.c @@ -23,7 +23,10 @@ int file_hash(struct file *file_p, int piece_length) { blocks_per_piece = piece_length / BLOCK_SIZE; fp = fopen(file_p->path,"rb"); - if(NULL==fp) { return -1; } + if(NULL==fp) { + perror("fopen"); + return -1; + } while(1) { if(block_init(&start)<0) { goto clean; } @@ -32,7 +35,6 @@ int file_hash(struct file *file_p, int piece_length) { size_t len = fread(data,sizeof(uint8_t),BLOCK_SIZE,fp); if((len<=BLOCK_SIZE)&&(ferror(fp)!=0)) { perror("fread"); - log_err(FILE_MESSAGE_FREAD_FAILED,file_p->path); goto clean; } diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index f9eca27..728c1b5 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -31,13 +31,16 @@ bencode_tests_SOURCES = \ block_tests_SOURCES = \ $(common_SOURCES) \ block.tests.c \ - $(top_srcdir)/src/block.c + $(top_srcdir)/src/block.c \ + $(top_srcdir)/src/hash.c file_tests_SOURCES = \ $(common_SOURCES) \ file.tests.c \ $(top_srcdir)/src/block.c \ - $(top_srcdir)/src/file.c + $(top_srcdir)/src/file.c \ + $(top_srcdir)/src/hash.c \ + $(top_srcdir)/src/util/pow.c hash_tests_SOURCES = \ $(common_SOURCES) \ @@ -54,16 +57,20 @@ torrent_tests_SOURCES = \ torrent.tests.c \ $(top_srcdir)/src/block.c \ $(top_srcdir)/src/file.c \ + $(top_srcdir)/src/hash.c \ $(top_srcdir)/src/hashmap.c \ $(top_srcdir)/src/torrent.c \ - $(top_srcdir)/src/tree.c + $(top_srcdir)/src/tree.c \ + $(top_srcdir)/src/util/pow.c tree_tests_SOURCES = \ $(common_SOURCES) \ tree.tests.c \ $(top_srcdir)/src/block.c \ $(top_srcdir)/src/file.c \ - $(top_srcdir)/src/tree.c + $(top_srcdir)/src/hash.c \ + $(top_srcdir)/src/tree.c \ + $(top_srcdir)/src/util/pow.c util_filter_tests_SOURCES = \ $(common_SOURCES) \ diff --git a/test/unit/block.tests.c b/test/unit/block.tests.c index 0cb3027..584b4ee 100644 --- a/test/unit/block.tests.c +++ b/test/unit/block.tests.c @@ -4,13 +4,14 @@ int main(); static void block_append_blank_basic_test(); +static void block_duplicate_basic_test(); static void block_init_basic_test(); int main() { setup_env(); -// block_append_blank_basic_test(); -// block_duplicate_basic_test(); + block_append_blank_basic_test(); + block_duplicate_basic_test(); block_init_basic_test(); //block_length_basic_test(); //block_merkle_root_basic_test(); @@ -21,14 +22,68 @@ int main() { } static void block_append_blank_basic_test() { - + struct block *root, *p; + unsigned char expected[crypto_hash_sha256_BYTES] = {0}; + + assert(block_init(&p)==1); + memset(p->hash,10,crypto_hash_sha256_BYTES); + + root = p; + + assert(block_length(root)==1); + assert(block_append_blank(p)==1); + assert(p->next!=NULL); + p = p->next; + + assert(block_length(root)==2); + assert(memcmp(p->hash,expected,crypto_hash_sha256_BYTES)==0); + + block_free(root); +} + +static void block_duplicate_basic_test() { + struct block *root, *root2, *p, *p2; + + assert(block_init(&root)==1); + + p = root; + memset(root->hash,rand()%255,crypto_hash_sha256_BYTES); + + for(int i=0;i<(rand()%100)+1;i++) { + assert(block_init(&(p->next))==1); + p = p->next; + memset(p->hash,rand()%255,crypto_hash_sha256_BYTES); + } + + assert(block_duplicate(NULL,root)==-1); + assert(block_duplicate(&root2,NULL)==-1); + assert(block_duplicate(&root2,root)==1); + assert(block_length(root)==block_length(root2)); + + p = root; + p2 = root2; + while(p!=NULL) { + assert(memcmp(p->hash,p2->hash,crypto_hash_sha256_BYTES)==0); + p = p->next; + p2 = p2->next; + } + + block_free(root); + block_free(root2); } static void block_init_basic_test() { - struct block *p; + struct block *root, *p; assert(block_init(NULL)==-1); assert(block_init(&p)==1); - block_free(p); + root = p; + + for(int i=0;i<(rand()%100)+1;i++) { + assert(block_init(&(p->next))==1); + p = p->next; + } + + block_free(root); } diff --git a/test/unit/test_utils.h b/test/unit/test_utils.h index 2d7e4e6..e658844 100644 --- a/test/unit/test_utils.h +++ b/test/unit/test_utils.h @@ -3,6 +3,7 @@ #include #include +#include #include #include