]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Fri, 10 Dec 2021 01:30:56 +0000 (17:30 -0800)
committeralex <[email protected]>
Fri, 10 Dec 2021 01:30:56 +0000 (17:30 -0800)
src/torrent/file.c
src/torrent/info.c
test/unit/torrent.tests.c

index aef04bdeb0271783c3c7a9bf2124bc5afbe2e01c..3666f2b0f86a92a3217f48f0e7b6fc5e4d306c83 100644 (file)
@@ -78,7 +78,7 @@ char *torrent_file_path(unsigned char *infohash, size_t len) {
        // this always returns hex, unnecessary to return
        sodium_bin2hex(hex,(crypto_hash_sha256_BYTES<<1)+1,infohash,crypto_hash_sha256_BYTES);
 
-       p = concat(PREFIX TORRENT_FILE_DIRECTORY,hex);
+       p = concat(TORRENT_FILE_DIRECTORY,hex);
        if(NULL==p) { return NULL; }
 
        path = malloc(strlen(p)+strlen(".torrent")+1);
@@ -87,7 +87,7 @@ char *torrent_file_path(unsigned char *infohash, size_t len) {
                return NULL;
        }
 
-       if(sprintf("%s" TORRENT_FILE_EXTENSION,p)<0) {
+       if(sprintf(path,"%s" TORRENT_FILE_EXTENSION,p)<0) {
                free(p);
                free(path);
                return NULL;
index 8d014a8271f5cc50456501c1e2e4fcab811f248a..99347e711e92c435666f181a36f15921731b5ea1 100644 (file)
@@ -7,6 +7,8 @@ ssize_t torrent_file_info(struct torrent *torrent_p, uint8_t **buf) {
        ssize_t i, len;
        size_t buf_len;
 
+       if(NULL==buf) { return -1; }
+
        p = malloc(INITIAL_TORRENT_INFO_SIZE);
        if(NULL==p) { return -1; }
 
index a6d924e9cdd38fb4bc6b5d681a9bdd1494039ee7..8b8300eb726b8401e7852fa8c7666e96b7baf407 100644 (file)
@@ -76,18 +76,43 @@ static void torrent_add_basic_test() {
 
 static void torrent_file_info_basic_test() {
        struct torrent *torrent;
+       uint8_t *buf;
 
        torrent_setup_example(&torrent);
 
+       assert(-1==torrent_file_info(NULL,NULL));
+       assert(-1==torrent_file_info(torrent,NULL));
+
+       assert(212==torrent_file_info(torrent,&buf));
+
+       uint8_t expected[] = "d9:file treed4:testd0:d6:lengthi0e11:pieces root32:" "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01" "ee5:test2d0:d6:lengthi0e11:pieces root32:" "\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02" "eee12:meta versioni2e4:name5:.test12:piece lengthi16384e";
+       assert(memcmp(buf,expected,212)==0);
+
+       free(buf);
+
        torrent_free(torrent);
 }
 
 static void torrent_file_path_basic_test() {
-       struct torrent *torrent;
+       unsigned char infohash[crypto_hash_sha256_BYTES];
+       char *p;
 
-       torrent_setup_example(&torrent);
+       memset(infohash,0,crypto_hash_sha256_BYTES);
 
-       torrent_free(torrent);
+               
+       p = torrent_file_path(NULL,0);
+       assert(NULL==p);
+
+       p = torrent_file_path(infohash,0);
+       assert(NULL==p);
+
+       p = torrent_file_path(infohash,23);
+       assert(NULL==p);
+
+       p = torrent_file_path(infohash,crypto_hash_sha256_BYTES);
+       assert(strcmp(p,"/torrents/0000000000000000000000000000000000000000000000000000000000000000.torrent")==0);
+
+       free(p);
 }
 
 static void torrent_init_basic_test() {