Skip to content

Commit 2e4c6a8

Browse files
committed
tests: Add end to end tests with bats
1 parent f6e8caf commit 2e4c6a8

14 files changed

+221
-7
lines changed

.github/workflows/tests.yml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,28 @@ jobs:
4141
poetry install
4242
- name: Run pytest
4343
run: |
44-
poetry run make test
44+
poetry run make pytest
45+
46+
bats:
47+
strategy:
48+
fail-fast: false
49+
50+
runs-on: ubuntu-latest
51+
container: debian:stable
52+
53+
steps:
54+
- uses: actions/checkout@v4
55+
- uses: actions/cache@v3
56+
with:
57+
path: ~/.cache/pypoetry/virtualenvs
58+
key: ${{ runner.os }}-poetry-${{ hashFiles('poetry.lock') }}-${{ hashFiles('pyproject.toml') }}
59+
- name: Install Dependencies
60+
run: |
61+
apt-get update -y && apt-get install -y bats python3 python3-poetry jq
62+
poetry install
63+
- name: Run bats
64+
run: |
65+
poetry run make bats
4566
4667
vecu:
4768
strategy:

.shellcheckrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-FileCopyrightText: AISEC Pentesting Team
2+
#
3+
# SPDX-License-Identifier: CC0-1.0
4+
5+
shell=bash

Makefile

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ default:
1010
@echo " fmt run autoformatters"
1111
@echo " lint run linters"
1212
@echo " docs build docs"
13-
@echo " test run testsuite"
13+
@echo " tests run testsuite"
14+
@echo " pytest run pytest tests"
15+
@echo " bats run bats end to end tests"
1416
@echo " clean delete build artifacts"
1517

1618
.PHONY: zipapp
@@ -26,6 +28,7 @@ lint:
2628
mypy src tests
2729
ruff check src tests
2830
ruff format --check src tests
31+
find tests/bats \( -iname "*.bash" -or -iname "*.bats" \) | xargs shellcheck
2932
reuse lint
3033

3134
.PHONY: lint-win32
@@ -35,16 +38,24 @@ lint-win32:
3538

3639
.PHONY: fmt
3740
fmt:
38-
ruff check --fix-only src tests
39-
ruff format src tests
41+
ruff check --fix-only src tests/pytest
42+
ruff format src tests/pytest
43+
find tests/bats \( -iname "*.bash" -or -iname "*.bats" \) | xargs shfmt -w
4044

4145
.PHONY: docs
4246
docs:
4347
$(MAKE) -C docs html
4448

45-
.PHONY: test
46-
test:
47-
python -m pytest -v --cov=$(PWD) --cov-report html tests
49+
.PHONY: tests
50+
tests: pytest bats
51+
52+
.PHONY: pytest
53+
pytest:
54+
python -m pytest -v --cov=$(PWD) --cov-report html tests/pytest
55+
56+
.PHONY: bats
57+
bats:
58+
bats -x -r tests/bats
4859

4960
.PHONY: clean
5061
clean:

flake.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
devShell.x86_64-linux = pkgs.mkShell {
1515
buildInputs = with pkgs; [
1616
poetry
17+
shellcheck
18+
shfmt
19+
bats
20+
nodePackages_latest.bash-language-server
1721
python311
1822
python312
1923
];

tests/bats/001-invocation.bats

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bats
2+
3+
# SPDX-FileCopyrightText: AISEC Pentesting Team
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
@test "invoke gallia without parameters" {
8+
# Should fail and print help page.
9+
run -64 gallia
10+
}

tests/bats/002-scans.bats

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env bats
2+
3+
# SPDX-FileCopyrightText: AISEC Pentesting Team
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
load helpers.sh
8+
9+
setup_file() {
10+
setup_gallia_toml
11+
run_vecu &
12+
}
13+
14+
teardown_file() {
15+
kill_vecu
16+
}
17+
18+
@test "scan services" {
19+
gallia scan uds services --sessions 1 2 --check-session
20+
}
21+
22+
@test "scan sessions" {
23+
gallia scan uds sessions --depth 2
24+
}
25+
26+
@test "scan fast" {
27+
gallia scan uds sessions --fast
28+
}
29+
30+
@test "scan identifiers sid 0x22" {
31+
gallia scan uds identifiers --start 0 --end 100 --sid 0x22
32+
}
33+
34+
@test "scan identifiers sid 0x2e" {
35+
gallia scan uds identifiers --start 0 --end 100 --sid 0x2e
36+
}
37+
38+
@test "scan identifiers sid 0x31" {
39+
gallia scan uds identifiers --start 0 --end 100 --sid 0x31
40+
}
41+
42+
@test "scan reset" {
43+
gallia scan uds reset
44+
}
45+
46+
@test "scan dump-seeds" {
47+
gallia scan uds dump-seeds --duration 0.01 --level 0x2f
48+
}
49+
50+
@test "scan memory" {
51+
for sid in 0x23 0x34 0x35 0x3d; do
52+
poetry run gallia scan uds memory --sid "$sid"
53+
done
54+
}

tests/bats/003-primitives.bats

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env bats
2+
3+
# SPDX-FileCopyrightText: AISEC Pentesting Team
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
load helpers.sh
8+
9+
setup_file() {
10+
setup_gallia_toml
11+
run_vecu &
12+
}
13+
14+
teardown_file() {
15+
kill_vecu
16+
}
17+
18+
@test "primitive ecu-reset" {
19+
gallia primitive uds ecu-reset
20+
}
21+
22+
@test "primitive vin" {
23+
gallia primitive uds vin
24+
}
25+
26+
@test "primitive ping" {
27+
gallia primitive uds ping --count 2
28+
}
29+
30+
@test "primitive rdbi" {
31+
gallia primitive uds rdbi 0x108d
32+
}
33+
34+
@test "primitive pdu" {
35+
gallia primitive uds pdu 1001
36+
}
37+
38+
@test "primitive wdbi" {
39+
gallia primitive uds wdbi 0x2266 --data 00
40+
}
41+
42+
@test "primitive dtc read" {
43+
gallia primitive uds dtc read
44+
}
45+
46+
@test "primitive dtc clear" {
47+
gallia primitive uds dtc clear
48+
}
49+
50+
@test "primitive dtc control stop" {
51+
gallia primitive uds dtc control --stop
52+
}
53+
54+
@test "primitive dtc control resume" {
55+
gallia primitive uds dtc control --resume
56+
}
57+
58+
@test "primitive iocbi reset-to-default" {
59+
gallia primitive uds iocbi 0x1000 reset-to-default
60+
}

tests/bats/helpers.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# SPDX-FileCopyrightText: AISEC Pentesting Team
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
close_non_std_fds() {
6+
for fd in $(ls /proc/$BASHPID/fd); do
7+
if [[ $fd -gt 2 ]]; then
8+
eval "exec $fd>&-" || true
9+
fi
10+
done
11+
}
12+
13+
run_vecu() {
14+
close_non_std_fds
15+
gallia script vecu "unix-lines:///tmp/vecu.sock" rng --seed 3 --mandatory_sessions "[1, 2, 3]" --mandatory_services "[DiagnosticSessionControl, EcuReset, ReadDataByIdentifier, WriteDataByIdentifier, RoutineControl, SecurityAccess, ReadMemoryByAddress, WriteMemoryByAddress, RequestDownload, RequestUpload, TesterPresent, ReadDTCInformation, ClearDiagnosticInformation, InputOutputControlByIdentifier]"
16+
}
17+
18+
kill_vecu() {
19+
kill -9 "$(pgrep gallia)"
20+
rm -f /tmp/vecu.sock
21+
}
22+
23+
setup_gallia_toml() {
24+
{
25+
echo "[gallia]"
26+
echo "[gallia.scanner]"
27+
echo 'target = "unix-lines:///tmp/vecu.sock"'
28+
echo 'dumpcap = false'
29+
30+
echo "[gallia.protocols.uds]"
31+
echo 'ecu_reset = 0x01'
32+
} > "$BATS_FILE_TMPDIR/gallia.toml"
33+
34+
export GALLIA_CONFIG="$BATS_FILE_TMPDIR/gallia.toml"
35+
}
36+

tests/bats/setup_suite.bash

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# SPDX-FileCopyrightText: AISEC Pentesting Team
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
setup_suite() {
6+
bats_require_minimum_version 1.5.0
7+
8+
# https://bats-core.readthedocs.io/en/stable/tutorial.html#let-s-do-some-setup
9+
DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" >/dev/null 2>&1 && pwd)"
10+
PATH="$DIR/..:$PATH"
11+
12+
cd "$BATS_TEST_TMPDIR" || exit 1
13+
}
File renamed without changes.

0 commit comments

Comments
 (0)