struct file *p;
int i;
- if((i = hashmap_insert(files,f->root,crypto_hash_sha256_BYTES,f))<=0) {
+ if((i = hashmap_insert(files,f->root->hash,crypto_hash_sha256_BYTES,f))<=0) {
if(i<0) { return -1; }
- p = hashmap_find(files,f->root,crypto_hash_sha256_BYTES);
+ p = hashmap_find(files,f->root->hash,crypto_hash_sha256_BYTES);
if((p!=NULL)&&(file_equals(f,p)<1)) { return 0; }
}
goto resize;
}
} else {
- if((ret = hashmap_insert(new,p->root,crypto_hash_sha256_BYTES,p))<=0) {
+ if((ret = hashmap_insert(new,p->root->hash,crypto_hash_sha256_BYTES,p))<=0) {
if(ret<0) { goto clean; }
goto resize;
}
static int torrent_piece_layers(struct torrent *torrent) {
struct file *file_p;
- size_t index, pieces;
+ size_t index;
- pieces = 0;
+ torrent->pieces_size = 0;
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>torrent->piece_length) { pieces++; }
+ if(file_p->size>torrent->piece_length) { torrent->pieces_size++; }
}
}
- torrent->pieces = malloc(sizeof(unsigned char*)*pieces);
+ /*
+ * This is possible if all files are smaller than
+ * piece size.
+ */
+ if(0==torrent->pieces_size) { return 0; }
+
+ torrent->pieces = malloc(sizeof(unsigned char*)*torrent->pieces_size);
if(NULL==torrent->pieces) { return -1; }
index = 0;
}
}
- qsort(torrent->pieces,pieces,sizeof(const unsigned char*),&piece_layers_sort);
+ qsort(torrent->pieces,torrent->pieces_size,sizeof(const unsigned char*),&piece_layers_sort);
return 1;
}
#include<file.h>
int main();
-static void extend_file(const char*);
static void file_bencode_basic_test();
static void file_hash_basic_test();
static void file_hash_large_file_test();
return EXIT_SUCCESS;
}
-static void extend_file(const char *file) {
- unsigned char buf[16384];
- FILE *fp;
-
- fp = fopen(file,"a");
- assert(fp!=NULL);
-
- for(size_t i=0;i<10000;i++) {
- memset(buf,i%255,16384);
- assert(16384==fwrite(buf,sizeof(char),16384,fp));
- }
-
- fclose(fp);
-}
-
static void file_bencode_basic_test() {
struct file *p;
uint8_t buf[100];
struct file *p;
unsigned char expected[crypto_hash_sha256_BYTES] = {58,71,55,213,6,94,65,97,113,64,176,175,139,67,229,164,13,18,213,164,166,129,202,162,224,45,9,227,191,155,144,144};
- extend_file(TEST_FILE_1);
+ EXTEND_FILE(TEST_FILE_1);
assert(file_init(&p,TEST_FILE_1)==1);
assert(file_hash(p,16384)==1);
size_t blocks = 10001;
for(int i=16384;i<131073;i<<=1) {
- extend_file(TEST_FILE_2);
+ EXTEND_FILE(TEST_FILE_2);
assert(1==file_init(&p,TEST_FILE_2));
assert(file_hash(p,i)==1);
#define TEST_FILE_9 PREFIX "/.meta"
#define TEST_FILE_9_CONTENTS "title=TITLE\nlink=http://test.com\ndescription=what is a description\nlanguage=en-us\nlastBuildDate=Wed, 29 Dec 2021 12:21:12 +0000"
+/*
+ * Parameters:
+ * const char *file
+ */
+#define EXTEND_FILE(file) { \
+ unsigned char buf[16384]; \
+ FILE *fp; \
+\
+ fp = fopen(file,"a"); \
+ assert(fp!=NULL); \
+\
+ for(size_t i=0;i<10000;i++) { \
+ memset(buf,i%255,16384); \
+ assert(16384==fwrite(buf,sizeof(char),16384,fp)); \
+ } \
+\
+ fclose(fp);\
+}
+
/* Parameters:
* const char *path
* unsigned char *hash
static void torrent_file_info_conformance_test();
static void torrent_file_path_basic_test();
static void torrent_file_piece_layers_basic_test();
+static void torrent_file_piece_layers_conformance_test();
static void torrent_init_basic_test();
static void torrent_magnet_basic_test();
int main() {
setup_env();
- torrent_init_basic_test();
torrent_add_basic_test();
- torrent_file_piece_layers_basic_test();
- torrent_magnet_basic_test();
- torrent_file_info_basic_test();
torrent_file_basic_test();
+ torrent_file_info_basic_test();
torrent_file_info_conformance_test();
torrent_file_path_basic_test();
+ torrent_file_piece_layers_basic_test();
+ torrent_file_piece_layers_conformance_test();
+ torrent_init_basic_test();
+ torrent_magnet_basic_test();
clean_env();
p = hashmap_find(torrent->files.paths,file1->path,strlen(file1->path));
assert(p==file1);
- p = hashmap_find(torrent->files.roots,file1->root,crypto_hash_sha256_BYTES);
+ p = hashmap_find(torrent->files.roots,file1->root->hash,crypto_hash_sha256_BYTES);
assert(p==file1);
p = hashmap_find(torrent->files.paths,file2->path,strlen(file2->path));
assert(p==file2);
- p = hashmap_find(torrent->files.roots,file2->root,crypto_hash_sha256_BYTES);
+ p = hashmap_find(torrent->files.roots,file2->root->hash,crypto_hash_sha256_BYTES);
assert(p==file2);
p = hashmap_find(torrent->files.paths,file3->path,strlen(file3->path));
assert(p==file3);
- p = hashmap_find(torrent->files.roots,file3->root,crypto_hash_sha256_BYTES);
+ p = hashmap_find(torrent->files.roots,file3->root->hash,crypto_hash_sha256_BYTES);
assert(p==file3);
p = hashmap_find(torrent->files.paths,file4->path,strlen(file4->path));
assert(p==file4);
- p = hashmap_find(torrent->files.roots,file4->root,crypto_hash_sha256_BYTES);
+ p = hashmap_find(torrent->files.roots,file4->root->hash,crypto_hash_sha256_BYTES);
assert(p==file4);
torrent_free(torrent);
reset_env();
}
+static void torrent_file_piece_layers_conformance_test() {
+ struct torrent *torrent;
+ struct file *file1, *file2;
+ FILE *fp;
+ unsigned char hash[crypto_hash_sha256_BYTES];
+ unsigned char expected[crypto_hash_sha256_BYTES] = {155,62,214};
+// 9b3ed6930ff8abc0cb657b9683cabb54c0e80128f7a7fe5c0f8e1af32de84b95
+
+ EXTEND_FILE(TEST_FILE_1);
+ assert(file_init(&file1,TEST_FILE_1)==1);
+ assert(file_hash(file1,16384)==1);
+ EXTEND_FILE(TEST_FILE_2);
+ assert(file_init(&file2,TEST_FILE_2)==1);
+ assert(file_hash(file2,16384)==1);
+
+ TORRENT_SETUP_EMPTY_EXAMPLE(&torrent);
+ assert(torrent_add(torrent,file1)==1);
+ assert(torrent_add(torrent,file2)==1);
+
+ fp = fopen(TEST_FILE_6,"w");
+ assert(fp!=NULL);
+
+ assert(torrent_file_piece_layers(fp,torrent)==1);
+
+ assert(0==fclose(fp));
+
+ HASH_FILE(TEST_FILE_6,hash,crypto_hash_sha256_BYTES);
+
+ assert(memcmp(hash,expected,crypto_hash_sha256_BYTES)==0);
+
+ torrent_free(torrent);
+
+ reset_env();
+}
+
static void torrent_init_basic_test() {
struct torrent *torrent;