From d6db47103f3dcd93665df369ddf0c6f97950ba0f Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 28 Mar 2022 18:00:45 -0700 Subject: [PATCH] ... --- src/session.c | 13 ++++++++++--- test/unit/session.tests.c | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/session.c b/src/session.c index 3f5c1af..fa843d1 100644 --- a/src/session.c +++ b/src/session.c @@ -7,15 +7,22 @@ static int session_torrent_add_by_path(struct torrent*); static int session_torrent_resize(struct hash_map**,size_t); void session_clean() { - // clear infohashes so no double-free - hashmap_clear(session.torrents.infohashes); + struct torrent *p; + + for(size_t i=0;isize;i++) { + p = session.torrents.paths->map[i]; + if(p!=NULL) { torrent_free(p); } + } + hashmap_clear(session.torrents.infohashes); hashmap_free(session.torrents.infohashes); + + hashmap_clear(session.torrents.paths); hashmap_free(session.torrents.paths); } struct torrent *session_find_torrent(uint8_t *infohash, size_t size) { - return NULL; + return hashmap_find(session.torrents.infohashes,infohash,size); } #define SESSION_HASHMAP_INITIAL_SIZE 8 diff --git a/test/unit/session.tests.c b/test/unit/session.tests.c index 1065ad0..50954ab 100644 --- a/test/unit/session.tests.c +++ b/test/unit/session.tests.c @@ -5,6 +5,7 @@ int main(); static void session_init_basic_test(); static void session_torrent_add_basic_test(); +static void session_find_torrent_basic_test(); int main() { setup_env(); @@ -12,6 +13,8 @@ int main() { session_init_basic_test(); session_torrent_add_basic_test(); + session_find_torrent_basic_test(); + clean_env(); return EXIT_SUCCESS; @@ -23,12 +26,43 @@ static void session_init_basic_test() { session_clean(); } -static void session_torrent_add_basic_test() { - struct torrent *p; +static void session_find_torrent_basic_test() { + uint8_t infohash[crypto_hash_sha256_BYTES]; + struct torrent *p, *p2; + + assert(1==session_init()); + memset(session.torrents.infohashes->key,0,crypto_shorthash_KEYBYTES); TORRENT_SETUP_EXAMPLE(&p); + memset(p->infohash,1,crypto_hash_sha256_BYTES); + assert(1==session_torrent_add(p)); + + memcpy(infohash,p->infohash,crypto_hash_sha256_BYTES); + + p2 = session_find_torrent(infohash,crypto_hash_sha256_BYTES); + assert(p==p2); + + p2 = session_find_torrent(infohash,crypto_hash_sha256_BYTES); + assert(p==p2); + + memset(infohash,2,crypto_hash_sha256_BYTES); + p2 = session_find_torrent(infohash,crypto_hash_sha256_BYTES); + assert(NULL==p2); + + memset(infohash,1,crypto_hash_sha256_BYTES); + p2 = session_find_torrent(infohash,crypto_hash_sha256_BYTES); + assert(p==p2); + + session_clean(); +} + +static void session_torrent_add_basic_test() { + struct torrent *p; assert(1==session_init()); + TORRENT_SETUP_EXAMPLE(&p); assert(1==session_torrent_add(p)); + + session_clean(); } -- 2.30.2