File: test_gdb.py

package info (click to toggle)
python-ptrace 0.9.9-0.2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 788 kB
  • sloc: python: 10,167; ansic: 263; makefile: 164
file content (79 lines) | stat: -rwxr-xr-x 2,558 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python
import os
import re
import subprocess
import sys
import unittest

GDB = os.path.normpath(os.path.join(os.path.dirname(__file__), '..', 'gdb.py'))


class TestGdb(unittest.TestCase):

    def run_command(self, command):
        if isinstance(command, str):
            command = command.encode('ascii')
        command = command + b'\n'
        args = [sys.executable, GDB, '--', sys.executable, '-c', 'pass']
        proc = subprocess.Popen(args,
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT)
        stdout, _ = proc.communicate(command)
        exitcode = proc.wait()
        self.assertEqual(exitcode, 0)
        if stdout.startswith(b'(gdb) '):
            stdout = stdout[6:]
        pos = stdout.rfind(b'(gdb) ')
        if pos:
            stdout = stdout[:pos]
        stdout = stdout.rstrip()
        if b'Traceback' in stdout:
            self.fail('Traceback in output: %r' % stdout)
        return stdout

    def check_stdout(self, pattern, stdout):
        self.assertTrue(re.search(pattern, stdout, re.MULTILINE),
                        (pattern, stdout))

    def test_proc(self):
        stdout = self.run_command('proc')
        for pattern in (
            b'^Process ID: [0-9]+',
            b'^Process state: ',
            b'^Process environment: ',
            b'^User identifier: [0-9]+',
            b'^Group identifier: [0-9]+',
        ):
            self.assertTrue(re.search(pattern, stdout, re.MULTILINE),
                            (pattern, stdout))

    def test_print(self):
        stdout = self.run_command('print 1+2')
        self.check_stdout(b'^Decimal: 3\n', stdout)

    def test_where(self):
        stdout = self.run_command('where')
        self.check_stdout(b'^CODE:', stdout)

    def test_regs(self):
        # Just check that the command doesn't raise an exception
        self.run_command('regs')

    def test_backtrace(self):
        # Just check that the command doesn't raise an exception
        self.run_command('backtrace')

    def test_maps(self):
        stdout = self.run_command('maps')
        self.check_stdout(b'^MAPS: ', stdout)

    def test_dbginfo(self):
        stdout = self.run_command('dbginfo')
        self.check_stdout(b'^Debugger process ID: [0-9]+', stdout)
        self.check_stdout(b'^python-ptrace version [0-9]+\\.[0-9]+', stdout)
        self.check_stdout(b'^Website: ', stdout)


if __name__ == "__main__":
    unittest.main()