diff options
| author | Jesper Jensen <jesper@jnsn.dev> | 2023-07-23 12:15:34 +0200 |
|---|---|---|
| committer | Jesper Jensen <jesper@jnsn.dev> | 2025-04-12 10:21:20 +0200 |
| commit | 47d4780eb448f839fc6b0644135395f879080ba4 (patch) | |
| tree | 2c9a84bf45a4b8af5411331db50173fc5a34f630 /test/peers.c | |
| parent | 3515701479fe04f73e9194ee3074457bb85030f6 (diff) | |
I don't remember
Diffstat (limited to 'test/peers.c')
| -rw-r--r-- | test/peers.c | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/test/peers.c b/test/peers.c new file mode 100644 index 0000000..1e9a454 --- /dev/null +++ b/test/peers.c @@ -0,0 +1,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); + } +} |
