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