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