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 31""" Tests for ValueMock class 32 33 Validates implementation of ValueMock mock class. 34""" 35 36import unittest 37from lldbmock.valuemock import ValueMock 38from lldbmock.utils import lookup_type 39 40 41class MockTest(unittest.TestCase): 42 """ Tests that mocking subsystem is working as expected. """ 43 44 def test_mockValidMember(self): 45 """ Ensure that valid member access works. """ 46 47 proc = ValueMock.createFromType('proc') 48 49 proc.p_pid = 5 50 self.assertEqual(proc.p_pid, 5) 51 52 def test_mockInvalidMember(self): 53 """ Ensure that invalid member access fails. """ 54 55 proc = ValueMock.createFromType('proc') 56 57 with self.assertRaises(AttributeError): 58 proc.foobar = 1 59 60 def test_mockAnonUnion(self): 61 """ Ensure that anon members are propagated to top level.""" 62 63 proc = ValueMock.createFromType('proc') 64 self.assertTrue(hasattr(proc, 'p_pid')) 65 66 def test_mockNestedInvalid(self): 67 """ Ensure that all sub-members are also speced mocks. """ 68 69 proc = ValueMock.createFromType('proc') 70 71 with self.assertRaises(AttributeError): 72 proc.p_list.foobar = 1 73 74 def test_mockNestedValid(self): 75 """ Ensure that all sub-members are initialized mocks. """ 76 77 proc = ValueMock.createFromType('proc') 78 proc.p_list.le_next = 5 79 proc.p_list.le_prev = 5 80 self.assertTrue(proc.p_list.le_next, 5) 81 82 def test_mockSimpleType(self): 83 """ Ensure that mock works for non-coumpound types. """ 84 85 value = ValueMock.createFromType('uint32_t') 86 87 # It is not possible to set a member 88 with self.assertRaises(AttributeError): 89 value.foo_member = 5 90 91 # It is possible to set a value 92 value = 5 93 self.assertEqual(value, 5) 94 95 def test_mockArray(self): 96 """ Ensure that array can use index operator. """ 97 98 arrtype = lookup_type('proc').GetArrayType(11) 99 procarr = ValueMock.createFromType(arrtype) 100 101 procarr[10].p_comm = "Hello world" 102 procarr[0].p_comm = "testproc" 103 procarr[3].p_comm = "slice" 104 procarr[4].p_comm = "slice" 105 procarr[5].p_comm = "slice" 106 107 self.assertEqual(procarr[10].p_comm, "Hello world") 108 self.assertNotEqual(procarr[0].p_comm, procarr[10].p_comm) 109 110 with self.assertRaises(AttributeError): 111 procarr[5].foobar = 5 112 113 self.assertSetEqual(set(a.p_comm for a in procarr[3:6]), {'slice'}) 114