|
1 | 1 | import json |
2 | 2 | import os |
| 3 | +import sys |
| 4 | +from unittest.mock import patch, MagicMock |
3 | 5 |
|
4 | 6 | import pytest |
5 | 7 |
|
6 | | -from pingping.ping import Ping |
| 8 | +from pingping.ping import Ping, get_index |
7 | 9 |
|
8 | 10 |
|
9 | 11 | class TestPing: |
@@ -108,3 +110,270 @@ def test_ping_hindi(self, setup): |
108 | 110 | def test_ping_hindi_2(self, setup): |
109 | 111 | result = self.read_all_inputs(setup, langauge="hindi") |
110 | 112 | self.validate_result(result) |
| 113 | + |
| 114 | + |
| 115 | +class TestPingAdditional: |
| 116 | + """Additional tests to improve coverage""" |
| 117 | + |
| 118 | + def test_set_logger_level(self): |
| 119 | + """Test logger level setting""" |
| 120 | + ping = Ping() |
| 121 | + logger = ping.set_logger_level("DEBUG") |
| 122 | + assert logger.level == 10 # DEBUG level |
| 123 | + |
| 124 | + logger = ping.set_logger_level("INFO") |
| 125 | + assert logger.level == 20 # INFO level |
| 126 | + |
| 127 | + logger = ping.set_logger_level("ERROR") |
| 128 | + assert logger.level == 40 # ERROR level |
| 129 | + |
| 130 | + def test_set_ping_layer_not_3(self): |
| 131 | + """Test _set_ping with layer != 3""" |
| 132 | + ping = Ping(layer=4, count=5, timeout=10) |
| 133 | + command = ping._set_ping(count=5, layer=4, timeout=10) |
| 134 | + assert "-c 5" in command |
| 135 | + assert "-t 10" in command |
| 136 | + |
| 137 | + def test_is_valid_ip_valid(self): |
| 138 | + """Test is_valid_ip with valid IPs""" |
| 139 | + assert Ping.is_valid_ip("192.168.1.1") is True |
| 140 | + assert Ping.is_valid_ip("8.8.8.8") is True |
| 141 | + assert Ping.is_valid_ip("127.0.0.1") is True |
| 142 | + assert Ping.is_valid_ip("1.1.1.1") is True |
| 143 | + assert Ping.is_valid_ip("255.255.255.255") is True |
| 144 | + |
| 145 | + def test_is_valid_ip_invalid(self): |
| 146 | + """Test is_valid_ip with invalid IPs""" |
| 147 | + assert Ping.is_valid_ip("256.1.1.1") is False |
| 148 | + assert Ping.is_valid_ip("192.168.1") is False |
| 149 | + assert Ping.is_valid_ip("invalid") is False |
| 150 | + assert Ping.is_valid_ip("192.168.1.1.1") is False |
| 151 | + assert Ping.is_valid_ip("") is False |
| 152 | + |
| 153 | + def test_add_ip_valid(self): |
| 154 | + """Test _add_ip with valid IP""" |
| 155 | + ping = Ping() |
| 156 | + command = ping._add_ip("192.168.1.1") |
| 157 | + assert command is not None |
| 158 | + assert "192.168.1.1" in command |
| 159 | + |
| 160 | + def test_add_ip_invalid(self): |
| 161 | + """Test _add_ip with invalid IP""" |
| 162 | + ping = Ping() |
| 163 | + command = ping._add_ip("invalid_ip") |
| 164 | + assert command is None |
| 165 | + |
| 166 | + @patch('subprocess.Popen') |
| 167 | + def test_ping_valid_ip(self, mock_popen): |
| 168 | + """Test ping method with valid IP""" |
| 169 | + mock_process = MagicMock() |
| 170 | + mock_process.communicate.return_value = ( |
| 171 | + b"PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.\n" |
| 172 | + b"64 bytes from 8.8.8.8: icmp_seq=1 ttl=117 time=20.1 ms\n" |
| 173 | + b"--- 8.8.8.8 ping statistics ---\n" |
| 174 | + b"1 packets transmitted, 1 received, 0% packet loss, time 0ms\n" |
| 175 | + b"rtt min/avg/max/mdev = 20.100/20.100/20.100/0.000 ms\n", |
| 176 | + b"" |
| 177 | + ) |
| 178 | + mock_popen.return_value = mock_process |
| 179 | + |
| 180 | + ping = Ping(count=1) |
| 181 | + result = ping.ping("8.8.8.8") |
| 182 | + assert result is not None |
| 183 | + mock_popen.assert_called_once() |
| 184 | + |
| 185 | + def test_ping_invalid_ip(self, capsys): |
| 186 | + """Test ping method with invalid IP""" |
| 187 | + ping = Ping() |
| 188 | + result = ping.ping("999.999.999.999") |
| 189 | + assert result is None |
| 190 | + |
| 191 | + def test_get_index_found(self): |
| 192 | + """Test get_index when element is found""" |
| 193 | + test_list = ["a", "b", "c", "d"] |
| 194 | + assert get_index(test_list, "b") == 1 |
| 195 | + assert get_index(test_list, "a") == 0 |
| 196 | + assert get_index(test_list, "d") == 3 |
| 197 | + |
| 198 | + def test_get_index_not_found(self): |
| 199 | + """Test get_index when element is not found""" |
| 200 | + test_list = ["a", "b", "c"] |
| 201 | + assert get_index(test_list, "z") is None |
| 202 | + assert get_index(test_list, "x") is None |
| 203 | + |
| 204 | + def test_ping_different_counts(self): |
| 205 | + """Test ping with different packet counts""" |
| 206 | + ping1 = Ping(count=1) |
| 207 | + ping2 = Ping(count=10) |
| 208 | + assert "-c 1" in ping1.command or "-n 1" in ping1.command |
| 209 | + assert "-c 10" in ping2.command or "-n 10" in ping2.command |
| 210 | + |
| 211 | + def test_ping_different_counts(self): |
| 212 | + """Test ping with different packet counts""" |
| 213 | + ping1 = Ping(count=1) |
| 214 | + ping2 = Ping(count=10) |
| 215 | + assert "-c 1" in ping1.command or "-n 1" in ping1.command |
| 216 | + assert "-c 10" in ping2.command or "-n 10" in ping2.command |
| 217 | + |
| 218 | + def test_set_ping_windows_os(self): |
| 219 | + """Test _set_ping for Windows OS""" |
| 220 | + ping = Ping(count=5) |
| 221 | + ping.os = "nt" |
| 222 | + command = ping._set_ping(count=5, layer=3, timeout=3) |
| 223 | + assert "-n 5" in command |
| 224 | + |
| 225 | + def test_set_ping_posix_os(self): |
| 226 | + """Test _set_ping for POSIX OS""" |
| 227 | + ping = Ping(count=5) |
| 228 | + ping.os = "posix" |
| 229 | + command = ping._set_ping(count=5, layer=3, timeout=3) |
| 230 | + assert "-c 5" in command |
| 231 | + |
| 232 | + def test_tcping_initialization(self): |
| 233 | + """Test tcping command initialization""" |
| 234 | + ping = Ping(command="tcping", layer=4) |
| 235 | + assert "tcping" in ping.command |
| 236 | + assert "-c 4" in ping.command or "-t" in ' '.join(ping.command) |
| 237 | + |
| 238 | + def test_ping_with_timeout(self): |
| 239 | + """Test ping with custom timeout""" |
| 240 | + ping1 = Ping(timeout=5) |
| 241 | + ping2 = Ping(timeout=10) |
| 242 | + # Timeout should be in the command for layer 4 |
| 243 | + ping_l4 = Ping(layer=4, timeout=5) |
| 244 | + assert "-t" in ' '.join(ping_l4.command) or "-t 5" in ping_l4.command |
| 245 | + |
| 246 | + def test_tcping_loss_percentage_calculation(self): |
| 247 | + """Test tcping loss percentage calculation (line 78)""" |
| 248 | + from pingping.ping import Ping |
| 249 | + |
| 250 | + # Mock tcping output with 80% successful (which means 80% success rate) |
| 251 | + tcping_output = """ |
| 252 | + Probing 192.168.1.1:80/tcp - Port is open |
| 253 | + Port is open |
| 254 | + Port is open |
| 255 | + Port is open |
| 256 | + |
| 257 | + Statistics: 4 probes sent, 80% successful |
| 258 | + """ |
| 259 | + |
| 260 | + # Test tcping command to hit line 78 |
| 261 | + # When command="tcping", line 78 calculates: 100 - percentage = 100 - 80 = 20% loss |
| 262 | + ping = Ping(command="tcping", layer=4) |
| 263 | + result = ping.fetch_ping_data(tcping_output, command="tcping") |
| 264 | + assert "loss_percentage" in result |
| 265 | + assert result["loss_percentage"] == 20.0 # 100 - 80 = 20% loss |
| 266 | + |
| 267 | + |
| 268 | +class TestCLIFunctions: |
| 269 | + """Test CLI functions: run() and help()""" |
| 270 | + |
| 271 | + def test_run_with_valid_ip(self): |
| 272 | + """Test run() function with valid IP address (lines 146-181)""" |
| 273 | + from pingping.ping import run |
| 274 | + |
| 275 | + with patch('sys.argv', ['pingping', '8.8.8.8']): |
| 276 | + with patch.object(Ping, 'ping', return_value={'ip': '8.8.8.8'}): |
| 277 | + result = run() |
| 278 | + assert result is not None |
| 279 | + assert result['ip'] == '8.8.8.8' |
| 280 | + |
| 281 | + def test_run_with_help_flag(self): |
| 282 | + """Test run() function with -h flag (lines 146-181)""" |
| 283 | + from pingping.ping import run |
| 284 | + |
| 285 | + with patch('sys.argv', ['pingping', '-h']): |
| 286 | + with pytest.raises(SystemExit) as exc_info: |
| 287 | + run() |
| 288 | + assert exc_info.value.code == -1 |
| 289 | + |
| 290 | + def test_run_with_help_long_flag(self): |
| 291 | + """Test run() function with --help flag (lines 146-181)""" |
| 292 | + from pingping.ping import run |
| 293 | + |
| 294 | + with patch('sys.argv', ['pingping', '--help']): |
| 295 | + with pytest.raises(SystemExit) as exc_info: |
| 296 | + run() |
| 297 | + assert exc_info.value.code == -1 |
| 298 | + |
| 299 | + def test_run_with_tcp_flag(self): |
| 300 | + """Test run() function with -l4 flag (lines 146-181)""" |
| 301 | + from pingping.ping import run |
| 302 | + |
| 303 | + with patch('sys.argv', ['pingping', '-l4', '192.168.1.1']): |
| 304 | + with patch.object(Ping, 'ping', return_value={'ip': '192.168.1.1'}): |
| 305 | + result = run() |
| 306 | + assert result is not None |
| 307 | + |
| 308 | + def test_run_with_web_flag(self): |
| 309 | + """Test run() function with --web flag (lines 146-181)""" |
| 310 | + from pingping.ping import run |
| 311 | + |
| 312 | + with patch('sys.argv', ['pingping', '--web', '192.168.1.1']): |
| 313 | + with patch.object(Ping, 'ping', return_value={'ip': '192.168.1.1'}): |
| 314 | + result = run() |
| 315 | + assert result is not None |
| 316 | + |
| 317 | + def test_run_with_tcp_long_flag(self): |
| 318 | + """Test run() function with --tcp flag (lines 146-181)""" |
| 319 | + from pingping.ping import run |
| 320 | + |
| 321 | + with patch('sys.argv', ['pingping', '--tcp', '192.168.1.1']): |
| 322 | + with patch.object(Ping, 'ping', return_value={'ip': '192.168.1.1'}): |
| 323 | + result = run() |
| 324 | + assert result is not None |
| 325 | + |
| 326 | + def test_run_with_http_flag(self): |
| 327 | + """Test run() function with --http flag (lines 146-181)""" |
| 328 | + from pingping.ping import run |
| 329 | + |
| 330 | + with patch('sys.argv', ['pingping', '--http', '192.168.1.1']): |
| 331 | + with patch.object(Ping, 'ping', return_value={'ip': '192.168.1.1'}): |
| 332 | + result = run() |
| 333 | + assert result is not None |
| 334 | + |
| 335 | + def test_run_with_count_flag(self): |
| 336 | + """Test run() function with -c flag (lines 146-181)""" |
| 337 | + from pingping.ping import run |
| 338 | + |
| 339 | + with patch('sys.argv', ['pingping', '-c', '10', '8.8.8.8']): |
| 340 | + with patch.object(Ping, 'ping', return_value={'ip': '8.8.8.8'}): |
| 341 | + result = run() |
| 342 | + assert result is not None |
| 343 | + |
| 344 | + def test_run_with_count_long_flag(self): |
| 345 | + """Test run() function with --count flag (lines 146-181)""" |
| 346 | + from pingping.ping import run |
| 347 | + |
| 348 | + with patch('sys.argv', ['pingping', '--count', '15', '1.1.1.1']): |
| 349 | + with patch.object(Ping, 'ping', return_value={'ip': '1.1.1.1'}): |
| 350 | + result = run() |
| 351 | + assert result is not None |
| 352 | + |
| 353 | + def test_run_with_no_ip_address(self): |
| 354 | + """Test run() function with invalid arguments - no valid IP (lines 146-181)""" |
| 355 | + from pingping.ping import run |
| 356 | + |
| 357 | + # When -c flag is used but no valid IP is provided, it will create a default Ping |
| 358 | + # and attempt to ping None, which returns None (not a SystemExit) |
| 359 | + with patch('sys.argv', ['pingping', '--help', 'invalid']): |
| 360 | + with pytest.raises(SystemExit) as exc_info: |
| 361 | + run() |
| 362 | + assert exc_info.value.code == -1 |
| 363 | + |
| 364 | + def test_run_with_no_arguments(self): |
| 365 | + """Test run() function with no arguments (lines 146-181)""" |
| 366 | + from pingping.ping import run |
| 367 | + |
| 368 | + with patch('sys.argv', ['pingping']): |
| 369 | + with pytest.raises(SystemExit) as exc_info: |
| 370 | + run() |
| 371 | + assert exc_info.value.code == -1 |
| 372 | + |
| 373 | + def test_help_function(self): |
| 374 | + """Test help() function (lines 185-189)""" |
| 375 | + from pingping.ping import help |
| 376 | + |
| 377 | + with pytest.raises(SystemExit) as exc_info: |
| 378 | + help() |
| 379 | + assert exc_info.value.code == -1 |
0 commit comments