...
authoralex <[email protected]>
Sat, 23 Apr 2022 19:25:28 +0000 (12:25 -0700)
committeralex <[email protected]>
Sat, 23 Apr 2022 19:25:28 +0000 (12:25 -0700)
src/torrent/add.c
src/torrent/piece.c
test/unit/file.tests.c
test/unit/test_macros.h
test/unit/torrent.tests.c

index 6b3b8da67848a4478b889ca2426068cbfc3df5d2..1a03445094478a9117b0d6497817a039be45c2a9 100644 (file)
@@ -66,10 +66,10 @@ static int torrent_add_file_by_root(struct hash_map *files, struct file *f) {
        struct file *p;
        int i;
 
-       if((i = hashmap_insert(files,f->root,crypto_hash_sha256_BYTES,f))<=0) {
+       if((i = hashmap_insert(files,f->root->hash,crypto_hash_sha256_BYTES,f))<=0) {
                if(i<0) { return -1; }
 
-               p = hashmap_find(files,f->root,crypto_hash_sha256_BYTES);
+               p = hashmap_find(files,f->root->hash,crypto_hash_sha256_BYTES);
                if((p!=NULL)&&(file_equals(f,p)<1)) { return 0; }
        }
        
@@ -97,7 +97,7 @@ static int torrent_files_resize(struct torrent *torrent_p, struct hash_map **map
                                        goto resize;
                                }
                        } else {
-                               if((ret = hashmap_insert(new,p->root,crypto_hash_sha256_BYTES,p))<=0) {
+                               if((ret = hashmap_insert(new,p->root->hash,crypto_hash_sha256_BYTES,p))<=0) {
                                        if(ret<0) { goto clean; }
                                        goto resize;
                                }
index 67b5565d35e720ee653869511d7453c779946f63..ec8488c2a6b848027fd14fd4846ecb26073ab1e0 100644 (file)
@@ -39,17 +39,23 @@ static int torrent_bencode_piece_layers(FILE *fp, struct file *file_p) {
 
 static int torrent_piece_layers(struct torrent *torrent) {
        struct file *file_p;
-       size_t index, pieces;
+       size_t index;
 
-       pieces = 0;
+       torrent->pieces_size = 0;
        for(size_t i=0;i<torrent->files.roots->size;i++) {
                file_p = (struct file*)torrent->files.roots->map[i];
                if(file_p!=NULL) {
-                       if(file_p->size>torrent->piece_length) { pieces++; }
+                       if(file_p->size>torrent->piece_length) { torrent->pieces_size++; }
                }
        }
 
-       torrent->pieces = malloc(sizeof(unsigned char*)*pieces);
+       /*
+        * This is possible if all files are smaller than 
+        * piece size.
+        */
+       if(0==torrent->pieces_size) { return 0; }
+
+       torrent->pieces = malloc(sizeof(unsigned char*)*torrent->pieces_size);
        if(NULL==torrent->pieces) { return -1; }
 
        index = 0;
@@ -61,7 +67,7 @@ static int torrent_piece_layers(struct torrent *torrent) {
                }
        }
        
-       qsort(torrent->pieces,pieces,sizeof(const unsigned char*),&piece_layers_sort);
+       qsort(torrent->pieces,torrent->pieces_size,sizeof(const unsigned char*),&piece_layers_sort);
 
        return 1;
 }
index 51b7b25073ad3cda9dc9c0fc033e70e5dd29b5a1..470bc2d34931dac19cca04f5eb546a593aed0d55 100644 (file)
@@ -3,7 +3,6 @@
 #include<file.h>
 
 int main();
-static void extend_file(const char*);
 static void file_bencode_basic_test();
 static void file_hash_basic_test();
 static void file_hash_large_file_test();
@@ -26,21 +25,6 @@ int main() {
        return EXIT_SUCCESS;
 }
 
-static void extend_file(const char *file) {
-       unsigned char buf[16384];
-       FILE *fp;
-
-       fp = fopen(file,"a");
-       assert(fp!=NULL);
-
-       for(size_t i=0;i<10000;i++) {
-               memset(buf,i%255,16384);
-               assert(16384==fwrite(buf,sizeof(char),16384,fp));
-       }
-
-       fclose(fp);
-}
-
 static void file_bencode_basic_test() {
        struct file *p;
        uint8_t buf[100];
@@ -116,7 +100,7 @@ static void file_hash_large_file_test() {
        struct file *p;
        unsigned char expected[crypto_hash_sha256_BYTES] = {58,71,55,213,6,94,65,97,113,64,176,175,139,67,229,164,13,18,213,164,166,129,202,162,224,45,9,227,191,155,144,144};
 
-       extend_file(TEST_FILE_1);
+       EXTEND_FILE(TEST_FILE_1);
 
        assert(file_init(&p,TEST_FILE_1)==1);
        assert(file_hash(p,16384)==1);
@@ -136,7 +120,7 @@ static void file_hash_large_piece_test() {
 
        size_t blocks = 10001;
        for(int i=16384;i<131073;i<<=1) {
-               extend_file(TEST_FILE_2);       
+               EXTEND_FILE(TEST_FILE_2);       
 
                assert(1==file_init(&p,TEST_FILE_2));
                assert(file_hash(p,i)==1);
index 5e7aa02566630b2c9b98b666d88f6cbbedb5540f..c35952c95141f9604ba8c7cc032aa4b93906fd5d 100644 (file)
 #define TEST_FILE_9 PREFIX "/.meta"
 #define TEST_FILE_9_CONTENTS "title=TITLE\nlink=http://test.com\ndescription=what is a description\nlanguage=en-us\nlastBuildDate=Wed, 29 Dec 2021 12:21:12 +0000"
 
+/*
+ * Parameters:
+ * const char *file
+ */
+#define EXTEND_FILE(file) { \
+       unsigned char buf[16384]; \
+       FILE *fp; \
+\
+       fp = fopen(file,"a"); \
+       assert(fp!=NULL); \
+\
+       for(size_t i=0;i<10000;i++) { \
+               memset(buf,i%255,16384); \
+               assert(16384==fwrite(buf,sizeof(char),16384,fp)); \
+       } \
+\
+       fclose(fp);\
+}
+
 /* Parameters:
  * const char *path
  * unsigned char *hash
index 8b5c7e3336a30eb69d5be8c6b77a082d870da809..bd62005690354cab9a1e0d0f6570c9d9ceee246f 100644 (file)
@@ -12,20 +12,22 @@ static void torrent_file_info_basic_test();
 static void torrent_file_info_conformance_test();
 static void torrent_file_path_basic_test();
 static void torrent_file_piece_layers_basic_test();
+static void torrent_file_piece_layers_conformance_test();
 static void torrent_init_basic_test();
 static void torrent_magnet_basic_test();
 
 int main() {
        setup_env();
 
-       torrent_init_basic_test();
        torrent_add_basic_test();
-       torrent_file_piece_layers_basic_test();
-       torrent_magnet_basic_test();
-       torrent_file_info_basic_test();
        torrent_file_basic_test();
+       torrent_file_info_basic_test();
        torrent_file_info_conformance_test();
        torrent_file_path_basic_test();
+       torrent_file_piece_layers_basic_test();
+       torrent_file_piece_layers_conformance_test();
+       torrent_init_basic_test();
+       torrent_magnet_basic_test();
 
        clean_env();
 
@@ -76,22 +78,22 @@ static void torrent_add_basic_test() {
 
                p = hashmap_find(torrent->files.paths,file1->path,strlen(file1->path));
                assert(p==file1);
-               p = hashmap_find(torrent->files.roots,file1->root,crypto_hash_sha256_BYTES);
+               p = hashmap_find(torrent->files.roots,file1->root->hash,crypto_hash_sha256_BYTES);
                assert(p==file1);
 
                p = hashmap_find(torrent->files.paths,file2->path,strlen(file2->path));
                assert(p==file2);
-               p = hashmap_find(torrent->files.roots,file2->root,crypto_hash_sha256_BYTES);
+               p = hashmap_find(torrent->files.roots,file2->root->hash,crypto_hash_sha256_BYTES);
                assert(p==file2);
        
                p = hashmap_find(torrent->files.paths,file3->path,strlen(file3->path));
                assert(p==file3);
-               p = hashmap_find(torrent->files.roots,file3->root,crypto_hash_sha256_BYTES);
+               p = hashmap_find(torrent->files.roots,file3->root->hash,crypto_hash_sha256_BYTES);
                assert(p==file3);
        
                p = hashmap_find(torrent->files.paths,file4->path,strlen(file4->path));
                assert(p==file4);
-               p = hashmap_find(torrent->files.roots,file4->root,crypto_hash_sha256_BYTES);
+               p = hashmap_find(torrent->files.roots,file4->root->hash,crypto_hash_sha256_BYTES);
                assert(p==file4);
 
                torrent_free(torrent);
@@ -229,6 +231,41 @@ static void torrent_file_piece_layers_basic_test() {
        reset_env();
 }
 
+static void torrent_file_piece_layers_conformance_test() {
+       struct torrent *torrent;
+       struct file *file1, *file2;
+       FILE *fp;
+       unsigned char hash[crypto_hash_sha256_BYTES];
+       unsigned char expected[crypto_hash_sha256_BYTES] = {155,62,214};
+//     9b3ed6930ff8abc0cb657b9683cabb54c0e80128f7a7fe5c0f8e1af32de84b95
+       
+       EXTEND_FILE(TEST_FILE_1);
+       assert(file_init(&file1,TEST_FILE_1)==1);
+       assert(file_hash(file1,16384)==1);
+       EXTEND_FILE(TEST_FILE_2);
+       assert(file_init(&file2,TEST_FILE_2)==1);
+       assert(file_hash(file2,16384)==1);
+
+       TORRENT_SETUP_EMPTY_EXAMPLE(&torrent);
+       assert(torrent_add(torrent,file1)==1);
+       assert(torrent_add(torrent,file2)==1);
+       
+       fp = fopen(TEST_FILE_6,"w");
+       assert(fp!=NULL);
+
+       assert(torrent_file_piece_layers(fp,torrent)==1);
+
+       assert(0==fclose(fp));
+
+       HASH_FILE(TEST_FILE_6,hash,crypto_hash_sha256_BYTES);
+
+       assert(memcmp(hash,expected,crypto_hash_sha256_BYTES)==0);
+
+       torrent_free(torrent);
+
+       reset_env();
+}
+
 static void torrent_init_basic_test() {
        struct torrent *torrent;