From edead1ba1091e3ac768eb52beb8afd00f409b7ef Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 6 Dec 2021 17:18:00 -0800 Subject: [PATCH] ... --- .gitignore | 3 ++ src/torrent/add.c | 8 +-- src/torrent/magnet.c | 33 +++++++++++- test/unit/Makefile.am | 2 + test/unit/torrent.tests.c | 104 +++++++++++++++++++++++++++----------- 5 files changed, 113 insertions(+), 37 deletions(-) diff --git a/.gitignore b/.gitignore index 1a7cd80..16b89ea 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,6 @@ stamp-h1 # binaries seederd + +# test files/objects +.test/ diff --git a/src/torrent/add.c b/src/torrent/add.c index c23f5f0..6b80dd1 100644 --- a/src/torrent/add.c +++ b/src/torrent/add.c @@ -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;ifiles->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) diff --git a/src/torrent/magnet.c b/src/torrent/magnet.c index 16a006d..3bce8e2 100644 --- a/src/torrent/magnet.c +++ b/src/torrent/magnet.c @@ -1,5 +1,36 @@ #include -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; } diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index 62c5b31..b975363 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -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 = \ diff --git a/test/unit/torrent.tests.c b/test/unit/torrent.tests.c index 93470d6..38de89c 100644 --- a/test/unit/torrent.tests.c +++ b/test/unit/torrent.tests.c @@ -3,14 +3,16 @@ #include 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); -} -- 2.30.2