-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_ping.py
More file actions
371 lines (299 loc) · 14 KB
/
Copy pathtest_ping.py
File metadata and controls
371 lines (299 loc) · 14 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
import json
import os
from unittest.mock import MagicMock, patch
import pytest
from pingping.ping import Ping, get_index
class TestPing:
file_path = os.path.abspath("data")
@pytest.fixture
def setup(self):
p = Ping()
yield p
def read_all_inputs(self, obj, langauge="en"):
file_name = f"ping_{langauge}.json"
ping_data = json.load(open(f"{self.file_path}/{file_name}"))
result = {}
for os_name, os_result in ping_data.items():
result[os_name] = {}
for ip, each_ping in os_result.items():
result[os_name][ip] = obj.fetch_ping_data(each_ping)
return result
def validate_result(self, result):
assert "linux" in result
assert "windows" in result
assert "mac" in result
assert "1.1.1.1" in result["linux"]
assert "loss_percentage" in result["linux"]["1.1.1.1"]
assert 0.0 == result["linux"]["1.1.1.1"]["loss_percentage"]
assert 58.489 == result["linux"]["1.1.1.1"]["min"]
assert 108.154 == result["linux"]["1.1.1.1"]["avg"]
assert 188.385 == result["linux"]["1.1.1.1"]["max"]
assert 4 == result["linux"]["1.1.1.1"]["packets_transmitted"]
assert "192.168.1.1" in result["linux"]
assert "loss_percentage" in result["linux"]["192.168.1.1"]
assert 100.0 == result["linux"]["192.168.1.1"]["loss_percentage"]
assert 4 == result["linux"]["192.168.1.1"]["packets_transmitted"]
assert "1.1.1.1" in result["windows"]
assert "loss_percentage" in result["windows"]["1.1.1.1"]
assert 0.0 == result["windows"]["1.1.1.1"]["loss_percentage"]
assert 68.0 == result["windows"]["1.1.1.1"]["min"]
assert 81.0 == result["windows"]["1.1.1.1"]["avg"]
assert 99.0 == result["windows"]["1.1.1.1"]["max"]
assert 4 == result["windows"]["1.1.1.1"]["packets_transmitted"]
assert "192.168.1.1" in result["windows"]
assert "loss_percentage" in result["windows"]["192.168.1.1"]
assert 100.0 == result["windows"]["192.168.1.1"]["loss_percentage"]
assert 4 == result["windows"]["192.168.1.1"]["packets_transmitted"]
assert "1.1.1.1" in result["mac"]
assert "loss_percentage" in result["mac"]["1.1.1.1"]
assert 0.0 == result["mac"]["1.1.1.1"]["loss_percentage"]
assert 24.02 == result["mac"]["1.1.1.1"]["min"]
assert 24.116 == result["mac"]["1.1.1.1"]["avg"]
assert 24.279 == result["mac"]["1.1.1.1"]["max"]
assert 4 == result["mac"]["1.1.1.1"]["packets_transmitted"]
assert "192.168.1.1" in result["mac"]
assert "loss_percentage" in result["mac"]["192.168.1.1"]
assert 100.0 == result["mac"]["192.168.1.1"]["loss_percentage"]
assert 4 == result["mac"]["192.168.1.1"]["packets_transmitted"]
assert "1.1.1.1" in result["mingw64"]
assert "loss_percentage" in result["mingw64"]["1.1.1.1"]
assert 25.0 == result["mingw64"]["1.1.1.1"]["loss_percentage"]
assert 64.0 == result["mingw64"]["1.1.1.1"]["min"]
assert 71.0 == result["mingw64"]["1.1.1.1"]["avg"]
assert 79.0 == result["mingw64"]["1.1.1.1"]["max"]
assert 4 == result["mingw64"]["1.1.1.1"]["packets_transmitted"]
assert "192.168.1.1" in result["mingw64"]
assert "loss_percentage" in result["mingw64"]["192.168.1.1"]
assert 100.0 == result["mingw64"]["192.168.1.1"]["loss_percentage"]
assert 4 == result["mingw64"]["192.168.1.1"]["packets_transmitted"]
def test_ping_en(self, setup):
result = self.read_all_inputs(setup)
self.validate_result(result)
def test_ping_spanish(self, setup):
result = self.read_all_inputs(setup, langauge="spanish")
self.validate_result(result)
def test_ping_french(self, setup):
result = self.read_all_inputs(setup, langauge="french")
self.validate_result(result)
def test_ping_afrikaans(self, setup):
result = self.read_all_inputs(setup, langauge="afrikaans")
self.validate_result(result)
def test_ping_telugu(self, setup):
result = self.read_all_inputs(setup, langauge="telugu")
self.validate_result(result)
def test_ping_hindi(self, setup):
result = self.read_all_inputs(setup, langauge="hindi")
self.validate_result(result)
def test_ping_hindi_2(self, setup):
result = self.read_all_inputs(setup, langauge="hindi")
self.validate_result(result)
class TestPingAdditional:
"""Additional tests to improve coverage"""
def test_set_logger_level(self):
"""Test logger level setting"""
ping = Ping()
logger = ping.set_logger_level("DEBUG")
assert logger.level == 10 # DEBUG level
logger = ping.set_logger_level("INFO")
assert logger.level == 20 # INFO level
logger = ping.set_logger_level("ERROR")
assert logger.level == 40 # ERROR level
def test_set_ping_layer_not_3(self):
"""Test _set_ping with layer != 3"""
ping = Ping(layer=4, count=5, timeout=10)
command = ping._set_ping(count=5, layer=4, timeout=10)
assert "-c 5" in command
assert "-t 10" in command
def test_is_valid_ip_valid(self):
"""Test is_valid_ip with valid IPs"""
assert Ping.is_valid_ip("192.168.1.1") is True
assert Ping.is_valid_ip("8.8.8.8") is True
assert Ping.is_valid_ip("127.0.0.1") is True
assert Ping.is_valid_ip("1.1.1.1") is True
assert Ping.is_valid_ip("255.255.255.255") is True
def test_is_valid_ip_invalid(self):
"""Test is_valid_ip with invalid IPs"""
assert Ping.is_valid_ip("256.1.1.1") is False
assert Ping.is_valid_ip("192.168.1") is False
assert Ping.is_valid_ip("invalid") is False
assert Ping.is_valid_ip("192.168.1.1.1") is False
assert Ping.is_valid_ip("") is False
def test_add_ip_valid(self):
"""Test _add_ip with valid IP"""
ping = Ping()
command = ping._add_ip("192.168.1.1")
assert command is not None
assert "192.168.1.1" in command
def test_add_ip_invalid(self):
"""Test _add_ip with invalid IP"""
ping = Ping()
command = ping._add_ip("invalid_ip")
assert command is None
@patch("subprocess.Popen")
def test_ping_valid_ip(self, mock_popen):
"""Test ping method with valid IP"""
mock_process = MagicMock()
mock_process.communicate.return_value = (
b"PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.\n"
b"64 bytes from 8.8.8.8: icmp_seq=1 ttl=117 time=20.1 ms\n"
b"--- 8.8.8.8 ping statistics ---\n"
b"1 packets transmitted, 1 received, 0% packet loss, time 0ms\n"
b"rtt min/avg/max/mdev = 20.100/20.100/20.100/0.000 ms\n",
b"",
)
mock_popen.return_value = mock_process
ping = Ping(count=1)
result = ping.ping("8.8.8.8")
assert result is not None
mock_popen.assert_called_once()
def test_ping_invalid_ip(self, capsys):
"""Test ping method with invalid IP"""
ping = Ping()
result = ping.ping("999.999.999.999")
assert result is None
def test_get_index_found(self):
"""Test get_index when element is found"""
test_list = ["a", "b", "c", "d"]
assert get_index(test_list, "b") == 1
assert get_index(test_list, "a") == 0
assert get_index(test_list, "d") == 3
def test_get_index_not_found(self):
"""Test get_index when element is not found"""
test_list = ["a", "b", "c"]
assert get_index(test_list, "z") is None
assert get_index(test_list, "x") is None
def test_ping_different_counts(self):
"""Test ping with different packet counts"""
ping1 = Ping(count=1)
ping2 = Ping(count=10)
assert "-c 1" in ping1.command or "-n 1" in ping1.command
assert "-c 10" in ping2.command or "-n 10" in ping2.command
def test_set_ping_windows_os(self):
"""Test _set_ping for Windows OS"""
ping = Ping(count=5)
ping.os = "nt"
command = ping._set_ping(count=5, layer=3, timeout=3)
assert "-n 5" in command
def test_set_ping_posix_os(self):
"""Test _set_ping for POSIX OS"""
ping = Ping(count=5)
ping.os = "posix"
command = ping._set_ping(count=5, layer=3, timeout=3)
assert "-c 5" in command
def test_tcping_initialization(self):
"""Test tcping command initialization"""
ping = Ping(command="tcping", layer=4)
assert "tcping" in ping.command
assert "-c 4" in ping.command or "-t" in " ".join(ping.command)
def test_ping_with_timeout(self):
"""Test ping with custom timeout"""
Ping(timeout=5)
Ping(timeout=10)
# Timeout should be in the command for layer 4
ping_l4 = Ping(layer=4, timeout=5)
assert "-t" in " ".join(ping_l4.command) or "-t 5" in ping_l4.command
def test_tcping_loss_percentage_calculation(self):
"""Test tcping loss percentage calculation (line 78)"""
from pingping.ping import Ping
# Mock tcping output with 80% successful (which means 80% success rate)
tcping_output = """
Probing 192.168.1.1:80/tcp - Port is open
Port is open
Port is open
Port is open
Statistics: 4 probes sent, 80% successful
"""
# Test tcping command to hit line 78
# When command="tcping", line 78 calculates: 100 - percentage = 100 - 80 = 20% loss
ping = Ping(command="tcping", layer=4)
result = ping.fetch_ping_data(tcping_output, command="tcping")
assert "loss_percentage" in result
assert result["loss_percentage"] == 20.0 # 100 - 80 = 20% loss
class TestCLIFunctions:
"""Test CLI functions: run() and help()"""
def test_run_with_valid_ip(self):
"""Test run() function with valid IP address (lines 146-181)"""
from pingping.ping import run
with patch("sys.argv", ["pingping", "8.8.8.8"]):
with patch.object(Ping, "ping", return_value={"ip": "8.8.8.8"}):
result = run()
assert result is not None
assert result["ip"] == "8.8.8.8"
def test_run_with_help_flag(self):
"""Test run() function with -h flag (lines 146-181)"""
from pingping.ping import run
with patch("sys.argv", ["pingping", "-h"]):
with pytest.raises(SystemExit) as exc_info:
run()
assert exc_info.value.code == -1
def test_run_with_help_long_flag(self):
"""Test run() function with --help flag (lines 146-181)"""
from pingping.ping import run
with patch("sys.argv", ["pingping", "--help"]):
with pytest.raises(SystemExit) as exc_info:
run()
assert exc_info.value.code == -1
def test_run_with_tcp_flag(self):
"""Test run() function with -l4 flag (lines 146-181)"""
from pingping.ping import run
with patch("sys.argv", ["pingping", "-l4", "192.168.1.1"]):
with patch.object(Ping, "ping", return_value={"ip": "192.168.1.1"}):
result = run()
assert result is not None
def test_run_with_web_flag(self):
"""Test run() function with --web flag (lines 146-181)"""
from pingping.ping import run
with patch("sys.argv", ["pingping", "--web", "192.168.1.1"]):
with patch.object(Ping, "ping", return_value={"ip": "192.168.1.1"}):
result = run()
assert result is not None
def test_run_with_tcp_long_flag(self):
"""Test run() function with --tcp flag (lines 146-181)"""
from pingping.ping import run
with patch("sys.argv", ["pingping", "--tcp", "192.168.1.1"]):
with patch.object(Ping, "ping", return_value={"ip": "192.168.1.1"}):
result = run()
assert result is not None
def test_run_with_http_flag(self):
"""Test run() function with --http flag (lines 146-181)"""
from pingping.ping import run
with patch("sys.argv", ["pingping", "--http", "192.168.1.1"]):
with patch.object(Ping, "ping", return_value={"ip": "192.168.1.1"}):
result = run()
assert result is not None
def test_run_with_count_flag(self):
"""Test run() function with -c flag (lines 146-181)"""
from pingping.ping import run
with patch("sys.argv", ["pingping", "-c", "10", "8.8.8.8"]):
with patch.object(Ping, "ping", return_value={"ip": "8.8.8.8"}):
result = run()
assert result is not None
def test_run_with_count_long_flag(self):
"""Test run() function with --count flag (lines 146-181)"""
from pingping.ping import run
with patch("sys.argv", ["pingping", "--count", "15", "1.1.1.1"]):
with patch.object(Ping, "ping", return_value={"ip": "1.1.1.1"}):
result = run()
assert result is not None
def test_run_with_no_ip_address(self):
"""Test run() function with invalid arguments - no valid IP (lines 146-181)"""
from pingping.ping import run
# When -c flag is used but no valid IP is provided, it will create a default Ping
# and attempt to ping None, which returns None (not a SystemExit)
with patch("sys.argv", ["pingping", "--help", "invalid"]):
with pytest.raises(SystemExit) as exc_info:
run()
assert exc_info.value.code == -1
def test_run_with_no_arguments(self):
"""Test run() function with no arguments (lines 146-181)"""
from pingping.ping import run
with patch("sys.argv", ["pingping"]):
with pytest.raises(SystemExit) as exc_info:
run()
assert exc_info.value.code == -1
def test_help_function(self):
"""Test help() function (lines 185-189)"""
from pingping.ping import help
with pytest.raises(SystemExit) as exc_info:
help()
assert exc_info.value.code == -1