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 * |
| 7 | |
| 8 | class ProcessIOTestCase(TestBase): |
| 9 | |
Greg Clayton | 4570d3e | 2013-12-10 23:19:29 +0000 | [diff] [blame] | 10 | mydir = TestBase.compute_mydir(__file__) |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 11 | |
| 12 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| 13 | @python_api_test |
Johnny Chen | 24086bc | 2012-04-06 19:54:10 +0000 | [diff] [blame] | 14 | @dsym_test |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 15 | def test_put_stdin_with_dsym(self): |
| 16 | """Exercise SBProcess.PutSTDIN().""" |
| 17 | self.buildDsym() |
| 18 | self.put_stdin() |
| 19 | |
| 20 | @python_api_test |
Johnny Chen | 24086bc | 2012-04-06 19:54:10 +0000 | [diff] [blame] | 21 | @dwarf_test |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 22 | def test_put_stdin_with_dwarf(self): |
| 23 | """Exercise SBProcess.PutSTDIN().""" |
| 24 | self.buildDwarf() |
| 25 | self.put_stdin() |
| 26 | |
| 27 | def setUp(self): |
| 28 | # Call super's setUp(). |
| 29 | TestBase.setUp(self) |
| 30 | # Get the full path to our executable to be debugged. |
| 31 | self.exe = os.path.join(os.getcwd(), "process_io") |
| 32 | |
| 33 | def put_stdin(self): |
| 34 | """Launch a process and use SBProcess.PutSTDIN() to write data to it.""" |
| 35 | |
| 36 | target = self.dbg.CreateTarget(self.exe) |
| 37 | |
Daniel Malea | 249287a | 2013-02-19 16:08:57 +0000 | [diff] [blame] | 38 | # Perform synchronous interaction with the debugger. |
| 39 | self.setAsync(True) |
| 40 | |
Greg Clayton | c694751 | 2013-12-13 19:18:59 +0000 | [diff] [blame] | 41 | process = target.LaunchSimple (None, None, self.get_process_working_directory()) |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 42 | if self.TraceOn(): |
| 43 | print "process launched." |
| 44 | |
| 45 | self.assertTrue(process, PROCESS_IS_VALID) |
| 46 | |
| 47 | process.PutSTDIN("Line 1 Entered.\n") |
| 48 | process.PutSTDIN("Line 2 Entered.\n") |
| 49 | process.PutSTDIN("Line 3 Entered.\n") |
| 50 | |
| 51 | for i in range(5): |
| 52 | output = process.GetSTDOUT(500) |
| 53 | error = process.GetSTDERR(500) |
| 54 | if self.TraceOn(): |
| 55 | print "output->|%s|" % output |
Johnny Chen | 5886fb5 | 2012-01-12 00:29:46 +0000 | [diff] [blame] | 56 | # Since we launched the process without specifying stdin/out/err, |
| 57 | # a pseudo terminal is used for stdout/err, and we are satisfied |
| 58 | # once "input line=>1" appears in stdout. |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 59 | # See also main.c. |
Johnny Chen | 49cb85d | 2011-11-28 21:39:07 +0000 | [diff] [blame] | 60 | if "input line=>1" in output: |
| 61 | return |
| 62 | time.sleep(5) |
| 63 | |
| 64 | self.fail("Expected output form launched process did not appear?") |
| 65 | |
| 66 | if __name__ == '__main__': |
| 67 | import atexit |
| 68 | lldb.SBDebugger.Initialize() |
| 69 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 70 | unittest2.main() |