Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 1 | """Test Python APIs for process IO.""" |
| 2 | |
| 3 | import os, sys, time |
| 4 | import unittest2 |
| 5 | import lldb |
| 6 | from lldbtest import * |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 7 | import lldbutil |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 8 | |
| 9 | class ProcessIOTestCase(TestBase): |
| 10 | |
Greg Clayton | 4570d3e | 2013-12-10 23:19:29 +0000 | [diff] [blame] | 11 | mydir = TestBase.compute_mydir(__file__) |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 12 | |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 13 | def setUp(self): |
| 14 | # Call super's setUp(). |
| 15 | TestBase.setUp(self) |
| 16 | # Get the full path to our executable to be debugged. |
| 17 | self.exe = os.path.join(os.getcwd(), "process_io") |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 18 | self.local_input_file = os.path.join(os.getcwd(), "input.txt") |
| 19 | self.local_output_file = os.path.join(os.getcwd(), "output.txt") |
| 20 | self.local_error_file = os.path.join(os.getcwd(), "error.txt") |
| 21 | |
| 22 | self.input_file = os.path.join(self.get_process_working_directory(), "input.txt") |
| 23 | self.output_file = os.path.join(self.get_process_working_directory(), "output.txt") |
| 24 | self.error_file = os.path.join(self.get_process_working_directory(), "error.txt") |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 25 | self.lines = ["Line 1", "Line 2", "Line 3"] |
Vince Harron | 4a8abd3 | 2015-02-13 19:15:24 +0000 | [diff] [blame] | 26 | |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 27 | @skipIfWindows # stdio manipulation unsupported on Windows |
| 28 | @python_api_test |
| 29 | def test_stdin_by_api(self): |
| 30 | """Exercise SBProcess.PutSTDIN().""" |
| 31 | self.build() |
| 32 | self.create_target() |
| 33 | self.run_process(True) |
| 34 | output = self.process.GetSTDOUT(1000) |
| 35 | self.check_process_output(output, output) |
| 36 | |
| 37 | @skipIfWindows # stdio manipulation unsupported on Windows |
| 38 | @python_api_test |
| 39 | def test_stdin_redirection(self): |
| 40 | """Exercise SBLaunchInfo::AddOpenFileAction() for STDIN without specifying STDOUT or STDERR.""" |
| 41 | self.build() |
| 42 | self.create_target() |
| 43 | self.redirect_stdin() |
| 44 | self.run_process(False) |
| 45 | output = self.process.GetSTDOUT(1000) |
| 46 | self.check_process_output(output, output) |
| 47 | |
| 48 | @skipIfWindows # stdio manipulation unsupported on Windows |
| 49 | @python_api_test |
| 50 | def test_stdout_redirection(self): |
| 51 | """Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT without specifying STDIN or STDERR.""" |
| 52 | self.build() |
| 53 | self.create_target() |
| 54 | self.redirect_stdout() |
| 55 | self.run_process(True) |
| 56 | output = self.read_output_file_and_delete() |
| 57 | error = self.process.GetSTDOUT(1000) |
| 58 | self.check_process_output(output, error) |
| 59 | |
| 60 | @skipIfWindows # stdio manipulation unsupported on Windows |
| 61 | @python_api_test |
| 62 | def test_stderr_redirection(self): |
| 63 | """Exercise SBLaunchInfo::AddOpenFileAction() for STDERR without specifying STDIN or STDOUT.""" |
| 64 | self.build() |
| 65 | self.create_target() |
| 66 | self.redirect_stderr() |
| 67 | self.run_process(True) |
| 68 | output = self.process.GetSTDOUT(1000) |
| 69 | error = self.read_error_file_and_delete() |
| 70 | self.check_process_output(output, error) |
| 71 | |
| 72 | @skipIfWindows # stdio manipulation unsupported on Windows |
| 73 | @python_api_test |
| 74 | def test_stdout_stderr_redirection(self): |
| 75 | """Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT and STDERR without redirecting STDIN.""" |
| 76 | self.build() |
| 77 | self.create_target() |
| 78 | self.redirect_stdout() |
| 79 | self.redirect_stderr() |
| 80 | self.run_process(True) |
| 81 | output = self.read_output_file_and_delete() |
| 82 | error = self.read_error_file_and_delete() |
| 83 | self.check_process_output(output, error) |
| 84 | |
Vince Harron | 4a8abd3 | 2015-02-13 19:15:24 +0000 | [diff] [blame] | 85 | # target_file - path on local file system or remote file system if running remote |
| 86 | # local_file - path on local system |
| 87 | def read_file_and_delete(self, target_file, local_file): |
| 88 | if lldb.remote_platform: |
| 89 | self.runCmd('platform get-file "{remote}" "{local}"'.format( |
| 90 | remote=target_file, local=local_file)) |
| 91 | |
| 92 | self.assertTrue(os.path.exists(local_file), 'Make sure "{local}" file exists'.format(local=local_file)) |
| 93 | f = open(local_file, 'r') |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 94 | contents = f.read() |
| 95 | f.close() |
Vince Harron | 4a8abd3 | 2015-02-13 19:15:24 +0000 | [diff] [blame] | 96 | |
| 97 | #TODO: add 'platform delete-file' file command |
| 98 | #if lldb.remote_platform: |
| 99 | # self.runCmd('platform delete-file "{remote}"'.format(remote=target_file)) |
| 100 | os.unlink(local_file) |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 101 | return contents |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 102 | |
Vince Harron | 4a8abd3 | 2015-02-13 19:15:24 +0000 | [diff] [blame] | 103 | def read_output_file_and_delete(self): |
| 104 | return self.read_file_and_delete(self.output_file, self.local_output_file) |
| 105 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 106 | def read_error_file_and_delete(self): |
Vince Harron | 4a8abd3 | 2015-02-13 19:15:24 +0000 | [diff] [blame] | 107 | return self.read_file_and_delete(self.error_file, self.local_error_file) |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 108 | |
| 109 | def create_target(self): |
| 110 | '''Create the target and launch info that will be used by all tests''' |
| 111 | self.target = self.dbg.CreateTarget(self.exe) |
| 112 | self.launch_info = lldb.SBLaunchInfo([self.exe]) |
| 113 | self.launch_info.SetWorkingDirectory(self.get_process_working_directory()) |
| 114 | |
| 115 | def redirect_stdin(self): |
| 116 | '''Redirect STDIN (file descriptor 0) to use our input.txt file |
| 117 | |
| 118 | Make the input.txt file to use when redirecting STDIN, setup a cleanup action |
| 119 | to delete the input.txt at the end of the test in case exceptions are thrown, |
| 120 | and redirect STDIN in the launch info.''' |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 121 | f = open(self.local_input_file, 'w') |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 122 | for line in self.lines: |
| 123 | f.write(line + "\n") |
| 124 | f.close() |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 125 | |
| 126 | if lldb.remote_platform: |
| 127 | self.runCmd('platform put-file "{local}" "{remote}"'.format( |
| 128 | local=self.local_input_file, remote=self.input_file)) |
| 129 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 130 | # This is the function to remove the custom formats in order to have a |
| 131 | # clean slate for the next test case. |
| 132 | def cleanup(): |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 133 | os.unlink(self.local_input_file) |
Vince Harron | 4a8abd3 | 2015-02-13 19:15:24 +0000 | [diff] [blame] | 134 | #TODO: add 'platform delete-file' file command |
| 135 | #if lldb.remote_platform: |
| 136 | # self.runCmd('platform delete-file "{remote}"'.format(remote=self.input_file)) |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 137 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 138 | # Execute the cleanup function during test case tear down. |
| 139 | self.addTearDownHook(cleanup) |
| 140 | self.launch_info.AddOpenFileAction(0, self.input_file, True, False); |
| 141 | |
| 142 | def redirect_stdout(self): |
| 143 | '''Redirect STDOUT (file descriptor 1) to use our output.txt file''' |
| 144 | self.launch_info.AddOpenFileAction(1, self.output_file, False, True); |
| 145 | |
| 146 | def redirect_stderr(self): |
| 147 | '''Redirect STDERR (file descriptor 2) to use our error.txt file''' |
| 148 | self.launch_info.AddOpenFileAction(2, self.error_file, False, True); |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 149 | |
| 150 | def run_process(self, put_stdin): |
| 151 | '''Run the process to completion and optionally put lines to STDIN via the API if "put_stdin" is True''' |
| 152 | # Set the breakpoints |
| 153 | self.breakpoint = self.target.BreakpointCreateBySourceRegex('Set breakpoint here', lldb.SBFileSpec("main.c")) |
| 154 | self.assertTrue(self.breakpoint.GetNumLocations() > 0, VALID_BREAKPOINT) |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 155 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 156 | # Launch the process, and do not stop at the entry point. |
| 157 | error = lldb.SBError() |
| 158 | # This should launch the process and it should exit by the time we get back |
| 159 | # because we have synchronous mode enabled |
| 160 | self.process = self.target.Launch (self.launch_info, error) |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 161 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 162 | self.assertTrue(error.Success(), "Make sure process launched successfully") |
| 163 | self.assertTrue(self.process, PROCESS_IS_VALID) |
Daniel Malea | 249287a | 2013-02-19 16:08:57 +0000 | [diff] [blame] | 164 | |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 165 | if self.TraceOn(): |
| 166 | print "process launched." |
| 167 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 168 | # Frame #0 should be at our breakpoint. |
| 169 | threads = lldbutil.get_threads_stopped_at_breakpoint (self.process, self.breakpoint) |
| 170 | |
| 171 | self.assertTrue(len(threads) == 1) |
| 172 | self.thread = threads[0] |
| 173 | self.frame = self.thread.frames[0] |
| 174 | self.assertTrue(self.frame, "Frame 0 is valid.") |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 175 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 176 | if self.TraceOn(): |
| 177 | print "process stopped at breakpoint, sending STDIN via LLDB API." |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 178 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 179 | # Write data to stdin via the public API if we were asked to |
| 180 | if put_stdin: |
| 181 | for line in self.lines: |
| 182 | self.process.PutSTDIN(line + "\n") |
| 183 | |
| 184 | # Let process continue so it will exit |
| 185 | self.process.Continue() |
| 186 | state = self.process.GetState() |
| 187 | self.assertTrue(state == lldb.eStateExited, PROCESS_IS_VALID) |
| 188 | |
| 189 | def check_process_output (self, output, error): |
Johnny Chen | 5886fb5 | 2012-01-12 00:29:46 +0000 | [diff] [blame] | 190 | # Since we launched the process without specifying stdin/out/err, |
| 191 | # a pseudo terminal is used for stdout/err, and we are satisfied |
| 192 | # once "input line=>1" appears in stdout. |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 193 | # See also main.c. |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 194 | if self.TraceOn(): |
| 195 | print "output = '%s'" % output |
| 196 | print "error = '%s'" % error |
| 197 | |
| 198 | for line in self.lines: |
| 199 | check_line = 'input line to stdout: %s' % (line) |
| 200 | self.assertTrue(check_line in output, "verify stdout line shows up in STDOUT") |
| 201 | for line in self.lines: |
| 202 | check_line = 'input line to stderr: %s' % (line) |
| 203 | self.assertTrue(check_line in error, "verify stderr line shows up in STDERR") |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 204 | |
| 205 | if __name__ == '__main__': |
| 206 | import atexit |
| 207 | lldb.SBDebugger.Initialize() |
| 208 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 209 | unittest2.main() |