]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Tue, 7 Dec 2021 01:18:00 +0000 (17:18 -0800)
committeralex <[email protected]>
Tue, 7 Dec 2021 01:18:00 +0000 (17:18 -0800)
.gitignore
src/torrent/add.c
src/torrent/magnet.c
test/unit/Makefile.am
test/unit/torrent.tests.c

index 1a7cd80df5def7f1dd372bf1527fa7f0ad1a0aa5..16b89eae2a3ff425a226cf2372bea61cdcb0b4fc 100644 (file)
@@ -26,3 +26,6 @@ stamp-h1
 
 # binaries
 seederd
+
+# test files/objects
+.test/
index c23f5f0d00d67da6393d04953b94ffabb65de1da..6b80dd173da9d041056b041d17dcadf98da124bf 100644 (file)
@@ -68,7 +68,7 @@ static int torrent_add_file_by_root(struct hash_map *files, struct file *f) {
 }
 
 static int torrent_files_resize(struct torrent *torrent_p, size_t new_size) {
-       struct file *p, *found;
+       struct file *p;
        struct hash_map *new, *old;
        int ret;
 
@@ -77,12 +77,6 @@ static int torrent_files_resize(struct torrent *torrent_p, size_t new_size) {
        for(size_t i=0;i<torrent_p->files->size;i++) {
                p = torrent_p->files->map[i];
                if(p!=NULL) {
-                       // remove reference to file
-                       // check if already added this file
-                       // necessary because of dual keys
-                       found = hashmap_find(new,p->root,crypto_hash_sha256_BYTES);
-                       if(found!=NULL) { continue; }
-
                        if(
                                ((ret = torrent_add_file_by_root(new,p))<=0) ||
                                ((ret = torrent_add_file_by_path(new,p))<=0)
index 16a006d96ef16547f226302207b7039a2bdbe48c..3bce8e2eaa7bbfb69fd3b64e083f99abf6256a9d 100644 (file)
@@ -1,5 +1,36 @@
 #include<torrent.h>
 
-char *torrent_magnet(struct torrent *p) {
+#define MAGNET_LINK_TEMPLATE "magnet:?xt=urn:btmh:%s&dn=%s"
+#define MAGNET_LINK_TEMPLATE_LENGTH 24
+
+char *torrent_magnet(struct torrent *torrent_p) {
+       uint8_t *info;
+       char *p;
+       unsigned char infohash[crypto_hash_sha256_BYTES];
+       char hex[(crypto_hash_sha256_BYTES<<1)+1];
+       size_t len;
+       ssize_t i;
+
+       info = NULL;
+       if((i = torrent_file_info(torrent_p,&info))<0) { goto clean; }
+
+       len = MAGNET_LINK_TEMPLATE_LENGTH+i+strlen(torrent_p->name)+1;
+       p = malloc(sizeof(char)*len);
+       if(NULL==p) { return NULL; }
+
+       if(hash(info,i,infohash,crypto_hash_sha256_BYTES)<0) { goto clean; }
+       free(info);
+       info = NULL;
+
+       // this always returns hex, unnecessary to return
+       sodium_bin2hex(hex,(crypto_hash_sha256_BYTES<<1)+1,infohash,crypto_hash_sha256_BYTES);
+
+       len--;
+       if(snprintf(p,len,MAGNET_LINK_TEMPLATE,hex,torrent_p->name)!=len) { goto clean; }
+
+       return p;
+clean:
+       if(info!=NULL) { free(info); }
+       free(p);
        return NULL;
 }
index 62c5b31c0ed6e514f381cd00f867057422568c73..b97536359a39c5ef4ae85698dfe84d8036c87a6f 100644 (file)
@@ -73,7 +73,9 @@ torrent_tests_SOURCES = \
        $(top_srcdir)/src/hashmap.c \
        $(top_srcdir)/src/torrent/add.c \
        $(top_srcdir)/src/torrent/init.c \
+       $(top_srcdir)/src/torrent/file.c \
        $(top_srcdir)/src/torrent/free.c \
+       $(top_srcdir)/src/torrent/magnet.c \
        $(top_srcdir)/src/tree.c
 
 tree_tests_SOURCES = \
index 93470d6d7ffb3f23b557c94e40bcbbb79706bde0..38de89c79c28af8e672bd323bb693cb39770340f 100644 (file)
@@ -3,14 +3,16 @@
 #include<torrent.h>
 
 int main();
-static void torrent_init_basic_test();
 static void torrent_add_basic_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_magnet_basic_test();
 
        clean_env();
 
@@ -19,45 +21,89 @@ int main() {
 
 static void torrent_add_basic_test() {
        struct torrent *torrent;
-       struct file *file1, *file2, *file3, *file4;
+       struct file *file1, *file2, *file3, *file4, *p;
+
+//     for(size_t i=0;i<100;i++) {
+               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);
+               assert(file_init(&file3,TEST_FILE_3)==1);
+               memset(file3->root,3,crypto_hash_sha256_BYTES);
+               assert(file_init(&file4,TEST_FILE_4)==1);
+               memset(file4->root,4,crypto_hash_sha256_BYTES);
+
+               assert(torrent_init(&torrent,TEST_DIRECTORY,TEST_DIRECTORY,16384)==1);
+
+               assert(torrent_add(torrent,file1)==1);
+               assert(torrent_add(torrent,file2)==1);
+               assert(torrent_add(torrent,file3)==1);
+               assert(torrent_add(torrent,file4)==1);
+
+               assert(torrent->tree->entries->file==file3);
+               assert(torrent->tree->entries->next->file==file4);
+               assert(torrent->tree->entries->next->next->file==file1);
+               assert(torrent->tree->entries->next->next->next->file==file2);
+
+               p = hashmap_find(torrent->files,file1->path,strlen(file1->path));
+               assert(p==file1);
+               p = hashmap_find(torrent->files,file1->root,crypto_hash_sha256_BYTES);
+               assert(p==file1);
+
+               p = hashmap_find(torrent->files,file2->path,strlen(file2->path));
+               assert(p==file2);
+               p = hashmap_find(torrent->files,file2->root,crypto_hash_sha256_BYTES);
+               assert(p==file2);
+       
+               p = hashmap_find(torrent->files,file3->path,strlen(file3->path));
+               assert(p==file3);
+               p = hashmap_find(torrent->files,file3->root,crypto_hash_sha256_BYTES);
+               assert(p==file3);
+       
+               p = hashmap_find(torrent->files,file4->path,strlen(file4->path));
+               assert(p==file4);
+               p = hashmap_find(torrent->files,file4->root,crypto_hash_sha256_BYTES);
+               assert(p==file4);
+
+               torrent_free(torrent);
+
+               reset_env();
+//     }
+}
+
+static void torrent_init_basic_test() {
+       struct torrent *torrent;
+
+       assert(torrent_init(&torrent,NULL,NULL,0)<0);
+       assert(torrent_init(&torrent,"src",NULL,0)<0);
+       assert(torrent_init(&torrent,"src","src",1000)<0);
+
+       assert(torrent_init(&torrent,"src","src",16384)==1);
+       torrent_free(torrent);
+}
+
+static void torrent_magnet_basic_test() {
+       struct torrent *torrent;
+       struct file *file1, *file2;
+       char *p;
 
        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);
-       assert(file_init(&file3,TEST_FILE_3)==1);
-       memset(file3->root,3,crypto_hash_sha256_BYTES);
-       assert(file_init(&file4,TEST_FILE_4)==1);
-       memset(file4->root,4,crypto_hash_sha256_BYTES);
-
+       
        assert(torrent_init(&torrent,TEST_DIRECTORY,TEST_DIRECTORY,16384)==1);
-
-       memset(torrent->files->key,0,crypto_shorthash_KEYBYTES);
-
+               
        assert(torrent_add(torrent,file1)==1);
        assert(torrent_add(torrent,file2)==1);
-       assert(torrent_add(torrent,file3)==1);
-       assert(torrent_add(torrent,file4)==1);
-
-       assert(torrent->tree->entries->file==file3);
-       assert(torrent->tree->entries->next->file==file4);
-       assert(torrent->tree->entries->next->next->file==file1);
-       assert(torrent->tree->entries->next->next->next->file==file2);
+       
+       p = torrent_magnet(torrent);
+       assert(p!=NULL);
 
-       assert(0);
+       assert(strcmp(p,"magnet:?xt=urn:btmh:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&dn=" TEST_DIRECTORY)==0);
 
+       free(p);
        torrent_free(torrent);
 
        reset_env();
 }
-
-static void torrent_init_basic_test() {
-       struct torrent *torrent;
-
-       assert(torrent_init(&torrent,NULL,NULL,0)<0);
-       assert(torrent_init(&torrent,"src",NULL,0)<0);
-       assert(torrent_init(&torrent,"src","src",1000)<0);
-
-       assert(torrent_init(&torrent,"src","src",16384)==1);
-       torrent_free(torrent);
-}