|
7 | 7 |
|
8 | 8 | sys.path.insert(0, '..') |
9 | 9 |
|
10 | | -from wifite.tools.hcxdumptool import HcxDumpTool |
| 10 | +from wifite.tools.hcxdumptool import HcxDumpTool, HcxDumpToolPassive |
11 | 11 | from wifite.config import Configuration |
12 | 12 |
|
13 | 13 |
|
@@ -165,5 +165,153 @@ def test_command_building_triple_interface(self, mock_process, mock_config_init) |
165 | 165 | self.assertEqual(call_args[i_indices[2] + 1], 'wlan2') |
166 | 166 |
|
167 | 167 |
|
| 168 | +class TestHcxDumpToolPassive(unittest.TestCase): |
| 169 | + """Test suite for HcxDumpToolPassive class""" |
| 170 | + |
| 171 | + def setUp(self): |
| 172 | + """Set up test fixtures""" |
| 173 | + Configuration.interface = None |
| 174 | + |
| 175 | + @patch('wifite.tools.hcxdumptool.Configuration.initialize') |
| 176 | + def test_initialization_with_interface(self, mock_config_init): |
| 177 | + """Test initialization with explicit interface""" |
| 178 | + tool = HcxDumpToolPassive(interface='wlan0', output_file='/tmp/passive.pcapng') |
| 179 | + |
| 180 | + self.assertEqual(tool.interface, 'wlan0') |
| 181 | + self.assertEqual(tool.output_file, '/tmp/passive.pcapng') |
| 182 | + self.assertIsNone(tool.pid) |
| 183 | + self.assertIsNone(tool.proc) |
| 184 | + |
| 185 | + @patch('wifite.tools.hcxdumptool.Configuration.initialize') |
| 186 | + @patch('wifite.tools.hcxdumptool.Configuration.temp') |
| 187 | + def test_initialization_with_defaults(self, mock_temp, mock_config_init): |
| 188 | + """Test initialization with default output file""" |
| 189 | + mock_temp.return_value = '/tmp/wifite_' |
| 190 | + Configuration.interface = 'wlan0' |
| 191 | + |
| 192 | + tool = HcxDumpToolPassive() |
| 193 | + |
| 194 | + self.assertEqual(tool.interface, 'wlan0') |
| 195 | + self.assertEqual(tool.output_file, '/tmp/wifite_passive_pmkid.pcapng') |
| 196 | + |
| 197 | + @patch('wifite.tools.hcxdumptool.Configuration.initialize') |
| 198 | + def test_initialization_no_interface_raises_error(self, mock_config_init): |
| 199 | + """Test that missing interface raises exception""" |
| 200 | + Configuration.interface = None |
| 201 | + |
| 202 | + with self.assertRaises(Exception) as context: |
| 203 | + HcxDumpToolPassive() |
| 204 | + |
| 205 | + self.assertIn('must be defined', str(context.exception)) |
| 206 | + |
| 207 | + @patch('wifite.tools.hcxdumptool.Configuration.initialize') |
| 208 | + @patch('wifite.tools.hcxdumptool.Process') |
| 209 | + @patch('wifite.tools.hcxdumptool.time.sleep') |
| 210 | + def test_enter_starts_process(self, mock_sleep, mock_process, mock_config_init): |
| 211 | + """Test that __enter__ starts hcxdumptool with correct flags""" |
| 212 | + mock_proc_instance = MagicMock() |
| 213 | + mock_proc_instance.pid.pid = 12345 |
| 214 | + mock_proc_instance.poll.return_value = None |
| 215 | + mock_process.return_value = mock_proc_instance |
| 216 | + |
| 217 | + tool = HcxDumpToolPassive(interface='wlan0', output_file='/tmp/passive.pcapng') |
| 218 | + |
| 219 | + with tool: |
| 220 | + # Verify Process was called |
| 221 | + mock_process.assert_called_once() |
| 222 | + |
| 223 | + # Get the command that was passed to Process |
| 224 | + call_args = mock_process.call_args[0][0] |
| 225 | + |
| 226 | + # Verify command structure |
| 227 | + self.assertEqual(call_args[0], 'hcxdumptool') |
| 228 | + self.assertIn('-i', call_args) |
| 229 | + self.assertIn('wlan0', call_args) |
| 230 | + self.assertIn('--rds=3', call_args) |
| 231 | + self.assertIn('-w', call_args) |
| 232 | + self.assertIn('/tmp/passive.pcapng', call_args) |
| 233 | + self.assertIn('--enable_status=15', call_args) |
| 234 | + |
| 235 | + # Verify PID was set |
| 236 | + self.assertEqual(tool.pid, 12345) |
| 237 | + self.assertIsNotNone(tool.proc) |
| 238 | + |
| 239 | + @patch('wifite.tools.hcxdumptool.Configuration.initialize') |
| 240 | + @patch('wifite.tools.hcxdumptool.Process') |
| 241 | + @patch('wifite.tools.hcxdumptool.time.sleep') |
| 242 | + def test_is_running_when_active(self, mock_sleep, mock_process, mock_config_init): |
| 243 | + """Test is_running returns True when process is active""" |
| 244 | + mock_proc_instance = MagicMock() |
| 245 | + mock_proc_instance.pid.pid = 12345 |
| 246 | + mock_proc_instance.poll.return_value = None # Process is running |
| 247 | + mock_process.return_value = mock_proc_instance |
| 248 | + |
| 249 | + tool = HcxDumpToolPassive(interface='wlan0', output_file='/tmp/passive.pcapng') |
| 250 | + |
| 251 | + with tool: |
| 252 | + self.assertTrue(tool.is_running()) |
| 253 | + |
| 254 | + @patch('wifite.tools.hcxdumptool.Configuration.initialize') |
| 255 | + @patch('wifite.tools.hcxdumptool.Process') |
| 256 | + @patch('wifite.tools.hcxdumptool.time.sleep') |
| 257 | + def test_is_running_when_stopped(self, mock_sleep, mock_process, mock_config_init): |
| 258 | + """Test is_running returns False when process has stopped""" |
| 259 | + mock_proc_instance = MagicMock() |
| 260 | + mock_proc_instance.pid.pid = 12345 |
| 261 | + mock_proc_instance.poll.return_value = 0 # Process has exited |
| 262 | + mock_process.return_value = mock_proc_instance |
| 263 | + |
| 264 | + tool = HcxDumpToolPassive(interface='wlan0', output_file='/tmp/passive.pcapng') |
| 265 | + |
| 266 | + with tool: |
| 267 | + self.assertFalse(tool.is_running()) |
| 268 | + |
| 269 | + @patch('wifite.tools.hcxdumptool.Configuration.initialize') |
| 270 | + @patch('wifite.tools.hcxdumptool.os.path.exists') |
| 271 | + @patch('wifite.tools.hcxdumptool.os.path.getsize') |
| 272 | + def test_get_capture_size_file_exists(self, mock_getsize, mock_exists, mock_config_init): |
| 273 | + """Test get_capture_size returns file size when file exists""" |
| 274 | + mock_exists.return_value = True |
| 275 | + mock_getsize.return_value = 1024000 # 1MB |
| 276 | + |
| 277 | + tool = HcxDumpToolPassive(interface='wlan0', output_file='/tmp/passive.pcapng') |
| 278 | + |
| 279 | + size = tool.get_capture_size() |
| 280 | + self.assertEqual(size, 1024000) |
| 281 | + mock_exists.assert_called_once_with('/tmp/passive.pcapng') |
| 282 | + mock_getsize.assert_called_once_with('/tmp/passive.pcapng') |
| 283 | + |
| 284 | + @patch('wifite.tools.hcxdumptool.Configuration.initialize') |
| 285 | + @patch('wifite.tools.hcxdumptool.os.path.exists') |
| 286 | + def test_get_capture_size_file_not_exists(self, mock_exists, mock_config_init): |
| 287 | + """Test get_capture_size returns 0 when file doesn't exist""" |
| 288 | + mock_exists.return_value = False |
| 289 | + |
| 290 | + tool = HcxDumpToolPassive(interface='wlan0', output_file='/tmp/passive.pcapng') |
| 291 | + |
| 292 | + size = tool.get_capture_size() |
| 293 | + self.assertEqual(size, 0) |
| 294 | + mock_exists.assert_called_once_with('/tmp/passive.pcapng') |
| 295 | + |
| 296 | + @patch('wifite.tools.hcxdumptool.Configuration.initialize') |
| 297 | + @patch('wifite.tools.hcxdumptool.Process') |
| 298 | + @patch('wifite.tools.hcxdumptool.time.sleep') |
| 299 | + @patch('wifite.tools.hcxdumptool.os.kill') |
| 300 | + def test_exit_stops_process(self, mock_kill, mock_sleep, mock_process, mock_config_init): |
| 301 | + """Test that __exit__ stops the process gracefully""" |
| 302 | + mock_proc_instance = MagicMock() |
| 303 | + mock_proc_instance.pid.pid = 12345 |
| 304 | + mock_proc_instance.poll.return_value = None # Process is running |
| 305 | + mock_process.return_value = mock_proc_instance |
| 306 | + |
| 307 | + tool = HcxDumpToolPassive(interface='wlan0', output_file='/tmp/passive.pcapng') |
| 308 | + |
| 309 | + with tool: |
| 310 | + pass # Exit the context |
| 311 | + |
| 312 | + # Verify interrupt was called |
| 313 | + mock_proc_instance.interrupt.assert_called_once() |
| 314 | + |
| 315 | + |
168 | 316 | if __name__ == '__main__': |
169 | 317 | unittest.main() |
0 commit comments