-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest_pack_scan.py
162 lines (134 loc) · 5.41 KB
/
test_pack_scan.py
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
# Generated by CodiumAI
from pack_scan import UDPServer, server as sampserver
import socket
import socketserver
from unittest.mock import patch
# Dependencies:
# pip install pytest-mock pytest
import pytest
class TestUDPServer:
# UDPServer can be instantiated with valid arguments
def test_instantiation_with_valid_arguments(self):
bind_address = ("127.0.0.1", 8000)
target_address = ("127.0.0.1", 9000)
server = UDPServer(bind_address, target_address)
assert server.target_address == target_address
assert server.timeout == 0.5
assert server.stop_thread is False
# UDPServer can start and stop without errors, with port check
def test_start_and_stop_without_errors_with_port_check(self, mocker):
bind_address = ("127.0.0.1", 8001)
target_address = ("127.0.0.1", 9001)
# Check if bind_address is already in use
bind_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
bind_socket.bind(bind_address)
except OSError:
pytest.fail(f"Port {bind_address[1]} is already in use")
finally:
bind_socket.close()
# Check if target_address is already in use
target_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
target_socket.bind(target_address)
except OSError:
pytest.fail(f"Port {target_address[1]} is already in use")
finally:
target_socket.close()
server = UDPServer(bind_address, target_address)
mocker.patch.object(socketserver.UDPServer, "serve_forever")
mocker.patch.object(socketserver.UDPServer, "shutdown")
server.start()
assert socketserver.UDPServer.serve_forever.called
server.stop()
# UDPServer handles invalid payload and returns False
def test_handles_invalid_payload_and_returns_false(self, mocker):
bind_address = ("127.0.0.1", 8004)
target_address = ("127.0.0.1", 9004)
server = UDPServer(bind_address, target_address)
server.timeout = 5
handler = mocker.Mock()
handler.request = (b"INVALID_PAYLOAD", "client_address")
sampserver.isonline = True
result = server.handle_external_packet(handler, sampserver=sampserver)
server.stop()
assert result is False
# UDPServer handles invalid server address and returns False
def test_handles_invalid_server_address_and_returns_false(self, mocker):
bind_address = ("127.0.0.1", 8000)
target_address = ("127.0.0.1", 9000)
server = UDPServer(bind_address, target_address)
# Check if bind address is already in use
try:
socket.socket(socket.AF_INET, socket.SOCK_DGRAM).bind(
("127.0.0.1", 8002)
)
except OSError:
pytest.skip("Bind address is already in use")
handler = mocker.Mock()
handler.request = (
b"SAMP\x01\x02\x03\x05\x05\x06\x07\x08\x09\x0a",
"client_address",
)
mocker.patch.object(server, "ping", return_value=True)
result = server.handle_external_packet(handler, sampserver=sampserver)
server.stop()
assert result is False
# Test handle_external_packet
def test_handle_external_packet(self, mocker):
# Mock the necessary attributes and methods
server = UDPServer(("127.0.0.1", 8008), ("127.0.0.1", 9008))
sampserver.isonline = True
sampserver.info = b"info_data"
sampserver.rules = b"rules_data"
sampserver.detail = b"detail_data"
sampserver.clients = b"clients_data"
handler = mocker.MagicMock()
handler.request = (
b"SAMP\x7b\x7b\x7b\x7b\00\00" + b"i",
"client_address",
)
# Mock the sendto() method
# Mock the sendto() method of the server socket
with patch("pack_scan.socket.socket.sendto") as sendto_mock:
sendto_mock.return_value = b"dummy_response"
# Test when server is online and payload is valid
assert (
server.handle_external_packet(handler, sampserver=sampserver)
is True
)
# Test when server is offline
sampserver.isonline = False
assert (
server.handle_external_packet(handler, sampserver=sampserver)
is False
)
# Test when payload address is invalid
handler.request = (
b"SAMP\x7f\x00\x00\x02\x03\x20\x03\x00\x00\x00",
"client_address",
)
assert (
server.handle_external_packet(handler, sampserver=sampserver)
is False
)
# Test when payload type is invalid
handler.request = (
b"SAMP\x7f\x00\x00\x01\x03\x20\x03\x00\x00\x01",
"client_address",
)
assert (
server.handle_external_packet(handler, sampserver=sampserver)
is False
)
# Test when payload type is not recognized
handler.request = (
b"SAMP\x7f\x00\x00\x01\x03\x20\x03\x00\x00\x61",
"client_address",
)
assert (
server.handle_external_packet(handler, sampserver=sampserver)
is False
)
if __name__ == "__main__":
pytest.main(["-v"])