From 2ff26e16d7de536127f8c5263950a5ff9d12d36a Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 2 Mar 2022 23:45:53 -0800 Subject: [PATCH] ... --- inc/block.h | 2 +- src/block.c | 16 ++++++++++++++-- src/file.c | 4 ++-- test/unit/block.tests.c | 38 ++++++++++++++++++++++++++++++++------ test/unit/file.tests.c | 5 ++++- 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/inc/block.h b/inc/block.h index 40c127a..4fab72d 100644 --- a/inc/block.h +++ b/inc/block.h @@ -15,12 +15,12 @@ struct block { struct block *next; }; -size_t block_end(struct block**); void block_free(struct block*); int block_init(struct block**); size_t block_length(struct block*); int block_merkle_layer(struct block*,struct block**); int block_merkle_root(struct block*,struct block**); int block_pad(struct block*); +size_t block_size(struct block*); #endif diff --git a/src/block.c b/src/block.c index 3067fa1..b148717 100644 --- a/src/block.c +++ b/src/block.c @@ -27,8 +27,10 @@ int block_init(struct block **p) { } size_t block_length(struct block *p) { - size_t i = 0; - while(p!=NULL) { + if((NULL==p)||(p->index<0)) { return 0; } + + size_t i = 1; + while((p->next!=NULL)&&(p->indexnext->index)) { i++; p = p->next; } @@ -36,6 +38,7 @@ size_t block_length(struct block *p) { return i; } + int block_merkle_layer(struct block *p, struct block **next_layer) { struct block *tail; crypto_hash_sha256_state state; @@ -121,3 +124,12 @@ int block_pad(struct block *p) { return 1; } + +size_t block_size(struct block *p) { + size_t i = 0; + while(p!=NULL) { + i++; + p = p->next; + } + return i; +} diff --git a/src/file.c b/src/file.c index af38d76..278befe 100644 --- a/src/file.c +++ b/src/file.c @@ -76,8 +76,8 @@ void file_free(struct file *p) { free(p->path); } - if(p->piece_layers!=NULL) { - block_free(p->piece_layers); + if(p->blocks!=NULL) { + block_free(p->blocks); } free(p); diff --git a/test/unit/block.tests.c b/test/unit/block.tests.c index 97b7f7d..c115b21 100644 --- a/test/unit/block.tests.c +++ b/test/unit/block.tests.c @@ -8,6 +8,7 @@ static void block_length_basic_test(); static void block_merkle_layer_basic_test(); static void block_merkle_root_basic_test(); static void block_pad_basic_test(); +static void block_size_basic_test(); int main() { setup_env(); @@ -17,6 +18,7 @@ int main() { block_merkle_layer_basic_test(); block_merkle_root_basic_test(); block_pad_basic_test(); + block_size_basic_test(); clean_env(); @@ -49,10 +51,12 @@ static void block_length_basic_test() { struct block *root, *p; assert(block_init(&root)==1); + root->index = 0; p = root; - for(size_t i=0;i<10;i++) { + for(size_t i=1;i<11;i++) { assert(block_init(&(p->next))==1); + p->next->index = i; p = p->next; } @@ -61,11 +65,13 @@ static void block_length_basic_test() { block_free(root); assert(block_init(&root)==1); + root->index = 0; p = root; size_t count = 1; - for(size_t i=0;inext))==1); + p->next->index = i; p = p->next; count++; } @@ -166,7 +172,7 @@ static void block_pad_basic_test() { assert(block_init(&root)==1); assert(block_pad(root)==1); - assert(block_length(root)==2); + assert(block_size(root)==2); p = root->next; for(size_t i=2;i<7;i++) { @@ -174,14 +180,34 @@ static void block_pad_basic_test() { p = p->next; } - assert(block_length(root)==7); + assert(block_size(root)==7); for(size_t i=8;i<=1024;i<<=1) { assert(block_pad(root)==1); - assert(block_length(root)==i); + assert(block_size(root)==i); assert(block_pad(root->next)==1); - assert(block_length(root)==i+1); + assert(block_size(root)==i+1); + } + + block_free(root); +} + +static void block_size_basic_test() { + struct block *root, *p; + + assert(1==block_init(&root)); + + p = root; + size_t j = 1; + for(size_t i=1;i<255%rand();i++) { + assert(1==block_init(&(p->next))); + p = p->next; + j++; } + assert(0==block_length(root)); + + assert(j==block_size(root)); + block_free(root); } diff --git a/test/unit/file.tests.c b/test/unit/file.tests.c index 7136983..51b7b25 100644 --- a/test/unit/file.tests.c +++ b/test/unit/file.tests.c @@ -55,6 +55,8 @@ static void file_bencode_basic_test() { assert(memcmp(buf,expected,69)==0); + block_free(p->root); + file_free(p); } @@ -119,6 +121,7 @@ static void file_hash_large_file_test() { assert(file_init(&p,TEST_FILE_1)==1); assert(file_hash(p,16384)==1); assert(memcmp(p->root->hash,expected,crypto_hash_sha256_BYTES)==0); + assert(10001==block_length(p->piece_layers)); assert((10000*16384+21)==p->size); @@ -137,7 +140,7 @@ static void file_hash_large_piece_test() { assert(1==file_init(&p,TEST_FILE_2)); assert(file_hash(p,i)==1); - assert(memcmp(p->root,expected,crypto_hash_sha256_BYTES)==0); + assert(memcmp(p->root->hash,expected,crypto_hash_sha256_BYTES)==0); assert(blocks==block_length(p->piece_layers)); assert((10000*16384+26)==p->size); -- 2.30.2