xref: /llvm-project/lldb/test/API/commands/platform/basic/TestPlatformPython.py (revision 4cc8f2a017c76af25234afc7c380550e9c93135c)
1"""
2Test the lldb platform Python API.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class PlatformPythonTestCase(TestBase):
14
15    @add_test_categories(['pyapi'])
16    @no_debug_info_test
17    def test_platform_list(self):
18        """Test SBDebugger::GetNumPlatforms() & GetPlatformAtIndex() API"""
19        # Verify the host platform is present by default.
20        initial_num_platforms = self.dbg.GetNumPlatforms()
21        self.assertGreater(initial_num_platforms, 0)
22        host_platform = self.dbg.GetPlatformAtIndex(0)
23        self.assertTrue(host_platform.IsValid() and
24                        host_platform.GetName() == 'host',
25                        'The host platform is present')
26        # Select another platform and verify that the platform is added to
27        # the platform list.
28        platform_idx = self.dbg.GetNumAvailablePlatforms() - 1
29        if platform_idx < 1:
30            self.fail('No platforms other than host are available')
31        platform_data = self.dbg.GetAvailablePlatformInfoAtIndex(platform_idx)
32        platform_name = platform_data.GetValueForKey('name').GetStringValue(100)
33        self.assertNotEqual(platform_name, 'host')
34        self.dbg.SetCurrentPlatform(platform_name)
35        selected_platform = self.dbg.GetSelectedPlatform()
36        self.assertTrue(selected_platform.IsValid())
37        self.assertEqual(selected_platform.GetName(), platform_name)
38        self.assertEqual(self.dbg.GetNumPlatforms(), initial_num_platforms + 1)
39        platform_found = False
40        for platform_idx in range(self.dbg.GetNumPlatforms()):
41            platform = self.dbg.GetPlatformAtIndex(platform_idx)
42            if platform.GetName() == platform_name:
43                platform_found = True
44                break
45        self.assertTrue(platform_found)
46
47    @add_test_categories(['pyapi'])
48    @no_debug_info_test
49    def test_host_is_connected(self):
50        # We've already tested that this one IS the host platform.
51        host_platform = self.dbg.GetPlatformAtIndex(0)
52        self.assertTrue(host_platform.IsConnected(), "The host platform is always connected")
53
54
55    @add_test_categories(['pyapi'])
56    @no_debug_info_test
57    def test_available_platform_list(self):
58        """Test SBDebugger::GetNumAvailablePlatforms() and GetAvailablePlatformInfoAtIndex() API"""
59        num_platforms = self.dbg.GetNumAvailablePlatforms()
60        self.assertGreater(
61            num_platforms, 0,
62            'There should be at least one platform available')
63
64        for i in range(num_platforms):
65            platform_data = self.dbg.GetAvailablePlatformInfoAtIndex(i)
66            name_data = platform_data.GetValueForKey('name')
67            desc_data = platform_data.GetValueForKey('description')
68            self.assertTrue(
69                name_data and name_data.IsValid(),
70                'Platform has a name')
71            self.assertEqual(
72                name_data.GetType(), lldb.eStructuredDataTypeString,
73                'Platform name is a string')
74            self.assertTrue(
75                desc_data and desc_data.IsValid(),
76                'Platform has a description')
77            self.assertEqual(
78                desc_data.GetType(), lldb.eStructuredDataTypeString,
79                'Platform description is a string')
80
81    @add_test_categories(['pyapi'])
82    @no_debug_info_test
83    @skipIfRemote
84    def test_shell_interpreter(self):
85        """ Test a shell with a custom interpreter """
86        platform = self.dbg.GetSelectedPlatform()
87        self.assertTrue(platform.IsValid())
88
89        sh_cmd = lldb.SBPlatformShellCommand('/bin/zsh', 'echo $0')
90        self.assertIn('/bin/zsh', sh_cmd.GetShell())
91        self.assertIn('echo $0', sh_cmd.GetCommand())
92
93        self.build()
94        sh_cmd.SetShell(self.getBuildArtifact('a.out'))
95        err = platform.Run(sh_cmd)
96        self.assertSuccess(err)
97        self.assertIn("SUCCESS", sh_cmd.GetOutput())
98