-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
69 lines (63 loc) · 1.66 KB
/
test.c
File metadata and controls
69 lines (63 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <openssl/sha.h>
#include "bencode.h"
bool
test_file(const char *file)
{
be_dict *dict = decode_file(file);
if(dict==NULL) {
printf("Syntax error\n");
return false;
}
else {
// The hashes for the torrent files provided should be:
// F1:FC:DC:14:62:D3:65:30:F5:26:C1:D9:40:2E:EC:91:00:B7:BA:18
// FB:4E:21:C8:42:74:A8:BA:AF:0F:E2:12:18:B5:94:2C:5C:02:14:06
// 21:DD:64:01:FE:71:25:0D:29:68:03:8D:D6:CA:07:EB:8D:60:DF:49
// Also check if the dictionary even has a info-hash
if(dict->has_info_hash) hex_dump(dict->info_hash, SHA_DIGEST_LENGTH);
void *val; be_type type;
unsigned char *key = (unsigned char*)"info";
val = dict_get(dict, key, &type);
// Can also do the following
// val = dict_get(dict, (unsigned char*)"info", &type);
if(val==NULL || type!=BE_DICT) {
dict_destroy(dict);
return false;
}
// Casting the void pointer after type has been checked
be_dict *info = (be_dict*)val;
key = (unsigned char*)"name";
val = dict_get(info, key, &type);
if(val==NULL || type!=BE_STR) {
dict_destroy(dict);
return false;
}
// Note that strings are stored in structs
be_string *string = (be_string*)val;
printf("%s\n", string->str);
// Uncomment the following line to dump the whole dictionary too
// dict_dump(dict);
}
dict_destroy(dict);
return true;
}
int
main()
{
int len = 3;
const char *files[len];
files[0] = "torrents/ubuntu.torrent";
files[1] = "torrents/opensuse.torrent";
files[2] = "torrents/fedora.torrent";
for(int i=0; i<len; ++i) {
if(!test_file(files[i])) {
printf("Failed\n");
return 1;
}
printf("Passed\n");
}
return 0;
}