Skip to content

Commit 81decac

Browse files
committed
tests: Add tests for Rust bindings
Add the regression tests (1-60) for the libseccomp crate that is Rust language bindings for the libseccomp library. You can run the tests as follows: ```sh $ sed -i "/^AC_INIT/ s/0.0.0/9.9.9/" configure.ac $ ./autogen.sh $ ./configure --prefix=$(pwd)/src/.libs --enable-rust $ make && make install $ make check-build $ cd tests && ./regression -m rust ``` Based on: #323 Signed-off-by: Manabu Sugimoto <[email protected]> Signed-off-by: mayank <[email protected]>
1 parent 47ca644 commit 81decac

File tree

70 files changed

+3915
-6
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+3915
-6
lines changed

.github/actions/setup/action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,8 @@ runs:
3737
shell: bash
3838
- run: |
3939
./autogen.sh
40+
# Specify the tentative version of the libseccomp test library because some
41+
# functions of the Rust bindings are restricted based on the version.
42+
TEST_VERSION=9.9.9
43+
sed -i "/^AC_INIT/ s/0.0.0/$TEST_VERSION/" configure.ac
4044
shell: bash

.github/workflows/continuous-integration.yml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ jobs:
3434
uses: ./.github/actions/setup
3535
- name: Build libseccomp
3636
run: |
37-
./configure --enable-python
37+
./configure --prefix=$(pwd)/src/.libs --enable-python --enable-rust
38+
# make and make install are required to create a pkconfig because
39+
# the Rust bindings need it for the compilation.
40+
make
41+
make install
3842
make check-build
3943
- name: Run tests
4044
run: |
@@ -52,13 +56,18 @@ jobs:
5256
uses: ./.github/actions/setup
5357
- name: Build libseccomp
5458
run: |
55-
./configure --enable-python
59+
./configure --prefix=$(pwd)/src/.libs --enable-python --enable-rust
60+
# make and make install are required to create a pkconfig because
61+
# the Rust bindings need it for the compilation.
62+
# This can be removed when the Rust bindings drop the version check by pkgconfig.
63+
make
64+
make install
5665
make check-build
5766
- name: Run live tests
5867
run: |
5968
LIBSECCOMP_TSTCFG_JOBS=0 \
6069
LIBSECCOMP_TSTCFG_TYPE=live \
61-
LIBSECCOMP_TSTCFG_MODE_LIST=c make -C tests check
70+
LIBSECCOMP_TSTCFG_MODE_LIST="c rust" make -C tests check
6271
6372
scanbuild:
6473
name: Scan Build

Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ AM_MAKEFLAGS_1 =
4141
AM_MAKEFLAGS_ = ${AM_MAKEFLAGS_0}
4242
AM_MAKEFLAGS = ${AM_MAKEFLAGS_@AM_V@}
4343

44-
# enable python during distcheck
45-
AM_DISTCHECK_CONFIGURE_FLAGS = --enable-python
44+
# enable python and rust during distcheck
45+
AM_DISTCHECK_CONFIGURE_FLAGS = --enable-python --enable-rust
4646

4747
check-build: all
4848
${MAKE} ${AM_MAKEFLAGS} -C src check-build

configure.ac

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,36 @@ AC_DEFINE_UNQUOTED([ENABLE_PYTHON],
123123
[$(test "$enable_python" = "yes" && echo 1 || echo 0)],
124124
[Python bindings build flag.])
125125

126+
dnl ####
127+
dnl rustc checks
128+
dnl ####
129+
AC_CHECK_PROGS(rustc, rustc, "no")
130+
AS_IF([test "$rustc" != no], [
131+
AS_ECHO("checking rustc version... $($rustc -V 2>&1 | cut -d' ' -f 2)")
132+
RUSTC_VER_MAJ=$($rustc -V 2>&1 | cut -d' ' -f 2 | cut -d'.' -f 1);
133+
RUSTC_VER_MIN=$($rustc -V 2>&1 | cut -d' ' -f 2 | cut -d'.' -f 2);
134+
],[
135+
RUSTC_VER_MAJ=0
136+
RUSTC_VER_MIN=0
137+
])
138+
139+
dnl ####
140+
dnl rust binding checks
141+
dnl ####
142+
AC_ARG_ENABLE([rust],
143+
[AS_HELP_STRING([--enable-rust],
144+
[build the rust bindings, requires rustc])])
145+
AS_IF([test "$enable_rust" = yes], [
146+
# rustc version check
147+
AS_IF([test "$RUSTC_VER_MAJ" -eq 1 -a "$RUSTC_VER_MIN" -lt 63], [
148+
AC_MSG_ERROR([rust bindings require rustc 1.63 or higher])
149+
])
150+
])
151+
AM_CONDITIONAL([ENABLE_RUST], [test "$enable_rust" = yes])
152+
AC_DEFINE_UNQUOTED([ENABLE_RUST],
153+
[$(test "$enable_rust" = yes && echo 1 || echo 0)],
154+
[Rust bindings build flag.])
155+
126156
AC_CHECK_TOOL(GPERF, gperf)
127157
if test -z "$GPERF"; then
128158
AC_MSG_ERROR([please install gperf])

tests/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,7 @@ util.pyc
6868
58-live-tsync_notify
6969
59-basic-empty_binary_tree
7070
60-sim-precompute
71+
/rust/target
72+
/rust/utils/target
73+
.crates.toml
74+
.crates2.json

tests/Makefile.am

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,32 @@ miniseq_LDADD =
3434

3535
TESTS = regression
3636

37+
if ENABLE_RUST
38+
rust_BINDINGS_TEST = yes
39+
else
40+
rust_BINDINGS_TEST = no
41+
endif
42+
43+
rust_TESTS_DIR = ./rust
44+
rust_CARGO_TOML = Cargo.toml
45+
rust_LINK_TYPE = static
46+
rust_LIBSECCOMP_LIBRARY_DIR = ../src/.libs/lib
47+
export LIBSECCOMP_LINK_TYPE =$(rust_LINK_TYPE)
48+
export LIBSECCOMP_LIB_PATH =$(shell realpath $(rust_LIBSECCOMP_LIBRARY_DIR))
49+
50+
define check_rust_bindings
51+
echo "Check format and lint for rust bindings"; \
52+
cargo fmt --manifest-path $(rust_TESTS_DIR)/$(rust_CARGO_TOML) -- --check || exit 1; \
53+
cargo clippy --manifest-path $(rust_TESTS_DIR)/$(rust_CARGO_TOML) \
54+
--all-targets --all-features -- -D warnings || exit 1;
55+
endef
56+
57+
define build_rust_bindings
58+
echo "Build test programs for rust bindings"; \
59+
cargo build --manifest-path $(rust_TESTS_DIR)/$(rust_CARGO_TOML) || exit 1; \
60+
cargo install --quiet --root $(rust_TESTS_DIR) --path $(rust_TESTS_DIR) || exit 1;
61+
endef
62+
3763
check_PROGRAMS = \
3864
miniseq \
3965
01-sim-allow \
@@ -240,6 +266,14 @@ EXTRA_PROGRAMS = 00-test
240266

241267
check-build:
242268
${MAKE} ${AM_MAKEFLAGS} ${check_PROGRAMS}
269+
@if [ "$(rust_BINDINGS_TEST)" = "yes" ]; then \
270+
$(call check_rust_bindings) \
271+
$(call build_rust_bindings) \
272+
fi
243273

244274
clean-local:
245275
${RM} -f 00-test *.pyc
276+
if [ "$(rust_BINDINGS_TEST)" = "yes" ]; then \
277+
cargo clean --manifest-path $(rust_TESTS_DIR)/$(rust_CARGO_TOML); \
278+
${RM} -rf $(rust_TESTS_DIR)/bin; \
279+
fi

tests/regression

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ GLBL_SYS_RESOLVER="../tools/scmp_sys_resolver"
6262
GLBL_SYS_SIM="../tools/scmp_bpf_sim"
6363
GLBL_SYS_API="../tools/scmp_api_level"
6464

65+
RUST_TESTS_DIR="./rust"
66+
RUST_TESTS_BIN_DIR="${RUST_TESTS_DIR}/bin"
67+
6568
####
6669
# functions
6770

@@ -104,7 +107,7 @@ optional arguments:
104107
-h show this help message and exit
105108
-j JOBS run up to JOBS test jobs in parallel
106109
can also be set via LIBSECCOMP_TSTCFG_JOBS env variable
107-
-m MODE specified the test mode [c (default), python]
110+
-m MODE specified the test mode [c (default), python, rust]
108111
can also be set via LIBSECCOMP_TSTCFG_MODE_LIST env variable
109112
-a specifies all tests are to be run
110113
-b BATCH_NAME specifies batch of tests to be run
@@ -266,6 +269,8 @@ function run_test_command() {
266269
else
267270
cmd="$cmd /usr/bin/env python ${srcdir}/$2.py $3"
268271
fi
272+
elif [[ $mode == "rust" ]]; then
273+
cmd="${RUST_TESTS_BIN_DIR}/$2 $3"
269274
else
270275
cmd="$2 $3"
271276
fi
@@ -871,6 +876,16 @@ function run_test() {
871876
# generate the test number string for the line of batch test data
872877
local testnumstr=$(generate_test_num "$1" $2 1)
873878

879+
# skip tests not yet supported for Rust bindings
880+
if [[ "$mode" == "rust" ]]; then
881+
if [[ ! -e "${RUST_TESTS_DIR}/${1}.rs" ]]; then
882+
print_result $testnumstr "SKIPPED" \
883+
"(test not yet supported)"
884+
stats_skipped=$(($stats_skipped+1))
885+
return
886+
fi
887+
fi
888+
874889
# ensure we only run tests which match the specified type
875890
match_csv_word "$type" "$4"
876891
local type_match=$?
@@ -1115,6 +1130,9 @@ while getopts "ab:gj:l:m:s:t:T:vh" opt; do
11151130
verify_deps python
11161131
mode_list="$mode_list python"
11171132
;;
1133+
rust)
1134+
mode_list="$mode_list rust"
1135+
;;
11181136
*)
11191137
usage
11201138
exit 1
@@ -1160,6 +1178,10 @@ if [[ -z $mode_list ]]; then
11601178
[[ "$(grep "ENABLE_PYTHON" ../configure.h | \
11611179
awk '{ print $3 }')" = "1" ]] && \
11621180
mode_list="$mode_list python"
1181+
# rust tests
1182+
[[ "$(grep "ENABLE_RUST" ../configure.h | \
1183+
awk '{ print $3 }')" = "1" ]] && \
1184+
mode_list="$mode_list rust"
11631185
fi
11641186
fi
11651187

tests/rust/01-sim-allow.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// SPDX-License-Identifier: LGPL-2.1-only
2+
//
3+
// Copyright 2024 Sony Group Corporation
4+
//
5+
// Seccomp Library test program
6+
//
7+
8+
use anyhow::Result;
9+
use libseccomp::*;
10+
use utils::*;
11+
12+
fn main() -> Result<()> {
13+
let opts = util_getopt();
14+
let ctx = ScmpFilterContext::new(ScmpAction::Allow)?;
15+
16+
util_filter_output(&opts, &ctx)
17+
}

tests/rust/02-sim-basic.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// SPDX-License-Identifier: LGPL-2.1-only
2+
//
3+
// Copyright 2024 Sony Group Corporation
4+
//
5+
// Seccomp Library test program
6+
//
7+
8+
use anyhow::Result;
9+
use libseccomp::*;
10+
use utils::*;
11+
12+
fn main() -> Result<()> {
13+
let opts = util_getopt();
14+
let mut ctx = ScmpFilterContext::new(ScmpAction::KillThread)?;
15+
16+
ctx.add_rule_exact(ScmpAction::Allow, ScmpSyscall::from_name("read")?)?;
17+
ctx.add_rule_exact(ScmpAction::Allow, ScmpSyscall::from_name("write")?)?;
18+
ctx.add_rule_exact(ScmpAction::Allow, ScmpSyscall::from_name("close")?)?;
19+
ctx.add_rule_exact(ScmpAction::Allow, ScmpSyscall::from_name("rt_sigreturn")?)?;
20+
21+
util_filter_output(&opts, &ctx)
22+
}

tests/rust/03-sim-basic_chains.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// SPDX-License-Identifier: LGPL-2.1-only
2+
//
3+
// Copyright 2024 Sony Group Corporation
4+
//
5+
// Seccomp Library test program
6+
//
7+
8+
use anyhow::Result;
9+
use libseccomp::*;
10+
use std::io::{stderr, stdin, stdout};
11+
use std::os::unix::io::AsRawFd;
12+
use utils::*;
13+
14+
fn main() -> Result<()> {
15+
let opts = util_getopt();
16+
let mut ctx = ScmpFilterContext::new(ScmpAction::KillThread)?;
17+
18+
ctx.add_rule_conditional_exact(
19+
ScmpAction::Allow,
20+
ScmpSyscall::from_name("read")?,
21+
&[scmp_cmp!($arg0 == stdin().as_raw_fd() as u64)],
22+
)?;
23+
ctx.add_rule_conditional_exact(
24+
ScmpAction::Allow,
25+
ScmpSyscall::from_name("write")?,
26+
&[scmp_cmp!($arg0 == stdout().as_raw_fd() as u64)],
27+
)?;
28+
ctx.add_rule_conditional_exact(
29+
ScmpAction::Allow,
30+
ScmpSyscall::from_name("write")?,
31+
&[scmp_cmp!($arg0 == stderr().as_raw_fd() as u64)],
32+
)?;
33+
ctx.add_rule_exact(ScmpAction::Allow, ScmpSyscall::from_name("close")?)?;
34+
ctx.add_rule_exact(ScmpAction::Allow, ScmpSyscall::from_name("rt_sigreturn")?)?;
35+
36+
util_filter_output(&opts, &ctx)
37+
}

0 commit comments

Comments
 (0)