#include<net.h>
#include<piece.h>
#include<session.h>
+#include<torrent.h>
int peer_bitfield(struct peer*);
int peer_bitfield_received(struct peer*,ssize_t);
#include<stddef.h>
#include<stdint.h>
+#include<torrent.h>
+
struct piece {
const char *path;
uint64_t start;
size_t data_size;
};
-#include<torrent.h>
-
int piece_init(struct piece**);
void piece_free(struct piece*);
-struct piece* piece_load(const struct torrent*,size_t);
#endif
struct tree *tree;
struct torrent_files files;
- struct piece **pieces;
+ unsigned char **pieces;
size_t pieces_size;
uint8_t infohash[crypto_hash_sha256_BYTES];
int torrent_init(struct torrent**,unsigned long long);
int torrent_infohash(struct torrent*);
char *torrent_magnet(struct torrent*);
+struct piece *torrent_piece_load(const struct torrent*,size_t);
int torrent_remove(struct torrent*,const char*);
#endif
}
static int add_find_all(struct torrent *p) {
+ if(NULL==p) { return -1; }
+
log_info(ADD_MESSAGE_ADDING_TORRENT,p->root,p->name);
current_torrent = p;
p = info->piece_requests;
- piece = piece_load(info->torrent,p->index);
+ piece = torrent_piece_load(info->torrent,p->index);
if(NULL==piece) { return -1; }
if( p->begin + p->length > piece->data_size ) { return -1; }
return -1;
}
-struct piece *piece_load(const struct torrent *torrent, size_t index) {
- return NULL;
-}
#include<torrent.h>
-static int piece_layer_sort(const void*, const void*);
+static int piece_layers_sort(const void*, const void*);
static int torrent_bencode_piece_layers(FILE*,struct file*);
static int torrent_piece_layers(struct torrent*);
+static int piece_layers_sort(const void *p1, const void *p2) {
+ if(NULL==p1) { return 1; }
+ if(NULL==p2) { return -1; }
+
+ return memcmp(*(unsigned char**)p1,*(unsigned char**)p2,crypto_hash_sha256_BYTES);
+}
+
#define PIECE_LAYER_BENCODED_LENGTH 2+1+32+2+1+32
static int torrent_bencode_piece_layers(FILE *fp, struct file *file_p) {
return 1;
}
-static int torrent_piece_layers(struct torrent *p) {
+static int torrent_piece_layers(struct torrent *torrent) {
struct file *file_p;
- struct block *block_p;
size_t index, pieces;
pieces = 0;
- for(size_t i=0;i<p->files.roots->size;i++) {
- file_p = (struct file*)p->files.roots->map[i];
+ for(size_t i=0;i<torrent->files.roots->size;i++) {
+ file_p = (struct file*)torrent->files.roots->map[i];
if(file_p!=NULL) {
- if(file_p->size>p->piece_length) { pieces++; }
+ if(file_p->size>torrent->piece_length) { pieces++; }
}
}
- // sort hashes first
- qsort(hashes,pieces,sizeof(const unsigned char*),&piece_layers_sort);
-
- torrent->pieces = malloc(sizeof(struct piece)*pieces);
+ torrent->pieces = malloc(sizeof(unsigned char*)*pieces);
if(NULL==torrent->pieces) { return -1; }
index = 0;
- for(size_t i=0;i<p->files.roots->size;i++) {
- file_p = (struct file*)p->files.roots->map[i];
+ for(size_t i=0;i<torrent->files.roots->size;i++) {
+ file_p = (struct file*)torrent->files.roots->map[i];
if(file_p!=NULL) {
- if(file_p->size>p->piece_length) {
- block_p = file_p->piece_layers;
- while(block_p!=NULL) {
- torrent->pieces[index] = file_p->root->hash;
- index++;
- }
- }
+ torrent->pieces[index] = file_p->root->hash;
+ index++;
}
}
+
+ qsort(torrent->pieces,pieces,sizeof(const unsigned char*),&piece_layers_sort);
- return -1;
+ return 1;
}
int torrent_file_piece_layers(FILE *fp, struct torrent *p) {
+ struct file *file_p;
+
if(torrent_piece_layers(p)<0) { return -1; }
for(size_t i=0;i<p->pieces_size;i++) {
+ file_p = hashmap_find(p->files.roots,p->pieces[i],crypto_hash_sha256_BYTES);
+ if(NULL==file_p) { return -1; }
+
if(torrent_bencode_piece_layers(fp,file_p)<0) { return -1; }
}
return 1;
}
-static int piece_layers_sort(const void *p1, const void *p2) {
- if(NULL==p1) { return 1; }
- if(NULL==p2) { return -1; }
-
- return memcmp(*(unsigned char**)p1,*(unsigned char**)p2,crypto_hash_sha256_BYTES);
+struct piece *torrent_piece_load(const struct torrent *torrent, size_t index) {
+ return NULL;
}
-DNDEBUG
endif
-check_PROGRAMS = bencode.tests block.tests feed.tests file.tests fs.concat.tests fs.filter.tests hash.tests hashmap.tests meta.tests net.tests opt.tests peer.tests pqueue.tests rss.tests session.tests torrent.tests tree.tests
+check_PROGRAMS = add.tests bencode.tests block.tests feed.tests file.tests fs.concat.tests fs.filter.tests hash.tests hashmap.tests meta.tests net.tests opt.tests peer.tests pqueue.tests rss.tests session.tests torrent.tests tree.tests
TESTS = $(check_PROGRAMS)
if ENABLE_MEMCHECK
$(top_srcdir)/src/torrent/remove.c \
$(top_srcdir)/src/tree.c
+add_tests_SOURCES = \
+ $(common_SOURCES) \
+ $(torrent_SOURCES) \
+ add.tests.c \
+ $(top_srcdir)/src/fs/filter.c \
+ $(top_srcdir)/src/hash.c \
+ $(top_srcdir)/src/session.c
+
+add_tests_CPPFLAGS = $(AM_CPPFLAGS) \
+ -DADD_SRC_FILE="$(top_srcdir)/src/add.c"
+
bencode_tests_SOURCES = \
$(common_SOURCES) \
bencode.tests.c \
--- /dev/null
+#include<test_utils.h>
+
+#include<add.h>
+
+#include INCLUDE(ADD_SRC_FILE)
+
+/* dummy functions */
+struct options global_options;
+void log_message(enum log_level lvl, FILE *fp, const char *format,...) { return; }
+/* end dummy functions */
+
+int main();
+static void add_find_all_basic_test();
+static void add_queue_resize_basic_test();
+static void add_to_queue_basic_test();
+
+int main() {
+ setup_env();
+
+ add_find_all_basic_test();
+ add_queue_resize_basic_test();
+ add_to_queue_basic_test();
+
+ clean_env();
+
+ return EXIT_SUCCESS;
+}
+
+static void add_find_all_basic_test() {
+ struct torrent *p;
+
+ TORRENT_SETUP_EMPTY_EXAMPLE(&p);
+
+ assert(hashmap_init(&add_queue,ADD_QUEUE_INITIAL_SIZE)==1);
+ current_torrent = p;
+
+ assert(-1==add_find_all(NULL));
+ assert(-1==add_find_all(NULL));
+
+ assert(1==add_find_all(p));
+
+ assert(0);
+}
+
+static void add_queue_resize_basic_test() {
+ assert(0);
+}
+static void add_to_queue_basic_test() {
+ assert(0);
+}
int peer_keepalive_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_KEEPALIVE+1]++; return 0; }
int peer_not_interested_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_NOT_INTERESTED+1]++; return 0; }
int peer_piece_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_PIECE+1]++; return 0; }
+int peer_queue_process(struct peer *info) { return 1; }
int peer_reject_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_REJECT+1]++; return 0; }
int peer_request_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_REQUEST+1]++; return 0; }
int peer_unchoke_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_UNCHOKE+1]++; return 0; }
static void opt_add_watch_basic_test() {
assert(-1==opt_add_watch(NULL));
assert(-1==opt_add_watch("notarealexistingdirectorylksdjflkajsdfklajsklf"));
+ assert(-1==opt_add_watch("notareal/existingdi/rectorylks/djflkajsd/fklajsklf"));
opt_set_log_level(LOG_LEVEL_SILENT);
assert(1==session_init());
assert(torrent_add(*p,file2)==1); \
}
+/* Parameters:
+ * struct torrent **p
+ */
+#define TORRENT_SETUP_EMPTY_EXAMPLE(p) { \
+ assert(torrent_init(p,16384)==1); \
+\
+ (*p)->root = strdup(TEST_DIRECTORY); \
+ assert((*p)->root!=NULL); \
+ (*p)->name = strdup(TEST_DIRECTORY); \
+ assert((*p)->name!=NULL); \
+ (*p)->feed_url = strdup("https://test.com"); \
+ assert((*p)->feed_url!=NULL); \
+}
+
#endif