Test the _set_module_options helper function.
| 155 | |
| 156 | |
| 157 | class TestSetModuleOptions: |
| 158 | """Test the _set_module_options helper function.""" |
| 159 | |
| 160 | @pytest.fixture |
| 161 | def mock_module(self): |
| 162 | """Fixture providing a mock module object.""" |
| 163 | module = Mock() |
| 164 | module.fullname = 'exploit/test/module' |
| 165 | module.__setitem__ = Mock() |
| 166 | return module |
| 167 | |
| 168 | @pytest.mark.asyncio |
| 169 | async def test_set_module_options_basic(self, mock_module): |
| 170 | """Test basic option setting.""" |
| 171 | options = {'RHOSTS': '192.168.1.1', 'RPORT': '80'} |
| 172 | |
| 173 | await _set_module_options(mock_module, options) |
| 174 | |
| 175 | # Should be called twice, once for each option |
| 176 | assert mock_module.__setitem__.call_count == 2 |
| 177 | mock_module.__setitem__.assert_any_call('RHOSTS', '192.168.1.1') |
| 178 | mock_module.__setitem__.assert_any_call('RPORT', 80) # Type conversion: '80' -> 80 |
| 179 | |
| 180 | @pytest.mark.asyncio |
| 181 | async def test_set_module_options_type_conversion(self, mock_module): |
| 182 | """Test option setting with type conversion.""" |
| 183 | options = { |
| 184 | 'RPORT': '80', # String number -> int |
| 185 | 'SSL': 'true', # String boolean -> bool |
| 186 | 'VERBOSE': 'false', # String boolean -> bool |
| 187 | 'THREADS': '10' # String number -> int |
| 188 | } |
| 189 | |
| 190 | await _set_module_options(mock_module, options) |
| 191 | |
| 192 | # Verify type conversions |
| 193 | calls = mock_module.__setitem__.call_args_list |
| 194 | call_dict = {call[0][0]: call[0][1] for call in calls} |
| 195 | |
| 196 | assert call_dict['RPORT'] == 80 |
| 197 | assert call_dict['SSL'] is True |
| 198 | assert call_dict['VERBOSE'] is False |
| 199 | assert call_dict['THREADS'] == 10 |
| 200 | |
| 201 | @pytest.mark.asyncio |
| 202 | async def test_set_module_options_error(self, mock_module): |
| 203 | """Test option setting with error.""" |
| 204 | mock_module.__setitem__.side_effect = KeyError("Invalid option") |
| 205 | options = {'INVALID_OPT': 'value'} |
| 206 | |
| 207 | with pytest.raises(ValueError, match="Failed to set option"): |
| 208 | await _set_module_options(mock_module, options) |
| 209 | |
| 210 | |
| 211 | class TestGetMsfConsole: |
nothing calls this directly
no outgoing calls
no test coverage detected