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