]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Sun, 26 Dec 2021 20:41:58 +0000 (12:41 -0800)
committeralex <[email protected]>
Sun, 26 Dec 2021 20:41:58 +0000 (12:41 -0800)
test/unit/torrent.tests.c
test/unit/tree.tests.c

index 9275a3990e1b63aa5f3ebd3bb745ebf0b3fdba07..7b688f2ec67a474ff616490824c505a4c5620d36 100644 (file)
@@ -3,8 +3,11 @@
 #include<torrent.h>
 
 int main();
+static void hash_file(const char*,unsigned char*,size_t);
 static void torrent_add_basic_test();
+static void torrent_file_basic_test();
 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_init_basic_test();
@@ -20,6 +23,8 @@ int main() {
        torrent_file_piece_layers_basic_test();
        torrent_magnet_basic_test();
        torrent_file_info_basic_test();
+       torrent_file_basic_test();
+       torrent_file_info_conformance_test();
        torrent_file_path_basic_test();
 
        clean_env();
@@ -77,6 +82,10 @@ static void torrent_add_basic_test() {
        }
 }
 
+static void torrent_file_basic_test() {
+       assert(1);
+}
+
 static void torrent_file_info_basic_test() {
        struct torrent *torrent;
        uint8_t *buf;
@@ -96,6 +105,59 @@ static void torrent_file_info_basic_test() {
        torrent_free(torrent);
 }
 
+static void torrent_file_info_conformance_test() {
+       struct torrent *torrent_p;
+       struct file *file_p;
+       uint8_t *buf;
+       char path[1024];
+       unsigned char expected[crypto_hash_sha256_BYTES];
+       unsigned char out[crypto_hash_sha256_BYTES];
+       size_t i;
+
+       const char infohash_expected[] = "267a5a34b592074f74287fc221a6cf64a3caad36377e57c5c5f64caea0527646";
+       const char torrent_file_hash_expected[] = "b39f0f4c88e8de4576d02969dfbefac0bbd6de2c9ab49109632e417f4ceb23ad";
+
+       assert(torrent_init(&torrent_p,TEST_DIRECTORY,TEST_DIRECTORY,16384)==1);
+
+       assert(1==file_init(&file_p,TEST_FILE_1));
+       assert(file_hash(file_p,16384)==1);
+       assert(torrent_add(torrent_p,file_p)==1);
+
+       assert(1==file_init(&file_p,TEST_FILE_2));
+       assert(file_hash(file_p,16384)==1);
+       assert(torrent_add(torrent_p,file_p)==1);
+
+       assert(1==file_init(&file_p,TEST_FILE_3));
+       assert(file_hash(file_p,16384)==1);
+       assert(torrent_add(torrent_p,file_p)==1);
+
+       assert(1==file_init(&file_p,TEST_FILE_4));
+       assert(file_hash(file_p,16384)==1);
+       assert(torrent_add(torrent_p,file_p)==1);
+
+       assert(369==torrent_file_info(torrent_p,&buf));
+
+       assert(sodium_hex2bin(expected,crypto_hash_sha256_BYTES,infohash_expected,strlen(infohash_expected),NULL,&i,NULL)==0);
+       assert(i==32);
+
+       assert(hash(buf,369,out,crypto_hash_sha256_BYTES)==1);
+       free(buf);
+
+       assert(memcmp(out,expected,crypto_hash_sha256_BYTES)==0);
+
+       assert(torrent_file(torrent_p)==1);
+
+       assert(sodium_hex2bin(expected,crypto_hash_sha256_BYTES,torrent_file_hash_expected,strlen(torrent_file_hash_expected),NULL,&i,NULL)==0);
+       assert(i==32);
+
+       sprintf(path,"%s/torrents/%s.torrent",TEST_DIRECTORY,infohash_expected);
+       hash_file(path,out,crypto_hash_sha256_BYTES);
+
+       assert(memcmp(out,expected,crypto_hash_sha256_BYTES)==0);
+
+       torrent_free(torrent_p);
+}
+
 static void torrent_file_path_basic_test() {
        unsigned char infohash[crypto_hash_sha256_BYTES];
        char *p;
@@ -118,7 +180,7 @@ static void torrent_file_path_basic_test() {
        free(p);
 }
 
-static void hash_torrent_file(unsigned char *hash, size_t hash_len) {
+static void hash_file(const char *filepath, unsigned char *hash, size_t hash_len) {
        uint8_t buf[16384];
        crypto_hash_sha256_state st;
        FILE *fp;
@@ -126,7 +188,7 @@ static void hash_torrent_file(unsigned char *hash, size_t hash_len) {
 
        assert(hash_len==crypto_hash_sha256_BYTES);
 
-       fp = fopen(TEST_FILE_6,"r");
+       fp = fopen(filepath,"r");
        assert(fp!=NULL);
 
        assert(1==hash_init(&st));
@@ -157,7 +219,7 @@ static void torrent_file_piece_layers_basic_test() {
 
        assert(0==fclose(fp));
 
-       hash_torrent_file(hash,crypto_hash_sha256_BYTES);
+       hash_file(TEST_FILE_6,hash,crypto_hash_sha256_BYTES);
 
        assert(memcmp(hash,expected,crypto_hash_sha256_BYTES)==0);
 
index 4e503eeeb97cb6cc487f4c04c112457c7455a05e..c86529df91796cf92751490ec2ab23e0cf2f3072 100644 (file)
@@ -4,6 +4,7 @@
 
 int main();
 static char *create_random_path();
+static void tree_add_alphabetic_test();
 static void tree_add_basic_test();
 static void tree_add_extended_test();
 static void tree_bencode_basic_test();
@@ -17,6 +18,7 @@ int main() {
        tree_add_basic_test();
        tree_bencode_basic_test();
        tree_add_extended_test();
+       tree_add_alphabetic_test();
        tree_bencode_length_basic_test();
 
        clean_env();
@@ -49,6 +51,45 @@ static char *create_random_path() {
        return str;
 }
 
+static void tree_add_alphabetic_test() {
+       struct tree *tree;
+       struct tree_entry *p;
+       struct file *file;
+       char base[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
+       char paths[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
+       char buf[2];
+
+       for(size_t j=0;j<100;j++) {
+               for(size_t i=0;i<26;i++) {
+                       paths[i] = base[i];
+               }
+
+               assert(1==tree_init(&tree));
+
+               for(size_t i=0;i<26;i++) {
+                       size_t index = rand()%26;
+                       while(paths[index]==0) {
+                               index++;
+                               index %= 26;
+                       }
+
+                       sprintf(buf,"%c",paths[index]);
+
+                       assert(1==file_init(&file,buf));
+                       assert(1==tree_add(tree,file->path,file));
+                       paths[index] = 0;
+               }
+
+               p = tree->entries;
+               for(char c='a';c<='z';c++) {
+                       assert(p->name[0]==c);
+                       p = p->next;
+               }
+
+               tree_free(tree);
+       }
+}
+
 static void tree_add_basic_test() {
        struct tree *tree;
        struct tree_entry *p;