1## 2# Copyright (c) 2023 Apple Inc. All rights reserved. 3# 4# @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5# 6# This file contains Original Code and/or Modifications of Original Code 7# as defined in and that are subject to the Apple Public Source License 8# Version 2.0 (the 'License'). You may not use this file except in 9# compliance with the License. The rights granted to you under the License 10# may not be used to create, or enable the creation or redistribution of, 11# unlawful or unlicensed copies of an Apple operating system, or to 12# circumvent, violate, or enable the circumvention or violation of, any 13# terms of an Apple operating system software license agreement. 14# 15# Please obtain a copy of the License at 16# http://www.opensource.apple.com/apsl/ and read it before using this file. 17# 18# The Original Code and all software distributed under the License are 19# distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20# EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21# INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22# FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23# Please see the License for the specific language governing rights and 24# limitations under the License. 25# 26# @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27## 28 29# pylint: disable=invalid-name 30# pylint: disable=protected-access 31 32""" Test process.py """ 33 34import unittest 35from lldbmock.utils import lookup_type 36from lldbmock.valuemock import ValueMock 37 38import process as tst_process 39import utils as tst_utils 40 41 42class ProcessTest(unittest.TestCase): 43 """ Tests for process.py module """ 44 45 def test_GetProcPid(self): 46 """ Test a pid gets returned. """ 47 48 proc = ValueMock.createFromType('proc') 49 proc.p_pid = 12345 50 51 self.assertEqual(tst_process.GetProcPID(proc), 12345) 52 self.assertEqual(tst_process.GetProcPID(None), -1) 53 54 def test_GetNameShort(self): 55 """ Test fallback to short name. """ 56 57 proc = ValueMock.createFromType('proc') 58 proc.p_name = "" 59 proc.p_comm = "short-proc" 60 61 self.assertEqual(tst_process.GetProcName(proc), "short-proc") 62 63 def test_GetNameLong(self): 64 """ Test that long name is preferred. """ 65 66 proc = ValueMock.createFromType('proc') 67 proc.p_name = "long-proc" 68 proc.p_comm = "short-proc" 69 70 self.assertEqual(tst_process.GetProcName(proc), "long-proc") 71 72 def test_GetNameInvalid(self): 73 """ Test that invalid proc returns default name. """ 74 75 self.assertEqual( 76 tst_process.GetProcName(None), 77 tst_process.NO_PROC_NAME 78 ) 79 80 def test_ASTValuesInSync(self): 81 """ Test that thread states cover all values defined in kernel. """ 82 83 # Compare all values with AST chars dictionary. 84 macro = tst_process._AST_CHARS.keys() 85 86 # Add rest of values from the enum in kernel. 87 enum = lookup_type('ast_t') 88 self.assertTrue(enum.IsValid()) 89 90 kernel = [ 91 k.GetValueAsUnsigned() 92 for k in enum.get_enum_members_array() 93 ] 94 95 # Assert that both sides handle identical set of flags. 96 self.assertSetEqual(set(macro), set(kernel), 97 "thread state chars mismatch") 98 99 def test_GetAstSummary(self): 100 """ Test AST string genration. """ 101 102 ast = tst_utils.GetEnumValue('ast_t', 'AST_DTRACE') 103 ast |= tst_utils.GetEnumValue('ast_t', 'AST_TELEMETRY_KERNEL') 104 105 # Check valid AST 106 self.assertEqual(tst_process.GetASTSummary(ast), 'TD') 107 108 # Check that we never touch unsupported bits in an invalid value 109 ast = 0xffffffff 110 aststr = ''.join(char for _, char in tst_process._AST_CHARS.items()) 111 112 self.assertEqual(tst_process.GetASTSummary(ast), aststr) 113