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