From cc595879c52965160d70a0ac78125b08c9686d25 Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 23 Apr 2022 12:25:28 -0700 Subject: [PATCH] ... --- src/torrent/add.c | 6 ++--- src/torrent/piece.c | 16 ++++++++---- test/unit/file.tests.c | 20 ++------------- test/unit/test_macros.h | 19 ++++++++++++++ test/unit/torrent.tests.c | 53 +++++++++++++++++++++++++++++++++------ 5 files changed, 80 insertions(+), 34 deletions(-) diff --git a/src/torrent/add.c b/src/torrent/add.c index 6b3b8da..1a03445 100644 --- a/src/torrent/add.c +++ b/src/torrent/add.c @@ -66,10 +66,10 @@ static int torrent_add_file_by_root(struct hash_map *files, struct file *f) { struct file *p; int i; - if((i = hashmap_insert(files,f->root,crypto_hash_sha256_BYTES,f))<=0) { + if((i = hashmap_insert(files,f->root->hash,crypto_hash_sha256_BYTES,f))<=0) { if(i<0) { return -1; } - p = hashmap_find(files,f->root,crypto_hash_sha256_BYTES); + p = hashmap_find(files,f->root->hash,crypto_hash_sha256_BYTES); if((p!=NULL)&&(file_equals(f,p)<1)) { return 0; } } @@ -97,7 +97,7 @@ static int torrent_files_resize(struct torrent *torrent_p, struct hash_map **map goto resize; } } else { - if((ret = hashmap_insert(new,p->root,crypto_hash_sha256_BYTES,p))<=0) { + if((ret = hashmap_insert(new,p->root->hash,crypto_hash_sha256_BYTES,p))<=0) { if(ret<0) { goto clean; } goto resize; } diff --git a/src/torrent/piece.c b/src/torrent/piece.c index 67b5565..ec8488c 100644 --- a/src/torrent/piece.c +++ b/src/torrent/piece.c @@ -39,17 +39,23 @@ static int torrent_bencode_piece_layers(FILE *fp, struct file *file_p) { static int torrent_piece_layers(struct torrent *torrent) { struct file *file_p; - size_t index, pieces; + size_t index; - pieces = 0; + torrent->pieces_size = 0; 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>torrent->piece_length) { pieces++; } + if(file_p->size>torrent->piece_length) { torrent->pieces_size++; } } } - torrent->pieces = malloc(sizeof(unsigned char*)*pieces); + /* + * This is possible if all files are smaller than + * piece size. + */ + if(0==torrent->pieces_size) { return 0; } + + torrent->pieces = malloc(sizeof(unsigned char*)*torrent->pieces_size); if(NULL==torrent->pieces) { return -1; } index = 0; @@ -61,7 +67,7 @@ static int torrent_piece_layers(struct torrent *torrent) { } } - qsort(torrent->pieces,pieces,sizeof(const unsigned char*),&piece_layers_sort); + qsort(torrent->pieces,torrent->pieces_size,sizeof(const unsigned char*),&piece_layers_sort); return 1; } diff --git a/test/unit/file.tests.c b/test/unit/file.tests.c index 51b7b25..470bc2d 100644 --- a/test/unit/file.tests.c +++ b/test/unit/file.tests.c @@ -3,7 +3,6 @@ #include int main(); -static void extend_file(const char*); static void file_bencode_basic_test(); static void file_hash_basic_test(); static void file_hash_large_file_test(); @@ -26,21 +25,6 @@ int main() { return EXIT_SUCCESS; } -static void extend_file(const char *file) { - unsigned char buf[16384]; - FILE *fp; - - fp = fopen(file,"a"); - assert(fp!=NULL); - - for(size_t i=0;i<10000;i++) { - memset(buf,i%255,16384); - assert(16384==fwrite(buf,sizeof(char),16384,fp)); - } - - fclose(fp); -} - static void file_bencode_basic_test() { struct file *p; uint8_t buf[100]; @@ -116,7 +100,7 @@ static void file_hash_large_file_test() { struct file *p; unsigned char expected[crypto_hash_sha256_BYTES] = {58,71,55,213,6,94,65,97,113,64,176,175,139,67,229,164,13,18,213,164,166,129,202,162,224,45,9,227,191,155,144,144}; - extend_file(TEST_FILE_1); + EXTEND_FILE(TEST_FILE_1); assert(file_init(&p,TEST_FILE_1)==1); assert(file_hash(p,16384)==1); @@ -136,7 +120,7 @@ static void file_hash_large_piece_test() { size_t blocks = 10001; for(int i=16384;i<131073;i<<=1) { - extend_file(TEST_FILE_2); + EXTEND_FILE(TEST_FILE_2); assert(1==file_init(&p,TEST_FILE_2)); assert(file_hash(p,i)==1); diff --git a/test/unit/test_macros.h b/test/unit/test_macros.h index 5e7aa02..c35952c 100644 --- a/test/unit/test_macros.h +++ b/test/unit/test_macros.h @@ -22,6 +22,25 @@ #define TEST_FILE_9 PREFIX "/.meta" #define TEST_FILE_9_CONTENTS "title=TITLE\nlink=http://test.com\ndescription=what is a description\nlanguage=en-us\nlastBuildDate=Wed, 29 Dec 2021 12:21:12 +0000" +/* + * Parameters: + * const char *file + */ +#define EXTEND_FILE(file) { \ + unsigned char buf[16384]; \ + FILE *fp; \ +\ + fp = fopen(file,"a"); \ + assert(fp!=NULL); \ +\ + for(size_t i=0;i<10000;i++) { \ + memset(buf,i%255,16384); \ + assert(16384==fwrite(buf,sizeof(char),16384,fp)); \ + } \ +\ + fclose(fp);\ +} + /* Parameters: * const char *path * unsigned char *hash diff --git a/test/unit/torrent.tests.c b/test/unit/torrent.tests.c index 8b5c7e3..bd62005 100644 --- a/test/unit/torrent.tests.c +++ b/test/unit/torrent.tests.c @@ -12,20 +12,22 @@ static void torrent_file_info_basic_test(); static void torrent_file_info_conformance_test(); static void torrent_file_path_basic_test(); static void torrent_file_piece_layers_basic_test(); +static void torrent_file_piece_layers_conformance_test(); static void torrent_init_basic_test(); static void torrent_magnet_basic_test(); int main() { setup_env(); - torrent_init_basic_test(); torrent_add_basic_test(); - torrent_file_piece_layers_basic_test(); - torrent_magnet_basic_test(); - torrent_file_info_basic_test(); torrent_file_basic_test(); + torrent_file_info_basic_test(); torrent_file_info_conformance_test(); torrent_file_path_basic_test(); + torrent_file_piece_layers_basic_test(); + torrent_file_piece_layers_conformance_test(); + torrent_init_basic_test(); + torrent_magnet_basic_test(); clean_env(); @@ -76,22 +78,22 @@ static void torrent_add_basic_test() { p = hashmap_find(torrent->files.paths,file1->path,strlen(file1->path)); assert(p==file1); - p = hashmap_find(torrent->files.roots,file1->root,crypto_hash_sha256_BYTES); + p = hashmap_find(torrent->files.roots,file1->root->hash,crypto_hash_sha256_BYTES); assert(p==file1); p = hashmap_find(torrent->files.paths,file2->path,strlen(file2->path)); assert(p==file2); - p = hashmap_find(torrent->files.roots,file2->root,crypto_hash_sha256_BYTES); + p = hashmap_find(torrent->files.roots,file2->root->hash,crypto_hash_sha256_BYTES); assert(p==file2); p = hashmap_find(torrent->files.paths,file3->path,strlen(file3->path)); assert(p==file3); - p = hashmap_find(torrent->files.roots,file3->root,crypto_hash_sha256_BYTES); + p = hashmap_find(torrent->files.roots,file3->root->hash,crypto_hash_sha256_BYTES); assert(p==file3); p = hashmap_find(torrent->files.paths,file4->path,strlen(file4->path)); assert(p==file4); - p = hashmap_find(torrent->files.roots,file4->root,crypto_hash_sha256_BYTES); + p = hashmap_find(torrent->files.roots,file4->root->hash,crypto_hash_sha256_BYTES); assert(p==file4); torrent_free(torrent); @@ -229,6 +231,41 @@ static void torrent_file_piece_layers_basic_test() { reset_env(); } +static void torrent_file_piece_layers_conformance_test() { + struct torrent *torrent; + struct file *file1, *file2; + FILE *fp; + unsigned char hash[crypto_hash_sha256_BYTES]; + unsigned char expected[crypto_hash_sha256_BYTES] = {155,62,214}; +// 9b3ed6930ff8abc0cb657b9683cabb54c0e80128f7a7fe5c0f8e1af32de84b95 + + EXTEND_FILE(TEST_FILE_1); + assert(file_init(&file1,TEST_FILE_1)==1); + assert(file_hash(file1,16384)==1); + EXTEND_FILE(TEST_FILE_2); + assert(file_init(&file2,TEST_FILE_2)==1); + assert(file_hash(file2,16384)==1); + + TORRENT_SETUP_EMPTY_EXAMPLE(&torrent); + assert(torrent_add(torrent,file1)==1); + assert(torrent_add(torrent,file2)==1); + + fp = fopen(TEST_FILE_6,"w"); + assert(fp!=NULL); + + assert(torrent_file_piece_layers(fp,torrent)==1); + + assert(0==fclose(fp)); + + HASH_FILE(TEST_FILE_6,hash,crypto_hash_sha256_BYTES); + + assert(memcmp(hash,expected,crypto_hash_sha256_BYTES)==0); + + torrent_free(torrent); + + reset_env(); +} + static void torrent_init_basic_test() { struct torrent *torrent; -- 2.30.2