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
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();
session_init_basic_test();
session_torrent_add_basic_test();
+ session_find_torrent_basic_test();
+
clean_env();
return EXIT_SUCCESS;
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();
}