hikvision-isup / test /test_connection_logging.py
Lewis Kimaru
feat: add WebSocket and HTTP tunnels for ISUP protocol to bypass HF port limitations
5003a03
Raw History Blame Contribute Delete
6.43 kB
#!/usr/bin/env python3
"""
Test Connection Logging
Simple test to verify that connection attempts and requests are being logged properly
"""
import asyncio
import socket
import struct
import sys
import os
# Add parent directory to path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
async def test_basic_connection():
"""Test basic TCP connection to see if it gets logged"""
print("πŸ§ͺ Testing basic TCP connection logging...")
try:
# Try to connect to the ISUP server with timeout
print("πŸ”— Attempting connection to localhost:7660...")
reader, writer = await asyncio.wait_for(
asyncio.open_connection('localhost', 7660),
timeout=10.0
)
print("βœ… Connected to ISUP server")
# Send some invalid data to test logging
writer.write(b"HELLO_WORLD_INVALID_ISUP_DATA")
await writer.drain()
print("πŸ“€ Sent invalid data")
# Wait a moment
await asyncio.sleep(1)
# Close connection
writer.close()
await writer.wait_closed()
print("πŸ“΄ Connection closed")
except Exception as e:
print(f"❌ Connection test failed: {e}")
async def test_isup_header():
"""Test sending a malformed ISUP header"""
print("\nπŸ§ͺ Testing ISUP header logging...")
try:
reader, writer = await asyncio.wait_for(
asyncio.open_connection('localhost', 7660),
timeout=10.0
)
print("βœ… Connected to ISUP server")
# Create a malformed ISUP header (wrong size)
fake_header = struct.pack('>BBHB32sHH',
0x50, # ISUP version 5.0
0x01, # REGISTER_REQ
0x0001, # sequence
10, # device_id_len
b"TEST_DEVICE".ljust(32, b'\x00'), # device_id
0, # data_length
0 # reserved
)
writer.write(fake_header)
await writer.drain()
print("πŸ“€ Sent malformed ISUP header")
# Wait a moment
await asyncio.sleep(1)
writer.close()
await writer.wait_closed()
print("πŸ“΄ Connection closed")
except Exception as e:
print(f"❌ ISUP header test failed: {e}")
async def test_registration_attempt():
"""Test sending a registration request"""
print("\nπŸ§ͺ Testing registration attempt logging...")
try:
reader, writer = await asyncio.wait_for(
asyncio.open_connection('localhost', 7660),
timeout=10.0
)
print("βœ… Connected to ISUP server")
# Create a proper ISUP registration request
device_id = "TEST_DEVICE_123"
registration_data = b"FAKE_REGISTRATION_DATA_FOR_TESTING" + b"\x00" * 50
header = struct.pack('>BBHB32sHH',
0x50, # ISUP version 5.0
0x01, # REGISTER_REQ
0x0001, # sequence
len(device_id), # device_id_len
device_id.encode('utf-8').ljust(32, b'\x00'), # device_id
len(registration_data), # data_length
0 # reserved
)
# Send header + data
writer.write(header + registration_data)
await writer.drain()
print("πŸ“€ Sent registration request")
# Try to read response
try:
response_header = await asyncio.wait_for(reader.read(20), timeout=2.0)
if response_header:
print(f"πŸ“₯ Received response: {len(response_header)} bytes")
else:
print("πŸ“₯ No response received")
except asyncio.TimeoutError:
print("⏰ Response timeout")
writer.close()
await writer.wait_closed()
print("πŸ“΄ Connection closed")
except Exception as e:
print(f"❌ Registration test failed: {e}")
async def test_multiple_connections():
"""Test multiple simultaneous connections"""
print("\nπŸ§ͺ Testing multiple connection logging...")
tasks = []
for i in range(3):
tasks.append(test_single_connection(i))
await asyncio.gather(*tasks, return_exceptions=True)
print("βœ… Multiple connection test completed")
async def test_single_connection(connection_id):
"""Single connection for multiple connection test"""
try:
reader, writer = await asyncio.wait_for(
asyncio.open_connection('localhost', 7660),
timeout=10.0
)
print(f"βœ… Connection {connection_id} established")
# Send some data
writer.write(f"CONNECTION_{connection_id}_TEST_DATA".encode())
await writer.drain()
await asyncio.sleep(0.5)
writer.close()
await writer.wait_closed()
print(f"πŸ“΄ Connection {connection_id} closed")
except Exception as e:
print(f"❌ Connection {connection_id} failed: {e}")
def test_raw_socket():
"""Test raw socket connection"""
print("\nπŸ§ͺ Testing raw socket connection...")
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5.0)
print("πŸ”— Attempting raw socket connection...")
sock.connect(('localhost', 7660))
print("βœ… Raw socket connected")
# Send some raw data
sock.send(b"RAW_SOCKET_TEST_DATA_123")
print("πŸ“€ Sent raw data")
sock.close()
print("πŸ“΄ Raw socket closed")
except Exception as e:
print(f"❌ Raw socket test failed: {e}")
async def main():
"""Run all connection tests"""
print("πŸš€ Starting Connection Logging Tests")
print("=" * 50)
# Test basic connection
await test_basic_connection()
# Test ISUP header
await test_isup_header()
# Test registration attempt
await test_registration_attempt()
# Test multiple connections
await test_multiple_connections()
# Test raw socket
test_raw_socket()
print("\n" + "=" * 50)
print("🏁 All connection tests completed!")
print("\nπŸ’‘ Check the Debug tab in the dashboard to see logged connections:")
print(" https://atomio-hikvision-isup.hf.space/#debug")
if __name__ == "__main__":
asyncio.run(main())