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