# Testing `linux-loader`

## Tests

Our Continuous Integration (CI) pipeline is implemented on top of
[Buildkite](https://buildkite.com/).
For the complete list of tests, check our
[CI pipeline](https://buildkite.com/rust-vmm/rust-vmm-ci).

Each individual test runs in a container. To reproduce a test locally, you can
use the dev-container on both x86 and arm64.

```bash
container_version=11
docker run -it \
           --security-opt seccomp=unconfined \
           --volume $(pwd):/linux-loader \
           rustvmm/dev:v${container_version}
cd linux-loader/
cargo test --all-features
```

## Test Images

The kernel images used in the unit tests fall into 3 categories:
- downloaded from known distro sources using the
  [download script](../.buildkite/download_resources.sh).
- fuzzing images; these images were generated by fuzzers such as `libfuzzer` or
  `afl` starting from an existing image already used by the unit tests. These
  are named with `fuzz_` so that they are easy to identify.
- generated images (either by building the Linux kernel, or by using other
  tools such as [ELFIO](https://github.com/serge1/ELFIO)).

### Building the `PE` image

The PE image used by the linux-loader unit tests is built on an `aarch64`
machine using the following commands:

```bash
git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
git checkout v4.20

# To be able to run the following command, you'll need to install
# flex and bison.
make allnoconfig
make Image
head -c 4096 arch/arm64/boot/Image > test_image.bin
```

### Generating `ELF` images with [ELFIO](https://github.com/serge1/ELFIO)

All the ELF files used in `linux-loader` are generated using the `ELFIO` tool.
The [`.cpp` source files](elfio_files) are created from the ELFIO `writer`
example, with minimal changes on top.

The next section includes an example of how to use the source files to generate
the binaries used in the unit tests.

| Source File | Generated Binary File |
|-------------|-----------------------|
| bad_align_writer.cpp | test_bad_align.bin |
| invalid_pvh_note_writer.cpp | test_invalid_pvh_note.bin |
| dummy_note.cpp | test_dummy_note.bin |
| basic_elf.cpp | test_elf.bin |
| ignored_phv.cpp | test_elfnote.bin |
| ignored_phv_8byte_align.cpp | test_elfnote_8byte_align.bin |

#### Example for generating `test_bad_align.bin`

```bash
git clone git@github.com:serge1/ELFIO.git

# Copy the bad_align_writer.cpp file from this repo to the ELFIO path.
cp "${LINUX_LOADER_PATH}/docs/elfio_files/bad_align_writer.cpp" "${ELFIO_PATH}"
cd "${ELFIO_PATH}"
# The images in this repo were built from the`57e614a` commit.
git checkout 57e614a

g++ bad_align_writer.cpp -o bad_align_writer -I. -std=c++11
./bad_align_writer
cp test_bad_align.bin "${LINUX_LOADER_PATH}/src/loader/elf/"
```
