]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Sat, 30 Oct 2021 20:47:03 +0000 (13:47 -0700)
committeralex <[email protected]>
Sat, 30 Oct 2021 20:47:03 +0000 (13:47 -0700)
inc/add.h
inc/file.h
inc/util.h
src/add.c
src/block.c
src/file.c
test/unit/Makefile.am
test/unit/block.tests.c
test/unit/test_utils.h

index a9dc464c40c11fdb86e7d0474efb036c29ba33ee..05c0ff2bbefa08c50f9bfee2c622615125270655 100644 (file)
--- 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
 
index 532240987e61a85edb4d50765d6141eb083f8b3d..2e029eceb9c100baf7b1216f520f9ffa753e792c 100644 (file)
@@ -9,9 +9,7 @@
 #include<sodium.h>
 
 #include<block.h>
-#include<log.h>
-
-#define FILE_MESSAGE_FREAD_FAILED "failed to read file: %s\n"
+#include<util.h>
 
 struct file {
        char *name;
index 7a8310daa51e54c829b6b337e13a43d8cfa0cafe..ebaf9cdff931d93f377ae8b5f3f8f81df2c72654 100644 (file)
@@ -3,11 +3,10 @@
 
 #include<dirent.h>
 #include<stdio.h>
+#include<stdlib.h>
 #include<string.h>
 #include<sys/stat.h>
 
-#include<log.h>
-
 char *concat(const char*,const char*);
 int file_filter_all(const char*);
 int file_filter_ignore_dotfiles(const char*);
index ea41ddc6851543758c994c44d613f9cdfa696fb9..8990296a05578bb2f6ab02569d98761d893b1d9a 100644 (file)
--- 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);
        }
index 12191252c0728877a86bb5a6d98e29c11825846d..61adb04d59c3b715b92676096338d05c3223b318 100644 (file)
@@ -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;
index 5262ebf010413d3b1c00f8955de48d08c0b5478a..fe630727f0062f3e3be2a16594c8f1711880ec2d 100644 (file)
@@ -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;
                        }
 
index f9eca27b27d75325ea02724aac26902379ca4a10..728c1b5ee2aed4548cedd462d44324263735f731 100644 (file)
@@ -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) \
index 0cb30273feecb6c679bce7a54b337e2d1b91bf0c..584b4ee0d0511d35097a351638b74060ff74d4dc 100644 (file)
@@ -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);
 }
index 2d7e4e63e977092ca7e3494e89a1a9d71dc07ba8..e65884496d402dc49cbc418ecb2df22f948bb403 100644 (file)
@@ -3,6 +3,7 @@
 
 #include<assert.h>
 #include<stdlib.h>
+#include<string.h>
 #include<time.h>
 
 #include<default.h>