def test_string_inout(self): inf = io.StringIO("help help\n") outf = io.StringIO() status = self.debugger.SetOutputFile(lldb.SBFile(outf)) self.assertTrue(status.Success()) status = self.debugger.SetInputFile(lldb.SBFile(inf)) self.assertTrue(status.Success()) opts = lldb.SBCommandInterpreterRunOptions() self.debugger.RunCommandInterpreter(True, False, opts, 0, False, False) self.debugger.GetOutputFile().Flush() output = outf.getvalue() self.assertIn('Show a list of all debugger commands', output)
def test_exceptions(self): self.assertRaises(Exception, lldb.SBFile, None) self.assertRaises(Exception, lldb.SBFile, "ham sandwich") self.assertRaises(OhNoe, lldb.SBFile, ReallyBadIO()) error, n = lldb.SBFile(BadIO()).Write(b"FOO") self.assertEqual(n, 0) self.assertTrue(error.Fail()) self.assertIn('OH NOE', error.GetCString()) error, n = lldb.SBFile(BadIO()).Read(bytearray(100)) self.assertEqual(n, 0) self.assertTrue(error.Fail()) self.assertIn('OH NOE', error.GetCString())
def test_identity(self): f = io.StringIO() sbf = lldb.SBFile(f) self.assertTrue(f is sbf.GetFile()) sbf.Close() self.assertTrue(f.closed) f = io.StringIO() sbf = lldb.SBFile.Create(f, borrow=True) self.assertTrue(f is sbf.GetFile()) sbf.Close() self.assertFalse(f.closed) with open(self.out_filename, 'w') as f: sbf = lldb.SBFile(f) self.assertTrue(f is sbf.GetFile()) sbf.Close() self.assertTrue(f.closed) with open(self.out_filename, 'w') as f: sbf = lldb.SBFile.Create(f, borrow=True) self.assertFalse(f is sbf.GetFile()) sbf.Write(b"foobar\n") self.assertEqual(f.fileno(), sbf.GetFile().fileno()) sbf.Close() self.assertFalse(f.closed) with open(self.out_filename, 'r') as f: self.assertEqual("foobar", f.read().strip()) with open(self.out_filename, 'wb') as f: sbf = lldb.SBFile.Create(f, borrow=True, force_io_methods=True) self.assertTrue(f is sbf.GetFile()) sbf.Write(b"foobar\n") self.assertEqual(f.fileno(), sbf.GetFile().fileno()) sbf.Close() self.assertFalse(f.closed) with open(self.out_filename, 'r') as f: self.assertEqual("foobar", f.read().strip()) with open(self.out_filename, 'wb') as f: sbf = lldb.SBFile.Create(f, force_io_methods=True) self.assertTrue(f is sbf.GetFile()) sbf.Write(b"foobar\n") self.assertEqual(f.fileno(), sbf.GetFile().fileno()) sbf.Close() self.assertTrue(f.closed) with open(self.out_filename, 'r') as f: self.assertEqual("foobar", f.read().strip())
def test_bytes_inout(self): inf = io.BytesIO(b"help help\nhelp b\n") outf = io.BytesIO() status = self.dbg.SetOutputFile(lldb.SBFile(outf)) self.assertSuccess(status) status = self.dbg.SetInputFile(lldb.SBFile(inf)) self.assertSuccess(status) opts = lldb.SBCommandInterpreterRunOptions() self.dbg.RunCommandInterpreter(True, False, opts, 0, False, False) self.dbg.GetOutputFile().Flush() output = outf.getvalue() self.assertIn(b'Show a list of all debugger commands', output) self.assertIn(b'Set a breakpoint', output)
def test_binary_inout(self): with open(self.in_filename, 'w') as f: f.write("help help\n") with open(self.out_filename, 'wb') as outf, \ open(self.in_filename, 'rb') as inf: status = self.dbg.SetOutputFile(lldb.SBFile(outf)) self.assertSuccess(status) status = self.dbg.SetInputFile(lldb.SBFile(inf)) self.assertSuccess(status) opts = lldb.SBCommandInterpreterRunOptions() self.dbg.RunCommandInterpreter(True, False, opts, 0, False, False) self.dbg.GetOutputFile().Flush() with open(self.out_filename, 'r') as f: output = f.read() self.assertIn('Show a list of all debugger commands', output)
def test_exceptions(self): self.assertRaises(TypeError, lldb.SBFile, None) self.assertRaises(TypeError, lldb.SBFile, "ham sandwich") if sys.version_info[0] < 3: self.assertRaises(TypeError, lldb.SBFile, ReallyBadIO()) else: self.assertRaises(OhNoe, lldb.SBFile, ReallyBadIO()) error, n = lldb.SBFile(BadIO()).Write(b"FOO") self.assertEqual(n, 0) self.assertTrue(error.Fail()) self.assertEqual(error.GetCString(), "OhNoe('OH NOE')") error, n = lldb.SBFile(BadIO()).Read(bytearray(100)) self.assertEqual(n, 0) self.assertTrue(error.Fail()) self.assertEqual(error.GetCString(), "OhNoe('OH NOE')")
def test_sbfile_type_errors(self): sbf = lldb.SBFile() self.assertRaises(Exception, sbf.Write, None) self.assertRaises(Exception, sbf.Read, None) self.assertRaises(Exception, sbf.Read, b'this bytes is not mutable') self.assertRaises(Exception, sbf.Write, u"ham sandwich") self.assertRaises(Exception, sbf.Read, u"ham sandwich")
def test_sbfile_read_string(self): f = io.StringIO('zork') sbf = lldb.SBFile(f) buf = bytearray(100) e, n = sbf.Read(buf) self.assertTrue(e.Success()) self.assertEqual(buf[:n], b'zork')
def test_expr(self): with open(self.getBuildArtifact("module.modulemap"), "w") as f: f.write(""" module Foo { header "f.h" } """) with open(self.getBuildArtifact("f.h"), "w") as f: f.write(""" typedef int my_int; void f() {} """) mod_cache = self.getBuildArtifact("private-module-cache") if os.path.isdir(mod_cache): shutil.rmtree(mod_cache) self.build() self.assertTrue(os.path.isdir(mod_cache), "module cache exists") logfile = self.getBuildArtifact("host.log") with open(logfile, 'w') as f: sbf = lldb.SBFile(f.fileno(), 'w', False) status = self.dbg.SetErrorFile(sbf) self.assertSuccess(status) target, _, _, _ = lldbutil.run_to_source_breakpoint( self, "break here", lldb.SBFileSpec("main.m")) target.GetModuleAtIndex(0).FindTypes('my_int') with open(logfile, 'r') as f: for line in f: if "hash mismatch" in line and "Foo" in line: found = True self.assertTrue(found)
def test_flush(self): flushed = MutableBool(False) closed = MutableBool(False) f = FlushTestIO(flushed, closed) self.assertFalse(flushed) self.assertFalse(closed) sbf = lldb.SBFile(f) self.assertFalse(flushed) self.assertFalse(closed) sbf = None self.assertFalse(flushed) self.assertTrue(closed) self.assertTrue(f.closed) flushed = MutableBool(False) closed = MutableBool(False) f = FlushTestIO(flushed, closed) self.assertFalse(flushed) self.assertFalse(closed) sbf = lldb.SBFile.Create(f, borrow=True) self.assertFalse(flushed) self.assertFalse(closed) sbf = None self.assertTrue(flushed) self.assertFalse(closed) self.assertFalse(f.closed)
def test_sbfile_read_bytes(self): f = io.BytesIO(b'zork') sbf = lldb.SBFile(f) buf = bytearray(100) e, n = sbf.Read(buf) self.assertSuccess(e) self.assertEqual(buf[:n], b'zork')
def test_help(self): with open(self.out_filename, 'w') as f: status = self.dbg.SetOutputFile(lldb.SBFile(f)) self.assertSuccess(status) self.handleCmd("help help", check=False, collect_result=False) with open(self.out_filename, 'r') as f: self.assertIn('Show a list of all debugger commands', f.read())
def test_exceptions_logged(self): messages = list() self.dbg.SetLoggingCallback(messages.append) self.handleCmd('log enable lldb script') self.dbg.SetOutputFile(lldb.SBFile(BadIO())) self.handleCmd('script 1+1') self.assertTrue(any('OH NOE' in msg for msg in messages))
def test_sbfile_out(self): with open(self.out_filename, 'w') as f: sbf = lldb.SBFile(f) status = self.dbg.SetOutputFile(sbf) self.assertSuccess(status) self.handleCmd('script 2+2') with open(self.out_filename, 'r') as f: self.assertEqual(f.read().strip(), '4')
def test_sbfile_read_string_one_byte(self): f = io.StringIO('z') sbf = lldb.SBFile(f) buf = bytearray(1) e, n = sbf.Read(buf) self.assertTrue(e.Fail()) self.assertEqual(n, 0) self.assertEqual(e.GetCString(), "can't read less than 6 bytes from a utf8 text stream")
def test_fileno_help(self): with open(self.out_filename, 'w') as f: sbf = lldb.SBFile(f.fileno(), "w", False) status = self.dbg.SetOutputFile(sbf) self.assertSuccess(status) self.handleCmd("help help", collect_result=False, check=False) with open(self.out_filename, 'r') as f: self.assertTrue(re.search(r'Show a list of all debugger commands', f.read()))
def test_sbfile_error(self): with open(self.out_filename, 'w') as f: sbf = lldb.SBFile(f) status = self.dbg.SetErrorFile(sbf) self.assertSuccess(status) self.handleCmd('lolwut', check=False, collect_result=False) with open(self.out_filename, 'r') as f: errors = f.read() self.assertTrue(re.search(r'error:.*lolwut', errors))
def test_sbfile_write_bytes(self): f = io.BytesIO() sbf = lldb.SBFile(f) e, n = sbf.Write(b'FOO') self.assertEqual(f.getvalue().strip(), b"FOO") self.assertSuccess(e) self.assertEqual(n, 3) sbf.Close() self.assertTrue(f.closed)
def test_sbfile_write_string(self): f = io.StringIO() sbf = lldb.SBFile(f) e, n = sbf.Write(b'FOO') self.assertEqual(f.getvalue().strip(), "FOO") self.assertTrue(e.Success()) self.assertEqual(n, 3) sbf.Close() self.assertTrue(f.closed)
def test_fileno_out(self): with open(self.out_filename, 'w') as f: sbf = lldb.SBFile(f.fileno(), "w", False) status = self.debugger.SetOutputFile(sbf) self.assertTrue(status.Success()) self.handleCmd('script 1+2') self.debugger.GetOutputFile().Write(b'quux') with open(self.out_filename, 'r') as f: self.assertEqual(readStrippedLines(f), ['3', 'quux'])
def test_sbfile_read_fileno(self): with open(self.out_filename, 'w') as f: f.write('FOO') with open(self.out_filename, 'r') as f: sbf = lldb.SBFile(f.fileno(), "r", False) self.assertTrue(sbf.IsValid()) buffer = bytearray(100) e, n = sbf.Read(buffer) self.assertSuccess(e) self.assertEqual(buffer[:n], b'FOO')
def test_sbfile_write(self): with open(self.out_filename, 'w') as f: sbf = lldb.SBFile(f) e, n = sbf.Write(b'FOO\n') self.assertSuccess(e) self.assertEqual(n, 4) sbf.Close() self.assertTrue(f.closed) with open(self.out_filename, 'r') as f: self.assertEqual(f.read().strip(), 'FOO')
def test_fileno_inout(self): with open(self.in_filename, 'w') as f: f.write("help help\n") with open(self.out_filename, 'w') as outf, open(self.in_filename, 'r') as inf: outsbf = lldb.SBFile(outf.fileno(), "w", False) status = self.dbg.SetOutputFile(outsbf) self.assertSuccess(status) insbf = lldb.SBFile(inf.fileno(), "r", False) status = self.dbg.SetInputFile(insbf) self.assertSuccess(status) opts = lldb.SBCommandInterpreterRunOptions() self.dbg.RunCommandInterpreter(True, False, opts, 0, False, False) self.dbg.GetOutputFile().Flush() with open(self.out_filename, 'r') as f: self.assertTrue(re.search(r'Show a list of all debugger commands', f.read()))
def test_sbfile_write_fileno(self): with open(self.out_filename, 'w') as f: sbf = lldb.SBFile(f.fileno(), "w", False) self.assertTrue(sbf.IsValid()) e, n = sbf.Write(b'FOO\nBAR') self.assertSuccess(e) self.assertEqual(n, 7) sbf.Close() self.assertFalse(sbf.IsValid()) with open(self.out_filename, 'r') as f: self.assertEqual(readStrippedLines(f), ['FOO', 'BAR'])
def test_SBFile(self): sbf = lldb.SBFile() self.assertFalse(sbf.IsValid()) self.assertFalse(bool(sbf)) e, n = sbf.Write(b'foo') self.assertTrue(e.Fail()) self.assertEqual(n, 0) buffer = bytearray(100) e, n = sbf.Read(buffer) self.assertEqual(n, 0) self.assertTrue(e.Fail())
def test_immediate_sbfile_string(self): f = io.StringIO() ret = lldb.SBCommandReturnObject() ret.SetImmediateOutputFile(lldb.SBFile(f)) interpreter = self.dbg.GetCommandInterpreter() interpreter.HandleCommand("help help", ret) output = f.getvalue() ret = None # call destructor and flush streams # sbfile default constructor doesn't borrow the file self.assertTrue(f.closed) self.assertTrue(re.search(r'Show a list of all debugger commands', output))
def test_sbfile_read(self): with open(self.out_filename, 'w') as f: f.write('foo') with open(self.out_filename, 'r') as f: sbf = lldb.SBFile(f) buf = bytearray(100) e, n = sbf.Read(buf) self.assertSuccess(e) self.assertEqual(n, 3) self.assertEqual(buf[:n], b'foo') sbf.Close() self.assertTrue(f.closed)
def test_set_sbstream(self): with open(self.out_filename, 'w') as outf: outsbf = lldb.SBFile(outf.fileno(), "w", False) status = self.dbg.SetOutputFile(outsbf) self.assertSuccess(status) self.dbg.SetInputString("help apropos\nhelp help\n") opts = lldb.SBCommandInterpreterRunOptions() self.dbg.RunCommandInterpreter(True, False, opts, 0, False, False) self.dbg.GetOutputFile().Flush() with open(self.out_filename, 'r') as f: output = f.read() self.assertIn('Show a list of all debugger commands', output) self.assertIn('List debugger commands related to a word', output)
def test_fileno_error(self): with open(self.out_filename, 'w') as f: sbf = lldb.SBFile(f.fileno(), 'w', False) status = self.dbg.SetErrorFile(sbf) self.assertTrue(status.Success()) self.handleCmd('lolwut', check=False, collect_result=False) self.dbg.GetErrorFile().Write(b'\nzork\n') with open(self.out_filename, 'r') as f: errors = f.read() self.assertTrue(re.search(r'error:.*lolwut', errors)) self.assertTrue(re.search(r'zork', errors))
def test_fileno_flush(self): with open(self.out_filename, 'w') as f: f.write("foo") sbf = lldb.SBFile(f) sbf.Write(b'bar') sbf = None self.assertTrue(f.closed) with open(self.out_filename, 'r') as f: self.assertEqual(f.read(), 'foobar') with open(self.out_filename, 'w+') as f: f.write("foo") sbf = lldb.SBFile.Create(f, borrow=True) sbf.Write(b'bar') sbf = None self.assertFalse(f.closed) f.seek(0) self.assertEqual(f.read(), 'foobar')