]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Tue, 29 Mar 2022 01:00:45 +0000 (18:00 -0700)
committeralex <[email protected]>
Tue, 29 Mar 2022 01:00:45 +0000 (18:00 -0700)
src/session.c
test/unit/session.tests.c

index 3f5c1af0f5b84bb773e9cdc6c3855924e605b5f0..fa843d1b273b945023e24d9a82ad447e46678b45 100644 (file)
@@ -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;i<session.torrents.paths->size;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
index 1065ad07cfdff5a26821f78059db67d9c6394681..50954ab40ec095602518135a339d8dad659b73b6 100644 (file)
@@ -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();
 }