def test_send_cmd_unix_endings(unicode_chr): """Ensure the correct line ending is used when unix is specified.""" runner = Bladerunner({ "unix_line_endings": True, "windows_line_endings": False, "second_password": "******", }) server = Mock() # server.expect returns an integer of the prompt matched in its list # we want to return N+1 to simulate matching a passwd prompt server.expect = Mock(return_value=( len(runner.options["shell_prompts"]) + len(runner.options["extra_prompts"]) + 1 )) with patch.object(base, "format_output") as p_format_out: runner._send_cmd("fake", server) p_format_out.assert_called_once_with(server.before, "fake", runner.options) server.send.assert_called_once_with("fake{0}".format(unicode_chr(0x000A))) # the second password should be send with sendline server.sendline.assert_called_once_with("hunter55") assert server.expect.call_count == 2
def test_send_cmd_unix_endings(unicode_chr): """Ensure the correct line ending is used when unix is specified.""" runner = Bladerunner({ "unix_line_endings": True, "windows_line_endings": False, "second_password": "******", }) server = Mock() # server.expect returns an integer of the prompt matched in its list # we want to return N+1 to simulate matching a passwd prompt server.expect = Mock(return_value=(len(runner.options["shell_prompts"]) + len(runner.options["extra_prompts"]) + 1)) with patch.object(base, "format_output") as p_format_out: runner._send_cmd("fake", server) p_format_out.assert_called_once_with(server.before, "fake", runner.options) server.send.assert_called_once_with("fake{0}".format(unicode_chr(0x000A))) # the second password should be send with sendline server.sendline.assert_called_once_with("hunter55") assert server.expect.call_count == 2
def test_fallback_prompt_guess(pexpect_exceptions): """If a TIMEOUT or EOF error is raised, call _try_for_unmatched_prompt.""" server = Mock() server.expect = Mock(side_effect=pexpect_exceptions("mock exception")) runner = Bladerunner({ "username": "******", "password": "******", }) with patch.object(runner, "_try_for_unmatched_prompt") as p_try_for: runner._send_cmd("not real", server) p_try_for.assert_called_once_with(server, server.before, "not real")
def test_send_cmd_no_preference(): """Ensure we call to pexpect's sendline which uses os.linesep.""" runner = Bladerunner() server = Mock() server.expect = Mock(return_value=1) with patch.object(base, "format_output") as p_format_out: runner._send_cmd("mock", server) p_format_out.assert_called_once_with(server.before, "mock", runner.options) server.sendline.assert_called_once_with("mock") assert server.expect.call_count == 1
def test_send_cmd_winderps_endings(unicode_chr): """Ensure the wrong line endings are used when winderps is required.""" runner = Bladerunner({ "unix_line_endings": False, "windows_line_endings": True, }) server = Mock() server.expect = Mock(return_value=1) with patch.object(base, "format_output") as p_format_out: runner._send_cmd("faked", server) p_format_out.assert_called_once_with(server.before, "faked", runner.options) server.send.assert_called_once_with("faked{0}{1}".format( unicode_chr(0x000D), unicode_chr(0x000A), )) assert server.expect.call_count == 1
def test_send_cmd_winderps_endings(unicode_chr): """Ensure the wrong line endings are used when winderps is required.""" runner = Bladerunner({ "unix_line_endings": False, "windows_line_endings": True, }) server = Mock() server.expect = Mock(return_value=1) with patch.object(base, "format_output") as p_format_out: runner._send_cmd("fake", server) p_format_out.assert_called_once_with(server.before, "fake", runner.options) server.send.assert_called_once_with("fake{0}{1}".format( unicode_chr(0x000D), unicode_chr(0x000A), )) assert server.expect.call_count == 1
def test_send_cmd_no_line_endings(): """If are False, pexpect's sendline should be called (which uses os).""" runner = Bladerunner() # we need to update after init as init will fallback to os preferred runner.options.update({ "unix_line_endings": False, "windows_line_endings": False, }) server = Mock() server.expect = Mock(return_value=1) with patch.object(base, "format_output") as p_format_out: runner._send_cmd("fake_cmd", server) p_format_out.assert_called_once_with( server.before, "fake_cmd", runner.options, ) server.sendline.assert_called_once_with("fake_cmd") assert server.expect.call_count == 1