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