}
int torrent_file_piece_layers(FILE *fp, struct torrent *p) {
- unsigned char **hashes;
+ const unsigned char **hashes;
size_t index, pieces;
struct file *file_p;
if(file_p!=NULL) { pieces++; }
}
- hashes = malloc(sizeof(unsigned char*));
+ hashes = malloc(sizeof(const unsigned char*)*pieces);
if(NULL==hashes) { return -1; }
index = 0;
}
}
- qsort(hashes,pieces,sizeof(unsigned char*),&piece_layer_sort);
+ qsort(hashes,pieces,sizeof(const unsigned char*),&piece_layer_sort);
for(size_t i=0;i<pieces;i++) {
file_p = hashmap_find(p->files.roots,hashes[i],crypto_hash_sha256_BYTES);
static void torrent_init_basic_test();
static void torrent_magnet_basic_test();
static void torrent_setup_example(struct torrent**);
+static void torrent_setup_example_file(struct file**,const char*,uint8_t);
int main() {
setup_env();
assert(hash_len==crypto_hash_sha256_BYTES);
- fp = fopen(TEST_FILE_6,"w");
+ fp = fopen(TEST_FILE_6,"r");
assert(fp!=NULL);
assert(1==hash_init(&st));
- while(feof(fp)!=0) {
+ while(feof(fp)==0) {
i = fread(buf,sizeof(uint8_t),16384,fp);
if(i>0) {
assert(1==hash_update(&st,buf,i));
struct torrent *torrent;
FILE *fp;
unsigned char hash[crypto_hash_sha256_BYTES];
- unsigned char expected[crypto_hash_sha256_BYTES] = {227,176,196,66,152,252,28,20,154,251,244,200,153,111,185,36,39,174,65,228,100,155,147,76,164,149,153,27,120,82,184,85};
+ unsigned char expected[crypto_hash_sha256_BYTES] = {32,188,23,84,39,38,213,235,27,41,120,194,91,119,181,244,243,49,193,113,138,136,121,232,42,97,20,207,246,225,132,236};
fp = fopen(TEST_FILE_6,"w");
assert(fp!=NULL);
reset_env();
}
+static void torrent_setup_example_file(struct file **file_p, const char *filename, uint8_t root_val) {
+ struct block *block, *p;
+
+ assert(file_init(file_p,filename)==1);
+ memset((*file_p)->root,root_val,crypto_hash_sha256_BYTES);
+ for(size_t i=0;i<10;i++) {
+ assert(block_init(&block)==1);
+ memset(block->hash,i,crypto_hash_sha256_BYTES);
+
+ if((*file_p)->piece_layers==NULL) {
+ (*file_p)->piece_layers = block;
+ } else {
+ p->next = block;
+ }
+
+ p = block;
+ }
+}
+
static void torrent_setup_example(struct torrent **p) {
struct file *file1, *file2;
- assert(file_init(&file1,TEST_FILE_1)==1);
- memset(file1->root,1,crypto_hash_sha256_BYTES);
- assert(file_init(&file2,TEST_FILE_2)==1);
- memset(file2->root,2,crypto_hash_sha256_BYTES);
+ torrent_setup_example_file(&file1,TEST_FILE_1,1);
+ torrent_setup_example_file(&file2,TEST_FILE_2,2);
assert(torrent_init(p,TEST_DIRECTORY,TEST_DIRECTORY,16384)==1);