From: alex Date: Wed, 20 Oct 2021 22:29:25 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=a36d98ad8a08fbd1640f07875c423ce6e7362721;p=seeder ... --- diff --git a/src/torrent.c b/src/torrent.c index 1b479b6..d50f864 100644 --- a/src/torrent.c +++ b/src/torrent.c @@ -21,27 +21,29 @@ static int torrent_add_file(struct torrent *p, struct file *f) { } void torrent_free(struct torrent *p) { + if(NULL==p) { return; } + if(p->root!=NULL) { free(p->root); } if(p->name!=NULL) { free(p->name); } if(p->files!=NULL) { hashmap_free(p->files); } if(p->file_tree!=NULL) { tree_free(p->file_tree); } + free(p); } #define TORRENT_FILES_HASHMAP_INITIAL_SIZE 8 int torrent_init(struct torrent **torrent_p, const char *root, const char *name) { + if(NULL==torrent_p) { return -1; } + if(NULL==root) { return -1; } + if(NULL==name) { return -1; } + *(torrent_p) = malloc(sizeof(struct torrent)); if(NULL==(*torrent_p)) { perror("malloc"); return -1; } - (*torrent_p)->root = NULL; - (*torrent_p)->name = NULL; - (*torrent_p)->file_tree = NULL; - (*torrent_p)->files = NULL; - (*torrent_p)->root = strdup(root); if(NULL==(*torrent_p)->root) { goto clean; } diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index 4f7e3f4..fd1acd0 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -12,7 +12,7 @@ AM_CPPFLAGS += \ -DNDEBUG endif -check_PROGRAMS = file.tests hashmap.tests tree.tests util.filter.tests +check_PROGRAMS = file.tests hashmap.tests torrent.tests tree.tests util.filter.tests TESTS = $(check_PROGRAMS) if ENABLE_MEMCHECK @@ -32,6 +32,14 @@ hashmap_tests_SOURCES = \ hashmap.tests.c \ $(top_srcdir)/src/hashmap.c +torrent_tests_SOURCES = \ + $(common_SOURCES) \ + torrent.tests.c \ + $(top_srcdir)/src/file.c \ + $(top_srcdir)/src/hashmap.c \ + $(top_srcdir)/src/torrent.c \ + $(top_srcdir)/src/tree.c + tree_tests_SOURCES = \ $(common_SOURCES) \ tree.tests.c \ diff --git a/test/unit/torrent.tests.c b/test/unit/torrent.tests.c new file mode 100644 index 0000000..f88a1a7 --- /dev/null +++ b/test/unit/torrent.tests.c @@ -0,0 +1,26 @@ +#include + +#include + +int main(); +static void torrent_init_basic_test(); + +int main() { + setup_env(); + + torrent_init_basic_test(); + + clean_env(); + + return EXIT_SUCCESS; +} + +static void torrent_init_basic_test() { + struct torrent *torrent; + + assert(torrent_init(&torrent,NULL,NULL)<0); + assert(torrent_init(&torrent,"src",NULL)<0); + + assert(torrent_init(&torrent,"src","src")==1); + torrent_free(torrent); +}