|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +Unit tests for channel synchronization in dual interface WPA attacks. |
| 6 | +
|
| 7 | +Tests the channel synchronization functionality including: |
| 8 | +- Channel verification on both interfaces |
| 9 | +- Handling of channel mismatch |
| 10 | +- Recovery from channel setting failures |
| 11 | +""" |
| 12 | + |
| 13 | +import unittest |
| 14 | +import sys |
| 15 | +from unittest.mock import Mock, patch, MagicMock, call |
| 16 | + |
| 17 | +# Mock sys.argv to prevent argparse from reading test arguments |
| 18 | +original_argv = sys.argv |
| 19 | +sys.argv = ['wifite'] |
| 20 | + |
| 21 | +from wifite.config import Configuration |
| 22 | +from wifite.model.target import Target |
| 23 | + |
| 24 | +# Set required Configuration attributes |
| 25 | +Configuration.interface = 'wlan0' |
| 26 | +Configuration.wpa_attack_timeout = 600 |
| 27 | +Configuration.wpa_deauth_timeout = 10 |
| 28 | +Configuration.interface_primary = None |
| 29 | +Configuration.interface_secondary = None |
| 30 | +Configuration.dual_interface_enabled = False |
| 31 | + |
| 32 | +from wifite.attack.wpa import AttackWPA |
| 33 | + |
| 34 | +# Restore original argv |
| 35 | +sys.argv = original_argv |
| 36 | + |
| 37 | + |
| 38 | +class TestChannelSynchronization(unittest.TestCase): |
| 39 | + """Test channel synchronization for dual interface WPA attacks.""" |
| 40 | + |
| 41 | + def setUp(self): |
| 42 | + """Set up test fixtures.""" |
| 43 | + self.mock_target = Mock(spec=Target) |
| 44 | + self.mock_target.bssid = 'AA:BB:CC:DD:EE:FF' |
| 45 | + self.mock_target.essid = 'TestNetwork' |
| 46 | + self.mock_target.channel = 6 |
| 47 | + self.mock_target.encryption = 'WPA2' |
| 48 | + self.mock_target.power = -50 |
| 49 | + self.mock_target.wps = False |
| 50 | + |
| 51 | + Configuration.interface = 'wlan0' |
| 52 | + Configuration.interface_primary = None |
| 53 | + Configuration.interface_secondary = None |
| 54 | + |
| 55 | + @patch('wifite.util.interface_manager.InterfaceManager') |
| 56 | + @patch('wifite.attack.wpa.Color') |
| 57 | + def test_verify_channel_sync_both_correct(self, mock_color, mock_interface_manager): |
| 58 | + """Test channel verification when both interfaces are on correct channel.""" |
| 59 | + # Setup |
| 60 | + attack = AttackWPA(self.mock_target) |
| 61 | + attack.capture_interface = 'wlan0mon' |
| 62 | + attack.deauth_interface = 'wlan1mon' |
| 63 | + attack.view = None |
| 64 | + |
| 65 | + # Mock both interfaces on correct channel |
| 66 | + mock_interface_manager._get_interface_channel.side_effect = [6, 6] |
| 67 | + |
| 68 | + # Execute |
| 69 | + attack._verify_channel_sync() |
| 70 | + |
| 71 | + # Verify |
| 72 | + self.assertEqual(mock_interface_manager._get_interface_channel.call_count, 2) |
| 73 | + mock_interface_manager._get_interface_channel.assert_any_call('wlan0mon') |
| 74 | + mock_interface_manager._get_interface_channel.assert_any_call('wlan1mon') |
| 75 | + |
| 76 | + # Should show success message |
| 77 | + success_calls = [c for c in mock_color.pl.call_args_list |
| 78 | + if 'Both interfaces verified' in str(c)] |
| 79 | + self.assertGreater(len(success_calls), 0) |
| 80 | + |
| 81 | + @patch('wifite.util.interface_manager.InterfaceManager') |
| 82 | + @patch('wifite.attack.wpa.Color') |
| 83 | + def test_verify_channel_sync_capture_mismatch(self, mock_color, mock_interface_manager): |
| 84 | + """Test channel verification when capture interface is on wrong channel.""" |
| 85 | + # Setup |
| 86 | + attack = AttackWPA(self.mock_target) |
| 87 | + attack.capture_interface = 'wlan0mon' |
| 88 | + attack.deauth_interface = 'wlan1mon' |
| 89 | + attack.view = None |
| 90 | + |
| 91 | + # Mock capture interface on wrong channel (11 instead of 6) |
| 92 | + mock_interface_manager._get_interface_channel.side_effect = [11, 6] |
| 93 | + |
| 94 | + # Execute |
| 95 | + attack._verify_channel_sync() |
| 96 | + |
| 97 | + # Verify warning was shown |
| 98 | + warning_calls = [c for c in mock_color.pl.call_args_list |
| 99 | + if 'Warning' in str(c) and 'wlan0mon' in str(c)] |
| 100 | + self.assertGreater(len(warning_calls), 0) |
| 101 | + |
| 102 | + @patch('wifite.util.interface_manager.InterfaceManager') |
| 103 | + @patch('wifite.attack.wpa.Color') |
| 104 | + def test_verify_channel_sync_deauth_mismatch(self, mock_color, mock_interface_manager): |
| 105 | + """Test channel verification when deauth interface is on wrong channel.""" |
| 106 | + # Setup |
| 107 | + attack = AttackWPA(self.mock_target) |
| 108 | + attack.capture_interface = 'wlan0mon' |
| 109 | + attack.deauth_interface = 'wlan1mon' |
| 110 | + attack.view = None |
| 111 | + |
| 112 | + # Mock deauth interface on wrong channel (1 instead of 6) |
| 113 | + mock_interface_manager._get_interface_channel.side_effect = [6, 1] |
| 114 | + |
| 115 | + # Execute |
| 116 | + attack._verify_channel_sync() |
| 117 | + |
| 118 | + # Verify warning was shown |
| 119 | + warning_calls = [c for c in mock_color.pl.call_args_list |
| 120 | + if 'Warning' in str(c) and 'wlan1mon' in str(c)] |
| 121 | + self.assertGreater(len(warning_calls), 0) |
| 122 | + |
| 123 | + @patch('wifite.util.interface_manager.InterfaceManager') |
| 124 | + @patch('wifite.attack.wpa.Color') |
| 125 | + def test_verify_channel_sync_both_mismatch(self, mock_color, mock_interface_manager): |
| 126 | + """Test channel verification when both interfaces are on wrong channels.""" |
| 127 | + # Setup |
| 128 | + attack = AttackWPA(self.mock_target) |
| 129 | + attack.capture_interface = 'wlan0mon' |
| 130 | + attack.deauth_interface = 'wlan1mon' |
| 131 | + attack.view = None |
| 132 | + |
| 133 | + # Mock both interfaces on wrong channels |
| 134 | + mock_interface_manager._get_interface_channel.side_effect = [11, 1] |
| 135 | + |
| 136 | + # Execute |
| 137 | + attack._verify_channel_sync() |
| 138 | + |
| 139 | + # Verify warnings for both interfaces |
| 140 | + warning_calls = [c for c in mock_color.pl.call_args_list |
| 141 | + if 'Warning' in str(c)] |
| 142 | + self.assertGreaterEqual(len(warning_calls), 2) |
| 143 | + |
| 144 | + @patch('wifite.util.process.Process') |
| 145 | + @patch('wifite.attack.wpa.Color') |
| 146 | + def test_set_interface_channels_both_success(self, mock_color, mock_process): |
| 147 | + """Test setting channels when both interfaces succeed.""" |
| 148 | + # Setup |
| 149 | + attack = AttackWPA(self.mock_target) |
| 150 | + attack.capture_interface = 'wlan0mon' |
| 151 | + attack.deauth_interface = 'wlan1mon' |
| 152 | + attack.view = None |
| 153 | + |
| 154 | + # Mock successful process execution |
| 155 | + mock_proc = MagicMock() |
| 156 | + mock_process.return_value = mock_proc |
| 157 | + |
| 158 | + # Execute |
| 159 | + attack._set_interface_channels() |
| 160 | + |
| 161 | + # Verify both interfaces were set |
| 162 | + self.assertEqual(mock_process.call_count, 2) |
| 163 | + mock_process.assert_any_call(['iw', 'wlan0mon', 'set', 'channel', '6']) |
| 164 | + mock_process.assert_any_call(['iw', 'wlan1mon', 'set', 'channel', '6']) |
| 165 | + |
| 166 | + # Should show success message |
| 167 | + success_calls = [c for c in mock_color.pl.call_args_list |
| 168 | + if 'Both interfaces set' in str(c)] |
| 169 | + self.assertGreater(len(success_calls), 0) |
| 170 | + |
| 171 | + @patch('wifite.util.process.Process') |
| 172 | + @patch('wifite.attack.wpa.Color') |
| 173 | + def test_set_interface_channels_capture_fails(self, mock_color, mock_process): |
| 174 | + """Test setting channels when capture interface fails.""" |
| 175 | + # Setup |
| 176 | + attack = AttackWPA(self.mock_target) |
| 177 | + attack.capture_interface = 'wlan0mon' |
| 178 | + attack.deauth_interface = 'wlan1mon' |
| 179 | + attack.view = None |
| 180 | + |
| 181 | + # Mock first call (capture) fails, second (deauth) succeeds |
| 182 | + def process_side_effect(cmd): |
| 183 | + mock_proc = MagicMock() |
| 184 | + if 'wlan0mon' in cmd: |
| 185 | + mock_proc.wait.side_effect = Exception('Channel set failed') |
| 186 | + return mock_proc |
| 187 | + |
| 188 | + mock_process.side_effect = process_side_effect |
| 189 | + |
| 190 | + # Execute |
| 191 | + attack._set_interface_channels() |
| 192 | + |
| 193 | + # Verify error was logged |
| 194 | + error_calls = [c for c in mock_color.pl.call_args_list |
| 195 | + if 'Error' in str(c) and 'wlan0mon' in str(c)] |
| 196 | + self.assertGreater(len(error_calls), 0) |
| 197 | + |
| 198 | + # Should show warning about only one interface working |
| 199 | + warning_calls = [c for c in mock_color.pl.call_args_list |
| 200 | + if 'Only' in str(c) and 'wlan1mon' in str(c)] |
| 201 | + self.assertGreater(len(warning_calls), 0) |
| 202 | + |
| 203 | + @patch('wifite.util.process.Process') |
| 204 | + @patch('wifite.attack.wpa.Color') |
| 205 | + def test_set_interface_channels_deauth_fails(self, mock_color, mock_process): |
| 206 | + """Test setting channels when deauth interface fails.""" |
| 207 | + # Setup |
| 208 | + attack = AttackWPA(self.mock_target) |
| 209 | + attack.capture_interface = 'wlan0mon' |
| 210 | + attack.deauth_interface = 'wlan1mon' |
| 211 | + attack.view = None |
| 212 | + |
| 213 | + # Mock first call (capture) succeeds, second (deauth) fails |
| 214 | + def process_side_effect(cmd): |
| 215 | + mock_proc = MagicMock() |
| 216 | + if 'wlan1mon' in cmd: |
| 217 | + mock_proc.wait.side_effect = Exception('Channel set failed') |
| 218 | + return mock_proc |
| 219 | + |
| 220 | + mock_process.side_effect = process_side_effect |
| 221 | + |
| 222 | + # Execute |
| 223 | + attack._set_interface_channels() |
| 224 | + |
| 225 | + # Verify error was logged |
| 226 | + error_calls = [c for c in mock_color.pl.call_args_list |
| 227 | + if 'Error' in str(c) and 'wlan1mon' in str(c)] |
| 228 | + self.assertGreater(len(error_calls), 0) |
| 229 | + |
| 230 | + # Should show warning about only one interface working |
| 231 | + warning_calls = [c for c in mock_color.pl.call_args_list |
| 232 | + if 'Only' in str(c) and 'wlan0mon' in str(c)] |
| 233 | + self.assertGreater(len(warning_calls), 0) |
| 234 | + |
| 235 | + @patch('wifite.util.process.Process') |
| 236 | + @patch('wifite.attack.wpa.Color') |
| 237 | + def test_set_interface_channels_both_fail(self, mock_color, mock_process): |
| 238 | + """Test setting channels when both interfaces fail.""" |
| 239 | + # Setup |
| 240 | + attack = AttackWPA(self.mock_target) |
| 241 | + attack.capture_interface = 'wlan0mon' |
| 242 | + attack.deauth_interface = 'wlan1mon' |
| 243 | + attack.view = None |
| 244 | + |
| 245 | + # Mock both calls fail |
| 246 | + mock_proc = MagicMock() |
| 247 | + mock_proc.wait.side_effect = Exception('Channel set failed') |
| 248 | + mock_process.return_value = mock_proc |
| 249 | + |
| 250 | + # Execute |
| 251 | + attack._set_interface_channels() |
| 252 | + |
| 253 | + # Verify error messages for both interfaces |
| 254 | + error_calls = [c for c in mock_color.pl.call_args_list |
| 255 | + if 'Error' in str(c)] |
| 256 | + self.assertGreaterEqual(len(error_calls), 2) |
| 257 | + |
| 258 | + # Should show error about both interfaces failing |
| 259 | + both_fail_calls = [c for c in mock_color.pl.call_args_list |
| 260 | + if 'Failed to set channel on both' in str(c)] |
| 261 | + self.assertGreater(len(both_fail_calls), 0) |
| 262 | + |
| 263 | + |
| 264 | +if __name__ == '__main__': |
| 265 | + unittest.main() |
0 commit comments