diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/integration/test_stats.py | 25 | ||||
| -rw-r--r-- | tests/integration/test_version.py | 36 | ||||
| -rw-r--r-- | tests/runner.c | 1 | ||||
| -rw-r--r-- | tests/stubs.c | 33 | ||||
| -rw-r--r-- | tests/t_trackerlogic.c | 243 | ||||
| -rw-r--r-- | tests/test.h | 1 |
6 files changed, 330 insertions, 9 deletions
diff --git a/tests/integration/test_stats.py b/tests/integration/test_stats.py index 820b917..088d8a1 100644 --- a/tests/integration/test_stats.py +++ b/tests/integration/test_stats.py | |||
| @@ -15,10 +15,10 @@ import xml.etree.ElementTree | |||
| 15 | from harness import TrackerTestCase | 15 | from harness import TrackerTestCase |
| 16 | 16 | ||
| 17 | # The modes a default build answers. "tpbs" and "statedump" are full scrapes | 17 | # The modes a default build answers. "tpbs" and "statedump" are full scrapes |
| 18 | # and belong to test_fullscrape; "fulllog" and "woodpeckers" sit behind | 18 | # and belong to test_fullscrape; "woodpeckers" sits behind a |
| 19 | # feature flags this build does not set. | 19 | # feature flag this build does not set. |
| 20 | MRTG_MODES = ["peer", "conn", "scrp", "udp4", "tcp4", "torr", "fscr", "completed", "syncs"] | 20 | MRTG_MODES = ["peer", "conn", "scrp", "udp4", "tcp4", "torr", "fscr", "completed", "syncs"] |
| 21 | TEXT_MODES = ["herr", "s24s", "top10", "top100", "renew", "version"] | 21 | TEXT_MODES = ["herr", "s24s", "top10", "top100", "renew"] |
| 22 | 22 | ||
| 23 | 23 | ||
| 24 | class StatsShape(TrackerTestCase): | 24 | class StatsShape(TrackerTestCase): |
| @@ -55,9 +55,22 @@ class StatsShape(TrackerTestCase): | |||
| 55 | self.assertIsNotNone(root.find("torrents/count_mutex")) | 55 | self.assertIsNotNone(root.find("torrents/count_mutex")) |
| 56 | self.assertIsNotNone(root.find("connections/tcp/announce")) | 56 | self.assertIsNotNone(root.find("connections/tcp/announce")) |
| 57 | 57 | ||
| 58 | def test_version_names_a_commit(self): | 58 | def test_the_version_mode_is_gone(self): |
| 59 | self.assertIn(b"opentracker", self.tracker.stats("version").lower() + b"opentracker") | 59 | """Which build a tracker is and what went into it is of use to whoever |
| 60 | self.assertRegex(self.tracker.stats("version"), rb"https?://") | 60 | attacks it and to nobody else on the far end of a socket. It is |
| 61 | reported by -v, to whoever already has the binary.""" | ||
| 62 | with self.assertRaises(urllib.error.HTTPError) as caught: | ||
| 63 | self.tracker.stats("version") | ||
| 64 | self.assertEqual(caught.exception.code, 400, "an unknown mode, like any other") | ||
| 65 | |||
| 66 | def test_everything_does_not_name_the_build(self): | ||
| 67 | self.assertNotIn(b"opentracker 1", self.tracker.stats("everything")) | ||
| 68 | |||
| 69 | def test_everything_does_not_name_the_tracker_id(self): | ||
| 70 | """The id a tracker recognises its own live sync packets by. Knowing | ||
| 71 | it is of no use to a client and of some use to whoever would like a | ||
| 72 | tracker to discard what it is told.""" | ||
| 73 | self.assertNotIn(b"tracker_id", self.tracker.stats("everything")) | ||
| 61 | 74 | ||
| 62 | 75 | ||
| 63 | class StatsCount(TrackerTestCase): | 76 | class StatsCount(TrackerTestCase): |
diff --git a/tests/integration/test_version.py b/tests/integration/test_version.py new file mode 100644 index 0000000..66f570d --- /dev/null +++ b/tests/integration/test_version.py | |||
| @@ -0,0 +1,36 @@ | |||
| 1 | """The -v report, which is where a build says what went into it. | ||
| 2 | |||
| 3 | Driving the binary is the only way to see this: the feature list is a table | ||
| 4 | of #ifdefs, so it is only true for the flags the binary itself was built with. | ||
| 5 | """ | ||
| 6 | |||
| 7 | import subprocess | ||
| 8 | import unittest | ||
| 9 | |||
| 10 | from harness import TRACKER | ||
| 11 | |||
| 12 | |||
| 13 | class VersionFlag(unittest.TestCase): | ||
| 14 | def report(self): | ||
| 15 | # -v writes to stderr and exits before it binds anything | ||
| 16 | done = subprocess.run([TRACKER, "-v"], capture_output=True, timeout=30) | ||
| 17 | self.assertEqual(done.returncode, 0, done.stderr) | ||
| 18 | return done.stderr.decode() | ||
| 19 | |||
| 20 | def test_first_line_names_the_release(self): | ||
| 21 | # The commit in brackets is there in a checkout and in a release | ||
| 22 | # tarball, and absent in a build that had neither to ask. | ||
| 23 | first = self.report().splitlines()[0] | ||
| 24 | self.assertRegex(first, r"^opentracker \d+\.\d+\.\d+( \(\S+\))?$") | ||
| 25 | |||
| 26 | def test_names_the_event_mechanism(self): | ||
| 27 | self.assertRegex(self.report(), r"(?m)^events: (epoll|kqueue|poll)$") | ||
| 28 | |||
| 29 | def test_lists_the_features_this_build_has(self): | ||
| 30 | # The Makefile builds this binary with these and not with those, so a | ||
| 31 | # feature table that stops following the build fails here. | ||
| 32 | report = self.report() | ||
| 33 | for want in ("WANT_KEEPALIVE", "WANT_ACCESSLIST_BLACK", "WANT_DYNAMIC_ACCESSLIST"): | ||
| 34 | self.assertIn(want, report) | ||
| 35 | for unwanted in ("WANT_SPOT_WOODPECKER", "WANT_V4_ONLY", "WANT_RESTRICT_STATS"): | ||
| 36 | self.assertNotIn(unwanted, report) | ||
diff --git a/tests/runner.c b/tests/runner.c index dcb009c..97c6d51 100644 --- a/tests/runner.c +++ b/tests/runner.c | |||
| @@ -28,6 +28,7 @@ static const struct module modules[] = {{"ot_urlencoded", test_urlencoded}, | |||
| 28 | {"ot_ip", test_ip}, | 28 | {"ot_ip", test_ip}, |
| 29 | {"ot_socket", test_socket}, | 29 | {"ot_socket", test_socket}, |
| 30 | {"ot_mutex", test_mutex}, | 30 | {"ot_mutex", test_mutex}, |
| 31 | {"trackerlogic", test_trackerlogic}, | ||
| 31 | {"ot_workqueue", test_workqueue}, | 32 | {"ot_workqueue", test_workqueue}, |
| 32 | {"ot_batch", test_batch}, | 33 | {"ot_batch", test_batch}, |
| 33 | {"ot_fdstate", test_fdstate}, | 34 | {"ot_fdstate", test_fdstate}, |
diff --git a/tests/stubs.c b/tests/stubs.c index a8e6962..5999e2d 100644 --- a/tests/stubs.c +++ b/tests/stubs.c | |||
| @@ -8,11 +8,9 @@ | |||
| 8 | behaviour should be supplied here rather than the test weakened. */ | 8 | behaviour should be supplied here rather than the test weakened. */ |
| 9 | 9 | ||
| 10 | /* Opentracker */ | 10 | /* Opentracker */ |
| 11 | #include "ot_stats.h" | ||
| 11 | #include "trackerlogic.h" | 12 | #include "trackerlogic.h" |
| 12 | 13 | ||
| 13 | /* ot_vector.c calls this from vector_clean_list, which no test drives yet. */ | ||
| 14 | void free_peerlist(ot_peerlist *peer_list) { (void)peer_list; } | ||
| 15 | |||
| 16 | /* ot_accesslist.c is built with WANT_ACCESSLIST_BLACK so that the hash parser | 14 | /* ot_accesslist.c is built with WANT_ACCESSLIST_BLACK so that the hash parser |
| 17 | is reachable. These two belong to accesslist_readfile and the reload worker, | 15 | is reachable. These two belong to accesslist_readfile and the reload worker, |
| 18 | neither of which any test drives. */ | 16 | neither of which any test drives. */ |
| @@ -26,3 +24,32 @@ long io_trywrite(long d, const char *buf, long len) { | |||
| 26 | (void)buf; | 24 | (void)buf; |
| 27 | return len; | 25 | return len; |
| 28 | } | 26 | } |
| 27 | |||
| 28 | /* trackerlogic.c is under test, and starting it up starts every module it | ||
| 29 | leans on. None of them is driven by these tests, so they are answered here | ||
| 30 | rather than linked in and left to run threads of their own. */ | ||
| 31 | void clean_init(void) {} | ||
| 32 | void clean_deinit(void) {} | ||
| 33 | int clean_single_torrent(ot_torrent *torrent) { | ||
| 34 | (void)torrent; | ||
| 35 | return 0; | ||
| 36 | } | ||
| 37 | void fullscrape_init(void) {} | ||
| 38 | void fullscrape_deinit(void) {} | ||
| 39 | void livesync_init(void) {} | ||
| 40 | void livesync_deinit(void) {} | ||
| 41 | void livesync_tell(struct ot_workstruct *ws) { (void)ws; } | ||
| 42 | void stats_init(void) {} | ||
| 43 | void stats_deinit(void) {} | ||
| 44 | void stats_issue_event(struct ot_workstruct *ws, ot_status_event event, PROTO_FLAG proto, uintptr_t event_data) { | ||
| 45 | (void)ws; | ||
| 46 | (void)event; | ||
| 47 | (void)proto; | ||
| 48 | (void)event_data; | ||
| 49 | } | ||
| 50 | void stats_counters_register(struct ot_workstruct *ws) { (void)ws; } | ||
| 51 | void stats_counters_retire(struct ot_workstruct *ws) { (void)ws; } | ||
| 52 | |||
| 53 | const char *g_stats_path; | ||
| 54 | ssize_t g_stats_path_len; | ||
| 55 | uint32_t g_tracker_id; | ||
diff --git a/tests/t_trackerlogic.c b/tests/t_trackerlogic.c new file mode 100644 index 0000000..d3d8755 --- /dev/null +++ b/tests/t_trackerlogic.c | |||
| @@ -0,0 +1,243 @@ | |||
| 1 | /* This software was written by Dirk Engling <erdgeist@erdgeist.org> | ||
| 2 | It is considered beerware. Prost. Skol. Cheers or whatever. | ||
| 3 | |||
| 4 | $id$ */ | ||
| 5 | |||
| 6 | /* System */ | ||
| 7 | #include <arpa/inet.h> | ||
| 8 | #include <signal.h> | ||
| 9 | #include <pthread.h> | ||
| 10 | #include <string.h> | ||
| 11 | |||
| 12 | /* Opentracker */ | ||
| 13 | #include "ot_mutex.h" | ||
| 14 | #include "trackerlogic.h" | ||
| 15 | #include "test.h" | ||
| 16 | |||
| 17 | /* The swarm itself: what an announce does to a torrent, and what a scrape | ||
| 18 | says about it afterwards. The counts are read back through the udp scrape | ||
| 19 | because that is the one reply the tracker builds as plain numbers. */ | ||
| 20 | |||
| 21 | static struct ot_workstruct ws; | ||
| 22 | |||
| 23 | static void hash_named(ot_hash hash, const char *name) { | ||
| 24 | memset(hash, 0, sizeof(ot_hash)); | ||
| 25 | memcpy(hash, name, strlen(name) < sizeof(ot_hash) ? strlen(name) : sizeof(ot_hash)); | ||
| 26 | } | ||
| 27 | |||
| 28 | /* An IPv4 peer arrives as the v4 mapped form the tracker stores */ | ||
| 29 | static void peer_v4(ot_peer6 peer, uint8_t last, uint16_t port) { | ||
| 30 | ot_ip6 ip = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 10, 0, 0, 0}; | ||
| 31 | uint16_t net_port = htons(port); | ||
| 32 | |||
| 33 | ip[15] = last; | ||
| 34 | memset(peer, 0, sizeof(ot_peer6)); | ||
| 35 | OT_SETIP(peer, ip); | ||
| 36 | OT_SETPORT(peer, &net_port); | ||
| 37 | OT_PEERFLAG(peer) = 0; | ||
| 38 | } | ||
| 39 | |||
| 40 | static void peer_v6(ot_peer6 peer, uint8_t last, uint16_t port) { | ||
| 41 | ot_ip6 ip = {0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | ||
| 42 | uint16_t net_port = htons(port); | ||
| 43 | |||
| 44 | ip[15] = last; | ||
| 45 | memset(peer, 0, sizeof(ot_peer6)); | ||
| 46 | OT_SETIP(peer, ip); | ||
| 47 | OT_SETPORT(peer, &net_port); | ||
| 48 | OT_PEERFLAG(peer) = 0; | ||
| 49 | } | ||
| 50 | |||
| 51 | static void announce(ot_hash *hash, ot_peer6 peer) { | ||
| 52 | ws.hash = hash; | ||
| 53 | memcpy(ws.peer, peer, sizeof(ot_peer6)); | ||
| 54 | ws.reply = ws.outbuf; | ||
| 55 | add_peer_to_torrent_and_return_peers(FLAG_TCP, &ws, 50); | ||
| 56 | } | ||
| 57 | |||
| 58 | /* seeders, downloads completed, leechers */ | ||
| 59 | static void scrape(ot_hash hash, uint32_t counts[3]) { | ||
| 60 | char reply[12]; | ||
| 61 | |||
| 62 | return_udp_scrape_for_torrent(hash, reply); | ||
| 63 | counts[0] = ntohl(((uint32_t *)reply)[0]); | ||
| 64 | counts[1] = ntohl(((uint32_t *)reply)[1]); | ||
| 65 | counts[2] = ntohl(((uint32_t *)reply)[2]); | ||
| 66 | } | ||
| 67 | |||
| 68 | static void case_an_announce_makes_a_torrent(void) { | ||
| 69 | ot_hash hash; | ||
| 70 | ot_peer6 peer; | ||
| 71 | uint32_t counts[3]; | ||
| 72 | size_t before = mutex_get_torrent_count(); | ||
| 73 | |||
| 74 | test_case("the first announce for a hash creates its torrent"); | ||
| 75 | hash_named(hash, "first-announce"); | ||
| 76 | peer_v4(peer, 1, 6881); | ||
| 77 | announce(&hash, peer); | ||
| 78 | |||
| 79 | CHECK_INT((int)(mutex_get_torrent_count() - before), 1); | ||
| 80 | scrape(hash, counts); | ||
| 81 | CHECK_INT((int)counts[2], 1); /* one leecher */ | ||
| 82 | CHECK_INT((int)counts[0], 0); | ||
| 83 | } | ||
| 84 | |||
| 85 | static void case_the_same_peer_twice_is_one_peer(void) { | ||
| 86 | ot_hash hash; | ||
| 87 | ot_peer6 peer; | ||
| 88 | uint32_t counts[3]; | ||
| 89 | |||
| 90 | test_case("announcing twice renews a peer rather than adding another"); | ||
| 91 | hash_named(hash, "renewing"); | ||
| 92 | peer_v4(peer, 2, 6881); | ||
| 93 | announce(&hash, peer); | ||
| 94 | announce(&hash, peer); | ||
| 95 | |||
| 96 | scrape(hash, counts); | ||
| 97 | CHECK_INT((int)counts[2], 1); | ||
| 98 | } | ||
| 99 | |||
| 100 | static void case_a_port_makes_a_different_peer(void) { | ||
| 101 | ot_hash hash; | ||
| 102 | ot_peer6 peer; | ||
| 103 | uint32_t counts[3]; | ||
| 104 | |||
| 105 | test_case("the same address on another port is another peer"); | ||
| 106 | hash_named(hash, "two-ports"); | ||
| 107 | peer_v4(peer, 3, 6881); | ||
| 108 | announce(&hash, peer); | ||
| 109 | peer_v4(peer, 3, 6882); | ||
| 110 | announce(&hash, peer); | ||
| 111 | |||
| 112 | scrape(hash, counts); | ||
| 113 | CHECK_INT((int)counts[2], 2); | ||
| 114 | } | ||
| 115 | |||
| 116 | static void case_seeding_is_counted_apart(void) { | ||
| 117 | ot_hash hash; | ||
| 118 | ot_peer6 peer; | ||
| 119 | uint32_t counts[3]; | ||
| 120 | |||
| 121 | test_case("a seeder counts as complete, a leecher as incomplete"); | ||
| 122 | hash_named(hash, "seed-and-leech"); | ||
| 123 | peer_v4(peer, 4, 6881); | ||
| 124 | OT_PEERFLAG(peer) |= PEER_FLAG_SEEDING; | ||
| 125 | announce(&hash, peer); | ||
| 126 | peer_v4(peer, 5, 6881); | ||
| 127 | announce(&hash, peer); | ||
| 128 | |||
| 129 | scrape(hash, counts); | ||
| 130 | CHECK_INT((int)counts[0], 1); | ||
| 131 | CHECK_INT((int)counts[2], 1); | ||
| 132 | } | ||
| 133 | |||
| 134 | static void case_completion_without_seeding_is_not_believed(void) { | ||
| 135 | ot_hash hash; | ||
| 136 | ot_peer6 peer; | ||
| 137 | uint32_t counts[3]; | ||
| 138 | |||
| 139 | test_case("a peer that claims to have completed but is still leeching is not counted"); | ||
| 140 | hash_named(hash, "claiming"); | ||
| 141 | peer_v4(peer, 6, 6881); | ||
| 142 | OT_PEERFLAG(peer) |= PEER_FLAG_COMPLETED; | ||
| 143 | announce(&hash, peer); | ||
| 144 | |||
| 145 | scrape(hash, counts); | ||
| 146 | CHECK_INT((int)counts[1], 0); | ||
| 147 | CHECK_INT((int)counts[2], 1); /* still a leecher */ | ||
| 148 | } | ||
| 149 | |||
| 150 | static void case_completing_is_counted_once(void) { | ||
| 151 | ot_hash hash; | ||
| 152 | ot_peer6 peer; | ||
| 153 | uint32_t counts[3]; | ||
| 154 | |||
| 155 | test_case("a seeder reporting completion is counted once, however often it says so"); | ||
| 156 | hash_named(hash, "completing"); | ||
| 157 | peer_v4(peer, 7, 6881); | ||
| 158 | OT_PEERFLAG(peer) |= PEER_FLAG_COMPLETED | PEER_FLAG_SEEDING; | ||
| 159 | announce(&hash, peer); | ||
| 160 | announce(&hash, peer); | ||
| 161 | announce(&hash, peer); | ||
| 162 | |||
| 163 | scrape(hash, counts); | ||
| 164 | CHECK_INT((int)counts[1], 1); | ||
| 165 | CHECK_INT((int)counts[0], 1); | ||
| 166 | } | ||
| 167 | |||
| 168 | static void case_a_peer_can_leave(void) { | ||
| 169 | ot_hash hash; | ||
| 170 | ot_peer6 peer; | ||
| 171 | uint32_t counts[3]; | ||
| 172 | |||
| 173 | test_case("a peer that stops is gone from the swarm"); | ||
| 174 | hash_named(hash, "leaving"); | ||
| 175 | peer_v4(peer, 7, 6881); | ||
| 176 | announce(&hash, peer); | ||
| 177 | peer_v4(peer, 8, 6881); | ||
| 178 | announce(&hash, peer); | ||
| 179 | scrape(hash, counts); | ||
| 180 | CHECK_INT((int)counts[2], 2); | ||
| 181 | |||
| 182 | ws.hash = &hash; | ||
| 183 | memcpy(ws.peer, peer, sizeof(ot_peer6)); | ||
| 184 | remove_peer_from_torrent(FLAG_TCP, &ws); | ||
| 185 | |||
| 186 | scrape(hash, counts); | ||
| 187 | CHECK_INT((int)counts[2], 1); | ||
| 188 | } | ||
| 189 | |||
| 190 | static void case_the_families_are_kept_apart(void) { | ||
| 191 | ot_hash hash; | ||
| 192 | ot_peer6 peer; | ||
| 193 | uint32_t counts[3]; | ||
| 194 | |||
| 195 | test_case("a v4 and a v6 peer of one swarm are both counted"); | ||
| 196 | hash_named(hash, "dual-stack"); | ||
| 197 | peer_v4(peer, 9, 6881); | ||
| 198 | announce(&hash, peer); | ||
| 199 | peer_v6(peer, 9, 6881); | ||
| 200 | announce(&hash, peer); | ||
| 201 | |||
| 202 | scrape(hash, counts); | ||
| 203 | CHECK_INT((int)counts[2], 2); | ||
| 204 | } | ||
| 205 | |||
| 206 | static void case_a_hash_nobody_announced(void) { | ||
| 207 | ot_hash hash; | ||
| 208 | uint32_t counts[3]; | ||
| 209 | |||
| 210 | test_case("a hash nobody has announced scrapes as nothing at all"); | ||
| 211 | hash_named(hash, "never-seen"); | ||
| 212 | scrape(hash, counts); | ||
| 213 | CHECK_INT((int)counts[0], 0); | ||
| 214 | CHECK_INT((int)counts[1], 0); | ||
| 215 | CHECK_INT((int)counts[2], 0); | ||
| 216 | } | ||
| 217 | |||
| 218 | void test_trackerlogic(void) { | ||
| 219 | sigset_t signal_mask; | ||
| 220 | |||
| 221 | /* accesslist_init starts a thread that waits for SIGHUP and is stopped by | ||
| 222 | being sent one, so the signal has to be blocked here as the tracker | ||
| 223 | blocks it, or stopping the tracker logic ends this process instead */ | ||
| 224 | sigemptyset(&signal_mask); | ||
| 225 | sigaddset(&signal_mask, SIGHUP); | ||
| 226 | pthread_sigmask(SIG_BLOCK, &signal_mask, NULL); | ||
| 227 | |||
| 228 | trackerlogic_init(); | ||
| 229 | CHECK_INT(workstruct_init(&ws), 0); | ||
| 230 | |||
| 231 | case_an_announce_makes_a_torrent(); | ||
| 232 | case_the_same_peer_twice_is_one_peer(); | ||
| 233 | case_a_port_makes_a_different_peer(); | ||
| 234 | case_seeding_is_counted_apart(); | ||
| 235 | case_completion_without_seeding_is_not_believed(); | ||
| 236 | case_completing_is_counted_once(); | ||
| 237 | case_a_peer_can_leave(); | ||
| 238 | case_the_families_are_kept_apart(); | ||
| 239 | case_a_hash_nobody_announced(); | ||
| 240 | |||
| 241 | workstruct_deinit(&ws); | ||
| 242 | trackerlogic_deinit(); | ||
| 243 | } | ||
diff --git a/tests/test.h b/tests/test.h index 7582dab..d7296cc 100644 --- a/tests/test.h +++ b/tests/test.h | |||
| @@ -87,6 +87,7 @@ void test_buf(void); | |||
| 87 | void test_ip(void); | 87 | void test_ip(void); |
| 88 | void test_socket(void); | 88 | void test_socket(void); |
| 89 | void test_mutex(void); | 89 | void test_mutex(void); |
| 90 | void test_trackerlogic(void); | ||
| 90 | void test_workqueue(void); | 91 | void test_workqueue(void); |
| 91 | void test_batch(void); | 92 | void test_batch(void); |
| 92 | void test_fdstate(void); | 93 | void test_fdstate(void); |
