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