Skip to main content

Testing Pipeline

This document describes the architecture of the automated testing environment, the logic behind the integration tests, and the configuration of the CI/CD pipeline (/.gitlab-ci.yml).

Pipeline Architecture & GitLab CI​

The pipeline was upgraded to support Full-Stack Integration Testing. Unlike unit tests, these integration tests verify the interaction between the Python logic, the MySQL database, and the Redis message broker.

Docker registry and image management
To ensure a consistent and fast environment, a custom Docker image is build:

  • Dockerfile: Located at /tests/integration/Dockerfile.
  • Base: python:3.11-slim.
  • Version Pinning: apt and python packages are pinned to specific versions. See /tests/integration/all_requirements.txt
  • Registry Upload: The image is built by the pipeline jobs generate-requirements and build_base_test and pushed to the GitLab Container Registry at hub.informatik.hs-augsburg.de as firmware_vault_test_base:<commit sha> (variable TEST_BASE_IMAGE).
  • Own name and tag per pipeline: the test image must not be pushed as firmware_vault_base:latest. That tag belongs to the production base image (build_base_main, without pytest). When both jobs wrote the same tag, test jobs sometimes got the production image and failed with pytest: command not found, and parallel pipelines overwrote each other's test image.

Environment Variables & PYTHONPATH
A critical configuration for the imports to work across the project structure is the PYTHONPATH.

  • Variable: PYTHONPATH: "$CI_PROJECT_DIR/jobs"
  • Purpose: By setting this in the CI variables or the Dockerfile, Python can resolve modules like from analysis_tool_set.redis_communication import RedisTaskTracker or from DBConnector import DBConnector correctly, even when the test runner is executed from the root directory.

Integration Test Logic​

The integration tests are located in /tests/integration/. They use a Live-Service approach, meaning they talk to real mysql:8 and redis:7-alpine containers started as GitLab services.

Modules Tested
Basically modules from /jobs are tested.

  • /jobs/analysis_tool_set/redis_communication.py: The handler for the redis queues.
  • /jobs/DBConnector.py: The database abstraction layer.
  • /jobs/executor/executor.py: The core Flask API and the "Splitter" logic.
  • tools which are implemented from the Template_Analysis_Tool. Currently tested are /jobs/extractor/extractor.py, /jobs/cve_bin_tool/cve_bin_tool.py, /jobs/cve_bin_tool/sbom_gen.py, /jobs/firmwalker/firmwalker.py and //jobs/trufflehog/trufflehog.py.
  • /jobs/scheduler.py: Manages the new jobs into queueing.
  • /jobs/api.py: The api which connects the frontend with the executor and scheduler.

Test Dependencies
The order of the tests was created in order to only test modules if all other modules they depend on were already tested. The dependencies can be reconstructed by simply following the modules imports.
The dependencies for all listed modules are as follows.

  • database > none
  • redis > none
  • tools > database, redis
  • executor > redis, tools
  • scheduler > executor
  • api > scheduler

Summary: - (database, redis) -> tools -> executor -> - scheduler -> api
While some endpoints are mocked to test the modules in isolation, tests will (try to) only use the functionality of other modules if they were previously tested.

Database Preparation​

Before tests run, the database is initialized using /docker_compose_dir/sql_scripts/init.sql.

  • Tables: Creates products, jobs, tasks, tools, tool_dependencies, etc.
  • Seeding: In the .db-setup stage of the pipeline, the mysql client is used to pipe SQL into the container.
  • Wait-Loop: The pipeline includes a timeouted loop which waites for the database to start. This prevents the tests from starting before the MySQL service is fully initialized and ready to accept connections.

Writing basic tests for a new tool​

The file test_tools.py under /tests/integration needs to be updated if a new tool is added to the code base to guarantee coverage of the test functions. All tools have (currently) two basic test cases.

  • The first tests reimports the tool and checks the database "tools" for whether the tool has successfully verified with the database (testing the @tool(...) decorator). If the tool has any dependencies, the tool_dependencies should also be verified.
  • The second test checks how the tool will handle a task. It manually creates a payload, and test whether the run_tool function throws any errors.

Relevant test's should also be updated, whenever functionality changes (e.g. when jobs were enabled to handle multiple tools, i.e. tool -> tools).

warning

Testing functions must be named with the prefix test_ otherwise, the pytest command will not recognize these as functions to be executed during testing!

build job for the python base​

The "prepare" stage is generating a python image which searched for all requirements in the project (all requirements.txt files), uploads it to the GitLab registry and uses this base for all tests. This ensures all tests can use all defined libraries.

Unit tests​

Unit tests are located in /tests/unit/. They need no database, no Redis and no external tools and run in the CI job test-extractor-unit (stage test, test image firmware_vault_test_base).

  • test_recurse_extract.py: the extraction pipeline in src/extractor/recurse_extract.py. External tools (FACT, unblob, sasquatch, jefferson, binwalk, untrx) are replaced by fakes, so every decision of the pipeline can be tested: file selection, root filesystem detection, archive/TRX/JFFS2 detection, the encrypted fast-path, the order of the extraction steps and the result.
  • test_analysis_tool_set_utils.py: get_rootfs_path(), which the downstream tools use to find the extracted root filesystem.

Run them locally with:

PYTHONPATH=src pytest tests/unit/

Extractor integration tests with generated test images​

The tests in /tests/extraction/ run the real extraction pipeline on small firmware images that are generated during the test run (SquashFS, TRX, nested images, ZIP and tar.gz downloads, Synology .pat, JFFS2, random data as encrypted image, a text file). No firmware images are stored in git.

They run in two CI jobs:

  • build_extractor_test (stage prepare) builds the extractor image of the current commit with kaniko and pushes it as extractor-test:<commit>. A separate build is needed because the regular build_*_mr jobs only run for merge requests into main.
  • test-extractor-integration (stage test) runs the tests inside this image, so they use exactly the tools of the production image. squashfs-tools and mtd-utils are installed in the job only to build the test images.

Outside of the extractor image the tests are skipped. Run them locally with Docker:

docker build --platform linux/amd64 -f src/extractor/Dockerfile_extractor -t extractor-test src
docker run --rm --platform linux/amd64 --entrypoint "" -v $(pwd):/src extractor-test sh -c \
"apt-get update && apt-get install -y squashfs-tools mtd-utils && pip install pytest && \
cd /src && python -m pytest tests/extraction/"

If a test uncovers a bug that cannot be fixed right away, mark the test with @pytest.mark.xfail(strict=True, reason="..."). It does not fail the pipeline, but as soon as the bug is fixed the test passes unexpectedly and the pipeline fails, so the xfail marker has to be removed together with the fix. The bugs found so far are listed under "Fixed issues" on the Extractor page.

Extractor regression tests with real firmware images​

The tests in /tests/firmware/ run the extractor on a small set of real vendor firmware images and compare the result with the values measured when the image was added. They catch regressions that generated images cannot show, e.g. a tool update that suddenly extracts far fewer files.

Where the images are stored

The images are not stored in git. They are uploaded once to the GitLab Generic Package Registry of the project (package test-firmware, version v1, see Deploy > Package registry). tests/firmware/manifest.json lists every image with its sha256 and the expected result:

  • extraction_method, rootfs_found, encrypted
  • min_files / max_files: 90 % and 120 % of the measured number of files
  • rootfs_files: paths that must exist directly in rootfs_path, so the extractor picked the right file system. Regular files among them (symlinks are resolved inside the rootfs) must not be empty: a tool can create all file names but fail on the data, which happened with the Belkin F5D8630 CramFS image (#269)
  • files_below_rootfs: vendor specific paths that must exist somewhere below rootfs_path, so it is really the file system of this image

CI job test-extractor-regression (stage test) runs in the extractor image built by build_extractor_test. fetch_images.py downloads the images with the CI_JOB_TOKEN and verifies their sha256; the GitLab cache (test-firmware-v1) keeps them between pipelines. The job fails if an image cannot be downloaded. pytest runs with -v, so the log shows every test with the image name.

Hanging extractions: one extraction may take at most FIRMWARE_TEST_TIMEOUT seconds (default 900; the slowest image needs about 2.5 minutes). After that the extractor and all processes it started (FACT, unblob workers, ...) are killed as one process group, and the tests of that image fail with the running processes and the end of the extractor output. Before, a hanging image blocked the job without any output until the 1 hour job timeout. The job itself has timeout: 45m (test-extractor-integration: 30m).

Run locally (needs a personal access token with scope read_api or api in ~/.config/gitlab_token or GITLAB_TOKEN):

python tests/firmware/fetch_images.py
docker run --rm --platform linux/amd64 --entrypoint "" -v $(pwd):/src extractor-test sh -c \
"pip install pytest && cd /src && python -m pytest tests/firmware/"

Selected images

The images were chosen so that every extraction path and result is covered with several vendors, while the job stays reasonably fast (about 12 minutes for all 21 images, 1.1 GB, cached between pipelines). Two groups:

Images with a root filesystem (content is checked):

ImageVendorSizeWhy it is in the set
FRITZ!Box 7490 7.62AVM38 MBFACT with nested filesystem_core.squashfs; showed the nested image bug (15,273 instead of 11,859 files); rootfs_path must be the inner core system (#271)
FRITZ!Box 7581 7.18AVM32 MBFACT, AVM with xDSL
FRITZ!Box 7690 8.22AVM47 MBFIT image, unblob, about 21,000 files
FRITZ!Powerline 1240 AX 2.75.08.20AVM26 MBFIT image, unblob
FRITZ!Powerline 540E 1.29.07.15AVM13 MBunblob
F9K1112 1.00.28Belkin9 MBmulti-partition image, unblob
F7D4302 1.00.28Belkin7 MBBroadcom SquashFS v3, unblob
F5D8630-4 1.00.07Belkin3 MBCramFS, unblob
RX7500 1.0.04.246Belkin25 MBOpenWrt on UBI/UBIFS, nested FACT extraction
RT1800 1.1.00.016Belkin23 MBOpenWrt SquashFS; 0 files before the nested image fix, now about 4,500
M7400 (EU) 1.0.2TP-Link74 MBfirmware inside a ZIP download, UBI/UBIFS, FACT
TWG-431BR 1.0.15TRENDnet16 MBfirmware inside a ZIP download, unblob
Mustang-200 QTS 5.1.8QNAP214 MBencrypted QTS image in a ZIP; initrd root next to rootfs2 (/usr) and many packages; showed the usr rootfs bug
DS1511+ DSM 6.2 (25556)Synology270 MB.pat tar archive, merged-/usr root filesystem; showed the usr rootfs bug

Images without a root filesystem (result is pinned so that changes become visible):

ImageVendorSizeExpected result
FRITZ!Powerline 530E 1.50.02.18AVM0.4 MBencrypted (fast-path)
TWG-BRF114 1.0.0.6TRENDnet0.6 MBencrypted, inside a ZIP download
DS1515 DSM 7 (42962)Synology304 MBencrypted .pat
TW100-BRV304 V2.2 V3.0TRENDnet1.4 MBfailed, nothing can be extracted
FRITZ!Box Fon WLAN 7390 6.88AVM18 MB3 files, no rootfs
TL-WA860RE (EU) V6TP-Link2 MB15 files, no rootfs
TW100-S4W1CA v2 2.3.32TRENDnet0.6 MB78 files, no rootfs

Not covered yet: a real TRX image (Netgear); TRX is covered by the generated images in /tests/extraction/.

Upload images to the package registry

The package registry has no upload button in the web interface; files can only be uploaded through the API with a personal access token. The token is only needed for the upload, the CI uses its own CI_JOB_TOKEN.

  1. Create a token: GitLab > avatar > Preferences > Access > Personal access tokens > Generate token. Name e.g. test-firmware-upload, expiration date in a few days, scope api only. The token is shown only once, copy it.
  2. Store the token in a file instead of pasting it into a terminal or chat (macOS, token in the clipboard):
    mkdir -p ~/.config && pbpaste > ~/.config/gitlab_token && chmod 600 ~/.config/gitlab_token
    Alternatively set the environment variable GITLAB_TOKEN for the command.
  3. Upload. The script searches the given directory recursively for the file names from manifest.json (e.g. the scraper download volume), checks the sha256 and skips images that already exist in the registry:
    python3 tests/firmware/upload_images.py docker_compose_dir/volumes/scraper_downloads
    Every image is reported as uploaded, exists or NOT FOUND.
  4. Check the download with an empty cache folder:
    FIRMWARE_TEST_DIR=/tmp/fw-check python3 tests/firmware/fetch_images.py
  5. Delete the token file (rm ~/.config/gitlab_token) and revoke the token in GitLab (Personal access tokens > Actions > Revoke).

Add a new image

  1. Run the extractor on the image in the extractor container (python /app/recurse_extract.py <image>) and note extraction_method, file_count and rootfs_path.
  2. Add an entry to manifest.json: name (original file name), registry_file (only letters, digits, ., - and _), sha256 (sha256sum <image>), size_bytes and the expected values. min_files/max_files are 90 % and 120 % of the measured file_count. Choose a few key files from the root filesystem (rootfs_files) and one or two vendor specific files (files_below_rootfs).
  3. Upload the image as described above.
  4. Run the tests locally (see above) and commit manifest.json.
  5. To replace an image, upload it under a new version (v2) instead of overwriting the old one, and change registry_version in manifest.json and the cache key in .gitlab-ci.yml.

Images should stay small (preferably below 50 MB) and fast to extract, because the job runs in every pipeline.

Dependency Vulnerability Scanning (Trivy)​

To continuously track security vulnerabilities in pinned dependencies and lockfiles, the scheduled nightly pipeline includes the dependency_vulnerability_scan job running Aqua Security Trivy.

Execution & Schedule​

  • Schedule: Executes nightly via GitLab CI pipeline schedule on the default branch (main). Can also be triggered manually via the GitLab CI Web UI (Run pipeline).
  • Merge requests: The scan runs automatically and publishes reports without synchronizing an issue. Issue synchronization is enabled by CI rules for scheduled and manual default-branch runs.
  • Scan Targets: Scans all repository lockfiles and requirement manifests (Python requirements.txt, frontend package-lock.json, documentation yarn.lock).
  • Reports & Artifacts:
    • JUnit test report (trivy-junit-report.xml) integrated into the GitLab Test Summary.
    • GitLab Code Quality report (gl-code-quality-report.json) displayed in MR widgets.
    • Raw JSON findings (trivy-results.json) and Markdown summary (trivy-issue-body.md) retained as build artifacts.

Automated Issue Tracking & Configuration​

The CI script .gitlab/ci/report_cve_issue.py automatically maintains a single open security issue titled [Security] Trivy Dependency Vulnerability Report. The issue description is refreshed on every run, and a comment notification is posted whenever findings change. When all vulnerabilities are resolved, the issue is automatically closed.

The following GitLab CI/CD variables configure this workflow:

Variable NameRequiredDefaultDescription
GITLAB_ISSUE_TOKENYes (for issue sync)NoneAccess token used by .gitlab/ci/report_cve_issue.py to create and update GitLab issues.
Token Setup & Expiration Renewal

To set up or renew the API token:

  1. Navigate to Settings > Access Tokens in the firmware_vault repository.
  2. Generate a Project Access Token (or use a group/personal token) with:
    • Role: Reporter
    • Scopes: api (required for issue writes; the Reporter role limits project permissions)
  3. Save the token as a masked CI/CD variable named GITLAB_ISSUE_TOKEN under Settings > CI/CD > Variables.
  4. Since GitLab tokens have a maximum lifetime, configure an expiration reminder or renew the variable value when the token nears expiration.