...
authoralex <[email protected]>
Tue, 2 Nov 2021 22:04:22 +0000 (15:04 -0700)
committeralex <[email protected]>
Tue, 2 Nov 2021 22:04:22 +0000 (15:04 -0700)
inc/util.h
src/block.c
src/file.c
src/util/pow.c
test/unit/block.tests.c
test/unit/file.tests.c
test/unit/tree.tests.c

index ebaf9cdff931d93f377ae8b5f3f8f81df2c72654..2329adb074b3111a0def2386bdb4e96681df93bd 100644 (file)
@@ -12,6 +12,6 @@ int file_filter_all(const char*);
 int file_filter_ignore_dotfiles(const char*);
 int is_directory(const char*);
 int is_file(const char*);
-size_t next_power_2(size_t);
+unsigned long next_power_2(unsigned long);
 
 #endif
index facc7151a4f83e63dc35c5590dba0f8da55cb5ad..f1b515965a704fcd30931d5679cc382a834354f6 100644 (file)
@@ -84,6 +84,7 @@ int block_merkle_root(struct block *root) {
        if(NULL==root) { return -1; }
 
        size_t len = block_length(root);
+       if(1==len) { return 0; }
        if((len&(len-1))!=0) { return -1; }
 
        p = root;
index fe630727f0062f3e3be2a16594c8f1711880ec2d..8bcbc0dd9c7d25d5b9fee0c3d2186d2cee00ebea 100644 (file)
@@ -11,6 +11,10 @@ void file_free(struct file *p) {
                free(p->path);
        }
 
+       if(p->blocks!=NULL) {
+               block_free(p->blocks);
+       }
+
        free(p);
 }
 
@@ -20,6 +24,11 @@ int file_hash(struct file *file_p, int piece_length) {
        FILE *fp;
        int blocks_per_piece;
 
+       if(NULL==file_p) { return -1; }
+       if((piece_length<16384)||(piece_length&&(piece_length&(piece_length-1)))) {
+               return -1;
+       }
+
        blocks_per_piece = piece_length / BLOCK_SIZE;
        
        fp = fopen(file_p->path,"rb");
@@ -29,8 +38,8 @@ int file_hash(struct file *file_p, int piece_length) {
        }
 
        while(1) {
-               if(block_init(&start)<0) { goto clean; }
-               p = start;
+               start = NULL;
+               p = NULL;
                for(size_t i=0;i<blocks_per_piece;i++) {
                        size_t len = fread(data,sizeof(uint8_t),BLOCK_SIZE,fp);
                        if((len<=BLOCK_SIZE)&&(ferror(fp)!=0)) {
@@ -38,15 +47,20 @@ int file_hash(struct file *file_p, int piece_length) {
                                goto clean;
                        }
 
-                       if(len==0) { break; }
+                       if(len==0) { goto done; }
 
                        file_p->size += len;
 
                        if(block_init(&next)<0) { goto clean; }
                        if(hash(data,len,next->hash,crypto_hash_sha256_BYTES)<0) { goto clean; }
 
-                       p->next = next;
-                       p = p->next;
+                       if(NULL==p) {
+                               start = next;
+                               p = next;
+                       } else {
+                               p->next = next;
+                               p = p->next;
+                       }
                }
 
                size_t blocks = block_length(start);
@@ -64,16 +78,17 @@ int file_hash(struct file *file_p, int piece_length) {
                }
 
                if(block_merkle_root(start)<0) { goto clean; }
-       
+
                if(NULL==file_p->blocks) {
                        file_p->blocks = start;
                        end = start;
+               } else {
+                       end->next = start;
+                       end = end->next;
                }
-               
-               end->next = start;
-               end = end->next;
        }
 
+done:
        fclose(fp);
 
        if(block_duplicate(&start,file_p->blocks)<0) { return -1; }
index aba6520f589c4c0c31ef5ec52c59f50be32d111e..b3db5dac669a843d4568188cc5009cf76e776ed7 100644 (file)
@@ -1,5 +1,13 @@
 #include<util.h>
 
-size_t next_power_2(size_t i) {
-       return 0;
+unsigned long next_power_2(unsigned long i) {
+       i--;
+       i |= i >> 1;
+       i |= i >> 2;
+       i |= i >> 4;
+       i |= i >> 8;
+       i |= i >> 16;
+       i++;
+
+       return i;
 }
index 9c33de5a4d70aa27731e60146706bac78eb74bdd..22a61c8bc9c4ce3d4f46fe9617d9e61b3a0e3e10 100644 (file)
@@ -12,9 +12,9 @@ static void block_merkle_root_basic_test();
 int main() {
        setup_env();
 
+       block_init_basic_test();
        block_append_blank_basic_test();
        block_duplicate_basic_test();
-       block_init_basic_test();
        block_length_basic_test();
        block_merkle_root_basic_test();
 
@@ -137,7 +137,7 @@ static void block_merkle_root_basic_test() {
        assert(block_init(&root)==1);
        memset(root->hash,0,crypto_hash_sha256_BYTES);
 
-       assert(block_merkle_root(root)==-1);
+       assert(block_merkle_root(root)==0);
 
        assert(block_init(&(root->next))==1);
        memset(root->next->hash,1,crypto_hash_sha256_BYTES);
index 0e0a31b16c18bb6dfafa4e9d7f82ae59dc747f54..7be295e7b4ae9f83c6b899c053d57ac58851943f 100644 (file)
@@ -3,20 +3,67 @@
 #include<file.h>
 
 int main();
+static void file_hash_basic_test();
 static void file_init_basic_test();
 
 int main() {
        setup_env();
 
        file_init_basic_test();
+       file_hash_basic_test();
 
        clean_env();
 
        return EXIT_SUCCESS;
 }
 
+static void file_hash_basic_test() {
+       struct file *p;
+       unsigned char expected1[crypto_hash_sha256_BYTES] = {174,73,190,37,189,244,236,113,18,122,255,20,5,229,77,151,187,72,27,75,203,32,25,154,81,249,19,29,118,7,228,229};
+       unsigned char expected2[crypto_hash_sha256_BYTES] = {71,126,141,85,81,21,1,209,253,63,10,198,227,135,44,112,211,128,113,240,243,4,185,72,5,139,121,89,41,155,245,5};
+       unsigned char expected3[crypto_hash_sha256_BYTES] = {47,41,56,87,56,3,117,236,236,192,175,52,172,206,68,105,215,102,47,147,47,213,216,92,13,12,117,223,28,179,169,157};
+       unsigned char expected4[crypto_hash_sha256_BYTES] = {13,185,47,130,186,141,40,43,108,4,196,12,148,165,132,182,128,76,83,36,20,191,31,59,218,88,213,132,228,13,139,159};
+
+       assert(file_init(&p,TEST_FILE_1)==1);
+
+       assert(file_hash(NULL,16384)==-1);
+       assert(file_hash(p,1384)==-1);
+
+       assert(file_hash(p,16384)==1);
+       assert(memcmp(p->root,expected1,crypto_hash_sha256_BYTES)==0);
+       assert(1==block_length(p->blocks));
+       assert(21==p->size);
+
+       file_free(p);
+
+       assert(file_init(&p,TEST_FILE_2)==1);
+       assert(file_hash(p,16384)==1);
+       assert(memcmp(p->root,expected2,crypto_hash_sha256_BYTES)==0);
+       assert(1==block_length(p->blocks));
+       assert(26==p->size);
+
+       file_free(p);
+
+       assert(file_init(&p,TEST_FILE_3)==1);
+       assert(file_hash(p,16384)==1);
+       assert(memcmp(p->root,expected3,crypto_hash_sha256_BYTES)==0);
+       assert(1==block_length(p->blocks));
+       assert(24==p->size);
+
+       file_free(p);
+       
+       assert(file_init(&p,TEST_FILE_4)==1);
+       assert(file_hash(p,16384)==1);
+       assert(memcmp(p->root,expected4,crypto_hash_sha256_BYTES)==0);
+       assert(1==block_length(p->blocks));
+       assert(51==p->size);
+
+       file_free(p);
+}
+
 static void file_init_basic_test() {
        struct file *p;
+       unsigned char expected[crypto_hash_sha256_BYTES];
 
        assert(-1==file_init(NULL,NULL));
        assert(-1==file_init(&p,NULL));
@@ -26,6 +73,10 @@ static void file_init_basic_test() {
 
        assert(strcmp(p->name,"test")==0);
        assert(strcmp(p->path,"testdir/test")==0);
+       memset(expected,0,crypto_hash_sha256_BYTES);
+       assert(memcmp(p->root,expected,crypto_hash_sha256_BYTES)==0);
+       assert(NULL==p->blocks);
+       assert(0==p->size);
 
        file_free(p);
 }
index c644c402dd17e44da664c2a336150d528687f4f1..dfe2e52e84e321669d156e4ee4a0c885dbe2ba74 100644 (file)
@@ -12,9 +12,9 @@ static void tree_init_basic_test();
 int main() {
        setup_env();
 
+       tree_init_basic_test();
        tree_add_basic_test();
        tree_add_extended_test();
-       tree_init_basic_test();
 
        clean_env();