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