-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend_test.py
More file actions
511 lines (417 loc) Β· 21.5 KB
/
backend_test.py
File metadata and controls
511 lines (417 loc) Β· 21.5 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#!/usr/bin/env python3
"""
Comprehensive Backend Tests for BitTorrent Tracker Aggregator
Tests all HTTP endpoints, WebSocket functionality, and BitTorrent protocol compliance
"""
import asyncio
import aiohttp
import websockets
import json
import time
import hashlib
import random
import string
import bencode
from urllib.parse import urlencode
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv('/app/frontend/.env')
BASE_URL = os.getenv('REACT_APP_BACKEND_URL', 'http://localhost:8001')
API_BASE = f"{BASE_URL}/api"
class BitTorrentTester:
def __init__(self):
self.session = None
self.test_results = []
async def __aenter__(self):
self.session = aiohttp.ClientSession()
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
if self.session:
await self.session.close()
def log_result(self, test_name, success, details="", response_time=None):
"""Log test results"""
status = "β
PASS" if success else "β FAIL"
result = {
'test': test_name,
'status': status,
'success': success,
'details': details,
'response_time': response_time
}
self.test_results.append(result)
print(f"{status} {test_name}")
if details:
print(f" Details: {details}")
if response_time:
print(f" Response Time: {response_time:.3f}s")
print()
def generate_info_hash(self):
"""Generate a realistic info_hash (40 hex chars)"""
return hashlib.sha1(f"test_torrent_{random.randint(1000, 9999)}".encode()).hexdigest()
def generate_peer_id(self):
"""Generate a realistic peer_id (20 bytes, URL encoded)"""
# Format: -AZ2060-{12 random chars} (Azureus/Vuze style)
random_chars = ''.join(random.choices(string.ascii_letters + string.digits, k=12))
return f"-AZ2060-{random_chars}"
async def test_http_announce_basic(self):
"""Test basic HTTP announce functionality"""
try:
start_time = time.time()
# Generate realistic BitTorrent parameters
info_hash = self.generate_info_hash()
peer_id = self.generate_peer_id()
port = random.randint(6881, 6999)
params = {
'info_hash': info_hash,
'peer_id': peer_id,
'port': port,
'uploaded': 0,
'downloaded': 0,
'left': 1073741824, # 1GB left
'event': 'started',
'numwant': 50
}
async with self.session.get(f"{API_BASE}/announce", params=params) as response:
response_time = time.time() - start_time
if response.status == 200:
content = await response.read()
# Try to decode bencoded response
try:
decoded = bencode.decode(content)
# Check required fields in BitTorrent announce response
required_fields = ['interval', 'complete', 'incomplete', 'peers']
missing_fields = [field for field in required_fields if field not in decoded]
if missing_fields:
self.log_result("HTTP Announce Basic", False,
f"Missing required fields: {missing_fields}", response_time)
else:
self.log_result("HTTP Announce Basic", True,
f"Valid bencoded response with {len(decoded.get('peers', []))} peers",
response_time)
except Exception as e:
self.log_result("HTTP Announce Basic", False,
f"Failed to decode bencoded response: {e}", response_time)
else:
content = await response.text()
self.log_result("HTTP Announce Basic", False,
f"HTTP {response.status}: {content}", response_time)
except Exception as e:
self.log_result("HTTP Announce Basic", False, f"Request failed: {e}")
async def test_http_announce_events(self):
"""Test different BitTorrent events (started, completed, stopped)"""
info_hash = self.generate_info_hash()
peer_id = self.generate_peer_id()
port = random.randint(6881, 6999)
events = [
('started', 1073741824, 0), # 1GB left, 0 downloaded
('completed', 0, 1073741824), # 0 left, 1GB downloaded
('stopped', 0, 1073741824) # Stopping
]
for event, left, downloaded in events:
try:
start_time = time.time()
params = {
'info_hash': info_hash,
'peer_id': peer_id,
'port': port,
'uploaded': downloaded,
'downloaded': downloaded,
'left': left,
'event': event,
'numwant': 30
}
async with self.session.get(f"{API_BASE}/announce", params=params) as response:
response_time = time.time() - start_time
if response.status == 200:
content = await response.read()
try:
decoded = bencode.decode(content)
self.log_result(f"HTTP Announce Event '{event}'", True,
f"Event processed successfully", response_time)
except Exception as e:
self.log_result(f"HTTP Announce Event '{event}'", False,
f"Invalid bencoded response: {e}", response_time)
else:
content = await response.text()
self.log_result(f"HTTP Announce Event '{event}'", False,
f"HTTP {response.status}: {content}", response_time)
except Exception as e:
self.log_result(f"HTTP Announce Event '{event}'", False, f"Request failed: {e}")
async def test_stats_endpoint(self):
"""Test /api/stats endpoint"""
try:
start_time = time.time()
async with self.session.get(f"{API_BASE}/stats") as response:
response_time = time.time() - start_time
if response.status == 200:
data = await response.json()
# Check required stats structure
required_sections = ['tracker_stats', 'swarm_stats', 'public_trackers']
missing_sections = [section for section in required_sections if section not in data]
if missing_sections:
self.log_result("Stats Endpoint", False,
f"Missing sections: {missing_sections}", response_time)
else:
# Check tracker_stats fields
tracker_stats = data['tracker_stats']
expected_fields = ['total_peers', 'total_swarms', 'announces_http',
'announces_udp', 'announces_websocket', 'active_connections']
missing_fields = [field for field in expected_fields if field not in tracker_stats]
if missing_fields:
self.log_result("Stats Endpoint", False,
f"Missing tracker_stats fields: {missing_fields}", response_time)
else:
self.log_result("Stats Endpoint", True,
f"Complete stats with {tracker_stats['total_swarms']} swarms, "
f"{tracker_stats['total_peers']} peers", response_time)
else:
content = await response.text()
self.log_result("Stats Endpoint", False,
f"HTTP {response.status}: {content}", response_time)
except Exception as e:
self.log_result("Stats Endpoint", False, f"Request failed: {e}")
async def test_swarms_endpoint(self):
"""Test /api/swarms endpoint"""
try:
start_time = time.time()
async with self.session.get(f"{API_BASE}/swarms") as response:
response_time = time.time() - start_time
if response.status == 200:
data = await response.json()
# Check swarms data structure
if 'swarms' in data and 'total_swarms' in data:
swarms = data['swarms']
self.log_result("Swarms Endpoint", True,
f"Retrieved {len(swarms)} swarms", response_time)
else:
self.log_result("Swarms Endpoint", False,
"Invalid swarms data structure", response_time)
else:
content = await response.text()
self.log_result("Swarms Endpoint", False,
f"HTTP {response.status}: {content}", response_time)
except Exception as e:
self.log_result("Swarms Endpoint", False, f"Request failed: {e}")
async def test_trackers_endpoint(self):
"""Test /api/trackers endpoint"""
try:
start_time = time.time()
async with self.session.get(f"{API_BASE}/trackers") as response:
response_time = time.time() - start_time
if response.status == 200:
data = await response.json()
if 'trackers' in data:
trackers = data['trackers']
self.log_result("Trackers Endpoint", True,
f"Retrieved {len(trackers)} public trackers", response_time)
else:
self.log_result("Trackers Endpoint", False,
"Invalid trackers data structure", response_time)
else:
content = await response.text()
self.log_result("Trackers Endpoint", False,
f"HTTP {response.status}: {content}", response_time)
except Exception as e:
self.log_result("Trackers Endpoint", False, f"Request failed: {e}")
async def test_scrape_trackers(self):
"""Test /api/scrape_trackers endpoint"""
try:
start_time = time.time()
async with self.session.post(f"{API_BASE}/scrape_trackers") as response:
response_time = time.time() - start_time
if response.status == 200:
data = await response.json()
if 'status' in data and 'trackers_loaded' in data:
self.log_result("Scrape Trackers", True,
f"Loaded {data['trackers_loaded']} trackers", response_time)
else:
self.log_result("Scrape Trackers", False,
"Invalid scrape response structure", response_time)
else:
content = await response.text()
self.log_result("Scrape Trackers", False,
f"HTTP {response.status}: {content}", response_time)
except Exception as e:
self.log_result("Scrape Trackers", False, f"Request failed: {e}")
async def test_metrics_endpoint(self):
"""Test /api/metrics Prometheus endpoint"""
try:
start_time = time.time()
async with self.session.get(f"{API_BASE}/metrics") as response:
response_time = time.time() - start_time
if response.status == 200:
content = await response.text()
# Check for Prometheus metrics format
required_metrics = [
'bittorrent_tracker_peers_total',
'bittorrent_tracker_swarms_total',
'bittorrent_tracker_announces_total',
'bittorrent_tracker_connections_active'
]
missing_metrics = [metric for metric in required_metrics if metric not in content]
if missing_metrics:
self.log_result("Metrics Endpoint", False,
f"Missing metrics: {missing_metrics}", response_time)
else:
self.log_result("Metrics Endpoint", True,
"Valid Prometheus metrics format", response_time)
else:
content = await response.text()
self.log_result("Metrics Endpoint", False,
f"HTTP {response.status}: {content}", response_time)
except Exception as e:
self.log_result("Metrics Endpoint", False, f"Request failed: {e}")
async def test_websocket_announce(self):
"""Test WebSocket announce functionality"""
try:
# Convert HTTP URL to WebSocket URL
ws_url = API_BASE.replace('http://', 'ws://').replace('https://', 'wss://') + '/announce_ws'
start_time = time.time()
async with websockets.connect(ws_url) as websocket:
connection_time = time.time() - start_time
# Send announce message
announce_data = {
"info_hash": self.generate_info_hash(),
"peer_id": self.generate_peer_id(),
"port": random.randint(6881, 6999),
"uploaded": 0,
"downloaded": 0,
"left": 1073741824,
"event": "started"
}
await websocket.send(json.dumps(announce_data))
# Wait for response
response = await asyncio.wait_for(websocket.recv(), timeout=10)
response_data = json.loads(response)
total_time = time.time() - start_time
if response_data.get('type') == 'announce_response':
self.log_result("WebSocket Announce", True,
"WebSocket announce successful", total_time)
else:
self.log_result("WebSocket Announce", False,
f"Unexpected response: {response_data}", total_time)
except asyncio.TimeoutError:
self.log_result("WebSocket Announce", False, "WebSocket response timeout")
except Exception as e:
self.log_result("WebSocket Announce", False, f"WebSocket error: {e}")
async def test_performance_latency(self):
"""Test low-latency performance requirements"""
info_hash = self.generate_info_hash()
peer_id = self.generate_peer_id()
port = random.randint(6881, 6999)
params = {
'info_hash': info_hash,
'peer_id': peer_id,
'port': port,
'uploaded': 0,
'downloaded': 0,
'left': 1073741824,
'event': 'started'
}
# Test multiple requests to measure average latency
response_times = []
for i in range(5):
try:
start_time = time.time()
async with self.session.get(f"{API_BASE}/announce", params=params) as response:
response_time = time.time() - start_time
response_times.append(response_time)
if response.status != 200:
self.log_result("Performance Latency", False,
f"Request {i+1} failed with status {response.status}")
return
except Exception as e:
self.log_result("Performance Latency", False, f"Request {i+1} failed: {e}")
return
avg_latency = sum(response_times) / len(response_times)
max_latency = max(response_times)
# BitTorrent trackers should respond very quickly (< 1 second is good, < 0.5s is excellent)
if avg_latency < 0.5:
self.log_result("Performance Latency", True,
f"Excellent latency - Avg: {avg_latency:.3f}s, Max: {max_latency:.3f}s")
elif avg_latency < 1.0:
self.log_result("Performance Latency", True,
f"Good latency - Avg: {avg_latency:.3f}s, Max: {max_latency:.3f}s")
else:
self.log_result("Performance Latency", False,
f"High latency - Avg: {avg_latency:.3f}s, Max: {max_latency:.3f}s")
async def test_concurrent_requests(self):
"""Test thread-safe concurrent operations"""
info_hash = self.generate_info_hash()
async def make_announce_request(peer_num):
peer_id = f"-AZ2060-peer{peer_num:08d}"
port = 6881 + peer_num
params = {
'info_hash': info_hash,
'peer_id': peer_id,
'port': port,
'uploaded': 0,
'downloaded': 0,
'left': 1073741824,
'event': 'started'
}
async with self.session.get(f"{API_BASE}/announce", params=params) as response:
return response.status == 200
try:
start_time = time.time()
# Make 10 concurrent requests
tasks = [make_announce_request(i) for i in range(10)]
results = await asyncio.gather(*tasks, return_exceptions=True)
response_time = time.time() - start_time
successful = sum(1 for result in results if result is True)
failed = len(results) - successful
if failed == 0:
self.log_result("Concurrent Requests", True,
f"All 10 concurrent requests successful", response_time)
else:
self.log_result("Concurrent Requests", False,
f"{successful} successful, {failed} failed", response_time)
except Exception as e:
self.log_result("Concurrent Requests", False, f"Concurrent test failed: {e}")
async def run_all_tests(self):
"""Run all backend tests"""
print("π Starting BitTorrent Tracker Aggregator Backend Tests")
print(f"π‘ Testing against: {API_BASE}")
print("=" * 60)
# Core functionality tests
await self.test_http_announce_basic()
await self.test_http_announce_events()
# API endpoint tests
await self.test_stats_endpoint()
await self.test_swarms_endpoint()
await self.test_trackers_endpoint()
await self.test_scrape_trackers()
await self.test_metrics_endpoint()
# WebSocket test
await self.test_websocket_announce()
# Performance tests
await self.test_performance_latency()
await self.test_concurrent_requests()
# Summary
print("=" * 60)
print("π TEST SUMMARY")
print("=" * 60)
total_tests = len(self.test_results)
passed_tests = sum(1 for result in self.test_results if result['success'])
failed_tests = total_tests - passed_tests
print(f"Total Tests: {total_tests}")
print(f"β
Passed: {passed_tests}")
print(f"β Failed: {failed_tests}")
print(f"Success Rate: {(passed_tests/total_tests)*100:.1f}%")
if failed_tests > 0:
print("\nπ FAILED TESTS:")
for result in self.test_results:
if not result['success']:
print(f" β {result['test']}: {result['details']}")
return self.test_results
async def main():
"""Main test runner"""
async with BitTorrentTester() as tester:
results = await tester.run_all_tests()
# Return exit code based on results
failed_count = sum(1 for result in results if not result['success'])
return 0 if failed_count == 0 else 1
if __name__ == "__main__":
exit_code = asyncio.run(main())
exit(exit_code)