blob: 2dc3f40ac9be44f9e59fce4c909b1b53ab63e136 [file] [log] [blame]
Johnny Chen49cb85d2011-11-28 21:39:07 +00001"""Test Python APIs for process IO."""
2
Zachary Turner35d017f2015-10-23 17:04:29 +00003from __future__ import print_function
4
Zachary Turner77db4a82015-10-22 20:06:20 +00005import lldb_shared
6
Johnny Chen49cb85d2011-11-28 21:39:07 +00007import os, sys, time
Johnny Chen49cb85d2011-11-28 21:39:07 +00008import lldb
9from lldbtest import *
Greg Claytonb8e9b8b2014-10-14 20:18:05 +000010import lldbutil
Johnny Chen49cb85d2011-11-28 21:39:07 +000011
12class ProcessIOTestCase(TestBase):
13
Greg Clayton4570d3e2013-12-10 23:19:29 +000014 mydir = TestBase.compute_mydir(__file__)
Johnny Chen49cb85d2011-11-28 21:39:07 +000015
Johnny Chen49cb85d2011-11-28 21:39:07 +000016 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 Harrondf3f00f2015-02-10 21:09:04 +000021 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 Claytonb8e9b8b2014-10-14 20:18:05 +000028 self.lines = ["Line 1", "Line 2", "Line 3"]
Vince Harron4a8abd32015-02-13 19:15:24 +000029
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000030 @skipIfWindows # stdio manipulation unsupported on Windows
Pavel Labathdc8b2d32015-10-26 09:28:32 +000031 @add_test_categories(['pyapi'])
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000032 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 Labathdc8b2d32015-10-26 09:28:32 +000041 @add_test_categories(['pyapi'])
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000042 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 Labathdc8b2d32015-10-26 09:28:32 +000052 @add_test_categories(['pyapi'])
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000053 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 Labathdc8b2d32015-10-26 09:28:32 +000064 @add_test_categories(['pyapi'])
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000065 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 Labathdc8b2d32015-10-26 09:28:32 +000076 @add_test_categories(['pyapi'])
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000077 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 Harron4a8abd32015-02-13 19:15:24 +000088 # 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 Claytonb8e9b8b2014-10-14 20:18:05 +000097 contents = f.read()
98 f.close()
Vince Harron4a8abd32015-02-13 19:15:24 +000099
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 Claytonb8e9b8b2014-10-14 20:18:05 +0000104 return contents
Johnny Chen49cb85d2011-11-28 21:39:07 +0000105
Vince Harron4a8abd32015-02-13 19:15:24 +0000106 def read_output_file_and_delete(self):
107 return self.read_file_and_delete(self.output_file, self.local_output_file)
108
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000109 def read_error_file_and_delete(self):
Vince Harron4a8abd32015-02-13 19:15:24 +0000110 return self.read_file_and_delete(self.error_file, self.local_error_file)
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000111
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 Harrondf3f00f2015-02-10 21:09:04 +0000124 f = open(self.local_input_file, 'w')
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000125 for line in self.lines:
126 f.write(line + "\n")
127 f.close()
Vince Harrondf3f00f2015-02-10 21:09:04 +0000128
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 Claytonb8e9b8b2014-10-14 20:18:05 +0000133 # 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 Harrondf3f00f2015-02-10 21:09:04 +0000136 os.unlink(self.local_input_file)
Vince Harron4a8abd32015-02-13 19:15:24 +0000137 #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 Harrondf3f00f2015-02-10 21:09:04 +0000140
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000141 # 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 Claytonb8e9b8b2014-10-14 20:18:05 +0000152
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 Chen49cb85d2011-11-28 21:39:07 +0000158
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000159 # 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 Chen49cb85d2011-11-28 21:39:07 +0000164
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000165 self.assertTrue(error.Success(), "Make sure process launched successfully")
166 self.assertTrue(self.process, PROCESS_IS_VALID)
Daniel Malea249287a2013-02-19 16:08:57 +0000167
Johnny Chen49cb85d2011-11-28 21:39:07 +0000168 if self.TraceOn():
Zachary Turner35d017f2015-10-23 17:04:29 +0000169 print("process launched.")
Johnny Chen49cb85d2011-11-28 21:39:07 +0000170
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000171 # 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 Chen49cb85d2011-11-28 21:39:07 +0000178
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000179 if self.TraceOn():
Zachary Turner35d017f2015-10-23 17:04:29 +0000180 print("process stopped at breakpoint, sending STDIN via LLDB API.")
Johnny Chen49cb85d2011-11-28 21:39:07 +0000181
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000182 # 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 Chen5886fb52012-01-12 00:29:46 +0000193 # 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 Chen49cb85d2011-11-28 21:39:07 +0000196 # See also main.c.
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000197 if self.TraceOn():
Zachary Turner35d017f2015-10-23 17:04:29 +0000198 print("output = '%s'" % output)
199 print("error = '%s'" % error)
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000200
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")