summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/integration/test_announce.py15
-rw-r--r--tests/integration/test_udp.py52
-rw-r--r--tests/t_socket.c19
-rw-r--r--tests/t_workqueue.c52
4 files changed, 137 insertions, 1 deletions
diff --git a/tests/integration/test_announce.py b/tests/integration/test_announce.py
index ffbc07b..2d21417 100644
--- a/tests/integration/test_announce.py
+++ b/tests/integration/test_announce.py
@@ -118,9 +118,24 @@ class AnnounceRejects(TrackerTestCase):
118 def test_a_port_out_of_range_is_rejected(self): 118 def test_a_port_out_of_range_is_rejected(self):
119 self.assert_rejected("info_hash=" + "a" * 20 + "&peer_id=" + "b" * 20 + "&port=65536&left=0") 119 self.assert_rejected("info_hash=" + "a" * 20 + "&peer_id=" + "b" * 20 + "&port=65536&left=0")
120 120
121 def test_a_negative_port_is_rejected(self):
122 self.assert_rejected("info_hash=" + "a" * 20 + "&peer_id=" + "b" * 20 + "&port=-1&left=0")
123
121 def test_a_port_that_is_not_a_number_is_rejected(self): 124 def test_a_port_that_is_not_a_number_is_rejected(self):
122 self.assert_rejected("info_hash=" + "a" * 20 + "&peer_id=" + "b" * 20 + "&port=abc&left=0") 125 self.assert_rejected("info_hash=" + "a" * 20 + "&peer_id=" + "b" * 20 + "&port=abc&left=0")
123 126
127 def test_an_ip_that_fills_the_reply_buffer_is_refused(self):
128 """The value is decoded into the reply buffer and then terminated, so
129 the longest one that fits has to leave room for the terminator. 8096
130 bytes is what lies between where it is decoded and the buffer's end."""
131 connection = self.tracker.connect()
132 try:
133 connection.sock.sendall(b"GET /a?ip=" + b"1" * 8096 + b"\n\n")
134 status, _, _ = connection.reply()
135 self.assertTrue(status.startswith(b"HTTP/1.0 400"), status)
136 finally:
137 connection.close()
138
124 def test_two_info_hashes_are_rejected(self): 139 def test_two_info_hashes_are_rejected(self):
125 self.assert_rejected("info_hash=" + "a" * 20 + "&info_hash=" + "c" * 20 + "&peer_id=" + "b" * 20 + "&port=6881&left=0") 140 self.assert_rejected("info_hash=" + "a" * 20 + "&info_hash=" + "c" * 20 + "&peer_id=" + "b" * 20 + "&port=6881&left=0")
126 141
diff --git a/tests/integration/test_udp.py b/tests/integration/test_udp.py
index 1cb2389..8a255ec 100644
--- a/tests/integration/test_udp.py
+++ b/tests/integration/test_udp.py
@@ -9,7 +9,7 @@ import socket
9import struct 9import struct
10import unittest 10import unittest
11 11
12from harness import TrackerTestCase 12from harness import Tracker, TrackerTestCase
13 13
14MAGIC = 0x41727101980 14MAGIC = 0x41727101980
15 15
@@ -44,6 +44,17 @@ class UdpProtocol(TrackerTestCase):
44 self.assertEqual(action, 3, "action 3 is the error packet") 44 self.assertEqual(action, 3, "action 3 is the error packet")
45 self.assertIn(b"Connection ID missmatch", answer[8:]) 45 self.assertIn(b"Connection ID missmatch", answer[8:])
46 46
47 def test_a_wrong_connection_id_is_not_answered_with_more_than_was_sent(self):
48 """The sender's address is unverified at this point, so the error
49 must not be a way to throw bytes at somebody else."""
50 scrape = struct.pack(">QII", 0, 2, 3) + self.hash_for("udpbadscrape")
51 answer = self.tracker.udp_raw(scrape)
52 self.assertIsNotNone(answer, "a whole scrape is long enough to be told")
53 self.assertLessEqual(len(answer), len(scrape))
54 self.assertIsNone(self.tracker.udp_raw(scrape[:16]))
55 self.assertIsNone(self.tracker.udp_raw(scrape[: len(answer) - 1]))
56 self.assertIsNotNone(self.tracker.udp_raw(scrape[: len(answer)]))
57
47 def test_a_packet_shorter_than_the_header_is_ignored(self): 58 def test_a_packet_shorter_than_the_header_is_ignored(self):
48 self.assertIsNone(self.tracker.udp_raw(b"\x00" * 15)) 59 self.assertIsNone(self.tracker.udp_raw(b"\x00" * 15))
49 60
@@ -148,6 +159,28 @@ class UdpScrape(TrackerTestCase):
148 finally: 159 finally:
149 sock.close() 160 sock.close()
150 161
162 def test_a_hash_cut_short_is_not_scraped(self):
163 """What follows the last whole hash is not a hash, and the bytes that
164 would complete it are not the client's."""
165 info_hash = self.hash_for("udpcut")
166 sock, connection_id = self.tracker.udp_connect()
167 try:
168 sock.sendto(
169 struct.pack(">QII", connection_id, 2, 3) + info_hash + b"\x00" * 7, ("127.0.0.1", self.tracker.port)
170 )
171 self.assertEqual(len(sock.recv(2048)), 8 + 12)
172 finally:
173 sock.close()
174
175 def test_a_scrape_answers_no_more_hashes_than_the_protocol_allows(self):
176 sock, connection_id = self.tracker.udp_connect()
177 try:
178 hashes = b"".join(self.hash_for("udpmany%d" % n) for n in range(80))
179 sock.sendto(struct.pack(">QII", connection_id, 2, 4) + hashes, ("127.0.0.1", self.tracker.port))
180 self.assertEqual(len(sock.recv(2048)), 8 + 12 * 74)
181 finally:
182 sock.close()
183
151 184
152class UdpWorkersConfiguredAfterTheListener(TrackerTestCase): 185class UdpWorkersConfiguredAfterTheListener(TrackerTestCase):
153 """The harness writes its listen lines first, so this directive lands 186 """The harness writes its listen lines first, so this directive lands
@@ -166,3 +199,20 @@ class UdpWorkersConfiguredAfterTheListener(TrackerTestCase):
166 199
167if __name__ == "__main__": 200if __name__ == "__main__":
168 unittest.main() 201 unittest.main()
202
203
204class UdpAddressAlreadyTaken(TrackerTestCase):
205 def test_a_second_tracker_on_the_same_udp_address_says_so(self):
206 """Linux lets it start all the same, and from then on the two take
207 each other's packets. FreeBSD refuses the bind that follows, and the
208 warning is then what explains the refusal."""
209 try:
210 second = Tracker("listen.udp 127.0.0.1:%d\n" % self.tracker.port)
211 except AssertionError as refused:
212 said = str(refused)
213 else:
214 said = second.output()
215 second.stop()
216 second._cleanup()
217 self.assertIn("is already in use", said)
218 self.assertNotIn("is already in use", self.tracker.output())
diff --git a/tests/t_socket.c b/tests/t_socket.c
index 8e917a3..6f07f95 100644
--- a/tests/t_socket.c
+++ b/tests/t_socket.c
@@ -342,6 +342,24 @@ static void case_bind_failure(void) {
342 CHECK_INT(next_descriptor(), before); 342 CHECK_INT(next_descriptor(), before);
343} 343}
344 344
345/* The holder binds the way the tracker does, which lets a second socket in
346 beside it, and that is what the probe is there to see through. */
347static void case_a_taken_udp_address_is_noticed(void) {
348 char ip[16];
349 uint16_t port;
350 int holder;
351
352 test_case("a udp address somebody holds is reported as taken");
353 holder = socket_udp_bind(ip_loopback, 0, SOCKET_SHARED);
354 CHECK(holder != -1);
355 bound_address(holder, ip, &port);
356 CHECK_INT(socket_udp_is_taken(ip_loopback, port), 1);
357
358 test_case("and as free again once it is let go");
359 close(holder);
360 CHECK_INT(socket_udp_is_taken(ip_loopback, port), 0);
361}
362
345#ifdef WANT_SYNC_LIVE 363#ifdef WANT_SYNC_LIVE
346/* Reading an IPv4 multicast option back: Linux answers with an int, the BSDs 364/* Reading an IPv4 multicast option back: Linux answers with an int, the BSDs
347 with a single byte, so ask how much was written. */ 365 with a single byte, so ask how much was written. */
@@ -391,6 +409,7 @@ void test_socket(void) {
391 case_deferred_accept(); 409 case_deferred_accept();
392 case_connect_refused(); 410 case_connect_refused();
393 case_bind_failure(); 411 case_bind_failure();
412 case_a_taken_udp_address_is_noticed();
394#ifdef WANT_SYNC_LIVE 413#ifdef WANT_SYNC_LIVE
395 case_mcast_sender(); 414 case_mcast_sender();
396#endif 415#endif
diff --git a/tests/t_workqueue.c b/tests/t_workqueue.c
index c454e5b..660d882 100644
--- a/tests/t_workqueue.c
+++ b/tests/t_workqueue.c
@@ -6,6 +6,7 @@
6#include <string.h> 6#include <string.h>
7 7
8/* Opentracker */ 8/* Opentracker */
9#include "ot_fdstate.h"
9#include "ot_workqueue.h" 10#include "ot_workqueue.h"
10#include "test.h" 11#include "test.h"
11 12
@@ -96,10 +97,61 @@ static void case_cancelled_task_is_not_handed_out(void) {
96 workqueue_canceltask(48); 97 workqueue_canceltask(48);
97} 98}
98 99
100/* Tasks are filed under a descriptor number, and the number is handed to the
101 next connection the moment this one closes. */
102
103static void case_cancel_takes_every_task_of_the_socket(void) {
104 ot_tasktype asked;
105
106 test_case("cancelling a socket leaves none of its tasks behind");
107 CHECK_INT(workqueue_pushtask(49, TASK_FULLSCRAPE_TPB_ASCII), 0);
108 CHECK_INT(workqueue_pushtask(49, TASK_FULLSCRAPE_TPB_BINARY), 0);
109 CHECK_INT(workqueue_pushtask(50, TASK_FULLSCRAPE), 0);
110 workqueue_canceltask(49);
111
112 asked = TASK_FULLSCRAPE;
113 CHECK(workqueue_poptask(&asked) != 0);
114 CHECK_INT(asked, TASK_FULLSCRAPE);
115 workqueue_canceltask(50);
116}
117
118static void case_result_names_the_connection_that_asked(void) {
119 ot_tasktype asked = TASK_STATS;
120 ot_taskid taskid;
121 struct iovec *iovector;
122 size_t iovec_entries;
123 int is_partial;
124 uint32_t asked_under, answered_under = ~0u;
125
126 CHECK_INT(fdstate_init(), 0);
127 asked_under = fdstate_open(51, NULL, 0);
128
129 test_case("a result comes back with the generation its task was pushed under");
130 CHECK_INT(workqueue_pushtask(51, TASK_STATS_PEERS), 0);
131 taskid = workqueue_poptask(&asked);
132 CHECK(taskid != 0);
133
134 /* The connection goes away without cancelling and the number is reused */
135 fdstate_close(51);
136 fdstate_open(51, NULL, 0);
137
138 CHECK_INT(workqueue_pushresult(taskid, 0, NULL), 0);
139 CHECK_INT(workqueue_popresult(&iovec_entries, &iovector, &is_partial, &answered_under), 51);
140 CHECK_INT(answered_under, asked_under);
141
142 test_case("which is no longer the one the descriptor carries");
143 CHECK(fdstate_generation(51) != answered_under);
144
145 fdstate_close(51);
146 fdstate_deinit();
147}
148
99void test_workqueue(void) { 149void test_workqueue(void) {
100 case_task_goes_to_its_own_class(); 150 case_task_goes_to_its_own_class();
101 case_flags_survive_the_queue(); 151 case_flags_survive_the_queue();
102 case_classes_do_not_cross(); 152 case_classes_do_not_cross();
103 case_every_task_gets_its_own_id(); 153 case_every_task_gets_its_own_id();
104 case_cancelled_task_is_not_handed_out(); 154 case_cancelled_task_is_not_handed_out();
155 case_cancel_takes_every_task_of_the_socket();
156 case_result_names_the_connection_that_asked();
105} 157}