...
authoralex <[email protected]>
Fri, 24 Dec 2021 19:56:59 +0000 (11:56 -0800)
committeralex <[email protected]>
Fri, 24 Dec 2021 19:56:59 +0000 (11:56 -0800)
src/torrent/piece.c
test/unit/torrent.tests.c

index 5443b1c7b0216b9ca4699617d918a4a10cc4e5e8..c8795f18794c818d644ac00cf6834339af0cb5df 100644 (file)
@@ -30,7 +30,7 @@ static int torrent_bencode_piece_layers(FILE *fp, struct file *file_p) {
 }
 
 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;
 
@@ -40,7 +40,7 @@ int torrent_file_piece_layers(FILE *fp, struct torrent *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;
@@ -52,7 +52,7 @@ int torrent_file_piece_layers(FILE *fp, struct torrent *p) {
                }
        }
 
-       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);
index 9c598486e233c0db7f46b56ba6cfbea21aefdfc6..9275a3990e1b63aa5f3ebd3bb745ebf0b3fdba07 100644 (file)
@@ -10,6 +10,7 @@ static void torrent_file_piece_layers_basic_test();
 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();
@@ -125,12 +126,12 @@ static void hash_torrent_file(unsigned char *hash, size_t hash_len) {
 
        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));
@@ -146,7 +147,7 @@ static void torrent_file_piece_layers_basic_test() {
        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);
@@ -195,13 +196,30 @@ static void torrent_magnet_basic_test() {
        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);