xref: /xnu-12377.81.4/tools/lldbmacros/tests/lldb_tests/test_examples.py (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions##
2*043036a2SApple OSS Distributions# Copyright (c) 2023 Apple Inc. All rights reserved.
3*043036a2SApple OSS Distributions#
4*043036a2SApple OSS Distributions# @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*043036a2SApple OSS Distributions#
6*043036a2SApple OSS Distributions# This file contains Original Code and/or Modifications of Original Code
7*043036a2SApple OSS Distributions# as defined in and that are subject to the Apple Public Source License
8*043036a2SApple OSS Distributions# Version 2.0 (the 'License'). You may not use this file except in
9*043036a2SApple OSS Distributions# compliance with the License. The rights granted to you under the License
10*043036a2SApple OSS Distributions# may not be used to create, or enable the creation or redistribution of,
11*043036a2SApple OSS Distributions# unlawful or unlicensed copies of an Apple operating system, or to
12*043036a2SApple OSS Distributions# circumvent, violate, or enable the circumvention or violation of, any
13*043036a2SApple OSS Distributions# terms of an Apple operating system software license agreement.
14*043036a2SApple OSS Distributions#
15*043036a2SApple OSS Distributions# Please obtain a copy of the License at
16*043036a2SApple OSS Distributions# http://www.opensource.apple.com/apsl/ and read it before using this file.
17*043036a2SApple OSS Distributions#
18*043036a2SApple OSS Distributions# The Original Code and all software distributed under the License are
19*043036a2SApple OSS Distributions# distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*043036a2SApple OSS Distributions# EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*043036a2SApple OSS Distributions# INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*043036a2SApple OSS Distributions# FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*043036a2SApple OSS Distributions# Please see the License for the specific language governing rights and
24*043036a2SApple OSS Distributions# limitations under the License.
25*043036a2SApple OSS Distributions#
26*043036a2SApple OSS Distributions# @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*043036a2SApple OSS Distributions##
28*043036a2SApple OSS Distributions
29*043036a2SApple OSS Distributions# pylint: disable=invalid-name
30*043036a2SApple OSS Distributions
31*043036a2SApple OSS Distributions""" Unit test examples
32*043036a2SApple OSS Distributions
33*043036a2SApple OSS Distributions    This is not a real test suite. It only demonstrates various approaches developers
34*043036a2SApple OSS Distributions    can use to write a test.
35*043036a2SApple OSS Distributions"""
36*043036a2SApple OSS Distributions
37*043036a2SApple OSS Distributionsimport contextlib
38*043036a2SApple OSS Distributionsimport io
39*043036a2SApple OSS Distributionsimport unittest.mock
40*043036a2SApple OSS Distributionsfrom lldbtest.testcase import LLDBTestCase
41*043036a2SApple OSS Distributionsimport lldb
42*043036a2SApple OSS Distributions
43*043036a2SApple OSS Distributionsfrom lldbmock.utils import lookup_type
44*043036a2SApple OSS Distributions
45*043036a2SApple OSS Distributions# Import macro function to be tested.
46*043036a2SApple OSS Distributionsfrom process import ShowTask, P_LHASTASK, TF_HASPROC
47*043036a2SApple OSS Distributions
48*043036a2SApple OSS Distributions
49*043036a2SApple OSS Distributionsclass TestExamples(LLDBTestCase):
50*043036a2SApple OSS Distributions    """ Unit test examples. """
51*043036a2SApple OSS Distributions
52*043036a2SApple OSS Distributions    ROUNDED_UP_PROC_SIZE = 2048
53*043036a2SApple OSS Distributions
54*043036a2SApple OSS Distributions    # Mock global variable value (accessed by the macro being)
55*043036a2SApple OSS Distributions    @unittest.mock.patch('xnu.kern.globals.proc_struct_size', ROUNDED_UP_PROC_SIZE)
56*043036a2SApple OSS Distributions    def test_function(self):
57*043036a2SApple OSS Distributions        """ This test shows how to run complex function against a mock. """
58*043036a2SApple OSS Distributions
59*043036a2SApple OSS Distributions        self.reset_mocks()
60*043036a2SApple OSS Distributions
61*043036a2SApple OSS Distributions        PROC_ADDR = 0xffffffff90909090
62*043036a2SApple OSS Distributions        TASK_ADDR = PROC_ADDR + self.ROUNDED_UP_PROC_SIZE
63*043036a2SApple OSS Distributions        PROC_RO_ADDR = 0xffffff0040404040
64*043036a2SApple OSS Distributions
65*043036a2SApple OSS Distributions        # Create fake proc_t instance at 0xffffffff90909090
66*043036a2SApple OSS Distributions        proc = self.create_mock('proc', PROC_ADDR).fromDict({
67*043036a2SApple OSS Distributions            'p_pid': 12345,
68*043036a2SApple OSS Distributions            'p_lflag': P_LHASTASK,
69*043036a2SApple OSS Distributions            'p_comm': b'test-proc\0'
70*043036a2SApple OSS Distributions        })
71*043036a2SApple OSS Distributions
72*043036a2SApple OSS Distributions        # Create task which is expected to be placed + sizeof(proc)
73*043036a2SApple OSS Distributions        task = self.create_mock('task', TASK_ADDR).fromDict({
74*043036a2SApple OSS Distributions            'effective_policy': {
75*043036a2SApple OSS Distributions                'tep_sup_active': 0,
76*043036a2SApple OSS Distributions                'tep_darwinbg': 0,
77*043036a2SApple OSS Distributions                'tep_lowpri_cpu': 1
78*043036a2SApple OSS Distributions            },
79*043036a2SApple OSS Distributions            't_flags': TF_HASPROC
80*043036a2SApple OSS Distributions        })
81*043036a2SApple OSS Distributions
82*043036a2SApple OSS Distributions        # Created shared proc_ro reference from both task/proc
83*043036a2SApple OSS Distributions        self.create_mock('proc_ro', PROC_RO_ADDR)
84*043036a2SApple OSS Distributions        proc.p_proc_ro = PROC_RO_ADDR
85*043036a2SApple OSS Distributions        task.bsd_info_ro = PROC_RO_ADDR
86*043036a2SApple OSS Distributions
87*043036a2SApple OSS Distributions        # Capture stdout and check expected output
88*043036a2SApple OSS Distributions        stdout = io.StringIO()
89*043036a2SApple OSS Distributions        with contextlib.redirect_stdout(stdout):
90*043036a2SApple OSS Distributions            ShowTask([f'{TASK_ADDR:#x}'])
91*043036a2SApple OSS Distributions
92*043036a2SApple OSS Distributions        # Note: Not the best way of writing a unit test.
93*043036a2SApple OSS Distributions        expected = (
94*043036a2SApple OSS Distributions            'task                 vm_map               ipc_space            #acts flags'
95*043036a2SApple OSS Distributions            '    pid   process              io_policy  wq_state  command'
96*043036a2SApple OSS Distributions            f'                         \n{TASK_ADDR:#x}   0x0                  0x0    '
97*043036a2SApple OSS Distributions            f'                  0        12345   {PROC_ADDR:#x}           L  0  0  0'
98*043036a2SApple OSS Distributions            '   test-proc                       \n'
99*043036a2SApple OSS Distributions        )
100*043036a2SApple OSS Distributions        self.assertEqual(stdout.getvalue(), expected)
101*043036a2SApple OSS Distributions
102*043036a2SApple OSS Distributions    def test_command(self):
103*043036a2SApple OSS Distributions        """ Test a simple LLDB command from user's CLI.
104*043036a2SApple OSS Distributions
105*043036a2SApple OSS Distributions            Creates mock of a structure and prints out member by using LLDB
106*043036a2SApple OSS Distributions            expression.
107*043036a2SApple OSS Distributions        """
108*043036a2SApple OSS Distributions
109*043036a2SApple OSS Distributions        self.reset_mocks()
110*043036a2SApple OSS Distributions
111*043036a2SApple OSS Distributions        PROC_ADDR = 0xffffffff90909090
112*043036a2SApple OSS Distributions
113*043036a2SApple OSS Distributions        self.create_mock('proc', PROC_ADDR).fromDict({
114*043036a2SApple OSS Distributions            'p_pid': 12345,
115*043036a2SApple OSS Distributions            'p_lflag': P_LHASTASK,
116*043036a2SApple OSS Distributions            'p_comm': b'unit-test-proc\0'
117*043036a2SApple OSS Distributions        })
118*043036a2SApple OSS Distributions
119*043036a2SApple OSS Distributions        res = self.run_command(f'p/x ((proc_t){PROC_ADDR:#x})->p_comm')
120*043036a2SApple OSS Distributions        self.assertEqual(res.GetOutput(), '(command_t) "unit-test-proc"\n')
121*043036a2SApple OSS Distributions        self.assertTrue(res.Succeeded())
122*043036a2SApple OSS Distributions
123*043036a2SApple OSS Distributions    @unittest.skipIf(LLDBTestCase.kernel().startswith('mach.release'),
124*043036a2SApple OSS Distributions                     "Not available in RELEASE embedded")
125*043036a2SApple OSS Distributions    def test_sbapi(self):
126*043036a2SApple OSS Distributions        """ Test SBAPI on top of a mocked target. """
127*043036a2SApple OSS Distributions
128*043036a2SApple OSS Distributions        DOFHELP_ADDR = 0xffffffff11220000
129*043036a2SApple OSS Distributions
130*043036a2SApple OSS Distributions        # Construct simple data structure mock.
131*043036a2SApple OSS Distributions        self.create_mock('struct dof_helper', DOFHELP_ADDR).fromDict({
132*043036a2SApple OSS Distributions            'dofhp_mod': b'mock-mod',
133*043036a2SApple OSS Distributions            'dofhp_addr': 0x1234,
134*043036a2SApple OSS Distributions            'dofhp_dof': 0x5678
135*043036a2SApple OSS Distributions        })
136*043036a2SApple OSS Distributions
137*043036a2SApple OSS Distributions        # Construct SBValue on top of the mock.
138*043036a2SApple OSS Distributions        addr = self.target.ResolveLoadAddress(DOFHELP_ADDR)
139*043036a2SApple OSS Distributions        sbv = self.target.CreateValueFromAddress('test',
140*043036a2SApple OSS Distributions                            addr, lookup_type('dof_helper_t'))
141*043036a2SApple OSS Distributions
142*043036a2SApple OSS Distributions        self.assertTrue(sbv.IsValid() and sbv.error.success)
143*043036a2SApple OSS Distributions
144*043036a2SApple OSS Distributions        # Check that LLDB SBAPI returns correct values from mock.
145*043036a2SApple OSS Distributions        err = lldb.SBError()
146*043036a2SApple OSS Distributions        self.assertEqual(
147*043036a2SApple OSS Distributions            sbv.GetChildMemberWithName('dofhp_mod').GetData().GetString(err, 0),
148*043036a2SApple OSS Distributions            "mock-mod"
149*043036a2SApple OSS Distributions        )
150*043036a2SApple OSS Distributions        self.assertEqual(
151*043036a2SApple OSS Distributions            sbv.GetChildMemberWithName('dofhp_addr').GetValueAsUnsigned(),
152*043036a2SApple OSS Distributions            0x1234
153*043036a2SApple OSS Distributions        )
154*043036a2SApple OSS Distributions        self.assertEqual(
155*043036a2SApple OSS Distributions            sbv.GetChildMemberWithName('dofhp_dof').GetValueAsUnsigned(),
156*043036a2SApple OSS Distributions            0x5678
157*043036a2SApple OSS Distributions        )
158*043036a2SApple OSS Distributions
159*043036a2SApple OSS Distributions    @unittest.skipIf(LLDBTestCase.arch() != 'arm64e', "Only on arm64e")
160*043036a2SApple OSS Distributions    def test_skip_arch(self):
161*043036a2SApple OSS Distributions        """ Example of architecture specific test. """
162*043036a2SApple OSS Distributions
163*043036a2SApple OSS Distributions        self.assertEqual(self.target.triple.split('-', 1)[0], 'arm64e')
164*043036a2SApple OSS Distributions
165*043036a2SApple OSS Distributions    @unittest.skipIf(LLDBTestCase.variant() != 'DEVELOPMENT', "DEVELOPMENT kernel only")
166*043036a2SApple OSS Distributions    def test_skip_development(self):
167*043036a2SApple OSS Distributions        """ Test that runs only on release kernel. """
168*043036a2SApple OSS Distributions
169*043036a2SApple OSS Distributions        self.assertEqual(LLDBTestCase.variant(), "DEVELOPMENT")
170