1*d4514f0bSApple OSS Distributions# LLDB macro testing 2*d4514f0bSApple OSS Distributions 3*d4514f0bSApple OSS DistributionsHow to work with bundled unit test framework. 4*d4514f0bSApple OSS Distributions 5*d4514f0bSApple OSS DistributionsBe careful when touching common framework code. For larger changes, ask the Platform Triage team to 6*d4514f0bSApple OSS Distributionsvalidate that the changes work in their environment before integration. 7*d4514f0bSApple OSS Distributions 8*d4514f0bSApple OSS DistributionsUnit-test architecture supports two kinds of tests: 9*d4514f0bSApple OSS Distributions 10*d4514f0bSApple OSS Distributions- Standalone unit test \ 11*d4514f0bSApple OSS Distributions `tools/lldbmacros/tests/standalone_tests` 12*d4514f0bSApple OSS Distributions- LLDB based unit test \ 13*d4514f0bSApple OSS Distributions `tools/lldbmacros/tests/lldb_tests` 14*d4514f0bSApple OSS Distributions 15*d4514f0bSApple OSS Distributions**Standalone** unit tests replace _lldb_ and _lldbwrap_ modules with _MagicMock_ 16*d4514f0bSApple OSS Distributionsinstances. All tests in this location must not depend on LLDB. 17*d4514f0bSApple OSS Distributions 18*d4514f0bSApple OSS Distributions**LLDB** unit tests are run from LLDB's python interpreter. It is possible to 19*d4514f0bSApple OSS Distributionsaccess debugger/process/target from such test or invoke LLDB commands. This 20*d4514f0bSApple OSS Distributionsway a test can exercise full stack including SBAPI and expression handlers. 21*d4514f0bSApple OSS Distributions 22*d4514f0bSApple OSS Distributions## How to run tests 23*d4514f0bSApple OSS Distributions 24*d4514f0bSApple OSS DistributionsStandalone tests do not need LLDB and can be run with: 25*d4514f0bSApple OSS Distributions```sh 26*d4514f0bSApple OSS DistributionsPYTHON=tools/lldbmacros python3 tools/lldbmacros/tests/runtest.py <kernel> 27*d4514f0bSApple OSS Distributions``` 28*d4514f0bSApple OSS Distributions 29*d4514f0bSApple OSS DistributionsTo run all tests (including LLDB based ones) a developer has to install 30*d4514f0bSApple OSS DistributionsXCode and configure properly Python's path: 31*d4514f0bSApple OSS Distributions 32*d4514f0bSApple OSS Distributions```sh 33*d4514f0bSApple OSS DistributionsPYTHONPATH="tools/lldbmacros:`xcrun --toolchain ios lldb -P`"xcrun --toolchain ios python3 tools/lldbmacros/tests/runtest.py <kernel> 34*d4514f0bSApple OSS Distributions``` 35*d4514f0bSApple OSS Distributions 36*d4514f0bSApple OSS DistributionsDefault runner supports few options: 37*d4514f0bSApple OSS Distributions 38*d4514f0bSApple OSS Distributions * `-v` enables verbose output from unit test framework 39*d4514f0bSApple OSS Distributions * `-d` enables debug logging and more detailed exception reports 40*d4514f0bSApple OSS Distributions * `-c <path>` outputus HTML coverage if `coverage` module is installed 41*d4514f0bSApple OSS Distributions 42*d4514f0bSApple OSS Distributions## Mocking framework 43*d4514f0bSApple OSS Distributions 44*d4514f0bSApple OSS DistributionsThe goal of the mocking framework is to enhance existing mocking solutions 45*d4514f0bSApple OSS Distributionsrather than building completely new framework. A test should still rely on 46*d4514f0bSApple OSS Distributions`unittest.mock` and cover specific needs with additional mocks provided by 47*d4514f0bSApple OSS Distributionsthis framework. 48*d4514f0bSApple OSS Distributions 49*d4514f0bSApple OSS DistributionsA test developer has three options how to handle mocking in a test: 50*d4514f0bSApple OSS Distributions 51*d4514f0bSApple OSS Distributions* `unittest.mock` that covers general purpose Python mocking. 52*d4514f0bSApple OSS Distributions* `lldbmock.valuemock` designed to mock away `value` class instances. 53*d4514f0bSApple OSS Distributions* `lldbmock.memorymock` designed to provide real object in target's memory. 54*d4514f0bSApple OSS Distributions 55*d4514f0bSApple OSS DistributionsExamples of usage can be found in: \ 56*d4514f0bSApple OSS Distributions `tools/lldbmacros/tests/lldb_tests/test_examples.py` 57*d4514f0bSApple OSS Distributions 58*d4514f0bSApple OSS Distributions### lldbmock.valuemock 59*d4514f0bSApple OSS Distributions 60*d4514f0bSApple OSS DistributionsA very simple mocking designed for replacing a `value` class instance or 61*d4514f0bSApple OSS Distributionssome similar construct in the code. 62*d4514f0bSApple OSS Distributions 63*d4514f0bSApple OSS DistributionsThe `ValueMock` class parses given `SBType` and recreates recursively whole 64*d4514f0bSApple OSS Distributionshierarchy of `MagicMock` instances. Final result looks like a value class but 65*d4514f0bSApple OSS Distributionsit does not implement any value class logic or methods. 66*d4514f0bSApple OSS Distributions 67*d4514f0bSApple OSS DistributionsIt does not perform any extra logic to handle special types like `union`. 68*d4514f0bSApple OSS DistributionsA developer has to correctly populate all members that overlap because this 69*d4514f0bSApple OSS Distributionsmock treats all such members as unique. 70*d4514f0bSApple OSS Distributions 71*d4514f0bSApple OSS DistributionsAuto generating mock specification from kernel under test allows checking that 72*d4514f0bSApple OSS Distributionsall referenced members do exist in the final binary. Broken reference will 73*d4514f0bSApple OSS Distributionsresult either in test or tested code failure. 74*d4514f0bSApple OSS Distributions 75*d4514f0bSApple OSS Distributions### lldbmock.memorymock 76*d4514f0bSApple OSS Distributions 77*d4514f0bSApple OSS DistributionsThe goal of memory mock is to provide easy to use interface for a test developer 78*d4514f0bSApple OSS Distributionsto describe object in target's memory. From technical perspective this is a data 79*d4514f0bSApple OSS Distributionsserializer that reflects memory location and representation of given SBType's 80*d4514f0bSApple OSS Distributionsmembers. 81*d4514f0bSApple OSS Distributions 82*d4514f0bSApple OSS DistributionsThe framework provides two kinds of mocks: 83*d4514f0bSApple OSS Distributions 84*d4514f0bSApple OSS Distributions * `RawMock` that is suitable to place unstructured data into target's memory. 85*d4514f0bSApple OSS Distributions * `MemoryMock` that mirrors given `SBType` and serializes data into target's 86*d4514f0bSApple OSS Distributions memory. 87*d4514f0bSApple OSS Distributions 88