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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
#include "unity.h"
#include "peers.h"
#include "log.h"
#include <string.h>
#include <arpa/inet.h>
#define IP(a, b, c, d) htonl(a << 24 | b << 16 | c << 8 | d)
void test_add_single_peer() {
allocate_hashtable();
struct infohash sometorrent = {.inner={0x0034048f, 0x08000020, 0x00888880, 0x02008460, 0x0ab00521}};
struct addr addr = (struct addr){.ip = IP(128,0,0,1), .port = 0};
int rc = add_peer(&sometorrent, &addr);
TEST_ASSERT_EQUAL(0, rc);
}
void test_add_9_peers_same_torrent() {
allocate_hashtable();
struct infohash sometorrent = {.inner={0x0034048f, 0x08000020, 0x00888880, 0x02008460, 0x0ab00521}};
struct addr addr = (struct addr){.ip = IP(128,0,0,1), .port = 0};
int rc = add_peer(&sometorrent, &addr);
TEST_ASSERT_EQUAL(0, rc);
rc = add_peer(&sometorrent, &addr);
TEST_ASSERT_EQUAL(0, rc);
rc = add_peer(&sometorrent, &addr);
TEST_ASSERT_EQUAL(0, rc);
rc = add_peer(&sometorrent, &addr);
TEST_ASSERT_EQUAL(0, rc);
rc = add_peer(&sometorrent, &addr);
TEST_ASSERT_EQUAL(0, rc);
rc = add_peer(&sometorrent, &addr);
TEST_ASSERT_EQUAL(0, rc);
rc = add_peer(&sometorrent, &addr);
TEST_ASSERT_EQUAL(0, rc);
rc = add_peer(&sometorrent, &addr);
TEST_ASSERT_EQUAL(0, rc);
// There's room for 8 peers per infohash
rc = add_peer(&sometorrent, &addr);
TEST_ASSERT_EQUAL(PEER_EFULL, rc);
}
void test_peers_for_only_torrent() {
allocate_hashtable();
struct infohash sometorrent = {.inner={0x0034048f, 0x08000020, 0x00888880, 0x02008460, 0x0ab00521}};
struct infohash othertorrent = {.inner={0x0034048f, 0x08000020, 0x00888880, 0x02008460, 0x0ab00522}};
struct addr someaddr = (struct addr){.ip = IP(128,0,0,1), .port = 0};
struct addr otheraddr = (struct addr){.ip = IP(128,0,0,1), .port = 1};
int rc = add_peer(&sometorrent, &someaddr);
TEST_ASSERT_EQUAL(0, rc);
rc = add_peer(&othertorrent, &otheraddr);
TEST_ASSERT_EQUAL(0, rc);
struct addr* found;
size_t found_len;
get_peers(&sometorrent, &found, &found_len);
TEST_ASSERT_EQUAL(1, found_len);
TEST_ASSERT_EQUAL_MEMORY(&someaddr, &found[0], sizeof(struct addr));
get_peers(&othertorrent, &found, &found_len);
TEST_ASSERT_EQUAL(1, found_len);
TEST_ASSERT_EQUAL_MEMORY(&otheraddr, &found[0], sizeof(struct addr));
}
void test_have_no_peers() {
allocate_hashtable();
struct infohash sometorrent = {.inner={0x0034048f, 0x08000020, 0x00888880, 0x02008460, 0x0ab00521}};
struct addr* found;
size_t found_len;
get_peers(&sometorrent, &found, &found_len);
TEST_ASSERT_NULL(found);
TEST_ASSERT_EQUAL(0, found_len);
}
void test_grows() {
allocate_hashtable();
struct addr addr = (struct addr){.ip = IP(128,0,0,1), .port = 0};
for(int i = 0; i < 17; i++) { // Peer table size + 1 to make sure it HAS to grow
struct infohash sometorrent = {.inner={0x0034048f, 0x08000020, 0x00888880, 0x02008460, i}};
int rc = add_peer(&sometorrent, &addr);
TEST_ASSERT_EQUAL(0, rc);
}
{
// We can find a peer after the resize again
struct infohash sometorrent = {.inner={0x0034048f, 0x08000020, 0x00888880, 0x02008460, 0x00000001}};
struct addr* found;
size_t found_len;
get_peers(&sometorrent, &found, &found_len);
TEST_ASSERT_NOT_NULL(found);
TEST_ASSERT_EQUAL(1, found_len);
}
}
|