#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
#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;
#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*);
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);
}
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;
}
}
(*p)->data = NULL;
+ memset((*p)->hash,0,crypto_hash_sha256_BYTES);
(*p)->next = NULL;
return 1;
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; }
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;
}
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) \
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) \
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();
}
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);
}
#include<assert.h>
#include<stdlib.h>
+#include<string.h>
#include<time.h>
#include<default.h>