1# Testing
2
3## Overview
4
5Hafnium has 4 main kinds of tests:
6
71.  Host tests
8    *   Unit tests of core functionality, e.g. page table manipulation.
9    *   Source in `src/*_test.cc`.
10    *   Using the [Google Test](https://github.com/google/googletest) framework,
11        built against 'fake' architecture (`src/arch/fake`).
121.  Arch tests
13    *   Architecture-specific unit tests, e.g. MMU setup.
14    *   Source under `test/arch`.
15    *   Using our own _hftest_ framework, with `standalone_main.c`.
16    *   Build own hypervisor image, run in EL2.
171.  VM API tests
18    *   Exercise hypervisor API from both primary and secondary VMs.
19    *   Source under `test/vmapi`.
20    *   Tests are run from the primary VM under a normal build of the Hafnium
21        hypervisor, possibly communicating with a helper service in one or more
22        secondary VMs.
23    *   Using our own _hftest_ framework, with `standalone_main.c` for the
24        primary VM and `service.c` for secondary VMs.
25    *   Build own primary and secondary VMs, run in EL1 under actual Hafnium
26        image.
271.  Linux tests
28    *   Exercise the Hafnium Linux kernel module.
29    *   Source under `test/linux`.
30    *   Tests are run from userspace (PID 1) under Linux in the primary VM under
31        Hafnium, possibly with other secondary VMs.
32    *   Using our own _hftest_ framework, with `linux_main.c`.
33
34Host tests run directly on the host machine where they are built, whereas the
35other 3 types can run under an emulator such as QEMU, or on real hardware.
36
37## Presubmit
38
39Presubmit builds everything, runs all tests and checks the source for formatting
40and lint errors. This can be run locally with:
41
42```shell
43./kokoro/build.sh
44```
45
46Or to just run the tests after having built everything manually run:
47
48```shell
49./kokoro/test.sh
50```
51
52## QEMU tests
53
54These tests boot Hafnium on QEMU and the VMs make calls to Hafnium to test its
55behaviour. They can also be run on the Arm [FVP](FVP.md) and in some cases on
56real hardware.
57
58### hftest
59
60Having a framework for tests makes them easier to read and write. _hftest_ is a
61framework to meet the needs of VM based tests for Hafnium. It consists of:
62
63*   assertions
64*   test declarations
65*   base VM image
66*   driver script
67
68Assertions should be familiar from other testing libraries. They make use of
69C11's `_Generic` expressions for type genericity.
70
71Test declarations name the test and the suite that the test is part of.
72Declarations are converted into descriptors stored in the `.hftest` section of
73the VM image which allows the image to inspect the structure of the tests it
74contains. The linker sorts the descriptors by their symbol name which is how
75descriptors from the same suite are grouped together for easier parsing.
76
77The base VM image offers a command line interface, via the bootargs, to query
78the tests in the image and to run specific tests. The driver script uses this
79interface to execute tests, each with a fresh QEMU boot to give a fresh
80environment.
81