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